#!/usr/bin/env python
import datetime
import logging
import os
import time
import pymongo
[docs]
def now():
"""
Returns the current timestamp in seconds, relative to the Unix epoch.
:return: float
"""
return datetime.datetime.timestamp(datetime.datetime.now())
[docs]
def get_current_run():
"""
Attempts to retrieve the current run number from the lst1_obs_summary database.
:return: int
"""
try:
client = pymongo.MongoClient("lst101")
database = client["lst1_obs_summary"]
camera_collection = database["camera"]
summaries = camera_collection.find({})
run_table = []
summaries = camera_collection.find({})
for summary in summaries:
if (
summary["tstart"]
> datetime.datetime.timestamp(datetime.datetime.now()) - 3600 * 24 * 100
):
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 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 Obtain_RA_DEC(runid):
try:
client = pymongo.MongoClient("lst101")
database = client["bridgesmonitoring"]
print(client)
client = pymongo.MongoClient("lst101")
database = client["lst1_obs_summary"]
camera_collection = database["telescope"]
summaries = camera_collection.find({"data.camera.run_number": runid})
for summary in summaries:
camera = summary["data"]["camera"]
count = 0
structure = summary["data"]["structure"]
for c in camera:
if c["run_number"] == runid:
run_count = count
count = count + 1
run_parameters = structure[run_count]
return (
run_parameters["target"]["ra"],
run_parameters["target"]["dec"],
run_parameters["target"]["source_ra"],
run_parameters["target"]["source_dec"],
)
except Exception as e:
# client = pymongo.MongoClient('lst101')
# database = client['lst1_obs_summary']
# camera_collection = database['camera']
# summaries = camera_collection.find({})
# for summary in summaries:
# if (summary['tstart'] > datetime.datetime.timestamp(datetime.datetime.now())-3600*24*100):
# if (summary['run_number']==runid and summary["kind"] != "data_taking"):
# return [0,0,0,0]
logging.error("Error DB !!!!!")
return [0, 0, 0, 0]
[docs]
def today_to_directory(today):
return today[0:4]+"/"+today[4:6]+"/"+today[6:8]
[docs]
def main():
"""
Initialize current_run_is and quit variables and get the current timestamp with Start_time = now()
Initialize the logging system by setting the log file name and logging level
Change the current working directory to /fefs/onsite/pipeline/rta/data
Find the current reservations for nodes using the scontrol command and parse the output to find the individual node names
Modify a configuration file with sed commands, replacing placeholders with the node names found in step 4
Check if the ib0 network interface is in connected mode for each of the nodes and log the results
"""
current_run_is = -1
quit = False
Start_time = now()
# 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_High_Level_" + today + ".txt", level=logging.INFO)
logging.info("Start RTA High Level for the day " + today)
RTA_ready = True
loop_id = 0
ra_dec = [0, 0, 0, 0]
data_directory = "/fefs/onsite/pipeline/rta/data/"
while quit == False and RTA_ready:
time.sleep(10)
# print(current_run_is+1)
if current_run_is == -1:
current_run_is = get_current_run()
if current_run_is == -1:
logging.info("None")
current_run_is = -1
continue
if current_run_is != get_current_run():
time.sleep(1)
current_run_is = get_current_run()
if current_run_is != -1:
logging.info("Start RTA check for run " + str(current_run_is))
time.sleep(2)
ra_dec = Obtain_RA_DEC(current_run_is + 1)
logging.info(
"srun --reservation=rta_one_node ./Theta_square.py -da "
+ today_to_directory(today)
+ " -r "
+ str(current_run_is + 1)
+ " -RA "
+ str(ra_dec[2])
+ " -DEC "
+ str(ra_dec[3])
)
os.system(
"srun --reservation=rta_one_node ./Theta_square.py -da "
+ today_to_directory(today)
+ " -r "
+ str(current_run_is + 1)
+ " -RA "
+ str(ra_dec[2])
+ " -DEC "
+ str(ra_dec[3])
)
logging.info(
"srun --reservation=rta_one_node ./Spectra.py -da "
+ today_to_directory(today)
+ " -r "
+ str(current_run_is + 1)
+ " -RA "
+ str(ra_dec[2])
+ " -DEC "
+ str(ra_dec[3])
)
os.system(
"srun --reservation=rta_one_node ./Spectra.py -da "
+ today_to_directory(today)
+ " -r "
+ str(current_run_is + 1)
+ " -RA "
+ str(ra_dec[2])
+ " -DEC "
+ str(ra_dec[3])
)
logging.info(
"srun --reservation=rta_one_node ./rta_var.py --dl3_folder " + data_directory
+ today_to_directory(today)
+ "/"
+ str(current_run_is + 1)
+ "/DL3 -ra "
+ str(ra_dec[2])
+ " -dec "
+ str(ra_dec[3])
+ " --output_dir "+ data_directory
+ today_to_directory(today)
+ "/"
+ str(current_run_is + 1)
+ "/plots --date "
+ today
)
os.system(
"srun --reservation=rta_one_node ./rta_var.py --dl3_folder " + data_directory
+ today_to_directory(today)
+ "/"
+ str(current_run_is + 1)
+ "/DL3 -ra "
+ str(ra_dec[2])
+ " -dec "
+ str(ra_dec[3])
+ " --output_dir " + data_directory
+ today_to_directory(today)
+ "/"
+ str(current_run_is + 1)
+ "/plots --date "
+ today
)
if loop_id % 1 == 0:
logging.info("Start RTA check for run " + str(current_run_is))
time.sleep(2)
logging.info(datetime.datetime.now())
ra_dec = Obtain_RA_DEC(current_run_is + 1)
logging.info(
"srun --reservation=rta_one_node ./Theta_square.py -da "
+ today_to_directory(today)
+ " -r "
+ str(current_run_is + 1)
+ " -RA "
+ str(ra_dec[2])
+ " -DEC "
+ str(ra_dec[3])
)
os.system(
"srun --reservation=rta_one_node ./Theta_square.py -da "
+ today_to_directory(today)
+ " -r "
+ str(current_run_is + 1)
+ " -RA "
+ str(ra_dec[2])
+ " -DEC "
+ str(ra_dec[3])
)
logging.info(
"srun --reservation=rta_one_node ./Spectra.py -da "
+ today_to_directory(today)
+ " -r "
+ str(current_run_is + 1)
+ " -RA "
+ str(ra_dec[2])
+ " -DEC "
+ str(ra_dec[3])
)
os.system(
"srun --reservation=rta_one_node ./Spectra.py -da "
+ today_to_directory(today)
+ " -r "
+ str(current_run_is + 1)
+ " -RA "
+ str(ra_dec[2])
+ " -DEC "
+ str(ra_dec[3])
)
logging.info(
"srun --reservation=rta_one_node ./rta_var.py --dl3_folder "+ data_directory
+ today_to_directory(today)
+ "/"
+ str(current_run_is + 1)
+ "/DL3 -ra "
+ str(ra_dec[2])
+ " -dec "
+ str(ra_dec[3])
+ " --output_dir "+ data_directory
+ today_to_directory(today)
+ "/"
+ str(current_run_is + 1)
+ "/plots --date "
+ today
)
os.system(
"srun --reservation=rta_one_node ./rta_var.py --dl3_folder "+ data_directory
+ today_to_directory(today)
+ "/"
+ str(current_run_is + 1)
+ "/DL3 -ra "
+ str(ra_dec[2])
+ " -dec "
+ str(ra_dec[3])
+ " --output_dir "+ data_directory
+ today_to_directory(today)
+ "/"
+ str(current_run_is + 1)
+ "/plots --date "
+ today
)
loop_id = loop_id + 1
if (
datetime.datetime.timestamp(datetime.datetime.now()) - Start_time
) > 14 * 3600:
quit = True
logging.info("End of the night, Stop the RTA check")
if __name__ == "__main__":
main()