diff options
Diffstat (limited to 'lib/stwcs')
| -rw-r--r-- | lib/stwcs/updatewcs/apply_corrections.py | 8 | ||||
| -rw-r--r-- | lib/stwcs/updatewcs/wfpc2_dgeo.py | 102 | 
2 files changed, 109 insertions, 1 deletions
| diff --git a/lib/stwcs/updatewcs/apply_corrections.py b/lib/stwcs/updatewcs/apply_corrections.py index 74feb82..86b623a 100644 --- a/lib/stwcs/updatewcs/apply_corrections.py +++ b/lib/stwcs/updatewcs/apply_corrections.py @@ -1,4 +1,4 @@ -from __future__ import division, print_function # confidence high +from __future__ import division, print_function  # confidence high  import os  from astropy.io import fits @@ -7,6 +7,7 @@ from stsci.tools import fileutil  import os.path  from stwcs.wcsutil import altwcs  from . import utils +from . import wfpc2_dgeo  import logging  logger = logging.getLogger("stwcs.updatewcs.apply_corrections") @@ -42,6 +43,11 @@ def setCorrections(fname, vacorr=True, tddcorr=True, npolcorr=True, d2imcorr=Tru      # make a copy of this list !      acorr = allowed_corrections[instrument][:] +    # For WFPC2 images, the old-style DGEOFILE needs to be +    # converted on-the-fly into a proper D2IMFILE here... +    if instrument == 'WFPC2': +        # check for DGEOFILE, and convert it to D2IMFILE if found +        d2imfile = wfpc2_dgeo.update_wfpc2_d2geofile(fname)      # Check if idctab is present on disk      # If kw IDCTAB is present in the header but the file is      # not found on disk, do not run TDDCorr, MakeCWS and CompSIP diff --git a/lib/stwcs/updatewcs/wfpc2_dgeo.py b/lib/stwcs/updatewcs/wfpc2_dgeo.py new file mode 100644 index 0000000..b7b064a --- /dev/null +++ b/lib/stwcs/updatewcs/wfpc2_dgeo.py @@ -0,0 +1,102 @@ +""" wfpc2_dgeo - Functions to convert WFPC2 DGEOFILE into D2IMFILE + +""" +import os +import datetime + +import astropy +from astropy.io import fits +import numpy as np + +from stsci.tools import fileutil + +import logging +logger = logging.getLogger("stwcs.updatewcs.apply_corrections") + +def update_wfpc2_d2geofile(filename, fhdu=None): +    """ +    Creates a D2IMFILE from the DGEOFILE for a WFPC2 image (input), and +    modifies the header to reflect the new usage. +    """ +     +    close_fhdu = False +    if fhdu is None: +        fhdu = fileutil.openImage(filename, mode='update') +        close_fhdu = True + +    dgeofile = fhdu['PRIMARY'].header.get('DGEOFILE', None) +    if dgeofile not in [None, "N/A", "", " "]: +        logger.info('Converting DGEOFILE %s into D2IMFILE...' % dgeofile) +        rootname = filename[:filename.find('.fits')] +        d2imfile = convert_dgeo_to_d2im(dgeofile,rootname) +        fhdu['PRIMARY'].header['ODGEOFIL'] = dgeofile +        fhdu['PRIMARY'].header['DGEOFILE'] = 'N/A' +        fhdu['PRIMARY'].header['D2IMFILE'] = d2imfile +    else: +        d2imfile = None +        fhdu['PRIMARY'].header['DGEOFILE'] = 'N/A' +        fhdu['PRIMARY'].header['D2IMFILE'] = 'N/A' + +    # Only close the file handle if opened in this function +    if close_fhdu: +        fhdu.close() + +    # return the d2imfile name so that calling routine can keep +    # track of the new file created and delete it later if necessary +    # (multidrizzle clean=True mode of operation) +    return d2imfile + +def convert_dgeo_to_d2im(dgeofile,output,clobber=True): +    """ Routine that converts the WFPC2 DGEOFILE into a D2IMFILE. +    """ +    dgeo = fileutil.openImage(dgeofile) +    outname = output+'_d2im.fits' + +    removeFileSafely(outname) +    data = np.array([dgeo['dy',1].data[:,0]]) +    scihdu = fits.ImageHDU(data=data) +    dgeo.close() +    # add required keywords for D2IM header +    scihdu.header['EXTNAME'] = ('DY', 'Extension name') +    scihdu.header['EXTVER'] = (1, 'Extension version') +    fits_str = 'PYFITS Version '+str(astropy.__version__) +    scihdu.header['ORIGIN'] = (fits_str, 'FITS file originator') +    scihdu.header['INHERIT'] = (False, 'Inherits global header') + +    dnow = datetime.datetime.now() +    scihdu.header['DATE'] = (str(dnow).replace(' ','T'), +                             'Date FITS file was generated') + +    scihdu.header['CRPIX1'] = (0, 'Distortion array reference pixel') +    scihdu.header['CDELT1'] = (1, 'Grid step size in first coordinate') +    scihdu.header['CRVAL1'] = (0, 'Image array pixel coordinate') +    scihdu.header['CRPIX2'] = (0, 'Distortion array reference pixel') +    scihdu.header['CDELT2'] = (1, 'Grid step size in second coordinate') +    scihdu.header['CRVAL2'] = (0, 'Image array pixel coordinate') + +    phdu = fits.PrimaryHDU() +    phdu.header['INSTRUME'] = 'WFPC2' +    d2imhdu = fits.HDUList() +    d2imhdu.append(phdu) +    scihdu.header['DETECTOR'] = (1, 'CCD number of the detector: PC 1, WFC 2-4 ') +    d2imhdu.append(scihdu.copy()) +    scihdu.header['EXTVER'] = (2, 'Extension version') +    scihdu.header['DETECTOR'] = (2, 'CCD number of the detector: PC 1, WFC 2-4 ') +    d2imhdu.append(scihdu.copy()) +    scihdu.header['EXTVER'] = (3, 'Extension version') +    scihdu.header['DETECTOR'] = (3, 'CCD number of the detector: PC 1, WFC 2-4 ') +    d2imhdu.append(scihdu.copy()) +    scihdu.header['EXTVER'] = (4, 'Extension version') +    scihdu.header['DETECTOR'] = (4, 'CCD number of the detector: PC 1, WFC 2-4 ') +    d2imhdu.append(scihdu.copy()) +    d2imhdu.writeto(outname) +    d2imhdu.close() + +    return outname + + +def removeFileSafely(filename,clobber=True): +    """ Delete the file specified, but only if it exists and clobber is True. +    """ +    if filename is not None and filename.strip() != '': +        if os.path.exists(filename) and clobber: os.remove(filename) | 
