diff options
-rw-r--r-- | lib/stwcs/updatewcs/wfpc2_dgeo.py | 102 |
1 files changed, 102 insertions, 0 deletions
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) |