Debian Codegraph @

asdf_astropy/converters/wcs/tests/test_wcs.py

raw
1 import warnings
2
3 import numpy as np
4 import pytest
5 from astropy.io import fits
6 from astropy.utils.data import get_pkg_data_filename, get_pkg_data_filenames
7 from astropy.wcs import WCS, DistortionLookupTable, FITSFixedWarning, Sip
8
9 from asdf_astropy.exceptions import InconsistentWCSError
10 from asdf_astropy.testing.helpers import assert_wcs_roundtrip
11
12 _astropy_test_header_filenames = list(get_pkg_data_filenames("tests/data/maps", "astropy.wcs", "*.hdr"))
13
14 _astropy_test_fits_filenames = [
15 get_pkg_data_filename(f"tests/data/{fn}", "astropy.wcs")
16 for fn in [
17 "ie6d07ujq_wcs.fits",
18 "j94f05bgq_flt.fits",
19 "sip.fits",
20 "sip2.fits",
21 ]
22 ]
23
24
25 def create_empty_wcs():
26 return WCS()
27
28
29 def create_wcs_with_attrs():
30 wcs = WCS(naxis=3)
31 wcs.pixel_shape = [100, 200, 300]
32 wcs.pixel_bounds = [[11, 22], [33, 45], [55, 67]]
33 return wcs
34
35
36 def create_sip_distortion_wcs():
37 rng = np.random.default_rng(42)
38 wcs = WCS(naxis=2)
39 wcs.wcs.crval = [251.29, 57.58]
40 wcs.wcs.cdelt = [1, 1]
41 wcs.wcs.crpix = [507, 507]
42 wcs.wcs.pc = np.array([[7.7e-6, 3.3e-5], [3.7e-5, -6.8e-6]])
43 wcs._naxis = [1014, 1014]
44 wcs.wcs.ctype = ["RA---TAN-SIP", "DEC--TAN-SIP"]
45
46 # Generate random SIP coefficients
47 a = rng.uniform(low=-1e-5, high=1e-5, size=(5, 5))
48 b = rng.uniform(low=-1e-5, high=1e-5, size=(5, 5))
49
50 # Assign SIP coefficients
51 wcs.sip = Sip(a, b, None, None, wcs.wcs.crpix)
52 wcs.wcs.set()
53
54 return wcs
55
56
57 def create_tabular_wcs():
58 # Creates a WCS object with distortion lookup tables
59 img_world_wcs = WCS(naxis=2)
60 img_world_wcs.wcs.crpix = 1, 1
61 img_world_wcs.wcs.crval = 0, 0
62 img_world_wcs.wcs.cdelt = 1, 1
63
64 # Create maps with zero distortion except at one particular pixel
65 x_dist_array = np.zeros((25, 25))
66 x_dist_array[10, 20] = 0.5
67 map_x = DistortionLookupTable(
68 x_dist_array.astype(np.float32),
69 (5, 10),
70 (10, 20),
71 (2, 2),
72 )
73 y_dist_array = np.zeros((25, 25))
74 y_dist_array[10, 5] = 0.7
75 map_y = DistortionLookupTable(
76 y_dist_array.astype(np.float32),
77 (5, 10),
78 (10, 20),
79 (3, 3),
80 )
81
82 img_world_wcs.cpdis1 = map_x
83 img_world_wcs.cpdis2 = map_y
84
85 return img_world_wcs
86
87
88 @pytest.mark.parametrize("version", ["1.5.0", "1.6.0"])
89 @pytest.mark.parametrize(
90 "wcs_gen",
91 [create_empty_wcs, create_wcs_with_attrs, create_tabular_wcs, create_sip_distortion_wcs],
92 )
93 def test_roundtrip(wcs_gen, tmp_path, version):
94 wcs = wcs_gen()
95 assert_wcs_roundtrip(wcs, tmp_path, version)
96
97
98 @pytest.mark.parametrize("fn", _astropy_test_header_filenames)
99 @pytest.mark.parametrize("version", ["1.5.0", "1.6.0"])
100 def test_astropy_data_header_roundtrip(fn, tmp_path, version):
101 with open(fn) as f:
102 header = f.read()
103
104 with warnings.catch_warnings():
105 warnings.filterwarnings("ignore", category=FITSFixedWarning)
106 wcs = WCS(header)
107
108 assert_wcs_roundtrip(wcs, tmp_path, version)
109
110
111 @pytest.mark.parametrize("fn", _astropy_test_fits_filenames)
112 @pytest.mark.parametrize("version", ["1.5.0", "1.6.0"])
113 def test_astropy_data_fits_roundtrip(fn, tmp_path, version):
114 with fits.open(fn) as ff:
115 with warnings.catch_warnings():
116 warnings.filterwarnings("ignore", category=FITSFixedWarning)
117 wcs = WCS(ff[0].header, ff)
118
119 assert_wcs_roundtrip(wcs, tmp_path, version)
120
121
122 @pytest.mark.parametrize("attr", ["pixel_shape", "pixel_bounds"])
123 def test_failure_to_convert_inconsistent(tmp_path, attr):
124 wcs = create_wcs_with_attrs()
125 wcs.naxis -= 1
126 with pytest.raises(InconsistentWCSError, match="does not match naxis"):
127 assert_wcs_roundtrip(wcs, tmp_path)