diff options
author | Nadia Dencheva <nadia.astropy@gmail.com> | 2016-08-11 14:42:28 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-11 14:42:28 -0400 |
commit | 892edca9d767cf2dfea48a60c9cc0eb97ef2e4e0 (patch) | |
tree | d81f29b7f54ff7132ac1073a2b9c2186da34aef9 /stwcs/wcsutil/altwcs.py | |
parent | 6ee1b08a2bc2fea4e61fb05d6c3d9250c15a1a75 (diff) | |
parent | 9932a03e2abe7c871195af0dc21f43415c12027d (diff) | |
download | stwcs_hcf-892edca9d767cf2dfea48a60c9cc0eb97ef2e4e0.tar.gz |
Merge pull request #12 from nden/pep8-pylint
Pep8 pylint
Diffstat (limited to 'stwcs/wcsutil/altwcs.py')
-rw-r--r-- | stwcs/wcsutil/altwcs.py | 91 |
1 files changed, 54 insertions, 37 deletions
diff --git a/stwcs/wcsutil/altwcs.py b/stwcs/wcsutil/altwcs.py index 5dc7dd6..64cdc87 100644 --- a/stwcs/wcsutil/altwcs.py +++ b/stwcs/wcsutil/altwcs.py @@ -1,7 +1,6 @@ -from __future__ import division, print_function # confidence high -import os -import string +from __future__ import absolute_import, division, print_function +import string import numpy as np from astropy import wcs as pywcs from astropy.io import fits @@ -9,9 +8,11 @@ from stsci.tools import fileutil as fu altwcskw = ['WCSAXES', 'CRVAL', 'CRPIX', 'PC', 'CDELT', 'CD', 'CTYPE', 'CUNIT', 'PV', 'PS'] -altwcskw_extra = ['LATPOLE','LONPOLE','RESTWAV','RESTFRQ'] +altwcskw_extra = ['LATPOLE', 'LONPOLE', 'RESTWAV', 'RESTFRQ'] # file operations + + def archiveWCS(fname, ext, wcskey=" ", wcsname=" ", reusekey=False): """ Copy the primary WCS to the header as an alternate WCS @@ -77,7 +78,7 @@ def archiveWCS(fname, ext, wcskey=" ", wcsname=" ", reusekey=False): closefobj(fname, f) raise KeyError("Wcskey %s is aready used. \ Run archiveWCS() with reusekey=True to overwrite this alternate WCS. \ - Alternatively choose another wcskey with altwcs.available_wcskeys()." %wcskey) + Alternatively choose another wcskey with altwcs.available_wcskeys()." % wcskey) elif wcskey == " ": # wcsname exists, overwrite it if reuse is True or get the next key if wcsname.strip() in wcsnames(f[wcsext].header).values(): @@ -89,13 +90,13 @@ def archiveWCS(fname, ext, wcskey=" ", wcsname=" ", reusekey=False): wkey = next_wcskey(f[wcsext].header) elif wkey is None: closefobj(fname, f) - raise KeyError("Could not get a valid wcskey from wcsname %s" %wcsname) + raise KeyError("Could not get a valid wcskey from wcsname %s" % wcsname) else: closefobj(fname, f) raise KeyError("Wcsname %s is aready used. \ Run archiveWCS() with reusekey=True to overwrite this alternate WCS. \ Alternatively choose another wcskey with altwcs.available_wcskeys() or\ - choose another wcsname." %wcsname) + choose another wcsname." % wcsname) else: wkey = next_wcskey(f[wcsext].header) if wcsname.strip(): @@ -103,7 +104,7 @@ def archiveWCS(fname, ext, wcskey=" ", wcsname=" ", reusekey=False): else: # determine which WCSNAME needs to be replicated in archived WCS wnames = wcsnames(f[wcsext].header) - if 'O' in wnames: del wnames['O'] # we don't want OPUS/original + if 'O' in wnames: del wnames['O'] # we don't want OPUS/original if len(wnames) > 0: if ' ' in wnames: wname = wnames[' '] @@ -139,15 +140,16 @@ def archiveWCS(fname, ext, wcskey=" ", wcsname=" ", reusekey=False): f[e].header[wcsnamekey] = wname try: - old_wcsname=hwcs.pop('WCSNAME') + old_wcsname = hwcs.pop('WCSNAME') except: pass for k in hwcs.keys(): - key = k[:7] + wkey + key = k[: 7] + wkey f[e].header[key] = hwcs[k] closefobj(fname, f) + def restore_from_to(f, fromext=None, toext=None, wcskey=" ", wcsname=" "): """ Copy an alternate WCS from one extension as a primary WCS of another extension @@ -188,14 +190,14 @@ def restore_from_to(f, fromext=None, toext=None, wcskey=" ", wcsname=" "): raise ValueError("Input parameters problem") # Interpret input 'ext' value to get list of extensions to process - #ext = _buildExtlist(fobj, ext) + # ext = _buildExtlist(fobj, ext) if isinstance(toext, str): toext = [toext] # the case of an HDUList object in memory without an associated file - #if fobj.filename() is not None: + # if fobj.filename() is not None: # name = fobj.filename() simplefits = fu.isFits(fobj)[1] is 'simple' @@ -221,14 +223,15 @@ def restore_from_to(f, fromext=None, toext=None, wcskey=" ", wcsname=" "): if not countext: raise KeyError("File does not have extension with extname %s", fromext) else: - for i in range(1, countext+1): + for i in range(1, countext + 1): for toe in toext: _restore(fobj, fromextnum=i, fromextnam=fromext, toextnum=i, toextnam=toe, ukey=wkey) if fobj.filename() is not None: - #fobj.writeto(name) + # fobj.writeto(name) closefobj(f, fobj) + def restoreWCS(f, ext, wcskey=" ", wcsname=" "): """ Copy a WCS with key "WCSKEY" to the primary WCS @@ -272,9 +275,6 @@ def restoreWCS(f, ext, wcskey=" ", wcsname=" "): # the case of an HDUList object in memory without an associated file - #if fobj.filename() is not None: - # name = fobj.filename() - simplefits = fu.isFits(fobj)[1] is 'simple' if simplefits: wcskeyext = 0 @@ -297,6 +297,7 @@ def restoreWCS(f, ext, wcskey=" ", wcsname=" "): if fobj.filename() is not None: closefobj(f, fobj) + def deleteWCS(fname, ext, wcskey=" ", wcsname=" "): """ Delete an alternate WCS defined with wcskey. @@ -351,7 +352,7 @@ def deleteWCS(fname, ext, wcskey=" ", wcsname=" "): prexts = [] for i in ext: hdr = fobj[i].header - hwcs = readAltWCS(fobj,i,wcskey=wkey) + hwcs = readAltWCS(fobj, i, wcskey=wkey) if hwcs is None: continue for k in hwcs[::-1]: @@ -363,6 +364,7 @@ def deleteWCS(fname, ext, wcskey=" ", wcsname=" "): print("Did not find WCS with key %s in any of the extensions" % wkey) closefobj(fname, fobj) + def _buildExtlist(fobj, ext): """ Utility function to interpret the provided value of 'ext' and return a list @@ -378,8 +380,8 @@ def _buildExtlist(fobj, ext): If a string is provided, it should specify the EXTNAME of extensions with WCSs to be archived """ - if not isinstance(ext,list): - if isinstance(ext,str): + if not isinstance(ext, list): + if isinstance(ext, str): extstr = ext ext = [] for extn in range(1, len(fobj)): @@ -391,6 +393,7 @@ def _buildExtlist(fobj, ext): raise KeyError("Valid extensions in 'ext' parameter need to be specified.") return ext + def _restore(fobj, ukey, fromextnum, toextnum=None, fromextnam=None, toextnam=None, verbose=True): """ @@ -415,7 +418,7 @@ def _restore(fobj, ukey, fromextnum, if toextnam: toextension = (toextnam, toextnum) else: - toextension =toextnum + toextension = toextnum else: toextension = fromextension @@ -445,34 +448,38 @@ def _restore(fobj, ukey, fromextnum, fobj[toextension].header['TDDALPHA'] = 0.0 fobj[toextension].header['TDDBETA'] = 0.0 if 'ORIENTAT' in fobj[toextension].header: - norient = np.rad2deg(np.arctan2(hwcs['CD1_2'+'%s' %ukey], hwcs['CD2_2'+'%s' %ukey])) + norient = np.rad2deg(np.arctan2(hwcs['CD1_2' + '%s' % ukey], hwcs['CD2_2' + '%s' % ukey])) fobj[toextension].header['ORIENTAT'] = norient # Reset 2014 TDD keywords prior to computing new values (if any are computed) - for kw in ['TDD_CYA','TDD_CYB','TDD_CXA','TDD_CXB']: + for kw in ['TDD_CYA', 'TDD_CYB', 'TDD_CXA', 'TDD_CXB']: if kw in fobj[toextension].header: fobj[toextension].header[kw] = 0.0 -#header operations + +# header operations + + def _check_headerpars(fobj, ext): if not isinstance(fobj, fits.Header) and not isinstance(fobj, fits.HDUList) \ - and not isinstance(fobj, str): + and not isinstance(fobj, str): raise ValueError("Expected a file name, a file object or a header\n") if not isinstance(fobj, fits.Header): - #raise ValueError("Expected a valid ext parameter when input is a file") if not isinstance(ext, int) and not isinstance(ext, tuple): raise ValueError("Expected ext to be a number or a tuple, e.g. ('SCI', 1)\n") + def _getheader(fobj, ext): if isinstance(fobj, str): - hdr = fits.getheader(fobj,ext) + hdr = fits.getheader(fobj, ext) elif isinstance(fobj, fits.Header): hdr = fobj else: hdr = fobj[ext].header return hdr -def readAltWCS(fobj, ext, wcskey=' ',verbose=False): + +def readAltWCS(fobj, ext, wcskey=' ', verbose=False): """ Reads in alternate primary WCS from specified extension. @@ -495,12 +502,12 @@ def readAltWCS(fobj, ext, wcskey=' ',verbose=False): if isinstance(fobj, str): fobj = fits.open(fobj) - hdr = _getheader(fobj,ext) + hdr = _getheader(fobj, ext) try: nwcs = pywcs.WCS(hdr, fobj=fobj, key=wcskey) except KeyError: if verbose: - print('readAltWCS: Could not read WCS with key %s' %wcskey) + print('readAltWCS: Could not read WCS with key %s' % wcskey) print(' Skipping %s[%s]' % (fobj.filename(), str(ext))) return None hwcs = nwcs.to_header() @@ -510,7 +517,8 @@ def readAltWCS(fobj, ext, wcskey=' ',verbose=False): return hwcs -def convertAltWCS(fobj,ext,oldkey=" ",newkey=' '): + +def convertAltWCS(fobj, ext, oldkey=" ", newkey=' '): """ Translates the alternate/primary WCS with one key to an alternate/primary WCS with another key. @@ -534,7 +542,7 @@ def convertAltWCS(fobj,ext,oldkey=" ",newkey=' '): hdr: `astropy.io.fits.Header` header object with keywords renamed from oldkey to newkey """ - hdr = readAltWCS(fobj,ext,wcskey=oldkey) + hdr = readAltWCS(fobj, ext, wcskey=oldkey) if hdr is None: return None # Converting WCS to new key @@ -543,10 +551,11 @@ def convertAltWCS(fobj,ext,oldkey=" ",newkey=' '): cname = card else: cname = card.rstrip(oldkey) - hdr.rename_key(card,cname+newkey,force=True) + hdr.rename_key(card, cname + newkey, force=True) return hdr + def wcskeys(fobj, ext=None): """ Returns a list of characters used in the header for alternate @@ -565,11 +574,12 @@ def wcskeys(fobj, ext=None): names = hdr["WCSNAME*"] d = [] for key in names: - wkey = key.replace('WCSNAME','') + wkey = key.replace('WCSNAME', '') if wkey == '': wkey = ' ' d.append(wkey) return d + def wcsnames(fobj, ext=None): """ Returns a dictionary of wcskey: WCSNAME pairs @@ -588,11 +598,12 @@ def wcsnames(fobj, ext=None): names = hdr["WCSNAME*"] d = {} for keyword, value in names.items(): - wkey = keyword.replace('WCSNAME','') + wkey = keyword.replace('WCSNAME', '') if wkey == '': wkey = ' ' d[wkey] = value return d + def available_wcskeys(fobj, ext=None): """ Returns a list of characters which are not used in the header @@ -618,6 +629,7 @@ def available_wcskeys(fobj, ext=None): [all_keys.remove(key) for key in used_keys] return all_keys + def next_wcskey(fobj, ext=None): """ Returns next available character to be used for an alternate WCS @@ -638,6 +650,7 @@ def next_wcskey(fobj, ext=None): else: return None + def getKeyFromName(header, wcsname): """ If WCSNAME is found in header, return its key, else return @@ -663,6 +676,7 @@ def getKeyFromName(header, wcsname): wkey = None return wkey + def pc2cd(hdr, key=' '): """ Convert a CD matrix to a CD matrix. @@ -690,6 +704,7 @@ def pc2cd(hdr, key=' '): hdr['CD{0}{1}'.format(c, key)] = val return hdr + def _parpasscheck(fobj, ext, wcskey, fromext=None, toext=None, reusekey=False): """ Check input parameters to altwcs functions @@ -722,7 +737,7 @@ def _parpasscheck(fobj, ext, wcskey, fromext=None, toext=None, reusekey=False): return False if not isinstance(ext, int) and not isinstance(ext, tuple) \ - and not isinstance(ext,str) \ + and not isinstance(ext, str) \ and not isinstance(ext, list) and ext is not None: print("Ext must be integer, tuple, string,a list of int extension numbers, \n\ or a list of tuples representing a fits extension, for example ('sci', 1).") @@ -733,7 +748,7 @@ def _parpasscheck(fobj, ext, wcskey, fromext=None, toext=None, reusekey=False): return False if not isinstance(toext, list) and not isinstance(toext, str) and \ - toext is not None : + toext is not None: print("toext must be a string or a list of strings representing extname") return False @@ -743,6 +758,7 @@ def _parpasscheck(fobj, ext, wcskey, fromext=None, toext=None, reusekey=False): return True + def closefobj(fname, f): """ Functions in this module accept as input a file name or a file object. @@ -752,6 +768,7 @@ def closefobj(fname, f): if isinstance(fname, str): f.close() + def mapFitsExt2HDUListInd(fname, extname): """ Map FITS extensions with 'EXTNAME' to HDUList indexes. |