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

Specify source also via coordinates #36

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/source/changes/36.added.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow to define a source also via its celestial coordinates.
41 changes: 35 additions & 6 deletions src/iact_estimator/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 16 additions & 9 deletions src/iact_estimator/resources/config.yml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
27 changes: 22 additions & 5 deletions src/iact_estimator/scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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}*")]
Expand All @@ -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(
Expand Down Expand Up @@ -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"])

Expand Down
Loading