Coverage for src / lstautorta / Auto_Calib.py: 0%
74 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-10 11:56 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-10 11:56 +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 return run_table[-1] + 1
66 except Exception:
67 logging.exception("Error DB !!!!!")
68 return -1
71def main():
72 """
73 Main function of the script. Monitors the LST1 MongoDB database for new "calib_drs4" and "calib_ped_run" runs, and
74 launches the appropriate calibration pipelines when new runs are detected.
75 """
76 current_run_drs4_is = -1
77 current_run_calib_is = -1
78 new_drs4 = False
79 new_ped = False
80 quit = False
82 # today = str(datetime.datetime.now().year) + str(datetime.datetime.now().month) + str(datetime.datetime.now().day)
83 today = get_night_timestamp(datetime.datetime.now())
84 logging.basicConfig(filename="log_AutoCalib_" + today + ".txt", level=logging.INFO)
85 logging.info("Start RTA Calib for the day " + today)
87 os.system("cd /fefs/onsite/pipeline/rta/data")
89 Start_time = datetime.datetime.timestamp(datetime.datetime.now())
91 while quit == False:
92 time.sleep(60)
93 logging.info(datetime.datetime.now())
94 # print(current_run_is+1)
95 if current_run_drs4_is == -1:
96 logging.info("New drs4 run " + str(get_last_specific_run("calib_drs4")))
97 current_run_drs4_is = get_last_specific_run("calib_drs4")
98 new_drs4 = True
99 if current_run_calib_is == -1:
100 logging.info("New ped run " + str(get_last_specific_run("calib_ped_run")))
101 current_run_calib_is = get_last_specific_run("calib_ped_run")
102 new_ped = True
103 if current_run_drs4_is == -1 or current_run_calib_is == -1:
104 logging.info("No Calib run, retry")
105 continue
106 # if (get_last_specific_run("calib_drs4") is None):
107 # logging.info("None")
108 # current_run_drs4_is = -1
109 # continue;
110 # if (get_last_specific_run("calib_ped_run") is None):
111 # logging.info("None")
112 # current_run_calib_is = -1
113 # continue;
114 if current_run_drs4_is != get_last_specific_run("calib_drs4"):
115 logging.info("New drs4 run " + str(get_last_specific_run("calib_drs4")))
116 current_run_drs4_is = get_last_specific_run("calib_drs4")
117 new_drs4 = True
118 time.sleep(1)
119 if current_run_calib_is != get_last_specific_run("calib_ped_run"):
120 logging.info("New ped run " + str(get_last_specific_run("calib_ped_run")))
121 current_run_calib_is = get_last_specific_run("calib_ped_run")
122 new_ped = True
123 time.sleep(1)
124 if (new_ped or new_drs4) and (current_run_drs4_is < current_run_calib_is):
125 logging.info(" Do Calib")
126 new_ped = False
127 new_drs4 = False
128 logging.info(
129 "rta_create_all_config_files --timestamp_night "
130 + today
131 + " --run_drs4 "
132 + str(current_run_drs4_is)
133 + " --run_calibration "
134 + str(current_run_calib_is)
135 + " --config_lstchain ./lstchain_standard_config_v063-RTA.json"
136 )
137 os.system(
138 "rta_create_all_config_files --timestamp_night "
139 + today
140 + " --run_drs4 "
141 + str(current_run_drs4_is)
142 + " --run_calibration "
143 + str(current_run_calib_is)
144 + " --config_lstchain ./lstchain_standard_config_v063-RTA.json "
145 )
146 time.sleep(30 * 60)
147 if datetime.datetime.timestamp(datetime.datetime.now()) - Start_time > 14 * 3600:
148 quit = True
151if __name__ == "__main__":
152 main()