summaryrefslogtreecommitdiff
path: root/wcsutil/convertwcs.py
diff options
context:
space:
mode:
authorembray <embray@stsci.edu>2011-04-01 15:06:38 -0400
committerembray <embray@stsci.edu>2011-04-01 15:06:38 -0400
commit9753d368de5771d33cf8dd2ae16e735402ec4298 (patch)
tree95ea164208e39a1eff7daf58ba1d54b739ca1f17 /wcsutil/convertwcs.py
parentda0c55b1fb14f62722468b3956ab4cf089699c69 (diff)
downloadstwcs_hcf-9753d368de5771d33cf8dd2ae16e735402ec4298.tar.gz
Per Warren's suggestion, moved the wcscorr and convertwcs modules out of betadrizzle and moved them into the stwcs.wcsutil package. Also cleaned up some imports and cleaned up some whitespace--might want to view this diff with ignore-whitespace.
git-svn-id: http://svn.stsci.edu/svn/ssb/stsci_python/stsci_python/trunk/stwcs@12388 fe389314-cf27-0410-b35b-8c050e845b92
Diffstat (limited to 'wcsutil/convertwcs.py')
-rw-r--r--wcsutil/convertwcs.py118
1 files changed, 118 insertions, 0 deletions
diff --git a/wcsutil/convertwcs.py b/wcsutil/convertwcs.py
new file mode 100644
index 0000000..2a53d57
--- /dev/null
+++ b/wcsutil/convertwcs.py
@@ -0,0 +1,118 @@
+import pyfits
+try:
+ import stwcs
+ from stwcs import wcsutil
+except:
+ stwcs = None
+
+from pytools import fileutil
+
+OPUS_WCSKEYS = ['OCRVAL1','OCRVAL2','OCRPIX1','OCRPIX2',
+ 'OCD1_1','OCD1_2','OCD2_1','OCD2_2',
+ 'OCTYPE1','OCTYPE2']
+
+
+def archive_prefix_OPUS_WCS(fobj,extname='SCI'):
+ """ Identifies WCS keywords which were generated by OPUS and archived
+ using a prefix of 'O' for all 'SCI' extensions in the file
+
+ Parameters
+ ----------
+ fobj: string or pyfits.HDUList
+ Filename or pyfits object of a file
+
+ """
+ if stwcs is None:
+ print '====================='
+ print 'The STWCS package is needed to convert an old-style OPUS WCS to an alternate WCS'
+ print '====================='
+ raise ImportError
+
+
+ closefits = False
+ if isinstance(fobj,str):
+ # A filename was provided as input
+ fobj = pyfits.open(fobj,mode='update')
+ closefits=True
+
+ # Define the header
+ ext = ('sci',1)
+ hdr = fobj[ext].header
+
+ numextn = fileutil.countExtn(fobj)
+ extlist = []
+ for e in xrange(1,numextn+1):
+ extlist.append(('sci',e))
+
+ # Insure that the 'O' alternate WCS is present
+ if 'O' not in wcsutil.wcskeys(hdr):
+ # if not, archive the Primary WCS as the default OPUS WCS
+ wcsutil.archiveWCS(fobj,extlist, wcskey='O', wcsname='OPUS')
+
+ # find out how many SCI extensions are in the image
+ numextn = fileutil.countExtn(fobj,extname=extname)
+ if numextn == 0:
+ extname = 'PRIMARY'
+
+ # create HSTWCS object from PRIMARY WCS
+ wcsobj = wcsutil.HSTWCS(fobj,ext=ext,wcskey='O')
+ # get list of WCS keywords
+ wcskeys = wcsobj.wcs2header().keys()
+
+ # For each SCI extension...
+ for e in xrange(1,numextn+1):
+ # Now, look for any WCS keywords with a prefix of 'O'
+ for key in wcskeys:
+ okey = 'O'+key[:7]
+ hdr = fobj[(extname,e)].header
+ if hdr.has_key(okey):
+ # Update alternate WCS keyword with prefix-O OPUS keyword value
+ hdr[key] = hdr[okey]
+
+ if closefits:
+ fobj.close()
+
+def create_prefix_OPUS_WCS(fobj,extname='SCI'):
+ """ Creates alternate WCS with a prefix of 'O' for OPUS generated WCS values
+ to work with old MultiDrizzle.
+
+ Parameters
+ ----------
+ fobj: string or pyfits.HDUList
+ Filename or pyfits object of a file
+
+ Raises
+ ------
+ IOError:
+ if input PyFITS object was not opened in 'update' mode
+
+ """
+ # List of O-prefix keywords to create
+ owcskeys = OPUS_WCSKEYS
+
+ closefits = False
+ if isinstance(fobj,str):
+ # A filename was provided as input
+ fobj = pyfits.open(fobj,mode='update')
+ closefits=True
+ else:
+ # check to make sure this FITS obj has been opened in update mode
+ if fobj.fileinfo(0)['filemode'] != 'update':
+ print 'File not opened with "mode=update". Quitting...'
+ raise IOError
+
+ # check for existance of O-prefix WCS
+ if not fobj['sci',1].header.has_key(owcskeys[0]):
+
+ # find out how many SCI extensions are in the image
+ numextn = fileutil.countExtn(fobj,extname=extname)
+ if numextn == 0:
+ extname = ''
+ for extn in xrange(1,numextn+1):
+ hdr = fobj[(extname,extn)].header
+ for okey in owcskeys:
+ hdr.update(okey,hdr[okey[1:]+'O'])
+
+ # Close FITS image if we had to open it...
+ if closefits:
+ fobj.close()