Example #1
0
# (e.g. ra,dec = 230,-30 will return a valid pixel even though it is exactly
# 180 degrees away from actual valid coordinates of ra,dec = 50,+30
from readcmd import ReadCmd

spec = """# Overlap coordinates in a file with a FITS image
          in    = ??? # Input file
          col   = 1,2 # Column numbers to read for RA,DEC
          fits  = ??? # Input FITS image(s)
          out   = ??? # Output file(s) of sources inside(,outside) image(s)
          logic = and # 'and' = in all image(s), 'or' = in any image(s)"""
#          null  = NaN # Pixels with this value are considered outside"""

arg = ReadCmd(spec)
infile = arg.getstr("in",exist=True)
col    = arg.getlistint("col",length=2,min=1)
fits   = arg.getliststr("fits",exist=True)
out    = arg.getliststr("out",length=[1,2],exist=False)
logic  = arg.getstr("logic",option=['and','or'])
#null   = arg.getstr("null")

import pyfits,pywcs
from numpy import loadtxt,transpose,ones,zeros,array,where,isnan

# read in ra/dec only
data = loadtxt(infile,usecols=[col[0]-1,col[1]-1])
mask = zeros(data.shape[0])

for f in fits:
   header = pyfits.getheader(f)
   fitsdata = pyfits.getdata(f)
   wcs = pywcs.WCS(header)
Example #2
0
   maxy = max(data[2])
   for i in reversed(range(len(data[0]))):
      if data[2][i] <= 0:
         del(data[0][i])
         del(data[1][i])
         del(data[2][i])
         del[data[3][i]]
      else:
         data[2][i] = data[2][i]/maxy
         data[3][i] = data[3][i]/(maxy*math.sqrt(data[1][i])) #std.dev. mean
   return data

### Start Program ###

arg = ReadCmd(spec)
image   = arg.getliststr('in',exist=True)
bin     = arg.getlistfloat('bin',length=3)
unit    = arg.getstr('unit',option=['nbin','step'])
offset  = arg.getlistfloat('offset',length=2)
outplot = arg.getstr('out')
junk    = arg.getlistfloat('range',length=2)
xmin = junk[0]
xmax = junk[1]
step    = arg.getstr('step',option=['b','p'])

if 'b' in step: # do binning
   outdata = []

   from pyraf import iraf

   for i in image:
Example #3
0
#!/usr/bin/env python

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()]
Example #4
0
#!/usr/bin/env python
"""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(':')
Example #5
0
#!/usr/bin/env python

from readcmd import ReadCmd

spec = """# compute statistics on an image or subregion of an image
          in   = ???  # Input image
          ext  = 1    # Extension number to read from in file
          reg  = None # subregion given as xmin:xmax,ymin:ymax (pixels)"""

arg = ReadCmd(spec)
infile = arg.getstr("in",exist=True,type='file')
reg    = arg.getliststr('reg',length=[0,2])
ext    = arg.getstr("ext")

import nlclib
import pyfits
from numpy import isfinite,where,nanmin,nanmax,median,average,std

ext = nlclib.findExt(infile,ext)
reg = nlclib.readRegion(reg)

header = pyfits.getheader(infile,ext=ext)
data   = pyfits.getdata(infile,ext=ext)

if reg is None:
   minval = nanmin(data)
   maxval = nanmax(data)
   mask = where(isfinite(data))
   medval = median(data[mask])
   avgval = average(data[mask])
   stdval = std(data[mask],ddof=1)
Example #6
0
#!/usr/bin/env python
"""Parse extension(s) from one or more files and combine into a cube"""

from readcmd import ReadCmd
import sys

spec = """in  = ??? # Input file(s) to process
          ext = ??? # Extension(s) to extract (start at 1)
          out = ??? # Output filename"""

arg = ReadCmd(spec,head=__doc__)
infiles = arg.getliststr('in',exist=True)
ext     = arg.getliststr('ext')
outfile = arg.getstr('out',exist=False)
hist    = arg.getkeys(comment='IMEXTRACT.PY:',format=72,time=True).strip().split('\n')

next = len(ext)
nfiles = len(infiles)

import pyfits
import nlcastro
from numpy import zeros

# do first file, need outside loop in case there is only one file in total
idx = nlcastro.findExt(infiles[0],ext)
header = [pyfits.getheader(infiles[0],idx[i]) for i in xrange(next)]

# compile list with names of extensions.  Read from first file
img = pyfits.open(infiles[0])
extname = [img[idx[i]].name for i in xrange(next)]
img.close()
Example #7
0
          ycol   = 2         # Y column number(s)
          dycol  = None      # Y error column number(s)
          ylabel = None      # Y-axis label"""

def error(text):
   """Write out error message and quit"""
   sys.stderr.write("### Fatal Error! %s\n" %text)
   sys.exit()

arg = ReadCmd(spec)
infile   = arg.getstr('in',exist=True)
xcol     = arg.getlistint('xcol',min=1)
ycol     = arg.getlistint('ycol',min=1)
dxcol    = arg.getlistint('dxcol',min=1)
dycol    = arg.getlistint('dycol',min=1)
style    = arg.getliststr('style')
color    = arg.getliststr('color')
limits   = arg.getlistfloat('limits',length=4)
out      = arg.getstr('out')
xlab     = arg.getstr('xlabel')
ylab     = arg.getstr('ylabel')
mytext   = arg.getliststr('text')
mytitle  = arg.getstr('title')
mywidth  = arg.getliststr('width')
legloc   = arg.getlistfloat('legend',length=2)

nx       = len(xcol)
ny       = len(ycol)
ndx      = len(dxcol)
ndy      = len(dycol)
nstyle   = len(style)
Example #8
0
#!/usr/bin/env python

from readcmd import ReadCmd

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]
Example #9
0
# avg will compute unweighted average, and produce uncertainty map from normal
# propagation of error.
# wgtavg will compute weighted average

from readcmd import ReadCmd
import nlclib

spec = """# Combine FITS images into one
          in     = ???  # Input images
          err    = None # Optional error images for input images
          oper   = avg  # Can compute sum, avg, or wgtavg (weighted average)
          out    = ???  # Output image
          outerr = None # Optional output error image (coverage map if no err)"""

arg = ReadCmd(spec)
inFiles  = arg.getliststr('in',exist=True)
errFiles = arg.getliststr('err',exist=True,length=[1,len(inFiles)])
oper     = arg.getstr('oper',option=['avg','sum','wgtavg'])
outFile  = arg.getstr('out',exist=False)
outErr   = arg.getstr('outerr',exist=False)

nimages = len(inFiles)
errFlag = False
if len(errFiles) > 0:
   errFlag = True
   if len(errFiles) == 1:
      errFiles = nimages*errFiles

import pyfits
from numpy import zeros,isfinite,where,sqrt