Coverage for lst_auto_rta/High_Level_analysis.py: 0%
109 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-07 13:48 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-07 13:48 +0000
1#!/usr/bin/env python
3import datetime
4import logging
5import os
6import time
8import pymongo
10def wait_for_directory(directory, interval=5):
11 """
12 Wait for the directory to exist
14 :param directory: directory path
15 :param interval: Time interval between verification
16 """
17 while not os.path.exists(directory):
18 print(f"{directory} does not exist yet. let's wait {interval} seconds...")
19 time.sleep(interval)
20 print(f"The directory {directory} finally exist !!!")
23def now():
24 """
25 Returns the current timestamp in seconds, relative to the Unix epoch.
27 :return: float
28 """
29 return datetime.datetime.timestamp(datetime.datetime.now())
32def get_current_run():
33 """
34 Attempts to retrieve the current run number from the lst1_obs_summary database.
36 :return: int
37 """
38 try:
39 client = pymongo.MongoClient("lst101")
40 database = client["lst1_obs_summary"]
41 camera_collection = database["camera"]
42 summaries = camera_collection.find({})
43 run_table = []
44 summaries = camera_collection.find({})
45 for summary in summaries:
46 if (
47 summary["tstart"]
48 > datetime.datetime.timestamp(datetime.datetime.now()) - 3600 * 24 * 100
49 ):
50 run_table.append(summary["run_number"])
51 if run_table[-2] != run_table[-1]:
52 return run_table[-1]
53 else:
54 return run_table[-1] + 1
55 except Exception as e:
56 logging.error("Error DB !!!!!")
57 return -1
60def get_night_timestamp(today):
61 """
62 Get current timestamp (YYYYMMDD) with respect to the night.
63 It will consider a same night everything run between 08h00 until 07h59 of the next day.
65 Ex: An observation launched at 04h00 (local time) will refer to the previous day.
67 :param today: datetime object
68 datetime object with current local time
70 :return:
71 timestamp: str
72 """
73 # TODO check that today is a datetime object ?
74 if today.hour < 8: # script launched between
75 yesterday = today - datetime.timedelta(days=1)
76 timestamp = f"{yesterday.year:04d}{yesterday.month:02d}{yesterday.day:02d}"
77 else:
78 timestamp = f"{today.year:04d}{today.month:02d}{today.day:02d}"
80 return timestamp
83def Obtain_RA_DEC(runid):
84 try:
85 client = pymongo.MongoClient("lst101")
86 database = client["bridgesmonitoring"]
87 print(client)
89 client = pymongo.MongoClient("lst101")
90 database = client["lst1_obs_summary"]
91 camera_collection = database["telescope"]
92 summaries = camera_collection.find({"data.camera.run_number": runid})
94 for summary in summaries:
95 camera = summary["data"]["camera"]
96 count = 0
97 structure = summary["data"]["structure"]
98 for c in camera:
99 if c["run_number"] == runid:
100 run_count = count
101 count = count + 1
102 run_parameters = structure[run_count]
103 return (
104 run_parameters["target"]["ra"],
105 run_parameters["target"]["dec"],
106 run_parameters["target"]["source_ra"],
107 run_parameters["target"]["source_dec"],
108 )
109 except Exception as e:
110 # client = pymongo.MongoClient('lst101')
111 # database = client['lst1_obs_summary']
112 # camera_collection = database['camera']
113 # summaries = camera_collection.find({})
114 # for summary in summaries:
115 # if (summary['tstart'] > datetime.datetime.timestamp(datetime.datetime.now())-3600*24*100):
116 # if (summary['run_number']==runid and summary["kind"] != "data_taking"):
117 # return [0,0,0,0]
118 logging.error("Error DB !!!!!")
119 return [0, 0, 0, 0]
121def today_to_directory(today):
122 return today[0:4]+"/"+today[4:6]+"/"+today[6:8]
124def main():
125 """
126 Initialize current_run_is and quit variables and get the current timestamp with Start_time = now()
127 Initialize the logging system by setting the log file name and logging level
128 Change the current working directory to /fefs/onsite/pipeline/rta/data
129 Find the current reservations for nodes using the scontrol command and parse the output to find the individual node names
130 Modify a configuration file with sed commands, replacing placeholders with the node names found in step 4
131 Check if the ib0 network interface is in connected mode for each of the nodes and log the results
132 """
133 current_run_is = -1
134 quit = False
136 Start_time = now()
138 # today = str(datetime.datetime.now().year) + str(datetime.datetime.now().month) + str(datetime.datetime.now().day)
139 today = get_night_timestamp(datetime.datetime.now())
141 wait_for_directory("/fefs/onsite/pipeline/rta/data/"+today_to_directory(today))
142 logging.basicConfig(filename="/fefs/onsite/pipeline/rta/data/"+today_to_directory(today)+"/log_High_Level_" + today + ".txt", level=logging.INFO)
143 logging.info("Start RTA High Level for the day " + today)
145 RTA_ready = True
147 loop_id = 0
148 ra_dec = [0, 0, 0, 0]
149 data_directory = "/fefs/onsite/pipeline/rta/data/"
150 while quit == False and RTA_ready:
151 time.sleep(10)
152 # print(current_run_is+1)
153 if current_run_is == -1:
154 current_run_is = get_current_run()
155 if current_run_is == -1:
156 logging.info("None")
157 current_run_is = -1
158 continue
159 if current_run_is != get_current_run():
160 time.sleep(1)
161 current_run_is = get_current_run()
162 if current_run_is != -1:
163 logging.info("Start RTA check for run " + str(current_run_is))
164 time.sleep(2)
165 ra_dec = Obtain_RA_DEC(current_run_is + 1)
166 logging.info(
167 # ~ "srun --reservation=rta_one_node ./Theta_square.py -da "
168 "./Theta_square.py -da "
169 + today_to_directory(today)
170 + " -r "
171 + str(current_run_is + 1)
172 + " -RA "
173 + str(ra_dec[2])
174 + " -DEC "
175 + str(ra_dec[3])
176 )
177 os.system(
178 # ~ "srun --reservation=rta_one_node ./Theta_square.py -da "
179 "./Theta_square.py -da "
180 + today_to_directory(today)
181 + " -r "
182 + str(current_run_is + 1)
183 + " -RA "
184 + str(ra_dec[2])
185 + " -DEC "
186 + str(ra_dec[3])
187 )
188 logging.info(
189 # ~ "srun --reservation=rta_one_node ./Spectra.py -da "
190 "./Spectra.py -da "
191 + today_to_directory(today)
192 + " -r "
193 + str(current_run_is + 1)
194 + " -RA "
195 + str(ra_dec[2])
196 + " -DEC "
197 + str(ra_dec[3])
198 )
199 os.system(
200 # ~ "srun --reservation=rta_one_node ./Spectra.py -da "
201 "./Spectra.py -da "
202 + today_to_directory(today)
203 + " -r "
204 + str(current_run_is + 1)
205 + " -RA "
206 + str(ra_dec[2])
207 + " -DEC "
208 + str(ra_dec[3])
209 )
210 logging.info(
211 # ~ "srun --reservation=rta_one_node ./rta_var.py --dl3_folder "
212 "./rta_var.py --dl3_folder "
213 + data_directory
214 + today_to_directory(today)
215 + "/"
216 + str(current_run_is + 1)
217 + "/DL3 -ra "
218 + str(ra_dec[2])
219 + " -dec "
220 + str(ra_dec[3])
221 + " --output_dir "+ data_directory
222 + today_to_directory(today)
223 + "/"
224 + str(current_run_is + 1)
225 + "/plots --date "
226 + today
227 )
228 os.system(
229 # ~ "srun --reservation=rta_one_node ./rta_var.py --dl3_folder "
230 "./rta_var.py --dl3_folder "
231 + data_directory
232 + today_to_directory(today)
233 + "/"
234 + str(current_run_is + 1)
235 + "/DL3 -ra "
236 + str(ra_dec[2])
237 + " -dec "
238 + str(ra_dec[3])
239 + " --output_dir " + data_directory
240 + today_to_directory(today)
241 + "/"
242 + str(current_run_is + 1)
243 + "/plots --date "
244 + today
245 )
247 if loop_id % 1 == 0:
248 logging.info("Start RTA check for run " + str(current_run_is))
249 time.sleep(2)
250 logging.info(datetime.datetime.now())
251 ra_dec = Obtain_RA_DEC(current_run_is + 1)
252 logging.info(
253 "./Theta_square.py -da "
254 + today_to_directory(today)
255 + " -r "
256 + str(current_run_is + 1)
257 + " -RA "
258 + str(ra_dec[2])
259 + " -DEC "
260 + str(ra_dec[3])
261 )
262 os.system(
263 "./Theta_square.py -da "
264 + today_to_directory(today)
265 + " -r "
266 + str(current_run_is + 1)
267 + " -RA "
268 + str(ra_dec[2])
269 + " -DEC "
270 + str(ra_dec[3])
271 )
272 logging.info(
273 "./Spectra.py -da "
274 + today_to_directory(today)
275 + " -r "
276 + str(current_run_is + 1)
277 + " -RA "
278 + str(ra_dec[2])
279 + " -DEC "
280 + str(ra_dec[3])
281 )
282 os.system(
283 "./Spectra.py -da "
284 + today_to_directory(today)
285 + " -r "
286 + str(current_run_is + 1)
287 + " -RA "
288 + str(ra_dec[2])
289 + " -DEC "
290 + str(ra_dec[3])
291 )
292 logging.info(
293 "./rta_var.py --dl3_folder "+ data_directory
294 + today_to_directory(today)
295 + "/"
296 + str(current_run_is + 1)
297 + "/DL3 -ra "
298 + str(ra_dec[2])
299 + " -dec "
300 + str(ra_dec[3])
301 + " --output_dir "+ data_directory
302 + today_to_directory(today)
303 + "/"
304 + str(current_run_is + 1)
305 + "/plots --date "
306 + today
307 )
308 os.system(
309 "./rta_var.py --dl3_folder "+ data_directory
310 + today_to_directory(today)
311 + "/"
312 + str(current_run_is + 1)
313 + "/DL3 -ra "
314 + str(ra_dec[2])
315 + " -dec "
316 + str(ra_dec[3])
317 + " --output_dir "+ data_directory
318 + today_to_directory(today)
319 + "/"
320 + str(current_run_is + 1)
321 + "/plots --date "
322 + today
323 )
325 loop_id = loop_id + 1
326 if (
327 datetime.datetime.timestamp(datetime.datetime.now()) - Start_time
328 ) > 14 * 3600:
329 quit = True
330 logging.info("End of the night, Stop the RTA check")
333if __name__ == "__main__":
334 main()