#!/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()
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]);
# 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) fitsdata = pyfits.getdata(f)
#!/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()
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) nx = len(xcol)
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!") scale = scale/3600.
#!/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()