Debian Codegraph @

asdf_astropy/converters/wcs/tests/test_highlevelwcswrapper.py

raw
1 import asdf
2 import pytest
3 from astropy.wcs import WCS
4 from astropy.wcs.wcsapi import HighLevelWCSWrapper
5 from astropy.wcs.wcsapi.wrappers.sliced_wcs import SlicedLowLevelWCS
6
7 from asdf_astropy.testing.helpers import assert_gwcs_equal, assert_wcs_equal
8
9 try:
10 import gwcs
11 except ImportError:
12 gwcs = None
13
14
15 @pytest.fixture
16 def sliced_astropy_wcs_4d(astropy_wcs_4d):
17 return SlicedLowLevelWCS(astropy_wcs_4d, 1)
18
19
20 @pytest.fixture
21 def hl_wcs(request):
22 return HighLevelWCSWrapper(request.getfixturevalue(request.param))
23
24
25 @pytest.mark.parametrize(
26 "hl_wcs",
27 [
28 "gwcs_4d_identity_units",
29 "astropy_wcs_4d",
30 "sliced_astropy_wcs_4d",
31 ],
32 indirect=True,
33 )
34 def test_hllwcs_serialization(hl_wcs, tmp_path):
35 file_path = tmp_path / "test_highlevelwcswrapper.asdf"
36 with asdf.AsdfFile() as af:
37 af["hl_wcs"] = hl_wcs
38 af.write_to(file_path, version="1.6.0")
39
40 ll_wcs = hl_wcs.low_level_wcs
41 with asdf.open(file_path) as af:
42 loaded_hl_wcs = af["hl_wcs"]
43 loaded_ll_wcs = loaded_hl_wcs._low_level_wcs
44
45 # Unwrap the SlicedLowLevelWCS
46 if isinstance(loaded_ll_wcs, SlicedLowLevelWCS):
47 assert isinstance(ll_wcs, SlicedLowLevelWCS)
48 assert loaded_ll_wcs._slices_array == ll_wcs._slices_array
49 loaded_ll_wcs = loaded_ll_wcs._wcs
50 ll_wcs = ll_wcs._wcs
51
52 if isinstance(loaded_ll_wcs, WCS):
53 assert_wcs_equal(ll_wcs, loaded_ll_wcs)
54 elif gwcs and isinstance(loaded_ll_wcs, gwcs.WCS):
55 assert_gwcs_equal(loaded_ll_wcs, ll_wcs)
56 else:
57 msg = f"Loaded an unexpected type: {type(loaded_ll_wcs)}"
58 raise TypeError(msg)