2222from astropy .tests .helper import assert_quantity_allclose
2323from astropy .io import fits
2424from astropy .coordinates import SkyCoord
25+ from astropy .nddata import Cutout2D
2526
2627
2728_WCSLIB_VER = Version (_wcs .__version__ )
@@ -117,11 +118,15 @@ def test_fixes():
117118 with pytest .raises (wcs .InvalidTransformError ), pytest .warns (wcs .FITSFixedWarning ) as w :
118119 wcs .WCS (header , translate_units = 'dhs' )
119120
120- assert len (w ) == 2
121+ if _WCSLIB_VER >= Version ('7.4' ):
122+ assert len (w ) == 3
123+ assert "'datfix' made the change 'Success'." in str (w .pop ().message )
124+ else :
125+ assert len (w ) == 2
121126
122127 first_wmsg = str (w [0 ].message )
123128 assert 'unitfix' in first_wmsg and 'Hz' in first_wmsg and 'M/S' in first_wmsg
124- assert 'm/s' in str (w [1 ].message )
129+ assert 'plane angle' in str ( w [ 1 ]. message ) and ' m/s' in str (w [1 ].message )
125130
126131
127132# Ignore "PV2_2 = 0.209028857410973 invalid keyvalue" warning seen on Windows.
@@ -145,6 +150,7 @@ def test_pix2world():
145150 """
146151 # TODO: write this to test the expected output behavior of pix2world,
147152 # currently this just makes sure it doesn't error out in unexpected ways
153+ # (and compares `wcs.pc` and `result` values?)
148154 filename = get_pkg_data_filename ('data/sip2.fits' )
149155 with pytest .warns (wcs .FITSFixedWarning ) as caught_warnings :
150156 # this raises a warning unimportant for this testing the pix2world
@@ -153,7 +159,10 @@ def test_pix2world():
153159 ww = wcs .WCS (filename )
154160
155161 # might as well monitor for changing behavior
156- assert len (caught_warnings ) == 1
162+ if _WCSLIB_VER >= Version ('7.4' ):
163+ assert len (caught_warnings ) == 2
164+ else :
165+ assert len (caught_warnings ) == 1
157166
158167 n = 3
159168 pixels = (np .arange (n ) * np .ones ((2 , n ))).T
@@ -162,18 +171,17 @@ def test_pix2world():
162171 # Catch #2791
163172 ww .wcs_pix2world (pixels [..., 0 ], pixels [..., 1 ], 0 , ra_dec_order = True )
164173
165- close_enough = 1e-8
166174 # assuming that the data of sip2.fits doesn't change
167175 answer = np .array ([[0.00024976 , 0.00023018 ],
168176 [0.00023043 , - 0.00024997 ]])
169177
170- assert np .all ( np . abs ( ww .wcs .pc - answer ) < close_enough )
178+ assert np .allclose ( ww .wcs .pc , answer , atol = 1.e-8 )
171179
172180 answer = np .array ([[202.39265216 , 47.17756518 ],
173181 [202.39335826 , 47.17754619 ],
174182 [202.39406436 , 47.1775272 ]])
175183
176- assert np .all ( np . abs ( result - answer ) < close_enough )
184+ assert np .allclose ( result , answer , atol = 1.e-8 , rtol = 1.e-10 )
177185
178186
179187def test_load_fits_path ():
@@ -319,13 +327,19 @@ def test_invalid_shape():
319327
320328def test_warning_about_defunct_keywords ():
321329 header = get_pkg_data_contents ('data/defunct_keywords.hdr' , encoding = 'binary' )
330+ if _WCSLIB_VER >= Version ('7.4' ):
331+ n_warn = 5
332+ else :
333+ n_warn = 4
334+
322335 # Make sure the warnings come out every time...
323336 for _ in range (2 ):
324337 with pytest .warns (wcs .FITSFixedWarning ) as w :
325338 wcs .WCS (header )
326339
327- assert len (w ) == 4
328- for item in w :
340+ assert len (w ) == n_warn
341+ # 7.4 adds a fifth warning "'datfix' made the change 'Success'."
342+ for item in w [:4 ]:
329343 assert 'PCi_ja' in str (item .message )
330344
331345
@@ -431,14 +445,14 @@ def test_find_all_wcs_crash():
431445def test_validate ():
432446 results = wcs .validate (get_pkg_data_filename ("data/validate.fits" ))
433447 results_txt = sorted (set ([x .strip () for x in repr (results ).splitlines ()]))
434- version = wcs ._wcs .__version__
435- if version [0 ] in ['6' , '7' ]:
448+ if _WCSLIB_VER >= Version ('7.4' ):
449+ filename = 'data/validate.7.4.txt'
450+ elif _WCSLIB_VER >= Version ('6.0' ):
436451 filename = 'data/validate.6.txt'
437- elif version [0 ] == '5' :
438- if version >= '5.13' :
439- filename = 'data/validate.5.13.txt'
440- else :
441- filename = 'data/validate.5.0.txt'
452+ elif _WCSLIB_VER >= Version ('5.13' ):
453+ filename = 'data/validate.5.13.txt'
454+ elif _WCSLIB_VER >= Version ('5.0' ):
455+ filename = 'data/validate.5.0.txt'
442456 else :
443457 filename = 'data/validate.txt'
444458 with open (get_pkg_data_filename (filename ), "r" ) as fd :
@@ -1450,3 +1464,40 @@ def test_no_pixel_area():
14501464
14511465 # Pixel scales still possible
14521466 assert_quantity_allclose (w .proj_plane_pixel_scales (), 1 )
1467+
1468+
1469+ @pytest .mark .skipif ('_WCSLIB_VER < Version("7.4")' ,
1470+ reason = "TPD coefficients incomplete prior to wcslib 7.4" )
1471+ def test_distortion_header (tmpdir ):
1472+ """
1473+ Test that plate distortion model is correctly described by `wcs.to_header()`
1474+ and preserved when creating a Cutout2D from the image, writing it to FITS,
1475+ and reading it back from the file.
1476+ """
1477+ path = get_pkg_data_filename ("data/dss.14.29.56-62.41.05.fits.gz" )
1478+ cen = np .array ((50 , 50 ))
1479+ siz = np .array ((20 , 20 ))
1480+
1481+ with fits .open (path ) as hdulist :
1482+ with pytest .warns (wcs .FITSFixedWarning ):
1483+ w = wcs .WCS (hdulist [0 ].header )
1484+ cut = Cutout2D (hdulist [0 ].data , position = cen , size = siz , wcs = w )
1485+
1486+ # This converts the DSS plate solution model with AMD[XY]n coefficients into a
1487+ # Template Polynomial Distortion model (TPD.FWD.n coefficients);
1488+ # not testing explicitly for the header keywords here.
1489+
1490+ w0 = wcs .WCS (w .wcs .to_header ())
1491+ assert w .pixel_to_world (0 , 0 ).separation (w0 .pixel_to_world (0 , 0 )) < 1.e-3 * u .mas
1492+ assert w .pixel_to_world (* cen ).separation (w0 .pixel_to_world (* cen )) < 1.e-3 * u .mas
1493+
1494+ w1 = wcs .WCS (cut .wcs .to_header ())
1495+ assert w .pixel_to_world (* cen ).separation (w1 .pixel_to_world (* (siz / 2 ))) < 1.e-3 * u .mas
1496+
1497+ cutfile = str (tmpdir .join ('cutout.fits' ))
1498+ fits .writeto (cutfile , cut .data , cut .wcs .to_header ())
1499+
1500+ with fits .open (cutfile ) as hdulist :
1501+ w2 = wcs .WCS (hdulist [0 ].header )
1502+
1503+ assert w .pixel_to_world (* cen ).separation (w2 .pixel_to_world (* (siz / 2 ))) < 1.e-3 * u .mas
0 commit comments