#!/usr/bin/env python3
import sys
import re
import datetime
[docs]
det_last_run_time = {"D0ARCA001": {"run": 9115,
"time": "2021-03-24 09:00"},
"D0ARCA006": {"run": 10287,
"time": "2021-09-10 09:00"},
"D0ARCA009": {"run": 12269,
"time": "2022-06-01 09:00"},
"D0ARCA020": {"run": 13194,
"time": "2022-09-07 15:00"},
"D0ARCA021": {"run": 17664,
"time": "2023-09-11 15:00"},
"D0ARCA028": {"run": 1e5,
"time": "2030-01-01 09:00"},
"D_ORCA006": {"run": 11295,
"time": "2021-11-18 15:38"},
"D0ORCA010": {"run": 13210,
"time": "2022-05-10 12:00"},
"D0ORCA007": {"run": 13702,
"time": "2022-06-29 18:00"},
"D1ORCA013": {"run": 13960,
"time": "2022-07-26 09:00"},
"D0ORCA011": {"run": 14318,
"time": "2022-09-05 21:00"},
"D1ORCA011": {"run": 15146,
"time": "2022-12-06 03:00"},
"D0ORCA015": {"run": 16338,
"time": "2023-04-05 08:00"},
"D1ORCA015": {"run": 16514,
"time": "2023-04-26 21:00"},
"D0ORCA018": {"run": 19944,
"time": "2024-01-09 06:00"},
"D1ORCA019": {"run": 20473,
"time": "2024-06-15 15:45"},
"D0ORCA023": {"run": 1e5,
"time": "2030-01-01 09:00"}
}
from km3dq_common.common_library import get_run_properties_from_db
###############################################################################
[docs]
def date_to_datetime(d):
"""
Convert a date-string "yyyy-mm-dd hh:mm" into a datetime
"""
re_time = re.compile(r"(\d\d\d\d)-(\d\d)-(\d\d)\s(\d\d):(\d\d)")
spl_time = re_time.match(d)
if spl_time:
output = datetime.datetime(int(spl_time.group(1)),
int(spl_time.group(2)),
int(spl_time.group(3)),
int(spl_time.group(4)),
int(spl_time.group(5)))
else:
print(f"Wrong datetime format {d}")
sys.exit()
return output
###############################################################################
[docs]
def get_det_run_timerange(site):
# Return all the start/end of time/run for all detectors of a site
tr = {}
if site == "ARCA":
start_date_time = "2019-01-16 06:00"
start_run = 6473
else:
start_date_time = "2020-01-26 15:38"
start_run = 7223
tr["Any"] = {"start": start_date_time,
"start_run": start_run}
for i_det in det_last_run_time.keys():
if site not in i_det:
continue
# NB: det_last_run_time keys are chronologically ordered
tr[i_det] = {"start": start_date_time,
"end": det_last_run_time[i_det]["time"],
"start_run": start_run,
"end_run": det_last_run_time[i_det]["run"]}
tr["Any"]["end"] = det_last_run_time[i_det]["time"]
tr["Any"]["end_run"] = det_last_run_time[i_det]["run"]
start_date_time = tr[i_det]["end"]
start_run = tr[i_det]["end_run"]
return tr
###############################################################################
[docs]
def get_det_run_from_date(site, date_str):
out = {"detector": "",
"run": 1e5}
tr = get_det_run_timerange(site)
date_time = date_to_datetime(date_str)
date_time_epoch = date_time.replace(tzinfo=datetime.timezone.utc).timestamp()
for i_det in tr.keys():
d_t_0 = date_to_datetime(tr[i_det]["start"])
d_t_1 = date_to_datetime(tr[i_det]["end"])
if d_t_0 <= date_time < d_t_1:
out["detector"] = i_det
continue
run_prop = get_run_properties_from_db(out["detector"], "PHYS")
for i_run in run_prop.keys():
if all((date_time_epoch < float(run_prop[i_run]['UNIXSTARTTIME'])/1000.,
i_run < out["run"])):
out["run"] = i_run
return out