summaryrefslogtreecommitdiff
path: root/updatewcs/alignwcs.py
diff options
context:
space:
mode:
authordencheva <dencheva@stsci.edu>2009-04-29 08:13:55 -0400
committerdencheva <dencheva@stsci.edu>2009-04-29 08:13:55 -0400
commit749102aed2e2c258f00aeef6c78fadd704c14fe7 (patch)
treeb7abe7b2d58c01251a4f697681bfde14443a7161 /updatewcs/alignwcs.py
parent9bbd241a0d442f556d5db5d65075d0d26822d1eb (diff)
downloadstwcs_hcf-749102aed2e2c258f00aeef6c78fadd704c14fe7.tar.gz
Renamed hstwcs to udpatewcs
git-svn-id: http://svn.stsci.edu/svn/ssb/stsci_python/development/trunk/stwcs@7976 fe389314-cf27-0410-b35b-8c050e845b92
Diffstat (limited to 'updatewcs/alignwcs.py')
-rwxr-xr-xupdatewcs/alignwcs.py95
1 files changed, 95 insertions, 0 deletions
diff --git a/updatewcs/alignwcs.py b/updatewcs/alignwcs.py
new file mode 100755
index 0000000..db96ef6
--- /dev/null
+++ b/updatewcs/alignwcs.py
@@ -0,0 +1,95 @@
+#!/usr/bin/env python
+
+from pydrizzle import process_input
+import pywcs, pyfits, numpy as np
+from updatewcs.distortion import utils
+from pytools import fileutil as fu
+from updatewcs import wcsutil
+
+
+def align_wcs(input, shiftfile=None, writeasn=False, asnname=None):
+ """
+ Purpose
+ =======
+ Correct the WCS of a fits file, so that multidrizzle aligns the images.
+ To view the format of the shift file:
+ >>>from pytools.asnutil import ShiftFile
+ >>>print ShiftFile.__doc__
+
+ Example
+ =======
+ >>>import alignwcs
+ >>>alignwcs.align_wcs('*flt.fits', shiftfile='shifts.txt')
+ It works also on the command line:
+ %./alignwcs.py *flt.fits shifts.txt
+
+ Dependencies
+ ============
+ `pytools`
+ `pyfits`
+ `pywcs`
+ `numpy`
+ `pydrizzle`
+
+ :Parameters:
+ `input`: a python list of file names or a string (wild card characters allowed)
+ input files may be in fits, geis or waiver fits format
+ `shiftfile`: a shift file, as the output of tweakshifts
+ `writeasn`: boolean
+ defines whether to write out the asn table updated with shift information
+ `asnname`: string
+ name for the output asn table
+ """
+ if shiftfile == None:
+ print 'A shift file is required but was not provided\n'
+ return
+
+ asndict,b,c = process_input.process_input(input, shiftfile=shiftfile, updatewcs=False)
+ if writeasn:
+ if asnname == None:
+ asnname = c.split('.fits')[0] + '_shifts' + '.fits'
+ a.write(asnname)
+ outwcs = get_outwcs(asndict)
+ apply_shifts(asndict, outwcs)
+
+def apply_shifts(asndict, outwcs):
+ for mem in asndict['members']:
+ filename = fu.buildRootname(mem)
+ xsh = asndict['members'][mem]['xshift']
+ ysh = asndict['members'][mem]['yshift']
+ shifts = np.array([[xsh, ysh]])
+ f = pyfits.open(filename)
+ for extn in f:
+ if extn.header.has_key('extname') and extn.header['extname'].lower() == 'sci':
+ extver = extn.header['extver']
+ owcs = pywcs.WCS(extn.header, f)
+ crvals = np.array([owcs.wcs.crval])
+ px = outwcs.wcs.s2p(crvals, 1)['pixcrd'] + shifts
+ ncrvals = outwcs.all_pix2sky(px, 1)
+ pyfits.setval(filename, 'CRVAL1', value=ncrvals[0,0], ext=extver)
+ pyfits.setval(filename, 'CRVAL2', value=ncrvals[0,1], ext=extver)
+ print 'Updated %s with shifts ' % filename, shifts
+ f.close()
+
+
+def get_outwcs(asndict):
+ wcslist = []
+ for mem in asndict['members']:
+ filename = fu.buildRootname(mem)
+ f = pyfits.open(filename)
+ hdr0 = f[0].header
+ for extn in f:
+ if extn.header.has_key('extname') and extn.header['extname'].lower() == 'sci':
+ owcs = wcsutil.HSTWCS(hdr0, extn.header,f)
+ wcslist.append(owcs)
+ f.close()
+ outwcs = utils.output_wcs(wcslist)
+ return outwcs
+
+if __name__ == '__main__':
+ import sys
+ args = sys.argv[1:]
+ input = args[:-1]
+ shifts = args[-1]
+
+ align_wcs(input, shiftfile=shifts)