diff options
author | dencheva <dencheva@stsci.edu> | 2009-02-10 15:58:37 -0500 |
---|---|---|
committer | dencheva <dencheva@stsci.edu> | 2009-02-10 15:58:37 -0500 |
commit | 914af36db70dafd6417198634ce1e55a5a5dd8d9 (patch) | |
tree | c878658f842a9deb072ecf494076e87ac31a42bd | |
parent | cc6eb9ab94b41a2f5ab513d21971bc6bfaa036c4 (diff) | |
download | stwcs_hcf-914af36db70dafd6417198634ce1e55a5a5dd8d9.tar.gz |
Modified the constructor for HSTWCS. Now it can be created given:
- a filename and extension number
- fully qualified filename
- a pyfits object and an extension number
- if given an instrument and detctor parameters, a template HSTWCS
object for that instrument will be created.
git-svn-id: http://svn.stsci.edu/svn/ssb/stsci_python/development/trunk/updatewcs@7534 fe389314-cf27-0410-b35b-8c050e845b92
-rw-r--r-- | hstwcs/__init__.py | 19 | ||||
-rw-r--r-- | hstwcs/makewcs.py | 2 | ||||
-rw-r--r-- | setup.py.old | 89 | ||||
-rw-r--r-- | wcsutil/__init__.py | 156 |
4 files changed, 104 insertions, 162 deletions
diff --git a/hstwcs/__init__.py b/hstwcs/__init__.py index 84f6cec..dfb2e4b 100644 --- a/hstwcs/__init__.py +++ b/hstwcs/__init__.py @@ -75,20 +75,20 @@ def makecorr(fname, allowed_corr): """ f = pyfits.open(fname, mode='update') - #Determine the reference chip and make a copy of its restored header. + #Determine the reference chip and create the reference HSTWCS object nrefchip, nrefext = getNrefchip(f) - primhdr = f[0].header - ref_hdr = f[nrefext].header.copy() - utils.write_archive(ref_hdr) - - for extn in f: + ref_wcs = HSTWCS(fobj=f, ext=nrefext) + ref_wcs.readModel(update=True,header=f[nrefext].header) + utils.write_archive(f[nrefext].header) + + for i in range(len(f))[1:]: # Perhaps all ext headers should be corrected (to be consistent) + extn = f[i] if extn.header.has_key('extname') and extn.header['extname'].lower() == 'sci': - ref_wcs = HSTWCS(primhdr, ref_hdr, fobj=f) - ref_wcs.readModel(update=True, header=ref_hdr) + ref_wcs.restore(f[nrefext].header) hdr = extn.header - ext_wcs = HSTWCS(primhdr, hdr, fobj=f) utils.write_archive(hdr) + ext_wcs = HSTWCS(fobj=f, ext=i) ext_wcs.readModel(update=True,header=hdr) for c in allowed_corr: if c != 'DGEOCorr': @@ -103,7 +103,6 @@ def makecorr(fname, allowed_corr): f[1].header.update(kw, kw2update[kw]) f.close() - def getNrefchip(fobj): """ This handles the fact that WFPC2 subarray observations diff --git a/hstwcs/makewcs.py b/hstwcs/makewcs.py index 650d251..809daab 100644 --- a/hstwcs/makewcs.py +++ b/hstwcs/makewcs.py @@ -1,4 +1,3 @@ -#from .. mappings import DEGTORAD, RADTODEG from updatewcs import DEGTORAD, RADTODEG import numpy from math import sin, sqrt, pow, cos, asin, atan2,pi @@ -42,7 +41,6 @@ class MakeWCS(object): cls.uprefwcs(ext_wcs, ref_wcs, rv23_corr, ltvoff, offshift) cls.upextwcs(ext_wcs, ref_wcs, v23_corr, rv23_corr, ltvoff, offshift) - kw2update = {'CD1_1': ext_wcs.wcs.cd[0,0], 'CD1_2': ext_wcs.wcs.cd[0,1], diff --git a/setup.py.old b/setup.py.old deleted file mode 100644 index a4a57a2..0000000 --- a/setup.py.old +++ /dev/null @@ -1,89 +0,0 @@ -from distutils.core import setup, Extension -from os.path import join - -###################################################################### -# NUMPY -try: - import numpy -except ImportError: - print "numpy must be installed to build pywcs." - print "ABORTING." - sys.exit(1) - -try: - numpy_include = numpy.get_include() -except AttributeError: - numpy_include = numpy.get_numpy_include() - -###################################################################### -# PyFITS -try: - import pyfits -except ImportError: - print "WARNING: PyFITS must be installed to use pywcs." - print " Since this is not a build-time dependency, the build will proceed." - -###################################################################### -###################################################################### -# WCSLIB -WCSVERSION = "4.3" -WCSLIB = "wcslib-%s" % WCSVERSION # Path to wcslib -WCSLIBC = join('pywcs', WCSLIB, "C") # Path to wcslib source files -WCSFILES = [ # List of wcslib files to compile - 'flexed/wcsulex.c', - 'flexed/wcsutrn.c', - 'cel.c', - 'lin.c', - 'log.c', - 'prj.c', - 'spc.c', - 'sph.c', - 'spx.c', - 'tab.c', - 'wcs.c', - 'wcsfix.c', - 'wcshdr.c', - 'wcspih.c', - 'wcstrig.c', - 'wcsunits.c', - 'wcsutil.c'] -WCSFILES = [join(WCSLIBC, x) for x in WCSFILES] -###################################################################### - -###################################################################### -# GENERATE DOCSTRINGS IN C -docstrings = {} -execfile("pywcs/doc/docstrings.py", docstrings) -keys = docstrings.keys() -keys.sort() -fd = open("pywcs/src/docstrings.h", "w") -fd.write('/* This file is autogenerated by setup.py. To edit its contents\n') -fd.write(' edit doc/docstrings.py\n') -fd.write('*/\n') -for key in keys: - if key.startswith('__'): - continue - val = docstrings[key].lstrip().encode("string_escape").replace('"', '\\"') - fd.write("static const char doc_%s[] = \"%s\";\n\n" % (key, val)) -fd.close() - -###################################################################### - -setup(name="hstwcs", - version="0.1", - description="HST WCS Corrections", - packages=['hstwcs', 'hstwcs/pywcs', 'hstwcs/updatewcs', 'hstwcs/wcsutil', 'hstwcs/distortion'], - package_dir={'hstwcs':'lib', 'hstwcs/pywcs': 'pywcs/pywcs', 'hstwcs/updatewcs': 'updatewcs', - 'hstwcs/wcsutil': 'wcsutil', 'hstwcs/distortion': 'distortion'}, - ext_modules=[ - Extension('hstwcs.pywcs._pywcs', - ['pywcs/src/pywcs.c'] + - WCSFILES, - include_dirs=[ - numpy_include, - WCSLIBC, - "src" - ] - ) - ] - ) diff --git a/wcsutil/__init__.py b/wcsutil/__init__.py index b682032..d2f4d8c 100644 --- a/wcsutil/__init__.py +++ b/wcsutil/__init__.py @@ -13,6 +13,7 @@ import mappings from mappings import inst_mappings, ins_spec_kw from mappings import basic_wcs, prim_hdr_kw + __docformat__ = 'restructuredtext' __version__ = '0.3' @@ -27,70 +28,75 @@ class HSTWCS(WCS): instrument specific attributes needed by the correction classes. """ - def __init__(self, hdr0=None, ehdr=None, fobj=None, fname=None, instrument=None): + def __init__(self, fobj=None, ext=None, instrument=None, detector=None): """ :Parameters: - `fname`: string - file/extension name - filename[EXTNAME,EXTNUM] - filename[extension_number] - `hdr0`: Pyfits Header - primary header - `ehdr`: Pyfits Header - extension header - `fobj`: PyFITS HDUList object or None - pyfits file object + `fobj`: string or PyFITS HDUList object or None + a file name, e.g j9irw4b1q_flt.fits + a fully qualified filename[EXTNAME,EXTNUM], e.g. j9irw4b1q_flt.fits[sci,1] + a pyfits file object, e.g pyfits.open('j9irw4b1q_flt.fits') + `ext`: int or None + extension number + if ext==None, it is assumed the data is in the primary hdu `instrument`: string one of 'ACS', 'NICMOS', 'WFPC2', 'STIS', 'WFC3' + `detector`: string + for example 'WFC' + If instrument and detector parameters are give, a default HSTWCS + instrument is created. """ self.inst_kw = ins_spec_kw - if fname == None and hdr0 == None and ehdr == None and instrument != None: - # create a default HSTWCS object based on instrument only + if instrument == None: + filename, hdr0, ehdr = self.parseInput(fobj=fobj, ext=ext) + self.filename = filename + WCS.__init__(self, ehdr, fobj=fobj) + self.setHDR0kw(hdr0, ehdr) + self.setInstrSpecKw(hdr0, ehdr) + self.setPscale() + self.setOrient() + self.readIDCCoeffs(ehdr) + extname = ehdr.get('EXTNAME', "") + extnum = ehdr.get('EXTVER', None) + self.extname = (extname, extnum) + else: + # create a default HSTWCS object based on + #instrument and detector parameters self.instrument = instrument + self.detector = detector self.setInstrSpecKw() - elif fname != None: + + def parseInput(self, fobj=None, ext=None): + if isinstance(fobj, str): # create an HSTWCS object from a filename - filename, extname = fileutil.parseFilename(fname) - self.filename = filename - self.extname = extname - if extname == None: - #data may be in the primary array - ehdr = pyfits.getheader(filename) - hdr0 = None - else: - ext = fileutil.parseExtn(extname) - hdr0 = pyfits.getheader(filename) - try: - ehdr = pyfits.getheader(filename, ext=ext) - except: - print 'Unable to get extension header based on filename %s. /n' % fname - elif fname == None and instrument == None: - # hdr0 may be None, a WCS object will still be created - if hdr0 == None and ehdr == None: - print 'Not enough information to create a WCS object\n' - self.help() - return - if hdr0 != None: - assert isinstance (hdr0, pyfits.Header) - if ehdr != None: - assert isinstance (ehdr, pyfits.Header) - - WCS.__init__(self, ehdr, fobj=fobj) - self.setHDR0kw(hdr0, ehdr) - self.setInstrSpecKw(hdr0, ehdr) - self.pscale = self.setPscale() - self.orientat = self.setOrient() - self.readIDCCoeffs(ehdr) - # if input was not a file name, try to get it from the primary header - self.filename = hdr0.get('FILENAME', "") - extname = ehdr.get('EXTNAME', "") - extnum = ehdr.get('EXTVER', None) - self.extname = (extname, extnum) + if ext != None: + filename = fobj + extnum = ext + elif ext == None: + filename, extname = fileutil.parseFilename(fobj) + if extname == None: + #data may be in the primary array + extnum = 0 + else: + extnum = fileutil.parseExtn(extname) + hdr0 = pyfits.getheader(filename) + try: + ehdr = pyfits.getheader(filename, ext=extnum) + except IndexError: + print 'Unable to get extension %d. /n' % ext + elif isinstance(fobj, pyfits.HDUList): + if ext == None: + extnum = 0 + else: + extnum = ext + ehdr = fobj[extnum].header + hdr0 = fobj[0].header + filename = hdr0.get('FILENAME', "") - + return filename, hdr0, ehdr + def setHDR0kw(self, primhdr, ehdr): # Set attributes from kw defined in the primary header. self.instrument = primhdr.get('INSTRUME', None) @@ -138,13 +144,13 @@ class HSTWCS(WCS): # corrections cd11 = self.wcs.cd[0][0] cd21 = self.wcs.cd[1][0] - return N.sqrt(N.power(cd11,2)+N.power(cd21,2)) * 3600. + self.pscale = N.sqrt(N.power(cd11,2)+N.power(cd21,2)) * 3600. def setOrient(self): # Recompute ORIENTAT cd12 = self.wcs.cd[0][1] cd22 = self.wcs.cd[1][1] - return RADTODEG(N.arctan2(cd12,cd22)) + self.orientat = RADTODEG(N.arctan2(cd12,cd22)) def updatePscale(self, pscale): """Given a plate scale, update the CD matrix""" @@ -181,7 +187,7 @@ class HSTWCS(WCS): ======= Read distortion model from idc table. Save some of the information as kw needed for interpreting the distortion - If header is provided and update is true, some IDC model kw + If header is provided and update is True, some IDC model kw will be recorded in the header. """ if self.idctab == None or self.date_obs == None: @@ -205,7 +211,37 @@ class HSTWCS(WCS): else: self.updatehdr(header) - + def restore(self, header=None): + """ + Restore a WCS archive in memory and update the WCS object. + Restored are the basic WCS kw as well as pscale and orient. + """ + from pywcs import Wcsprm + backup = {} + if header == None: + print 'Need a valid header in order to restore archive\n' + return + + for k in basic_wcs: + try: + nkw = ('O'+k)[:7] + backup[k] = header[nkw] + except KeyError: + pass + if backup == {}: + print 'No archive was found\n' + return + cdl=pyfits.CardList() + for item in backup.items(): + card = pyfits.Card(key=item[0], value=item[1]) + cdl.append(card) + + h = pyfits.Header(cdl) + wprm = Wcsprm("".join([str(x) for x in h.ascardlist()])) + self.wcs = wprm + self.setPscale() + self.setOrient() + def updatehdr(self, ext_hdr, newkeywords=None): #kw2add : OCX10, OCX11, OCY10, OCY11 # record the model in the header for use by pydrizzle @@ -224,13 +260,11 @@ class HSTWCS(WCS): def help(self): print 'How to create an HSTWCS object:\n\n' print """ \ - 1. Create an HSTWCS object using pyfits.Header objects. \n + 1. Using a pyfits HDUList object and an extension number \n Example:\n - hdr0 = pyfits.getheader('j9irw4b1q_flt.fits', ext=0)\n - hdr1 = pyfits.getheader('j9irw4b1q_flt.fits', ext=1)\n - f = pyfits.open('j9irw4b1q_flt.fits')\n - f is required only if a lookup table distortion is available.\n - w = wcsutil.HSTWCS(hdr0, hdr1,f)\n\n + + fobj = pyfits.open('some_file.fits')\n + w = wcsutil.HSTWCS(fobj, 3)\n\n 2. Create an HSTWCS object using a qualified file name. \n Example:\n |