diff options
-rw-r--r-- | lib/stwcs/updatewcs/__init__.py | 23 | ||||
-rw-r--r-- | lib/stwcs/updatewcs/utils.py | 63 |
2 files changed, 66 insertions, 20 deletions
diff --git a/lib/stwcs/updatewcs/__init__.py b/lib/stwcs/updatewcs/__init__.py index fb77b91..2a3cb07 100644 --- a/lib/stwcs/updatewcs/__init__.py +++ b/lib/stwcs/updatewcs/__init__.py @@ -187,28 +187,11 @@ def makecorr(fname, allowed_corr): f[0].header.update(key='PYWCSVER', value=pywcsversion, comment="Version of PYWCS used to updated the WCS",after=i) # add additional keywords to be used by headerlets - distname = construct_distname(f) - f[0].header.update('DISTNAME', distname) - idcname = f[0].header.get('IDCTAB', " ") - f[0].header.update('SIPNAME', idcname) + distdict = utils.construct_distname(f,rwcs) + f[0].header.update('DISTNAME', distdict['DISTNAME']) + f[0].header.update('SIPNAME', distdict['SIPNAME']) f.close() -def construct_distname(fobj): - idcname = fobj[0].header.get('IDCTAB', " ") - if idcname.strip() and 'fits' in idcname: - idcname = idcname.split('.fits')[0] - - npolname = fobj[0].header.get('NPOLFILE', " ") - if npolname.strip() and '.fits' in npolname: - npolname = npolname.split('.fits')[0] - - d2imname = fobj[0].header.get('D2IMFILE', " ") - if d2imname.strip() and '.fits' in d2imname: - d2imname = d2imname.split('.fits')[0] - - distname = idcname.strip().join(npolname.strip()).join(d2imname.strip()) - return distname - def copyWCS(w, ehdr): """ This is a convenience function to copy a WCS object diff --git a/lib/stwcs/updatewcs/utils.py b/lib/stwcs/updatewcs/utils.py index 29ba5f3..6789c19 100644 --- a/lib/stwcs/updatewcs/utils.py +++ b/lib/stwcs/updatewcs/utils.py @@ -1,4 +1,5 @@ from __future__ import division # confidence high +import os def diff_angles(a,b): """ @@ -26,3 +27,65 @@ def getBinning(fobj, extver=1): binned = fobj['SCI', extver].header.get('BINAXIS',1) return binned +def extract_rootname(kwvalue): + """ Returns the rootname from a full reference filename + + If a non-valid value (any of ['','N/A','NONE','INDEF',None]) is input, + simply return a string value of 'NONE' + + """ + # check to see whether a valid kwvalue has been provided as input + if kwvalue.strip() in ['','N/A','NONE','INDEF',None]: + return 'NONE' # no valid value, so return 'NONE' + + # for a valid kwvalue, parse out the rootname + # strip off any environment variable from input filename, if any are given + if '$' in kwvalue: + fullval = kwvalue[kwvalue.find('$')+1:] + else: + fullval = kwvalue + # Extract filename without path from kwvalue + fname = os.path.basename(fullval).strip() + + # Now, rip out just the rootname from the full filename + if '_' in fname: + rootname = fname[:fname.rfind('_')] + elif '.fit' in fname: # on some systems, only .fit is used instead of .fits + rootname = fname[:fname.rfind('.fit')] + elif '.' in fname: # account for non-standard file extensions + rootname = fname[:fname.rfind('.')] + else: + rootname = fname + + return rootname + +def construct_distname(fobj,wcsobj): + """ + This function constructs the value for the keyword 'DISTNAME'. + It relies on the reference files specified by the keywords 'IDCTAB', + 'NPOLFILE', and 'D2IMFILE'. + + The final constructed value will be of the form: + <idctab rootname>-<npolfile rootname>-<d2imfile rootname> + and have a value of 'NONE' if no reference files are specified. + """ + idcname = extract_rootname(fobj[0].header.get('IDCTAB', "NONE")) + if idcname is None and wcsobj.sip is not None: + idcname = 'UNKNOWN' + + npolname = extract_rootname(fobj[0].header.get('NPOLFILE', "NONE")) + if npolname is None and wcsobj.cpdis1 is not None: + npolname = 'UNKNOWN' + + d2imname = extract_rootname(fobj[0].header.get('D2IMFILE', "NONE")) + if d2imname is None and wcsobj.det2im is not None: + d2imname = 'UNKNOWN' + + distname = idcname.strip() + if npolname != 'NONE' or d2imname != 'NONE': + if d2imname != 'NONE': + distname+= '-'+npolname.strip() + '-'+d2imname.strip() + else: + distname+='-'+npolname.strip() + + return {'DISTNAME':distname,'SIPNAME':idcname}
\ No newline at end of file |