#!/usr/bin/env python
"""
This script monitors the LST1 MongoDB database for new "calib_drs4" and "calib_ped_run" runs, and launches the appropriate
calibration pipelines when new runs are detected.
Dependencies:
- pymongo
"""
import datetime
import logging
import os
import time
import pymongo
[docs]
def get_night_timestamp(today):
"""
Get current timestamp (YYYYMMDD) with respect to the night.
It will consider a same night everything run between 08h00 until 07h59 of the next day.
Ex: An observation launched at 04h00 (local time) will refer to the previous day.
:param today: datetime object
datetime object with current local time
:return:
timestamp: str
"""
# TODO check that today is a datetime object ?
if today.hour < 8: # script launched between
yesterday = today - datetime.timedelta(days=1)
timestamp = f"{yesterday.year:04d}{yesterday.month:02d}{yesterday.day:02d}"
else:
timestamp = f"{today.year:04d}{today.month:02d}{today.day:02d}"
return timestamp
[docs]
def get_last_specific_run(kind_of_run):
"""
Query the LST1 MongoDB database for the most recent run of a specific type.
:param kind_of_run: str
The type of run to search for (e.g. "calib_drs4", "calib_ped_run")
:return: int
The run number of the most recent run of the specified type, or -1 if an error occurs or no runs of that type
are found.
"""
try:
client = pymongo.MongoClient("lst101")
database = client["lst1_obs_summary"]
camera_collection = database["camera"]
summaries = camera_collection.find({})
run_table = []
for summary in summaries:
if summary["tstart"] > datetime.datetime.timestamp(datetime.datetime.now()) - 3600 * 24 * 30:
if summary["kind"] == kind_of_run:
run_table.append(summary["run_number"])
if run_table[-2] != run_table[-1]:
return run_table[-1]
else:
return run_table[-1] + 1
except Exception as e:
logging.error("Error DB !!!!!")
return -1
[docs]
def main():
"""
Main function of the script. Monitors the LST1 MongoDB database for new "calib_drs4" and "calib_ped_run" runs, and
launches the appropriate calibration pipelines when new runs are detected.
"""
current_run_drs4_is = -1
current_run_calib_is = -1
new_drs4 = False
new_ped = False
quit = False
# today = str(datetime.datetime.now().year) + str(datetime.datetime.now().month) + str(datetime.datetime.now().day)
today = get_night_timestamp(datetime.datetime.now())
logging.basicConfig(filename="log_AutoCalib_" + today + ".txt", level=logging.INFO)
logging.info("Start RTA Calib for the day " + today)
os.system("cd /fefs/onsite/pipeline/rta/data")
Start_time = datetime.datetime.timestamp(datetime.datetime.now())
while quit == False:
time.sleep(60)
logging.info(datetime.datetime.now())
# print(current_run_is+1)
if current_run_drs4_is == -1:
logging.info("New drs4 run " + str(get_last_specific_run("calib_drs4")))
current_run_drs4_is = get_last_specific_run("calib_drs4")
new_drs4 = True
if current_run_calib_is == -1:
logging.info("New ped run " + str(get_last_specific_run("calib_ped_run")))
current_run_calib_is = get_last_specific_run("calib_ped_run")
new_ped = True
if current_run_drs4_is == -1 or current_run_calib_is == -1:
logging.info("No Calib run, retry")
continue
# if (get_last_specific_run("calib_drs4") is None):
# logging.info("None")
# current_run_drs4_is = -1
# continue;
# if (get_last_specific_run("calib_ped_run") is None):
# logging.info("None")
# current_run_calib_is = -1
# continue;
if current_run_drs4_is != get_last_specific_run("calib_drs4"):
logging.info("New drs4 run " + str(get_last_specific_run("calib_drs4")))
current_run_drs4_is = get_last_specific_run("calib_drs4")
new_drs4 = True
time.sleep(1)
if current_run_calib_is != get_last_specific_run("calib_ped_run"):
logging.info("New ped run " + str(get_last_specific_run("calib_ped_run")))
current_run_calib_is = get_last_specific_run("calib_ped_run")
new_ped = True
time.sleep(1)
if (new_ped or new_drs4) and (current_run_drs4_is < current_run_calib_is):
logging.info(" Do Calib")
new_ped = False
new_drs4 = False
logging.info(
"rta_create_all_config_files --timestamp_night "
+ today
+ " --run_drs4 "
+ str(current_run_drs4_is)
+ " --run_calibration "
+ str(current_run_calib_is)
+ " --config_lstchain ./lstchain_standard_config_v063-RTA.json"
)
os.system(
"rta_create_all_config_files --timestamp_night "
+ today
+ " --run_drs4 "
+ str(current_run_drs4_is)
+ " --run_calibration "
+ str(current_run_calib_is)
+ " --config_lstchain ./lstchain_standard_config_v063-RTA.json "
)
time.sleep(30 * 60)
if datetime.datetime.timestamp(datetime.datetime.now()) - Start_time > 14 * 3600:
quit = True
if __name__ == "__main__":
main()