def _compute_err_ext(self, sci_ext): """ Compute the error image for a science image For a image in [e], the module computes the associated error image assuming a simple noise model with photon shot noise and readout noise. @param sci_ext: the input image @type sci_extsci_ext: string """ # unlearn some iraf tasks iraf.unlearn('imexpr') # get a random filename tmpfile1 = get_random_filename('t', '.fits') # compute the error array expression = "sqrt(a + b*b)" iraf.imexpr(expr=expression, output=tmpfile1, a=sci_ext, b=self.rdnoise, Stdout=1) return tmpfile1
def _make_real_sciimage(self, in_image, bck_flux, exptime): """ Create the science extension Starting from a simulated image in [e/s], the module adds background and scales to [e]. The output image is returned. @param in_image: the simulated image @type in_image: string @param bck_flux: background flux [e/s] @type bck_flux: float @param exptime: the exposure time @type exptime: float """ # get a random filename tmpfile1 = get_random_filename('t', '.fits') # add background; scale by exptime expression = "(a + b)*c" iraf.imexpr(expr=expression, output=tmpfile1, a=in_image, b=bck_flux, c=exptime, Stdout=1) # return the image name return tmpfile1
def rssflux(self, obj, sens, do_error=None): do_error = do_error if do_error is not None else self.do_error rt.rmexist([obj.flux2d]) if not rt.header(obj.bkg_var,'AIRMASS'): rt.rssairmass(obj.bkg_var) iraf.longslit.calibrate(input=obj.bkg, output=obj.flux2d, extinct='yes', flux='yes', fnu='no',sensitivity=sens, extinction=self.extinction) rt.putheader(obj.flux2d, 'STDNAME', sens) if do_error: rt.rmexist([obj.flux2d_var, obj.flux2d_err]) # Convert variance to error iraf.imexpr("sqrt(a)", obj.bkg_err, a = obj.bkg_var) iraf.longslit.calibrate(input=obj.bkg_err, output=obj.flux2d_err, extinct='yes', flux='yes',observa='saao',fnu='no', sensitivity=sens, extinction=self.extinction)
def skyest(inputimg, segimg, gsigma): #if len(glob.glob("cutout.fits")): # os.system("rm cutout.fits") #iraf.imcopy("%s[%d:%d,%d:%d]"%(inputimg,xmin,xmax,ymin,ymax),"cutout.fits") #iraf.imcopy(segimg+"[%d:%d,%d:%d]"%(xmin,xmax,ymin,ymax),"segcutout.fits") if len(glob.glob('segcutout*.fits')): os.system('rm segcutout*.fits') iraf.imexpr("a > 0 ? 100 : 0", "segcutout2.fits", a=segimg) # grow mask by Gaussian wing iraf.gauss(input="segcutout2.fits", output="segcutout3.fits", sigma=gsigma) h1 = pyfits.open(inputimg) h2 = pyfits.open('segcutout3.fits') him1 = h1[0].data # image data him2 = h2[0].data skyim = compress(him2.ravel() < 10., him1.ravel()) bkgnd = median(skyim) # take the median of unmasked sky #os.system('rm cutout.fits') os.system('rm segcutout*.fits') return bkgnd
def prepare_extraction(self): """ Prepares the aXe extraction The module does some preparatory stuff before the extraction can start. This includes copying the simulated dispersed image to AXE_IMAGE_PATH and subtracting the background on this copy. """ import shutil from pyraf import iraf # give brief feedback print('Dummy extraction on the dispersed image:') sys.stdout.flush() # get a random filenames tmpfile1 = get_random_filename('t', '.fits') tmpfile2 = get_random_filename('t', '.fits') # copy the grism image to AXE_IMAGE_PATH shutil.copy(getOUTSIM(self.simul_grisim), getIMAGE(tmpfile1)) # subtract the background from # the grism image expression = "(a - b)" iraf.imexpr(expr=expression, output=tmpfile2, a=getIMAGE(tmpfile1) + '[SCI]', b=self.bck_flux, Stdout=1) # copy the background subracted image to the grism image, sci-extension iraf.imcopy(input=tmpfile2, output=getIMAGE(tmpfile1) + '[SCI,overwrite]', Stdout=1) # delete the background subtracted # tmp image os.unlink(tmpfile2) # store the name of the background # subtracted grism image self.dispersed_image = tmpfile1
def skyest(inputimg, segimg, gsigma): """ Rough estimate of local sky background. """ if len(glob.glob('segcutout*.fits')): os.system('rm segcutout*.fits') iraf.imexpr("a > 0 ? 100 : 0", "segcutout2.fits", a=segimg) # grow mask by Gaussian wing iraf.gauss(input="segcutout2.fits", output="segcutout3.fits", sigma=gsigma) h1 = pyfits.open(inputimg) h2 = pyfits.open('segcutout3.fits') him1 = h1[0].data # image data him2 = h2[0].data skyim = np.compress(him2.ravel() < 10., him1.ravel()) bkgnd = np.median(skyim) # take the median of unmasked sky #os.system('rm cutout.fits') os.system('rm segcutout*.fits') return bkgnd
def reduce2d(self, obj, arc, do_error=None): images = obj.name do_error = do_error if do_error is not None else self.do_error hduorig= fits.open(images) os.remove(images) hduimg = fits.PrimaryHDU(data=hduorig[0].data,header=hduorig[0].header) hduimg.writeto(images) hduvarimg = fits.PrimaryHDU(data=hduorig[1].data,header=hduorig[0].header) rt.rmexist([obj.var]) hduvarimg.writeto(obj.var) rt.prepare_image(images, do_error=do_error) self.rssidentify(arc) nxpix = obj.header['NAXIS1'] rt.loadparam(self.rssconfig, ['iraf.transform', 'iraf.fit1d']) config= self.rssconfig if self.do_error: iraf.errorpars.errtype='uniform' iraf.errorpars.resample='no' iraf.samplepars.axis=1 iraf.samplepars.setParam('naverage', config['iraf.fit1d']['naverage']) iraf.samplepars.setParam('low_reject', config['iraf.fit1d']['low_reject']) iraf.samplepars.setParam('high_reject', config['iraf.fit1d']['high_reject']) iraf.samplepars.setParam('niterate', config['iraf.fit1d']['niterate']) iraf.samplepars.setParam('grow', config['iraf.fit1d']['grow']) rt.rmexist([obj.wave, obj.bkg, obj.wave_var, obj.bkg_var]) # Wavelength calibration iraf.transform(obj.noext, obj.wave, fitnames = arc.noext, logfiles = self.logfile) iraf.fit1d(obj.wave, obj.sky, "fit", axis=2) iraf.imarith(obj.wave, "-", obj.sky, obj.bkg) # Calibrated error file if do_error: iraf.transform(obj.var, obj.wave_var, fitnames = arc.noext, logfiles = self.logfile) iraf.gfit1d(obj.wave, 'tmptab.tab', function=config['iraf.fit1d']['function'], order=config['iraf.fit1d']['order'], xmin=1, xmax=nxpix, interactive='no') iraf.tabpar('tmptab.tab',"rms", 1, Stdout=self.logfile) rmsval = float(iraf.tabpar.value) tmpval=rmsval*rmsval print('RMS of the fit (for VAR plane propagation) = %s ' %rmsval) iraf.imexpr("a+b",obj.bkg_var, a=obj.wave_var, b=tmpval) print("**** Done with image %s ****" % (images))
def mkIrafIm(self): """ make the detection image. This method creates the detection Image using the CHI^2 algorithm. """ self.logfile.write('Generating detection Image...') cwd = os.getcwd() os.chdir(self.obsFits) # cd to the observations Images dir self.logfile.write('cd to observation Images dir ' + self.obsFits) for i in range(len(self.statsList)): image, background, noise = self.statsList[i] expr = "((a - %s)/%s)**2" % (background, noise) self.logfile.write( 'Generating background subtracted, squared image: ' + 'subtracted_' + image) iraf.imexpr(expr, 'subtracted_' + image, a=image) inputString = string.join( map(lambda x: 'subtracted_' + x, self.sciImageList), ',') iraf.unlearn(iraf.imsum) iraf.imsum(input=inputString, output='dummy.fits', option='sum') iraf.unlearn(iraf.imexpr) self.logfile.write('Generating detectionImage.fits for observation...') iraf.imexpr('sqrt(a)', self.detImName, a="dummy.fits") self.logfile.write('Removing dummy image...') iraf.imdelete('dummy.fits', verify='no') self.logfile.write('Removing subtraction images...') iraf.imdelete(inputString, verify='no') # the header of the image produced by iraf is junk. fix it up self._fixIrafHeader() os.chdir(cwd) # return to orig dir. return
def wirc_flatscale(infiles,inflat,outflat,clipfrac=0.03, clobber=globclob): # "infiles" is a list of filenames inlist=','.join(infiles) bpmfile=get_head(infiles[0],'BPM') # Make a median combination of input files imcmb1=iraf.mktemp("iqwircc")+".fits" iraf.imcombine(inlist,imcmb1,combine="median",reject="none", project=no,outtype="real",outlimits="",offsets="", masktype="none",blank=0.0,scale="!SKYBKG",zero="none", weight="none",statsec="",lthreshold="INDEF", hthreshold="INDEF",nkeep=1) [naxis1,naxis2]=get_head(imcmb1,['NAXIS1','NAXIS2']) npixall=float(naxis1)*float(naxis2) # Calculate sky background & divide, subtract 1 skybkg=wirc_sky(imcmb1,bpmfile) imcmb2=iraf.mktemp("iqwircc")+".fits" iraf.imarith(imcmb1,'/',skybkg,imcmb2,verbose=no,noact=no) iraf.imdel(imcmb1,verify=no,go_ahead=yes) iraf.imarith(imcmb2,'-',1.0,imcmb1,verbose=no,noact=no) # Surface fit to median image imsurf1=iraf.mktemp("iqwircs")+".fits" iraf.imsurfit(imcmb1,imsurf1,xorder=6,yorder=6,type_output="fit", function="chebyshev",cross_terms=yes, xmedian=21,ymedian=21,median_percent=50.0, lower=0.0,upper=0.0,ngrow=0,niter=0, regions="all",rows="*",columns="*",border=50, sections="",circle="",div_min="INDEF") # Corresponding bad pixel mask imbpm1=iraf.mktemp("iqwircb")+".pl" iraf.imexpr("abs(x)>%.3f ? 0 : 1" % clipfrac,imbpm1,x=imsurf1) # Subtract 1.0 from flatfield imflat1=iraf.mktemp("iqwircf")+".fits" iraf.imarith(inflat,'-',1.0,imflat1,verbose=no,noact=no) # Surface fit to the flatfield imsurf2=iraf.mktemp("iqwircs")+".fits" iraf.imsurfit(imflat1,imsurf2,xorder=6,yorder=6,type_output="fit", function="chebyshev",cross_terms=yes, xmedian=21,ymedian=21,median_percent=50.0, lower=0.0,upper=0.0,ngrow=0,niter=0, regions="all",rows="*",columns="*",border=50, sections="",circle="",div_min="INDEF") # Corresponding bad pixel mask imbpm2=iraf.mktemp("iqwircb")+".pl" iraf.imexpr("abs(x)>%.3f ? 0 : 1" % clipfrac,imbpm2,x=imsurf2) # Combine bad pixel masks for median + flat imbpm3=iraf.mktemp("iqwircb")+".pl" iraf.imexpr("(x>0 || y>0) ? 1 : 0",imbpm3,x=imbpm1,y=imbpm2) # Calculate the ratio image imratio=iraf.mktemp("iqwircr")+".fits" iraf.imexpr("z>0 ? 0 : x/y",imratio,x=imsurf1,y=imsurf2,z=imbpm3) # Mimstat on the ratio image mimstat=iraf.mimstatistics(imratio,imasks=imbpm3,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() npix=float(mimels[1]) xmult=float(mimels[2]) # Check that a reasonable number of pixels have made the grade check_exist(outflat,'w',clobber=clobber) if npix<0.05*npixall: print "Less than 5% of pixels passed the cut... preserving flatfield" iraf.imcopy(inflat,outflat,verbose=no) xmult=1.0 else: # Create the final flatfield image iraf.imexpr("x*%.3f + 1" % xmult,outflat,x=imflat1) # Update header keywords update_head(outflat,'RESCALE',1,"Flatfield has been rescaled") update_head(outflat,'ORIGFLAT',inflat, "Input flatfield name (before rescaling)") update_head(outflat,'XMULT',xmult, "Multiplied ORIGFLAT by this factor to rescale") # Clean up iraf.imdel(imcmb1,verify=no,go_ahead=yes) iraf.imdel(imcmb2,verify=no,go_ahead=yes) iraf.imdel(imsurf1,verify=no,go_ahead=yes) iraf.imdel(imsurf2,verify=no,go_ahead=yes) iraf.imdel(imbpm1,verify=no,go_ahead=yes) iraf.imdel(imbpm2,verify=no,go_ahead=yes) iraf.imdel(imbpm3,verify=no,go_ahead=yes) iraf.imdel(imflat1,verify=no,go_ahead=yes) iraf.imdel(imratio,verify=no,go_ahead=yes)
for row in chm_data: col = 0 for elem in row: if elem == 1: orig_data[lin, col] = int(999999) col = col + 1 lin = lin + 1 hd_orig[0].data = orig_data hd_orig.writeto(fich_ch) hd_orig.close() #cria mascara.pl # bad: 0 -> 1 # god: 999999 -> 0 iraf.imexpr(expr='(a == 0) ? 1 : 0', output=fich_mask, a=fich_ch) #====================================================== ## number of sigma-clip iterations iraf.samplepar.nclip = 12 ## Parametros da geometria EM IMG iraf.geompar.x0 = x0 iraf.geompar.y0 = y0 iraf.geompar.ellip0 = 0.1 iraf.geompar.sma0 = 15 ## ----- nao ajustamos em menos de 1 pixels ## iraf.geompar.minsma = 1
def ratir_doall(name, ra, dec, refdate, cat="SDSS-R9", varorder=1): """Full pipeline for image subtractions with RATIR data.""" filts = ["r", "i", "Z", "Y", "J", "H"] pixscale_dict = { 'r': 0.317, 'i': 0.317, 'Z': 0.292, 'Y': 0.292, 'J': 0.292, 'H': 0.292 } exptime_dict = { 'r': 80.0, 'i': 80.0, 'Z': 67.11, 'Y': 67.11, 'J': 67.11, 'H': 67.11 } gain_dict = { 'r': 1.23, 'i': 1.23, 'Z': 2.20, 'Y': 2.20, 'J': 2.40, 'H': 2.40 } readn_dict = { 'r': 13.6, 'i': 13.6, 'Z': 14.70, 'Y': 14.70, 'J': 11.25, 'H': 11.25 } sat_dict = { 'r': 50000.0, 'i': 50000.0, 'Z': 24000.0, 'Y': 24000.0, 'J': 24000.0, 'H': 24000.0 } dirs = glob.glob("20??????") dirs.remove(refdate) # Create directory structure for filt in filts: os.mkdir(filt) # Rename "new" files for dir in dirs: if os.path.exists("%s/stack_C0_r.fits" % dir) and os.path.exists( "%s/stack_C0_r.rms.fits" % dir): shutil.copy("%s/stack_C0_r.fits" % dir, "r/%s_r.fits" % dir) shutil.copy("%s/stack_C0_r.rms.fits" % dir, "r/%s_r.rms.fits" % dir) if os.path.exists("%s/stack_C1_i.fits" % dir) and os.path.exists( "%s/stack_C1_i.rms.fits" % dir): shutil.copy("%s/stack_C1_i.fits" % dir, "i/%s_i.fits" % dir) shutil.copy("%s/stack_C1_i.rms.fits" % dir, "i/%s_i.rms.fits" % dir) if os.path.exists("%s/stackA_C2_ZY.fits" % dir) and os.path.exists( "%s/stackA_C2_ZY.rms.fits" % dir): shutil.copy("%s/stackA_C2_ZY.fits" % dir, "Z/%s_Z.fits" % dir) shutil.copy("%s/stackA_C2_ZY.rms.fits" % dir, "Z/%s_Z.rms.fits" % dir) if os.path.exists("%s/stackB_C2_ZY.fits" % dir) and os.path.exists( "%s/stackB_C2_ZY.rms.fits" % dir): shutil.copy("%s/stackB_C2_ZY.fits" % dir, "Y/%s_Y.fits" % dir) shutil.copy("%s/stackB_C2_ZY.rms.fits" % dir, "Y/%s_Y.rms.fits" % dir) if os.path.exists("%s/stackA_C3_JH.fits" % dir) and os.path.exists( "%s/stackA_C3_JH.rms.fits" % dir): shutil.copy("%s/stackA_C3_JH.fits" % dir, "J/%s_J.fits" % dir) shutil.copy("%s/stackA_C3_JH.rms.fits" % dir, "J/%s_J.rms.fits" % dir) if os.path.exists("%s/stackB_C3_JH.fits" % dir) and os.path.exists( "%s/stackB_C3_JH.rms.fits" % dir): shutil.copy("%s/stackB_C3_JH.fits" % dir, "H/%s_H.fits" % dir) shutil.copy("%s/stackB_C3_JH.rms.fits" % dir, "H/%s_H.rms.fits" % dir) # Rename reference files shutil.copy("%s/stack_C0_r.fits" % refdate, "r/ref_r.fits") shutil.copy("%s/stack_C0_r.rms.fits" % refdate, "r/ref_r.rms.fits") shutil.copy("%s/stack_C1_i.fits" % refdate, "i/ref_i.fits") shutil.copy("%s/stack_C1_i.rms.fits" % refdate, "i/ref_i.rms.fits") shutil.copy("%s/stackA_C2_ZY.fits" % refdate, "Z/ref_Z.fits") shutil.copy("%s/stackA_C2_ZY.rms.fits" % refdate, "Z/ref_Z.rms.fits") shutil.copy("%s/stackB_C2_ZY.fits" % refdate, "Y/ref_Y.fits") shutil.copy("%s/stackB_C2_ZY.rms.fits" % refdate, "Y/ref_Y.rms.fits") shutil.copy("%s/stackA_C3_JH.fits" % refdate, "J/ref_J.fits") shutil.copy("%s/stackA_C3_JH.rms.fits" % refdate, "J/ref_J.rms.fits") shutil.copy("%s/stackB_C3_JH.fits" % refdate, "H/ref_H.fits") shutil.copy("%s/stackB_C3_JH.rms.fits" % refdate, "H/ref_H.rms.fits") # Create ds9 region file for OT location coofile = open("Coords.reg", "w") coofile.write('fk5;circle(%10.5f,%10.5f,1.0") # text={%s}' % (ra, dec, name)) coofile.close() # Get SDSS reference stars os.system("getsdss.pl -r 7.0 -f sdss.reg -p %10.5f %10.5f sdss.txt" % (ra, dec)) # Get 2MASS stars os.system( "getastrom.pl -d 2mass -r 7.0 -f temp.reg %10.5f %10.5f 2mass.txt" % (ra, dec)) tmass = Starlist("temp.reg") for star in tmass: star.mags["YMAG"] = star.mags["JMAG"] + 0.5 * ( star.mags["JMAG"] - star.mags["HMAG"]) + 0.08 tmass.write("2mass.reg") os.remove("temp.reg") # If no SDSS coverage, copy 2mass.reg to sdss.reg (kludge until PS1) if os.stat("sdss.txt")[6] == 0: shutil.copy("ps1.reg", "sdss.reg") # Loop over filters for filt in filts: iraf.chdir(filt) # Update ref header update_head("ref_%s.fits" % filt, ["PIXSCALE", "SATURATE"], [pixscale_dict[filt], sat_dict[filt]]) # Generate mask for reference iraf.imexpr("a == 0 ? 1 : 0", "ref_%s.mask.fits" % filt, "ref_%s.fits" % filt) # Generate variance for reference iraf.imcopy("ref_%s.rms.fits" % filt, "ref_%s.var.fits" % filt) iraf.imreplace("ref_%s.var.fits" % filt, 1.0e31, lower=INDEF, upper=0.0) # Detect objects in reference iqobjs("ref_%s.fits" % filt, 3.0, sat_dict[filt], wtimage="ref_%s.var.fits" % filt, skyval="0.0") # Tweak WCS in reference image p60scamp("ref_%s.fits" % filt, distortdeg=1, match=yes, rms=True, mask=True, cat=cat) os.remove("ref_%s.cat" % filt) # Get reference stars for PSF matching sdss = Starlist("../sdss.reg") ref = Starlist("ref_%s.fits.stars" % filt) sdss.wcs2pix("ref_%s.fits" % filt) a, b = sdss.match(ref, maxnum=1000) a.write("psf_%s.reg" % filt) # Loop over "new images" ims = glob.glob("????????_%s.fits" % filt) for im in ims: # Update headers nstack = int(get_head(im, "EXPTIME") / exptime_dict[filt]) update_head(im, ["GAIN", "READN", "SATURATE"], [ nstack * gain_dict[filt], readn_dict[filt] / np.sqrt(nstack), sat_dict[filt] ]) # Create variance files iraf.imcopy("%s.rms.fits" % im[:-5], "%s.var.fits" % im[:-5]) iraf.imreplace("%s.var.fits" % im[:-5], 1.0e31, lower=INDEF, upper=0.0) # Create bad pixel mask iraf.imexpr("a == 0 ? 1 : 0", "%s.mask.fits" % im[:-5], im) # Update WCS for new images p60scamp(im, distortdeg=1, match=yes, rms=True, mask=True, cat=cat) os.remove("%s.cat" % im[:-5]) # Detect objects and measure seeing iqobjs(im, 5.0, sat_dict[filt], wtimage="%s.var.fits" % im[:-5], skyval="0.0") # Match sources for PSF determination newstars = Starlist("%s.stars" % im) newstars.pix2wcs(im) newstars.wcs2pix("ref_%s.fits" % filt) c, d = newstars.match(a, maxnum=1000) d.write("psf_%s.%s.reg" % (filt, im[:-5])) # If there was a problem with alignment, will be no PSF stars if not os.stat("psf_%s.%s.reg" % (filt, im[:-5]))[6] == 0: if varorder == 0: #if (filt=="J") or (filt=="H"): p60sdsssub(im, "ref_%s.fits" % filt, "../Coords.reg", stamps="psf_%s.%s.reg" % (filt, im[:-5]), nsx=2, nsy=2, ko=0, distortdeg=1) else: p60sdsssub(im, "ref_%s.fits" % filt, "../Coords.reg", stamps="psf_%s.%s.reg" % (filt, im[:-5]), nsx=5, nsy=5, ko=1, distortdeg=1) # PSF photometry for im in ims: if os.path.exists("%s.sub.fits" % im[:-5]): psfphot(im, "psf_%s.reg" % filt, "../Coords.reg", wtimage="ref_%s.var.fits" % filt, varorder=varorder) # Photometry if (filt == "J") or (filt == "H") or (filt == "Y"): ratircal("%s.%s.dat" % (name, filt), ims, "../2mass.reg", filt) else: ratircal("%s.%s.dat" % (name, filt), ims, "../sdss.reg", filt) iraf.chdir("../") return
def p60swarp(image, outfile, ractr=None, dcctr=None, pixscale=None, size=None, backsub=no, refimage=None, rms=True, mask=True): '''Run SWarp on P60 images''' swarpcmd = 'swarp %s ' % image if ractr != None or dcctr != None: swarpcmd += '-CENTER_TYPE MANUAL -CENTER "%s, %s" ' % (ractr, dcctr) if pixscale != None: swarpcmd += '-PIXELSCALE_TYPE MANUAL -PIXEL_SCALE %f ' % pixscale if size != None: swarpcmd += '-IMAGE_SIZE "%i, %i" ' % (size[0], size[1]) if backsub == no: swarpcmd += '-SUBTRACT_BACK N ' swarpcmd += '-COPY_KEYWORDS OBJECT,SKYSUB,SKYBKG,SEEPIX ' if refimage != None: [ nax1, nax2, crval1, crval2, crpix1, crpix2, cd1_1, cd1_2, cd2_1, cd2_2 ] = get_head(refimage, [ "NAXIS1", "NAXIS2", "CRVAL1", "CRVAL2", "CRPIX1", "CRPIX2", "CD1_1", "CD1_2", "CD2_1", "CD2_2" ]) hout = open("coadd.head", "w") hout.write("CRVAL1 = %.8g\n" % crval1) hout.write("CRVAL2 = %.8g\n" % crval2) hout.write("CRPIX1 = %.8g\n" % crpix1) hout.write("CRPIX2 = %.8g\n" % crpix2) hout.write("CD1_1 = %.8g\n" % cd1_1) hout.write("CD1_2 = %.8g\n" % cd1_2) hout.write("CD2_1 = %.8g\n" % cd2_1) hout.write("CD2_2 = %.8g\n" % cd2_2) hout.write("END") hout.close() swarpcmd += '-IMAGE_SIZE "%i, %i" ' % (nax1, nax2) scmd = os.popen(swarpcmd, 'r', -1) scmd.readlines() shutil.move('coadd.fits', outfile) if rms == True: swarpcmd = 'swarp %s.rms.fits ' % image[:-5] if ractr != None or dcctr != None: swarpcmd += '-CENTER_TYPE MANUAL -CENTER "%s, %s" ' % (ractr, dcctr) if pixscale != None: swarpcmd += '-PIXELSCALE_TYPE MANUAL -PIXEL_SCALE %f ' % pixscale if size != None: swarpcmd += '-IMAGE_SIZE "%i, %i" ' % (size[0], size[1]) if backsub == no: swarpcmd += '-SUBTRACT_BACK N ' swarpcmd += '-COPY_KEYWORDS OBJECT,SKYSUB,SKYBKG,SEEPIX ' if refimage != None: [ nax1, nax2, crval1, crval2, crpix1, crpix2, cd1_1, cd1_2, cd2_1, cd2_2 ] = get_head(refimage, [ "NAXIS1", "NAXIS2", "CRVAL1", "CRVAL2", "CRPIX1", "CRPIX2", "CD1_1", "CD1_2", "CD2_1", "CD2_2" ]) hout = open("coadd.head", "w") hout.write("CRVAL1 = %.8g\n" % crval1) hout.write("CRVAL2 = %.8g\n" % crval2) hout.write("CRPIX1 = %.8g\n" % crpix1) hout.write("CRPIX2 = %.8g\n" % crpix2) hout.write("CD1_1 = %.8g\n" % cd1_1) hout.write("CD1_2 = %.8g\n" % cd1_2) hout.write("CD2_1 = %.8g\n" % cd2_1) hout.write("CD2_2 = %.8g\n" % cd2_2) hout.write("END") hout.close() swarpcmd += '-IMAGE_SIZE "%i, %i" ' % (nax1, nax2) scmd = os.popen(swarpcmd, 'r', -1) scmd.readlines() shutil.move('coadd.fits', '%s.rms.fits' % outfile[:-5]) if mask == True: swarpcmd = 'swarp %s.mask.fits ' % image[:-5] if ractr != None or dcctr != None: swarpcmd += '-CENTER_TYPE MANUAL -CENTER "%s, %s" ' % (ractr, dcctr) if pixscale != None: swarpcmd += '-PIXELSCALE_TYPE MANUAL -PIXEL_SCALE %f ' % pixscale if size != None: swarpcmd += '-IMAGE_SIZE "%i, %i" ' % (size[0], size[1]) if backsub == no: swarpcmd += '-SUBTRACT_BACK N ' swarpcmd += '-COPY_KEYWORDS OBJECT,SKYSUB,SKYBKG,SEEPIX ' if refimage != None: [ nax1, nax2, crval1, crval2, crpix1, crpix2, cd1_1, cd1_2, cd2_1, cd2_2 ] = get_head(refimage, [ "NAXIS1", "NAXIS2", "CRVAL1", "CRVAL2", "CRPIX1", "CRPIX2", "CD1_1", "CD1_2", "CD2_1", "CD2_2" ]) hout = open("coadd.head", "w") hout.write("CRVAL1 = %.8g\n" % crval1) hout.write("CRVAL2 = %.8g\n" % crval2) hout.write("CRPIX1 = %.8g\n" % crpix1) hout.write("CRPIX2 = %.8g\n" % crpix2) hout.write("CD1_1 = %.8g\n" % cd1_1) hout.write("CD1_2 = %.8g\n" % cd1_2) hout.write("CD2_1 = %.8g\n" % cd2_1) hout.write("CD2_2 = %.8g\n" % cd2_2) hout.write("END") hout.close() swarpcmd += '-IMAGE_SIZE "%i, %i" ' % (nax1, nax2) scmd = os.popen(swarpcmd, 'r', -1) scmd.readlines() iraf.imexpr("a > 0 ? 1 : 0", "%s.mask.fits" % outfile[:-5], "coadd.fits") os.remove("coadd.fits") return
def fitone(self, objectid, psffile=None, magzpt=None, exptime=1.0): """ A driver function that fits one object, given its ID. Most of the work is done here. Provide a PSF file and magnitude zero point for each object! """ if psffile == None: psffile = self.psffile if magzpt == None: magzpt = self.magzpt idcrit = (self.id_array == objectid) ra = self.ra_array[idcrit][0] dec = self.dec_array[idcrit][0] mag = self.mag_array[idcrit][0] Re = self.Re_array[idcrit][0] axratio = self.axratio_array[idcrit][0] theta = self.theta_array[idcrit][0] isoarea = self.isoarea_array[idcrit][0] # Now figure out image coordinates x = self.x_array[idcrit][0] y = self.y_array[idcrit][0] print "x, y =", x, y OUT1 = open(self.crashes, "ab") OUT2 = open(self.fitted, "ab") # now create arrays that will be included in this GALFIT run id_fit = [objectid] mag_fit = [mag] Re_fit = [Re] x_fit = [x] y_fit = [y] theta_fit = [theta] axratio_fit = [axratio] # step 1: determine temporary image size # Experimental image size majx = abs(N.sqrt(isoarea) * 4. * N.sin(theta / 180. * N.pi)) # convert degree into radian majy = abs(N.sqrt(isoarea) * 4. * N.cos(theta / 180. * N.pi)) minx = axratio * N.sqrt(isoarea) * 4. * abs( N.sin((theta + N.pi / 2.) / 180. * N.pi)) miny = axratio * N.sqrt(isoarea) * 4. * abs( N.sin((theta + N.pi / 2.) / 180. * N.pi)) #Re_max = 20. Re_max = 60 if Re > Re_max: Re_max = Re xsize = majx # determine the offset of this cutout relative to the entire mosaic if minx >= majx: xsize = minx if xsize <= 30.: xsize = 30. ysize = majy if miny >= majy: ysize = miny if ysize <= 30.: ysize = 30. xmin = int(x - xsize) if xmin <= 0: xmin = 1 xmax = int(x + xsize) ymin = int(y - ysize) if ymin <= 0: ymin = 1 ymax = int(y + ysize) # construct postage stamp name outname = self.root + '/obj%d_out.fits' % objectid # step 2: determine which objects to be included for jj in range(self.nobj_all): # iterate through other objects if self.id_array[jj] != objectid: dx = x - self.x_array[jj] dy = y - self.y_array[jj] dist = N.sqrt(dx**2 + dy**2) # Fit all objects that fall within a certain # radius of the central object: radck = N.sqrt(self.isoarea_array[jj]) angle = N.arctan2(dy, dx) phi = N.pi / 2. - (self.theta_array[jj] / 180. * N.pi + N.pi / 2. - angle) thetap = N.arctan2(self.axratio_array[jj] * N.tan(phi), 1.) isorad = radck * N.sqrt((self.axratio_array[jj] * N.cos(thetap))**2 + N.sin(thetap)**2) # If object is within the fitted image region... if (((self.x_array[jj] >= xmin) and (self.x_array[jj] <= xmax) and (self.y_array[jj] >= ymin) and (self.y_array[jj] <= ymax) and # but is greater than 2 Re away, and is not more than 3 mag fainter, or ((dist > 2 * Re and self.mag_array[jj] - 3. <= mag) or # is less than 2 Re away and not more than 5 mag fainter, or (dist < 2. * Re and self.mag_array[jj] - 5. <= mag))) or # is outside image, but is about 3 Re away and 1 mag brighter ((self.x_array[jj] >= (xmin - isorad) and (self.x_array[jj] <= (xmax + isorad) and (self.y_array[jj] >= (ymin - isorad)) and (self.y_array[jj] <= (ymax + isorad)) and (self.mag_array[jj] + 1.) <= mag)))): id_fit += [self.id_array[jj]] x_fit += [self.x_array[jj]] y_fit += [self.y_array[jj]] mag_fit += [self.mag_array[jj]] Re_fit += [self.Re_array[jj]] axratio_fit += [self.axratio_array[jj]] theta_fit += [self.theta_array[jj]] if self.Re_array[jj] > Re_max: Re_max = self.Re_array[jj] # step 3: determine fitting size & copy images # Determine image fitting size and convolution box info # into the galfit template file Refrac = 0.7 #Refrac = 2.0 if xmin >= min(x_fit) - Refrac * Re_max: xmin = min(x_fit) - Refrac * Re_max if xmin < 1: xmin = 1 if ymin >= min(y_fit) - Refrac * Re_max: ymin = min(y_fit) - Refrac * Re_max if ymin < 1: ymin = 1 if xmax <= max(x_fit) + Refrac * Re_max: xmax = max(x_fit) + Refrac * Re_max if ymax <= max(y_fit) + Refrac * Re_max: ymax = max(y_fit) + Refrac * Re_max #cbox = 60 imgxsize = xmax - xmin + 1 imgysize = ymax - ymin + 1 if os.path.exists(outname): os.remove(outname) # copy drizzled images if os.path.exists(self.root + '/obj%d_drz.fits' % objectid): os.system('rm %s/obj%d_drz.fits' % (self.root, objectid)) drzimg = self.sci_image + '[%d:%d,%d:%d]' % (xmin, xmax, ymin, ymax) iraf.imcopy(drzimg, self.root + '/obj%d_drz.fits' % objectid) h = pyfits.open(self.root + '/obj%d_drz.fits' % objectid, 'update') h[0].header.update('xmin', xmin) h[0].header.update('xmax', xmax) h[0].header.update('ymin', ymin) h[0].header.update('ymax', ymax) h[0].header.update('exptime', exptime) h.flush() h.close() # copy sigma image if os.path.exists(self.root + '/obj%d_rms.fits' % objectid): os.system('rm %s/obj%d_rms.fits' % (self.root, objectid)) iraf.imcopy( self.rms_image + '[%d:%d,%d:%d]' % (xmin, xmax, ymin, ymax), self.root + '/obj%d_rms.fits' % objectid) # copy segmentation images if os.path.exists(self.root + '/obj%d_seg.fits' % objectid): os.system('rm %s/obj%d_seg.fits' % (self.root, objectid)) iraf.imcopy( self.seg_image + '[%d:%d,%d:%d]' % (xmin, xmax, ymin, ymax), self.root + '/obj%d_seg.fits' % objectid) # make mask image -- zero-out the included components in the mask image if os.path.exists("mask2.fits"): iraf.imdelete("mask2.fits") if os.path.exists("mask3.fits"): iraf.imdelete("mask3.fits") maskexpr = "" maskname = self.root + "/obj%d_seg.fits" % objectid print "id_fit", id_fit for j in range(len(id_fit)): inid = id_fit[j] maskexpr = maskexpr + "(a==%d)" % inid if j != len(id_fit) - 1: maskexpr = maskexpr + "||" print maskexpr # Now make the masks # In GALFIT, good pixels have mask value 0, while bad pixels have mask values > 0 iraf.imexpr(maskexpr + " ? 0 : a", "mask2.fits", a=maskname) # After this step, the only non-zero pixels will be those belonging to the objects that are NOT # included in the list id_fit. # Now flatten the mask image and convolve with a Gaussian, in order to grow the masked region a little bit # to account for wings of objects. iraf.imexpr("a>0 ? 100. : 0.", "mask3.fits", a="mask2.fits") iraf.gauss(input="mask3.fits", output="mask3.fits", sigma=5) if os.path.exists(self.root + '/mask_obj%d.fits' % objectid): os.remove(self.root + '/mask_obj%d.fits' % objectid) iraf.imexpr("a<=10. ? 0. : a", self.root + "/mask_obj%d.fits" % objectid, a="mask3.fits") os.remove("mask2.fits") os.remove("mask3.fits") # step 4: write GALFIT input file parfile = "obj%d" % objectid f = open(parfile, 'w') # Write image parameters print >> f, "===============================================================================" print >> f, "# IMAGE PARAMETERS" print >> f, "A) %s/obj%d_drz.fits # Input Data image (FITS file) " % ( self.root, objectid) print >> f, "B) %s/obj%d_out.fits # Output data image block " % ( self.root, objectid) print >> f, 'C) %s/obj%d_rms.fits # Noise image name (made from data if blank or "none") ' % ( self.root, objectid) print >> f, "D) %s # Input PSF image for convolution (FITS file) " % psffile print >> f, "E) 1 # PSF oversampling factor relative to data " print >> f, "F) %s/mask_obj%d.fits # Bad pixel mask (FITS image or ASCII coord list)" % ( self.root, objectid) print >> f, "G) %s # Parameter constraint file " % self.constraint_file print >> f, "H) %d %d %d %d # Image region to fit " % (1, imgxsize, 1, imgysize) print >> f, "I) %d %d # Size of the convolution box (x y)" % ( self.cbox, self.cbox) print >> f, "J) %f # Magnitude photometric zeropoint " % magzpt print >> f, "K) %s %s # Plate scale (dx dy). " % (self.xscale, self.yscale) print >> f, "O) regular # Display type (regular, curses, both) " print >> f, "P) 0 # Create ouput only? (1=yes; 0=optimize) " print >> f, "S) 0 # Modify/create objects interactively? " print >> f, "" print >> f, "# INITIAL FITTING PARAMETERS " print >> f, "" print >> f, "# For object type, allowed functions are: sersic, nuker, " print >> f, "# expdisk, devauc, moffat, gaussian. " print >> f, "" print >> f, "# Objtype: Fit? Parameters " print >> f, "" # Write individual component for j in range(len(id_fit)): self.component(f, j + 1, x_fit[j] - xmin + 1, y_fit[j] - ymin + 1, mag_fit[j], Re_fit[j], axratio_fit[j], theta_fit[j]) # Make sky component #bkgnd = skyest('images/obj%d_drz.fits'%objectid,'images/obj%d_seg.fits'%objectid,5.) bkgnd = 0. self.sky(f, len(id_fit) + 1, bkgnd) f.close() # step 5: run GALFIT t1 = time.time() galfitrun = subprocess.Popen(["galfit", parfile], stdout=sys.stdout) # runs GALFIT # ======================================================= # # Try to time GALFIT and kill it after spending more than # 10 min on one object # ======================================================= # timelimit = 10. * 60. # time limit: 10 minutes timeint = 10. # check every 10 seconds maxcheck = int(timelimit / timeint) kill = 1 for j in range(maxcheck): time.sleep(timeint) # sleep for check time interval if galfitrun.poll() == None: # if it's still running: pass # haven't reached time limit; let it run else: kill = 0 # finished before time limit; don't need to kill break # jump out of process checking if kill: # if reached time limit and still running: os.kill(galfitrun.pid, signal.SIGKILL) # kill GALFIT print "Killed GALFIT; GALFIT froze" retcode = galfitrun.returncode t2 = time.time() try: print >> OUT1, "returned code is %d" % retcode except: print >> OUT1, "returned code is ", retcode dt = t2 - t1 if retcode != 7: # not sure about the behavior when GALFIT crashes...!!! print >> OUT1, "%d %s" % (objectid, parfile) else: print >> OUT2, "%s-wmask-out.fits %.1f seconds" % (outname, dt) OUT1.close() OUT2.close() print "Finishes GALFIT fitting"
def mkweightmap(bpm,outweight='weight.fits'): iraf.unlearn('imexpr') iraf.imexpr('a = 0 ? 1 : 0',outweight,a=bpm)