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 RF model to produce the DL2 files #280

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 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
9b4b441
Merge branch 'main' into dl2_production
morcuended Jun 25, 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
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
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
3 changes: 2 additions & 1 deletion src/osa/configs/sequencer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ DL1_DIR: %(BASE)s/DL1
DL1AB_DIR: %(BASE)s/DL1
DL2_DIR: %(BASE)s/DL2
DL3_DIR: %(BASE)s/DL3
RF_MODELS: %(BASE)s/models/AllSky
OSA_DIR: %(BASE)s/OSA
CLOSER_DIR: %(OSA_DIR)s/Closer
HIGH_LEVEL_DIR: %(OSA_DIR)s/HighLevel
Expand Down Expand Up @@ -55,7 +56,7 @@ store_image_dl1ab: True
merge_dl1_datacheck: True
dl1b_config: /software/lstchain/data/lstchain_standard_config.json
dl2_config: /software/lstchain/data/lstchain_standard_config.json
rf_models: /data/models/prod5/zenith_20deg/20201023_v0.6.3
mc_prod: 20240131_allsky_v0.10.5_all_dec_base
morcuended marked this conversation as resolved.
Show resolved Hide resolved
dl3_config: /software/lstchain/data/dl3_std_config.json
max_tries: 3

Expand Down
18 changes: 18 additions & 0 deletions src/osa/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,21 @@ def database(base_test_dir):
)
cursor.connection.commit()
yield cursor


@pytest.fixture(scope="session")
def rf_models_dir(base_test_dir):
marialainez marked this conversation as resolved.
Show resolved Hide resolved
directory = base_test_dir / "models/AllSky"
directory.mkdir(parents=True, exist_ok=True)
return directory


@pytest.fixture(scope="session")
def rf_model_path(rf_models_dir):
mc_prod = "20240131_allsky_v0.10.5_all_dec_base"
declination_str = "dec_2276"
rf_model_path = rf_models_dir / mc_prod / declination_str
rf_model_path.mkdir(parents=True, exist_ok=True)
return rf_model_path


77 changes: 77 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,85 @@
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_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(cfg.get(options.tel_id, "RUN_CATALOG"))
run_catalog_file = run_catalog_dir / f"RunCatalog_{utils.date_to_dir(options.date)}.ecsv"
run_catalog = Table.read(run_catalog_file)
run = run_catalog[run_catalog["run_id"]==int(run_str)]
target_name = run["source_name"]

try:
source_coordinates = SkyCoord.from_name(target_name[0])
morcuended marked this conversation as resolved.
Show resolved Hide resolved
source_dec = source_coordinates.dec.value

except TypeError:
tcu_server = cfg.get("database", "tcu_db")
source_dec = utils.get_source_dec_from_TCU(target_name, tcu_server)

Check warning on line 449 in src/osa/paths.py

View check run for this annotation

Codecov / codecov/patch

src/osa/paths.py#L448-L449

Added lines #L448 - L449 were not covered by tests

source_culmination = utils.culmination_angle(source_dec)

rf_models_dir = Path(cfg.get("LST1", "RF_MODELS"))
mc_prod = cfg.get("lstchain", "mc_prod")
dec_list = os.listdir(rf_models_dir / mc_prod)

# 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)
declination_str = corresponding_dict[closest_declination]
dec_values.remove(closest_declination)
dec_list.remove(declination_str)
closest_declination = min(dec_values, key=lambda x: abs(x - source_dec))

Check warning on line 474 in src/osa/paths.py

View check run for this annotation

Codecov / codecov/patch

src/osa/paths.py#L470-L474

Added lines #L470 - L474 were not covered by tests

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

Check warning on line 476 in src/osa/paths.py

View check run for this annotation

Codecov / codecov/patch

src/osa/paths.py#L476

Added line #L476 was not covered by tests

corresponding_dict = get_corresponding_string(dec_list, dec_values)
declination_str = corresponding_dict[closest_declination]

rf_model_path = rf_models_dir / mc_prod / declination_str

return rf_model_path
2 changes: 1 addition & 1 deletion src/osa/provenance/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def parse_variables(class_instance):
configfile_dl1b = cfg.get("lstchain", "dl1b_config")
configfile_dl2 = cfg.get("lstchain", "dl2_config")
raw_dir = Path(cfg.get("LST1", "R0_DIR"))
rf_models_directory = Path(cfg.get("lstchain", "RF_MODELS"))
rf_models_directory = Path(cfg.get("LST1", "RF_MODELS"))
dl1_dir = Path(cfg.get("LST1", "DL1_DIR"))
dl2_dir = Path(cfg.get("LST1", "DL2_DIR"))
calib_dir = Path(cfg.get("LST1", "CALIB_DIR"))
Expand Down
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[:5])
dl1_file = dl1ab_subdirectory / f"dl1_LST-1.Run{run_str}.h5"

command = cfg.get("lstchain", "dl1_to_dl2")
Expand Down
23 changes: 17 additions & 6 deletions src/osa/scripts/tests/test_osa_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ def test_simulate_processing(

with open(json_file_dl2) as file:
dl2 = yaml.safe_load(file)
assert len(dl2["entity"]) == 25
assert len(dl2["activity"]) == 6
assert len(dl2["used"]) == 21
assert len(dl2["wasGeneratedBy"]) == 12
assert len(dl2["entity"]) == 19
assert len(dl2["activity"]) == 5
assert len(dl2["used"]) == 15
assert len(dl2["wasGeneratedBy"]) == 10

rc = run_program("simulate_processing", "-p")
assert rc.returncode == 0
Expand Down Expand Up @@ -238,17 +238,28 @@ def test_closer(
assert closed_seq_file.exists()


def test_datasequence(running_analysis_dir):
def test_datasequence(
running_analysis_dir,
run_catalog,
run_catalog_dir,
rf_models_dir,
rf_model_path
):
drs4_file = "drs4_pedestal.Run00001.0000.fits"
calib_file = "calibration.Run00002.0000.hdf5"
timecalib_file = "time_calibration.Run00002.0000.hdf5"
systematic_correction_file = "no_sys_corrected_calibration_scan_fit_20210514.0000.h5"
drive_file = "DrivePosition_20200117.txt"
runsummary_file = "RunSummary_20200117.ecsv"
prod_id = "v0.1.0"
run_number = "00003.0000"
run_number = "01807.0000"
options.directory = running_analysis_dir

assert run_catalog_dir.exists()
assert run_catalog.exists()
assert rf_models_dir.exists()
assert rf_model_path.exists()

output = run_program(
"datasequence",
"--date=2020-01-17",
Expand Down
30 changes: 30 additions & 0 deletions src/osa/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
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
from pymongo import MongoClient

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 +289,29 @@
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


def get_source_dec_from_TCU(source_name: str, tcu_server: str) -> float:
"""Get the declination of a given source from the TCU database."""
client = MongoClient(tcu_server)
collection = client["lst1_config"]["structure_configurations"]
source_dec = collection.find_one({"name": source_name})["target"]["source_dec"]
return source_dec

Check warning on line 317 in src/osa/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

src/osa/utils/utils.py#L314-L317

Added lines #L314 - L317 were not covered by tests
morcuended marked this conversation as resolved.
Show resolved Hide resolved
Loading