-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart_run_checker.py
executable file
·76 lines (67 loc) · 3.23 KB
/
start_run_checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
import os
import glob
import sys
import datetime
import subprocess
from time import sleep
LAST_PROCESSED_RUN = 0 # just for initialization
TIME_BETWEEN_CHECKS = 60 # time in s
STATUS_FILE = '/SNS/MANDI/shared/autoreduce/autoreduce_status.txt'
LAST_RUN_FILE = '/SNS/MANDI/shared/autoreduce/last_run_processed.dat'
def read_last_processed_run(fname=LAST_RUN_FILE):
with open(fname, 'r') as f:
ll = f.readline()
return int(ll)
def write_last_processed_run(runNumber, fname=LAST_RUN_FILE):
with open(fname, 'w') as f:
lWrite = f.write(str(runNumber)) # noqa: F841
# Initialization
try:
LAST_PROCESSED_RUN = read_last_processed_run()
except:
raise UserWarning("Cannot read last_run_processed.dat"
" to know which run to process")
NEXT_RUN_TO_PROCESS = LAST_PROCESSED_RUN + 1
with open(STATUS_FILE, 'r') as f:
_ = f.readline()
ipts_number = int(f.readline())
while True:
LAST_PROCESSED_RUN = read_last_processed_run()
NEXT_RUN_TO_PROCESS = LAST_PROCESSED_RUN + 1
print('{2}: Last processed run is {0}, looking for run {1}'.format(
LAST_PROCESSED_RUN, NEXT_RUN_TO_PROCESS, str(datetime.datetime.now())))
nxs_filename = '/SNS/MANDI/IPTS-{0}/nexus/MANDI_{1}.nxs.h5'.format(ipts_number, NEXT_RUN_TO_PROCESS)
try:
file_exists = os.path.isfile(nxs_filename)
if not file_exists:
sleep(TIME_BETWEEN_CHECKS)
else: # we have the file
outputdir = '/SNS/MANDI/IPTS-{0}/shared/autoreduce/'.format(ipts_number)
python_command = '/SNS/users/ntv/workspace/mantid/release/bin/mantidpython'
script_name = '/SNS/MANDI/shared/autoreduce/mandi_singlerun.py'
mtz_script = '/SNS/MANDI/shared/autoreduce/mandi_createmtz.py'
config_filename = '/SNS/MANDI/IPTS-{0}/shared/autoreduce/mandi_autoreduce.config'.format(ipts_number)
output_dir = outputdir
run_number = NEXT_RUN_TO_PROCESS
config_file_list = glob.glob(config_filename)
if len(config_file_list) != 1:
raise UserWarning("Config file {0} does not exist. Cannot do profile fitting.".format(config_filename))
else: # We have a config file we can use
command_pf = python_command + ' ' + script_name + ' ' + \
config_filename + ' ' + nxs_filename + ' ' + \
output_dir + ' ' + str(run_number)
command_mtz = python_command + ' ' + mtz_script + ' ' + config_filename + ' ' + output_dir + ' ' + str(run_number)
with open(STATUS_FILE, 'w') as f:
f.write('Processing run {0}\n{1}\n'.format(NEXT_RUN_TO_PROCESS, ipts_number))
subprocess.call(command_pf.split(' ')) # Does the profile fitting
subprocess.call(command_mtz.split(' '))
with open(STATUS_FILE, 'w') as f:
f.write('Finished processing run {0}. Waiting...\n{1}\n'.format(NEXT_RUN_TO_PROCESS, ipts_number))
print(command_pf)
print(command_mtz)
write_last_processed_run(NEXT_RUN_TO_PROCESS)
LAST_PROCESSED_RUN += 1
NEXT_RUN_TO_PROCESS += 1
except KeyboardInterrupt:
sys.exit(1)