Example #1
0
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])

import pyfits
import numpy as np

stepx  = (rangex[1] - rangex[0])/(1.0*nbins[0]);
stepy  = (rangey[1] - rangey[0])/(1.0*nbins[1]);
Example #2
0
      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)
      defend_loss.append(nd - nd_final)
Example #3
0
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()

data0 = loadtxt(infile,usecols=cols,comments='#')
Example #4
0
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:
      cols.append(i-1)
Example #5
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 #6
0
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 #7
0
          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':
   arg.warning("fk5 doesn't work correctly yet!")
   scale = scale/3600.
if mag == 'p':
   data[:,2] = scale*data[:,2]
else:
   data[:,2] = scale
Example #8
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!")