diff --git a/docs/source/changes/36.added.rst b/docs/source/changes/36.added.rst new file mode 100644 index 0000000..abe808e --- /dev/null +++ b/docs/source/changes/36.added.rst @@ -0,0 +1 @@ +Allow to define a source also via its celestial coordinates. diff --git a/src/iact_estimator/core.py b/src/iact_estimator/core.py index c42b5ff..bcdad97 100644 --- a/src/iact_estimator/core.py +++ b/src/iact_estimator/core.py @@ -4,6 +4,8 @@ import importlib import astropy.units as u +from astropy.coordinates import SkyCoord +from astroplan import FixedTarget from gammapy.stats import WStatCountsStatistic import numpy as np from scipy import interpolate @@ -32,6 +34,35 @@ logger = logging.getLogger(__name__) +def load_target_source_coordinates(config): + """Load target source using celestial coordinates. + + Parameters + ---------- + config : dict + Loaded configuration file. + + Returns + ------- + target_source : `~astroplan.FixedTarget` + """ + source_name = ( + config["target_source"]["name"] + if config["target_source"]["name"] + else "test_source" + ) + + try: + coords = config["target_source"]["coordinates"] + target_source_coordinates = SkyCoord( + coords["ra_l"], coords["dec_b"], frame=coords["frame"].lower() + ) + target_source = FixedTarget(coord=target_source_coordinates, name=source_name) + except ValueError: + logging.exception("Invalid target source coordinates.") + return target_source + + def setup_logging(log_level, source_name): """ Create a logger. @@ -213,17 +244,15 @@ def initialize_model(config): initialized_model : `~gammapy.modeling.models.SpectralModel` Initialized instance of a spectral model. """ - model_name = config["assumed_model"]["name"] + assumed_model_cfg = config["target_source"]["assumed_model"] + model_name = assumed_model_cfg["name"] module_name = ".".join(model_name.split(".")[:-1]) class_name = model_name.split(".")[-1] module = importlib.import_module(module_name) model = getattr(module, class_name) - model_parameters = config["assumed_model"]["parameters"] + model_parameters = assumed_model_cfg["parameters"] - if ( - class_name == "LogParabolaSpectralModel" - and config["assumed_model"]["from_log10"] - ): + if class_name == "LogParabolaSpectralModel" and assumed_model_cfg["from_log10"]: initialized_model = model.from_log10(**model_parameters) else: initialized_model = model(**model_parameters) diff --git a/src/iact_estimator/resources/config.yml b/src/iact_estimator/resources/config.yml index 45190c4..1ae590a 100644 --- a/src/iact_estimator/resources/config.yml +++ b/src/iact_estimator/resources/config.yml @@ -1,12 +1,19 @@ -assumed_model: - # see https://docs.gammapy.org/1.1/user-guide/model-gallery/index.html#spectral-models - name: gammapy.modeling.models.LogParabolaSpectralModel - parameters: - amplitude: 3.39e-11 TeV^-1 cm^-2 s^-1 - reference: 1 TeV - alpha: 2.51 - beta: 0.21 - from_log10: True # relevant for e.g. LogParabolaSpectralModel +target_source: + name: "Crab Nebula" + coordinates: + force: false # force use of custom coordinates for sources found also by name + ra_l: + dec_b: + frame: + assumed_model: + # see https://docs.gammapy.org/1.1/user-guide/model-gallery/index.html#spectral-models + name: gammapy.modeling.models.LogParabolaSpectralModel + parameters: + amplitude: 3.39e-11 TeV^-1 cm^-2 s^-1 + reference: 1 TeV + alpha: 2.51 + beta: 0.21 + from_log10: True # relevant for e.g. LogParabolaSpectralModel observation_time: 50 h observation_datetime: "2024-06-15 18:00" diff --git a/src/iact_estimator/scripts/main.py b/src/iact_estimator/scripts/main.py index e9cb4a5..c1da902 100644 --- a/src/iact_estimator/scripts/main.py +++ b/src/iact_estimator/scripts/main.py @@ -6,6 +6,7 @@ import shutil from astroplan import FixedTarget, Observer +from astropy.coordinates.name_resolve import NameResolveError from astropy.time import Time import astropy.units as u from astropy.visualization import quantity_support @@ -15,6 +16,7 @@ from ..io import read_yaml from ..core import ( setup_logging, + load_target_source_coordinates, initialize_model, check_input_configuration, prepare_data, @@ -97,7 +99,15 @@ def main(): output_path = ( Path(args.output_path) if args.output_path is not None else Path.cwd() ) - source_name = args.source_name + + logging.info("Loading configuration file") + config = read_yaml(args.config) + + source_name = ( + config["target_source"]["name"] + if config["target_source"]["name"] + else "test_source" + ) previous_output = len( [file for file in output_path.rglob(f"**/{source_name}*")] @@ -109,9 +119,6 @@ def main(): logger = setup_logging(args.log_level, source_name) - logger.info("Loading configuration file") - config = read_yaml(args.config) - logging.info("Validating input configuration") if not check_input_configuration(config): logging.critical( @@ -175,7 +182,17 @@ def main(): logger.info("All expected operations have been perfomed succesfully.") - target_source = FixedTarget.from_name(source_name) + if source_name: + try: + target_source = FixedTarget.from_name(source_name) + except NameResolveError: + target_source = load_target_source_coordinates(config) + elif ( + source_name != "test_source" + and config["target_source"]["coordinates"]["force"] + ): + target_source = load_target_source_coordinates(config) + observer = Observer.at_site("Roque de los Muchachos") time = Time(config["observation_datetime"])