#!/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)
#!/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(':')
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': Av,dAv = nice(intrinsic, d[:,0],d[:,1],d[:,2],d[:,3],d[:,4],d[:,5])
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]) import pyfits
#!/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!")
#!/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)
#!/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)
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)
#!/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()
#!/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)
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': arg.warning("fk5 doesn't work correctly yet!")
# 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 for i in range(nimages): img = pyfits.open(inFiles[i])
#!/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)
#!/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()
#!/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'):
#!/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)
# 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: header = pyfits.getheader(f)
#!/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()]
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) header = pyfits.getheader(inFile,ext=ext)
"""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'))
#!/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()
] 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 xmax = naxis1 + 0.5
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: outdata.append(dobin(i,offset,bin,unit))
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