diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/stwcs/updatewcs/__init__.py | 7 | ||||
-rw-r--r-- | lib/stwcs/wcsutil/altwcs.py | 76 |
2 files changed, 33 insertions, 50 deletions
diff --git a/lib/stwcs/updatewcs/__init__.py b/lib/stwcs/updatewcs/__init__.py index ac4ea91..0d6e08e 100644 --- a/lib/stwcs/updatewcs/__init__.py +++ b/lib/stwcs/updatewcs/__init__.py @@ -106,7 +106,6 @@ def updatewcs(input, vacorr=True, tddcorr=True, npolcorr=True, d2imcorr=True, logger.warning("\n\tNew IDCTAB file detected. All current WCSs will be deleted") cleanWCS(f) - #restore the original WCS keywords makecorr(f, acorr, wkey=wcskey, wname=wcsname, clobber=False) return files @@ -137,7 +136,7 @@ def makecorr(fname, allowed_corr, wkey=" ", wname=" ", clobber=False): #wcsutil.restoreWCS(f, ext=[], wcskey='O', clobber=True) #Determine the reference chip and create the reference HSTWCS object nrefchip, nrefext = getNrefchip(f) - wcsutil.restoreWCS(f, nrefext, wcskey='O', clobber=True) + wcsutil.restoreWCS(f, nrefext, wcskey='O') rwcs = HSTWCS(fobj=f, ext=nrefext) rwcs.readModel(update=True,header=f[nrefext].header) @@ -156,7 +155,7 @@ def makecorr(fname, allowed_corr, wkey=" ", wname=" ", clobber=False): if extn.header.has_key('extname'): extname = extn.header['extname'].lower() if extname == 'sci': - wcsutil.restoreWCS(f, ext=i, wcskey='O', clobber=True) + wcsutil.restoreWCS(f, ext=i, wcskey='O') sciextver = extn.header['extver'] ref_wcs = rwcs.deepcopy() hdr = extn.header @@ -172,7 +171,7 @@ def makecorr(fname, allowed_corr, wkey=" ", wname=" ", clobber=False): hdr.update(kw, kw2update[kw]) #if wkey is None, do not archive the primary WCS if key is not None: - wcsutil.archiveWCS(f, ext=i, wcskey=key, wcsname=name, reusekey=True) + wcsutil.archiveWCS(f, ext=i, wcskey=key, wcsname=name, reusekey=False) elif extname in ['err', 'dq', 'sdq', 'samp', 'time']: cextver = extn.header['extver'] if cextver == sciextver: diff --git a/lib/stwcs/wcsutil/altwcs.py b/lib/stwcs/wcsutil/altwcs.py index 05784a9..dd5f1e9 100644 --- a/lib/stwcs/wcsutil/altwcs.py +++ b/lib/stwcs/wcsutil/altwcs.py @@ -46,7 +46,7 @@ def archiveWCS(fname, ext=None, wcskey=" ", wcsname=" ", reusekey=False): See Also -------- - wcsutils.restoreWCS: Copy an alternate WCS to the primary WCS + wcsutil.restoreWCS: Copy an alternate WCS to the primary WCS """ @@ -57,7 +57,7 @@ def archiveWCS(fname, ext=None, wcskey=" ", wcsname=" ", reusekey=False): if not _parpasscheck(f, ext, wcskey, wcsname): closefobj(fname, f) - return + raise ValueError("Input parameters problem") if not ext: simplefits = fu.isFits(f)[1] is 'simple' @@ -76,6 +76,7 @@ def archiveWCS(fname, ext=None, wcskey=" ", wcsname=" ", reusekey=False): wcsext = ext[0] if wcskey in wcskeys(f[wcsext].header) and not reusekey: + 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) @@ -87,8 +88,10 @@ def archiveWCS(fname, ext=None, wcskey=" ", wcsname=" ", reusekey=False): wkey = getKeyFromName(f[wcsext].header, wcsname) wname = wcsname if not wkey: + closefobj(fname, f) 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\ @@ -120,19 +123,14 @@ def archiveWCS(fname, ext=None, wcskey=" ", wcsname=" ", reusekey=False): #f[e].header.update(key=okey, value=norient) closefobj(fname, f) -def restoreWCS(f, ext=None, fromext=None, toext=None, wcskey=" ", wcsname=" ", - clobber=False): +def restoreWCS(f, ext=None, fromext=None, toext=None, wcskey=" ", wcsname=" "): """ Copy a WCS with key "WCSKEY" to the primary WCS Reads in a WCS defined with wcskey and saves it as the primary WCS. - If clobber is False, writes out a new file whose name is the original - name with an attached 3 character string _'WCSKEY'_. - Otherwise overwrites the file. Goes sequentially through the list - of extensions. - The WCS is restored from the 'SCI' extension but the WCS of all - extensions with the same EXTVER are updated. - + Goes sequentially through the list of extensions in ext. + Alternatively uses 'fromext' and 'toext'. + Parameters ---------- @@ -151,8 +149,6 @@ def restoreWCS(f, ext=None, fromext=None, toext=None, wcskey=" ", wcsname=" ", or " " - find a key from WCSNAMe value wcsname: string (optional) if given and wcskey is " ", will try to restore by WCSNAME value - clobber: boolean, default False - A flag to define if the original files should be overwritten See Also -------- @@ -160,29 +156,24 @@ def restoreWCS(f, ext=None, fromext=None, toext=None, wcskey=" ", wcsname=" ", """ if isinstance(f, str): - if clobber: - fobj = pyfits.open(f, mode='update') - else: - fobj = pyfits.open(f) + fobj = pyfits.open(f, mode='update') else: fobj = f - if not _parpasscheck(fobj, ext=ext, wcskey=wcskey, fromext=fromext, toext=toext, clobber=clobber): + if not _parpasscheck(fobj, ext=ext, wcskey=wcskey, fromext=fromext, toext=toext): closefobj(f, fobj) - return + raise ValueError("Input parameters problem") if isinstance(ext, int) or isinstance(ext, tuple): ext = [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 not clobber: - name = (fobj.filename().split('.fits')[0] + '_%s_' + '.fits') %wcskey - else: - name = fobj.filename() + #if fobj.filename() is not None: + # name = fobj.filename() + simplefits = fu.isFits(fobj)[1] is 'simple' if simplefits: wcskeyext = 0 @@ -193,9 +184,8 @@ def restoreWCS(f, ext=None, fromext=None, toext=None, wcskey=" ", wcsname=" ", if wcsname.strip(): wkey = getKeyFromName(fobj[wcskeyext].header, wcsname) if not wkey: - print 'Could not get a key from wcsname %s .' % wcsname closefobj(f, fobj) - return + raise KeyError("Could not get a key from wcsname %s ." % wcsname) else: if wcskey not in wcskeys(fobj, ext=wcskeyext): print "Could not find alternate WCS with key %s in this file" % wcskey @@ -226,8 +216,7 @@ def restoreWCS(f, ext=None, fromext=None, toext=None, wcskey=" ", wcsname=" ", _restore(fobj, wkey, fromextnum=e) if fobj.filename() is not None: - if not clobber: - fobj.writeto(name) + #fobj.writeto(name) closefobj(f, fobj) def deleteWCS(fname, ext=None, wcskey=" ", wcsname=" "): @@ -251,7 +240,7 @@ def deleteWCS(fname, ext=None, wcskey=" ", wcsname=" "): if not _parpasscheck(fobj, ext, wcskey, wcsname): closefobj(fname, fobj) - return + raise ValueError("Input parameters problem") if not ext: ext = range(len(fobj)) @@ -269,23 +258,20 @@ def deleteWCS(fname, ext=None, wcskey=" ", wcsname=" "): wcskeyext = 0 else: wcskeyext = 1 + + if not wcskeys and not wcsname: + raise KeyError("Either wcskey or wcsname should be specified") + if wcskey == " ": # try getting the key from WCSNAME - if wcsname == " ": - print "Could not get a valid key from header" + wkey = getKeyFromName(fobj[wcskeyext].header, wcsname) + if not wkey: closefobj(fname, fobj) - return - else: - wkey = getKeyFromName(fobj[wcskeyext].header, wcsname) - if not wkey: - print 'Could not get a key: wcsname "%s" not found in header.' % wcsname - closefobj(fname, fobj) - return + raise KeyError("Could not get a key: wcsname '%s' not found in header." % wcsname) else: - if wcskey not in wcskeys(fobj[wcskeyext].header): - print "Could not find alternate WCS with key %s in this file" % wcskey + if wcskey not in wcskeys(fobj[wcskeyext].header): closefobj(fname, fobj) - return + raise KeyError("Could not find alternate WCS with key %s in this file" % wcskey) wkey = wcskey prexts = [] @@ -509,7 +495,7 @@ def pc2cd(hdr, key=' '): hdr.update(key='CD'+c+'%s' %key, value=val) return hdr -def _parpasscheck(fobj, ext, wcskey, fromext=None, toext=None, reusekey=False, clobber=False): +def _parpasscheck(fobj, ext, wcskey, fromext=None, toext=None, reusekey=False): """ Check input parameters to altwcs functions @@ -524,8 +510,6 @@ def _parpasscheck(fobj, ext, wcskey, fromext=None, toext=None, reusekey=False, c if given and wcskey is " ", will try to restore by WCSNAME value reusekey: boolean A flag which indicates whether to reuse a wcskey in the header - clobber: boolean - A flag to define if the original files should be overwritten """ if not isinstance(fobj,pyfits.HDUList): print "First parameter must be a fits file object or a file name." @@ -537,7 +521,7 @@ def _parpasscheck(fobj, ext, wcskey, fromext=None, toext=None, reusekey=False, c pass else: # an HDUList object with associated file - if fobj.fileinfo(0)['filemode'] is not 'update' and clobber: + if fobj.fileinfo(0)['filemode'] is not 'update': print "First parameter must be a file name or a file object opened in 'update' mode." return False |