Example #1
0
         try:
            angle = float(header[rot]) # try to extract value from FITS header
            del(header[rot])
         except KeyError:
            arg.error("No header keyword '%s' found for rotation angle!" %rot)
         except ValueError:
            arg.error("Cannot read rotation angle '%s'!" %header[rot])
   return angle

arg = ReadCmd(spec)
inFile  = arg.getstr('in',exist=True,type='file')
outFile = arg.getstr('out',exist=False)
expand  = arg.getbool('expand')
tmpext  = arg.getstr('ext')
rot     = arg.getstr('rot')
hist    = arg.getkeys(comment='IMROTATE.PY:',format=72,time=True).strip().split('\n')

import nlcastro
import pyfits
import scipy.interpolate
from numpy import nan,isnan,pi,arctan2,cos,sin,meshgrid,arange,ceil,floor
from numpy import amin,amax,where,isfinite,zeros,around

ext = nlcastro.findExt(inFile,tmpext)

header = pyfits.getheader(inFile,ext=ext)
if header['naxis'] != 2:
   arg.error('Can only rotate 2-d images!')
data   = pyfits.getdata(inFile,ext=ext)

angle = getAngle(rot,header)
Example #2
0
"""Simple script to do subimages of a larger FITS"""

from readcmd import ReadCmd
import sys

spec = """in     = ???   # Input filename
          region = ???   # Region to clip, as xmin:xmax,ymin:ymax
          out    = ???   # Output filename
          wcs    = True  # Compute new WCS for output image?"""

arg = ReadCmd(spec,head=__doc__)
inFile  = arg.getstr('in',exist=True)
region  = arg.getliststr('region',length=2)
outFile = arg.getstr('out',exist=False)
wcsFlag = arg.getbool('wcs')
keys = arg.getkeys(comment='IMSUB.PY:',format=80,time=True).strip().split('\n')

import pyfits,pywcs
from numpy import where,nan
   
# open FITS image and read header
if inFile == '-':
   fp = sys.stdin
else:
   fp = pyfits.open(inFile)

header = fp[0].header

# read and interpret region keyword
tmp1 = region[0].split(':')
tmp2 = region[1].split(':')
Example #3
0
spec = """# Edit, add, or delete one or more FITS header keywords and values
            in    = ??? # Input FITS file(s)
            ext   = 1   # FITS extension(s) to edit (name or number)
            key   = ??? # FITS keyword(s) to edit (or add) prepend '-' to delete
            value = ??? # values for each keyword (or 1 for all keywords)
            out   = ??? # Output FITS file"""

### Main Program ###
arg = ReadCmd(spec)
infile  = arg.getliststr('in',exist=True)
ext     = arg.getliststr('ext')
junk    = arg.getliststr('key')
value   = arg.getliststr('value',length=[1,len(junk)])
outfile = arg.getliststr('out',exist=False,length=len(infile))
histroot = arg.getkeys(comment='HEDIT.PY:',format=72,time=True).strip().split('\n')

keylist = [a.lower() for a in junk]
if len(value) == 1:
   value = len(junk)*value

import pyfits
import nlcastro

for inname,outname in zip(infile,outfile):
   print "%s -> %s" %(inname,outname)
   img = pyfits.open(inname)
   extnames = [nlcastro.findExt(inname,e) for e in ext]

   for e in extnames:
      head = img[e].header
Example #4
0
from readcmd import ReadCmd

spec = """# Copy header keywords from input file to output file
          in     = ??? # Input file (from file)
          inext  = 1   # Extension name/number of input file
          key    = ??? # Header key(s) to copy (all for all keywords)
          out    = ??? # Output file (to file, must exist)
          outext = 1   # Output extension(s) to modify"""

arg     = ReadCmd(spec)
inFile  = arg.getstr('in',exist=True)
inext   = arg.getstr('inext')
junk    = arg.getliststr('key')
outFile = arg.getstr('out',exist=True)
outext  = arg.getliststr('outext')
hist    = arg.getkeys(comment='CPHEAD.PY:',format=72,time=True).strip().split('\n')

import nlcastro
import pyfits

keylist = [a.lower() for a in junk]


img   = pyfits.open(inFile)
idx   = nlcastro.findExt(img,inext)
head1 = img[idx].header
img.close()

junk = [a.lower() for a in head1.keys()]
if keylist[0] != 'all':
   for key in keylist:
Example #5
0
#!/usr/bin/env python

from readcmd import ReadCmd

spec = """# recenter an image so crpix1,2 and crval1,2 are at the center of the map
          in  = ??? # Input FITS image
          out = ??? # Output FITS image
       """

arg = ReadCmd(spec)
infile  = arg.getstr("in",exist=True,type="file")
outfile = arg.getstr("out",exist=False)
hist = arg.getkeys(comment='RECENTER.PY:',format=72,time=True).strip().split('\n')

import pyfits
import pywcs

fp = pyfits.open(infile)
wcs = pywcs.WCS(fp[0].header)
crpix1 = fp[0].header['naxis1']/2. + 0.5
crpix2 = fp[0].header['naxis2']/2. + 0.5

ra,dec = wcs.wcs_pix2sky(crpix1,crpix2,1)
fp[0].header['crpix1'] = crpix1
fp[0].header['crpix2'] = crpix2
fp[0].header['crval1'] = ra[0]
fp[0].header['crval2'] = dec[0]

# add history
for h in hist:
   fp[0].header.add_history(h)