From 5504b779007fb160bbc95265b2aa2421bc49c446 Mon Sep 17 00:00:00 2001 From: Chiara Marmo Date: Sat, 18 Nov 2023 18:15:09 +0100 Subject: [PATCH 1/4] Fix support of custom WCS mapping. --- astropy/wcs/tests/test_utils.py | 44 ++++++++++++++++++++++++++++++++- astropy/wcs/utils.py | 4 +-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/astropy/wcs/tests/test_utils.py b/astropy/wcs/tests/test_utils.py index 36a851dcaea..050946389c2 100644 --- a/astropy/wcs/tests/test_utils.py +++ b/astropy/wcs/tests/test_utils.py @@ -29,6 +29,8 @@ from astropy.utils.exceptions import AstropyUserWarning from astropy.wcs import _wcs from astropy.wcs.utils import ( + FRAME_WCS_MAPPINGS, + WCS_FRAME_MAPPINGS, _pixel_to_pixel_correlation_matrix, _pixel_to_world_correlation_matrix, _split_matrix, @@ -417,7 +419,10 @@ def test_wcs_to_body_frame(): unknown_wcs = WCS(naxis=2) unknown_wcs.wcs.ctype = ["UTLN-TAN", "UTLT-TAN"] - with pytest.raises(KeyError, match="unknown solar system object abbreviation UT"): + with pytest.raises( + ValueError, + match="Could not determine celestial frame corresponding to the specified WCS object", + ): frame = wcs_to_celestial_frame(unknown_wcs) triaxial_wcs = WCS(naxis=2) @@ -1593,3 +1598,40 @@ def test_obsgeo_infinite(dkist_location): def test_obsgeo_invalid(obsgeo): with pytest.raises(ValueError): obsgeo_to_frame(obsgeo, None) + + +def test_custom_wcs_to_from_frame(): + # See https://github.com/astropy/astropy/issues/15625 + # test from Sam van Kooten + + class CustomFrame(BaseCoordinateFrame): + obstime = Time("2017-08-17T12:41:04.43") + + def custom_wcs_frame_mapping(wcs): + ctypes = {c[:4] for c in wcs.wcs.ctype} + if not ({"CSLN", "CSLT"} <= ctypes): + return None + + dateobs = wcs.wcs.dateavg or wcs.wcs.dateobs or None + custom_frame = CustomFrame() + return custom_frame + + def custom_frame_wcs_mapping(frame, projection="TAN"): + if not isinstance(frame, CustomFrame): + return None + wcs = WCS(naxis=2) + wcs.wcs.ctype = [f"CSLN-{projection}", f"CSLT-{projection}"] + return wcs + + FRAME_WCS_MAPPINGS.append([custom_wcs_frame_mapping]) + WCS_FRAME_MAPPINGS.append([custom_frame_wcs_mapping]) + + mywcs = WCS(naxis=2) + mywcs.wcs.ctype = ["CSLN-TAN", "CSLT-TAN"] + custom_frame = custom_wcs_frame_mapping(mywcs) + assert isinstance(custom_frame, CustomFrame) + + custom_wcs = custom_frame_wcs_mapping(custom_frame) + print(custom_wcs.wcs.ctype) + assert custom_wcs.wcs.ctype[0] == "CSLN-TAN" + assert custom_wcs.wcs.ctype[1] == "CSLT-TAN" diff --git a/astropy/wcs/utils.py b/astropy/wcs/utils.py index a7149fe16a5..ed98fc4e41a 100644 --- a/astropy/wcs/utils.py +++ b/astropy/wcs/utils.py @@ -158,13 +158,11 @@ def _wcs_to_celestial_frame_builtin(wcs): representation_type=SphericalRepresentation, obstime=wcs.wcs.dateobs or None, ) - elif xcoord[2:4] in ("LN", "LT") and "H" not in xcoord and "CR" not in xcoord: + elif xcoord[2:4] in ("LN", "LT") and xcoord[:2] in SOLAR_SYSTEM_OBJ_DICT.keys(): # Coordinates on a planetary body, as defined in # https://agupubs.onlinelibrary.wiley.com/doi/10.1029/2018EA000388 object_name = SOLAR_SYSTEM_OBJ_DICT.get(xcoord[:2]) - if object_name is None: - raise KeyError(f"unknown solar system object abbreviation {xcoord[:2]}") a_radius = wcs.wcs.aux.a_radius b_radius = wcs.wcs.aux.b_radius From b285d5ec0f6d128d98d2a815860811fae0e061ed Mon Sep 17 00:00:00 2001 From: Chiara Marmo Date: Sat, 18 Nov 2023 19:09:02 +0100 Subject: [PATCH 2/4] Fix append. --- astropy/wcs/tests/test_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/astropy/wcs/tests/test_utils.py b/astropy/wcs/tests/test_utils.py index 050946389c2..ead7aac6cb2 100644 --- a/astropy/wcs/tests/test_utils.py +++ b/astropy/wcs/tests/test_utils.py @@ -1623,8 +1623,8 @@ def custom_frame_wcs_mapping(frame, projection="TAN"): wcs.wcs.ctype = [f"CSLN-{projection}", f"CSLT-{projection}"] return wcs - FRAME_WCS_MAPPINGS.append([custom_wcs_frame_mapping]) - WCS_FRAME_MAPPINGS.append([custom_frame_wcs_mapping]) + WCS_FRAME_MAPPINGS.append([custom_wcs_frame_mapping]) + FRAME_WCS_MAPPINGS.append([custom_frame_wcs_mapping]) mywcs = WCS(naxis=2) mywcs.wcs.ctype = ["CSLN-TAN", "CSLT-TAN"] From 3da88ab57582bb82c82941d58680413974108178 Mon Sep 17 00:00:00 2001 From: Chiara Marmo Date: Mon, 20 Nov 2023 21:47:54 +0100 Subject: [PATCH 3/4] Add changelog. --- docs/changes/wcs/15630.bugfix.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 docs/changes/wcs/15630.bugfix.rst diff --git a/docs/changes/wcs/15630.bugfix.rst b/docs/changes/wcs/15630.bugfix.rst new file mode 100644 index 00000000000..35ff04bd494 --- /dev/null +++ b/docs/changes/wcs/15630.bugfix.rst @@ -0,0 +1,2 @@ +Fix a regression in custom WCS mapping due to the recent introduction of +Solar System frames. From 4116facd08391453e1a443fecdadb119ad926c7d Mon Sep 17 00:00:00 2001 From: Chiara Marmo Date: Tue, 21 Nov 2023 21:16:18 +0100 Subject: [PATCH 4/4] Trig CI