-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve_inspection.py
executable file
·210 lines (171 loc) · 8.12 KB
/
retrieve_inspection.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/p/project/chtb00/htb006/software_new/envs/surveys/bin/python3
# -*- coding: utf-8 -*-
"""
Surveys KSP monitoring script -- see https://github.com/adrabent/JURECA_tools
"""
import argparse, logging
import os, sys, shutil
import subprocess, time
from surveys_utils import *
_version = '1.0' ## program version
cal_dir = 'gsiftp://gridftp.grid.sara.nl:2811/pnfs/grid.sara.nl/data/lofar/user/sksp/diskonly/pipelines/SKSP/prefactor_v3.0/pref_cal'
targ_dir = 'gsiftp://gridftp.grid.sara.nl:2811/pnfs/grid.sara.nl/data/lofar/user/sksp/archive/SKSP_Spider_Pref3'
cal_prefix = 'pref3_cal_'
def add_coloring_to_emit_ansi(fn):
def new(*args):
levelno = args[0].levelno
if(levelno>=50):
color = '\x1b[31m' # red
elif(levelno>=40):
color = '\x1b[31m' # red
elif(levelno>=30):
color = '\x1b[33m' # yellow
elif(levelno>=20):
color = '\x1b[32m' # green
elif(levelno>=10):
color = '\x1b[35m' # pink
else:
color = '\x1b[0m' # normal
args[0].msg = color + args[0].msg + '\x1b[0m' # normal
return fn(*args)
return new
def my_handler(type, value, tb):
exception = logging.critical("{0}".format(str(value)))
lock = os.environ['SCRATCH_chtb00'] + '/htb006' '/.lock'
if os.path.exists(lock):
os.remove(lock)
time.sleep(300)
def get_calibrator(cal_obsid, field_name, cal_results_dir, working_directory):
logging.info('Checking calibrator observation: \033[35mL' + cal_obsid)
cal_solution = cal_results_dir + '/L' + cal_obsid + '/' + cal_prefix + 'L' + cal_obsid + '.tar'
existence = subprocess.Popen(['uberftp', '-ls', cal_solution], env = {'GLOBUS_GSSAPI_MAX_TLS_PROTOCOL' : 'TLS1_2_VERSION'})
errorcode = existence.wait()
if errorcode == 0:
logging.info('Found calibrator results for this field from: \033[35m' + cal_solution)
else:
cal_solution = cal_results_dir + '/Spider/' + cal_prefix + 'L' + cal_obsid + '.tar'
existence = subprocess.Popen(['uberftp', '-ls', cal_solution], env = {'GLOBUS_GSSAPI_MAX_TLS_PROTOCOL' : 'TLS1_2_VERSION'})
errorcode = existence.wait()
if errorcode == 0:
logging.info('Found calibrator results for this field from: \033[35m' + cal_solution)
else:
logging.warning('Could not find any calibrator results for this field in: \033[35m' + cal_solution)
return (True, False)
filename = working_directory + '/' + os.path.basename(cal_solution)
transfer = subprocess.Popen(['globus-url-copy', cal_solution, 'file://' + filename], stdout=subprocess.PIPE)
errorcode = transfer.wait()
if errorcode != 0:
logging.error('\033[31mDownloading calibrator results has failed.')
return (False, True)
os.chdir(working_directory)
logging.info('Unpacking calibrator results from: \033[35m' + filename)
unpack = subprocess.Popen(['tar', 'xfv', filename, '-C', working_directory], stdout=subprocess.PIPE)
errorcode = unpack.wait()
if errorcode != 0:
logging.error('\033[31m Unpacking calibrator results has failed.')
return (False, True)
os.remove(filename)
logging.info('File \033[35m' + filename + '\033[32m was removed.')
return (False, False)
def get_calibrator(cal_obsid, cal_results_dir, working_directory):
home_directory = os.environ['PROJECT_chtb00'] + '/htb006'
logging.info('Checking calibrator observation: \033[35mL' + cal_obsid)
cal_solution = cal_results_dir + '/L' + cal_obsid + '/' + cal_prefix + 'L' + cal_obsid + '.tar'
existence = subprocess.Popen(['uberftp', '-ls', cal_solution], env = {'GLOBUS_GSSAPI_MAX_TLS_PROTOCOL' : 'TLS1_2_VERSION'})
errorcode = existence.wait()
if errorcode == 0:
logging.info('Found calibrator results for this field from: \033[35m' + cal_solution)
else:
cal_solution = cal_results_dir + '/Spider/' + cal_prefix + 'L' + cal_obsid + '.tar'
existence = subprocess.Popen(['uberftp', '-ls', cal_solution], env = {'GLOBUS_GSSAPI_MAX_TLS_PROTOCOL' : 'TLS1_2_VERSION'})
errorcode = existence.wait()
if errorcode == 0:
logging.info('Found calibrator results for this field from: \033[35m' + cal_solution)
else:
logging.warning('Could not find any calibrator results for this field in: \033[35m' + cal_solution)
return False
#filename = working_directory + '/' + os.path.basename(cal_solution)
filename = os.environ['HOME'] + '/' + os.path.basename(cal_solution)
transfer = subprocess.Popen(['singularity', 'exec', home_directory + '/lta-client.sif', 'globus-url-copy', cal_solution, 'file://' + filename], stdout=subprocess.PIPE)
errorcode = transfer.wait()
if errorcode != 0:
logging.error('\033[31mDownloading calibrator results has failed.')
return False
os.chdir(working_directory)
logging.info('Unpacking calibrator results from: \033[35m' + filename)
unpack = subprocess.Popen(['tar', 'xfv', filename, '-C', working_directory], stdout=subprocess.PIPE)
errorcode = unpack.wait()
if errorcode != 0:
logging.error('\033[31m Unpacking calibrator results has failed.')
return False
os.remove(filename)
logging.info('File \033[35m' + filename + '\033[32m was removed.')
return True
def get_target(targ_obsid, cal_results_dir, working_directory):
home_directory = os.environ['PROJECT_chtb00'] + '/htb006'
logging.info('Checking target observation: \033[35mL' + targ_obsid)
cal_solution = cal_results_dir + '/L' + targ_obsid + '/inspection.tar'
existence = subprocess.Popen(['uberftp', '-ls', cal_solution], env = {'GLOBUS_GSSAPI_MAX_TLS_PROTOCOL' : 'TLS1_2_VERSION'})
errorcode = existence.wait()
if errorcode == 0:
logging.info('Found target results for this field from: \033[35m' + cal_solution)
else:
logging.warning('Could not find any target results for this field in: \033[35m' + cal_solution)
return False
#filename = working_directory + '/' + os.path.basename(cal_solution)
filename = os.environ['HOME'] + '/' + os.path.basename(cal_solution)
transfer = subprocess.Popen(['singularity', 'exec', home_directory + '/lta-client.sif', 'globus-url-copy', cal_solution, 'file://' + filename], stdout=subprocess.PIPE)
errorcode = transfer.wait()
if errorcode != 0:
logging.error('\033[31mDownloading target results has failed.')
return False
os.chdir(working_directory)
logging.info('Unpacking target results from: \033[35m' + filename)
unpack = subprocess.Popen(['tar', 'xfv', filename, '-C', working_directory], stdout=subprocess.PIPE)
errorcode = unpack.wait()
if errorcode != 0:
logging.error('\033[31m Unpacking target results has failed.')
return False
os.remove(filename)
logging.info('File \033[35m' + filename + '\033[32m was removed.')
return True
def main(working_directory = os.environ['SCRATCH_chtb00'] + '/htb006', field_id = None):
## load working environment
if not working_directory or not os.path.exists(working_directory):
logging.warning('No working directory was specified or could be find')
#os.remove(working_directory + '/.submitted')
if not field_id:
logging.error('No field ID specified.')
return False
try:
field_name = field_id.split('_')[0]
targ_obsid = field_id.split('_')[1].lstrip('L')
except:
logging.error('Provided field ID is invalid.')
return False
field = get_one_observation(field_name, targ_obsid)
try:
cal_obsid = str(field['calibrator_id'])
except TypeError:
cal_obsid = targ_obsid
error = get_calibrator(cal_obsid, cal_dir, working_directory)
error = get_target(targ_obsid, targ_dir, working_directory)
return error
if __name__=='__main__':
# Get command-line options.
parser = argparse.ArgumentParser(description='Reset failed field in LoTSS MySQL database')
parser.add_argument('field_id', type=str, default = None, help='Specify field ID to retrieve diagnostics from.')
parser.add_argument('--workdir', type=str, default = os.environ['SCRATCH_chtb00'] + '/htb006', help='Specify working directory to be reset.')
args = parser.parse_args()
format_stream = logging.Formatter("%(asctime)s\033[1m %(levelname)s:\033[0m %(message)s","%Y-%m-%d %H:%M:%S")
format_file = logging.Formatter("%(asctime)s %(levelname)s: %(message)s","%Y-%m-%d %H:%M:%S")
logging.root.setLevel(logging.INFO)
log = logging.StreamHandler()
log.setFormatter(format_stream)
log.emit = add_coloring_to_emit_ansi(log.emit)
logging.root.addHandler(log)
# install exception handler
sys.excepthook = my_handler
# start running script
main(args.workdir, args.field_id)
sys.exit(0)