Repository: gammapy/gammapy
Pull request #5513 (2024-10-21 11:06:33)

Use `ctao_south` and `ctao_north` in observatory locations defined in Gammapy
<!-- These are hidden commments. You can leave them or delete them. -->

<!-- Don't be shy! Github issues or pull requests are welcome any time. -->
<!-- But if you first want to chat, use the Gammapy Slack. -->
<!-- See https://gammapy.org/contact.html -->

**Description**
<!-- What is the motivation for this pull request? -->
<!-- Briefly: what changes are done here? -->
<!-- If this is to address a Github issue, mention its number and use the specific keywords to create a link -->
<!-- See docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue -->
<!-- Example: This PR is to resolve #42 -->

This PR is to addresses #5501. 

**Dear reviewer**
<!-- Let the reviewer and Gammapy team know what you want: -->
<!-- * Is this ready for review? Or is it work in progress and you want some feedback? -->
<!-- * Do you want to go through review here? Or if someone should just finish this up and merge it? -->
<!-- Do you have any specific questions, e.g. about API or implementation? -->
<!-- Do you include a test executing the new code you're adding (to make sure it runs)? -->
<!-- Do you include some documentation? Is it needed? -->

@bkhelifi, regarding the log info message, should it go inside the definition of the `observatory_locations` dictionary? Maybe using `@deprecated_renamed_argument`?



codecov[bot]: ## [Codecov](https://app.codecov.io/gh/gammapy/gammapy/pull/5513?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy) Report
Attention: Patch coverage is `60.00000%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 94.60%. Comparing base [(`1545062`)](https://app.codecov.io/gh/gammapy/gammapy/commit/15450627e32c5d1205bf1fe62f92dfc1e946b9d1?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy) to head [(`34503bb`)](https://app.codecov.io/gh/gammapy/gammapy/commit/34503bb2eacb02b15f7325a4ee8f97df8328ee79?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy).
> Report is 1 commits behind head on main.

| [Files with missing lines](https://app.codecov.io/gh/gammapy/gammapy/pull/5513?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy) | Patch % | Lines |
|---|---|---|
| [gammapy/datasets/simulate.py](https://app.codecov.io/gh/gammapy/gammapy/pull/5513?src=pr&el=tree&filepath=gammapy%2Fdatasets%2Fsimulate.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy#diff-Z2FtbWFweS9kYXRhc2V0cy9zaW11bGF0ZS5weQ==) | 33.33% | [2 Missing :warning: ](https://app.codecov.io/gh/gammapy/gammapy/pull/5513?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy) |

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #5513   +/-   ##
=======================================
  Coverage   94.60%   94.60%           
=======================================
  Files         232      232           
  Lines       36282    36282           
=======================================
  Hits        34325    34325           
  Misses       1957     1957           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/gammapy/gammapy/pull/5513?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy).

morcuended: > a warning and internally overload the parameter to ctao-north such that the code continues to run

To be able to do this I propose creating a class that handles the deprecation warning. Let me know your opinions. I'll add a test for this deprecation warning as well.
Astro-Kirsty: Ahh I see the issue! 
This is not really a deprecation as it is a dictionary.

I would suggest to make it a little less complicated and stay with the 'log.warning' idea (based off your class idea):

```
class ObservatoryDict(dict):
    def __missing__(self, key):
        if key == "cta_north":
            log.warning("The key 'cta_north' is incorrect. Automatically changing the location to 'ctao_north'.")
            return self["ctao_north"]
        else:
            raise KeyError(f"'{key}' not found in observatory locations.")

observatory_locations = ObservatoryDict()
"""Gamma-ray observatory locations (dict).....
"""
```
bkhelifi: Indeed, this is a depreciation of a default value inside a dictionnary. This issue has happened couple of time and we had the only solution to send a log.info or log.warning to inform that the dict entry has changed.
One might add a dict_entry_depreciation in the utils... because this will happen in the future again and again
morcuended: Thanks for the feedback @Astro-Kirsty, @bkhelifi! I'll work it out following your suggestions.
AtreyeeS: What is the status here @morcuended ? 
I think the modification of deprecation warning can be an independent PR.
morcuended: I stopped working on it. I will resume it next week.

Patch:
diff --git a/examples/tutorials/analysis-1d/cta_sensitivity.py b/examples/tutorials/analysis-1d/cta_sensitivity.py
index d1a3814c2..5b36dd4cf 100644
--- a/examples/tutorials/analysis-1d/cta_sensitivity.py
+++ b/examples/tutorials/analysis-1d/cta_sensitivity.py
@@ -89,7 +89,7 @@ empty_dataset = SpectrumDataset.create(geom=geom, energy_axis_true=energy_axis_t
 irfs = load_irf_dict_from_file(
     "$GAMMAPY_DATA/cta-1dc/caldb/data/cta/1dc/bcf/South_z20_50h/irf_file.fits"
 )
-location = observatory_locations["cta_south"]
+location = observatory_locations["ctao_south"]
 livetime = 50.0 * u.h
 obs = Observation.create(
     pointing=pointing_info, irfs=irfs, livetime=livetime, location=location
diff --git a/examples/tutorials/analysis-1d/spectrum_simulation.py b/examples/tutorials/analysis-1d/spectrum_simulation.py
index 522f073c4..f83585688 100644
--- a/examples/tutorials/analysis-1d/spectrum_simulation.py
+++ b/examples/tutorials/analysis-1d/spectrum_simulation.py
@@ -130,7 +130,7 @@ irfs = load_irf_dict_from_file(
     "$GAMMAPY_DATA/cta-1dc/caldb/data/cta/1dc/bcf/South_z20_50h/irf_file.fits"
 )
 
-location = observatory_locations["cta_south"]
+location = observatory_locations["ctao_south"]
 obs = Observation.create(
     pointing=pointing,
     livetime=livetime,
diff --git a/examples/tutorials/analysis-3d/event_sampling.py b/examples/tutorials/analysis-3d/event_sampling.py
index d09192180..2f8ed7944 100644
--- a/examples/tutorials/analysis-3d/event_sampling.py
+++ b/examples/tutorials/analysis-3d/event_sampling.py
@@ -141,7 +141,7 @@ pointing = FixedPointingInfo(
     fixed_icrs=SkyCoord(0.0, 0.0, frame="galactic", unit="deg").icrs,
 )
 livetime = 1 * u.hr
-location = observatory_locations["cta_south"]
+location = observatory_locations["ctao_south"]
 
 
 irfs = load_irf_dict_from_file(path / irf_filename)
diff --git a/examples/tutorials/analysis-3d/event_sampling_nrg_depend_models.py b/examples/tutorials/analysis-3d/event_sampling_nrg_depend_models.py
index b9e9065e4..ef6a9f9c9 100644
--- a/examples/tutorials/analysis-3d/event_sampling_nrg_depend_models.py
+++ b/examples/tutorials/analysis-3d/event_sampling_nrg_depend_models.py
@@ -267,7 +267,7 @@ pointing = FixedPointingInfo(fixed_icrs=pointing_position)
 livetime = 1 * u.hr
 
 irfs = load_irf_dict_from_file(path / irf_filename)
-location = observatory_locations["cta_south"]
+location = observatory_locations["ctao_south"]
 
 observation = Observation.create(
     obs_id=1001,
diff --git a/examples/tutorials/analysis-3d/simulate_3d.py b/examples/tutorials/analysis-3d/simulate_3d.py
index aebbc01e7..dc7d7af1d 100644
--- a/examples/tutorials/analysis-3d/simulate_3d.py
+++ b/examples/tutorials/analysis-3d/simulate_3d.py
@@ -145,7 +145,7 @@ print(models)
 #
 
 # Create an in-memory observation
-location = observatory_locations["cta_south"]
+location = observatory_locations["ctao_south"]
 obs = Observation.create(
     pointing=pointing, livetime=livetime, irfs=irfs, location=location
 )
diff --git a/examples/tutorials/analysis-time/light_curve_simulation.py b/examples/tutorials/analysis-time/light_curve_simulation.py
index 656103462..d0105a39f 100644
--- a/examples/tutorials/analysis-time/light_curve_simulation.py
+++ b/examples/tutorials/analysis-time/light_curve_simulation.py
@@ -195,7 +195,7 @@ for idx in range(n_obs):
         irfs=irfs,
         reference_time=gti_t0,
         obs_id=idx,
-        location=observatory_locations["cta_south"],
+        location=observatory_locations["ctao_south"],
     )
     empty_i = empty.copy(name=f"dataset-{idx}")
     dataset = maker.run(empty_i, obs)
diff --git a/gammapy/datasets/simulate.py b/gammapy/datasets/simulate.py
index b8f462e63..aadbfc4a5 100644
--- a/gammapy/datasets/simulate.py
+++ b/gammapy/datasets/simulate.py
@@ -532,11 +532,11 @@ class MapDatasetEventSampler:
         if loc is None:
             if telescope == "CTA":
                 if instrument == "Southern Array":
-                    loc = observatory_locations["cta_south"]
+                    loc = observatory_locations["ctao_south"]
                 elif instrument == "Northern Array":
-                    loc = observatory_locations["cta_north"]
+                    loc = observatory_locations["ctao_north"]
                 else:
-                    loc = observatory_locations["cta_south"]
+                    loc = observatory_locations["ctao_south"]
 
             else:
                 loc = observatory_locations[telescope.lower()]
diff --git a/gammapy/utils/observers.py b/gammapy/utils/observers.py
index 1ab9a77ea..5cd6da0db 100644
--- a/gammapy/utils/observers.py
+++ b/gammapy/utils/observers.py
@@ -1,5 +1,6 @@
 # Licensed under a 3-clause BSD style license - see LICENSE.rst
 """Location of gamma-ray observatories."""
+
 import astropy.units as u
 from astropy.coordinates import EarthLocation
 
@@ -19,9 +20,9 @@ Not that with ``EarthLocation`` the orientation of angles is as follows:
 
 Available observatories (alphabetical order):
 
-- ``cta_south`` and ``cta_north`` for CTA, see
-  `Website <https://www.cta-observatory.org/>`__ and
-  `Wikipedia <https://en.wikipedia.org/wiki/Cherenkov_Telescope_Array>`__
+- ``ctao_south`` and ``ctao_north`` for CTAO, see
+  `Website <https://www.ctao-observatory.org/>`__ and
+  `Wikipedia <https://en.wikipedia.org/wiki/Cherenkov_Telescope_Array_Observatory>`__
 - ``hawc`` for HAWC, see
   `Website <https://www.hawc-observatory.org/>`__ and
   `Wikipedia <https://en.wikipedia.org/wiki/High_Altitude_Water_Cherenkov_Experiment>`__
@@ -52,14 +53,19 @@ Examples
 # Latitude: 24d41m0.34s South, Longitude: 70d18m58.84s West, Height: not given
 # Email from Gernot Maier on Sep 8, 2017, stating what they use in the CTA MC group:
 # lon=-70.31634499364885d, lat=-24.68342915473787d, height=2150m
+observatory_locations["ctao_south"] = EarthLocation(
+    lon="-70d18m58.84s", lat="-24d41m0.34s", height="2150m"
+)
 observatory_locations["cta_south"] = EarthLocation(
     lon="-70d18m58.84s", lat="-24d41m0.34s", height="2150m"
 )
-
 # Values from https://www.cta-observatory.org/about/array-locations/la-palma/
 # Latitude: 28d45m43.7904s North, Longitude: 17d53m31.218s West, Height: 2200 m
 # Email from Gernot Maier on Sep 8, 2017, stating what they use in the CTA MC group for MST-1:
 # lon=-17.891571d, lat=28.762158d, height=2147m
+observatory_locations["ctao_north"] = EarthLocation(
+    lon="-17d53m31.218s", lat="28d45m43.7904s", height="2147m"
+)
 observatory_locations["cta_north"] = EarthLocation(
     lon="-17d53m31.218s", lat="28d45m43.7904s", height="2147m"
 )


Test patch:
diff --git a/gammapy/data/tests/test_metadata.py b/gammapy/data/tests/test_metadata.py
index 6f65c1d3c..b0c6b3dd2 100644
--- a/gammapy/data/tests/test_metadata.py
+++ b/gammapy/data/tests/test_metadata.py
@@ -39,7 +39,7 @@ def test_observation_metadata():
     input = {
         "obs_info": ObsInfoMetaData(**obs_info),
         "pointing": PointingInfoMetaData(),
-        "location": "cta_north",
+        "location": "ctao_north",
         "deadtime_fraction": 0.05,
         "time_info": time_info,
         "target": TargetMetaData(**target),
diff --git a/gammapy/data/tests/test_pointing.py b/gammapy/data/tests/test_pointing.py
index 7b3ef1673..c2bf0602b 100644
--- a/gammapy/data/tests/test_pointing.py
+++ b/gammapy/data/tests/test_pointing.py
@@ -15,7 +15,7 @@ from gammapy.utils.time import time_ref_to_dict, time_relative_to_ref
 
 def test_fixed_pointing_icrs():
     """Test new api of FixedPointingInfo in ICRS (POINTING)"""
-    location = observatory_locations["cta_south"]
+    location = observatory_locations["ctao_south"]
     fixed_icrs = SkyCoord(ra=83.28 * u.deg, dec=21.78 * u.deg)
 
     pointing = FixedPointingInfo(
@@ -61,7 +61,7 @@ def test_fixed_pointing_icrs():
 
 def test_fixed_pointing_info_altaz():
     """Test new api of FixedPointingInfo in AltAz (DRIFT)"""
-    location = observatory_locations["cta_south"]
+    location = observatory_locations["ctao_south"]
     fixed_altaz = SkyCoord(alt=70 * u.deg, az=0 * u.deg, frame=AltAz())
     pointing = FixedPointingInfo(
         fixed_altaz=fixed_altaz,
@@ -239,7 +239,7 @@ class TestPointingInfo:
     ],
 )
 def test_fixed_pointing_info_fixed_icrs_from_meta(obs_mode):
-    location = observatory_locations["cta_south"]
+    location = observatory_locations["ctao_south"]
     start = Time("2020-11-01T03:00:00")
     stop = Time("2020-11-01T03:15:00")
     ref = Time("2020-11-01T00:00:00")
@@ -287,7 +287,7 @@ def test_fixed_pointing_info_fixed_icrs_from_meta(obs_mode):
 
 
 def test_fixed_pointing_info_fixed_altaz_from_meta():
-    location = observatory_locations["cta_south"]
+    location = observatory_locations["ctao_south"]
     start = Time("2020-11-01T03:00:00")
     stop = Time("2020-11-01T03:15:00")
     ref = Time("2020-11-01T00:00:00")