diff options
Diffstat (limited to 'distortion')
-rw-r--r-- | distortion/coeff_converter.py | 32 | ||||
-rw-r--r-- | distortion/utils.py | 31 |
2 files changed, 46 insertions, 17 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 |