Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look for the closest declination line and adequate NSB random forest model for DL2 production #280

Draft
wants to merge 107 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
79ffaf6
Look for the closest declination line to choose the RF model to produ…
marialainez Feb 28, 2024
ce2fab1
Get the path names from the cfg file
marialainez Feb 29, 2024
147e88c
Get the source declination from the TCU database (not from the run ca…
marialainez Feb 29, 2024
045fb01
Get the RF models for a given run (not subrun), run_str is run.subrun
marialainez Feb 29, 2024
5f5071f
Change name of variable
marialainez Feb 29, 2024
0bb0185
Convert date to string in the correct format to create the path
marialainez Feb 29, 2024
2be6466
Adapt tests
marialainez Feb 29, 2024
51d40c4
Add necessary argument
marialainez Feb 29, 2024
cbdee3e
Adapt tests + create dec_list looking into the RF_models directory
marialainez Mar 1, 2024
5ea46a4
Use only the TCU database to get the source coordinates (no access to…
marialainez Mar 18, 2024
f0e1680
Use only dec strings with the format dec_(min)_XXXX
marialainez Mar 21, 2024
4915cc1
Add new step in datasequence to produce the CatB calibration files
marialainez May 28, 2024
53d57b3
add r0-dir to the calibration scripts (e.g. in case R0G is used)
marialainez May 28, 2024
0b43696
Add CatB calibration file as input in dl1ab step
marialainez May 28, 2024
95290da
adapt configuration file
marialainez May 28, 2024
0828b26
add docstring
marialainez May 28, 2024
763ec64
adapt tests
marialainez May 29, 2024
a3f0e1a
add argument in catB_calibration function
marialainez May 29, 2024
9e9193c
adapt tests
marialainez May 29, 2024
5ac3770
fix small issues
marialainez Jun 3, 2024
889508c
adapt tests
marialainez Jun 3, 2024
9b4b441
Merge branch 'main' into dl2_production
morcuended Jun 25, 2024
2d3e2bd
Update src/osa/configs/sequencer.cfg
marialainez Jun 26, 2024
eefba65
adapt name of Cat-A and Cat-B calibration dir to the cfg file
marialainez Jun 26, 2024
f483be3
use lstchain major version as calib_prod_id
marialainez Jun 26, 2024
34c8ab0
add option in the cfg to apply or not catB calibration
marialainez Aug 5, 2024
c67cdd5
add cat A calibration run number
marialainez Aug 6, 2024
d3f5d22
remove unused import
marialainez Sep 19, 2024
434ad9b
check always if the culmination angle is larger than for the source
marialainez Sep 19, 2024
78b918e
return the declination as an astropy quantity
marialainez Sep 19, 2024
22e7a2d
rename directory
marialainez Sep 19, 2024
7207dbb
fix mistake
marialainez Sep 19, 2024
0c15ed7
change directory name
marialainez Sep 19, 2024
d6c45cd
adapt tests
marialainez Sep 19, 2024
0b25f30
adapt tests
marialainez Sep 20, 2024
c3f8608
add filters as an argument of the catB calibration command
marialainez Sep 23, 2024
ccf89db
correct name of catB_calib_dir
marialainez Sep 23, 2024
f8b0edd
wait until the catB file is created if subrun != 0000
marialainez Sep 23, 2024
e94a15a
adapt tests
marialainez Sep 23, 2024
a9ebfdb
remove unused imports
marialainez Sep 23, 2024
d8df805
adapt tests
marialainez Sep 24, 2024
37062cd
adapt historylevel function
marialainez Sep 25, 2024
7bfbdb2
do not check the culmination angle for sources between 22.76 and 34.7…
marialainez Sep 26, 2024
ae9ccc0
get the general value of the two closest values
marialainez Sep 27, 2024
d220c81
add --no-dl1ab option to process data with catB (sequencer launched t…
marialainez Oct 4, 2024
805964b
if option --no-dl1ab, just execute the r0_to_dl1 step
marialainez Oct 10, 2024
48f13b2
do not launch catB_calibration in datasequence
marialainez Oct 14, 2024
cf7b610
fix historylevel
marialainez Oct 14, 2024
754721d
add rf_model_path as an input argument for datasequence + move get_RF…
marialainez Dec 12, 2024
37b8fef
pass the rf_model_path when writing the sequence.py files
marialainez Dec 12, 2024
cc94ccb
adapt tests
marialainez Dec 13, 2024
97f78fe
remove unused imports
marialainez Dec 13, 2024
9f59675
adapt tests
marialainez Dec 15, 2024
5b875b3
remove unnecessary part
marialainez Dec 16, 2024
2137efd
remove unused import
marialainez Dec 16, 2024
bb08a83
Merge branch 'main' into dl2_production
marialainez Dec 16, 2024
56294a8
launch catB and tailcuts_finder in a separate sequencer
marialainez Jan 22, 2025
52d08bb
remove cat-B calibration function from datasequence
marialainez Jan 22, 2025
9f75081
use options.date instead of date
marialainez Jan 22, 2025
19108d1
fix path names
marialainez Jan 22, 2025
564932a
use the config file produced by lstchain_find_tailcuts
marialainez Jan 23, 2025
dab59a0
check if catB and tailcuts_finder finished before launching dl1ab
marialainez Jan 23, 2025
c33586a
allow to use the dl1b standard config file is apply_standard_dl1b_con…
marialainez Jan 23, 2025
20b5eb9
move catB_closed_file_exists function to paths
marialainez Jan 23, 2025
c5ff8ae
Merge branch 'main' into catB_option1
marialainez Jan 23, 2025
d02810c
add necessary imports
marialainez Jan 23, 2025
169b20b
Merge branch 'catB_option1' of https://github.com/cta-observatory/lst…
marialainez Jan 23, 2025
9229ad3
add sequencer_catB_tailcuts to pyproject.toml
marialainez Jan 24, 2025
ac064c9
add verbose option
marialainez Jan 24, 2025
77b5d21
use the date provided in the command line (if given)
marialainez Jan 24, 2025
0e1e799
fix the name of the path of the dl1a files (without tailcut84)
marialainez Jan 24, 2025
360693f
write job_id correctly in the log file of the tailcut finder
marialainez Jan 24, 2025
1a66fee
use the command name given in the cfg
marialainez Jan 24, 2025
17bd18c
[skip ci] Correct language in help string
morcuended Jan 28, 2025
5b51983
use more descriptive names
marialainez Jan 28, 2025
a4a2d5c
check if sequencer is completed by looking at the history files
marialainez Jan 28, 2025
02fc610
set rc to 1 (and not 0) to see that the job failed and be aware of it
marialainez Jan 28, 2025
8b4487e
check if the catB files already exist
marialainez Jan 28, 2025
450ae0d
add dococstrings
marialainez Jan 28, 2025
7b49647
add necessary import
marialainez Jan 28, 2025
a4dd985
adapt tests
marialainez Jan 28, 2025
a30be4a
fix name
marialainez Jan 28, 2025
dcc641b
remove unnecessary f-string
marialainez Jan 28, 2025
f9456e1
add simulate option
marialainez Jan 29, 2025
42c46e1
add abreviation for simulate option
marialainez Jan 29, 2025
c6896ac
select the rf models with the correct NSB settings
marialainez Jan 29, 2025
2d540ca
require at least lstchain v0.10.15
marialainez Jan 29, 2025
e437c8c
add docstrings
marialainez Jan 29, 2025
3aecfff
use the mc_prod (rf_models_prefix) given in the cfg file
marialainez Jan 29, 2025
8070e23
write the DL1b files in the corresponding tailcutXY directory
marialainez Feb 7, 2025
4dc81f3
use pointing dec (as the median of the dec from datacheck files) inst…
marialainez Feb 10, 2025
1d2b2e3
remove unnecessary imports and unnecessary function
marialainez Feb 10, 2025
88f6331
Apply suggestions from code review
morcuended Feb 10, 2025
5a53596
bumo ctapipe io lst version compatible with latest lstchain
morcuended Feb 10, 2025
412eb7e
adapt tests
marialainez Feb 10, 2025
64c74dc
rename input_dir as analysis_dir
marialainez Feb 10, 2025
7658bfe
rename source_dec as pointing_dec
marialainez Feb 10, 2025
e6ea503
get closest NSB among those with the same cleaning
marialainez Feb 10, 2025
309968b
Merge branch 'catB_option1' into dl2_production
marialainez Feb 11, 2025
8b005ba
write the dl2 files in tailcutXY/nsb_tuning_x.xx directories
marialainez Feb 11, 2025
58aead8
use the pointing_dec given in the run catalog
marialainez Feb 11, 2025
3eb000a
fix mistake
marialainez Feb 11, 2025
b13a07b
add necessary import
marialainez Feb 11, 2025
ad45095
avoid writing the RF model if --no-dl1ab option is given
marialainez Feb 14, 2025
391f284
add units to pointing_dec
marialainez Feb 14, 2025
5fa55ff
remove unused import
marialainez Feb 14, 2025
b906992
include all tags in dl2_prod_id[C
marialainez Feb 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions src/osa/paths.py
morcuended marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from typing import List
import subprocess
import time
import os

import lstchain
from astropy.table import Table
from astropy.coordinates import SkyCoord
marialainez marked this conversation as resolved.
Show resolved Hide resolved
from lstchain.onsite import (find_systematics_correction_file,
find_time_calibration_file,
find_filter_wheels)
Expand Down Expand Up @@ -397,10 +399,100 @@ def create_longterm_symlink(cherenkov_job_id: str = None):
else:
log.warning(f"Job {cherenkov_job_id} (lstchain_cherenkov_transparency) did not finish successfully.")


def dl1_datacheck_longterm_file_exits() -> bool:
"""Return true if the longterm DL1 datacheck file was already produced."""
nightdir = utils.date_to_dir(options.date)
longterm_dir = Path(cfg.get("LST1", "LONGTERM_DIR"))
longterm_file = longterm_dir / options.prod_id / nightdir / f"DL1_datacheck_{nightdir}.h5"
return longterm_file.exists()


def convert_dec_string(dec_str: str) -> float:
"""Return the declination angle in degrees corresponding to a
given string of the form "dec_XXXX" or "dec_min_XXXX"."""
# Split the string into parts
parts = dec_str.split('_')

# Extract the sign, degrees, and minutes
sign = 1 if 'min' not in parts else -1
degrees = int(parts[-1])
morcuended marked this conversation as resolved.
Show resolved Hide resolved

# Calculate the numerical value
dec_value = sign * (degrees / 100)

return dec_value


def get_corresponding_string(list1: list, list2: list) -> dict:
"""Return a dictionary created from two given lists."""
corresponding_dict = {}
for index, element in enumerate(list2):
corresponding_dict[element] = list1[index]
return corresponding_dict


def get_latest_RF_model_path(dec_str: str) -> Path:
"""Get the path of the most recent version of RF models for a given declination,
excluding the ones produced for the source-dependent analysis."""
BASE_MODELS = Path("/fefs/aswg/data/models/AllSky")
morcuended marked this conversation as resolved.
Show resolved Hide resolved
# make sure the RF models correspond to the current version of lstchain
current_version = get_major_version(utils.get_lstchain_version())
list_nodes = sorted(BASE_MODELS.rglob(f"*{current_version}*/{dec_str}"), key=os.path.getmtime)
morcuended marked this conversation as resolved.
Show resolved Hide resolved

log.debug(f"Found len(list_nodes) paths with {current_version} corresponding to {dec_str}:")
for path in list_nodes:
log.debug(path)

# remove from the list the models produced for the source-dependent analysis
for i in list_nodes:
if "srcdep" in str(i):
list_nodes.remove(i)
morcuended marked this conversation as resolved.
Show resolved Hide resolved

return list_nodes[-1]


def get_RF_model(run_str: str) -> Path:
"""Get the path of the RF model to be used in the DL2 production for a given run."""
run_catalog_dir = Path("/fefs/aswg/data/real/monitoring/RunCatalog")
morcuended marked this conversation as resolved.
Show resolved Hide resolved
run_catalog_file = run_catalog_dir / f"RunCatalog_{options.date}.ecsv"
run_catalog = Table.read(run_catalog_file)
run = run_catalog[run_catalog["run_id"]==int(run_str)]

try:
target_name = run["source_name"]
source_coordinates = SkyCoord.from_name(target_name)
source_dec = source_coordinates.dec.value

except TypeError:
source_dec = run["source_dec"][0]
morcuended marked this conversation as resolved.
Show resolved Hide resolved

source_culmination = utils.culmination_angle(source_dec)

dec_list = os.listdir("/fefs/aswg/data/mc/DL0/LSTProd2/TrainingDataset/Protons")[:-2]

# Convert each string in the list to numerical values
dec_values = [convert_dec_string(dec) for dec in dec_list]

closest_declination = min(dec_values, key=lambda x: abs(x - source_dec))
closest_dec_culmination = utils.culmination_angle(closest_declination)
log.debug(
f"The declination closest to {source_dec} is: {closest_declination}."
"Checking if the culmination angle is larger than the one of the target source."
)

if closest_dec_culmination > source_culmination:
# If the culmination angle of the closest declination line is larger than for the source,
# remove it from the declination lines list and look for the second closest declination line.
corresponding_dict = get_corresponding_string(dec_list, dec_values)
corresponding_string = corresponding_dict[closest_declination]
dec_values.remove(closest_declination)
dec_list.remove(corresponding_string)
closest_declination = min(dec_values, key=lambda x: abs(x - source_dec))

log.debug(f"The declination line to use for the DL2 production is: {closest_declination}")

corresponding_dict = get_corresponding_string(dec_list, dec_values)
corresponding_string = corresponding_dict[closest_declination]
morcuended marked this conversation as resolved.
Show resolved Hide resolved

return get_latest_RF_model_path(corresponding_string)
3 changes: 2 additions & 1 deletion src/osa/scripts/datasequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from osa.utils.cliopts import data_sequence_cli_parsing
from osa.utils.logging import myLogger
from osa.utils.utils import date_to_dir
from osa.paths import get_RF_model

__all__ = ["data_sequence", "r0_to_dl1", "dl1_to_dl2", "dl1ab", "dl1_datacheck"]

Expand Down Expand Up @@ -265,7 +266,7 @@ def dl1_to_dl2(run_str: str) -> int:
dl1ab_subdirectory = Path(options.directory) / options.dl1_prod_id
dl2_subdirectory = Path(options.directory) / options.dl2_prod_id
dl2_config = Path(cfg.get("lstchain", "dl2_config"))
rf_models_directory = Path(cfg.get("lstchain", "RF_MODELS"))
rf_models_directory = get_RF_model(run_str)
dl1_file = dl1ab_subdirectory / f"dl1_LST-1.Run{run_str}.h5"

command = cfg.get("lstchain", "dl1_to_dl2")
Expand Down
21 changes: 21 additions & 0 deletions src/osa/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
from datetime import datetime, timedelta
from pathlib import Path
from socket import gethostname
from gammapy.data import observatory_locations
from astropy import units as u

import osa.paths
from osa.configs import options
from osa.configs.config import cfg
from osa.utils.iofile import write_to_file
from osa.utils.logging import myLogger


__all__ = [
"get_lstchain_version",
"date_to_dir",
Expand Down Expand Up @@ -285,3 +288,21 @@ def wait_for_daytime(start=8, end=18):
while time.localtime().tm_hour <= start or time.localtime().tm_hour >= end:
log.info("Waiting for sunrise to not interfere with the data-taking. Sleeping.")
time.sleep(3600)


def culmination_angle(dec: int) -> float:
"""
Calculate culmination angle for a given declination.

Parameters
----------
dec: int
marialainez marked this conversation as resolved.
Show resolved Hide resolved
morcuended marked this conversation as resolved.
Show resolved Hide resolved
declination in degrees
marialainez marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
Culmination angle in degrees
"""
location = observatory_locations["cta_north"]
Lat = location.lat # latitude of the LST1 site
return abs(Lat - dec*u.deg).value
morcuended marked this conversation as resolved.
Show resolved Hide resolved
Loading