summaryrefslogtreecommitdiff
path: root/stwcs/tests/test_updatewcs.py
blob: c4e58dbdb9abe1de3a8fee995295c2923aa3ba75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
from __future__ import absolute_import, division, print_function

import shutil
import os

from astropy import wcs
from astropy.io import fits
from .. import updatewcs
from ..updatewcs import apply_corrections
from ..distortion import utils as dutils
from ..wcsutil import HSTWCS
import numpy as np
from numpy.testing import utils
import pytest


from . import data
data_path = os.path.split(os.path.abspath(data.__file__))[0]


def get_filepath(filename, directory=data_path):
    return os.path.join(directory, filename)


class TestStwcs(object):

    def setup_class(self):
        acs_orig_file = get_filepath('j94f05bgq_flt.fits')
        current_dir = os.path.abspath(os.path.curdir)
        self.ref_file = get_filepath('j94f05bgq_flt_r.fits', current_dir)
        self.acs_file = get_filepath('j94f05bgq_flt.fits', current_dir)

        try:
            os.remove(self.acs_file)
            os.remove(self.ref_file)
        except OSError:
            pass
        idctab = get_filepath('postsm4_idc.fits')
        npol_file = get_filepath('qbu16424j_npl.fits')
        d2imfile = get_filepath('new_wfc_d2i.fits ')

        shutil.copyfile(acs_orig_file, self.acs_file)

        fits.setval(self.acs_file, ext=0, keyword="IDCTAB", value=idctab)
        fits.setval(self.acs_file, ext=0, keyword="NPOLFILE", value=npol_file)
        fits.setval(self.acs_file, ext=0, keyword="D2IMFILE", value=d2imfile)
        updatewcs.updatewcs(self.acs_file)
        #self.ref_file = ref_file
        shutil.copyfile(self.acs_file, self.ref_file)

        self.w1 = HSTWCS(self.acs_file, ext=1)
        self.w4 = HSTWCS(self.acs_file, ext=4)
        self.w = wcs.WCS()

    def test_default(self):
        crval = np.array([0., 0.])
        crpix = np.array([0., 0.])
        cdelt = np.array([1., 1.])
        pc = np.array([[1., 0], [0., 1.]])
        ctype = np.array(['', ''])
        utils.assert_almost_equal(self.w.wcs.crval, crval)
        utils.assert_almost_equal(self.w.wcs.crpix, crpix)
        utils.assert_almost_equal(self.w.wcs.cdelt, cdelt)
        utils.assert_almost_equal(self.w.wcs.pc, pc)
        assert((self.w.wcs.ctype == np.array(['', ''])).all())

    def test_simple_sci1(self):
        """
        A simple sanity check that CRPIX corresponds to CRVAL within wcs
        """
        px1 = np.array([self.w1.wcs.crpix])
        rd1 = np.array([self.w1.wcs.crval])
        assert(((self.w1.all_pix2world(px1, 1) - rd1) < 5E-7).all())

    def test_simple_sci2(self):
        """
        A simple sanity check that CRPIX corresponds to CRVAL within wcs
        """
        px4 = np.array([self.w4.wcs.crpix])
        rd4 = np.array([self.w4.wcs.crval])
        assert(((self.w4.all_pix2world(px4, 1) - rd4) < 2E-6).all())

    def test_pipeline_sci1(self):
        """
        Internal consistency check of the wcs pipeline
        """
        px = np.array([[100, 125]])
        sky1 = self.w1.all_pix2world(px, 1)
        dpx1 = self.w1.det2im(px, 1)
        #fpx1 = dpx1 + (self.w1.sip_pix2foc(dpx1,1)-dpx1) + (self.w1.p4_pix2foc(dpx1,1)-dpx1)
        fpx1 = dpx1 + (self.w1.sip_pix2foc(dpx1, 1)-dpx1+self.w1.wcs.crpix) + \
            (self.w1.p4_pix2foc(dpx1, 1)-dpx1)
        pipelinepx1 = self.w1.wcs_pix2world(fpx1, 1)
        utils.assert_almost_equal(pipelinepx1, sky1)

    def test_pipeline_sci2(self):
        """
        Internal consistency check of the wcs pipeline
        """
        px = np.array([[100, 125]])
        sky4 = self.w4.all_pix2world(px, 1)
        dpx4 = self.w4.det2im(px, 1)
        fpx4 = dpx4 + (self.w4.sip_pix2foc(dpx4, 1)-dpx4 + self.w4.wcs.crpix) + \
            (self.w4.p4_pix2foc(dpx4, 1)-dpx4)
        pipelinepx4 = self.w4.wcs_pix2world(fpx4, 1)
        utils.assert_almost_equal(pipelinepx4, sky4)

    def test_outwcs(self):
        """
        Test the WCS of the output image
        """
        outwcs = dutils.output_wcs([self.w1, self.w4])

        #print('outwcs.wcs.crval = {0}'.format(outwcs.wcs.crval))
        utils.assert_allclose(
            outwcs.wcs.crval, np.array([5.65109952, -72.0674181]), atol=1e-7)

        utils.assert_almost_equal(outwcs.wcs.crpix, np.array([2107.0, 2118.5]))
        utils.assert_almost_equal(
            outwcs.wcs.cd,
            np.array([[1.2787045268089949e-05, 5.4215042082174661e-06],
                      [5.4215042082174661e-06, -1.2787045268089949e-05]]))
        assert(outwcs._naxis1 == 4214)
        assert(outwcs._naxis2 == 4237)

    def test_repeate(self):
        # make sure repeated runs of updatewcs do not change the WCS.
        updatewcs.updatewcs(self.acs_file)
        w1 = HSTWCS(self.acs_file, ext=('SCI', 1))
        w4 = HSTWCS(self.acs_file, ext=('SCI', 2))
        w1r = HSTWCS(self.ref_file, ext=('SCI', 1))
        w4r = HSTWCS(self.ref_file, ext=('SCI', 2))
        utils.assert_almost_equal(w1.wcs.crval, w1r.wcs.crval)
        utils.assert_almost_equal(w1.wcs.crpix, w1r.wcs.crpix)
        utils.assert_almost_equal(w1.wcs.cdelt, w1r.wcs.cdelt)
        utils.assert_almost_equal(w1.wcs.cd, w1r.wcs.cd)
        assert((np.array(w1.wcs.ctype) == np.array(w1r.wcs.ctype)).all())
        utils.assert_almost_equal(w1.sip.a, w1r.sip.a)
        utils.assert_almost_equal(w1.sip.b, w1r.sip.b)
        utils.assert_almost_equal(w4.wcs.crval, w4r.wcs.crval)
        utils.assert_almost_equal(w4.wcs.crpix, w4r.wcs.crpix)
        utils.assert_almost_equal(w4.wcs.cdelt, w4r.wcs.cdelt)
        utils.assert_almost_equal(w4.wcs.cd, w4r.wcs.cd)
        assert((np.array(self.w4.wcs.ctype) == np.array(w4r.wcs.ctype)).all())
        utils.assert_almost_equal(w4.sip.a, w4r.sip.a)
        utils.assert_almost_equal(w4.sip.b, w4r.sip.b)


def test_remove_npol_distortion():
    acs_orig_file = get_filepath('j94f05bgq_flt.fits')
    current_dir = os.path.abspath(os.path.curdir)
    acs_file = get_filepath('j94f05bgq_flt.fits', current_dir)
    idctab = get_filepath('postsm4_idc.fits')
    npol_file = get_filepath('qbu16424j_npl.fits')
    d2imfile = get_filepath('new_wfc_d2i.fits ')

    try:
        os.remove(acs_file)
    except OSError:
        pass

    shutil.copyfile(acs_orig_file, acs_file)
    fits.setval(acs_file, ext=0, keyword="IDCTAB", value=idctab)
    fits.setval(acs_file, ext=0, keyword="NPOLFILE", value=npol_file)
    fits.setval(acs_file, ext=0, keyword="D2IMFILE", value=d2imfile)

    updatewcs.updatewcs(acs_file)
    fits.setval(acs_file, keyword="NPOLFILE", value="N/A")
    updatewcs.updatewcs(acs_file)
    w1 = HSTWCS(acs_file, ext=("SCI", 1))
    w4 = HSTWCS(acs_file, ext=("SCI", 2))
    assert w1.cpdis1 is None
    assert w4.cpdis2 is None


def test_remove_d2im_distortion():
    acs_orig_file = get_filepath('j94f05bgq_flt.fits')
    current_dir = os.path.abspath(os.path.curdir)
    acs_file = get_filepath('j94f05bgq_flt.fits', current_dir)

    idctab = get_filepath('postsm4_idc.fits')
    npol_file = get_filepath('qbu16424j_npl.fits')
    d2imfile = get_filepath('new_wfc_d2i.fits ')

    try:
        os.remove(acs_file)
    except OSError:
        pass
    shutil.copyfile(acs_orig_file, acs_file)
    fits.setval(acs_file, ext=0, keyword="IDCTAB", value=idctab)
    fits.setval(acs_file, ext=0, keyword="NPOLFILE", value=npol_file)
    fits.setval(acs_file, ext=0, keyword="D2IMFILE", value=d2imfile)

    updatewcs.updatewcs(acs_file)
    fits.setval(acs_file, keyword="D2IMFILE", value="N/A")
    updatewcs.updatewcs(acs_file)
    w1 = HSTWCS(acs_file, ext=("SCI", 1))
    w4 = HSTWCS(acs_file, ext=("SCI", 2))
    assert w1.det2im1 is None
    assert w4.det2im2 is None


def test_missing_idctab():
    """ Tests that an IOError is raised if an idctab file is not found on disk."""
    acs_orig_file = get_filepath('j94f05bgq_flt.fits')
    current_dir = os.path.abspath(os.path.curdir)
    acs_file = get_filepath('j94f05bgq_flt.fits', current_dir)

    try:
        os.remove(acs_file)
    except OSError:
        pass
    shutil.copyfile(acs_orig_file, acs_file)

    fits.setval(acs_file, keyword="IDCTAB", value="my_missing_idctab.fits")
    with pytest.raises(IOError):
        updatewcs.updatewcs(acs_file)


def test_missing_npolfile():
    """ Tests that an IOError is raised if an NPOLFILE file is not found on disk."""
    acs_orig_file = get_filepath('j94f05bgq_flt.fits')
    current_dir = os.path.abspath(os.path.curdir)
    acs_file = get_filepath('j94f05bgq_flt.fits', current_dir)

    try:
        os.remove(acs_file)
    except OSError:
        pass

    shutil.copyfile(acs_orig_file, acs_file)

    fits.setval(acs_file, keyword="NPOLFILE", value="missing_npl.fits")
    with pytest.raises(IOError):
        updatewcs.updatewcs(acs_file)


def test_missing_d2imfile():
    """ Tests that an IOError is raised if a D2IMFILE file is not found on disk."""
    acs_orig_file = get_filepath('j94f05bgq_flt.fits')
    current_dir = os.path.abspath(os.path.curdir)
    acs_file = get_filepath('j94f05bgq_flt.fits', current_dir)

    try:
        os.remove(acs_file)
    except OSError:
        pass

    shutil.copyfile(acs_orig_file, acs_file)

    fits.setval(acs_file, keyword="D2IMFILE", value="missing_d2i.fits")
    with pytest.raises(IOError):
        updatewcs.updatewcs(acs_file)


def test_found_idctab():
    """ Tests the return value of apply_corrections.foundIDCTAB()."""
    acs_orig_file = get_filepath('j94f05bgq_flt.fits')
    current_dir = os.path.abspath(os.path.curdir)
    acs_file = get_filepath('j94f05bgq_flt.fits', current_dir)
    npol_file = get_filepath('qbu16424j_npl.fits')
    d2imfile = get_filepath('new_wfc_d2i.fits ')

    try:
        os.remove(acs_file)
    except OSError:
        pass

    shutil.copyfile(acs_orig_file, acs_file)
    fits.setval(acs_file, ext=0, keyword="NPOLFILE", value=npol_file)
    fits.setval(acs_file, ext=0, keyword="D2IMFILE", value=d2imfile)
    fits.setval(acs_file, keyword="IDCTAB", value="N/A")
    corrections = apply_corrections.setCorrections(acs_file)
    assert('MakeWCS' not in corrections)
    assert('TDDCor' not in corrections)
    assert('CompSIP' not in corrections)


def test_add_radesys():
    """ test that RADESYS was successfully added to headers."""
    acs_orig_file = get_filepath('j94f05bgq_flt.fits')
    current_dir = os.path.abspath(os.path.curdir)
    acs_file = get_filepath('j94f05bgq_flt.fits', current_dir)

    idctab = get_filepath('postsm4_idc.fits')
    npol_file = get_filepath('qbu16424j_npl.fits')
    d2imfile = get_filepath('new_wfc_d2i.fits ')

    try:
        os.remove(acs_file)
    except OSError:
        pass

    shutil.copyfile(acs_orig_file, acs_file)
    fits.setval(acs_file, ext=0, keyword="IDCTAB", value=idctab)
    fits.setval(acs_file, ext=0, keyword="NPOLFILE", value=npol_file)
    fits.setval(acs_file, ext=0, keyword="D2IMFILE", value=d2imfile)

    updatewcs.updatewcs(acs_file)
    for ext in [('SCI', 1), ('SCI', 2)]:
        hdr = fits.getheader(acs_file, ext)
        assert hdr['RADESYS'] == 'FK5'

def test_update_d2im_distortion():
    acs_orig_file = get_filepath('j94f05bgq_flt.fits')
    current_dir = os.path.abspath(os.path.curdir)
    acs_file = get_filepath('j94f05bgq_flt.fits', current_dir)

    idctab = get_filepath('postsm4_idc.fits')
    npol_file = get_filepath('qbu16424j_npl.fits')
    d2imfile = get_filepath('new_wfc_d2i.fits')
    newd2im = get_filepath('new_wfc_d2i.fits', current_dir)
    try:
        os.remove(acs_file)
    except OSError:
        pass
    shutil.copyfile(acs_orig_file, acs_file)
    fits.setval(acs_file, ext=0, keyword="IDCTAB", value=idctab)
    fits.setval(acs_file, ext=0, keyword="NPOLFILE", value=npol_file)
    fits.setval(acs_file, ext=0, keyword="D2IMFILE", value=d2imfile)
    updatewcs.updatewcs(acs_file)
    d2imerr1 = fits.getval(acs_file, ext=1, keyword='D2IMERR1')
    d2imerr4 = fits.getval(acs_file, ext=4, keyword='D2IMERR1')
    shutil.copyfile(d2imfile, newd2im)
    with fits.open(newd2im, mode='update') as newf:
        for ext in newf[1:]:
            ext.data = ext.data * 100

    fits.setval(acs_file, keyword="D2IMFILE", value=newd2im)
    updatewcs.updatewcs(acs_file)
    nd2imerr1 = fits.getval(acs_file, ext=1, keyword='D2IMERR1')
    nd2imerr4 = fits.getval(acs_file, ext=4, keyword='D2IMERR1')
    assert np.isclose(d2imerr1 * 100, nd2imerr1)
    assert np.isclose(d2imerr4 * 100, nd2imerr4)


def test_apply_d2im():
    from stwcs.updatewcs import apply_corrections as appc
    acs_orig_file = get_filepath('j94f05bgq_flt.fits')
    current_dir = os.path.abspath(os.path.curdir)
    fname = get_filepath('j94f05bgq_flt.fits', current_dir)
    d2imfile = get_filepath('new_wfc_d2i.fits')
    try:
        os.remove(fname)
    except OSError:
        pass
    shutil.copyfile(acs_orig_file, fname)
    fits.setval(fname, ext=0, keyword="D2IMFILE", value=d2imfile)
    fits.setval(fname, ext=0, keyword="IDCTAB", value='N/A')
    fits.setval(fname, ext=0, keyword="NPOLFILE", value='N/A')
    # If D2IMEXT does not exist, the correction should be applied
    assert appc.apply_d2im_correction(fname, d2imcorr=True)
    updatewcs.updatewcs(fname)

    # Test the case when D2IMFILE == D2IMEXT
    assert not appc.apply_d2im_correction(fname, d2imcorr=True)
    assert not appc.apply_d2im_correction(fname, d2imcorr=False)

    fits.setval(fname, ext=0, keyword='D2IMFILE', value="N/A")
    assert not appc.apply_d2im_correction(fname, d2imcorr=True)
    # No D2IMFILE keyword in primary header
    fits.delval(fname, ext=0, keyword='D2IMFILE')
    assert not appc.apply_d2im_correction(fname, d2imcorr=True)