Here is a piece of code I wrote a while back. It actually does 2 things, one of which is getting the pixel scale (you might need the 'get the position angle' part later too).
The CD1_2 and CD2_2 keywords are a different way of storing the coordinate system information -- generally an image will have either
CDELT1,CDELT2,CROTA1,CROTA2
or CD1_1, CD 1_2, CD2_1, CD2_2
but not both sets.
def get_wcs(im):
''' given an image, returns PA in degrees
and pixel size (assumed to be square) in degrees'''
fimg = pyfits.open(im,'readonly')
if len(fimg) > 1:
hdr = fimg['SCI'].header
else:
hdr = fimg[0].header
if hdr.has_key('CROTA2') and hdr.has_key('CDELT2'): # use CROTA1 and CDELT keywords if there
cr2 = 0.0 - hdr['CROTA2']
pxsc = math.fabs(hdr['CDELT2'])
elif hdr.has_key('CD1_2') and hdr.has_key('CD2_2'): # or use CD matrix: lazy case assuming no skew
cd12 = hdr['CD1_2']
cd22 = hdr['CD2_2']
cr2 = math.atan2(cd12,cd22)_180.0/math.pi
pxsc = math.sqrt(cd12_2+cd22*2)
else:
cr2 = 0.0
pxsc = 0.0
fimg.close()
return(cr2,pxsc)
Here is a piece of code I wrote a while back. It actually does 2 things, one of which is getting the pixel scale (you might need the 'get the position angle' part later too). The CD1_2 and CD2_2 keywords are a different way of storing the coordinate system information -- generally an image will have either CDELT1,CDELT2,CROTA1,CROTA2 or CD1_1, CD 1_2, CD2_1, CD2_2 but not both sets.
def get_wcs(im): ''' given an image, returns PA in degrees and pixel size (assumed to be square) in degrees''' fimg = pyfits.open(im,'readonly') if len(fimg) > 1: hdr = fimg['SCI'].header else: hdr = fimg[0].header if hdr.has_key('CROTA2') and hdr.has_key('CDELT2'): # use CROTA1 and CDELT keywords if there cr2 = 0.0 - hdr['CROTA2']
pxsc = math.fabs(hdr['CDELT2']) elif hdr.has_key('CD1_2') and hdr.has_key('CD2_2'): # or use CD matrix: lazy case assuming no skew cd12 = hdr['CD1_2'] cd22 = hdr['CD2_2'] cr2 = math.atan2(cd12,cd22)_180.0/math.pi
pxsc = math.sqrt(cd12_2+cd22*2) else: cr2 = 0.0 pxsc = 0.0 fimg.close() return(cr2,pxsc)