def kaitphot(refimage, inlist, sncoo, aperture=10.0, dx=20, dy=20): infiles = iraffiles(inlist) # Find location of SN [[snra, sndec]] = impix2wcs(refimage, sncoo[0], sncoo[1]) sn=[Star(name="New_SN",xval=sncoo[0],yval=sncoo[1])] snstars=Starlist(stars=sn) print "Coordinates of new SN: %s %s (J2000.0)" % (snra, sndec) # Grab reference rootname refroot,null = refimage.split('.') # Big loop for image in infiles: # Image root root,null = image.split('.') # Grab zeropt and zeropt error from image header zp,zpu = get_head(image,[ZPTKEY,ZPUKEY]) # Detect objects in difference image iraf.iqobjs('%s.sub.fits' % root,SIGMA,SATVAL,skyval="0.0", zeropt=float(zp),masksfx=MASKSFX,wtimage="",fwhm=FWHM, pix=PIXSCALE,aperture=2*FWHM/PIXSCALE,gain=FL_GAIN, minlim=no,clobber=yes,verbose=no) # See if object was detected stars=Starlist('%s.sub.fits.stars' % root) match,snmatch=stars.match(snstars,useflags=yes) # If so, use SN magnitude and statistical error if len(match): snmag = match[0].mag snerr = match[0].magu # Otherwise calculate upper limit else: statsec = '[%i:%i,%i:%i]' % (int(sncoo[0]-dx/2), int(sncoo[0]+dx/2), int(sncoo[1]-dy/2), int(sncoo[1]+dy/2)) iraf.iterstat('%s.sub%s' % (root, statsec), nsigrej=5, maxiter=5, prin=no,verbose=no) sigma=float(iraf.iterstat.sigma) snmag = - 2.5 * log10(3 * sigma * sqrt(pi * power(aperture,2))) + \ float(zp) snerr = 99.999 # Want UT of Observation [dateobs,ut] = get_head(image,['DATE-OBS','UT']) tobs=DateTime(int(dateobs[6:]),int(dateobs[3:5]),int(dateobs[:2]), int(ut[:2]),int(ut[3:5]),int(ut[6:])) # Return SN Mag / UL print '%s\t%s %.3f %10.3f%10.3f%10.3f%10.3f' % (root,tobs.Format("%b"), (((tobs.second/3600.0)+tobs.minute)/60.0+tobs.hour)/24.0+ tobs.day,snmag,snerr,float(zp),float(zpu)) print 'Exiting Successfully' return
def lmi_detrend( imlist, otype="OBJECT", ppre="p", bkey="BIASSEC", tkey="TRIMSEC", bpre="b", bfile="Bias.fits", fkey="FILTER", fpre="f", flatpre="Flat", skybkg="SKYBKG", skysub="SKYSUB", skysig="SKYSIG", fpfx="F", clobber=globclob, verbose=globver, ): """Identify science frames, pre-process, subtract bias, divide by flat, and correct for non-linearity.""" images = glob.glob(imlist) images.sort() # Loop through all images for im in images: hdr = pyfits.getheader(im) if hdr["OBSTYPE"] == otype: # Preprocess preproc(im, fkey=fkey, ppre=ppre, bkey=bkey, tkey=tkey, clobber=clobber, verbose=verbose) # Bias subtraction lmi_debias("%s%s" % (ppre, im), bpre=bpre, bfile=bfile) # Flat field lmi_flat("%s%s%s" % (bpre, ppre, im), fpre=fpre, fkey=fkey, flatpre="Flat") # Linearity correction lmi_lincor("%s%s%s%s" % (fpre, bpre, ppre, im)) # Write sky values to header iraf.iterstat("%s%s%s%s" % (fpre, bpre, ppre, im), verbose=no, prin=no) fimg = pyfits.open("%s%s%s%s" % (fpre, bpre, ppre, im), mode="update") fimg[0].header[skybkg] = iraf.iterstat.median fimg[0].header[skysig] = iraf.iterstat.sigma fimg[0].header[skysub] = 0 fimg.flush() fimg.close() # Final processed image shutil.copy("%s%s%s%s" % (fpre, bpre, ppre, im), "%s%s" % (fpfx, im)) return
def nirc_sky(image, bpm): usebpm = 0 if len(bpm) > 0: re1 = re.search("^\!(\w+)", bpm) if re1: bpmkey = re1.group(1) bpmfile = get_head(image, bpmkey) else: bpmfile = bpm if len(bpmfile) > 0 and os.path.exists(bpmfile): usebpm = 1 if usebpm: mimstat = iraf.mimstatistics( image, imasks='!BPM', omasks="", fields='image,npix,mean,stddev,min,max,mode', lower='INDEF', upper='INDEF', nclip=4, lsigma=5.0, usigma=5.0, binwidth=0.1, format=no, Stdout=1, Stderr=1) mimels = mimstat[0].split() skybkg = None if len(mimels) == 6: try: skybkg = float(mimels[5]) except: pass if not skybkg: usebpm = 0 if not usebpm: iraf.iterstat(image, nsigrej=5, maxiter=10, prin=no, verbose=no) skybkg = float(iraf.iterstat.median) return skybkg
def wirc_sky(image,bpm): usebpm=0 if len(bpm)>0: re1=re.search("^\!(\w+)",bpm) if re1: bpmkey=re1.group(1) bpmfile=get_head(image,bpmkey) else: bpmfile=bpm if len(bpmfile)>0 and os.path.exists(bpmfile): usebpm=1 if usebpm: mimstat=iraf.mimstatistics(image,imasks='!BPM',omasks="", fields='image,npix,mean,stddev,min,max,mode', lower='INDEF',upper='INDEF',nclip=4, lsigma=5.0,usigma=5.0,binwidth=0.1, format=no,Stdout=1,Stderr=1) mimels=mimstat[0].split() skybkg=None if len(mimels)==6: try: skybkg=float(mimels[5]) except: pass if not skybkg: usebpm=0 if not usebpm: iraf.iterstat(image,nsigrej=5,maxiter=10, prin=no,verbose=no) skybkg=float(iraf.iterstat.median) return skybkg
def iq_ratir(chip="C1", filt="i", reflist="sdss.reg"): '''Process RATIR data for given chip and filter''' # Check and see if bias frame exists. If not, create one if not os.path.exists("Bias-%s.fits" % chip): imlist = glob.glob("[0-9]*T[0-9]*%sb.fits" % chip) hdr = pyfits.getheader(imlist[0]) # Setup zerocombine zerocombine = iraf.ccdred.zerocombine zerocombine.combine = 'median' zerocombine.reject = 'avsigclip' zerocombine.ccdtype = '' zerocombine.process = no zerocombine.delete = no zerocombine.clobber = no zerocombine.scale = 'none' zerocombine.statsec = '*' zerocombine.nlow = 0 zerocombine.nhigh = 1 zerocombine.nkeep = 1 zerocombine.mclip = yes zerocombine.lsigma = 3.0 zerocombine.hsigma = 3.0 zerocombine.rdnoise = 0.0 zerocombine.gain = hdr['SOFTGAIN'] zerocombine.snoise = 0 zerocombine.pclip = -0.5 zerocombine.blank = 0.0 # Run zerocombine bstr = ",".join(imlist) zerocombine(input=bstr, output="Bias-%s.fits" % chip) # Bias subtract flat frames imlist = glob.glob("[0-9]*T[0-9]*%sf.fits" % chip) flis = [] for im in imlist: hdr = pyfits.getheader(im) if (hdr["FILTER"] == filt) and (not os.path.exists("b%s" % im)): # Subtract bias frame ccdproc = iraf.ccdred.ccdproc ccdproc.ccdtype = "" ccdproc.noproc = no ccdproc.fixpix = no ccdproc.overscan = no ccdproc.trim = no ccdproc.zerocor = yes ccdproc.darkcor = no ccdproc.flatcor = no ccdproc.illumcor = no ccdproc.fringecor = no ccdproc.readcor = no ccdproc.scancor = no ccdproc.readaxis = 'line' ccdproc.fixfile = "" ccdproc.zero = "Bias-%s.fits" % chip ccdproc.interactive = no ccdproc.function = "legendre" ccdproc.order = 1 ccdproc.sample = "*" ccdproc.naverage = 1 ccdproc.niterate = 1 ccdproc.low_reject = 3.0 ccdproc.high_reject = 3.0 ccdproc.grow = 0 ccdproc(images=im, output="b%s" % im) flis.append("b%s" % im) elif (hdr["FILTER"] == filt): flis.append("b%s" % im) # Create flat field if not os.path.exists("Flat-%s-%s.fits" % (chip, filt)): glis = [] for im in flis: iraf.iterstat.nsigrej = 5.0 iraf.iterstat.maxiter = 10 iraf.iterstat.verbose = globver iraf.iterstat.lower = INDEF iraf.iterstat.upper = INDEF iraf.iterstat(im) if (iraf.iterstat.median < RATIRSAT[chip]) and (iraf.iterstat.median > 1000.0): glis.append(im) # Set up flatcombine flatcombine = iraf.ccdred.flatcombine flatcombine.combine = 'median' flatcombine.reject = 'avsigclip' flatcombine.ccdtype = '' flatcombine.scale = 'median' flatcombine.statsec = '' flatcombine.nlow = 1 flatcombine.nhigh = 1 flatcombine.nkeep = 1 flatcombine.mclip = yes flatcombine.lsigma = 3.0 flatcombine.hsigma = 3.0 flatcombine.rdnoise = 0.0 flatcombine.gain = hdr["SOFTGAIN"] flatcombine.snoise = 0.0 flatcombine.pclip = -0.5 # Run flatcombine fstr = ",".join(glis[:10]) flatcombine(fstr, output="Flat-%s-%s.fits" % (chip, filt)) # Normalize iraf.iterstat.nsigrej = 5.0 iraf.iterstat.maxiter = 10 iraf.iterstat.verbose = globver iraf.iterstat.lower = INDEF iraf.iterstat.upper = INDEF iraf.iterstat("Flat-%s-%s.fits" % (chip, filt)) iraf.imarith("Flat-%s-%s.fits" % (chip, filt), "/", iraf.iterstat.median, "Flat-%s-%s.fits" % (chip, filt)) # Bias subtract and flat-field science images imlist = glob.glob("[0-9]*T[0-9]*%so_img_1.fits" % chip) for im in imlist: hdr = pyfits.getheader(im) if (hdr["FILTER"] == filt) and (not os.path.exists("fb%s" % im)): hdr = pyfits.getheader(im) # Subtract bias frame ccdproc = iraf.ccdred.ccdproc ccdproc.ccdtype = "" ccdproc.noproc = no ccdproc.fixpix = no ccdproc.overscan = no ccdproc.trim = no ccdproc.zerocor = yes ccdproc.darkcor = no ccdproc.flatcor = yes ccdproc.illumcor = no ccdproc.fringecor = no ccdproc.readcor = no ccdproc.scancor = no ccdproc.readaxis = 'line' ccdproc.fixfile = "" ccdproc.zero = "Bias-%s.fits" % chip ccdproc.flat = "Flat-%s-%s.fits" % (chip, filt) ccdproc.interactive = no ccdproc.function = "legendre" ccdproc.order = 1 ccdproc.sample = "*" ccdproc.naverage = 1 ccdproc.niterate = 1 ccdproc.low_reject = 3.0 ccdproc.high_reject = 3.0 ccdproc.grow = 0 ccdproc(images=im, output="fb%s" % im) # Create sky frame (including dark current!) if not os.path.exists("Sky-%s-%s.fits" % (chip, filt)): imlist = glob.glob("fb[0-9]*T[0-9]*%so_img_1.fits" % chip) hdr = pyfits.getheader(imlist[0]) slis = [] for im in imlist: if (hdr["FILTER"] == filt): slis.append(im) # Set up combine imcombine = iraf.immatch.imcombine imcombine.headers = "" imcombine.bpmasks = "" imcombine.rejmasks = "" imcombine.nrejmasks = "" imcombine.expmasks = "" imcombine.sigmas = "" imcombine.combine = "median" imcombine.reject = "avsigclip" imcombine.project = no imcombine.outtype = "real" imcombine.offsets = "none" imcombine.masktype = "none" imcombine.scale = "median" imcombine.zero = "none" imcombine.weight = "none" imcombine.statsec = "" imcombine.lsigma = 3.0 imcombine.hsigma = 3.0 imcombine.rdnoise = 0.0 imcombine.gain = hdr["SOFTGAIN"] sstr = ",".join(slis[:10]) imcombine(sstr, "Sky-%s-%s.fits" % (chip, filt)) # Normalize iraf.iterstat("Sky-%s-%s.fits" % (chip, filt)) iraf.imarith("Sky-%s-%s.fits" % (chip, filt), "/", iraf.iterstat.median, "Sky-%s-%s.fits" % (chip, filt)) # Subtract sky frame (and dark!) imlist = glob.glob("fb[0-9]*T[0-9]*%so_img_1.fits" % chip) for im in imlist: if not os.path.exists("s%s" % im): iraf.iterstat(im) iraf.imarith("Sky-%s-%s.fits" % (chip, filt), "*", iraf.iterstat.median, "temp.fits") iraf.imarith(im, "-", "temp.fits", "s%s" % im) os.remove("temp.fits") fimg = pyfits.open("s%s" % im, "update") fimg[0].header["SKYMED"] = iraf.iterstat.median fimg[0].header["SKYSIG"] = iraf.iterstat.sigma fimg[0].header["SKYSUB"] = 1 fimg.flush() # Remove cosmic rays imlist = glob.glob("sfb[0-9]*T[0-9]*%so_img_1.fits" % chip) for im in imlist: if not os.path.exists("c%s" % im): fimg = pyfits.open(im) iraf.lacos_im(im, "c%s" % im, "c%s.mask.fits" % im[:-5], gain=hdr['SOFTGAIN'], readn=0.0, statsec="*,*", skyval=fimg[0].header["SKYMED"], sigclip=4.5, sigfrac=0.5, objlim=1.0, niter=3, verbose=no) # Add WCS keywords clis = glob.glob("csfb[0-9]*T[0-9]*%so_img_1.fits" % chip) for im in clis: fimg = pyfits.open(im, mode='update') if fimg[0].header.get("RA") == None: ra0 = fimg[0].header["STRCURA"] dec0 = fimg[0].header["STRCUDE"] nax = [fimg[0].header["NAXIS1"], fimg[0].header["NAXIS2"]] fimg[0].header["RA"] = ra0 fimg[0].header["DEC"] = dec0 fimg[0].header["PIXSCALE"] = RATIRPIXSCALE[chip] fimg[0].header["PIXSCAL1"] = RATIRPIXSCALE[chip] fimg[0].header["PIXSCAL2"] = RATIRPIXSCALE[chip] fimg[0].header["CTYPE1"] = "RA---TAN" fimg[0].header["CTYPE2"] = "DEC--TAN" fimg[0].header["WCSDIM"] = 2 fimg[0].header["WAT0_001"] = "system=image" fimg[0].header["WAT1_001"] = "wtype=tan axtype=ra" fimg[0].header["WAT2_001"] = "wtype=tan axtype=dec" fimg[0].header["LTM1_1"] = 1.0 fimg[0].header["LTM2_2"] = 1.0 fimg[0].header["CRPIX1"] = nax[0] / 2.0 fimg[0].header["CRPIX2"] = nax[1] / 2.0 fimg[0].header["CRVAL1"] = ra0 fimg[0].header["CRVAL2"] = dec0 fimg[0].header["CD1_1"] = -RATIRPIXSCALE[chip] / 3600.0 fimg[0].header["CD1_2"] = 0.0 fimg[0].header["CD2_1"] = 0.0 fimg[0].header["CD2_2"] = RATIRPIXSCALE[chip] / 3600.0 fimg.flush() # Crude astrometry for im in clis: if not os.path.exists("a%s" % im): os.system("python %s %s" % (PYASTROM, im)) # Refine Astrometry o1 = open("daofind.param", "w") o1.write( "NUMBER\nXWIN_IMAGE\nYWIN_IMAGE\nMAG_AUTO\nFLAGS\nA_IMAGE\nB_IMAGE\n") o1.write("ELONGATION\nFWHM_IMAGE\nXWIN_WORLD\nYWIN_WORLD\n") o1.write( "ERRAWIN_IMAGE\nERRBWIN_IMAGE\nERRTHETAWIN_IMAGE\nERRAWIN_WORLD\n") o1.write( "ERRBWIN_WORLD\nERRTHETAWIN_WORLD\nFLUX_AUTO\nFLUX_RADIUS\nFLUXERR_AUTO" ) o1.close() o2 = open("default.conv", "w") o2.write( "CONV NORM\n# 5x5 convolution mask of a gaussian PSF with FWHM = 3.0 pixels.\n0.092163 0.221178 0.296069 0.221178 0.092163\n0.221178 0.530797 0.710525 0.530797 0.221178\n0.296069 0.710525 0.951108 0.710525 0.296069\n0.221178 0.530797 0.710525 0.530797 0.221178\n0.092163 0.221178 0.296069 0.221178 0.092163" ) o2.close() alis = glob.glob("acsfb[0-9]*T[0-9]*%so_img_1.fits" % chip) for im in alis: # Detect sources os.system( "sex -CATALOG_NAME %s.cat -CATALOG_TYPE FITS_LDAC -PARAMETERS_NAME daofind.param -DETECT_THRESH 2.0 -ANALYSIS_THRESH 2.0 -GAIN_KEY SOFTGAIN -PIXEL_SCALE 0 %s" % (im[:-5], im)) # Run scamp for alignment imlist = " ".join(alis) os.system( "scamp -ASTREF_CATALOG SDSS-R7 -DISTORTDEG 1 -SOLVE_PHOTOM N -SN_THRESHOLDS 3.0,10.0 -CHECKPLOT_DEV NULL %s" % imlist.replace(".fits", ".cat")) # Update header for im in alis: os.system("missfits %s" % im) os.system("rm %s.head %s.back" % (im[:-5], im)) # Find good images for coadd slis = [] for im in alis: hdr = pyfits.getheader(im) rms1 = hdr["ASTRRMS1"] rms2 = hdr["ASTRRMS2"] if (rms1 < 1.0e-4) and (rms1 > 5.0e-6) and (rms2 < 1.0e-4) and ( rms2 > 5.0e-6): slis.append(im) # Initial (unweighted) coadd sstr = " ".join(slis) os.system("swarp -GAIN_KEYWORD SOFTGAIN %s" % sstr) # Identify sources in the coadded frame hdr = pyfits.getheader("coadd.fits") iqpkg.iqobjs("coadd.fits", 10.0, RATIRSAT[chip], skyval="0.0", pix=RATIRPIXSCALE[chip], gain=hdr["GAIN"], aperture=20.0, wtimage="coadd.weight.fits") hdr = pyfits.getheader("coadd.fits") cpsfdiam = 1.34 * float(hdr["SEEPIX"]) iqpkg.iqobjs("coadd.fits", 10.0, RATIRSAT[chip], skyval="0.0", pix=RATIRPIXSCALE[chip], gain=hdr["GAIN"], aperture=cpsfdiam, wtimage="coadd.weight.fits") refstars1 = Starlist("coadd.fits.stars") refstars1.pix2wcs("coadd.fits") truemags = np.array(refstars1.mags()) maglist = [] errlist = [] # (Relative) Zeropoints for individual images for im in slis: hdr = pyfits.getheader(im) iqpkg.iqobjs(im, 3.0, RATIRSAT[chip], skyval="!SKYMED", pix=RATIRPIXSCALE[chip], gain=hdr["SOFTGAIN"], aperture=20.0) hdr = pyfits.getheader(im) psfdiam = 1.34 * float(hdr["SEEPIX"]) iqpkg.iqobjs(im, 3.0, RATIRSAT[chip], skyval="!SKYMED", pix=RATIRPIXSCALE[chip], gain=hdr["SOFTGAIN"], aperture=psfdiam) stars = Starlist("%s.stars" % im) refstars1.wcs2pix(im) a, b = stars.match(refstars1, tol=10.0, maxnum=1000) newmags = np.zeros(len(refstars1)) newwts = np.zeros(len(refstars1)) for i in range(len(refstars1)): for j in range(len(a)): if b[j].mag == truemags[i]: newmags[i] = a[j].mag newwts[i] = 1.0 / np.power(np.maximum(a[j].magu, 0.01), 2) continue maglist.append(newmags) errlist.append(newwts) obsmags = np.array(maglist) wts = np.array(errlist) [zpts, scatt, rms] = calc_zpts(truemags, obsmags, wts, sigma=3.0) # Update headers medzp = np.median(zpts) for i in range(len(slis)): im = slis[i] fimg = pyfits.open(im, mode="update") fimg[0].header["RELZPT"] = zpts[i] fimg[0].header["RELZPTSC"] = scatt[i] fimg[0].header["RELZPRMS"] = rms[i] fimg[0].header["FLXSCALE"] = 1.0 / np.power(10, (zpts[i] - medzp) / 2.5) fimg.flush() # Final coadd os.system("swarp -GAIN_KEYWORD SOFTGAIN %s" % sstr) # Calibration iqpkg.iqobjs("coadd.fits", 10.0, RATIRSAT[chip], skyval="0.0", pix=RATIRPIXSCALE[chip], gain=hdr["GAIN"], aperture=cpsfdiam, wtimage="coadd.weight.fits") stars = Starlist("coadd.fits.stars") refstars = Starlist(reflist) refstars.wcs2pix("coadd.fits") refstars.set_mag("%sMAG" % filt.upper()) truemags = np.array(refstars.mags()) maglist = [] errlist = [] a, b = stars.match(refstars, tol=10.0, maxnum=1000) newmags = np.zeros(len(refstars)) newwts = np.zeros(len(refstars)) for i in range(len(refstars)): for j in range(len(a)): if b[j].mag == truemags[i]: newmags[i] = a[j].mag newwts[i] = 1.0 / np.power(np.maximum(a[j].magu, 0.01), 2) continue maglist.append(newmags) errlist.append(newwts) obsmags = np.array(maglist) wts = np.array(errlist) [zpts, scatt, rms] = calc_zpts(truemags, obsmags, wts, sigma=3.0) fimg = pyfits.open("coadd.fits", mode="update") fimg[0].header["ABSZPT"] = zpts[0] + 25 fimg[0].header["ABSZPTSC"] = scatt[0] fimg[0].header["ABZPRMS"] = rms[0] print "Zeropoint for coadded image: %.3f" % (25.0 + zpts[0]) print "Robust scatter for coadded image: %.3f" % scatt[0] print "RMS for coadded image: %.3f" % rms[0]
def lmi_cals( imlist, dobias=yes, dobpm=yes, doflats=yes, btype="BIAS", ftype="SKY FLAT", fkey="FILTER", ppre="p", bkey="BIASSEC", tkey="TRIMSEC", bfile="Bias.fits", bpmfile="BPM.pl", bpre="b", flatpre="Flat", clobber=globclob, verbose=globver, ): """Process bias and twilight flats, creating relevant calibration files for nightly processing of LMI data.""" images = glob.glob(imlist) blist = [] flist = [] # Preprocess all the relevant images for im in images: hdr = pyfits.getheader(im) if hdr["OBSTYPE"] == btype: blist.append("%s%s" % (ppre, im)) preproc(im, fkey=fkey, ppre=ppre, bkey=bkey, tkey=tkey, clobber=clobber, verbose=verbose) elif hdr["OBSTYPE"] == ftype: flist.append("%s%s" % (ppre, im)) preproc(im, fkey=fkey, ppre=ppre, bkey=bkey, tkey=tkey, clobber=clobber, verbose=verbose) if dobias: # Setup zerocombine zerocombine = iraf.ccdred.zerocombine zerocombine.combine = "median" zerocombine.reject = "avsigclip" zerocombine.ccdtype = "" zerocombine.process = no zerocombine.delete = no zerocombine.clobber = no zerocombine.scale = "none" zerocombine.statsec = "*" zerocombine.nlow = 0 zerocombine.nhigh = 1 zerocombine.nkeep = 1 zerocombine.mclip = yes zerocombine.lsigma = 3.0 zerocombine.hsigma = 3.0 zerocombine.rdnoise = hdr["RDNOISE"] zerocombine.gain = hdr["GAIN"] zerocombine.snoise = 0 zerocombine.pclip = -0.5 zerocombine.blank = 0.0 # Run zerocombine bstr = ",".join(blist) if os.path.exists(bfile): os.remove(bfile) zerocombine(input=bstr, output=bfile) if dobpm: # Setup imcombine imcombine = iraf.immatch.imcombine imcombine.sigmas = "bsigma.fits" imcombine.combine = "median" imcombine.reject = "none" imcombine.project = no imcombine.outtype = "real" imcombine.outlimits = "" imcombine.offsets = "none" imcombine.masktype = "none" imcombine.scale = "none" imcombine.zero = "none" imcombine.weight = "none" # Run imcombine bstr = ",".join(blist) if os.path.exists("junk.fits"): os.remove("junk.fits") if os.path.exists("bsigma.fits"): os.remove("bsigma.fits") imcombine(input=bstr, output="junk.fits") # Run ccdmask to create BPM file if os.path.exists(bpmfile): os.remove(bpmfile) iraf.ccdred.ccdmask( "bsigma.fits", bpmfile, ncmed=7, nlmed=7, ncsig=15, nlsig=15, lsigma=10, hsigma=10, ngood=1, linterp=1, cinterp=1, eqinterp=1, ) os.remove("bsigma.fits") os.remove("junk.fits") if doflats: # Subtract bias images from all flats for im in flist: lmi_debias(im, bfile=bfile) # Loop over filters for filt in LMIFILTS: # ID files in appropriate filter flis = [] for im in flist: hdr = pyfits.getheader(im) if hdr[fkey] == filt: flis.append("%s%s" % (bpre, im)) if flis == []: continue # Set up flatcombine flatcombine = iraf.ccdred.flatcombine flatcombine.combine = "median" flatcombine.reject = "avsigclip" flatcombine.ccdtype = "" flatcombine.process = no flatcombine.scale = "median" flatcombine.statsec = "" flatcombine.nlow = 1 flatcombine.nhigh = 1 flatcombine.nkeep = 1 flatcombine.mclip = yes flatcombine.lsigma = 3.0 flatcombine.hsigma = 3.0 flatcombine.rdnoise = hdr["RDNOISE"] flatcombine.gain = hdr["GAIN"] flatcombine.snoise = 0.0 flatcombine.pclip = -0.5 flatcombine.blank = 1.0 # Run flatcombine fstr = ",".join(flis) if os.path.exists("%s-%s.fits" % (flatpre, filt)): os.remove("%s-%s.fits" % (flatpre, filt)) flatcombine(input=fstr, output="%s-%s.fits" % (flatpre, filt)) # Normalize iraf.iterstat.nsigrej = 5.0 iraf.iterstat.maxiter = 10 iraf.iterstat.verbose = globver iraf.iterstat.lower = INDEF iraf.iterstat.upper = INDEF iraf.iterstat("%s-%s.fits" % (flatpre, filt), verbose=no, prin=no) iraf.imarith("%s-%s.fits" % (flatpre, filt), "/", iraf.iterstat.median, "%s-%s.fits" % (flatpre, filt)) return
def spitzer_sub(new, newunc, newcov, ref, refunc, refcov, out, stamps=None, tmass=None): # Remove nan from mosaics x = pyfits.open(new) y = np.nan_to_num(x[0].data) x[0].data = y new2 = "n%s" % new x.writeto(new2) # Find stars in new image os.system("$REDUCTION/runsex.pl %s 5.0 -weight %s" % (new2, newcov)) # Find stars in reference os.system("$REDUCTION/runsex.pl %s 5.0 -weight %s" % (ref, refcov)) # Create file for geotran stars = Starlist("%s.stars" % new2) refstars = Starlist("%s.stars" % ref) refstars.pix2wcs(ref) refstars.wcs2pix(new2) a, b = stars.match(refstars, maxnum=1000) # Create file for geotran b.pix2wcs(new2) b.wcs2pix(ref) refroot = ref.split(".")[0] outf = open("%s.match" % refroot, "w") for i in range(len(a)): outf.write("%10.3f%10.3f%10.3f%10.3f\n" % (a[i].xval, a[i].yval, b[i].xval, b[i].yval)) outf.close() # Geomap and geotran [naxis1, naxis2] = get_head(new, ["NAXIS1", "NAXIS2"]) iraf.geomap("%s.match" % refroot, "%s.db" % refroot, 1, naxis1, 1, naxis2, fitgeometry="rotate", interactive=no) iraf.geotran(ref, "t%s" % ref, "%s.db" % refroot, "%s.match" % refroot) iraf.geotran(refunc, "t%s" % refunc, "%s.db" % refroot, "%s.match" % refroot) # Get stars for PSF matching if stamps != None: psfstars = Starlist(stamps) psfstars.pix2wcs(ref) psfstars.wcs2pix(new2) outf = open("stamps.lis", "w") for star in psfstars: outf.write("%10.3f%10.3f\n" % (star.xval, star.yval)) outf.close() # Appropriate parameters iraf.iterstat(ref) update_head(ref, ["MEDSKY", "SKYSIG"], [iraf.iterstat.median, iraf.iterstat.sigma]) [refskybkg, refskysig] = get_head(ref, ["MEDSKY", "SKYSIG"]) tl = refskybkg - 10 * refskysig tu = 30000.0 iraf.iterstat(new) update_head(new, ["MEDSKY", "SKYSIG"], [iraf.iterstat.median, iraf.iterstat.sigma]) [newskybkg, newskysig] = get_head(new, ["MEDSKY", "SKYSIG"]) il = newskybkg - 10 * newskysig iu = 30000.0 # Run hotpants hpcmd = "hotpants -inim %s -tmplim t%s -outim %s -tni t%s -ini %s -nsx 3 -nsy 3 -savexy %s.xy -ko 0 -bgo 0 -oni u%s -n t -tl %.2f -tu %.2f -il %.2f -iu %.2f -r 7.5 -rss 18.0" % ( new2, ref, out, refunc, newunc, new2, out, tl, tu, il, iu) if stamps != None: hpcmd += " -ssf stamps.lis -afssc 0" os.system(hpcmd) #iraf.imarith(new2, "-", "t%s" % ref, out) # Create appropriate weight image #iraf.imexpr("sqrt(a**2 + b**2)", "u%s" % out, a=newunc, b=refunc) iraf.imarith(1, "/", "u%s" % out, "temp1.fits") iraf.imarith("temp1.fits", "/", "u%s" % out, "w%s" % out) os.remove("temp1.fits") # Pick up candidates os.system("$REDUCTION/runsex.pl %s 2.0 -weight w%s" % (out, out)) stars = Starlist("%s.stars" % out) cands = [] [nax1, nax2] = get_head(ref, ["NAXIS1", "NAXIS2"]) stars.pix2wcs(new) stars.wcs2pix(ref) refu = pyfits.open(refunc) for star in stars: if star.xval > EDGETOL and star.xval < ( nax1 - EDGETOL) and star.yval > EDGETOL and star.yval < ( nax2 - EDGETOL) and refu[0].data[ star.yval, star. xval] != 0 and star.fwhmw > 0.5 and star.fwhmw < 10.0: cands.append(star) scands = Starlist(stars=cands) # Filter out bright stars if tmass == None: scands.write("cands.reg") else: stmass = Starlist(tmass) stmass.wcs2pix(ref) s2cands = scands.nomatch(stmass) s2cands.write("cands.reg") # More comprehensive list os.system("$REDUCTION/runsex.pl %s 1.5" % out) stars = Starlist("%s.stars" % out) cands = [] [nax1, nax2] = get_head(ref, ["NAXIS1", "NAXIS2"]) stars.pix2wcs(new) stars.wcs2pix(ref) refu = pyfits.open(refunc) for star in stars: if star.xval > EDGETOL and star.xval < ( nax1 - EDGETOL) and star.yval > EDGETOL and star.yval < ( nax2 - EDGETOL) and refu[0].data[ star.yval, star. xval] != 0 and star.fwhmw > 0.5 and star.fwhmw < 10.0: cands.append(star) scands = Starlist(stars=cands) scands.write("cands_all.reg") return
def saofocus(inpat,prepfile=yes,endhow="never",endwhen="", clobber=globclob,verbose=globver): """ derive best focus from an SAOFOCUS run on P60 """ # Defaults twait=30 reduced={} bpmname="BPM.pl" saokey="IMGTYPE" saore="SAOFOCUS" fseqkey="SAOFVALS" sigma=2.0 satval=50000.0 masksfx="mask" pix=0.3787 # arcsec per pixel mindiff=2 # minimum tolerable diff b/ best and worst seeing (pix) # Parse end-condition "time" if endhow=="time": re1=re.search("^(\d+):(\d+)",endwhen) if re1: tnow=time.gmtime() # Force UT calculation w/ ignoring of DST issues reftime=time.mktime([tnow[0],tnow[1],tnow[2], int(re1.group(1)),int(re1.group(2)),0, tnow[6],tnow[7],0]) - time.timezone if reftime<time.time(): reftime+=86400 if verbose: print "Running until %s" % \ time.strftime("%d %b %H:%M",time.gmtime(reftime)) else: print "Failed to parse %s as UT time" % endwhen print "Running until stopped..." endhow="never" ################################################## # Big Loop done=no while not done: # Parse inputs allfiles=glob.glob(inpat) newfiles=[] for image in allfiles: if not reduced.has_key(image): # Interested only in SAOFOCUS images saoval=get_head(image,saokey) if re.search(saore,saoval,re.I): reduced[image]=no newfiles.append(image) else: # Skip reduced[image]=yes for image in newfiles: if verbose: print "Processing new SAOFOCUS image %s" % image # Only attempt reduction once for each image reduced[image]=yes # Demosaic ccdproc=iraf.ccdproc ccdproc.overscan=yes ccdproc.trim=yes ccdproc.zerocor=no iraf.iqmosaic(image,outpfx="",biassec="!BIASSEC", trimsec="!TRIMSEC",joinkeys="DETSEC", splitkeys="AMPSEC,CCDSEC,TRIM,OVERSCAN", clobber=yes,verbose=verbose) # Fix the WCS that IRAF has now clobbered iraf.add_wcs(image,instrument="p60new") # Get some important keywords [raoff,decoff,saonseq,saoshift] = \ get_head(image,['RAOFF','DECOFF','NUMFOC', 'SAOSHIFT']) # Type conversions saonseq=int(saonseq) saoshift=float(saoshift) # No bias-subtraction or flat-fielding # Subtract stair-step background if requested if prepfile: qtmp=iraf.mktemp("saofoc")+".fits" iraf.imcopy(image,qtmp,verbose=no) for iamp in [-1,1]: for istep in range(saonseq): qsec=iraf.mktemp("saofsc")+".fits" y1=1024+iamp*(1+istep*saoshift)+max(0,iamp) y2=y1+iamp*(saoshift-1) if istep==0: y1-=iamp elif istep==saonseq-1: y2=1+max(0,iamp)*2047 ymin=min(y1,y2) ymax=max(y1,y2) statsec='[*,%d:%d]' % (ymin,ymax) iraf.iterstat(image+statsec,nsigrej=5, maxiter=10,prin=no,verbose=no) medreg=float(iraf.iterstat.median) iraf.imarith(image+statsec,'-',medreg, qsec,verbose=no,noact=no) iraf.imcopy(qsec,qtmp+statsec,verbose=no) iraf.imdel(qsec,verify=no,go_ahead=yes) iraf.imdel(image,verify=no,go_ahead=yes) iraf.imrename(qtmp,image,verbose=no) # No renaming of the file # No Bad Pixel Masking or rudimentary WCS # Object-detection doobj=1 if check_head(image,'STARFILE'): if os.path.exists(get_head(image,'STARFILE')): doobj=0 if doobj: iraf.iqobjs(image,sigma,satval,skyval="0.0", masksfx=masksfx,wtimage="none",minlim=no, clobber=yes,verbose=verbose) # Establish our "region of interest" for pixels pixrough=800 pixminx=1024-float(decoff)/pix-pixrough/2 pixmaxx=pixminx+pixrough pixmaxy=1023 pixminy=pixmaxy-float(raoff)/pix-(1+saonseq)*saoshift-pixrough/2 # The array of focus positions (from low Y to high Y) farrkeys=[] for i in range(saonseq): farrkeys.append("SAOFOC%02d" % (i+1)) farr=get_head(image,farrkeys) # Starlist from Sextractor starfile=get_head(image,'STARFILE') stars=Starlist(starfile) stars.mag_sort() # Find some of the stars in the sequence xvals=[] yvals=[] good1=[] for star in stars: # We're going in order from bright to faint if star.xval>pixminx and star.xval<pixmaxx and \ star.yval<pixmaxy and star.yval>pixminy and \ star.elong<3: good1.append(star) xvals.append(star.xval) yvals.append(star.yval) if len(good1)>=1.2*saonseq: break # Sanity checking if len(xvals)<1: print "No stars found in search region" update_head(image,'SAOFOCUS',0, "saofocus processing failed for this image") continue # Guess for x is median of the xvals of the goodstars xctr=median(xvals,pick=1) # First guess for y is median of the goodstars that fall # within +/- (pixfine) pix of this X value pixfine=8 yval2=[] good2=[] for star in good1: if abs(star.xval-xctr)<pixfine: good2.append(star) yval2.append(star.yval) # Sanity checking if len(yval2)<1: print "No stars found in refined search region" update_head(image,'SAOFOCUS',0, "saofocus processing failed for this image") continue yctr=median(yval2,pick=1) # This will be our reference star usestar=good2[yval2.index(yctr)] [refx,refy]=[usestar.xval,usestar.yval] # Identify additional stars, working out from the # reference star usestars=[usestar] # Search increasing & decreasing in Y # (we use a bigger box in Y, and correct for the last star # position, because of potential for tracking errors) for isgn in [+1,-1]: shifty=refy for i in range(1,saonseq): shifty += isgn*saoshift tgtstars=stars.starsinbox([[refx-pixfine,refx+pixfine], [shifty-1.5*pixfine, shifty+1.5*pixfine]]) if len(tgtstars)>0: minmag=99.99 # Pick the brightest in the box for tstar in tgtstars: if tstar.mag<minmag: svstar=tstar minmag=tstar.mag usestars.append(svstar) shifty=svstar.yval else: # Quit searching when there are no stars in box break # Exclude faint stars if we have too many # (could also drop stars until we reach the number, but # then we would want to check that all the y-values are in # strict succession...) magrough=2.0 if len(usestars)>saonseq: usemags=[] for star in usestars: usemags.append(star.mag) minmag=min(usemags) use2=[] for star in usestars: if star.mag<minmag+magrough: use2.append(star) if len(use2)==saonseq: usestars=use2 # Check for the right number if len(usestars)!=saonseq: print "Failed to get right number of target stars" update_head(image,'SAOFOCUS',0, "saofocus processing failed for this image") continue # Construct the array of x- and y-values xval3=[] yval3=[] for star in usestars: xval3.append(star.xval) yval3.append(star.yval) # Choose a single x-value for the run xctr2="%.3f" % median(xval3) # Increasing y-value means increasing index in farr yval3.sort() ylis=','.join(map(str,yval3)) flis=','.join(map(str,farr)) # Run iqfocus iraf.iqfocus(image,xctr2,ylis,flis,method="acorr", boxsize=saoshift,focuskey="BESTFOC", seekey="SEEING",fseekey="SAOSEE", update=yes,clobber=yes,verbose=verbose) # Check for successful iqfocus run if not check_head(image,'BESTFOC'): print "iqfocus run failed to find good focus" update_head(image,'SAOFOCUS',0, "saofocus processing failed for this image") continue # Grab focus positions and seeing values bestfoc,saosee=get_head(image,['BESTFOC','SAOSEE']) # Some information if verbose: print "Focus settings: "+flis print "Seeing values: "+saosee print "Best focus: %.3f" % bestfoc # Check for best-focus "near edge" condition for i in range(saonseq): if ("%.3f" % bestfoc)==("%.3f" % float(farr[i])): ibest=i break if (saonseq>3 and (ibest==0 or ibest==saonseq-1)) or \ (saonseq>5 and (ibest==1 or ibest==saonseq-2)): update_head(image,'SAOEDGE',1, "Best seeing is near edge of focus run") # Check for insufficient variation across the focus run maxsee=0 minsee=0.5*saoshift saoseevals=eval(saosee) for seeval in saoseevals: minsee=min(minsee,seeval) maxsee=max(maxsee,seeval) if (maxsee-minsee) < mindiff: print "Failed to observe significant variation "+\ "across focus run" update_head(image,'SAOFOCUS',0, "saofocus processing failed for this image") continue # Convert seeing value to arcsec seepix=get_head(image,'SEEPIX') update_head(image,'BESTSEE',seepix, "Best seeing in pixels for this series") try: seeasec=float(seepix)*pix update_head(image,'SEEING',seeasec, "Best seeing in arcsec for this series") except: print "Failed to convert seeing to arcsec for %s" % image # Successful processing update_head(image,'SAOFOCUS',1, "saofocus processing succeeded for this image") ############################## # Test end conditions if endhow=="never": done=no elif endhow=="once": done=yes elif endhow=="time": if time.time()>reftime: done=yes # Wait a little while if not done: time.sleep(twait)
def fastfocus(inpat, prepfile=yes, endhow="never", endwhen="", clobber=globclob, verbose=globver): """ derive best focus from an SAOFOCUS run on P60 """ # Defaults twait = 30 reduced = {} bpmname = "BPM.pl" saokey = "IMGTYPE" saore = "SAOFOCUS" fseqkey = "SAOFVALS" sigma = 10.0 satval = 50000.0 masksfx = "mask" pix = 0.3787 # arcsec per pixel mindiff = 2 # minimum tolerable diff b/ best and worst seeing (pix) # Parse end-condition "time" if endhow == "time": re1 = re.search("^(\d+):(\d+)", endwhen) if re1: tnow = time.gmtime() # Force UT calculation w/ ignoring of DST issues reftime = time.mktime([ tnow[0], tnow[1], tnow[2], int(re1.group(1)), int(re1.group(2)), 0, tnow[6], tnow[7], 0 ]) - time.timezone if reftime < time.time(): reftime += 86400 if verbose: print "Running until %s" % \ time.strftime("%d %b %H:%M",time.gmtime(reftime)) else: print "Failed to parse %s as UT time" % endwhen print "Running until stopped..." endhow = "never" # Open fake OCS fakeocs = ocs_ooriented(TEST=1) fakeocs.read_foctable() ################################################## # Big Loop done = no while not done: # Parse inputs allfiles = glob.glob(inpat) newfiles = [] for image in allfiles: if not reduced.has_key(image): # Interested only in SAOFOCUS images saoval = get_head(image, saokey) if re.search(saore, saoval, re.I): reduced[image] = no newfiles.append(image) else: # Skip reduced[image] = yes for image in newfiles: if verbose: print "Processing new SAOFOCUS image %s" % image # Only attempt reduction once for each image reduced[image] = yes # Demosaic ccdproc = iraf.ccdproc ccdproc.overscan = yes ccdproc.trim = yes ccdproc.zerocor = no iraf.iqmosaic(image, outpfx="", biassec="!BIASSEC", trimsec="!TRIMSEC", joinkeys="DETSEC", splitkeys="AMPSEC,CCDSEC,TRIM,OVERSCAN", clobber=yes, verbose=verbose) # Fix the WCS that IRAF has now clobbered iraf.add_wcs(image, instrument="p60new") # Get some important keywords [raoff,decoff,saonseq,saoshift] = \ get_head(image,['RAOFF','DECOFF','NUMFOC', 'SAOSHIFT']) # Type conversions saonseq = int(saonseq) saoshift = float(saoshift) # No bias-subtraction or flat-fielding # Subtract stair-step background if requested if prepfile: qtmp = iraf.mktemp("saofoc") + ".fits" iraf.imcopy(image, qtmp, verbose=no) for iamp in [-1, 1]: for istep in range(saonseq): qsec = iraf.mktemp("saofsc") + ".fits" y1 = 1024 + iamp * (1 + istep * saoshift) + max( 0, iamp) y2 = y1 + iamp * (saoshift - 1) if istep == 0: y1 -= iamp elif istep == saonseq - 1: y2 = 1 + max(0, iamp) * 2047 ymin = min(y1, y2) ymax = max(y1, y2) statsec = '[*,%d:%d]' % (ymin, ymax) iraf.iterstat(image + statsec, nsigrej=5, maxiter=10, prin=no, verbose=no) medreg = float(iraf.iterstat.median) iraf.imarith(image + statsec, '-', medreg, qsec, verbose=no, noact=no) iraf.imcopy(qsec, qtmp + statsec, verbose=no) iraf.imdel(qsec, verify=no, go_ahead=yes) iraf.imdel(image, verify=no, go_ahead=yes) iraf.imrename(qtmp, image, verbose=no) # No renaming of the file # No Bad Pixel Masking or rudimentary WCS # Object-detection doobj = 1 if check_head(image, 'STARFILE'): if os.path.exists(get_head(image, 'STARFILE')): doobj = 0 if doobj: iraf.iqobjs(image, sigma, satval, skyval="0.0", masksfx=masksfx, wtimage="none", minlim=no, clobber=yes, verbose=verbose) # Establish our "region of interest" for pixels pixrough = 800 pixminx = 1024 - float(decoff) / pix - pixrough / 2 pixmaxx = pixminx + pixrough pixmaxy = 1023 pixminy = pixmaxy - float(raoff) / pix - ( 1 + saonseq) * saoshift - pixrough / 2 # The array of focus positions (from low Y to high Y) farrkeys = [] for i in range(saonseq): farrkeys.append("SAOFOC%02d" % (i + 1)) farr = get_head(image, farrkeys) # Starlist from Sextractor starfile = get_head(image, 'STARFILE') stars = Starlist(starfile) stars.mag_sort() # Find some of the stars in the sequence xvals = [] yvals = [] good1 = [] for star in stars: # We're going in order from bright to faint if star.xval>pixminx and star.xval<pixmaxx and \ star.yval<pixmaxy and star.yval>pixminy and \ star.elong<3: good1.append(star) xvals.append(star.xval) yvals.append(star.yval) if len(good1) >= 1.2 * saonseq: break # Sanity checking if len(xvals) < 1: print "No stars found in search region" update_head(image, 'SAOFOCUS', 0, "saofocus processing failed for this image") continue # Guess for x is median of the xvals of the goodstars xctr = median(xvals, pick=1) # First guess for y is median of the goodstars that fall # within +/- (pixfine) pix of this X value #pixfine=10 pixfine = 20 yval2 = [] good2 = [] for star in good1: if abs(star.xval - xctr) < pixfine: good2.append(star) yval2.append(star.yval) # Sanity checking if len(yval2) < 1: print "No stars found in refined search region" update_head(image, 'SAOFOCUS', 0, "saofocus processing failed for this image") continue yctr = median(yval2, pick=1) # This will be our reference star usestar = good2[yval2.index(yctr)] [refx, refy] = [usestar.xval, usestar.yval] # Identify additional stars, working out from the # reference star usestars = [usestar] # Search increasing & decreasing in Y # (we use a bigger box in Y, and correct for the last star # position, because of potential for tracking errors) for isgn in [+1, -1]: shifty = refy for i in range(1, saonseq): shifty += isgn * saoshift tgtstars = stars.starsinbox( [[refx - pixfine, refx + pixfine], [shifty - pixfine, shifty + pixfine]]) #[shifty-1.5*pixfine, #shifty+1.5*pixfine]]) if len(tgtstars) > 0: minmag = 99.99 # Pick the brightest in the box for tstar in tgtstars: if tstar.mag < minmag: svstar = tstar minmag = tstar.mag usestars.append(svstar) shifty = svstar.yval else: # Quit searching when there are no stars in box break # Exclude faint stars if we have too many # (could also drop stars until we reach the number, but # then we would want to check that all the y-values are in # strict succession...) magrough = 2.0 if len(usestars) > saonseq: usemags = [] for star in usestars: usemags.append(star.mag) minmag = min(usemags) use2 = [] for star in usestars: if star.mag < minmag + magrough: use2.append(star) if len(use2) == saonseq: usestars = use2 # Check for the right number if len(usestars) != saonseq: print "Failed to get right number of target stars" update_head(image, 'SAOFOCUS', 0, "saofocus processing failed for this image") continue # Construct the array of x- and y-values xval3 = [] yval3 = [] for star in usestars: xval3.append(star.xval) yval3.append(star.yval) # Choose a single x-value for the run xctr2 = "%.3f" % median(xval3) # Increasing y-value means increasing index in farr yval3.sort() ylis = ','.join(map(str, yval3)) flis = ','.join(map(str, farr)) # Run iqfocus iraf.iqfocus(image, xctr2, ylis, flis, method="acorr", boxsize=saoshift, focuskey="BESTFOC", seekey="SEEPIX", fseekey="SAOSEE", update=yes, clobber=yes, verbose=verbose) # Check for successful iqfocus run if not check_head(image, 'BESTFOC'): print "iqfocus run failed to find good focus" update_head(image, 'SAOFOCUS', 0, "saofocus processing failed for this image") continue # Grab focus positions and seeing values bestfoc, saosee = get_head(image, ['BESTFOC', 'SAOSEE']) # Some information if verbose: print "Focus settings: " + flis print "Seeing values: " + saosee print "Best focus: %.3f" % bestfoc # Check for best-focus "near edge" condition for i in range(saonseq): if ("%.3f" % bestfoc) == ("%.3f" % float(farr[i])): ibest = i break if (saonseq>3 and (ibest==0 or ibest==saonseq-1)) or \ (saonseq>5 and (ibest==1 or ibest==saonseq-2)): update_head(image, 'SAOEDGE', 1, "Best seeing is near edge of focus run") # Check for insufficient variation across the focus run maxsee = 0 minsee = 0.5 * saoshift saoseevals = eval(saosee) for seeval in saoseevals: minsee = min(minsee, seeval) maxsee = max(maxsee, seeval) if (maxsee - minsee) < mindiff: print "Failed to observe significant variation "+\ "across focus run" update_head(image, 'SAOFOCUS', 0, "saofocus processing failed for this image") continue # Convert seeing value to arcsec seepix = get_head(image, 'SEEPIX') update_head(image, 'BESTSEE', seepix, "Best seeing in pixels for this series") try: seeasec = float(seepix) * pix update_head(image, 'SEEING', seeasec, "Best seeing in arcsec for this series") except: print "Failed to convert seeing to arcsec for %s" % image # Successful processing update_head(image, 'SAOFOCUS', 1, "saofocus processing succeeded for this image") # Get other necessary keywords [filter, btubtemp, airmass] = get_head(image, \ ['FILTER','BTUBTEMP','AIRMASS']) # Update focus file fakeocs.focus = [filter, bestfoc, float(btubtemp), float(airmass)] fakeocs.write_foctable() ############################## # Test end conditions if endhow == "never": done = no elif endhow == "once": done = yes elif endhow == "time": if time.time() > reftime: done = yes # Wait a little while if not done: time.sleep(twait)
def psfphot(image, coofile, ot, wtimage="", varorder=1, clobber=globclob, verbose=globver, pixtol=3.0, maxnpsf=25): """ perform PSF-based photometry on a single target star (SN?) at RA, Dec and also on a set of comparison stars, using daophot. simultaneously perform aperture photometry on all the comparison stars (after subtracting off contributions from neighbors) to enable absolute photometry by comparison to aperture photometry of standard stars observed in other fields """ # Defaults / constants psfmult = 5.0 #standard factor (multiplied by fwhm to get psfradius) psfmultsmall = 3.0 #similar to psfmult, adjusted for nstar and substar # Necessary package iraf.imutil() iraf.digiphot() iraf.daophot() # Detect stars iqobjs("%s.sub.fits" % image[:-5], 1.5, 12000.0, wtimage=wtimage, skyval="0.0") root = image[:-5] [gain, rnoise, fwhm] = get_head(image, ["GAIN", "READN", "SEEPIX"]) fwhm = float(fwhm) rnoise = float(rnoise) iraf.iterstat(image) # Saturation level if not check_head(image, "SATURATE"): saturate = 60000.0 else: saturate = get_head(image, "SATURATE") # Update datapars and daopars iraf.datapars.fwhmpsf = fwhm iraf.datapars.sigma = iraf.iterstat.sigma iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma iraf.datapars.datamax = 0.50 * saturate iraf.datapars.readnoise = rnoise iraf.datapars.epadu = gain iraf.daopars.psfrad = psfmult * fwhm iraf.daopars.fitrad = fwhm iraf.daopars.function = "gauss,moffat15,moffat25,lorentz,penny1" iraf.daopars.varorder = varorder # Reference stars file stars = Starlist(coofile) stars.wcs2pix(image) outf = open("%s.coo.1" % image[:-5], "w") for star in stars: outf.write("%10.3f%10.3f\n" % (star.xval, star.yval)) outf.close() #Aperture photometry iraf.daophot.phot(root, 'default', 'default', apertures=fwhm, verify=no, interac=no, verbose=verbose) iraf.datapars.datamax = 0.50 * saturate iraf.pstselect(root, 'default', 'default', maxnpsf, interactive=no, verify=no, verbose=verbose) iraf.psf(root, 'default', 'default', 'default', 'default', 'default', interactive=no, showplots=no, verify=no, verbose=verbose) iraf.allstar(root, 'default', 'default', 'default', 'default', 'default', verify=no, verbose=verbose) # Prep for subtracted image iraf.iterstat("%s.sub.fits" % root) iraf.datapars.sigma = iraf.iterstat.sigma iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma iraf.datapars.datamax = saturate # Look for source at OT location substars = Starlist("%s.sub.fits.stars" % image[:-5]) otstars = Starlist(ot) otstars.wcs2pix("%s.sub.fits" % image[:-5]) smatch, omatch = substars.match(otstars, tol=pixtol, useflags=no) # Generate coo file otcoo = open("%s.sub.coo.1" % image[:-5], "w") if len(smatch) == 0: otcoo.write("%10.3f%10.3f\n" % (otstars[0].xval, otstars[0].yval)) else: otcoo.write("%10.3f%10.3f\n" % (smatch[0].xval, smatch[0].yval)) otcoo.close() iraf.daophot.phot("%s.sub.fits" % root, "%s.sub.coo.1" % image[:-5], 'default', 'default', apertures=fwhm, calgorithm="none", interac=no, verify=no, verbose=verbose) if len(smatch) == 0: print "No match in subtracted image: %s.sub.fits" % root else: iraf.allstar("%s.sub.fits" % root, 'default', "%s.psf.1.fits" % root, 'default', 'default', 'default', verify=no, verbose=no) return
def iq_ratir(chip="C1", filt="i", reflist="sdss.reg"): '''Process RATIR data for given chip and filter''' # Check and see if bias frame exists. If not, create one if not os.path.exists("Bias-%s.fits" % chip): imlist = glob.glob("[0-9]*T[0-9]*%sb.fits" % chip) hdr = pyfits.getheader(imlist[0]) # Setup zerocombine zerocombine = iraf.ccdred.zerocombine zerocombine.combine = 'median' zerocombine.reject = 'avsigclip' zerocombine.ccdtype = '' zerocombine.process = no zerocombine.delete = no zerocombine.clobber = no zerocombine.scale = 'none' zerocombine.statsec = '*' zerocombine.nlow = 0 zerocombine.nhigh = 1 zerocombine.nkeep = 1 zerocombine.mclip = yes zerocombine.lsigma = 3.0 zerocombine.hsigma = 3.0 zerocombine.rdnoise = 0.0 zerocombine.gain = hdr['SOFTGAIN'] zerocombine.snoise = 0 zerocombine.pclip = -0.5 zerocombine.blank = 0.0 # Run zerocombine bstr = ",".join(imlist) zerocombine(input=bstr, output="Bias-%s.fits" % chip) # Bias subtract flat frames imlist = glob.glob("[0-9]*T[0-9]*%sf.fits" % chip) flis = [] for im in imlist: hdr = pyfits.getheader(im) if (hdr["FILTER"] == filt) and (not os.path.exists("b%s" % im)): # Subtract bias frame ccdproc = iraf.ccdred.ccdproc ccdproc.ccdtype = "" ccdproc.noproc = no ccdproc.fixpix = no ccdproc.overscan = no ccdproc.trim = no ccdproc.zerocor = yes ccdproc.darkcor = no ccdproc.flatcor = no ccdproc.illumcor = no ccdproc.fringecor = no ccdproc.readcor = no ccdproc.scancor = no ccdproc.readaxis = 'line' ccdproc.fixfile = "" ccdproc.zero = "Bias-%s.fits" % chip ccdproc.interactive = no ccdproc.function = "legendre" ccdproc.order = 1 ccdproc.sample = "*" ccdproc.naverage = 1 ccdproc.niterate = 1 ccdproc.low_reject = 3.0 ccdproc.high_reject = 3.0 ccdproc.grow = 0 ccdproc(images=im, output="b%s" % im) flis.append("b%s" % im) elif (hdr["FILTER"] == filt): flis.append("b%s" % im) # Create flat field if not os.path.exists("Flat-%s-%s.fits" % (chip, filt)): glis = [] for im in flis: iraf.iterstat.nsigrej = 5.0 iraf.iterstat.maxiter = 10 iraf.iterstat.verbose = globver iraf.iterstat.lower = INDEF iraf.iterstat.upper = INDEF iraf.iterstat(im) if (iraf.iterstat.median < RATIRSAT[chip]) and (iraf.iterstat.median > 1000.0): glis.append(im) # Set up flatcombine flatcombine = iraf.ccdred.flatcombine flatcombine.combine = 'median' flatcombine.reject = 'avsigclip' flatcombine.ccdtype = '' flatcombine.scale = 'median' flatcombine.statsec = '' flatcombine.nlow = 1 flatcombine.nhigh = 1 flatcombine.nkeep = 1 flatcombine.mclip = yes flatcombine.lsigma = 3.0 flatcombine.hsigma = 3.0 flatcombine.rdnoise = 0.0 flatcombine.gain = hdr["SOFTGAIN"] flatcombine.snoise = 0.0 flatcombine.pclip = -0.5 # Run flatcombine fstr = ",".join(glis[:10]) flatcombine(fstr, output="Flat-%s-%s.fits" % (chip, filt)) # Normalize iraf.iterstat.nsigrej = 5.0 iraf.iterstat.maxiter = 10 iraf.iterstat.verbose = globver iraf.iterstat.lower = INDEF iraf.iterstat.upper = INDEF iraf.iterstat("Flat-%s-%s.fits" % (chip, filt)) iraf.imarith("Flat-%s-%s.fits" % (chip, filt), "/", iraf.iterstat.median, "Flat-%s-%s.fits" % (chip, filt)) # Bias subtract and flat-field science images imlist = glob.glob("[0-9]*T[0-9]*%so_img_1.fits" % chip) for im in imlist: hdr = pyfits.getheader(im) if (hdr["FILTER"] == filt) and (not os.path.exists("fb%s" % im)): hdr = pyfits.getheader(im) # Subtract bias frame ccdproc = iraf.ccdred.ccdproc ccdproc.ccdtype = "" ccdproc.noproc = no ccdproc.fixpix = no ccdproc.overscan = no ccdproc.trim = no ccdproc.zerocor = yes ccdproc.darkcor = no ccdproc.flatcor = yes ccdproc.illumcor = no ccdproc.fringecor = no ccdproc.readcor = no ccdproc.scancor = no ccdproc.readaxis = 'line' ccdproc.fixfile = "" ccdproc.zero = "Bias-%s.fits" % chip ccdproc.flat = "Flat-%s-%s.fits" % (chip, filt) ccdproc.interactive = no ccdproc.function = "legendre" ccdproc.order = 1 ccdproc.sample = "*" ccdproc.naverage = 1 ccdproc.niterate = 1 ccdproc.low_reject = 3.0 ccdproc.high_reject = 3.0 ccdproc.grow = 0 ccdproc(images=im, output="fb%s" % im) # Create sky frame (including dark current!) if not os.path.exists("Sky-%s-%s.fits" % (chip, filt)): imlist = glob.glob("fb[0-9]*T[0-9]*%so_img_1.fits" % chip) hdr = pyfits.getheader(imlist[0]) slis = [] for im in imlist: if (hdr["FILTER"] == filt): slis.append(im) # Set up combine imcombine = iraf.immatch.imcombine imcombine.headers = "" imcombine.bpmasks = "" imcombine.rejmasks = "" imcombine.nrejmasks = "" imcombine.expmasks = "" imcombine.sigmas = "" imcombine.combine = "median" imcombine.reject = "avsigclip" imcombine.project = no imcombine.outtype = "real" imcombine.offsets = "none" imcombine.masktype = "none" imcombine.scale = "median" imcombine.zero = "none" imcombine.weight = "none" imcombine.statsec = "" imcombine.lsigma = 3.0 imcombine.hsigma = 3.0 imcombine.rdnoise = 0.0 imcombine.gain = hdr["SOFTGAIN"] sstr = ",".join(slis[:10]) imcombine(sstr, "Sky-%s-%s.fits" % (chip, filt)) # Normalize iraf.iterstat("Sky-%s-%s.fits" % (chip, filt)) iraf.imarith("Sky-%s-%s.fits" % (chip, filt), "/", iraf.iterstat.median, "Sky-%s-%s.fits" % (chip, filt)) # Subtract sky frame (and dark!) imlist = glob.glob("fb[0-9]*T[0-9]*%so_img_1.fits" % chip) for im in imlist: if not os.path.exists("s%s" % im): iraf.iterstat(im) iraf.imarith("Sky-%s-%s.fits" % (chip, filt), "*", iraf.iterstat.median, "temp.fits") iraf.imarith(im, "-", "temp.fits", "s%s" % im) os.remove("temp.fits") fimg = pyfits.open("s%s" % im, "update") fimg[0].header["SKYMED"] = iraf.iterstat.median fimg[0].header["SKYSIG"] = iraf.iterstat.sigma fimg[0].header["SKYSUB"] = 1 fimg.flush() # Remove cosmic rays imlist = glob.glob("sfb[0-9]*T[0-9]*%so_img_1.fits" % chip) for im in imlist: if not os.path.exists("c%s" % im): fimg = pyfits.open(im) iraf.lacos_im(im, "c%s" % im, "c%s.mask.fits" % im[:-5], gain=hdr['SOFTGAIN'], readn=0.0, statsec="*,*", skyval=fimg[0].header["SKYMED"], sigclip=4.5, sigfrac=0.5, objlim=1.0, niter=3, verbose=no) # Add WCS keywords clis = glob.glob("csfb[0-9]*T[0-9]*%so_img_1.fits" % chip) for im in clis: fimg = pyfits.open(im, mode='update') if fimg[0].header.get("RA")==None: ra0 = fimg[0].header["STRCURA"]; dec0 = fimg[0].header["STRCUDE"] nax = [fimg[0].header["NAXIS1"], fimg[0].header["NAXIS2"]] fimg[0].header["RA"] = ra0 fimg[0].header["DEC"] = dec0 fimg[0].header["PIXSCALE"] = RATIRPIXSCALE[chip] fimg[0].header["PIXSCAL1"] = RATIRPIXSCALE[chip] fimg[0].header["PIXSCAL2"] = RATIRPIXSCALE[chip] fimg[0].header["CTYPE1"] = "RA---TAN" fimg[0].header["CTYPE2"] = "DEC--TAN" fimg[0].header["WCSDIM"] = 2 fimg[0].header["WAT0_001"] = "system=image" fimg[0].header["WAT1_001"] = "wtype=tan axtype=ra" fimg[0].header["WAT2_001"] = "wtype=tan axtype=dec" fimg[0].header["LTM1_1"] = 1.0 fimg[0].header["LTM2_2"] = 1.0 fimg[0].header["CRPIX1"] = nax[0] / 2.0 fimg[0].header["CRPIX2"] = nax[1] / 2.0 fimg[0].header["CRVAL1"] = ra0 fimg[0].header["CRVAL2"] = dec0 fimg[0].header["CD1_1"] = -RATIRPIXSCALE[chip] / 3600.0 fimg[0].header["CD1_2"] = 0.0 fimg[0].header["CD2_1"] = 0.0 fimg[0].header["CD2_2"] = RATIRPIXSCALE[chip] / 3600.0 fimg.flush() # Crude astrometry for im in clis: if not os.path.exists("a%s" % im): os.system("python %s %s" % (PYASTROM, im)) # Refine Astrometry o1 = open("daofind.param", "w") o1.write("NUMBER\nXWIN_IMAGE\nYWIN_IMAGE\nMAG_AUTO\nFLAGS\nA_IMAGE\nB_IMAGE\n") o1.write("ELONGATION\nFWHM_IMAGE\nXWIN_WORLD\nYWIN_WORLD\n") o1.write("ERRAWIN_IMAGE\nERRBWIN_IMAGE\nERRTHETAWIN_IMAGE\nERRAWIN_WORLD\n") o1.write("ERRBWIN_WORLD\nERRTHETAWIN_WORLD\nFLUX_AUTO\nFLUX_RADIUS\nFLUXERR_AUTO") o1.close() o2 = open("default.conv", "w") o2.write("CONV NORM\n# 5x5 convolution mask of a gaussian PSF with FWHM = 3.0 pixels.\n0.092163 0.221178 0.296069 0.221178 0.092163\n0.221178 0.530797 0.710525 0.530797 0.221178\n0.296069 0.710525 0.951108 0.710525 0.296069\n0.221178 0.530797 0.710525 0.530797 0.221178\n0.092163 0.221178 0.296069 0.221178 0.092163") o2.close() alis = glob.glob("acsfb[0-9]*T[0-9]*%so_img_1.fits" % chip) for im in alis: # Detect sources os.system("sex -CATALOG_NAME %s.cat -CATALOG_TYPE FITS_LDAC -PARAMETERS_NAME daofind.param -DETECT_THRESH 2.0 -ANALYSIS_THRESH 2.0 -GAIN_KEY SOFTGAIN -PIXEL_SCALE 0 %s" % (im[:-5], im)) # Run scamp for alignment imlist = " ".join(alis) os.system("scamp -ASTREF_CATALOG SDSS-R7 -DISTORTDEG 1 -SOLVE_PHOTOM N -SN_THRESHOLDS 3.0,10.0 -CHECKPLOT_DEV NULL %s" % imlist.replace(".fits", ".cat")) # Update header for im in alis: os.system("missfits %s" % im) os.system("rm %s.head %s.back" % (im[:-5], im)) # Find good images for coadd slis = [] for im in alis: hdr = pyfits.getheader(im) rms1 = hdr["ASTRRMS1"]; rms2 = hdr["ASTRRMS2"] if (rms1 < 1.0e-4) and (rms1 > 5.0e-6) and (rms2 < 1.0e-4) and (rms2 > 5.0e-6): slis.append(im) # Initial (unweighted) coadd sstr = " ".join(slis) os.system("swarp -GAIN_KEYWORD SOFTGAIN %s" % sstr) # Identify sources in the coadded frame hdr = pyfits.getheader("coadd.fits") iqpkg.iqobjs("coadd.fits", 10.0, RATIRSAT[chip], skyval="0.0", pix=RATIRPIXSCALE[chip], gain=hdr["GAIN"], aperture=20.0, wtimage="coadd.weight.fits") hdr = pyfits.getheader("coadd.fits") cpsfdiam = 1.34 * float(hdr["SEEPIX"]) iqpkg.iqobjs("coadd.fits", 10.0, RATIRSAT[chip], skyval="0.0", pix=RATIRPIXSCALE[chip], gain=hdr["GAIN"], aperture=cpsfdiam, wtimage="coadd.weight.fits") refstars1 = Starlist("coadd.fits.stars") refstars1.pix2wcs("coadd.fits") truemags = np.array(refstars1.mags()) maglist = []; errlist = [] # (Relative) Zeropoints for individual images for im in slis: hdr = pyfits.getheader(im) iqpkg.iqobjs(im, 3.0, RATIRSAT[chip], skyval="!SKYMED", pix=RATIRPIXSCALE[chip], gain=hdr["SOFTGAIN"], aperture=20.0) hdr = pyfits.getheader(im) psfdiam = 1.34 * float(hdr["SEEPIX"]) iqpkg.iqobjs(im, 3.0, RATIRSAT[chip], skyval="!SKYMED", pix=RATIRPIXSCALE[chip], gain=hdr["SOFTGAIN"], aperture=psfdiam) stars = Starlist("%s.stars" % im) refstars1.wcs2pix(im) a,b = stars.match(refstars1,tol=10.0,maxnum=1000) newmags = np.zeros(len(refstars1)); newwts = np.zeros(len(refstars1)) for i in range(len(refstars1)): for j in range(len(a)): if b[j].mag == truemags[i]: newmags[i] = a[j].mag newwts[i] = 1.0 / np.power(np.maximum(a[j].magu, 0.01),2) continue maglist.append(newmags); errlist.append(newwts) obsmags = np.array(maglist) wts = np.array(errlist) [zpts, scatt, rms] = calc_zpts(truemags, obsmags, wts, sigma=3.0) # Update headers medzp = np.median(zpts) for i in range(len(slis)): im = slis[i] fimg = pyfits.open(im, mode="update") fimg[0].header["RELZPT"] = zpts[i] fimg[0].header["RELZPTSC"] = scatt[i] fimg[0].header["RELZPRMS"] = rms[i] fimg[0].header["FLXSCALE"] = 1.0 / np.power(10, (zpts[i] - medzp) / 2.5) fimg.flush() # Final coadd os.system("swarp -GAIN_KEYWORD SOFTGAIN %s" % sstr) # Calibration iqpkg.iqobjs("coadd.fits", 10.0, RATIRSAT[chip], skyval="0.0", pix=RATIRPIXSCALE[chip], gain=hdr["GAIN"], aperture=cpsfdiam, wtimage="coadd.weight.fits") stars = Starlist("coadd.fits.stars") refstars = Starlist(reflist) refstars.wcs2pix("coadd.fits") refstars.set_mag("%sMAG" % filt.upper()) truemags = np.array(refstars.mags()) maglist = []; errlist = [] a,b = stars.match(refstars, tol=10.0, maxnum=1000) newmags = np.zeros(len(refstars)); newwts = np.zeros(len(refstars)) for i in range(len(refstars)): for j in range(len(a)): if b[j].mag == truemags[i]: newmags[i] = a[j].mag newwts[i] = 1.0 / np.power(np.maximum(a[j].magu, 0.01),2) continue maglist.append(newmags); errlist.append(newwts) obsmags = np.array(maglist) wts = np.array(errlist) [zpts, scatt, rms] = calc_zpts(truemags, obsmags, wts, sigma=3.0) fimg = pyfits.open("coadd.fits", mode="update") fimg[0].header["ABSZPT"] = zpts[0] + 25 fimg[0].header["ABSZPTSC"] = scatt[0] fimg[0].header["ABZPRMS"] = rms[0] print "Zeropoint for coadded image: %.3f" % (25.0+zpts[0]) print "Robust scatter for coadded image: %.3f" % scatt[0] print "RMS for coadded image: %.3f" % rms[0]
def psfphot(image, clobber=globclob, verbose=globver, pixtol=3.0, maxnpsf=5, interact=yes): """ perform PSF-based photometry on a single target star (SN?) at RA, Dec and also on a set of comparison stars, using daophot. simultaneously perform aperture photometry on all the comparison stars (after subtracting off contributions from neighbors) to enable absolute photometry by comparison to aperture photometry of standard stars observed in other fields """ # Defaults / constants psfmult = 5.0 #standard factor (multiplied by fwhm to get psfradius) psfmultsmall = 3.0 #similar to psfmult, adjusted for nstar and substar # Necessary package iraf.imutil() # Detect stars iqpkg.iqobjs(image, 3.0, 50000.0, wtimage="", skyval="!MEDSKY") root = image[:-5] [gain, rnoise, fwhm] = get_head(image, ["GAIN", "READNOI", "SEEPIX"]) fwhm = float(fwhm) rnoise = float(rnoise) iraf.iterstat(image) # Saturation level if not check_head(image, "SATURATE"): saturate = 60000.0 else: saturate = get_head(image, "SATURATE") # Update datapars and daopars iraf.datapars.fwhmpsf = fwhm iraf.datapars.sigma = iraf.iterstat.sigma iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma iraf.datapars.datamax = 70000.0 iraf.datapars.readnoise = rnoise iraf.datapars.epadu = gain iraf.daopars.psfrad = psfmult * fwhm iraf.daopars.fitrad = fwhm iraf.daopars.function = "gauss,moffat15,moffat25,lorentz,penny1" # coo file stars = Starlist("%s.stars" % image) outf = open("%s.coo.1" % image[:-5], "w") for star in stars: outf.write("%10.3f%10.3f\n" % (star.xval, star.yval)) outf.close() #initial photometry iraf.daophot.phot(root, 'default', 'default', aperture=fwhm, verify=no, verbose=verbose) iraf.datapars.datamax = 30000.0 iraf.pstselect(root, 'default', 'default', maxnpsf, interactive=yes, verify=no, verbose=verbose) iraf.psf(root, 'default', 'default', 'default', 'default', 'default', interactive=interact, verify=no, verbose=verbose) iraf.allstar(root, 'default', 'default', 'default', 'default', 'default', verify=no, verbose=verbose) iraf.iterstat("%s.sub.fits" % root) iraf.datapars.sigma = iraf.iterstat.sigma iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma iraf.datapars.datamax = 70000.0 iraf.daophot.phot("%s.sub.fits" % root, "SN.coo", 'default', 'default', aperture=fwhm, verify=no, verbose=verbose) iraf.datapars.datamax = 30000.0 iraf.daopars.fitrad = fwhm * 2.0 iraf.allstar("%s.sub.fits" % root, 'default', "%s.psf.1.fits" % root, 'default', 'default', 'default', verify=no, verbose=no)
def psfphot(inlist, ra, dec, reffilt, interact, fwhm, readnoise, gain, threshold, refimage=None, starfile=None, maxnpsf=5, clobber=globclob, verbose=globver, skykey='SKYBKG', filtkey='FILTER', pixtol=3.0): """ perform PSF-based photometry on a single target star (SN?) at RA, Dec and also on a set of comparison stars, using daophot. simultaneously perform aperture photometry on all the comparison stars (after subtracting off contributions from neighbors) to enable absolute photometry by comparison to aperture photometry of standard stars observed in other fields """ # Defaults / constants psfmult = 5.0 #standard factor (multiplied by fwhm to get psfradius) psfmultsmall = 3.0 #similar to psfmult, adjusted for nstar and substar # Necessary package iraf.imutil() # Parse inputs infiles = iraffiles(inlist) # Which file is reffilt? call it refimage if refimage == None: for image in infiles: if check_head(image, filtkey): try: imgfilt = get_head(image, filtkey) if imgfilt == reffilt: refimage = image break except: pass if not refimage: print "BAD USER! No image corresponds to the filter: %s" % reffilt return else: refroot = 's' + refimage.split('.')[0] #first make sure to add back in background of sky iraf.iqsubsky(inlist, sub=no, skykey=skykey) #put reference image first on list infiles.remove(refimage) infiles.insert(0, refimage) #setup for keywords if gain == "!GAIN": try: gainval = float(get_head(image, gain)) except: print "Bad header keyword for gain." else: gainval = float(gain) if readnoise == "!READNOISE": try: readval = float(get_head(image, readnoise)) except: print "Bad header keyword for readnoise." else: readval = float(readnoise) # Process each file in turn for image in infiles: # Check that the image is there check_exist(image, "r") # Grab image root name root = image.split('.')[0] # Map image to reference image if not (image == refimage): [nx, ny] = get_head(image, ['NAXIS1', 'NAXIS2']) stars = Starlist(get_head(image, 'STARFILE')) refstars = Starlist(get_head(refimage, 'STARFILE')) refstars.pix2wcs(refimage) refstars.wcs2pix(image) match, refmatch = stars.match(refstars, useflags=yes, tol=10.0) nstars = len(match) if not (nstars > 2): print 'Could not find star matches between reference and %s' % image infiles.remove(image) continue refmatch.pix2wcs(image) refmatch.wcs2pix(refimage) matchfile = open('%s.match' % root, 'w') for i in range(len(match)): matchfile.write('%10.3f%10.3f%10.3f%10.3f\n' % (refmatch[i].xval, refmatch[i].yval, match[i].xval, match[i].yval)) matchfile.close() check_exist('%s.geodb' % root, 'w', clobber=clobber) iraf.geomap('%s.match' % root, '%s.geodb' % root, 1.0, nx, 1.0, ny, verbose=no, interactive=no) check_exist('s%s.fits' % root, 'w', clobber=clobber) iraf.geotran(image, 's%s' % root, '%s.geodb' % root, '%s.match' % root, geometry="geometric", boundary="constant", verbose=no) else: iraf.imcopy(image, 's%s' % root) root = 's%s' % root #get sky level and calculate sigma #if check_head(image, skykey): # try: # sky=float(get_head(image, skykey)) # except: # print "No sky levels in header." #sigma= (((sky * gainval) + readval**2)**.5) / gainval iraf.iterstat(image) # Saturation level if not check_head(image, "SATURATE"): saturate = 60000.0 else: saturate = get_head(image, "SATURATE") # Update datapars and daopars iraf.datapars.fwhmpsf = fwhm iraf.datapars.sigma = iraf.iterstat.sigma iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma iraf.datapars.datamax = 0.90 * saturate iraf.datapars.readnoise = readval iraf.datapars.epadu = gainval iraf.datapars.filter = filtkey iraf.daopars.psfrad = psfmult * fwhm iraf.daopars.fitrad = fwhm iraf.daopars.function = "gauss,moffat15,moffat25,lorentz,penny1" #find stars in image unless a starlist is given if image == refimage and starfile == None: iraf.daophot.daofind(root, 'refimage.coo.1', threshold=threshold, verify=no, verbose=verbose) elif image == refimage: shutil.copy(starfile, 'refimage.coo.1') #initial photometry iraf.daophot.phot(root, 'refimage.coo.1', 'default', aperture=fwhm, verify=no, verbose=verbose) #select stars for psf the first time refstarsfile = "refimage.pst.1" if image == refimage: iraf.pstselect(root, 'default', refstarsfile, maxnpsf, interactive=yes, verify=no, verbose=verbose) #fit the psf iraf.psf(root, 'default', refstarsfile, 'default', 'default', 'default', interactive=interact, verify=no, verbose=verbose) #identify neighboring/interfering stars to selected stars groupingfile = root + ".psg.1" iraf.nstar(root, groupingfile, 'default', 'default', 'default', psfrad=psfmultsmall * fwhm, verify=no, verbose=verbose) #subtract out neighboring stars from image iraf.substar(root, 'default', refstarsfile, 'default', 'default', psfrad=psfmultsmall * fwhm, verify=no, verbose=verbose) #repeat psf to get better psf model #IRAF's interactive version usually crashes subtractedimage = root + ".sub.1" iraf.psf(subtractedimage, root + ".nst.1", refstarsfile, '%s.psf.2' % root, '%s.pst.2' % root, '%s.psg.2' % root, interactive=interact, verify=no, verbose=verbose) #Need to make sure SN was detected by daofind stars = Starlist('%s.mag.1' % root) SN = Star(name='SN', radeg=ra, dcdeg=dec, fwhm=2.0, fwhmw=2.0) SNlis = Starlist(stars=[SN]) SNlis.wcs2pix(image) if (len(stars.match(SNlis)[0]) == 0): #No match - need to add to daofind file print "No match!" coofile = open('refimage.coo.1', 'a+') coofile.write('%10.3f%10.3f%9.3f%8.3f%13.3f%12.3f%8i\n' % (SNlis[0].xval, SNlis[0].yval, 99.999, 0.500, 0.000, 0.000, 999)) coofile.close() #repeat aperture photometry to get good comparisons to standard fields iraf.daophot.phot(root, 'refimage.coo.1', 'default', aperture=psfmult * fwhm, verify=no, verbose=verbose) # allstar run iraf.allstar(root, 'default', 'default', 'default', 'default', 'default', verify=no, verbose=verbose)
def lmi_detrend(imlist, otype="OBJECT", ppre="p", bkey="BIASSEC", tkey="TRIMSEC", bpre="b", bfile="Bias.fits", fkey="FILTER", fpre="f", flatpre="Flat", skybkg="SKYBKG", skysub="SKYSUB", skysig="SKYSIG", fpfx="F", clobber=globclob, verbose=globver): '''Identify science frames, pre-process, subtract bias, divide by flat, and correct for non-linearity.''' images = glob.glob(imlist) images.sort() # Loop through all images for im in images: hdr = pyfits.getheader(im) if hdr['OBSTYPE'] == otype: # Preprocess preproc(im, fkey=fkey, ppre=ppre, bkey=bkey, tkey=tkey, clobber=clobber, verbose=verbose) # Bias subtraction lmi_debias("%s%s" % (ppre, im), bpre=bpre, bfile=bfile) # Flat field lmi_flat("%s%s%s" % (bpre, ppre, im), fpre=fpre, fkey=fkey, flatpre="Flat") # Linearity correction lmi_lincor("%s%s%s%s" % (fpre, bpre, ppre, im)) # Write sky values to header iraf.iterstat("%s%s%s%s" % (fpre, bpre, ppre, im), verbose=no, prin=no) fimg = pyfits.open("%s%s%s%s" % (fpre, bpre, ppre, im), mode="update") fimg[0].header[skybkg] = iraf.iterstat.median fimg[0].header[skysig] = iraf.iterstat.sigma fimg[0].header[skysub] = 0 fimg.flush() fimg.close() # Final processed image shutil.copy("%s%s%s%s" % (fpre, bpre, ppre, im), "%s%s" % (fpfx, im)) return
def lmi_cals(imlist, dobias=yes, dobpm=yes, doflats=yes, btype="BIAS", ftype="SKY FLAT", fkey="FILTER", ppre="p", bkey="BIASSEC", tkey="TRIMSEC", bfile="Bias.fits", bpmfile="BPM.pl", bpre="b", flatpre="Flat", clobber=globclob, verbose=globver): '''Process bias and twilight flats, creating relevant calibration files for nightly processing of LMI data.''' images = glob.glob(imlist) blist = [] flist = [] # Preprocess all the relevant images for im in images: hdr = pyfits.getheader(im) if hdr['OBSTYPE'] == btype: blist.append("%s%s" % (ppre, im)) preproc(im, fkey=fkey, ppre=ppre, bkey=bkey, tkey=tkey, clobber=clobber, verbose=verbose) elif hdr['OBSTYPE'] == ftype: flist.append("%s%s" % (ppre, im)) preproc(im, fkey=fkey, ppre=ppre, bkey=bkey, tkey=tkey, clobber=clobber, verbose=verbose) if dobias: # Setup zerocombine zerocombine = iraf.ccdred.zerocombine zerocombine.combine = 'median' zerocombine.reject = 'avsigclip' zerocombine.ccdtype = '' zerocombine.process = no zerocombine.delete = no zerocombine.clobber = no zerocombine.scale = 'none' zerocombine.statsec = '*' zerocombine.nlow = 0 zerocombine.nhigh = 1 zerocombine.nkeep = 1 zerocombine.mclip = yes zerocombine.lsigma = 3.0 zerocombine.hsigma = 3.0 zerocombine.rdnoise = hdr['RDNOISE'] zerocombine.gain = hdr['GAIN'] zerocombine.snoise = 0 zerocombine.pclip = -0.5 zerocombine.blank = 0.0 # Run zerocombine bstr = ",".join(blist) if os.path.exists(bfile): os.remove(bfile) zerocombine(input=bstr, output=bfile) if dobpm: # Setup imcombine imcombine = iraf.immatch.imcombine imcombine.sigmas = "bsigma.fits" imcombine.combine = "median" imcombine.reject = "none" imcombine.project = no imcombine.outtype = "real" imcombine.outlimits = "" imcombine.offsets = "none" imcombine.masktype = "none" imcombine.scale = "none" imcombine.zero = "none" imcombine.weight = "none" # Run imcombine bstr = ",".join(blist) if os.path.exists("junk.fits"): os.remove("junk.fits") if os.path.exists("bsigma.fits"): os.remove("bsigma.fits") imcombine(input=bstr, output="junk.fits") # Run ccdmask to create BPM file if os.path.exists(bpmfile): os.remove(bpmfile) iraf.ccdred.ccdmask("bsigma.fits", bpmfile, ncmed=7, nlmed=7, ncsig=15, nlsig=15, lsigma=10, hsigma=10, ngood=1, linterp=1, cinterp=1, eqinterp=1) os.remove("bsigma.fits") os.remove("junk.fits") if doflats: # Subtract bias images from all flats for im in flist: lmi_debias(im, bfile=bfile) # Loop over filters for filt in LMIFILTS: # ID files in appropriate filter flis = [] for im in flist: hdr = pyfits.getheader(im) if hdr[fkey] == filt: flis.append("%s%s" % (bpre, im)) if flis == []: continue # Set up flatcombine flatcombine = iraf.ccdred.flatcombine flatcombine.combine = 'median' flatcombine.reject = 'avsigclip' flatcombine.ccdtype = '' flatcombine.process = no flatcombine.scale = 'median' flatcombine.statsec = '' flatcombine.nlow = 1 flatcombine.nhigh = 1 flatcombine.nkeep = 1 flatcombine.mclip = yes flatcombine.lsigma = 3.0 flatcombine.hsigma = 3.0 flatcombine.rdnoise = hdr["RDNOISE"] flatcombine.gain = hdr["GAIN"] flatcombine.snoise = 0.0 flatcombine.pclip = -0.5 flatcombine.blank = 1.0 # Run flatcombine fstr = ",".join(flis) if os.path.exists("%s-%s.fits" % (flatpre, filt)): os.remove("%s-%s.fits" % (flatpre, filt)) flatcombine(input=fstr, output="%s-%s.fits" % (flatpre, filt)) # Normalize iraf.iterstat.nsigrej = 5.0 iraf.iterstat.maxiter = 10 iraf.iterstat.verbose = globver iraf.iterstat.lower = INDEF iraf.iterstat.upper = INDEF iraf.iterstat("%s-%s.fits" % (flatpre, filt), verbose=no, prin=no) iraf.imarith("%s-%s.fits" % (flatpre, filt), "/", iraf.iterstat.median, "%s-%s.fits" % (flatpre, filt)) return
def psfphot(inlist, ra, dec, reffilt, interact, fwhm, readnoise, gain, threshold,refimage=None,starfile=None,maxnpsf=5, clobber=globclob,verbose=globver,skykey='SKYBKG', filtkey='FILTER',pixtol=3.0): """ perform PSF-based photometry on a single target star (SN?) at RA, Dec and also on a set of comparison stars, using daophot. simultaneously perform aperture photometry on all the comparison stars (after subtracting off contributions from neighbors) to enable absolute photometry by comparison to aperture photometry of standard stars observed in other fields """ # Defaults / constants psfmult=5.0 #standard factor (multiplied by fwhm to get psfradius) psfmultsmall=3.0 #similar to psfmult, adjusted for nstar and substar # Necessary package iraf.imutil() # Parse inputs infiles=iraffiles(inlist) # Which file is reffilt? call it refimage if refimage==None: for image in infiles: if check_head(image, filtkey): try: imgfilt = get_head(image, filtkey) if imgfilt == reffilt: refimage = image break except: pass if not refimage: print "BAD USER! No image corresponds to the filter: %s" % reffilt return else: refroot='s'+refimage.split('.')[0] #first make sure to add back in background of sky iraf.iqsubsky(inlist, sub=no, skykey=skykey) #put reference image first on list infiles.remove(refimage) infiles.insert(0,refimage) #setup for keywords if gain == "!GAIN": try: gainval = float(get_head(image, gain)) except: print "Bad header keyword for gain." else: gainval = float(gain) if readnoise == "!READNOISE": try: readval = float(get_head(image, readnoise)) except: print "Bad header keyword for readnoise." else: readval = float(readnoise) # Process each file in turn for image in infiles: # Check that the image is there check_exist(image,"r") # Grab image root name root=image.split('.')[0] # Map image to reference image if not (image==refimage): [nx,ny]=get_head(image,['NAXIS1','NAXIS2']) stars=Starlist(get_head(image,'STARFILE')) refstars=Starlist(get_head(refimage,'STARFILE')) refstars.pix2wcs(refimage) refstars.wcs2pix(image) match,refmatch=stars.match(refstars,useflags=yes,tol=10.0) nstars=len(match) if not (nstars>2): print 'Could not find star matches between reference and %s' % image infiles.remove(image) continue refmatch.pix2wcs(image) refmatch.wcs2pix(refimage) matchfile=open('%s.match' % root, 'w') for i in range(len(match)): matchfile.write('%10.3f%10.3f%10.3f%10.3f\n' % (refmatch[i].xval,refmatch[i].yval, match[i].xval,match[i].yval)) matchfile.close() check_exist('%s.geodb' % root, 'w', clobber=clobber) iraf.geomap('%s.match' % root,'%s.geodb' % root,1.0,nx,1.0,ny, verbose=no,interactive=no) check_exist('s%s.fits' % root, 'w', clobber=clobber) iraf.geotran(image,'s%s' % root,'%s.geodb' % root, '%s.match' % root,geometry="geometric", boundary="constant",verbose=no) else: iraf.imcopy(image,'s%s' % root) root='s%s' % root #get sky level and calculate sigma #if check_head(image, skykey): # try: # sky=float(get_head(image, skykey)) # except: # print "No sky levels in header." #sigma= (((sky * gainval) + readval**2)**.5) / gainval iraf.iterstat(image) # Saturation level if not check_head(image, "SATURATE"): saturate = 60000.0 else: saturate = get_head(image, "SATURATE") # Update datapars and daopars iraf.datapars.fwhmpsf=fwhm iraf.datapars.sigma=iraf.iterstat.sigma iraf.datapars.datamin=iraf.iterstat.median-10*iraf.iterstat.sigma iraf.datapars.datamax=0.90*saturate iraf.datapars.readnoise=readval iraf.datapars.epadu=gainval iraf.datapars.filter=filtkey iraf.daopars.psfrad=psfmult*fwhm iraf.daopars.fitrad=fwhm iraf.daopars.function="gauss,moffat15,moffat25,lorentz,penny1" #find stars in image unless a starlist is given if image==refimage and starfile==None: iraf.daophot.daofind(root,'refimage.coo.1',threshold=threshold,verify=no, verbose=verbose) elif image==refimage: shutil.copy(starfile,'refimage.coo.1') #initial photometry iraf.daophot.phot(root,'refimage.coo.1','default',aperture=fwhm,verify=no, verbose=verbose) #select stars for psf the first time refstarsfile = "refimage.pst.1" if image == refimage: iraf.pstselect(root,'default',refstarsfile,maxnpsf, interactive=yes,verify=no,verbose=verbose) #fit the psf iraf.psf(root,'default',refstarsfile,'default','default','default', interactive=interact,verify=no,verbose=verbose) #identify neighboring/interfering stars to selected stars groupingfile = root+".psg.1" iraf.nstar(root,groupingfile,'default','default','default', psfrad= psfmultsmall * fwhm,verify=no,verbose=verbose) #subtract out neighboring stars from image iraf.substar(root,'default',refstarsfile,'default','default', psfrad=psfmultsmall*fwhm,verify=no,verbose=verbose) #repeat psf to get better psf model #IRAF's interactive version usually crashes subtractedimage = root+".sub.1" iraf.psf(subtractedimage,root+".nst.1",refstarsfile,'%s.psf.2' % root, '%s.pst.2' % root,'%s.psg.2' % root,interactive=interact, verify=no,verbose=verbose) #Need to make sure SN was detected by daofind stars=Starlist('%s.mag.1' % root) SN=Star(name='SN',radeg=ra,dcdeg=dec,fwhm=2.0,fwhmw=2.0) SNlis=Starlist(stars=[SN]) SNlis.wcs2pix(image) if (len(stars.match(SNlis)[0])==0): #No match - need to add to daofind file print "No match!" coofile=open('refimage.coo.1', 'a+') coofile.write('%10.3f%10.3f%9.3f%8.3f%13.3f%12.3f%8i\n' % (SNlis[0].xval, SNlis[0].yval,99.999,0.500,0.000,0.000,999)) coofile.close() #repeat aperture photometry to get good comparisons to standard fields iraf.daophot.phot(root,'refimage.coo.1','default',aperture=psfmult*fwhm, verify=no,verbose=verbose) # allstar run iraf.allstar(root,'default','default','default','default','default', verify=no,verbose=verbose)
def spitzer_sub(new, newunc, newcov, ref, refunc, refcov, out, stamps=None, tmass=None): # Remove nan from mosaics x = pyfits.open(new) y = np.nan_to_num(x[0].data) x[0].data = y new2 = "n%s" % new x.writeto(new2) # Find stars in new image os.system("$REDUCTION/runsex.pl %s 5.0 -weight %s" % (new2, newcov)) # Find stars in reference os.system("$REDUCTION/runsex.pl %s 5.0 -weight %s" % (ref, refcov)) # Create file for geotran stars = Starlist("%s.stars" % new2) refstars = Starlist("%s.stars" % ref) refstars.pix2wcs(ref) refstars.wcs2pix(new2) a,b = stars.match(refstars, maxnum=1000) # Create file for geotran b.pix2wcs(new2) b.wcs2pix(ref) refroot = ref.split(".")[0] outf = open("%s.match" % refroot, "w") for i in range(len(a)): outf.write("%10.3f%10.3f%10.3f%10.3f\n" % (a[i].xval, a[i].yval, b[i].xval, b[i].yval)) outf.close() # Geomap and geotran [naxis1, naxis2] = get_head(new, ["NAXIS1", "NAXIS2"]) iraf.geomap("%s.match" % refroot, "%s.db" % refroot, 1, naxis1, 1, naxis2, fitgeometry="rotate", interactive=no) iraf.geotran(ref, "t%s" % ref, "%s.db" % refroot, "%s.match" % refroot) iraf.geotran(refunc, "t%s" % refunc, "%s.db" % refroot, "%s.match" % refroot) # Get stars for PSF matching if stamps != None: psfstars = Starlist(stamps) psfstars.pix2wcs(ref) psfstars.wcs2pix(new2) outf = open("stamps.lis", "w") for star in psfstars: outf.write("%10.3f%10.3f\n" % (star.xval, star.yval)) outf.close() # Appropriate parameters iraf.iterstat(ref) update_head(ref, ["MEDSKY", "SKYSIG"], [iraf.iterstat.median, iraf.iterstat.sigma]) [refskybkg, refskysig] = get_head(ref, ["MEDSKY", "SKYSIG"]) tl = refskybkg - 10 * refskysig; tu = 30000.0 iraf.iterstat(new) update_head(new, ["MEDSKY", "SKYSIG"], [iraf.iterstat.median, iraf.iterstat.sigma]) [newskybkg, newskysig] = get_head(new, ["MEDSKY", "SKYSIG"]) il = newskybkg - 10 * newskysig; iu = 30000.0 # Run hotpants hpcmd = "hotpants -inim %s -tmplim t%s -outim %s -tni t%s -ini %s -nsx 3 -nsy 3 -savexy %s.xy -ko 0 -bgo 0 -oni u%s -n t -tl %.2f -tu %.2f -il %.2f -iu %.2f -r 7.5 -rss 18.0" % (new2, ref, out, refunc, newunc, new2, out, tl, tu, il, iu) if stamps != None: hpcmd += " -ssf stamps.lis -afssc 0" os.system(hpcmd) #iraf.imarith(new2, "-", "t%s" % ref, out) # Create appropriate weight image #iraf.imexpr("sqrt(a**2 + b**2)", "u%s" % out, a=newunc, b=refunc) iraf.imarith(1, "/", "u%s" % out, "temp1.fits") iraf.imarith("temp1.fits", "/", "u%s" % out, "w%s" % out) os.remove("temp1.fits") # Pick up candidates os.system("$REDUCTION/runsex.pl %s 2.0 -weight w%s" % (out, out)) stars = Starlist("%s.stars" % out) cands = [] [nax1, nax2] = get_head(ref, ["NAXIS1", "NAXIS2"]) stars.pix2wcs(new) stars.wcs2pix(ref) refu = pyfits.open(refunc) for star in stars: if star.xval > EDGETOL and star.xval < (nax1 - EDGETOL) and star.yval > EDGETOL and star.yval < (nax2 - EDGETOL) and refu[0].data[star.yval,star.xval] != 0 and star.fwhmw > 0.5 and star.fwhmw < 10.0: cands.append(star) scands = Starlist(stars=cands) # Filter out bright stars if tmass==None: scands.write("cands.reg") else: stmass = Starlist(tmass) stmass.wcs2pix(ref) s2cands = scands.nomatch(stmass) s2cands.write("cands.reg") # More comprehensive list os.system("$REDUCTION/runsex.pl %s 1.5" % out) stars = Starlist("%s.stars" % out) cands = [] [nax1, nax2] = get_head(ref, ["NAXIS1", "NAXIS2"]) stars.pix2wcs(new) stars.wcs2pix(ref) refu = pyfits.open(refunc) for star in stars: if star.xval > EDGETOL and star.xval < (nax1 - EDGETOL) and star.yval > EDGETOL and star.yval < (nax2 - EDGETOL) and refu[0].data[star.yval,star.xval] != 0 and star.fwhmw > 0.5 and star.fwhmw < 10.0: cands.append(star) scands = Starlist(stars=cands) scands.write("cands_all.reg") return
def p60sdsssub(inlis, refimage, ot, distortdeg=1, scthresh1=3.0, scthresh2=10.0, tu=50000, iu=50000, ig=2.3, tg=1.0, stamps=None, nsx=4, nsy=4, ko=0, bgo=0, radius=10, tlow=None, ilow=None, sthresh=5.0, ng=None, aperture=10.0): '''P60 Subtraction using SDSS image as reference''' images = iraffiles(inlis) images.sort() # Get WCS center, pixel scale, and pixel extent of reference [n1, n2] = get_head(refimage, ['NAXIS1', 'NAXIS2']) [[ractr, dcctr]] = impix2wcs(refimage, n1 / 2.0, n2 / 2.0) if not (check_head(refimage, 'PIXSCALE')): print 'Error: Please add PIXSCALE keyword to reference image' return 0 else: pix = get_head(refimage, 'PIXSCALE') # Create stamps file if not (stamps == None): refstars = Starlist(stamps) refstars.wcs2pix(refimage) outf = open('ref.stamps', 'w') for star in refstars: outf.write('%.1f\t%.1f\n' % (star.xval, star.yval)) outf.close() # Create OT file refstars = Starlist(ot) refstars.wcs2pix(refimage) outf = open('ref.coo', 'w') outf.write('%.2f\t%.2f\n' % (refstars[0].xval, refstars[0].yval)) outf.close() # First get good distortion correction (if succifient number of images) # if len(images)>5: # p60scampall(images, 't20', 'dist', distortdeg=3, scthresh1=3.0, # scthresh2=10.0) # else: # for image in images: # iraf.imcopy(image, '%s.dist.fits' % image.split('.')[0]) for image in images: root = image.split('.')[0] # Subtract sky background if necessary [iskysub, iskybkg] = get_head(image, ['SKYSUB', 'SKYBKG']) if (iskysub == 1): iraf.imarith(image, '+', iskybkg, image) update_head(image, 'SKYSUB', 0) # First use pre-determined distortion file #shutil.copy(distfile, '%s.head' % root) #p60swarp(image, '%s.dist.fits' % root, backsub=no) # Run scamp #p60scamp('%s.dist.fits' % root, refimage=refimage, p60scamp('%s.fits' % root, refimage=refimage, distortdeg=distortdeg, scthresh1=scthresh1, scthresh2=scthresh2, rms=True, mask=True) # Run Swarp #p60swarp('%s.dist.fits' % root, '%s.shift.fits' % root, ractr=ractr, p60swarp('%s.fits' % root, '%s.shift.fits' % root, backsub=no, refimage=refimage, rms=True, mask=True) # Subtract if (ilow == None): iraf.iterstat(image) ilow = iraf.iterstat.median - 10.0 * iraf.iterstat.sigma if (tlow == None): iraf.iterstat(refimage) tlow = iraf.iterstat.median - 10.0 * iraf.iterstat.sigma tu = get_head(refimage, "SATURATE") * 0.90 iu = get_head(image, "SATURATE") * 0.90 fwhm = get_head(image, "SEEPIX") if fwhm > 15.0: radius = 15 p60hotpants('%s.shift.fits' % root, refimage, '%s.sub.fits' % root, tu=tu, iu=iu, ko=ko, bgo=bgo, nsx=nsx, nsy=nsy, radius=radius, tlow=tlow, ilow=ilow, sthresh=sthresh, ng=ng, stamps="ref.stamps", scimage=no, rms=True) # Photometer subtracted image #iraf.phot('%s.sub.fits' % root, coords='ref.coo', output='%s.mag' % #root, epadu=ig, exposure='', calgorithm='none', #salgorithm='median', annulus=30.0, dannulus=10.0, #weighting='constant', apertures=aperture, zmag=25.0, #interactive=no) print "Exiting successfully" return
def kaitphot(refimage, inlist, sncoo, aperture=10.0, dx=20, dy=20): infiles = iraffiles(inlist) # Find location of SN [[snra, sndec]] = impix2wcs(refimage, sncoo[0], sncoo[1]) sn = [Star(name="New_SN", xval=sncoo[0], yval=sncoo[1])] snstars = Starlist(stars=sn) print "Coordinates of new SN: %s %s (J2000.0)" % (snra, sndec) # Grab reference rootname refroot, null = refimage.split('.') # Big loop for image in infiles: # Image root root, null = image.split('.') # Grab zeropt and zeropt error from image header zp, zpu = get_head(image, [ZPTKEY, ZPUKEY]) # Detect objects in difference image iraf.iqobjs('%s.sub.fits' % root, SIGMA, SATVAL, skyval="0.0", zeropt=float(zp), masksfx=MASKSFX, wtimage="", fwhm=FWHM, pix=PIXSCALE, aperture=2 * FWHM / PIXSCALE, gain=FL_GAIN, minlim=no, clobber=yes, verbose=no) # See if object was detected stars = Starlist('%s.sub.fits.stars' % root) match, snmatch = stars.match(snstars, useflags=yes) # If so, use SN magnitude and statistical error if len(match): snmag = match[0].mag snerr = match[0].magu # Otherwise calculate upper limit else: statsec = '[%i:%i,%i:%i]' % ( int(sncoo[0] - dx / 2), int(sncoo[0] + dx / 2), int(sncoo[1] - dy / 2), int(sncoo[1] + dy / 2)) iraf.iterstat('%s.sub%s' % (root, statsec), nsigrej=5, maxiter=5, prin=no, verbose=no) sigma = float(iraf.iterstat.sigma) snmag = - 2.5 * log10(3 * sigma * sqrt(pi * power(aperture,2))) + \ float(zp) snerr = 99.999 # Want UT of Observation [dateobs, ut] = get_head(image, ['DATE-OBS', 'UT']) tobs = DateTime(int(dateobs[6:]), int(dateobs[3:5]), int(dateobs[:2]), int(ut[:2]), int(ut[3:5]), int(ut[6:])) # Return SN Mag / UL print '%s\t%s %.3f %10.3f%10.3f%10.3f%10.3f' % ( root, tobs.Format("%b"), (((tobs.second / 3600.0) + tobs.minute) / 60.0 + tobs.hour) / 24.0 + tobs.day, snmag, snerr, float(zp), float(zpu)) print 'Exiting Successfully' return