diff options
Diffstat (limited to 'wcsutil/altwcs.py')
-rw-r--r-- | wcsutil/altwcs.py | 134 |
1 files changed, 67 insertions, 67 deletions
diff --git a/wcsutil/altwcs.py b/wcsutil/altwcs.py index b483e46..cbeeaef 100644 --- a/wcsutil/altwcs.py +++ b/wcsutil/altwcs.py @@ -1,37 +1,40 @@ from __future__ import division # confidence high +import os +import string -import os.path, string -import pywcs import numpy as np +import pywcs import pyfits -altwcskw = ['WCSAXES', 'CRVAL', 'CRPIX', 'PC', 'CDELT', 'CD', 'CTYPE', 'CUNIT', 'PV', 'PS'] +altwcskw = ['WCSAXES', 'CRVAL', 'CRPIX', 'PC', 'CDELT', 'CD', 'CTYPE', 'CUNIT', + 'PV', 'PS'] + # file operations def archiveWCS(fname, ext, wcskey=" ", wcsname=" ", clobber=False): """ - Copy the primary WCS to the hader as an alternate WCS - with wcskey and name WCSNAME. It loops over all extensions in 'ext' - + Copy the primary WCS to the hader as an alternate WCS + with wcskey and name WCSNAME. It loops over all extensions in 'ext' + Parameters ---------- fname: string or pyfits.HDUList a file name or a file object ext: an int, a tuple, a python list of integers or a python list of tuples (e.g.('sci',1)) fits extensions to work with - wcskey: string "A"-"Z" or " " - if " ": get next available key if wcsname is also " " or try - to get a key from WCSNAME value + wcskey: string "A"-"Z" or " " + if " ": get next available key if wcsname is also " " or try + to get a key from WCSNAME value wcsname: string Name of alternate WCS description clobber: boolean if Ture - overwrites a WCS with the same key - + See Also -------- wcsutils.restoreWCS: Copy an alternate WCS to the primary WCS - + """ - + if isinstance(fname, str): f = pyfits.open(fname, mode='update') else: @@ -40,11 +43,11 @@ def archiveWCS(fname, ext, wcskey=" ", wcsname=" ", clobber=False): if not parpasscheck(f, ext, wcskey, wcsname): closefobj(fname, f) return - + if isinstance(ext, int) or isinstance(ext, tuple): ext = [ext] - - if wcskey == " ": + + if wcskey == " ": # try getting the key from WCSNAME if not wcsname.strip(): wkey = next_wcskey(f[1].header) @@ -53,7 +56,7 @@ def archiveWCS(fname, ext, wcskey=" ", wcsname=" ", clobber=False): else: if wcskey not in available_wcskeys(f[1].header): # reuse wcsname - if not wcsname.strip(): + if not wcsname.strip(): wcsname = f[1].header["WCSNAME"+wcskey] wname = wcsname wkey = wcskey @@ -61,9 +64,9 @@ def archiveWCS(fname, ext, wcskey=" ", wcsname=" ", clobber=False): wkey = wcskey wname = wcsname else: - wkey = wcskey + wkey = wcskey wname = wcsname - + for e in ext: w = pywcs.WCS(f[e].header, fobj=f) hwcs = w.to_header() @@ -76,27 +79,27 @@ def archiveWCS(fname, ext, wcskey=" ", wcsname=" ", clobber=False): f[e].header.update(key=key, value=hwcs[k]) #norient = np.rad2deg(np.arctan2(hwcs['CD1_2'],hwcs['CD2_2'])) #okey = 'ORIENT%s' % wkey - #f[e].header.update(key=okey, value=norient) + #f[e].header.update(key=okey, value=norient) closefobj(fname, f) def restoreWCS(f, ext, wcskey=" ", wcsname=" ", clobber=False): """ Copy a WCS with key "WCSKEY" to a primary WCS - + Reads in a WCS defined with wcskey and saves it as the primary WCS. - If clobber is False, writes out new files whose names are the original - names with an attached 3 character string _'WCSKEY'_. + If clobber is False, writes out new files whose names are the original + names with an attached 3 character string _'WCSKEY'_. Otherwise overwrites the files. Goes sequentially through the list of extensions - The WCS is restored from the 'SCI' extension but the primary WCS of all + The WCS is restored from the 'SCI' extension but the primary WCS of all extensions with the same EXTVER are updated. - + Parameters ---------- f: string or pyfits.HDUList object a file name or a file object - ext: an int, a tuple, a python list of integers or a python list - of tuples (e.g.('sci',1)) + ext: an int, a tuple, a python list of integers or a python list + of tuples (e.g.('sci',1)) fits extensions to work with wcskey: a charater "A"-"Z" - Used for one of 26 alternate WCS definitions. @@ -105,27 +108,27 @@ def restoreWCS(f, ext, wcskey=" ", wcsname=" ", clobber=False): if given and wcskey is " ", will try to restore by WCSNAME value clobber: boolean A flag to define if the original files should be overwritten - + See Also -------- wcsutil.archiveWCS - copy the primary WCS as an alternate WCS - + """ if isinstance(f, str): if clobber: fobj = pyfits.open(f, mode='update') else: fobj = pyfits.open(f) - else: + else: fobj = f - + if not parpasscheck(fobj, ext, wcskey, wcsname): closefobj(f, fobj) return - + if isinstance(ext, int) or isinstance(ext, tuple): ext = [ext] - + if not clobber: name = (fobj.filename().split('.fits')[0] + '_%s_' + '.fits') %wcskey else: @@ -143,7 +146,7 @@ def restoreWCS(f, ext, wcskey=" ", wcsname=" ", clobber=False): print "Could not find alternate WCS with key %s in this file" % wcskey closefobj(f, fobj) return - wkey = wcskey + wkey = wcskey for e in ext: try: @@ -161,7 +164,7 @@ def restoreWCS(f, ext, wcskey=" ", wcsname=" ", clobber=False): closefobj(f, fobj) return #raise hwcs = nwcs.to_header() - + if nwcs.wcs.has_cd(): pc2cd(hwcs, key=wkey) for k in hwcs.keys(): @@ -187,7 +190,7 @@ def restoreWCS(f, ext, wcskey=" ", wcsname=" ", clobber=False): fobj[e].header.update(key='ORIENTAT', value=norient) else: continue - + if not clobber: fobj.writeto(name) closefobj(f, fobj) @@ -196,7 +199,7 @@ def deleteWCS(fname, ext, wcskey=" ", wcsname=" "): """ Delete an alternate WCS defined with wcskey. If wcskey is " " try to get a key from WCSNAME. - + Parameters ---------- fname: sting or a pyfits.HDUList object @@ -210,21 +213,21 @@ def deleteWCS(fname, ext, wcskey=" ", wcsname=" "): fobj = pyfits.open(fname, mode='update') else: fobj = fname - + if not parpasscheck(fobj, ext, wcskey, wcsname): closefobj(fname, fobj) return - + if isinstance(ext, int) or isinstance(ext, tuple): ext = [ext] # Do not allow deleting the original WCS. if wcskey == 'O': - print "Wcskey 'O' is reserved for the original WCS and should not be deleted." + print "Wcskey 'O' is reserved for the original WCS and should not be deleted." closefobj(fname, fobj) return - - if wcskey == " ": + + if wcskey == " ": # try getting the key from WCSNAME if wcsname == " ": print "Could not get a valid key from header" @@ -241,8 +244,8 @@ def deleteWCS(fname, ext, wcskey=" ", wcsname=" "): print "Could not find alternate WCS with key %s in this file" % wcskey closefobj(fname, fobj) return - wkey = wcskey - + wkey = wcskey + prexts = [] for i in ext: hdr = fobj[i].header @@ -266,9 +269,9 @@ def deleteWCS(fname, ext, wcskey=" ", wcsname=" "): #header operations def wcskeys(header): """ - Returns a list of characters used in the header for alternate + Returns a list of characters used in the header for alternate WCS description with WCSNAME keyword - + Parameters ---------- hdr: pyfits.Header @@ -280,7 +283,7 @@ def wcskeys(header): def wcsnames(header): """ Returns a dictionary of wcskey: WCSNAME pairs - + Parameters ---------- header: pyfits.Header @@ -290,13 +293,13 @@ def wcsnames(header): for c in names: d[c.key[-1]] = c.value return d - + def available_wcskeys(header): """ Returns a list of characters which are not used in the header with WCSNAME keyword. Any of them can be used to save a new WCS. - + Parameters ---------- header: pyfits.Header @@ -304,9 +307,9 @@ def available_wcskeys(header): assert isinstance(header, pyfits.Header), "Requires a pyfits.Header object as input" all_keys = list(string.ascii_uppercase) used_keys = wcskeys(header) - try: + try: used_keys.remove("") - except ValueError: + except ValueError: pass [all_keys.remove(key) for key in used_keys] return all_keys @@ -314,10 +317,10 @@ def available_wcskeys(header): def next_wcskey(header): """ Returns next available character to be used for an alternate WCS - + Parameters ---------- - header: pyfits.Header + header: pyfits.Header """ assert isinstance(header, pyfits.Header), "Requires a pyfits.Header object as input" allkeys = available_wcskeys(header) @@ -328,10 +331,10 @@ def next_wcskey(header): def getKeyFromName(header, wcsname): """ - If WCSNAME is found in header, return its key, else return + If WCSNAME is found in header, return its key, else return None. This is used to update an alternate WCS repeatedly and not generate new keys every time. - + Parameters ---------- header: pyfits.Header @@ -349,17 +352,17 @@ def getKeyFromName(header, wcsname): def pc2cd(hdr, key=' '): """ Convert a CD PC matrix to a CD matrix. - + WCSLIB (and PyWCS) recognizes CD keywords as input but converts them and works internally with the PC matrix. - to_header() returns the PC matrix even if the i nput was a - CD matrix. To keep input and output consistent we check + to_header() returns the PC matrix even if the i nput was a + CD matrix. To keep input and output consistent we check for has_cd and convert the PC back to CD. - + Parameters ---------- hdr: pyfits.Header - + """ for c in ['1_1', '1_2', '2_1', '2_2']: try: @@ -370,7 +373,7 @@ def pc2cd(hdr, key=' '): val = 1. else: val = 0. - hdr.update(key='CD'+c+'%s' %key, value=val) + hdr.update(key='CD'+c+'%s' %key, value=val) return hdr def parpasscheck(fobj, ext, wcskey, wcsname, clobber=True): @@ -389,12 +392,12 @@ def parpasscheck(fobj, ext, wcskey, wcsname, clobber=True): print "Ext must be integer, tuple, a list of int extension numbers, \ or a list of tuples representing a fits extension, for example ('sci', 1)." return False - + if len(wcskey) != 1: print 'Parameter wcskey must be a character - one of "A"-"Z" or " "' return False - - if wcskey == " ": + + if wcskey == " ": # try getting the key from WCSNAME """ if wcsname == " " or wcsname == "": @@ -421,7 +424,7 @@ def parpasscheck(fobj, ext, wcskey, wcsname, clobber=True): return True -def closefobj(fname,f): +def closefobj(fname, f): """ Functions in this module accept as input a file name or a file object. If the input was a file name (string) we close the object. If the user @@ -429,6 +432,3 @@ def closefobj(fname,f): """ if isinstance(fname, str): f.close() - - -
\ No newline at end of file |