Example #1
0
   start = 1000
   for t in range(1,1000):
      f = _func(t,flux1,flux2,wave1,wave2,beta)
      if isfinite(f):
         sign = f/abs(f)
         start = t
         break
   for t in range(start,10000):
      f = _func(t,flux1,flux2,wave1,wave2,beta)
      s = f/abs(f)
      if sign*s == -1:
         return t
   nlclib.error('Could not estimate initial temperature for wavelengths %f,%f!' %(wave1,wave2))

if __name__ == "__main__":
   arg = ReadCmd(spec,__doc__)
   infile      = arg.getstr('in',exist=True)
   dist        = arg.getfloat('dist')
   beta        = arg.getfloat('beta')
   sumMethod   = arg.getstr('sum',option=['midpoint','simpson','trapezoid'])
      
   from numpy import loadtxt,where,exp,zeros,pi,log,seterr,arange,linspace,isnan,isfinite

   seterr(over='ignore')
   # some constants
   sun_lum  = 3.826e33      # solar luminosity in ergs/s
   parsec   = 3.08567802e18 # 1 parsec in cm
   planck   = 6.62618e-27   # Planck's constant in erg s
   light    = 2.99792458e10 # speed of light in cm/s
   boltz    = 1.38066e-16   # Boltzmann's constant in K^-1
   hck      = planck*light/boltz
Example #2
0
#!/usr/bin/env python

from readcmd import ReadCmd
import os
import tempfile

spec = """# Take a ds9 contour file and make it usable by WIP
          in    = ??? # DS9 contour file
          out   = ??? # Output WIP file
          dir   = ??? # Output directory with WIP contours
          units = wcs # Units of contour file, other option is linear"""

if __name__ == "__main__":
   arg = ReadCmd(spec)
   infile   = arg.getstr('in',exist=True)
   outfile  = arg.getstr('out',exist=False)
   units    = arg.getstr('units',option=['wcs','linear'])
   contdir  = arg.getstr('dir',exist=False)
      
   os.mkdir(contdir)

   fp0 = open(infile,'r')
   fp1 = open(outfile,'w')
   fp1.write("symbol -1\n")
   written = 0
   fp2 = tempfile.NamedTemporaryFile(mode='w',dir=contdir,delete=False)
   for line in fp0:
      if len(line) == 0: # blank line starts/ends a contour
         fp2.close()
         if written:
            fp1.write("data %s\n" %fp2.name)
Example #3
0
#!/usr/bin/env python

import nlclib
from readcmd import ReadCmd

spec = """# Grid a data set
          in     = ???  # Input file
          xrange = ???  # Range in x (min,max)
          yrange = ???  # Range in y (min,max)
          bins   = 10   # Number of bins (can be x,y)
          xcol   = 1    # Column with x values
          ycol   = 2    # Column with y values
          out    = ???  # Output fits image
          norm   = None # Normalize by sum, max, or none (no normalization)"""

spec = ReadCmd(spec)
inFile  = spec.getstr('in')
rangex  = spec.getlistfloat('xrange',length=2)
rangey  = spec.getlistfloat('yrange',length=2)
nbins   = spec.getlistint('bins',length=[1,2])
xcol    = spec.getint('xcol',min=1) - 1
ycol    = spec.getint('ycol',min=1) - 1
outFile = spec.getstr('out',exist=False)
norm    = spec.getstr('norm',option=['sum','none','max'],ignorecase=True)

if norm is None:
   norm = 'none'

if len(nbins) == 1: #by default make x and y the same size
   nbins.append(nbins[0])
Example #4
0
#!/usr/bin/env python
"""This program will read the output of extmap and make a FITS image"""

from readcmd import ReadCmd

spec = """in  = ??? # Input file from extmap
          out = ??? # Output FITS file"""

arg = ReadCmd(spec,head=__doc__)
inFile  = arg.getstr("in",exist=True)
outFile = arg.getstr("out",exist=False)

import pyfits
from numpy import loadtxt

# read header
head = {'comment' : [], 'history' : []}
fp = open(inFile,'r')
for line in fp:
   if line[0] == '#':
      tmp = line.split()
      key = tmp[1].lower()
      if key == 'comment':
         head['comment'].append(' '.join(tmp[2:]))
      elif key == 'history':
         head['history'].append(' '.join(tmp[2:]))
      elif key in ('naxis1','naxis2'):
         head[key] = int(tmp[3])
      elif key in ('cdelt1','cdelt2','crval1','crval2','crpix1','crpix2'):
         head[key] = float(tmp[3])
      elif key in ('ctype1','ctype2'):
Example #5
0
#!/usr/bin/env python

from readcmd import ReadCmd
import os
import tempfile

spec = """# Take a ds9 contour file and make it usable by WIP
          in    = ??? # DS9 contour file
          out   = ??? # Output WIP file
          dir   = ??? # Output directory with WIP contours
          units = wcs # Units of contour file, other option is linear"""

if __name__ == "__main__":
    arg = ReadCmd(spec)
    infile = arg.getstr('in', exist=True)
    outfile = arg.getstr('out', exist=False)
    units = arg.getstr('units', option=['wcs', 'linear'])
    contdir = arg.getstr('dir', exist=False)

    os.mkdir(contdir)

    fp0 = open(infile, 'r')
    fp1 = open(outfile, 'w')
    fp1.write("symbol -1\n")
    written = 0
    fp2 = tempfile.NamedTemporaryFile(mode='w', dir=contdir, delete=False)
    for line in fp0:
        if len(line) == 0:  # blank line starts/ends a contour
            fp2.close()
            if written:
                fp1.write("data %s\n" % fp2.name)
Example #6
0
      defend_roll.sort()
      attack_roll.reverse()
      defend_roll.reverse()
      ncompare = min(len(attack_roll),len(defend_roll))
      for i in range(ncompare):
         if defend_roll[i] >= attack_roll[i]:
            na = na - 1
         else:
            nd = nd - 1
      #print attack_roll,defend_roll,na,nd
      attack_roll = []
      defend_roll = []
   return na,nd

if __name__ == "__main__":
   arg  = ReadCmd(spec)
   na   = arg.getint('na',min=2)
   nd   = arg.getint('nd',min=1)
   da   = arg.getint('da')
   dd   = arg.getint('dd')
   nsim = arg.getint('nsim')
   
   attack_loss = [] # compute mean and median attacker losses
   defend_loss = [] # compute mean and median defender losses
   na_array = [0]*na # array for number of final attacking armies
   nd_array = [0]*(nd+1) # array for number of final defending armies
   for i in range(nsim):
      na_final,nd_final = simulate_round(da,dd,na,nd)
      na_array[na_final - 1] += 1
      nd_array[nd_final] += 1
      attack_loss.append(na - na_final)
Example #7
0
#!/usr/bin/env python
""" Compute the intrinsic colors and the off-axis covariance matrix value
    for JHKs colors.
    
   This program requires an input file in the format:
   ra dec j_mag j_mag_sigma h_mag h_mag_sigma k_mag k_mag_sigma
"""

from readcmd import ReadCmd

spec = """# Compute intrinsic colors on an input data file
          in = ??? # Input (table) file name"""

arg = ReadCmd(spec)
infile = arg.getstr("in",exist=True)
   
from numpy import loadtxt,sum,sqrt,average,cov

data = loadtxt(infile,usecols=[2,3,4,5,6,7])
wJH    = 1./(data[:,1]**2 + data[:,3]**2)
wHK    = 1./(data[:,3]**2 + data[:,5]**2)
wJHHK  = sqrt(wJH*wHK)

sumJH    = sum(wJH*(data[:,0] - data[:,2]))
sumHK    = sum(wHK*(data[:,2] - data[:,4]))
sumJH2   = sum(wJH*(data[:,0] - data[:,2])**2)
sumHK2   = sum(wHK*(data[:,2] - data[:,4])**2)
sumJHHK  = sum(wJHHK*(data[:,0] - data[:,2])*(data[:,2] - data[:,4]))
sumwJH   = sum(wJH)
sumwHK   = sum(wHK)
sumwJHHK = sum(wJHHK)
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
#!/usr/bin/env python

from readcmd import ReadCmd
import math

spec = """# Make a ds9 region file from vectors
          in    = ???     # Input file
          cols  = 1,2,3,5 # Columns for x,y,polarization,angle
          out   = ???     # Output region file
          scale = 5       # length of 1% polarization (in pixels or arcsec)
          color = green   # Color for region file
          width = 1       # Line width
          coord = fk5     # coordinates of vectors [fk5,physical]
          mag   = p       # Make vector lengths (p)roportional or (u)niform?"""

arg = ReadCmd(spec)
infile  = arg.getstr('in',exist=True)
cols    = arg.getlistint('cols',length=4,min=1)
outfile = arg.getstr('out',exist=False)
scale   = arg.getfloat('scale')
color   = arg.getstr('color')
width   = arg.getint('width')
coord   = arg.getstr('coord',option=['fk5','physical'])
mag     = arg.getstr('mag',option=['p','u'])

from numpy import loadtxt,pi,sin,cos

cols = [a - 1 for a in cols] # make zero-based
data = loadtxt(infile,usecols=cols)

if coord == 'fk5':
Example #10
0
# and nan)
# 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
Example #11
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)
Example #12
0
#!/usr/bin/env python
"""Convert the WCS coordinates in a polarization file to pixels.
Uses rotatevector.py to do the heavy lifting."""

from readcmd import ReadCmd
import os,sys

spec = """in   = ???   # Input polarization file
          fits = ???   # FITS image
          out  = ???   # Output file
          ext  = 0     # Extension number
          cd   = False # Set to True to use cd_matrix instead of cdelt"""

arg = ReadCmd(spec,__doc__)
infile = arg.getstr('in',exist=True)
fits   = arg.getstr('fits',exist=True)
outfile= arg.getstr('out',exist=False)
ext    = arg.getint('ext')
cdFlag = arg.getstr('cd')

import nlclib

blah = nlclib.createtempname() # new file made by rotatevector.py

# get path of this script so we can call rotatevector.py in the same dir
path,junk = os.path.split(os.path.abspath(sys.argv[0]))
os.system('%s/rotatevector.py in=%s fits=%s out=%s ext=%d cd=%s' %(path,
   infile,fits,blah,ext,cdFlag))

data1 = nlclib.readdata(infile)
data2 = nlclib.readdata(blah,dtype=float)
Example #13
0
#!/usr/bin/env python

from readcmd import ReadCmd
import nlclib

spec = """# Convert image to table of pixels or wcs
          in  = ???   # Input image
          ext = 1     # Extension name/number
          wcs = False # Set to true to output RA/DEC instead of pixels
          out = ???   # Output file (can be - for stdout)"""

arg = ReadCmd(spec)
infile  = arg.getstr("in",exist=True)
ext     = arg.getstr("ext")
wcsFlag = arg.getbool("wcs")
outFile = arg.getstr("out",exist=False)

# try to convert extension to an integer
try:
   blah = int(ext) - 1
   ext = blah
except ValueError:
   pass

import pyfits,pywcs
from numpy import arange,meshgrid,savetxt,column_stack

fp = pyfits.open(infile) #,ignore_missing_end=True)
data = fp[ext].data
header = fp[ext].header
fp.close()
Example #14
0
#!/usr/bin/env python
"""change zeros in the FITS file to NaNs"""

from readcmd import ReadCmd

spec = """in  = ??? # Input FITS file
          tol = 3   # Round to this many decimal places before NaN conversion"""

arg = ReadCmd(spec,__doc__)
fitsfile = arg.getstr('in',exist=True)
tol      = arg.getint('tol')

import pyfits,numpy
import nlclib

fimg = pyfits.open(fitsfile,mode='update')
data = fimg[0].data
hdr  = fimg[0].header
if hdr['bitpix'] != -32:
   fimg.close()
   nlclib.error("Bitpix is not -32, NaNs will not work!")

if hdr['naxis'] == 3:
   mask = numpy.where(data[0,:,:].round(tol) == 0)
   data[mask] = numpy.nan
elif hdr['naxis'] == 2:
   mask = numpy.where(data.round(tol) == 0)
   data[mask] = numpy.nan
else:
   fimg.close()
   nlclib.error("NAXIS is not 2 or 3.  I don't know what to do!")
Example #15
0
#!/usr/bin/env python

from numpy import digitize,linspace,loadtxt,zeros,amin,amax,where,ones,sqrt,isinf
from readcmd import ReadCmd

spec = """# Bin data in x, but compute averages in y
          in    = ???  # Input data file
          xcol  = 1    # x column (1-based)
          ycol  = 2    # y column (set to None to skip)
          dxcol = None # x errors column
          dycol = None # y errors column
          xmin  = None # Min value for bins (defaults to data min)
          xmax  = None # Max value for bins (defaults to data max)
          nbin  = 10   # Number of bins"""
   
arg = ReadCmd(spec)
infile = arg.getstr("in",exist=True)
xcol   = arg.getint("xcol",min=1)
ycol   = arg.getint("ycol",min=1)
dxcol  = arg.getint("dxcol",min=1)
dycol  = arg.getint("dycol",min=1)
xmin   = arg.getfloat("xmin")
xmax   = arg.getfloat("xmax")
nbin   = arg.getint("nbin",min=1)

if xcol is None:
   arg.error("You must specify a column number for x!")
   
cols = []
for i in [xcol,ycol,dxcol,dycol]:
   if i is not None:
Example #16
0
          text   = None      # Labels for legend
          title  = None      # Title for plot
          width  = 1         # line thickness
          xcol   = 1         # X column number(s)
          dxcol  = None      # X error column number(s)
          xlabel = None      # X-axis label
          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)
Example #17
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 #18
0
#!/usr/bin/env python

from readcmd import ReadCmd

spec = """# Rotate vectors to account for WCS curvature
          in   = ???     # Input file
          cols = 1,2,3,5 # Columns for ra,dec,polarization percent,angle
          out  = ???     # Output file of x,y,polarization percent, new angle
          fits = ???     # FITS image
          ext  = 0       # Extension number for fits file"""

arg = ReadCmd(spec)
image = arg.getstr('fits', exist=True)
infile = arg.getstr('in', exist=True)
cols = arg.getlistint('cols', length=4)
outfile = arg.getstr('out', exist=False)
ext = arg.getint('ext')

cols = map(lambda a: a - 1,
           cols)  # convert cols to a zero-based column numbers

import worldpos

from numpy import loadtxt, savetxt, sin, cos, pi, arctan, column_stack
import pyfits

# read CDELT from FITS header to pick an appropriate distance for scaling
fp = pyfits.open(image, mode='readonly')
cdelt = abs(float(fp[ext].header['cdelt2']))  # cdelt in degrees
scale = 5 * cdelt
fp.close()
Example #19
0
      dAv = sqrt((sigmaj**2 + sigmah**2 + intrinsic['jh'][1]**2))/intrinsic['k1']
   elif intrinsic['method'] == 'hk':
      Av  = ((magh - magks) - intrinsic['hk'][0])/intrinsic['k2']
      dAv = sqrt((sigmah**2 + sigmaks**2 + intrinsic['hk'][1]**2))/intrinsic['k2']
   else:
      Av = None
      dAv = None
   return Av,dAv
   
def rjce(intrinsic, magj, sigmaj, magh, sigmah, magks, sigmaks):
   """Compute extinction using RJCE (Majewski TODO)"""
   
   return None,None

if __name__ == "__main__":
   arg = ReadCmd(spec)
   infile  = arg.getstr("in",exist=True)
   outfile = arg.getstr("out",exist=False)
   intrinsic = {}
   intrinsic['k1']  = 1./arg.getfloat("avaj")
   intrinsic['k2']  = 1./arg.getfloat("avah")
   intrinsic['jh']  = arg.getlistfloat("jh")
   intrinsic['hk']  = arg.getlistfloat("hk")
   intrinsic['cov'] = arg.getfloat("cov")
   intrinsic['method'] =  arg.getstr("method",option=['jh','hk','nicer','rjce'])

   from numpy import loadtxt,savetxt,sqrt

   data = loadtxt(infile,dtype=str)
   d    = data[:,2:8].astype(float)
   if intrinsic['method'] == 'jh' or intrinsic['method'] == 'hk':
Example #20
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 #21
0
   # label steps in hour-seconds
   tmplabel = [1  ,  2,5,10,15,20,30,60,120,300,600,900,1200,1800,3600,18000]
   tmpstep  = [0.5,0.5,1, 2, 5, 5, 5,20, 30, 60,300,300, 300, 600,1200, 3600]
   
   ralabel = map(lambda a: a/3600.,tmplabel) # convert to hours
   stepra  = map(lambda a: a/3600.,tmpstep)  # convert to hours

   tmp = abs(maxra - minra)
   guess = tmp/6. # aim for about 6 labels
   diff  = [abs(guess-a) for a in ralabel]
   idx   = diff.index(min(diff))
   
   return stepra[idx],ralabel[idx]
 
if __name__ == "__main__":
   arg = ReadCmd(spec)
   image      = arg.getstr('in',exist=True)
   rangex     = arg.getlistfloat('xrange',length=2)
   rangey     = arg.getlistfloat('yrange',length=2)
   tmplength  = arg.getfloat('length')
   tmpra      = arg.getlistfloat('ra',length=2)
   tmpdec     = arg.getlistfloat('dec',length=2)
   outfile    = arg.getstr('out',exist=False)

   import worldpos
   wcs = worldpos.wcs(image)
   if wcs.header['rot'] != 0:
      error('WIP cannot handle rotations!')
      
   if len(rangex) == 0:
      xmin = 0.5
Example #22
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 #23
0
#!/usr/bin/env python
# This is a python replacement for overlap.c, which chokes when pixels are
# too far from image.  This works, but does have a 180 degree bug in pywcs.
# (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:
Example #24
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 #25
0
         header['CD1_1'] = -1*scale
         header['CD1_2'] = 0.0
         header['CD2_1'] = 0.0
         header['CD2_2'] = scale
         angle = 180./pi*angle
      else:      
         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)
Example #26
0
      """Action to take when user clicks on 'Show' button"""

      global _circsize,_circlecolor
      _circsize = self.size.get()
      _circlecolor = self.color.get()
      os.system('xpaset -p %s regions format ds9' %self.window)
      blah = nlclib.createtempname()
      try:
         fp = open(blah,'w')
         ds9.makeRegion(fp,_all.ra,_all.dec,self.color.get(),float(self.size.get()),
            self.number.get())
         fp.close()
      except IOError, value:
         if "Permission denied" in value:
            nlclib.error('You do not have write permissions for %s' %os.getcwd())
         else:
            nlclib.error('Unknown problem with opening temp. region file!')
      for i in range(0,self.numframes):
         print "starting frame %d" %(i+1)
         ds9.region(self.window,i+1,blah,self.delete.get())
      ds9.view(self.window)
      nlclib.remove(blah)
      
      if self.reset.get():
         os.system('xpaset -p %s frame center' %self.window)
         os.system('xpaset -p %s zoom 1' %self.window)

if __name__ == '__main__':
   arg = ReadCmd(spec)
   start(arg.getstr('in',exist=True),arg.getstr('window'),arg.getint('nframes'))
Example #27
0
#!/usr/bin/env python

from readcmd import ReadCmd

spec = """# Rotate vectors to account for WCS curvature
          in   = ???     # Input file
          cols = 1,2,3,5 # Columns for ra,dec,polarization percent,angle
          out  = ???     # Output file of x,y,polarization percent, new angle
          fits = ???     # FITS image
          ext  = 0       # Extension number for fits file"""

arg = ReadCmd(spec)
image  = arg.getstr('fits',exist=True)
infile = arg.getstr('in',exist=True)
cols   = arg.getlistint('cols',length=4)
outfile= arg.getstr('out',exist=False)
ext    = arg.getint('ext')

cols = map(lambda a: a-1,cols) # convert cols to a zero-based column numbers

import worldpos

from numpy import loadtxt,savetxt,sin,cos,pi,arctan,column_stack
import pyfits

# read CDELT from FITS header to pick an appropriate distance for scaling
fp = pyfits.open(image,mode='readonly')
cdelt = abs(float(fp[ext].header['cdelt2'])) # cdelt in degrees
scale = 5*cdelt
fp.close()
Example #28
0
        0.5, 0.5, 1, 2, 5, 5, 5, 20, 30, 60, 300, 300, 300, 600, 1200, 3600
    ]

    ralabel = map(lambda a: a / 3600., tmplabel)  # convert to hours
    stepra = map(lambda a: a / 3600., tmpstep)  # convert to hours

    tmp = abs(maxra - minra)
    guess = tmp / 6.  # aim for about 6 labels
    diff = [abs(guess - a) for a in ralabel]
    idx = diff.index(min(diff))

    return stepra[idx], ralabel[idx]


if __name__ == "__main__":
    arg = ReadCmd(spec)
    image = arg.getstr('in', exist=True)
    rangex = arg.getlistfloat('xrange', length=2)
    rangey = arg.getlistfloat('yrange', length=2)
    tmplength = arg.getfloat('length')
    tmpra = arg.getlistfloat('ra', length=2)
    tmpdec = arg.getlistfloat('dec', length=2)
    outfile = arg.getstr('out', exist=False)

    import worldpos
    wcs = worldpos.wcs(image)
    if wcs.header['rot'] != 0:
        error('WIP cannot handle rotations!')

    if len(rangex) == 0:
        xmin = 0.5
Example #29
0
   data = nlclib.readcolumns(filename,(1,2,5,6),value='float')
   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
Example #30
0
#!/usr/bin/env python

from readcmd import ReadCmd

spec = """# Read a FITS header and print it
          in  = ???  # Input FITS file
          hdu = None # List of HDUs #'s to print, starting with 1.  Defaults to all HDUs""" 

arg = ReadCmd(spec)
infile  = arg.getstr("in",exist=True,type='file')
hdulist = arg.getlistint("hdu",min=1)

import pyfits

fp = pyfits.open(infile)
n = len(fp) # number of hdus

if len(hdulist) == 0:
   hdulist = range(n)
else:
   hdulist = [a-1 for a in hdulist]

for i in hdulist:
   print("### HDU #%d" %(i+1))
   head = fp[i].header
   bob = head.tostring('\n').split('\n')
   for line in bob:
      tmp = line.strip()
      if len(tmp) > 0:
         print(tmp)
fp.close()