Created
November 30, 2012 15:33
-
-
Save dsberry/4176461 to your computer and use it in GitHub Desktop.
Translates a FITS image in X and y, correcting the WCS accordingly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
# Moves a simple 2D FITS image in X and Y. The output image is the same size as | |
# the input image. | |
# Usage: | |
# move_wcs.py <infile> <outfile> <xshift> <yshift> | |
# | |
# <infile> = Input fits file | |
# <outfile> = Output fits file | |
# <xcen> = The shift in X pixels | |
# <ycen> = The shift in Y pixels | |
# Notes: | |
# - Requires pyast version 2.3 | |
import pyfits | |
import sys | |
import starlink.Ast as Ast | |
import starlink.Atl as Atl | |
# Open the FITS file using pyfits. A list of the HDUs in the FITS file is | |
# returned. | |
hdu_list = pyfits.open( sys.argv[1] ) | |
# Create an object that will transfer FITS header cards between an AST | |
# FitsChan and the PyFITS header describing the primary HDU of the | |
# supplied FITS file. | |
adapter = Atl.PyFITSAdapter( hdu_list[ 0 ] ) | |
# Create a FitsChan and use the above adapter to copy all the header | |
# cards into it. | |
fitschan = Ast.FitsChan( adapter, adapter ) | |
# Get the flavour of FITS-WCS used by the header cards currently in the | |
# FitsChan. This is so that we can use the same flavour when we write | |
# out the modified WCS. | |
encoding = fitschan.Encoding | |
# Read WCS information from the FitsChan. Additionally, this removes all | |
# WCS information from the FitsChan. The returned wcsinfo object | |
# is an AST FrameSet, in which the current Frame describes WCS coordinates | |
# and the base Frame describes pixel coodineates. The FrameSet includes a | |
# Mapping that specifies the transformation between the two Frames. | |
wcsinfo = fitschan.read() | |
# Check that the FITS header contained WCS in a form that can be | |
# understood by AST. | |
if wcsinfo == None: | |
print( "Failed to read WCS information from {0}".format(sys.argv[1]) ) | |
# This script is restricted to 2D arrays, so check the FITS file has 2 pixel | |
# axes (given by Nin) and 2 WCS axes (given by Nout). | |
elif wcsinfo.Nin != 2 or wcsinfo.Nout != 2: | |
print( "{0} is not 2-dimensional".format(sys.argv[1]) ) | |
# Proceed if the WCS information was read OK. | |
else: | |
# Get the lower and upper bounds of the input image in pixel indices. | |
# All FITS arrays by definition have lower pixel bounds of [1,1] (unlike | |
# NDFs). Note, unlike pyfits AST uses FITS ordering for storing pixel axis | |
# values in an array (i.e. NAXIS1 first, NAXIS2 second, etc). | |
lbnd_in = [ 1, 1 ] | |
ubnd_in = [ fitschan["NAXIS1"], fitschan["NAXIS2"] ] | |
# Get the required pixel shifts in X and Y. | |
x0 = float( sys.argv[3] ) | |
y0 = float( sys.argv[4] ) | |
# Construct a ShiftMap to do the shift. | |
pixmap = Ast.ShiftMap( [ x0, y0 ] ) | |
# The dimensions of the output array are the same as the input array. | |
lbnd_out = lbnd_in | |
ubnd_out = ubnd_in | |
# Get the value used to represent missing pixel values | |
if "BLANK" in fitschan: | |
badval = fitschan["BLANK"] | |
flags = Ast.USEBAD | |
else: | |
badval = 0 | |
flags = 0 | |
# Resample the data array using the above mapping. | |
( npix, out, out_var ) = pixmap.resample( lbnd_in, ubnd_in, | |
hdu_list[0].data, None, | |
Ast.LINEAR, None, flags, | |
0.05, 1000, badval, lbnd_out, | |
ubnd_out, lbnd_out, ubnd_out ) | |
# Store the shifted data in the HDU. | |
hdu_list[0].data = out | |
# Re-map the base Frame (i.e. the pixel coordinates Frame) so that it | |
# refers to the new data grid instead of the old one. | |
wcsinfo.remapframe( Ast.BASE, pixmap ) | |
# Attempt to write the modified WCS information to the primary HDU (i.e. | |
# convert the FrameSet to a set of FITS header cards stored in the | |
# FITS file). Indicate that we want to use original flavour of FITS-WCS. | |
fitschan.Encoding = encoding | |
fitschan.clear('Card') | |
if fitschan.write( wcsinfo ) == 0 : | |
print( "Failed to convert the rotated WCS to Fits-WCS" ) | |
# If successfull, force the FitsCHan to copy its contents into the | |
# PyFITS header, then write the changed data and header to the output | |
# FITS file. | |
else: | |
fitschan.writefits() | |
hdu_list.writeto(sys.argv[2],clobber=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment