diff options
Diffstat (limited to 'updatewcs')
-rw-r--r-- | updatewcs/__init__.py | 43 | ||||
-rw-r--r-- | updatewcs/apply_corrections.py | 26 | ||||
-rw-r--r-- | updatewcs/corrections.py | 10 | ||||
-rw-r--r-- | updatewcs/det2im.py | 22 | ||||
-rw-r--r-- | updatewcs/makewcs.py | 8 | ||||
-rw-r--r-- | updatewcs/npol.py | 20 |
6 files changed, 90 insertions, 39 deletions
diff --git a/updatewcs/__init__.py b/updatewcs/__init__.py index c43b016..08a0a46 100644 --- a/updatewcs/__init__.py +++ b/updatewcs/__init__.py @@ -13,12 +13,19 @@ import npol, det2im from pytools import parseinput, fileutil import apply_corrections +import time +import logging +logger = logging.getLogger('stwcs.updatewcs') + +import atexit +atexit.register(logging.shutdown) + #Note: The order of corrections is important __docformat__ = 'restructuredtext' def updatewcs(input, vacorr=True, tddcorr=True, npolcorr=True, d2imcorr=True, - checkfiles=True, wcskey=" ", wcsname=" ", clobber=False): + checkfiles=True, wcskey=" ", wcsname=" ", clobber=False, verbose=False): """ Updates HST science files with the best available calibration information. @@ -67,23 +74,33 @@ def updatewcs(input, vacorr=True, tddcorr=True, npolcorr=True, d2imcorr=True, clobber: boolean a flag for reusing the wcskey when archiving the primary WCS """ + if verbose == False: + logger.setLevel(100) + else: + logger.setLevel(verbose) + args = "vacorr=%s, tddcorr=%s, npolcorr=%s, d2imcorr=%s, checkfiles=%s, \ + wcskey=%s, wcsname=%s, clobber=%s" % (str(vacorr), str(tddcorr), str(npolcorr), + str(d2imcorr), str(checkfiles), str(wcskey), + str(wcsname), str(clobber)) + logger.info('\n\tStarting UPDATEWCS: %s', time.asctime()) + files = parseinput.parseinput(input)[0] + logger.info("\n\tInput files: %s, " % [i for i in files]) + logger.info("\n\tInput arguments: %s" %args) if checkfiles: files = checkFiles(files) if not files: print 'No valid input, quitting ...\n' return + for f in files: acorr = apply_corrections.setCorrections(f, vacorr=vacorr, \ tddcorr=tddcorr,npolcorr=npolcorr, d2imcorr=d2imcorr) - if 'MakeWCS' in acorr and newIDCTAB(f): - print "New IDCTAB file detected. This invalidates all WCS's." - print "Deleting all previous WCS's" + logger.warning("\n\tNew IDCTAB file detected. All current WCSs will be deleted") cleanWCS(f) #restore the original WCS keywords - #wcsutil.restoreWCS(f, ext=[], wcskey='O', clobber=True) makecorr(f, acorr, wkey=wcskey, wname=wcsname, clobber=False) return files @@ -280,6 +297,7 @@ def checkFiles(input): Converts geis and waiver fits files to multiextension fits. """ from pytools.check_files import geis2mef, waiver2mef, checkFiles + logger.info("\n\tChecking files %s" % input) removed_files = [] newfiles = [] if not isinstance(input, list): @@ -289,8 +307,7 @@ def checkFiles(input): try: imgfits,imgtype = fileutil.isFits(file) except IOError: - print "Warning: File %s could not be found\n" %file - print "Removing file %s from input list" %file + logger.warning( "\n\tFile %s could not be found, removing it from the input list.\n" %file) removed_files.append(file) continue # Check for existence of waiver FITS input, and quit if found. @@ -299,7 +316,7 @@ def checkFiles(input): if imgtype == 'waiver': newfilename = waiver2mef(file, convert_dq=True) if newfilename == None: - print "Removing file %s from input list - could not convert waiver to mef" %file + logger.warning("\n\tRemoving file %s from input list - could not convert waiver to mef" %file) removed_files.append(file) else: newfiles.append(newfilename) @@ -312,17 +329,17 @@ def checkFiles(input): if not imgfits: newfilename = geis2mef(file, convert_dq=True) if newfilename == None: - print "Removing file %s from input list - could not convert geis to mef" %file + logger.warning("\n\tRemoving file %s from input list - could not convert geis to mef" %file) removed_files.append(file) else: newfiles.append(newfilename) if removed_files: - print 'The following files will be removed from the list of files to be processed :\n' - for f in removed_files: - print f + logger.warning('\n\tThe following files will be removed from the list of files to be processed %s' % removed_files) + #for f in removed_files: + # print f newfiles = checkFiles(newfiles)[0] - + logger.info("\n\tThese files passed the input check and will be processed: %s" % newfiles) return newfiles def newIDCTAB(fname): diff --git a/updatewcs/apply_corrections.py b/updatewcs/apply_corrections.py index a7149d9..881e5d1 100644 --- a/updatewcs/apply_corrections.py +++ b/updatewcs/apply_corrections.py @@ -7,6 +7,9 @@ from pytools import fileutil import os.path from stwcs.wcsutil import altwcs +import logging +logger = logging.getLogger("stwcs.updatewcs.apply_corrections") + #Note: The order of corrections is important __docformat__ = 'restructuredtext' @@ -58,6 +61,7 @@ def setCorrections(fname, vacorr=True, tddcorr=True, npolcorr=True, d2imcorr=Tru if 'DET2IMCorr' in acorr: d2imcorr = applyD2ImCorr(fname, d2imcorr) if d2imcorr == False: acorr.remove('DET2IMCorr') + logger.info("\n\tCorrections to be applied to %s: %s " % (fname, str(acorr))) return acorr def foundIDCTAB(fname): @@ -127,8 +131,10 @@ def applyNpolCorr(fname, unpolcorr): return False fnpol0 = fileutil.osfn(fnpol0) if not fileutil.findFile(fnpol0): - print 'Kw "NPOLFILE" exists in primary header but file %s not found\n' % fnpol0 - print 'Non-polynomial distortion correction will not be applied\n' + msg = """\n\tKw "NPOLFILE" exists in primary header but file %s not found + Non-polynomial distortion correction will not be applied\n + """ % fnpol0 + logger.critical(msg) applyNPOLCorr = False return applyNPOLCorr try: @@ -139,6 +145,9 @@ def applyNpolCorr(fname, unpolcorr): if fnpol0 != fnpol1: applyNPOLCorr = True else: + msg = """\n\tNPOLEXT with the same value as NPOLFILE found in first extension. + NPOL correction will not be applied.""" + logger.info(msg) applyNPOLCorr = False else: # npl file defined in first extension may not be found @@ -150,7 +159,7 @@ def applyNpolCorr(fname, unpolcorr): # in first extension header applyNPOLCorr = True except KeyError: - print '"NPOLFILE" keyword not found in primary header' + logger.info('\n\t"NPOLFILE" keyword not found in primary header') applyNPOLCorr = False return applyNPOLCorr @@ -167,8 +176,9 @@ def isOldStyleDGEO(fname, dgname): dg_naxis1 = pyfits.getval(dgname, 'NAXIS1', ext=1) dg_naxis2 = pyfits.getval(dgname, 'NAXIS2', ext=1) if sci_naxis1 <= dg_naxis1 or sci_naxis2 <= dg_naxis2: - print 'Only full size (old style) NPL file was found.' - print 'Non-polynomial distortion correction will not be applied.\n' + msg = """\n\tOnly full size (old style) DGEO file was found.\n + Non-polynomial distortion correction will not be applied.""" + logger.critical(msg) return True else: return False @@ -182,8 +192,10 @@ def applyD2ImCorr(fname, d2imcorr): return False fd2im0 = fileutil.osfn(fd2im0) if not fileutil.findFile(fd2im0): - print 'Kw D2IMFILE exists in primary header but file %s not found\n' % fd2im0 - print 'Detector to image correction will not be applied\n' + 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 applyD2IMCorr = False return applyD2IMCorr try: diff --git a/updatewcs/corrections.py b/updatewcs/corrections.py index 6f2aa0b..2bbdfb1 100644 --- a/updatewcs/corrections.py +++ b/updatewcs/corrections.py @@ -7,6 +7,9 @@ from pytools import fileutil from utils import diff_angles import makewcs, npol +import logging, time +logger=logging.getLogger('stwcs.updatewcs.corrections') + MakeWCS = makewcs.MakeWCS NPOLCorr = npol.NPOLCorr @@ -55,13 +58,12 @@ class TDDCorr(object): .. [1] Jay Anderson, "Variation of the Distortion Solution of the WFC", ACS ISR 2007-08. """ - def updateWCS(cls, ext_wcs, ref_wcs): """ - Calculates alpha and beta for ACS/WFC data. - Writes 2 new kw to the extension header: TDDALPHA and TDDBETA """ - + logger.info("\n\tStarting TDDCorr: %s" % time.asctime()) alpha, beta = cls.compute_alpha_beta(ext_wcs) cls.apply_tdd2idc(ref_wcs, alpha, beta) cls.apply_tdd2idc(ext_wcs, alpha, beta) @@ -156,8 +158,8 @@ class VACorr(object): The correction is applied to the CD matrix and CRVALs. """ - def updateWCS(cls, ext_wcs, ref_wcs): + logger.info("\n\tStarting VACorr: %s" % time.asctime()) if ext_wcs.vafactor != 1: ext_wcs.wcs.cd = ext_wcs.wcs.cd * ext_wcs.vafactor crval0 = ref_wcs.wcs.crval[0] + ext_wcs.vafactor*diff_angles(ext_wcs.wcs.crval[0], @@ -193,8 +195,8 @@ class CompSIP(object): Conference Series, Vol. 347, 2005 """ - def updateWCS(cls, ext_wcs, ref_wcs): + logger.info("\n\tStarting CompSIP: %s" %time.asctime()) kw2update = {} order = ext_wcs.idcmodel.norder kw2update['A_ORDER'] = order diff --git a/updatewcs/det2im.py b/updatewcs/det2im.py index 6eb7fa7..b59ca17 100644 --- a/updatewcs/det2im.py +++ b/updatewcs/det2im.py @@ -1,9 +1,13 @@ from __future__ import division # confidence high +import time import pyfits from pytools import fileutil import utils +import logging +logger = logging.getLogger('stwcs.updatewcs.Det2IM') + class DET2IMCorr(object): """ Stores a small correction to the detector coordinates as a d2imarr @@ -44,7 +48,12 @@ class DET2IMCorr(object): Uses the file pointed to in the primary header keyword 'D2IMFILE' to create an extension with a detector to image correction. """ - assert isinstance(fobj, pyfits.HDUList) + logger.info("\n\tStarting Det2IM Correction: %s" % time.asctime()) + try: + assert isinstance(fobj, pyfits.HDUList) + except AssertionError: + logger.exception('\n\tInput must be a pyfits.HDUList object') + raise d2imfile = fileutil.osfn(fobj[0].header['D2IMFILE']) axiscorr = cls.getAxisCorr(d2imfile) @@ -64,13 +73,12 @@ class DET2IMCorr(object): if direction == 'DX': return 1 elif direction == 'DY': return 2 else: - print '\tDET2IM correction expects the reference file to have' - print '\tan EXTNAME keyword of value "DX" or "DY"' + logger.warning('\n\tDET2IM correction expects the reference file to have \ + an EXTNAME keyword of value "DX" or "DY", EXTNAMe %s detected' % direction) return None - except AttributeError: - print "\tAxis to which to apply the detector to image " - print "\tcorrection cannot be determined because the reference " - print "\tfile %s is missing a keyword EXTNAME" % refname + except KeyError: + logger.exception("\n\tD2IMFILE %s is missing EXTNAME keyword. Unable to determine axis \ + to which to apply the correction." % refname) direction = None return direction getAxisCorr = classmethod(getAxisCorr) diff --git a/updatewcs/makewcs.py b/updatewcs/makewcs.py index 4210fd3..4037877 100644 --- a/updatewcs/makewcs.py +++ b/updatewcs/makewcs.py @@ -6,6 +6,9 @@ from math import sin, sqrt, pow, cos, asin, atan2,pi import utils from pytools import fileutil +import logging, time +logger = logging.getLogger("stwcs.updatewcs.makewcs") + class MakeWCS(object): """ Recompute basic WCS keywords based on PA_V3 and distortion model. @@ -36,6 +39,7 @@ class MakeWCS(object): """ recomputes the basic WCS kw """ + logger.info("\n\tStarting MakeWCS: %s" % time.asctime()) ltvoff, offshift = cls.getOffsets(ext_wcs) v23_corr = cls.zero_point_corr(ext_wcs) @@ -173,13 +177,13 @@ class MakeWCS(object): alpha = hwcs.idcmodel.refpix['TDDALPHA'] beta = hwcs.idcmodel.refpix['TDDBETA'] except KeyError: - return np.array([[0., 0.],[0.,0.]]) + v23_corr = np.array([[0., 0.],[0.,0.]]) tdd = np.array([[beta, alpha], [alpha, -beta]]) mrotp = fileutil.buildRotMatrix(2.234529)/2048. xy0 = np.array([[cls.tdd_xyref[hwcs.chip][0]-2048.], [cls.tdd_xyref[hwcs.chip][1]-2048.]]) v23_corr = np.dot(mrotp,np.dot(tdd,xy0)) * 0.05 - + logger.debug("\n\tTDD Zero point correction for chip %s: %s" % (hwcs.chip, v23_corr)) return v23_corr zero_point_corr = classmethod(zero_point_corr) diff --git a/updatewcs/npol.py b/updatewcs/npol.py index fb2c128..62c44bd 100644 --- a/updatewcs/npol.py +++ b/updatewcs/npol.py @@ -5,6 +5,9 @@ from pytools import fileutil import utils import numpy as np +import logging, time +logger = logging.getLogger('stwcs.updatewcs.npol') + class NPOLCorr(object): """ Defines a Lookup table prior distortion correction as per WCS paper IV. @@ -38,7 +41,13 @@ class NPOLCorr(object): Science file, for which a distortion correction in a NPOLFILE is available """ - assert isinstance(fobj, pyfits.HDUList) + logger.info("\n\tStarting CompSIP: %s" %time.asctime()) + try: + assert isinstance(fobj, pyfits.HDUList) + except AssertionError: + logger.exception('\n\tInput must be a pyfits.HDUList object') + raise + cls.applyNPOLCorr(fobj) nplfile = fobj[0].header['NPOLFILE'] @@ -107,7 +116,7 @@ class NPOLCorr(object): continue if ename == 'WCSDVARR': wcsd[fobj[e].header['EXTVER']] = e - + logger.debug("A map of WSCDVARR externsions %s" % wcsd) return wcsd getWCSIndex = classmethod(getWCSIndex) @@ -188,12 +197,13 @@ class NPOLCorr(object): ocy11 = header['OCY11'] coeffs = np.array([[ocx11, ocx10], [ocy11,ocy10]], dtype=np.float32) except KeyError: - print 'First order IDCTAB coefficients are not available.\n' - print 'Cannot convert SIP to IDC coefficients.\n' + logger.exception('\n\tFirst order IDCTAB coefficients are not available. \n\ + Cannot convert SIP to IDC coefficients.') return None try: idcscale = header['IDCSCALE'] except KeyError: + logger.exception("IDCSCALE not found in header - setting it to 1.") idcscale = 1 return np.linalg.inv(coeffs/idcscale) @@ -220,13 +230,11 @@ class NPOLCorr(object): """ npl = pyfits.open(npolfile) for ext in npl: - #for i in range(len(dgf)): try: nplextname = ext.header['EXTNAME'] nplextver = ext.header['EXTVER'] except KeyError: continue - #dgccdchip = ext.header.get('CCDCHIP', 0) nplccdchip = cls.get_ccdchip(npl, extname=nplextname, extver=nplextver) if nplextname == npl_extname and nplccdchip == ccdchip: npol_header = ext.header |