diff options
author | dencheva <dencheva@stsci.edu> | 2010-01-25 17:13:05 -0500 |
---|---|---|
committer | dencheva <dencheva@stsci.edu> | 2010-01-25 17:13:05 -0500 |
commit | 1c380263a895a6ce91ef24ee9c2c0445df7bbebb (patch) | |
tree | 75a3f18b0fd58d6ac5adfb1d3af7520967f3a67d | |
parent | add5bab6ddb69e48bc6f95e2f0b38e60c4fb9fed (diff) | |
download | stwcs_hcf-1c380263a895a6ce91ef24ee9c2c0445df7bbebb.tar.gz |
- If idc table is not found and idc model cannot be restored from SIP, idcmodel is always set to None.
- Two cases are considered when output_wcs is created and idcmodel for the reference image is None:
-- if idcmodel can be found, a RuntimeError will be raised and the user prompted to run 'updatewcs' or pass 'undistort=False' kw.
-- if idcmodel cannot be found, the original WCS for the reference object will be returned and used as an output WCS.
git-svn-id: http://svn.stsci.edu/svn/ssb/stsci_python/stsci_python/trunk/stwcs@8640 fe389314-cf27-0410-b35b-8c050e845b92
-rw-r--r-- | distortion/coeff_converter.py | 32 | ||||
-rw-r--r-- | distortion/utils.py | 31 | ||||
-rw-r--r-- | updatewcs/__init__.py | 6 | ||||
-rw-r--r-- | wcsutil/__init__.py | 33 |
4 files changed, 71 insertions, 31 deletions
diff --git a/distortion/coeff_converter.py b/distortion/coeff_converter.py index 807bc3f..f2eb4ad 100644 --- a/distortion/coeff_converter.py +++ b/distortion/coeff_converter.py @@ -16,12 +16,11 @@ def sip2idc(wcs): ocx11 = wcs.get('OCX11', None) ocy10 = wcs.get('OCY10', None) ocy11 = wcs.get('OCY11', None) - order = hdr.get('A_ORDER', None) - sipa, sipb = _read_sip_kw(header) - if sipa == None or sipb == None: - print 'SIP coefficients are not available.\n' + order = wcs.get('A_ORDER', None) + sipa, sipb = _read_sip_kw(wcs) + if None in [ocx10, ocx11, ocy10, ocy11, sipa, sipb]: print 'Cannot convert SIP to IDC coefficients.\n' - return + return None, None elif isinstance(wcs,pywcs.WCS): try: ocx10 = wcs.ocx10 @@ -31,27 +30,28 @@ def sip2idc(wcs): except AttributeError: print 'First order IDCTAB coefficients are not available.\n' print 'Cannot convert SIP to IDC coefficients.\n' - return + return None, None try: sipa = wcs.sip.a sipb = wcs.sip.b except AttributeError: - print 'SIP coefficients are not available.\n' + print 'SIP coefficients are not available.' print 'Cannot convert SIP to IDC coefficients.\n' - return + return None, None + try: + order = wcs.sip.a_order + except AttributeError: + print 'SIP model order unknown, exiting ...\n' + return None, None + else: print 'Input to sip2idc must be a PyFITS header or a wcsutil.HSTWCS object\n' return - try: - order = wcs.sip.a_order - except AttributeError: - print 'SIP model order unknown, exiting ...\n' - return - + if None in [ocx10, ocx11, ocy10, ocy11]: print 'First order IDC coefficients not found, exiting ...\n' - return + return idc_coeff = np.array([[ocx11, ocx10], [ocy11, ocy10]]) cx = np.zeros((order+1,order+1), dtype=np.double) cy = np.zeros((order+1,order+1), dtype=np.double) @@ -102,6 +102,8 @@ def _read_sip_kw(header): b = None return a , b + + """ def idc2sip(wcsobj, idctab = None): if isinstance(wcs,pywcs.WCS): diff --git a/distortion/utils.py b/distortion/utils.py index 06bf5f8..bc25daf 100644 --- a/distortion/utils.py +++ b/distortion/utils.py @@ -1,9 +1,11 @@ from __future__ import division # confidence high +import os import numpy as np import pywcs import pyfits from stwcs import wcsutil from numpy import sqrt, arctan2 +from pytools import fileutil def output_wcs(list_of_wcsobj, ref_wcs=None, outwcs=None, undistort=True): fra_dec = np.vstack([w.calcFootprint() for w in list_of_wcsobj]) @@ -52,6 +54,18 @@ def undistortWCS(wcsobj): import coeff_converter cx, cy = coeff_converter.sip2idc(wcsobj) + # cx, cy can be None because either there is no model available + # or updatewcs was not run. + if cx == None or cy == None: + if foundIDCTAB(wcsobj.idctab): + m = """IDCTAB is present but distortion model is missing. + Run updatewcs() to update the headers or + pass 'undistort=False' keyword to output_wcs().\n + """ + raise RuntimeError, m + else: + print 'Distortion model is not available, using input reference image for output WCS.\n' + return wcsobj.copy() crpix1 = wcsobj.wcs.crpix[0] crpix2 = wcsobj.wcs.crpix[1] xy = np.array([(crpix1,crpix2),(crpix1+1.,crpix2),(crpix1,crpix2+1.)],dtype=np.double) @@ -75,8 +89,8 @@ def undistortWCS(wcsobj): if ( _det == 0.0): print 'Singular matrix in updateWCS, aborting ...' return - #lin_wcsobj = wcsutil.HSTWCS(instrument=wcsobj.instrument) - lin_wcsobj = pywcs.WCS() #instrument=wcsobj.instrument) + + lin_wcsobj = pywcs.WCS() cd_inv = np.linalg.inv(cd_mat) lin_wcsobj.wcs.cd = np.dot(wcsobj.wcs.cd, cd_inv) @@ -120,4 +134,17 @@ def apply_idc(pixpos, cx, cy, pixref, pscale= None, order=None): return c +def foundIDCTAB(idctab): + idctab_found = True + try: + idctab = fileutil.osfn(idctab) + if idctab == 'N/A' or idctab == "": + idctab_found = False + if os.path.exists(idctab): + idctab_found = True + else: + idctab_found = False + except KeyError: + idctab_found = False + return idctab_found diff --git a/updatewcs/__init__.py b/updatewcs/__init__.py index 26834db..282d6c9 100644 --- a/updatewcs/__init__.py +++ b/updatewcs/__init__.py @@ -16,7 +16,7 @@ __docformat__ = 'restructuredtext' __version__ = '0.4' -def updatewcs(input, vacorr=True, tddcorr=True, dgeocorr=True, checkfiles=True, d2imcorr=True): +def updatewcs(input, vacorr=True, tddcorr=True, dgeocorr=True, d2imcorr=True, checkfiles=True): """ Purpose ======= @@ -46,6 +46,10 @@ def updatewcs(input, vacorr=True, tddcorr=True, dgeocorr=True, checkfiles=True, If True, vecocity aberration correction will be applied `tddcorr`: boolean If True, time dependent distortion correction will be applied + `dgeocorr`: boolean + If True, a Lookup table distortion will be applied + `d2imcorr`: boolean + If True, detector to image correction will be applied `checkfiles`: boolean If True, the format of the input files will be checked, geis and waiver fits files will be converted to MEF format. diff --git a/wcsutil/__init__.py b/wcsutil/__init__.py index dcce32f..791f3aa 100644 --- a/wcsutil/__init__.py +++ b/wcsutil/__init__.py @@ -181,25 +181,33 @@ class HSTWCS(WCS): def readModel(self, update=False, header=None): + """ + Reads distortion model from IDCTAB. + If IDCTAB is not found ('N/A', "", or not found on disk), then + if SIP coefficients and first order IDCTAb coefficients are present + in the header, restore the idcmodel from the header. + If not - assign None to self.idcmodel. + """ - if self.idctab == None: + if self.idctab == None or self.idctab == ' ': #Keyword idctab is not present in header - check for sip coefficients if header.has_key('IDCSCALE'): - self.readModelFromHeader(header) + self._readModelFromHeader(header) else: - print 'Distortion model is not available\n' - return + print "Distortion model is not available: IDCTAB=None\n" + self.idcmodel = None elif not os.path.exists(fileutil.osfn(self.idctab)): if header.has_key('IDCSCALE'): - self.readModelFromHeader(header) + self._readModelFromHeader(header) else: - print 'Distortion model is not available\n' - return + print 'Distortion model is not available: IDCTAB file %s not found\n' % self.idctab + self.idcmodel = None else: - self.readModelFromIDCTAB(header=header, update=update) + self._readModelFromIDCTAB(header=header, update=update) - def readModelFromHeader(self, header): + def _readModelFromHeader(self, header): # Recreate idc model from SIP coefficients and header kw + print 'Restoring IDC model from SIP coefficients\n' model = models.GeometryModel() cx, cy = coeff_converter.sip2idc(self) model.cx = cx @@ -219,7 +227,7 @@ class HSTWCS(WCS): self.idcmodel = model - def readModelFromIDCTAB(self, header=None, update=False): + def _readModelFromIDCTAB(self, header=None, update=False): """ Purpose ======= @@ -245,9 +253,8 @@ class HSTWCS(WCS): if update: if header==None: print 'Update header with IDC model kw requested but header was not provided\n.' - return else: - self.updatehdr(header) + self._updatehdr(header) def restore(self, header=None): @@ -281,7 +288,7 @@ class HSTWCS(WCS): self.setPscale() self.setOrient() - def updatehdr(self, ext_hdr, newkeywords=None): + def _updatehdr(self, ext_hdr): #kw2add : OCX10, OCX11, OCY10, OCY11 # record the model in the header for use by pydrizzle ext_hdr.update('OCX10', self.idcmodel.cx[1,0]) |