diff options
author | dencheva <dencheva@stsci.edu> | 2009-01-20 16:41:48 -0500 |
---|---|---|
committer | dencheva <dencheva@stsci.edu> | 2009-01-20 16:41:48 -0500 |
commit | 0c06bf709697cfb06ed400a6e4d4d26112e4b3a9 (patch) | |
tree | c3f5f4b07bb4214753fc17b8d3e55a99f1118462 | |
parent | 01940713ac0e35aea6db10aea99dbcf871b323ba (diff) | |
download | stwcs_hcf-0c06bf709697cfb06ed400a6e4d4d26112e4b3a9.tar.gz |
Initial import of a script to handle image alignment in multidrizzle.
git-svn-id: http://svn.stsci.edu/svn/ssb/stsci_python/development/trunk/updatewcs@7454 fe389314-cf27-0410-b35b-8c050e845b92
-rwxr-xr-x | hstwcs/alignwcs.py | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/hstwcs/alignwcs.py b/hstwcs/alignwcs.py new file mode 100755 index 0000000..9bdef8e --- /dev/null +++ b/hstwcs/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_fits(crvals)['pixcrd'] + shifts + ncrvals = outwcs.all_pix2sky_fits(px) + 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) |