Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions docs/changes/2318.maintenance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Simplify naming of sim_telarray configuration files written to disk for simulation production:
- telescope configuration files are now of type `CTAO-LSTS-01.cfg`, `CTAO-SSTS-25.cfg`
- array configuration files are of type `CTAO-South-beta.cfg`
- raises an exception if existing configuration files are overwritten by an application.
- add a run-number level to the model sub directory to avoid overwriting for applications using several models.
Comment thread
GernotMaier marked this conversation as resolved.
2 changes: 1 addition & 1 deletion src/simtools/io/io_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,5 @@ def get_model_configuration_directory(self, model_version, sub_dir=None):
Path
"""
return self.get_output_directory(
sub_dir=[sub_dir, "model", model_version] if sub_dir else ["model", model_version]
sub_dir=["model", sub_dir, model_version] if sub_dir else ["model", model_version]
)
19 changes: 12 additions & 7 deletions src/simtools/model/array_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(
calibration_device_types=None,
overwrite_model_parameters=None,
ignore_software_version=False,
model_directory_subdir=None,
):
"""Initialize ArrayModel."""
self._logger = logging.getLogger(__name__)
Expand All @@ -72,6 +73,13 @@ def __init__(
self.array_elements, self.site_model, self.telescope_models, self.calibration_models = (
self._initialize(site, array_elements, calibration_device_types)
)
if model_directory_subdir:
self._config_file_directory = self.io_handler.get_model_configuration_directory(
model_version=self.model_version, sub_dir=model_directory_subdir
)
models = [self.site_model, *self.telescope_models.values()]
for model in models:
model._config_file_directory = self._config_file_directory

self._telescope_model_files_exported = False
self._array_model_file_exported = False
Expand Down Expand Up @@ -143,14 +151,11 @@ def config_file_path(self):
Path
Path of the exported config file for sim_telarray.
"""
if self._config_file_path is None:
config_file_name = names.simtel_config_file_name(
array_name=self.layout_name,
site=self.site_model.site,
label=self.label,
return self._config_file_path or self.get_config_directory().joinpath(
names.sim_telarray_config_file_name(
array_name=self.layout_name, site=self.site_model.site
)
self._config_file_path = self.get_config_directory().joinpath(config_file_name)
return self._config_file_path
)

@property
def number_of_telescopes(self):
Expand Down
49 changes: 8 additions & 41 deletions src/simtools/model/model_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,14 @@ def _set_config_file_directory_and_name(self):
if self.name is None and self.site is None:
return

self._config_file_directory = self.io_handler.get_model_configuration_directory(
model_version=self.model_version
)
if self._config_file_directory is None:
self._config_file_directory = self.io_handler.get_model_configuration_directory(
model_version=self.model_version
)

# Setting file name and the location
config_file_name = names.simtel_config_file_name(
self.site,
telescope_model_name=self.name,
label=self.label,
self._config_file_path = self.config_file_directory.joinpath(
names.sim_telarray_config_file_name(site=self.site, telescope_model_name=self.name)
)
self._config_file_path = self.config_file_directory.joinpath(config_file_name)

def get_simulation_software_parameters(self, simulation_software):
"""
Expand Down Expand Up @@ -828,29 +825,7 @@ def export_model_files(self, destination_path=None, update_if_necessary=False):
)
self._is_exported_model_files_up_to_date = True

def get_config_file_path(self, label=None):
"""Return config file path for a given label.

Parameters
----------
label : str or None
Label used for output file naming. If None, use this model's label.

Returns
-------
pathlib.Path
Path to the sim_telarray configuration file.
"""
config_file_name = names.simtel_config_file_name(
self.site,
telescope_model_name=self.name,
label=self.label if label is None else label,
)
return self.config_file_directory.joinpath(config_file_name)

def write_sim_telarray_config_file(
self, additional_models=None, label=None, config_file_path=None
):
def write_sim_telarray_config_file(self, additional_models=None, label=None):
"""
Write the sim_telarray configuration file.

Expand All @@ -860,24 +835,16 @@ def write_sim_telarray_config_file(
Model object for additional parameter to be written to the config file.
label: str or None
Optional label override used for output file naming.
config_file_path: pathlib.Path or str or None
Optional explicit path of the config file. If not given, it is derived from ``label``.
"""
self.parameters.update(self._simulation_config_parameters.get("sim_telarray", {}))
self.export_model_files(update_if_necessary=True)

self._add_additional_models(additional_models)

config_file_path = (
Path(config_file_path)
if config_file_path is not None
else self.get_config_file_path(label=label)
)

# Ensure the writer label matches the config file naming label.
self._load_simtel_config_writer(label=label)
self.simtel_config_writer.write_telescope_config_file(
config_file_path=config_file_path,
config_file_path=self.config_file_path,
parameters=self.parameters,
Comment thread
GernotMaier marked this conversation as resolved.
)

Expand Down
9 changes: 9 additions & 0 deletions src/simtools/simtel/simtel_config_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,17 @@ def write_array_config_file(
Site model.
additional_metadata: dict
Dictionary with additional metadata to include.

Raises
------
FileExistsError
If the config file already exists.
"""
config_file_directory = Path(config_file_path).parent
config_file_path = Path(config_file_path)
if config_file_path.exists():
raise FileExistsError(f"Array config file {config_file_path} already exists.")

with open(config_file_path, "w", encoding="utf-8") as file:
self._write_header(file, "ARRAY CONFIGURATION FILE")

Expand Down
4 changes: 2 additions & 2 deletions src/simtools/simtel/simulator_ray_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _load_required_files(self, force_simulate):
self.__dict__["_" + base_name + "_file"] = file

if not file.exists() or force_simulate:
config_file_path = self.telescope_model.get_config_file_path(label=self.label)
config_file_path = self.telescope_model.config_file_path
# Adding header to photon list file.
with self._photons_file.open("w", encoding="utf-8") as file:
file.write(f"#{50 * '='}\n")
Expand Down Expand Up @@ -147,7 +147,7 @@ def make_run_command(self, run_number=None, input_file=None): # pylint: disable
label=self.label,
)

config_file_path = self.telescope_model.get_config_file_path(label=self.label)
config_file_path = self.telescope_model.config_file_path

if self.config.single_mirror_mode:
# Note: no mirror length defined for dual-mirror telescopes
Expand Down
2 changes: 2 additions & 0 deletions src/simtools/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def _initialize_array_models(self):

array_model = []
corsika_configurations = []
model_subdir = f"run{self.run_number:06d}"

for version in versions:
model = ArrayModel(
Expand All @@ -134,6 +135,7 @@ def _initialize_array_models(self):
model_version=version,
calibration_device_types=self._get_calibration_device_types(self.run_mode),
overwrite_model_parameters=settings.config.args.get("overwrite_model_parameters"),
model_directory_subdir=model_subdir,
)
cfg = CorsikaConfig(array_model=model, label=self.label, run_number=self.run_number)
model.initialize_seeds(cfg.zenith_angle, cfg.azimuth_angle)
Expand Down
13 changes: 10 additions & 3 deletions src/simtools/testing/validate_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import simtools.utils.general as gen
from simtools.db import db_handler
from simtools.io import ascii_handler
from simtools.sim_events import file_info
from simtools.testing import assertions

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -480,10 +481,16 @@ def _validate_simtel_cfg_files(config, simtel_cfg_file):

"""
reference_file = _resolve_path(simtel_cfg_file)
configuration = config["configuration"]
run_number = (
file_info.get_corsika_run_number(configuration["corsika_file"])
if configuration.get("corsika_file")
else configuration.get("run_number", 1) + configuration.get("run_number_offset", 0)
)
test_file = (
Path(config["configuration"]["output_path"])
/ f"model/{config['configuration']['model_version']}"
/ reference_file.name.replace("_test", f"_{config['configuration']['label']}")
Path(configuration["output_path"])
/ f"model/run{run_number:06d}/{configuration['model_version']}"
/ reference_file.name.replace("_test", f"_{configuration['label']}")
)
_logger.info(
f"Comparing simtel cfg files: {reference_file} and {test_file} "
Expand Down
51 changes: 16 additions & 35 deletions src/simtools/utils/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import re
from functools import cache
from pathlib import Path
from uuid import uuid4

import yaml

Expand Down Expand Up @@ -740,56 +739,38 @@ def get_simulation_software_name_from_parameter_name(
return None


def simtel_config_file_name(
site,
array_name=None,
telescope_model_name=None,
label=None,
extra_label=None,
):
def sim_telarray_config_file_name(site, array_name=None, telescope_model_name=None):
"""
sim_telarray config file name for a telescope.
sim_telarray configuration file name for an array or a telescope.

Parameters
----------
site: str
South or North.
array_name: str
Array name (e.g., CTAO-North-4-LSTs-1-MSTs)
telescope_model_name: str
LST-1, MST-FlashCam, ...
label: str
Instance label.
extra_label: str
Extra label in case of multiple telescope config files.
Telescope name (e.g., LSTN-01, MSTS-05)
Comment thread
GernotMaier marked this conversation as resolved.

Returns
-------
str
File name.
Configuration file name.
"""
name = "CTA"
name += f"-{array_name}" if array_name is not None else ""
name += f"-{site}"
name += f"-{telescope_model_name}" if telescope_model_name is not None else ""
name += f"_{label}" if label is not None else ""
name += f"_{extra_label}" if extra_label is not None else ""
name = "CTAO"
if array_name:
name += f"-{site}-{array_name}"
if telescope_model_name:
name += f"-{telescope_model_name}"
name += ".cfg"
Comment thread
Copilot marked this conversation as resolved.

# Telescope config files are included by the array config and parsed by sim_telarray
# through a fixed-size getword buffer. Keep include targets within that parser limit.
# through a fixed-size getword buffer.
if telescope_model_name is not None and len(name) > SIM_TELARRAY_INCLUDE_FILENAME_MAX_LENGTH:
token = uuid4().hex[:8]
prefix = "CTA"
prefix += f"-{array_name}" if array_name is not None else ""
prefix += f"-{site}"
suffix = f"_{token}.cfg"
telescope_component = f"-{telescope_model_name}"

available_length = SIM_TELARRAY_INCLUDE_FILENAME_MAX_LENGTH - len(prefix) - len(suffix)
if available_length < len(telescope_component):
telescope_component = telescope_component[: max(0, available_length)]

name = f"{prefix}{telescope_component}{suffix}"

raise ValueError(
f"Generated configuration file name {name} exceeds the maximum length of "
f"{SIM_TELARRAY_INCLUDE_FILENAME_MAX_LENGTH} characters."
)
return name


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ applications:
integration_tests:
- output_file: sim_telarray/run000007/gamma_run000007_za40deg_azm180deg_South_subsystem_lsts_6.0.2_test.simtel.zst
- test_simtel_cfg_files:
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTA-South-LSTS-01_test.cfg}
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTAO-LSTS-01.cfg}
- test_output_files:
- expected_sim_telarray_output:
pe_sum:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ applications:
overwrite_model_parameters: tests/resources/static/info_test_model_parameter_changes.yml
integration_tests:
- test_simtel_cfg_files:
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTA-South-MSTS-01_test.cfg}
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTAO-MSTS-01.cfg}
- test_simtel_cfg_files:
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTA-beta-South_test.cfg}
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTAO-South-beta.cfg}
- test_output_files:
- file: gamma_run000020_za62deg_azm180deg_South_beta_6.0.2_test.log_hist.tar.gz
path_descriptor: pack_for_grid_register
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ applications:
zenith_angle: 20
integration_tests:
- test_simtel_cfg_files:
"5.0.0": ${generated:sim_telarray_configurations/5.0.0/CTA-North-LSTN-01_test.cfg}
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTA-North-LSTN-01_test.cfg}
"5.0.0": ${generated:sim_telarray_configurations/5.0.0/CTAO-LSTN-01.cfg}
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTAO-LSTN-01.cfg}
- test_simtel_cfg_files:
"5.0.0": ${generated:sim_telarray_configurations/5.0.0/CTA-North-LSTN-02_test.cfg}
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTA-North-LSTN-02_test.cfg}
"5.0.0": ${generated:sim_telarray_configurations/5.0.0/CTAO-LSTN-02.cfg}
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTAO-LSTN-02.cfg}
- test_simtel_cfg_files:
"5.0.0": ${generated:sim_telarray_configurations/5.0.0/CTA-North-MSTN-01_test.cfg}
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTA-North-MSTN-01_test.cfg}
"5.0.0": ${generated:sim_telarray_configurations/5.0.0/CTAO-MSTN-01.cfg}
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTAO-MSTN-01.cfg}
- test_simtel_cfg_files:
"5.0.0": ${generated:sim_telarray_configurations/5.0.0/CTA-alpha-North_test.cfg}
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTA-alpha-North_test.cfg}
"5.0.0": ${generated:sim_telarray_configurations/5.0.0/CTAO-North-alpha.cfg}
"6.0.2": ${generated:sim_telarray_configurations/6.0.2/CTAO-North-alpha.cfg}
- test_output_files:
- file: proton_run000001_za20deg_azm180deg_North_alpha_6.0.2_test.log_hist.tar.gz
path_descriptor: pack_for_grid_register
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fadc_max_sum = 16777215
fadc_mhz = 1024.0
fadc_noise = 6.7
fadc_pedestal = 400.0
fadc_pulse_shape = fadc_pulse_shape-CTA-North-LSTN-01_test.dat
fadc_pulse_shape = fadc_pulse_shape-CTAO-LSTN-01.dat
fadc_sensitivity = 1.0
fadc_sum_bins = 40
fadc_sum_offset = 9
Expand Down Expand Up @@ -135,7 +135,7 @@ save_pe_with_amplitude = 1
store_photoelectrons = 20
tailcut_scale = 2.6
stars = none
config_release = 5.0.0 with simtools v0.32.1.dev99+gb55cc6a0c
config_release = 5.0.0 with simtools v0.33.1.dev228+g5a60b2353.d20260702
config_version = 5.0.0
camera_config_name = LSTN-design
camera_config_variant = LSTN-01
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fadc_max_sum = 16777215
fadc_mhz = 1024.0
fadc_noise = 6.7
fadc_pedestal = 400.0
fadc_pulse_shape = fadc_pulse_shape-CTA-North-LSTN-02_test.dat
fadc_pulse_shape = fadc_pulse_shape-CTAO-LSTN-02.dat
fadc_sensitivity = 1.0
fadc_sum_bins = 40
fadc_sum_offset = 9
Expand Down Expand Up @@ -135,7 +135,7 @@ save_pe_with_amplitude = 1
store_photoelectrons = 20
tailcut_scale = 2.6
stars = none
config_release = 5.0.0 with simtools v0.32.1.dev99+gb55cc6a0c
config_release = 5.0.0 with simtools v0.33.1.dev228+g5a60b2353.d20260702
config_version = 5.0.0
camera_config_name = LSTN-design
camera_config_variant = LSTN-02
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fadc_max_sum = 16777215
fadc_mhz = 1000.0
fadc_noise = 3.6
fadc_pedestal = 250.0
fadc_pulse_shape = fadc_pulse_shape-CTA-North-MSTN-01_test.dat
fadc_pulse_shape = fadc_pulse_shape-CTAO-MSTN-01.dat
fadc_sensitivity = 1.0
fadc_sum_bins = 60
fadc_sum_offset = 18
Expand Down Expand Up @@ -148,7 +148,7 @@ save_pe_with_amplitude = 1
store_photoelectrons = 26
tailcut_scale = 2.3
stars = none
config_release = 5.0.0 with simtools v0.32.1.dev99+gb55cc6a0c
config_release = 5.0.0 with simtools v0.33.1.dev228+g5a60b2353.d20260702
config_version = 5.0.0
camera_config_name = MSTx-NectarCam
camera_config_variant = MSTN-01
Expand Down
Loading