From 1f87e08c731e5da975659e6aa17de042625404d2 Mon Sep 17 00:00:00 2001 From: Nadia Dencheva Date: Fri, 9 Dec 2016 14:35:25 -0500 Subject: save work --- stwcs/updatewcs/apply_corrections.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stwcs/updatewcs/apply_corrections.py b/stwcs/updatewcs/apply_corrections.py index 4a8e5ac..f3da018 100644 --- a/stwcs/updatewcs/apply_corrections.py +++ b/stwcs/updatewcs/apply_corrections.py @@ -158,7 +158,7 @@ def applyNpolCorr(fname, unpolcorr): fnpol0 = fileutil.osfn(fnpol0) if not fileutil.findFile(fnpol0): msg = '"NPOLFILE" exists in primary header but file {0} not found.' - 'Non-polynomial distortion correction will not be applied.'.format(file) + 'Non-polynomial distortion correction will not be applied.'.format(fnpol0) logger.critical(msg) raise IOError("NPOLFILE {0} not found".format(fnpol0)) try: -- cgit From a6eb705d7bcb11ae1951a9fef0c8256a49f5f52c Mon Sep 17 00:00:00 2001 From: Nadia Dencheva Date: Tue, 13 Dec 2016 15:07:24 -0500 Subject: fix problem with updating d2im file --- stwcs/updatewcs/apply_corrections.py | 70 ++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/stwcs/updatewcs/apply_corrections.py b/stwcs/updatewcs/apply_corrections.py index f3da018..c554564 100644 --- a/stwcs/updatewcs/apply_corrections.py +++ b/stwcs/updatewcs/apply_corrections.py @@ -66,7 +66,7 @@ def setCorrections(fname, vacorr=True, tddcorr=True, npolcorr=True, d2imcorr=Tru if not npolcorr: acorr.remove('NPOLCorr') if 'DET2IMCorr' in acorr: - d2imcorr = applyD2ImCorr(fname, d2imcorr) + d2imcorr = apply_d2im_correction(fname, d2imcorr) if not d2imcorr: acorr.remove('DET2IMCorr') logger.info("Corrections to be applied to {0} {1}".format(fname, acorr)) @@ -211,40 +211,42 @@ def isOldStyleDGEO(fname, dgname): return False -def applyD2ImCorr(fname, d2imcorr): +def apply_d2im_correction(fname, d2imcorr): applyD2IMCorr = True + if not d2imcorr: + logger.info("D2IM correction not requested - not applying it.") + return False + # get D2IMFILE kw from primary header try: - # get D2IMFILE kw from primary header fd2im0 = fits.getval(fname, 'D2IMFILE') - if fd2im0 == 'N/A': - utils.remove_distortion(fname, "D2IMFILE") - return False - fd2im0 = fileutil.osfn(fd2im0) - if not fileutil.findFile(fd2im0): - msg = """\n\tKw D2IMFILE exists in primary header but file %s not found\n - Detector to image correction will not be applied\n""" % fd2im0 - logger.critical(msg) - print(msg) - raise IOError("D2IMFILE {0} not found".format(fd2im0)) - try: - # get D2IMEXT kw from first extension header - fd2imext = fits.getval(fname, 'D2IMEXT', ext=1) - fd2imext = fileutil.osfn(fd2imext) - if fd2imext and fileutil.findFile(fd2imext): - if fd2im0 != fd2imext: - applyD2IMCorr = True - else: - applyD2IMCorr = False - else: - # D2IM file defined in first extension may not be found - # but if a valid kw exists in the primary header, - # detector to image correction should be applied. - applyD2IMCorr = True - except KeyError: - # the case of D2IMFILE kw present in primary header but D2IMEXT missing - # in first extension header - applyD2IMCorr = True except KeyError: - print('D2IMFILE keyword not found in primary header') - applyD2IMCorr = False - return applyD2IMCorr + logger.info("D2IMFILE keyword is missing - D2IM correction will not be applied.") + + if fd2im0 == 'N/A': + utils.remove_distortion(fname, "D2IMFILE") + return False + fd2im0 = fileutil.osfn(fd2im0) + if not fileutil.findFile(fd2im0): + message = "D2IMFILE {0} not found.".format(fname) + logger.critical(message) + raise IOError(message) + try: + # get D2IMEXT kw from first extension header + fd2imext = fits.getval(fname, 'D2IMEXT', ext=1) + + except KeyError: + # the case of D2IMFILE kw present in primary header but D2IMEXT missing + # in first extension header + return True + fd2imext = fileutil.osfn(fd2imext) + if fd2imext and fileutil.findFile(fd2imext): + if fd2im0 != fd2imext: + applyD2IMCorr = True + else: + applyD2IMCorr = False + else: + # D2IM file defined in first extension may not be found + # but if a valid kw exists in the primary header, + # detector to image correction should be applied. + applyD2IMCorr = True + return applyD2IMCorr -- cgit From 4b46410a60318138892d9726eb2f472f619d425d Mon Sep 17 00:00:00 2001 From: Nadia Dencheva Date: Thu, 15 Dec 2016 16:58:28 -0500 Subject: Add tests --- stwcs/tests/test_altwcs.py | 3 -- stwcs/tests/test_headerlet.py | 19 ++++++----- stwcs/tests/test_updatewcs.py | 65 +++++++++++++++++++++++++++++++++--- stwcs/updatewcs/apply_corrections.py | 24 ++++++++++++- 4 files changed, 94 insertions(+), 17 deletions(-) diff --git a/stwcs/tests/test_altwcs.py b/stwcs/tests/test_altwcs.py index d646106..fa7eb9b 100644 --- a/stwcs/tests/test_altwcs.py +++ b/stwcs/tests/test_altwcs.py @@ -117,7 +117,6 @@ class TestAltWCS(object): def test_restore_wcs_from_to(self): # test restore from ... to ... - #altwcs.archiveWCS(self.acs_file, ext=[('SCI',1), ('SCI',2)], wcskey='T') pyfits.setval(self.acs_file, ext=('SCI', 1), keyword='CRVAL1', value=1) pyfits.setval(self.acs_file, ext=('SCI', 2), keyword='CRVAL1', value=1) f = pyfits.open(self.acs_file, mode='update') @@ -140,9 +139,7 @@ class TestAltWCS(object): compare_wcs(w3, w1o, exclude_keywords=['ctype']) def test_delete_wcs(self): - #altwcs.archiveWCS(self.acs_file, ext=1, wcskey='Z') altwcs.deleteWCS(self.acs_file, ext=1, wcskey='Z') - #utils.assert_raises(KeyError, wcsutil.HSTWCS, self.acs_file, ext=1, wcskey='Z') with pytest.raises(KeyError): wcsutil.HSTWCS(self.acs_file, ext=1, wcskey='Z') diff --git a/stwcs/tests/test_headerlet.py b/stwcs/tests/test_headerlet.py index f4fa050..462b7e4 100644 --- a/stwcs/tests/test_headerlet.py +++ b/stwcs/tests/test_headerlet.py @@ -8,7 +8,7 @@ from ..wcsutil import headerlet, wcsdiff from ..wcsutil import HSTWCS import numpy as np from numpy.testing import utils -from nose.tools import * +import pytest from . import data data_path = os.path.split(os.path.abspath(data.__file__))[0] @@ -100,7 +100,6 @@ class TestCreateHeaderlet(object): assert(wcsdiff.is_wcs_identical(self.simple_file, self.headerlet_name, [0], [1], verbose=True)[0]) - @raises(KeyError) def test_no_HDRNAME_no_WCSNAME(self): """ Test create_headerlet stepping through all @@ -110,7 +109,8 @@ class TestCreateHeaderlet(object): shutil.copyfile(self.comp_file, newf) fits.delval(newf, 'HDRNAME', ext=1) fits.delval(newf, 'WCSNAME', ext=1) - hlet = headerlet.create_headerlet(newf) + with pytest.raises(KeyError): + hlet = headerlet.create_headerlet(newf) def test1SciExt(self): """ @@ -176,23 +176,24 @@ class TestApplyHeaderlet: hlet.apply_as_primary('comp.fits') """ - @raises(ValueError) + def testWrongSIPModel(self): hlet = headerlet.create_headerlet(self.comp_file, hdrname='test1', sipname='WRONG') - hlet.apply_as_primary(self.comp_file) + with pytest.raises(ValueError): + hlet.apply_as_primary(self.comp_file) - @raises(ValueError) def testWrongNPOLModel(self): hlet = headerlet.create_headerlet(self.comp_file, hdrname='test1', npolfile='WRONG') - hlet.apply_as_primary(self.comp_file) + with pytest.raises(ValueError): + hlet.apply_as_primary(self.comp_file) - @raises(ValueError) def testWrongD2IMModel(self): hlet = headerlet.create_headerlet(self.comp_file, hdrname='test1', d2imfile='WRONG') - hlet.apply_as_primary(self.comp_file) + with pytest.raises(ValueError): + hlet.apply_as_primary(self.comp_file) def test_apply_as_primary_method(self): hlet = headerlet.create_headerlet(self.comp_file, hdrname='test2') diff --git a/stwcs/tests/test_updatewcs.py b/stwcs/tests/test_updatewcs.py index b7e75c8..4afd7cd 100644 --- a/stwcs/tests/test_updatewcs.py +++ b/stwcs/tests/test_updatewcs.py @@ -296,12 +296,69 @@ def test_add_radesys(): fits.setval(acs_file, ext=0, keyword="NPOLFILE", value=npol_file) fits.setval(acs_file, ext=0, keyword="D2IMFILE", value=d2imfile) - #shutil.copyfile('orig/ibof01ahq_flt.fits', './ibof01ahq_flt.fits') updatewcs.updatewcs(acs_file) - # updatewcs.updatewcs('ibof01ahq_flt.fits') for ext in [('SCI', 1), ('SCI', 2)]: hdr = fits.getheader(acs_file, ext) assert hdr['RADESYS'] == 'FK5' - #hdr = fits.getheader('ibof01ahq_flt.fits', ext=('SCI', 1)) - #assert hdr['RADESYS'] == 'ICRS' +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) + print('newd2im', newd2im) + 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) + newf = fits.open(newd2im, mode='update') + for ext in newf[1:]: + ext.data *= 100 + newf.close() + 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) diff --git a/stwcs/updatewcs/apply_corrections.py b/stwcs/updatewcs/apply_corrections.py index c554564..247dba0 100644 --- a/stwcs/updatewcs/apply_corrections.py +++ b/stwcs/updatewcs/apply_corrections.py @@ -212,6 +212,28 @@ def isOldStyleDGEO(fname, dgname): def apply_d2im_correction(fname, d2imcorr): + """ + Logic to decide whether to apply the D2IM correction. + + Parameters + ---------- + fname : str + Science file name. + d2imcorr : bool + Flag indicating if D2IM is should be enabled if allowed. + + Return + ------ + applyD2IMCorr : bool + Flag whether to apply the correction. + + The D2IM correction is applied to a science file if it is in the + allowed corrections for the instrument. The name of the file + with the correction is saved in the ``D2IMFILE`` keyword in the + primary header. When the correction is applied the name of the + file is saved in the ``D2IMEXT`` keyword in the 1st extension header. + + """ applyD2IMCorr = True if not d2imcorr: logger.info("D2IM correction not requested - not applying it.") @@ -221,7 +243,7 @@ def apply_d2im_correction(fname, d2imcorr): fd2im0 = fits.getval(fname, 'D2IMFILE') except KeyError: logger.info("D2IMFILE keyword is missing - D2IM correction will not be applied.") - + return False if fd2im0 == 'N/A': utils.remove_distortion(fname, "D2IMFILE") return False -- cgit From 75887a8631a01869636cc5111e925fed473beec8 Mon Sep 17 00:00:00 2001 From: Nadia Dencheva Date: Thu, 15 Dec 2016 19:54:02 -0500 Subject: fix test --- stwcs/tests/test_headerlet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stwcs/tests/test_headerlet.py b/stwcs/tests/test_headerlet.py index 462b7e4..ab250a8 100644 --- a/stwcs/tests/test_headerlet.py +++ b/stwcs/tests/test_headerlet.py @@ -107,7 +107,7 @@ class TestCreateHeaderlet(object): """ newf = get_filepath('ncomp.fits', os.path.abspath(os.path.curdir)) shutil.copyfile(self.comp_file, newf) - fits.delval(newf, 'HDRNAME', ext=1) + #fits.delval(newf, 'HDRNAME', ext=1) fits.delval(newf, 'WCSNAME', ext=1) with pytest.raises(KeyError): hlet = headerlet.create_headerlet(newf) -- cgit From c4ca4b31d032b0e1ccab795843c1d1933bcbb7e1 Mon Sep 17 00:00:00 2001 From: Nadia Dencheva Date: Thu, 15 Dec 2016 20:17:22 -0500 Subject: reference pytest correctly --- setup.cfg | 2 +- stwcs/tests/test_updatewcs.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index d8d65c1..8d7a78d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -8,7 +8,7 @@ license = BSD edit_on_github = False github_project = spacetelescope/stwcs -[pytest] +[tool:pytest] minversion = 2.2 norecursedirs = build docs/_build relic diff --git a/stwcs/tests/test_updatewcs.py b/stwcs/tests/test_updatewcs.py index 4afd7cd..ca034fa 100644 --- a/stwcs/tests/test_updatewcs.py +++ b/stwcs/tests/test_updatewcs.py @@ -325,7 +325,7 @@ def test_update_d2im_distortion(): shutil.copyfile(d2imfile, newd2im) newf = fits.open(newd2im, mode='update') for ext in newf[1:]: - ext.data *= 100 + ext.data = ext.data * 100 newf.close() fits.setval(acs_file, keyword="D2IMFILE", value=newd2im) updatewcs.updatewcs(acs_file) -- cgit From aed4fedd83502944f3401b382b02bbddc862cfe2 Mon Sep 17 00:00:00 2001 From: Nadia Dencheva Date: Fri, 16 Dec 2016 16:01:24 -0500 Subject: remove a print statement --- stwcs/tests/test_updatewcs.py | 1 - 1 file changed, 1 deletion(-) diff --git a/stwcs/tests/test_updatewcs.py b/stwcs/tests/test_updatewcs.py index ca034fa..675e78f 100644 --- a/stwcs/tests/test_updatewcs.py +++ b/stwcs/tests/test_updatewcs.py @@ -310,7 +310,6 @@ def test_update_d2im_distortion(): npol_file = get_filepath('qbu16424j_npl.fits') d2imfile = get_filepath('new_wfc_d2i.fits') newd2im = get_filepath('new_wfc_d2i.fits', current_dir) - print('newd2im', newd2im) try: os.remove(acs_file) except OSError: -- cgit From 68255f10fccc6171658f31b1550290f4f4d59a28 Mon Sep 17 00:00:00 2001 From: Nadia Dencheva Date: Fri, 16 Dec 2016 16:34:43 -0500 Subject: try to fix test failing on travis but passing on redaht --- stwcs/tests/test_updatewcs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/stwcs/tests/test_updatewcs.py b/stwcs/tests/test_updatewcs.py index 675e78f..218f4f2 100644 --- a/stwcs/tests/test_updatewcs.py +++ b/stwcs/tests/test_updatewcs.py @@ -325,6 +325,7 @@ def test_update_d2im_distortion(): newf = fits.open(newd2im, mode='update') for ext in newf[1:]: ext.data = ext.data * 100 + newf.flush() newf.close() fits.setval(acs_file, keyword="D2IMFILE", value=newd2im) updatewcs.updatewcs(acs_file) -- cgit From 61a9c34dabee4cfad1c2e97cbee1cba5be7331e4 Mon Sep 17 00:00:00 2001 From: Nadia Dencheva Date: Sun, 18 Dec 2016 15:33:15 -0500 Subject: use with - cm --- stwcs/tests/test_updatewcs.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/stwcs/tests/test_updatewcs.py b/stwcs/tests/test_updatewcs.py index 218f4f2..c4e58db 100644 --- a/stwcs/tests/test_updatewcs.py +++ b/stwcs/tests/test_updatewcs.py @@ -322,11 +322,10 @@ def test_update_d2im_distortion(): d2imerr1 = fits.getval(acs_file, ext=1, keyword='D2IMERR1') d2imerr4 = fits.getval(acs_file, ext=4, keyword='D2IMERR1') shutil.copyfile(d2imfile, newd2im) - newf = fits.open(newd2im, mode='update') - for ext in newf[1:]: - ext.data = ext.data * 100 - newf.flush() - newf.close() + 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') -- cgit From 0bd69fca03ec01f8b51bdf50531c19fcfeaa9be1 Mon Sep 17 00:00:00 2001 From: Nadia Dencheva Date: Sun, 18 Dec 2016 15:59:07 -0500 Subject: changelog entry [skip ci] --- CHANGES.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 6b9da19..ef7a82c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,9 @@ - updatewcs() now reads all extension immediately after opening a file to fix a problem after astropy implemented fits lazy loading. [#21] +- Fixed a bug in updating the D2IM correction in a science file when the + a new distortion file was supplied through D2IMFILE keyword. [#22] + 1.2.4 (2016-10-27) ------------------ -- cgit