diff options
-rw-r--r-- | lib/stwcs/updatewcs/apply_corrections.py | 4 | ||||
-rw-r--r-- | lib/stwcs/updatewcs/utils.py | 33 |
2 files changed, 35 insertions, 2 deletions
diff --git a/lib/stwcs/updatewcs/apply_corrections.py b/lib/stwcs/updatewcs/apply_corrections.py index b189b43..74feb82 100644 --- a/lib/stwcs/updatewcs/apply_corrections.py +++ b/lib/stwcs/updatewcs/apply_corrections.py @@ -6,6 +6,7 @@ import time from stsci.tools import fileutil import os.path from stwcs.wcsutil import altwcs +from . import utils import logging logger = logging.getLogger("stwcs.updatewcs.apply_corrections") @@ -80,7 +81,6 @@ def foundIDCTAB(fname): """ try: - #idctab = fileutil.osfn(fits.getval(fname, 'IDCTAB')) idctab = fits.getval(fname, 'IDCTAB').strip() if idctab == 'N/A' or idctab == "": return False @@ -146,6 +146,7 @@ def applyNpolCorr(fname, unpolcorr): # get NPOLFILE kw from primary header fnpol0 = fits.getval(fname, 'NPOLFILE') if fnpol0 == 'N/A': + utils.remove_distortion(fname, "NPOLFILE") return False fnpol0 = fileutil.osfn(fnpol0) if not fileutil.findFile(fnpol0): @@ -208,6 +209,7 @@ def applyD2ImCorr(fname, d2imcorr): # 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): diff --git a/lib/stwcs/updatewcs/utils.py b/lib/stwcs/updatewcs/utils.py index e5f4deb..9a66225 100644 --- a/lib/stwcs/updatewcs/utils.py +++ b/lib/stwcs/updatewcs/utils.py @@ -1,8 +1,12 @@ from __future__ import division # confidence high import os - +from astropy.io import fits from stsci.tools import fileutil +import logging +logger = logging.getLogger("stwcs.updatewcs.utils") + + def diff_angles(a,b): """ Perform angle subtraction a-b taking into account @@ -231,3 +235,30 @@ def build_d2imname(fobj, d2imfile=None): if d2imname == 'NONE': d2imname = 'NOMODEL' return d2imname, d2imfile + + +def remove_distortion(fname, dist_keyword): + logger.info("Removing distortion {0} from file {0}".format(dist_keyword, fname)) + from ..wcsutil import altwcs + if dist_keyword == "NPOLFILE": + extname = "WCSDVARR" + keywords = ["CPERR*", "DP1.*", "DP2.*", "CPDIS*", "NPOLEXT"] + elif dist_keyword == "D2IMFILE": + extname = "D2IMARR" + keywords = ["D2IMERR*", "D2IM1.*", "D2IM2.*", "D2IMDIS*", "D2IMEXT"] + else: + raise AttributeError("Unrecognized distortion keyword " + "{0} when attempting to remove distortion".format(dist_keyword)) + ext_mapping = altwcs.mapFitsExt2HDUListInd(fname, "SCI").values() + f = fits.open(fname, mode="update") + for hdu in ext_mapping: + for kw in keywords: + try: + del f[hdu].header[kw] + except AttributeError: + pass + ext_mapping = altwcs.mapFitsExt2HDUListInd(fname, extname).values() + ext_mapping.sort() + for hdu in ext_mapping[::-1]: + del f[hdu] + f.close() |