Coverage for lst_auto_rta/Auto_Calib.py: 0%
74 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-03 14:47 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-03 14:47 +0000
1#!/usr/bin/env python
3"""
4This script monitors the LST1 MongoDB database for new "calib_drs4" and "calib_ped_run" runs, and launches the appropriate
5calibration pipelines when new runs are detected.
7Dependencies:
8- pymongo
9"""
11import datetime
12import logging
13import os
14import time
16import pymongo
19def get_night_timestamp(today):
20 """
21 Get current timestamp (YYYYMMDD) with respect to the night.
22 It will consider a same night everything run between 08h00 until 07h59 of the next day.
24 Ex: An observation launched at 04h00 (local time) will refer to the previous day.
26 :param today: datetime object
27 datetime object with current local time
29 :return:
30 timestamp: str
31 """
32 # TODO check that today is a datetime object ?
33 if today.hour < 8: # script launched between
34 yesterday = today - datetime.timedelta(days=1)
35 timestamp = f"{yesterday.year:04d}{yesterday.month:02d}{yesterday.day:02d}"
36 else:
37 timestamp = f"{today.year:04d}{today.month:02d}{today.day:02d}"
39 return timestamp
42def get_last_specific_run(kind_of_run):
43 """
44 Query the LST1 MongoDB database for the most recent run of a specific type.
46 :param kind_of_run: str
47 The type of run to search for (e.g. "calib_drs4", "calib_ped_run")
49 :return: int
50 The run number of the most recent run of the specified type, or -1 if an error occurs or no runs of that type
51 are found.
52 """
53 try:
54 client = pymongo.MongoClient("lst101")
55 database = client["lst1_obs_summary"]
56 camera_collection = database["camera"]
57 summaries = camera_collection.find({})
58 run_table = []
59 for summary in summaries:
60 if summary["tstart"] > datetime.datetime.timestamp(datetime.datetime.now()) - 3600 * 24 * 30:
61 if summary["kind"] == kind_of_run:
62 run_table.append(summary["run_number"])
63 if run_table[-2] != run_table[-1]:
64 return run_table[-1]
65 else:
66 return run_table[-1] + 1
67 except Exception as e:
68 logging.error("Error DB !!!!!")
69 return -1
72def main():
73 """
74 Main function of the script. Monitors the LST1 MongoDB database for new "calib_drs4" and "calib_ped_run" runs, and
75 launches the appropriate calibration pipelines when new runs are detected.
76 """
78 current_run_drs4_is = -1
79 current_run_calib_is = -1
80 new_drs4 = False
81 new_ped = False
82 quit = False
84 # today = str(datetime.datetime.now().year) + str(datetime.datetime.now().month) + str(datetime.datetime.now().day)
85 today = get_night_timestamp(datetime.datetime.now())
86 logging.basicConfig(filename="log_AutoCalib_" + today + ".txt", level=logging.INFO)
87 logging.info("Start RTA Calib for the day " + today)
89 os.system("cd /fefs/onsite/pipeline/rta/data")
91 Start_time = datetime.datetime.timestamp(datetime.datetime.now())
93 while quit == False:
94 time.sleep(60)
95 logging.info(datetime.datetime.now())
96 # print(current_run_is+1)
97 if current_run_drs4_is == -1:
98 logging.info("New drs4 run " + str(get_last_specific_run("calib_drs4")))
99 current_run_drs4_is = get_last_specific_run("calib_drs4")
100 new_drs4 = True
101 if current_run_calib_is == -1:
102 logging.info("New ped run " + str(get_last_specific_run("calib_ped_run")))
103 current_run_calib_is = get_last_specific_run("calib_ped_run")
104 new_ped = True
105 if current_run_drs4_is == -1 or current_run_calib_is == -1:
106 logging.info("No Calib run, retry")
107 continue
108 # if (get_last_specific_run("calib_drs4") is None):
109 # logging.info("None")
110 # current_run_drs4_is = -1
111 # continue;
112 # if (get_last_specific_run("calib_ped_run") is None):
113 # logging.info("None")
114 # current_run_calib_is = -1
115 # continue;
116 if current_run_drs4_is != get_last_specific_run("calib_drs4"):
117 logging.info("New drs4 run " + str(get_last_specific_run("calib_drs4")))
118 current_run_drs4_is = get_last_specific_run("calib_drs4")
119 new_drs4 = True
120 time.sleep(1)
121 if current_run_calib_is != get_last_specific_run("calib_ped_run"):
122 logging.info("New ped run " + str(get_last_specific_run("calib_ped_run")))
123 current_run_calib_is = get_last_specific_run("calib_ped_run")
124 new_ped = True
125 time.sleep(1)
126 if (new_ped or new_drs4) and (current_run_drs4_is < current_run_calib_is):
127 logging.info(" Do Calib")
128 new_ped = False
129 new_drs4 = False
130 logging.info(
131 "rta_create_all_config_files --timestamp_night "
132 + today
133 + " --run_drs4 "
134 + str(current_run_drs4_is)
135 + " --run_calibration "
136 + str(current_run_calib_is)
137 + " --config_lstchain ./lstchain_standard_config_v063-RTA.json"
138 )
139 os.system(
140 "rta_create_all_config_files --timestamp_night "
141 + today
142 + " --run_drs4 "
143 + str(current_run_drs4_is)
144 + " --run_calibration "
145 + str(current_run_calib_is)
146 + " --config_lstchain ./lstchain_standard_config_v063-RTA.json "
147 )
148 time.sleep(30 * 60)
149 if datetime.datetime.timestamp(datetime.datetime.now()) - Start_time > 14 * 3600:
150 quit = True
153if __name__ == "__main__":
154 main()