summaryrefslogtreecommitdiff
path: root/lib/utils.py
diff options
context:
space:
mode:
authordencheva <dencheva@stsci.edu>2008-08-22 13:59:11 -0400
committerdencheva <dencheva@stsci.edu>2008-08-22 13:59:11 -0400
commitb003de463c45950a475892c8f5316c2f13a7536f (patch)
tree773f31bc7fe0149be7576667d2403c4c53fa771c /lib/utils.py
parent33107fbdd5bba3625c63bf944cfb7a927f18cd1e (diff)
downloadstwcs_hcf-b003de463c45950a475892c8f5316c2f13a7536f.tar.gz
- HSTWCS class was modified to
- not keep the headers as attributes - accept an adittional paramter fobj, consistent with SIP and WCS classes. - The restore method was renamed to get_archive and moved to utils - corrections classes: - updateWCS were turned into classmethods - diff_abgles was moved to utils - restoreWCS() was moved to utils git-svn-id: http://svn.stsci.edu/svn/ssb/stsci_python/development/trunk/hstwcs@6973 fe389314-cf27-0410-b35b-8c050e845b92
Diffstat (limited to 'lib/utils.py')
-rw-r--r--lib/utils.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/utils.py b/lib/utils.py
new file mode 100644
index 0000000..bec12f7
--- /dev/null
+++ b/lib/utils.py
@@ -0,0 +1,83 @@
+from pytools import parseinput, fileutil
+import pyfits
+from mappings import basic_wcs
+
+def restoreWCS(fnames):
+ """
+ Given a list of fits file names restore the original basic WCS kw
+ and write out the files. This overwrites the original files.
+
+ Affected keywords:
+ 'CD1_1', 'CD1_2', 'CD2_1', 'CD2_2', 'CRVAL1','CRVAL2','CTYPE1', 'CTYPE2',
+ 'CRPIX1', 'CRPIX2', 'CTYPE1', 'CTYPE2', 'ORIENTAT'
+ """
+ files = parseinput.parseinput(fnames)[0]
+ for f in files:
+ isfits, ftype = fileutil.isFits(f)
+ if not isfits or (isfits and ftype == 'waiver'):
+ print "RestoreWCS works only with true fits files."
+ return
+ else:
+ fobj = pyfits.open(f, mode='update')
+ for ext in fobj:
+ try:
+ extname = ext.header['EXTNAME'].lower()
+ except KeyError:
+ continue
+ if extname in ['sci', 'err', 'sdq']:
+ hdr = ext.header
+ backup = get_archive(hdr)
+ if not backup:
+ #print 'No archived keywords found.\n'
+ continue
+ else:
+ for kw in basic_wcs:
+ nkw = ('O'+kw)[:7]
+ if backup.has_key(kw):
+ hdr.update(kw, hdr[nkw])
+ fobj.close()
+
+def get_archive(header):
+ """
+ Returns a dictionary with the archived basic WCS keywords.
+ """
+
+ backup = {}
+ for k in basic_wcs:
+ try:
+ nkw = ('O'+k)[:7]
+ backup[k] = header[nkw]
+ except KeyError:
+ pass
+ return backup
+
+def write_archive(header):
+ """
+ Archives original WCS kw before recalculating them.
+ """
+ backup_kw = get_archive(header)
+ if backup_kw != {}:
+ print "Archive already exists\n."
+ return
+ else:
+ cmt = 'archived value'
+ for kw in basic_wcs:
+ nkw = 'O'+kw
+ header.update(nkw[:7], header[kw], comment=cmt)
+
+
+def diff_angles(a,b):
+ """
+ Perform angle subtraction a-b taking into account
+ small-angle differences across 360degree line.
+ """
+
+ diff = a - b
+
+ if diff > 180.0:
+ diff -= 360.0
+
+ if diff < -180.0:
+ diff += 360.0
+
+ return diff \ No newline at end of file