diff --git a/NuRadioMC/simulation/simulation.py b/NuRadioMC/simulation/simulation.py index 42a1d19fa..2732e38bb 100644 --- a/NuRadioMC/simulation/simulation.py +++ b/NuRadioMC/simulation/simulation.py @@ -99,6 +99,8 @@ class simulation: def __init__(self, inputfilename, outputfilename, detectorfile, + det=None, + det_kwargs={}, outputfilenameNuRadioReco=None, debug=False, evt_time=datetime.datetime(2018, 1, 1), @@ -124,6 +126,10 @@ def __init__(self, inputfilename, specify hdf5 output filename. detectorfile: string path to the json file containing the detector description + det: detector object + Pass a detector class object + det_kwargs: dict + Pass arguments to the detector (only used when det == None) station_id: int the station id for which the simulation is performed. Must match a station deself._fined in the detector description @@ -157,8 +163,9 @@ def __init__(self, inputfilename, allows to specify a custom ice model. This model is used if the config file specifies the ice model as "custom". """ logger.setLevel(log_level) - if 'write_mode' in kwargs.keys(): + if 'write_mode' in kwargs: logger.warning('Parameter write_mode is deprecated. Define the output format in the config file instead.') + self._log_level = log_level self._log_level_ray_propagation = log_level_propagation config_file_default = os.path.join(os.path.dirname(__file__), 'config_default.yaml') @@ -212,19 +219,15 @@ def __init__(self, inputfilename, self._mout_groups = collections.OrderedDict() self._mout_attrs = collections.OrderedDict() - # read in detector positions - logger.status("Detectorfile {}".format(os.path.abspath(self._detectorfile))) - self._det = None - if default_detector_station is not None: - logger.warning( - 'Deprecation warning: Passing the default detector station is deprecated. Default stations and default' - 'channel should be specified in the detector description directly.' - ) - logger.status(f"Default detector station provided (station {default_detector_station}) -> Using generic detector") - self._det = gdetector.GenericDetector(json_filename=self._detectorfile, default_station=default_detector_station, - default_channel=default_detector_channel, antenna_by_depth=False) + # Initialize detector + if det is None: + logger.status("Detectorfile {}".format(os.path.abspath(self._detectorfile))) + kwargs = dict(json_filename=self._detectorfile, default_station=default_detector_station, + default_channel=default_detector_channel, antenna_by_depth=False) + kwargs.update(det_kwargs) + self._det = detector.Detector(**kwargs) else: - self._det = detector.Detector(json_filename=self._detectorfile, antenna_by_depth=False) + self._det = det self._det.update(evt_time) @@ -851,7 +854,7 @@ def run(self): self._station = NuRadioReco.framework.station.Station(self._station_id) self._station.set_sim_station(self._sim_station) self._station.get_sim_station().set_station_time(self._evt_time) - + # convert efields to voltages at digitizer if hasattr(self, '_detector_simulation_part1'): # we give the user the opportunity to define a custom detector simulation diff --git a/NuRadioReco/detector/detector.py b/NuRadioReco/detector/detector.py index 883a46b76..c2e311720 100644 --- a/NuRadioReco/detector/detector.py +++ b/NuRadioReco/detector/detector.py @@ -1,8 +1,12 @@ +import json +import os +import numpy as np +import logging + import NuRadioReco.detector.detector_base import NuRadioReco.detector.generic_detector import NuRadioReco.detector.RNO_G.rnog_detector -import json -import os + def find_path(name): @@ -137,7 +141,17 @@ def Detector(*args, **kwargs): if source == 'json': f.close() - if has_reference_entry: + has_default = np.any([arg in kwargs and kwargs[arg] is not None for arg in ["default_station", "default_channel", "default_device"]]) + + if has_reference_entry or has_default: + if has_default: + logging.warning( + 'Deprecation warning: Passing the default detector station is deprecated. Default stations and default' + 'channel should be specified in the detector description directly.') + + if "default_station" in kwargs: + logging.status(f'Default detector station provided (station {kwargs["default_station"]}) -> Using generic detector') + return NuRadioReco.detector.generic_detector.GenericDetector( json_filename=filename, source=source, dictionary=dictionary, assume_inf=assume_inf, antenna_by_depth=antenna_by_depth, **kwargs)