'>>> Order of function in y for bg fit ? [' + str(_ybgord0) + '] ? ') if not ybgord0: ybgord0 = _ybgord0 else: _ybgord0 = ybgord0 try: float(xbgord0) float(ybgord0) checkorder = 'no' except: print 'WARNING: value not valid !!' checimsurfitkorder = 'yes' iraf.imsurfit("original", "bg", xorder=xbgord0, yorder=ybgord0, regions="section", section="sec") else: if not _interactive: print 'select' else: agnkey.util.delete("tmplabel") agnkey.util.delete("tmptbl") ff = open('tmplabel', 'w') ff.write('') ff.close() print ">>> Mark corners of bg-region with >b<, exit >q<" iraf.tvmark(1, "tmplabel", autol='no',
def fringe_sub(inlist, inpref='', outpref='s', mskpref='none', iternum=50, nsigma=3, bpm='none', second=False, fr_x0=0.0, fr_y0=0.0, fr_step=5, surfit=False, xsorder=4, ysorder=4): # load IRAF package iraf.proto() # check input image list inimg_arr = check_input(inlist, inpref) if isinstance(inimg_arr, int): return 1 # check output outimg_arr = check_outpref(outpref, inimg_arr) if isinstance(outimg_arr, int): return 1 # check mask images if mskpref.lower() == 'none' or mskpref == '': mask = 0 else: mask = 1 mskimg_arr = check_inpref(mskpref, inimg_arr) if isinstance(mskimg_arr, int): return 1 # check bad pixel mask if bpm.lower() != 'none': bpmex = 1 if os.access(bpm, os.R_OK) == False: print >> sys.stderr, 'cannot read bad pixel mask (%s)' % bpm return 1 else: bpmex = 0 # prefix for temoprary images tmp = tempfile.NamedTemporaryFile(suffix='', prefix='', dir='/tmp') tmp_prefix = tmp.name tmp.close() # subtract sky for i in range(len(inimg_arr)): if mask == 1: iraf.unlearn('mimstat') if not second: ret = iraf.mimstat(inimg_arr[i], imasks=mskimg_arr[i]+'[pl]', format='no', fields='midpt', nclip=iternum, lsigma=nsigma, usigma=nsigma, Stdout=1) else: if bpmex == 1: tmp_mask = tmp_prefix + os.path.basename(mask_img_arr[i]) iraf.imarith(maskimg_arr[i], '+', bpm, tmp_mask, verbose='no') convert_maskfits_int(tmp_mask, tmp_mask) ret = iraf.mimstat(inimg_arr[i], imasks=tmp_mask, format='no', fields='midpt', nclip=iternum, lsigma=nsigma, usigma=nsigma, Stdout=1) else: ret = iraf.mimstat(inimg_arr[i], imasks=mskimg_arr[i], format='no', fields='midpt', nclip=iternum, lsigma=nsigma, usigma=nsigma, Stdout=1) else: iraf.unlearn('imstat') ret = iraf.imstat(inimg_arr[i], format='no', fields='midpt', nclip=iternum, lsigma=nsigma, usigma=nsigma, Stdout=1) if len(ret) == 1: objmed = float(ret[0]) else : print >> sys.stderr, 'failed to calculate median of the object background' return 1 # record input fits data into memory im = pyfits.open(inimg_arr[i]) im_hdr = im[0].header im_data = im[0].data out_data = im_data - objmed if surfit: out_data_org = np.copy(out_data) im.close() # fringe subtraction for NB imaging if mask == 1: if bpmex == 1: tmp_mask = tmp_prefix + os.path.basename(mask_img_arr[i]) mask_im = pyfits.open(tmp_mask) else: mask_im = pyfits.open(mskimg_arr[i]) if not second: mask_data = mask_im[1].data else: mask_data = mask_im[0].data mask_im.close() x = np.ones(out_data.shape[0]*out_data.shape[1]).reshape(out_data.shape[0],out_data.shape[1])* np.arange(out_data.shape[1]) + 1 y = (np.ones(out_data.shape[1]*out_data.shape[0]).reshape(out_data.shape[1],out_data.shape[0]) * np.arange(out_data.shape[0]) + 1).T r = np.sqrt((x-fr_x0)*(x-fr_x0) + (y-fr_y0)*(y-fr_y0)) rmax = np.max(r) rmin = np.min(r) r1 = rmin while r1 <= rmax: r2 = r1 + fr_step idx = np.logical_and(r>=r1, r<r2) out_subset = out_data[idx] if mask == 1: mask_subset = mask_data[idx] idx_sky = np.where(mask_subset == 0) #if len(out_subset[idx_sky]) != len(out_subset): # print "masked:",inimg_arr[i],r1,r2,fr_x0,fr_y0,len(out_subset[idx_sky]),"/",len(out_subset),meanclip(out_subset[idx_sky],median=1)[0],meanclip(out_subset, median=1)[0] med, sigma = meanclip(out_subset[idx_sky],median=1) else: med, sigma = meanclip(out_data[idx],median=1) out_data[idx] = out_data[idx] - med r1 += fr_step # save output fits file hdu = pyfits.PrimaryHDU(out_data) imgout = pyfits.HDUList([hdu]) imgout[0].header = im_hdr if surfit: tmp_fsub = tmp_prefix + "fsub" + os.path.basename(inimg_arr[i]) tmp_fsub_fit = tmp_prefix + "fsub_fit" + os.path.basename(inimg_arr[i]) imgout.writeto(tmp_fsub) imgout.close() iraf.imsurfit(tmp_fsub, tmp_fsub_fit, xsorder, ysorder) imfit = pyfits.open(tmp_fsub_fit) imfit_data = imfit[0].data out_data_fs = np.copy(out_data_org - imfit_data) r1 = rmin while r1 <= rmax: r2 = r1 + fr_step idx = np.logical_and(r>=r1, r<r2) out_subset = out_data_fs[idx] if mask == 1: mask_subset = mask_data[idx] idx_sky = np.where(mask_subset == 0) med, sigma = meanclip(out_subset[idx_sky],median=1) else: med, sigma = meanclip(out_data_fs[idx],median=1) out_data_org[idx] = out_data_org[idx] - med r1 += fr_step hdu = pyfits.PrimaryHDU(out_data_org) imgout = pyfits.HDUList([hdu]) imgout[0].header = im_hdr imgout.writeto(outimg_arr[i]) imgout.close() remove_temp_all(tmp_prefix) else: imgout.writeto(outimg_arr[i]) imgout.close() if bpmex == 1: remove_temp_all(tmp_prefix) return 0
os.system("rm -f back.fits") iraf.images() iraf.images.imfit() iraf.images.imutil() #iraf.crutil() if mode == 'MMIRS': flipme = True headers = iraf.imhead(fitsfile, long='yes', Stdout=1) for line in headers: if re.match('WAT', line): flipme = False if flipme: print "not flipped" iraf.imsurfit(fitsfile, 'back.fits', xorder=2, yorder=2, upper=2, lower=2, ngrow=15) iraf.imarith(fitsfile, '-', 'back.fits', fitsfile) iraf.imcopy(fitsfile + '[*,*]', fitsfile) # iraf.cosmicrays(fitsfile, fitsfile, interactive='no') else: print "flipped" else: iraf.imsurfit(fitsfile, 'back.fits', xorder=2, yorder=2) iraf.imarith(fitsfile, '-', 'back.fits', fitsfile) hdu = rfits(fitsfile) image = hdu.data hdr = hdu.header try:
def errore(img, imgpsf, coordlist, size, truemag, fwhm0, leng0, _show, _interactive, _numiter, z11, z22, midpt, nax, nay, xbgord0, ybgord0, _recenter, apco0, dmax=51000, dmin=-500): import lsc import os, sys, re, string from numpy import array, mean, std, compress, average from pyraf import iraf if not _numiter: _numiter = 3 dartf = 100 while dartf >= size - 1: if _interactive: artfac0 = raw_input( '>>> Dispersion of artificial star positions (in units of FWHM) [1] ' ) if not artfac0: artfac0 = 1 else: artfac0 = 1 try: artfac0 = float(artfac0) if float(artfac0) >= size - 1: print '!!! WARNING: ' + str( artfac0) + ' too large (max ' + str(size) + '- 1)' print 'try again....' else: dartf = artfac0 except: print '#### WARNING: ' + str(artfac0) + ' should be a number !!!!' print 'try again....' lsc.util.delete("tmpar?") lsc.util.delete('artskyfit.fits') os.system('cp skyfit.fits artskyfit.fits') i = 0 tmpart = [] while i <= 8: lsc.util.delete( "reserr.fit?,artbg.fit?,artstar.fit?,artres.fit?,artfit.fit?") artrad = fwhm0 / 2. #artseed = artseed+1234 artx = int(i / 3.) - 1 if i <= 2: arty = artx + i if 3 <= i <= 5: arty = artx - 1 + i - 3 if i >= 6: arty = artx - 2 + i - 6 ff = open(img + ".sn.coo", 'r') ss = ff.readline() ff.close() xbb = float(string.split(ss)[0]) ybb = float(string.split(ss)[1]) xbb = xbb + artx * fwhm0 * artfac0 ybb = ybb + arty * fwhm0 * artfac0 lsc.util.delete(coordlist) ff = open(coordlist, 'w') ff.write(str(xbb) + ' ' + str(ybb) + ' ' + str(truemag[0]) + " 1") ff.close() xb1 = int(float(xbb) - fwhm0 * float(leng0) / 2) xb2 = int(float(xbb) + fwhm0 * float(leng0) / 2) yb1 = int(float(ybb) - fwhm0 * float(leng0) / 2) yb2 = int(float(ybb) + fwhm0 * float(leng0) / 2) sec = "1 " + str(xb1) + " 1 " + str(nay) + '\n' sec = sec + str(xb2) + ' ' + str(nax) + " 1 " + str(nay) + '\n' sesc = sec + str(xb1) + ' ' + str(xb2) + " 1 " + str(yb1) + '\n' sec = sec + str(xb1) + ' ' + str(xb2) + ' ' + str(yb2) + ' ' + str( nay) + '\n' ff = open('sec', 'w') ff.write(sec) ff.close() lsc.util.delete("reserr.ar?") lsc.util.delete("artlist.ma?") lsc.util.delete("artsky.fit?") lsc.util.delete("artbg.fit?") lsc.util.delete("artbgs.fit?") lsc.util.delete("artsn.fit?") lsc.util.delete("artres.fit?") lsc.util.delete("artlist.al?") iraf.addstar("artskyfit", coordlist, imgpsf, "reserr", nstar=1, veri='no', simple='yes', verb='no') # reserr = skyfit + artificial star ######## inp = "artbg.fits[" + str(xb1) + ":" + str(xb2) + "," + str( yb1) + ":" + str(yb2) + "]" out = "artsky.fits[" + str(xb1) + ":" + str(xb2) + "," + str( yb1) + ":" + str(yb2) + "]" iraf.imsurfit("reserr", "artbg", xorder=xbgord0, yorder=ybgord0, regions="section", section="sec") midpt = np.mean(fits.getdata('artbg.fits')) iraf.imcopy('reserr.fits', 'artsky.fits') iraf.imcopy(inp, 'artbgs.fits') iraf.imcopy("artbgs.fits", out) iraf.imarith("reserr", "-", "artsky", "artsn", calctype="r", pixtype="r", verb='no') iraf.imarith("artsn", "+", midpt, "artsn", verb='no') artap1, artap2, artap3, artmag1, artmag2, artmag3, artfitmag, arttruemag, artmagerr, artcentx, artcenty = \ fitsn(img,imgpsf,coordlist,_recenter,fwhm0,'reserr','artsn','artres',_show,_interactive,z11,z22,midpt,size,apco0,dmax,dmin) for ii in range(0, _numiter): lsc.util.delete("reserr.ar?") lsc.util.delete("artlist.ma?") lsc.util.delete("artsky.fit?") lsc.util.delete("artbg.fit?") lsc.util.delete("artbgs.fit?") lsc.util.delete("artsn.fit?") lsc.util.delete("artres.fit?") lsc.util.delete("artlist.al?") iraf.imsurfit("skyfit", "artbg", xorder=xbgord0, yorder=ybgord0, regions="section", section="sec") midpt = np.mean(fits.getdata('artbg.fits')) iraf.imcopy("reserr.fits", "artsky.fits") iraf.imcopy(inp, "artbgs.fits") iraf.imcopy("artbgs.fits", out) iraf.imarith("reserr", "-", "artsky", "artsn", calctype="r", pixtype="r", verb='no') iraf.imarith("artsn.fits", "+", midpt, "artsn.fits", verb='no') artap1, artap2, artap3, artmag1, artmag2, artmag3, artfitmag, arttruemag, artmagerr, artcentx, artcenty = \ fitsn(img,imgpsf,coordlist,_recenter,fwhm0,'reserr','artsn','artres',_show,_interactive,z11,z22,midpt,size,0,dmax,dmin) ####### if i == 0: era = 'yes' else: era = 'no' artx = .5 + .25 * artx arty = .5 + .25 * arty if _show: _tmp1, _tmp2, goon = lsc.util.display_image('skyfit.fits', 1, '', '', False, _xcen=artx, _ycen=arty, _xsize=.25, _ysize=.25, _erase=era) try: tmpart.append(float(arttruemag[0])) except: pass i = i + 1 for i in tmpart: print i print " ########## " try: media = mean(array(tmpart)) arterr = std(array(tmpart)) arterr2 = std( compress((average(tmpart) - std(tmpart) < array(tmpart)) & (array(tmpart) < average(tmpart) + std(tmpart)), array(tmpart))) except: media = 0 arterr = 0 arterr2 = 0 print '### average = %6.6s \t arterr= %6.6s ' % (str(media), str(arterr)) print '### %6.6s \t (error at 1 sigma rejection) ' % (str(arterr2)) lsc.util.delete( "reserr.fit?,artbg.fit?,artstar.fit?,artres.fit?,artfit.fit?,artskyfit.fit?" ) lsc.util.delete("reserr.ar?") lsc.util.delete("artlist.co?") return arterr2, arterr
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)
fitsfiles = sys.argv[1] mode = sys.argv[2] spottol = int(sys.argv[3]) fitsfile = average(fitsfiles) if fitsfile.find('/') is -1: fitsfile = "%s/%s" % (os.getcwd(), fitsfile) os.system("rm -f back.fits") iraf.images() iraf.images.imfit() iraf.images.imutil() iraf.crutil() iraf.set(uparm="./uparm") if mode == 'MMIRS': iraf.imsurfit(fitsfile, 'back.fits', xorder=3, yorder=3, upper=2, lower=2, ngrow=35, rows='[20:510]', columns='[20:510]') iraf.imarith(fitsfile, '-', 'back.fits', fitsfile) iraf.cosmicrays(fitsfile,fitsfile,interactive='no',threshold=20,fluxratio=3,window=7) else: iraf.imsurfit(fitsfile, 'back.fits', xorder=2, yorder=2, upper=2, lower=2, ngrow=15) iraf.imarith(fitsfile, '-', 'back.fits', fitsfile) hdu = rfits(fitsfile) hdu.verify('fix') image = hdu.data hdr = hdu.header try: rot = hdr['ROT'] except KeyError: rot = 0.0
def errore(img,imgpsf,coordlist,size,truemag,fwhm0,leng0,_show,_interactive,_numiter,z11,z22,midpt,nax,nay,xbgord0,ybgord0,_recenter,apco0,dmax,dmin): import lsc import os,sys,re,string from numpy import array,mean,std,compress,average from pyraf import iraf if not _numiter: _numiter=3 dartf=100 while dartf>=size-1: if _interactive: artfac0 =raw_input('>>> Dispersion of artificial star positions (in units of FWHM) [1] ') if not artfac0: artfac0=1 else: artfac0=1 try: artfac0=float(artfac0) if float(artfac0)>=size-1: print '!!! WARNING: '+str(artfac0)+' too large (max '+str(size)+'- 1)' print 'try again....' else: dartf = artfac0 except: print '#### WARNING: '+str(artfac0)+' should be a number !!!!' print 'try again....' lsc.util.delete("tmpar?") lsc.util.delete('artskyfit.fits') os.system('cp skyfit.fits artskyfit.fits') i=0 tmpart=[] while i<=8: lsc.util.delete("reserr.fit?,artbg.fit?,artstar.fit?,artres.fit?,artfit.fit?") artrad = fwhm0/2. #artseed = artseed+1234 artx = int(i/3.)-1 if i<=2: arty = artx+i if 3<=i<=5: arty = artx-1+i-3 if i>=6: arty = artx-2+i-6 ff=open(img+".sn.coo",'r') ss=ff.readline() ff.close() xbb=float(string.split(ss)[0]) ybb=float(string.split(ss)[1]) xbb=xbb+artx*fwhm0*artfac0 ybb=ybb+arty*fwhm0*artfac0 lsc.util.delete(coordlist) ff=open(coordlist,'w') ff.write(str(xbb)+' '+str(ybb)+' '+str(truemag[0])+" 1") ff.close() xb1 = int(float(xbb)-fwhm0*float(leng0)/2) xb2 = int(float(xbb)+fwhm0*float(leng0)/2) yb1 = int(float(ybb)-fwhm0*float(leng0)/2) yb2 = int(float(ybb)+fwhm0*float(leng0)/2) sec="1 "+str(xb1)+" 1 "+str(nay)+'\n' sec=sec+str(xb2)+' '+str(nax)+" 1 "+str(nay)+'\n' sesc=sec+str(xb1)+' '+str(xb2)+" 1 "+str(yb1)+'\n' sec=sec+str(xb1)+' '+str(xb2)+' '+str(yb2)+' '+str(nay)+'\n' ff = open('sec','w') ff.write(sec) ff.close() lsc.util.delete("reserr.ar?") lsc.util.delete("artlist.ma?") lsc.util.delete("artsky.fit?") lsc.util.delete("artbg.fit?") lsc.util.delete("artbgs.fit?") lsc.util.delete("artsn.fit?") lsc.util.delete("artres.fit?") lsc.util.delete("artlist.al?") iraf.addstar("artskyfit",coordlist,imgpsf,"reserr",nstar=1,veri='no',simple='yes',verb='no') # reserr = skyfit + artificial star ######## inp = "artbg.fits["+str(xb1)+":"+str(xb2)+","+str(yb1)+":"+str(yb2)+"]" out = "artsky.fits["+str(xb1)+":"+str(xb2)+","+str(yb1)+":"+str(yb2)+"]" iraf.imsurfit("reserr","artbg",xorder=xbgord0,yorder=ybgord0,regions="section",section="sec") midpt=np.mean(fits.getdata('artbg.fits')) iraf.imcopy('reserr.fits','artsky.fits') iraf.imcopy(inp,'artbgs.fits') iraf.imcopy("artbgs.fits",out) iraf.imarith("reserr","-","artsky","artsn",calctype="r",pixtype="r",verb='no') iraf.imarith("artsn","+",midpt,"artsn",verb='no') artap1, artap2, artap3, artmag1, artmag2, artmag3, artfitmag, arttruemag, artmagerr, artcentx, artcenty = \ fitsn(img,imgpsf,coordlist,_recenter,fwhm0,'reserr','artsn','artres',_show,_interactive,dmax,dmin,z11,z22,midpt,size,apco0) for ii in range(0,_numiter): lsc.util.delete("reserr.ar?") lsc.util.delete("artlist.ma?") lsc.util.delete("artsky.fit?") lsc.util.delete("artbg.fit?") lsc.util.delete("artbgs.fit?") lsc.util.delete("artsn.fit?") lsc.util.delete("artres.fit?") lsc.util.delete("artlist.al?") iraf.imsurfit("skyfit","artbg",xorder=xbgord0,yorder=ybgord0,regions="section",section="sec") midpt = np.mean(fits.getdata('artbg.fits')) iraf.imcopy("reserr.fits","artsky.fits") iraf.imcopy(inp,"artbgs.fits") iraf.imcopy("artbgs.fits",out) iraf.imarith("reserr","-","artsky","artsn",calctype="r",pixtype="r",verb='no') iraf.imarith("artsn.fits","+",midpt,"artsn.fits",verb='no') artap1, artap2, artap3, artmag1, artmag2, artmag3, artfitmag, arttruemag, artmagerr, artcentx, artcenty = \ fitsn(img,imgpsf,coordlist,_recenter,fwhm0,'reserr','artsn','artres',_show,_interactive,dmax,dmin,z11,z22,midpt,size,0) ####### if i==0: era='yes' else: era='no' artx = .5+.25*artx arty = .5+.25*arty if _show: _tmp1,_tmp2,goon=lsc.util.display_image('skyfit.fits',1,'', '', False, _xcen=artx, _ycen=arty, _xsize=.25, _ysize=.25, _erase=era) try: tmpart.append(float(arttruemag[0])) except: pass i=i+1 for i in tmpart: print i print " ########## " try: media = mean(array(tmpart)) arterr = std(array(tmpart)) arterr2 = std(compress((average(tmpart)-std(tmpart)<array(tmpart))&(array(tmpart)<average(tmpart)+std(tmpart)),array(tmpart))) except: media = 0 arterr = 0 arterr2 = 0 print '### average = %6.6s \t arterr= %6.6s ' % (str(media),str(arterr)) print '### %6.6s \t (error at 1 sigma rejection) ' % (str(arterr2)) lsc.util.delete("reserr.fit?,artbg.fit?,artstar.fit?,artres.fit?,artfit.fit?,artskyfit.fit?") lsc.util.delete("reserr.ar?") lsc.util.delete("artlist.co?") return arterr2,arterr
mode = sys.argv[2] spottol = int(sys.argv[3]) fitsfile = average(fitsfiles) if fitsfile.find("/") is -1: fitsfile = "%s/%s" % (os.getcwd(), fitsfile) os.system("rm -f back.fits") iraf.images() iraf.images.imfit() iraf.images.imutil() iraf.crutil() iraf.set(uparm="./uparm") if mode == "MMIRS": iraf.imsurfit( fitsfile, "back.fits", xorder=3, yorder=3, upper=2, lower=2, ngrow=35, rows="[20:510]", columns="[20:510]" ) iraf.imarith(fitsfile, "-", "back.fits", fitsfile) iraf.cosmicrays(fitsfile, fitsfile, interactive="no", threshold=20, fluxratio=3, window=7) else: iraf.imsurfit(fitsfile, "back.fits", xorder=2, yorder=2, upper=2, lower=2, ngrow=15) iraf.imarith(fitsfile, "-", "back.fits", fitsfile) hdu = rfits(fitsfile) hdu.verify("fix") image = hdu.data hdr = hdu.header try: rot = hdr["ROT"] except KeyError: rot = 0.0
else: _xbgord0 = xbgord0 ybgord0 = raw_input( '>>> Order of function in y for bg fit ? [' + str(_ybgord0) + '] ? ') if not ybgord0: ybgord0 = _ybgord0 else: _ybgord0 = ybgord0 try: float(xbgord0) float(ybgord0) checkorder = 'no' except: print 'WARNING: value not valid !!' checimsurfitkorder = 'yes' iraf.imsurfit("original", "bg", xorder=xbgord0, yorder=ybgord0, regions="section", section="sec") else: if not _interactive: print 'select' xb1 = int(min(xxsn) - fwhm0*_fb) xb2 = int(max(xxsn) + fwhm0*_fb) yb1 = int(min(yysn) - fwhm0*_fb) yb2 = int(max(yysn) + fwhm0*_fb) else: lsc.util.delete("tmplabel") lsc.util.delete("tmptbl") ff = open('tmplabel', 'w') ff.write('') ff.close() print ">>> Mark corners of bg-region with >b<, exit >q<"
def makefringing(listimg, _output, _xorder, _yorder, _interactive, combine='average', rejection='avsigclip'): # print "LOGX:: Entering `makefringing` method/function in %(__file__)s" % # globals() import os import string import re import ntt from ntt.util import readhdr, readkey3, delete, updateheader from pyraf import iraf from pyfits import open as popen from numpy import median, where, mean iraf.noao(_doprint=0) iraf.nproto(_doprint=0) if _interactive == True: listimg2 = [] for img in listimg: _exptime = readkey3(readhdr(img), 'exptime') if float(_exptime) >= 100: answ = 'xxx' while answ.lower() not in ['y', 'n', 's', 'a']: iraf.display(img, frame=1, fill='yes') answ = raw_input( 'use this image (yes,no,stop (not more images),all) [[y]/n/s/a] ? ') if not answ: answ = 'y' if answ.lower() == 'y': listimg2.append(img) elif answ.lower() == 'a': listimg2 = listimg[:] if answ.lower() in ['a', 's']: break listimg = listimg2[:] iraf.nproto.objmasks1.fitxord = 1 iraf.nproto.objmasks1.fityord = 1 listmask = [] ff = open('_listmask', 'w') gg = open('_listobg', 'w') hh = open('_listobgz', 'w') for img in listimg: _exptime = readkey3(readhdr(img), 'exptime') gg.write(img + '\n') hh.write('z_' + img + '\n') ff.write('mask_' + img + '\n') listmask.append('mask_' + img) os.system('rm -rf mask_' + img) os.system('rm -rf t_' + img) os.system('rm -rf z_' + img) iraf.imsurfit(img, 't_' + img, xorder=_xorder, yorder=_yorder, type_ou='residual', regions='all') iraf.imarith('t_' + img, '/', _exptime, 'z_' + img) os.system('rm -rf t_' + img) gg.close() ff.close() hh.close() os.system('rm ' + _output) iraf.objmasks(images='@_listobgz', objmasks='@_listmask', omtype='boolean', blksize=-16, convolv='block 3 3', hsigma=5, lsigma=3, minpix=10, ngrow=2, agrow=4.) iraf.imcombine('@_listobgz', output=_output, masktyp='!OBJMASK', maskval=0, combine=_combine, reject=_rejection, scale='none', statsec='[100:800,100:800]', offsets='', rdnoise='', gain='', nlow=1, nhigh=1, logfile='imcombinelog') for img in listimg: os.system('rm -rf mask_' + img) os.system('rm -rf z_' + img) os.system('rm _listmask') os.system('rm _listobg') iraf.display(_output, frame=2, fill='yes')
def makefringing(listimg, _output, _xorder, _yorder, _interactive, combine='average', rejection='avsigclip'): # print "LOGX:: Entering `makefringing` method/function in %(__file__)s" % # globals() import os import string import re import ntt from ntt.util import readhdr, readkey3, delete, updateheader from pyraf import iraf from pyfits import open as popen from numpy import median, where, mean iraf.noao(_doprint=0) iraf.nproto(_doprint=0) if _interactive == True: listimg2 = [] for img in listimg: _exptime = readkey3(readhdr(img), 'exptime') if float(_exptime) >= 100: answ = 'xxx' while answ.lower() not in ['y', 'n', 's', 'a']: iraf.display(img, frame=1, fill='yes') answ = raw_input( 'use this image (yes,no,stop (not more images),all) [[y]/n/s/a] ? ' ) if not answ: answ = 'y' if answ.lower() == 'y': listimg2.append(img) elif answ.lower() == 'a': listimg2 = listimg[:] if answ.lower() in ['a', 's']: break listimg = listimg2[:] iraf.nproto.objmasks1.fitxord = 1 iraf.nproto.objmasks1.fityord = 1 listmask = [] ff = open('_listmask', 'w') gg = open('_listobg', 'w') hh = open('_listobgz', 'w') for img in listimg: _exptime = readkey3(readhdr(img), 'exptime') gg.write(img + '\n') hh.write('z_' + img + '\n') ff.write('mask_' + img + '\n') listmask.append('mask_' + img) os.system('rm -rf mask_' + img) os.system('rm -rf t_' + img) os.system('rm -rf z_' + img) iraf.imsurfit(img, 't_' + img, xorder=_xorder, yorder=_yorder, type_ou='residual', regions='all') iraf.imarith('t_' + img, '/', _exptime, 'z_' + img) os.system('rm -rf t_' + img) gg.close() ff.close() hh.close() os.system('rm ' + _output) iraf.objmasks(images='@_listobgz', objmasks='@_listmask', omtype='boolean', blksize=-16, convolv='block 3 3', hsigma=5, lsigma=3, minpix=10, ngrow=2, agrow=4.) iraf.imcombine('@_listobgz', output=_output, masktyp='!OBJMASK', maskval=0, combine=_combine, reject=_rejection, scale='none', statsec='[100:800,100:800]', offsets='', rdnoise='', gain='', nlow=1, nhigh=1, logfile='imcombinelog') for img in listimg: os.system('rm -rf mask_' + img) os.system('rm -rf z_' + img) os.system('rm _listmask') os.system('rm _listobg') iraf.display(_output, frame=2, fill='yes')
def errore(size, truemag, fwhm0, leng0): from pyraf import iraf import string, os, sys dartf = 100 while dartf >= size - 1: artfac0 = raw_input( '>>> Dispersion of artificial star positions (in units of FWHM) [1] ' ) if not artfac0: artfac0 = 1 try: artfac0 = float(artfac0) if float(artfac0) >= size - 1: print '!!! WARNING: ' + str( artfac0) + ' too large (max ' + str(size) + '- 1)' print 'try again....' else: dartf = artfac0 except: print '#### WARNING: ' + str(artfac0) + ' should be a number !!!!' print 'try again....' iraf.delete("tmpar?", ve='no') i = 0 tmpart = [] while i <= 8: iraf.delete( "reserr.fit?,artbg.fit?,artstar.fit?,artres.fit?,artfit.fit?,artskyfit.fit?", ve='no') artrad = fwhm0 / 2. #artseed = artseed+1234 artx = int(i / 3.) - 1 if i <= 2: arty = artx + i if 3 <= i <= 5: arty = artx - 1 + i - 3 if i >= 6: arty = artx - 2 + i - 6 ff = open(img + ".sn.coo", 'r') ss = ff.readline() ff.close() xbb = float(string.split(ss)[0]) ybb = float(string.split(ss)[1]) xbb = xbb + artx * fwhm0 * artfac0 ybb = ybb + arty * fwhm0 * artfac0 print xbb, ybb iraf.delete("artlist.coo", ve='no') ff = open("artlist.coo", 'w') ff.write(str(xbb) + ' ' + str(ybb) + ' ' + str(truemag[0]) + " 1") ff.close() xb1 = int(float(xbb) - fwhm0 * float(leng0) / 2) xb2 = int(float(xbb) + fwhm0 * float(leng0) / 2) yb1 = int(float(ybb) - fwhm0 * float(leng0) / 2) yb2 = int(float(ybb) + fwhm0 * float(leng0) / 2) sec = "1 " + str(xb1) + " 1 " + str(nay) + '\n' sec = sec + str(xb2) + ' ' + str(nax) + " 1 " + str(nay) + '\n' sec = sec + str(xb1) + ' ' + str(xb2) + " 1 " + str(yb1) + '\n' sec = sec + str(xb1) + ' ' + str(xb2) + ' ' + str(yb2) + ' ' + str( nay) + '\n' ff = open('sec', 'w') ff.write(sec) ff.close() iraf.delete("reserr.art", ve='no') iraf.delete("artlist.mag", ve='no') iraf.delete("artlist.als", ve='no') iraf.addstar("skyfit", "artlist.coo", imgpsf, "reserr", nstar=1, veri='no', simple='yes') iraf.imsurfit("reserr", "artbg", xorder=xbgord0, yorder=ybgord0, regions="section", section="sec") iraf.imarith("reserr", "-", "artbg", "artstar") iraf.phot("artstar", "artlist.coo", "artlist.mag", veri='no') iraf.allstar("artstar", "artlist.mag", imgpsf, "artlist.als", "", "artres", veri='no', verb='no') iraf.imarith("artstar", "-", "artres", "artfit") iraf.imarith("reserr", "-", "artfit", "artskyfit") if i == 0: era = 'yes' else: era = 'no' artx = .5 + .25 * artx arty = .5 + .25 * arty iraf.display("artskyfit", 1, fill='yes', erase=era, xcen=artx, ycen=arty, xsize=.25, ysize=.25) try: tmpart.append( float( iraf.txdump("artlist.als", "mag", expr='yes', Stdout=1)[0])) except: pass i = i + 1 for i in tmpart: print i print " ########## " try: media = mean(array(tmpart)) arterr = std(array(tmpart)) arterr2 = std( compress((average(tmpart) - std(tmpart) < array(tmpart)) & (array(tmpart) < average(tmpart) + std(tmpart)), array(tmpart))) except: media = 0 arterr = 0 arterr2 = 0 print '### average = %6.6s \t arterr= %6.6s ' % (str(media), str(arterr)) print '### %6.6s \t (error at 1 sigma rejection) ' % (str(arterr2)) iraf.delete( "reserr.fit?,artbg.fit?,artstar.fit?,artres.fit?,artfit.fit?,artskyfit.fit?", ve='no') iraf.delete("reserr.art", ve='no') iraf.delete("artlist.*", ve='no') return arterr2, arterr