def lris_standard(standards, xcorr=yes): '''Extract standard stars and calculate sensitivity functions.''' for ofile, nstd in standards: shutil.copy(ofile, "%s.fits" % nstd) # Extract standard if get_head("%s.fits" % nstd, "SKYSUB"): iraf.apall(nstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none") else: iraf.apall(nstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit") if xcorr: # Cross correlate to tweak wavelength iraf.xcsao("%s.ms" % nstd, templates=XCTEMPLATE, correlate="wavelength", logfiles="%s.xcsao" % ofile) # If a solution was found, apply shift try: xcsaof = open("%s.xcsao" % ofile) shift = float(xcsaof.readlines()[6].split()[14]) except: shift = 0.0 iraf.specshift("%s.ms" % nstd, -shift) # Create telluric iraf.splot("%s.ms" % nstd) # Remove telluric and absorption, save as a%s.ms iraf.sarith("%s.ms" % nstd, "/", "a%s.ms" % nstd, "telluric.%s.fits" % nstd) iraf.imreplace("telluric.%s.fits" % nstd, 1.0, lower=0.0, upper=0.0) iraf.splot("telluric.%s.fits" % nstd) # Remove stellar features and resave # Create smoothed standard iraf.gauss("a%s.ms[*,1,1]" % nstd, "s%s.ms" % nstd, 5.0) iraf.sarith("%s.ms" % nstd, "/", "s%s.ms" % nstd, "ds%s.ms" % nstd) # Apply telluric correction iraf.telluric("ds%s.ms" % nstd, "tds%s.ms" % nstd, "telluric.%s.fits" % nstd, xcorr=no, tweakrms=no, interactive=no, sample='4000:4010,6850:6975,7150:7350,7575:7725,8050:8400,8900:9725') # Define bandpasses for standard star calculation obj=get_head("%s.fits" % nstd, "OBJECT") iraf.standard("tds%s.ms" % nstd, "%s.std" % nstd, extinction='home$extinct/maunakeaextinct.dat', caldir='home$standards/', observatory='Keck', interac=yes, star_name=obj, airmass='', exptime='') # Determine sensitivity function iraf.sensfunc('%s.std' % nstd, '%s.sens' % nstd, extinction='home$extinct/maunakeaextinct.dat', newextinction='extinct.dat', observatory='Keck', function='legendre', order=4, interactive=yes) return
def make_flat(images, outflat, gain=1.0, rdnoise=0.0, xwindow=50, ywindow=50, hmin=0, hmax=65535, lowclip=0.7, highclip=1.3): '''Construct flat field from individual frames''' flatimages = ','.join(images) iraf.flatcombine(flatimages, output='flat1', combine='median', reject='avsigclip', ccdtype='', process=no, subsets=no, delete=no, clobber=no, scale='median', lsigma=3.0, hsigma=3.0, gain=gain, rdnoise=rdnoise) iraf.fmedian('flat1', 'flat2', xwindow, ywindow, hmin=hmin, hmax=hmax) iraf.imarith('flat1', '/', 'flat2', outflat) iraf.imreplace(outflat, 1.0, lower=INDEF, upper=lowclip) iraf.imreplace(outflat, 1.0, lower=highclip, upper=INDEF) return
def run_imreplace(imageFilename, lowPixVal, uppPixVal): ''' replace all pixels in image between lower and upper with zero parameter imageFilename - the full path filename of the image on which to invoke imexam parameter lowPixVal - the lower bound of pixels to be replaced with zero parameter uppPixVal - the upper bound of pixels to be replaced with zero ''' # TODO: this could be inlines easily, only called once in main() iraf.imreplace(imageFilename, 0, lower=lowPixVal, upper=uppPixVal)
def flatten(flatFile): """grab any sky-subtracted images, and flatten them using flatFile""" # first we have to clean up the flat, and get rid of any negative numbers or zeros # just set them equal to 1 (??? why do we do this ???) iraf.imreplace(flatFile, 1.0, imaginary=0.0, lower="INDEF", upper=1.0, radius=0.0) # get a list of the sky subtracted images skyims = glob.glob("scratch/s-binir*fits") flatIms = ["s-f-" + i[10:] for i in skyims] # organize input and output in iraf-friendly ways inputFiles = joinStrList(skyims) outputFiles = joinStrList(flatIms, scratch=True) # now flatfield the sky subtracted images iraf.ccdproc( images=inputFiles, output=outputFiles, ccdtype="", max_cache=0, noproc="no", fixpix="no", overscan="no", trim="no", zerocor="no", darkcor="no", flatcor="yes", illumcor="no", fringecor="no", readcor="no", scancor="no", readaxis="line", fixfile="", biassec="", trimsec="", zero="", dark="", flat=flatFile, illum="", fringe="", minreplace=1.0, scantype="shortscan", nscan=1, interactive="no", function="legendre", order=1, sample="*", naverage=1, niterate=1, low_reject=3.0, high_reject=3.0, grow=0.0, ) return
def make_flat(images, outflat, gain=1.0, rdnoise=0.0, xwindow=50, ywindow=50, hmin=0, hmax=65535, lowclip=0.7, highclip=1.3): '''Construct flat field from individual frames''' flatimages=','.join(images) iraf.flatcombine(flatimages, output='flat1', combine='median', reject='avsigclip', ccdtype='', process=no, subsets=no, delete=no, clobber=no, scale='median', lsigma=3.0, hsigma=3.0, gain=gain, rdnoise=rdnoise) iraf.fmedian('flat1', 'flat2', xwindow, ywindow, hmin=hmin, hmax=hmax) iraf.imarith('flat1', '/', 'flat2', outflat) iraf.imreplace(outflat, 1.0, lower=INDEF, upper=lowclip) iraf.imreplace(outflat, 1.0, lower=highclip, upper=INDEF) return
def main(fits): fits_skysig = fits[0:fits.rfind(".")] + "_skysig.fits" sexpath = "/Users/nagashima/astro/default/sextractor/object/default.sex" #set sextractor configure file path #make object mask image fits_seg = fits[0:fits.rfind(".")] + "_seg.fits" cat_seg = fits_seg[0:fits_seg.rfind(".")] + ".cat" dma = 1. os.system( "sex %s -c %s -DETECT_MINAREA %s -CHECKIMAGE_TYPE SEGMENTATION -CATALOG_NAME %s -CHECKIMAGE_NAME %s" % (fits, sexpath, dma, cat_seg, fits_seg)) iraf.imreplace(images=fits_seg, value=1e06, lower=1.) #use stack image and only object image -> make skysigma image if os.path.exists(fits_skysig) == True: os.system("rm %s" % fits_skysig) iraf.imarith(fits, "+", fits_seg, fits_skysig) print "ds9 %s &" % fits_skysig return fits_skysig
def prep(df_image, hi_res_image): 'run SExtractor to get bright sources that are easily detected in the high resolution data' ##### DEB: Can choose to run sextractor more or less aggressively subprocess.call('sex %s' % hi_res_image, shell=True) 'copy the segmentation map to a mask' iraf.imcopy('seg.fits', '_mask.fits') 'replace the values in the segments in the segmentation map (i.e. stars) all to 1, all the background is still 0' iraf.imreplace('_mask.fits', 1, lower=0.5) 'multiply the mask (with 1s at the stars) by the high res image to get the star flux back - now have the flux model' iraf.imarith('_mask.fits', '*', '%s' % hi_res_image, '_fluxmod_cfht') 'smooth the flux model' 'increase size of mask, so more is subtracted' 'the "1.5" in the next line should be a user-defined parameter that is given' 'to the script; it controls how much of the low surface brightness' 'emission in the outskirts of galaxies is subtracted. This choice' 'depends on the science application' iraf.gauss('_fluxmod_cfht', '_fluxmod_cfht_smoothed', 2.5) ### nsigma=4 ''' imreplace _fluxmod_cfht_rs -1 lower=0 upper=0 imreplace _fluxmod_cfht_rs 1 lower=-0.5 imreplace _fluxmod_cfht_rs 0 upper=-0.5 imarith _cfht_r * _fluxmod_cfht_rs _fluxmod_cfht_r_new ''' ''' # this is the key new step! we're registering the CFHT image # to a frame that is 4x finer sampled than the Dragonfly image. # this avoids all the pixelation effects we had before. # (4x seems enough; but we could have it as a free parameter - need # to be careful as it occurs elsewhere in the scripts too) blkrep _df_g _df_g4 4 4 ''' 'register the flux model onto the same pixel scale as the dragonfly image' iraf.wregister('_fluxmod_cfht_smoothed', '%s' % df_image, '_fluxmod_dragonfly', interpo='linear', fluxcon='yes') return None
def make_skysigim( fitspath ): """ stdout = iraf.imstat(images=fitspath,fields="npix,min,max,stddev,midpt,mode",Stdout=1) key_ls = stdout[0].strip("#").split() val_ls = map(float,stdout[1].split()) data = dict([(key,val) for key,val in zip(key_ls,val_ls)]) if ( data["MIDPT"] < 1000 ) and ( data["MODE"] < 1000 ): fits_skypath = fitspath print "Already substract sky!!" else: fits_skypath = fitspath[0:fitspath.rfind(".")] + "_sk.fits" os.system("sex %s -c /mnt/data5/users/nagashima/default/default.sex -CHECKIMAGE_TYPE -BACKGROUND -CHECKIMAGE_NAME %s" % (fitspath,fits_skypath)) print "Subtract sky!!" """ objonlypath = fitspath[0:fitspath.rfind("/")] + "/object_only.fits" skysigpath = fitspath[0:fitspath.rfind("/")] + "/skysig.fits" if os.path.exists(objonlypath) == True :os.remove(objonlypath) if os.path.exists(skysigpath) == True :os.remove(skysigpath) os.system("sex %s -c /mnt/data5/users/nagashima/default/default.sex -CHECKIMAGE_TYPE OBJECTS -CHECKIMAGE_NAME %s" % (fitspath,objonlypath)) iraf.imreplace(images=objonlypath,value=-300000,lower=1)#not 0 not float so iraf.imarith(fitspath,"+",objonlypath,skysigpath) return skysigpath
def mask(res_org,upperlim=0.04): print "\n**** Masking the residual, using upperlim of %s ****\n" % upperlim upperlim = float(upperlim) iraf.imdel('_res_final.fits') iraf.imdel('_model_mask') iraf.imdel('_model_maskb') iraf.imcopy('_model_sc.fits','_model_mask.fits') iraf.imreplace('_model_mask.fits',0,upper=upperlim) iraf.imreplace('_model_mask.fits',1,lower=upperlim/2.) iraf.boxcar('_model_mask','_model_maskb',5,5) iraf.imreplace('_model_maskb.fits',1,lower=0.1) iraf.imreplace('_model_maskb.fits',0,upper=0.9) iraf.imarith(1,'-','_model_maskb','_model_maskb') iraf.imarith('_model_maskb','*',res_org,'_res_final') return None
def lacos(_input0, output='clean.fits', outmask='mask.fits', gain=1.3, readn=9, xorder=9, yorder=9, sigclip=4.5, sigfrac=0.5, objlim=1, verbose=True, interactive=False): # print "LOGX:: Entering `lacos` method/function in %(__file__)s" % # globals() import ntt from ntt.util import delete import sys import re import os import string from pyraf import iraf import numpy as np oldoutput, galaxy, skymod, med5 = 'oldoutput.fits', 'galaxy.fits', 'skymod.fits', 'med5.fits' blk, lapla, med3, med7, sub5, sigima, finalsel = 'blk.fits', 'lapla.fits', 'med3.fits', 'med7.fits', 'sub5.fits', 'sigima.fits', 'finalsel.fits' deriv2, noise, sigmap, firstsel, starreject = 'deriv2.fits', 'noise.fits', 'sigmap.fits', 'firstsel.fits', 'starreject.fits' inputmask = 'inputmask.fits' # set some parameters in standard IRAF tasks iraf.convolve.bilinear = 'no' iraf.convolve.radsym = 'no' # create Laplacian kernel # laplkernel = np.array([[0.0, -1.0, 0.0], [-1.0, 4.0, -1.0], [0.0, -1.0, 0.0]]) f = open('_kernel', 'w') f.write('0 -1 0;\n-1 4 -1;\n0 -1 0') f.close() # create growth kernel f = open('_gkernel', 'w') f.write('1 1 1;\n1 1 1;\n1 1 1') f.close() gkernel = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]) delete(galaxy) delete(skymod) delete(oldoutput) if not output: output = _input0 else: delete(output) iraf.imcopy(_input0, output, verbose='no') delete('_xxx.fits,_yyy.fits') iraf.imcopy(_input0 + '[350:550,*]', '_xxx.fits', verbose='no') _input = '_xxx.fits' arrayinput, headerinput = ntt.cosmics.fromfits(_input, verbose=False) ntt.cosmics.tofits(outmask, np.float32( arrayinput - arrayinput), headerinput, verbose=False) # subtract object spectra if desired iraf.fit1d(_input, galaxy, "fit", axis=2, order=9, func="leg", low=4., high=4., nav=1, inter='no', sample="*", niter=3, grow=0, cursor="") iraf.imarith(_input, "-", galaxy, oldoutput) # Subtract sky lines iraf.fit1d(oldoutput, skymod, "fit", axis=1, order=5, func="leg", low=4., high=4., inter='no', sample="*", nav=1, niter=3, grow=0, cursor="") iraf.imarith(oldoutput, "-", skymod, oldoutput) arrayoldoutput, headeroldoutput = ntt.cosmics.fromfits( oldoutput, verbose=False) # add object spectra to sky model iraf.imarith(skymod, "+", galaxy, skymod) delete(med5) # add median of residuals to sky model iraf.median(oldoutput, med5, 5, 5, zlor='INDEF', zhir='INDEF', verbose='no') # m5 = ndimage.filters.median_filter(_inputarray, size=5, mode='mirror') iraf.imarith(skymod, "+", med5, med5) # take second-order derivative (Laplacian) of input image # kernel is convolved with subsampled image, in order to remove negative # pattern around high pixels delete(blk) delete(lapla) iraf.blkrep(oldoutput, blk, 2, 2) iraf.convolve(blk, lapla, '_kernel') iraf.imreplace(lapla, 0, upper=0, lower='INDEF') delete(deriv2) delete(noise) iraf.blkavg(lapla, deriv2, 2, 2, option="average") # create noise model iraf.imutil.imexpr(expr='sqrt(a*' + str(gain) + '+' + str(readn) + '**2)/' + str(gain), a=med5, output=noise, verbose='no') iraf.imreplace(med5, 0.00001, upper=0, lower='INDEF') # divide Laplacian by noise model delete(sigmap) iraf.imutil.imexpr(expr='(a/b)/2', a=deriv2, b=noise, output=sigmap, verbose='no') # removal of large structure (bright, extended objects) delete(med5) iraf.median(sigmap, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no') iraf.imarith(sigmap, "-", med5, sigmap) # find all candidate cosmic rays # this selection includes sharp features such as stars and HII regions arraysigmap, headersigmap = ntt.cosmics.fromfits(sigmap, verbose=False) arrayf = np.where(arraysigmap < sigclip, 0, arraysigmap) arrayf = np.where(arrayf > 0.1, 1, arrayf) ntt.cosmics.tofits(firstsel, np.float32( arrayf), headersigmap, verbose=False) # compare candidate CRs to median filtered image # this step rejects bright, compact sources from the initial CR list # subtract background and smooth component of objects delete(med3) iraf.median(oldoutput, med3, 3, 3, zlo='INDEF', zhi='INDEF', verbose='no') delete(med7) delete('_' + med3) iraf.median(med3, med7, 7, 7, zlo='INDEF', zhi='INDEF', verbose='no') iraf.imutil.imexpr(expr='(a-b)/c', a=med3, b=med7, c=noise, output='_' + med3, verbose='no') iraf.imreplace('_' + med3, 0.01, upper=0.01, lower='INDEF') # compare CR flux to object flux delete(starreject) iraf.imutil.imexpr(expr='a+b+c', a=firstsel, b=sigmap, c="_" + med3, output=starreject, verbose='no') # discard if CR flux <= objlim * object flux iraf.imreplace(starreject, 0, upper=objlim, lower='INDEF') iraf.imreplace(starreject, 1, lower=0.5, upper='INDEF') iraf.imarith(firstsel, "*", starreject, firstsel) # grow CRs by one pixel and check in original sigma map arrayfirst, headerfirst = ntt.cosmics.fromfits(firstsel, verbose=False) arraygfirst = ntt.cosmics.my_convolve_with_FFT2(arrayfirst, gkernel) arraygfirst = np.where(arraygfirst > 0.5, 1, arraygfirst) arraygfirst = arraygfirst * arraysigmap arraygfirst = np.where(arraygfirst < sigclip, 0, arraygfirst) arraygfirst = np.where(arraygfirst > 0.1, 1, arraygfirst) # grow CRs by one pixel and lower detection limit sigcliplow = sigfrac * sigclip # Finding neighbouring pixels affected by cosmic rays arrayfinal = ntt.cosmics.my_convolve_with_FFT2(arraygfirst, gkernel) arrayfinal = np.where(arrayfinal > 0.5, 1, arrayfinal) arrayfinal = arrayfinal * arraysigmap arrayfinal = np.where(arrayfinal < sigcliplow, 0, arrayfinal) arrayfinal = np.where(arrayfinal > 0.1, 1, arrayfinal) # determine number of CRs found in this iteration arraygfirst = (1 - (arrayfinal - arrayfinal)) * arrayfinal npix = [str(int(np.size(np.where(arraygfirst > 0.5)) / 2.))] # create cleaned output image; use 3x3 median with CRs excluded arrayoutmask = np.where(arrayfinal > 1, 1, arrayfinal) ntt.cosmics.tofits(outmask, np.float32( arrayoutmask), headerfirst, verbose=False) delete(inputmask) arrayinputmask = (1 - (10000 * arrayoutmask)) * arrayoldoutput ntt.cosmics.tofits(inputmask, np.float32( arrayinputmask), headerfirst, verbose=False) delete(med5) iraf.median(inputmask, med5, 5, 5, zloreject=- 9999, zhi='INDEF', verbose='no') iraf.imarith(outmask, "*", med5, med5) delete('_yyy.fits') iraf.imutil.imexpr(expr='(1-a)*b+c', a=outmask, b=oldoutput, c=med5, output='_yyy.fits', verbose='no') # add sky and object spectra back in iraf.imarith('_yyy.fits', "+", skymod, '_yyy.fits') # cleanup and get ready for next iteration if npix == 0: stop = yes # delete temp files iraf.imcopy('_yyy.fits', output + '[350:550,*]', verbose='no') delete(blk + "," + lapla + "," + deriv2 + "," + med5) delete(med3 + "," + med7 + "," + noise + "," + sigmap) delete(firstsel + "," + starreject) delete(finalsel + "," + inputmask) delete(oldoutput + "," + skymod + "," + galaxy) delete("_" + med3 + ",_" + sigmap) delete('_kernel' + "," + '_gkernel') delete(outmask) delete('_xxx.fits,_yyy.fits')
def lacos_spec(_input, output='clean.fits', outmask='mask.fits', gain=1.3, readn=9,\ xorder=9, yorder=3, sigclip=4.5, sigfrac=0.5, objlim=1, niter=4, instrument='kastr', verbose=True, interactive=False): # print "LOGX:: Entering `lacos` method/function in %(__file__)s" % # globals() import lickshane import sys import re import os import string from pyraf import iraf import numpy as np oldoutput, galaxy, skymod, med5 = 'oldoutput.fits', 'galaxy.fits', 'skymod.fits', 'med5.fits' blk, lapla, med3, med7, sub5, sigima, finalsel = 'blk.fits', 'lapla.fits', 'med3.fits', 'med7.fits', 'sub5.fits', 'sigima.fits', 'finalsel.fits' deriv2, noise, sigmap, firstsel, starreject = 'deriv2.fits', 'noise.fits', 'sigmap.fits', 'firstsel.fits', 'starreject.fits' inputmask = 'inputmask.fits' # set some parameters in standard IRAF tasks iraf.convolve.bilinear = 'no' iraf.convolve.radsym = 'no' # create Laplacian kernel # laplkernel = np.array([[0.0, -1.0, 0.0], [-1.0, 4.0, -1.0], [0.0, -1.0, 0.0]]) f = open('_kernel', 'w') f.write('0 -1 0;\n-1 4 -1;\n0 -1 0') f.close() # create growth kernel f = open('_gkernel', 'w') f.write('1 1 1;\n1 1 1;\n1 1 1') f.close() gkernel = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]) lickshane.util.delete(galaxy) lickshane.util.delete(skymod) lickshane.util.delete(oldoutput) if not output: output = _input else: os.system('cp ' + _input + ' ' + output) os.system('cp ' + _input + ' ' + oldoutput) arrayinput, headerinput = lickshane.cosmics.fromfits(oldoutput, verbose=False) lickshane.cosmics.tofits(outmask, np.float32(arrayinput - arrayinput), headerinput, verbose=False) if instrument in ['kastr']: axis1 = 1 axis2 = 2 elif instrument in ['kastb']: axis1 = 2 axis2 = 1 # subtract object spectra if desired if xorder > 0: iraf.fit1d(oldoutput, galaxy, "fit", axis=axis1, order=xorder, func="leg", low=4., high=4., nav=1, inter='no', sample="*", niter=3, grow=0, cursor="") iraf.imarith(oldoutput, "-", galaxy, oldoutput) else: lickshane.cosmics.tofits(galaxy, np.float32(arrayinput - arrayinput), headerinput, verbose=False) # Subtract sky lines if yorder > 0: iraf.fit1d(oldoutput, skymod, "fit", axis=axis2, order=yorder, func="leg", low=4., high=4., inter='no', sample="*", nav=1, niter=3, grow=0, cursor="") iraf.imarith(oldoutput, "-", skymod, oldoutput) else: lickshane.cosmics.tofits(skymod, np.float32(arrayinput - arrayinput), headerinput, verbose=False) arrayoldoutput, headeroldoutput = lickshane.cosmics.fromfits(oldoutput, verbose=False) # add object spectra to sky model iraf.imarith(skymod, "+", galaxy, skymod) ########### ## start iteration ########### ii = 0 while ii < niter: print ii # add median of residuals to sky model lickshane.util.delete(med5) iraf.median(oldoutput, med5, 5, 5, zlor='INDEF', zhir='INDEF', verbose='no') # m5 = ndimage.filters.median_filter(_inputarray, size=5, mode='mirror') iraf.imarith(skymod, "+", med5, med5) # take second-order derivative (Laplacian) of input image # kernel is convolved with subsampled image, in order to remove negative # pattern around high pixels lickshane.util.delete(blk) lickshane.util.delete(lapla) lickshane.util.delete(deriv2) lickshane.util.delete(noise) lickshane.util.delete(sigmap) iraf.blkrep(oldoutput, blk, 2, 2) iraf.convolve(blk, lapla, '_kernel') iraf.imreplace(lapla, 0, upper=0, lower='INDEF') iraf.blkavg(lapla, deriv2, 2, 2, option="average") # create noise model iraf.imutil.imexpr(expr='sqrt(a*' + str(gain) + '+' + str(readn) + '**2)/' + str(gain), a=med5, output=noise, verbose='no') iraf.imreplace(med5, 0.00001, upper=0, lower='INDEF') # divide Laplacian by noise model iraf.imutil.imexpr(expr='(a/b)/2', a=deriv2, b=noise, output=sigmap, verbose='no') # removal of large structure (bright, extended objects) lickshane.util.delete(med5) iraf.median(sigmap, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no') iraf.imarith(sigmap, "-", med5, sigmap) # find all candidate cosmic rays # this selection includes sharp features such as stars and HII regions arraysigmap, headersigmap = lickshane.cosmics.fromfits(sigmap, verbose=False) arrayf = np.where(arraysigmap < sigclip, 0, arraysigmap) arrayf = np.where(arrayf > 0.1, 1, arrayf) lickshane.cosmics.tofits(firstsel, np.float32(arrayf), headersigmap, verbose=False) # compare candidate CRs to median filtered image # this step rejects bright, compact sources from the initial CR list # subtract background and smooth component of objects lickshane.util.delete(med3) iraf.median(oldoutput, med3, 3, 3, zlo='INDEF', zhi='INDEF', verbose='no') lickshane.util.delete(med7) lickshane.util.delete('_' + med3) iraf.median(med3, med7, 7, 7, zlo='INDEF', zhi='INDEF', verbose='no') iraf.imutil.imexpr(expr='(a-b)/c', a=med3, b=med7, c=noise, output='_' + med3, verbose='no') iraf.imreplace('_' + med3, 0.01, upper=0.01, lower='INDEF') # compare CR flux to object flux lickshane.util.delete(starreject) iraf.imutil.imexpr(expr='(a*b)/c', a=firstsel, b=sigmap, c="_" + med3, output=starreject, verbose='no') # ###### ##### ###### ##### ###### FOUND A BUG ? # iraf.imutil.imexpr(expr='a+b+c', a=firstsel, b=sigmap, # c="_" + med3, output=starreject, verbose='no') # discard if CR flux <= objlim * object flux iraf.imreplace(starreject, 0, upper=objlim, lower='INDEF') iraf.imreplace(starreject, 1, lower=0.5, upper='INDEF') iraf.imarith(firstsel, "*", starreject, firstsel) # grow CRs by one pixel and check in original sigma map arrayfirst, headerfirst = lickshane.cosmics.fromfits(firstsel, verbose=False) arraygfirst = lickshane.cosmics.my_convolve_with_FFT2( arrayfirst, gkernel) arraygfirst = np.where(arraygfirst > 0.5, 1, arraygfirst) arraygfirst = arraygfirst * arraysigmap arraygfirst = np.where(arraygfirst < sigclip, 0, arraygfirst) arraygfirst = np.where(arraygfirst > 0.1, 1, arraygfirst) # grow CRs by one pixel and lower detection limit sigcliplow = sigfrac * sigclip # Finding neighbouring pixels affected by cosmic rays arrayfinal = lickshane.cosmics.my_convolve_with_FFT2( arraygfirst, gkernel) arrayfinal = np.where(arrayfinal > 0.5, 1, arrayfinal) arrayfinal = arrayfinal * arraysigmap arrayfinal = np.where(arrayfinal < sigcliplow, 0, arrayfinal) arrayfinal = np.where(arrayfinal > 0.1, 1, arrayfinal) # determine number of CRs found in this iteration arraygfirst = (1 - (arrayfinal - arrayfinal)) * arrayfinal npix = [str(int(np.size(np.where(arraygfirst > 0.5)) / 2.))] # create cleaned output image; use 3x3 median with CRs excluded arrayoutmask = np.where(arrayfinal > 1, 1, arrayfinal) lickshane.cosmics.tofits(outmask, np.float32(arrayoutmask), headerfirst, verbose=False) lickshane.util.delete(inputmask) arrayinputmask = (1 - (10000 * arrayoutmask)) * arrayoldoutput lickshane.cosmics.tofits(inputmask, np.float32(arrayinputmask), headerfirst, verbose=False) lickshane.util.delete(med5) iraf.median(inputmask, med5, 5, 5, zloreject=-9999, zhi='INDEF', verbose='no') iraf.imarith(outmask, "*", med5, med5) lickshane.util.delete(output) iraf.imutil.imexpr(expr='(1-a)*b+c', a=outmask, b=oldoutput, c=med5, output=output, verbose='no') lickshane.util.delete(oldoutput) os.system('cp ' + output + ' ' + oldoutput) # add sky and object spectra back in iraf.imarith(output, "+", skymod, output) # cleanup and get ready for next iteration ii = ii + 1 if npix == 0: ii = niter # delete temp files lickshane.util.delete(blk + "," + lapla + "," + deriv2 + "," + med5) lickshane.util.delete(med3 + "," + med7 + "," + noise + "," + sigmap) lickshane.util.delete(firstsel + "," + starreject) lickshane.util.delete(finalsel + "," + inputmask) lickshane.util.delete(oldoutput + "," + skymod + "," + galaxy) lickshane.util.delete("_" + med3 + ",_" + sigmap) lickshane.util.delete('_kernel' + "," + '_gkernel') lickshane.util.delete(outmask)
def lacos(_input0, output='clean.fits', outmask='mask.fits', gain=1.3, readn=9, xorder=9, yorder=9, sigclip=4.5, sigfrac=0.5, objlim=1, verbose=True, interactive=False): import floyds from floyds.util import delete from pyraf import iraf import numpy as np oldoutput, galaxy, skymod, med5 = 'oldoutput.fits', 'galaxy.fits', 'skymod.fits', 'med5.fits' blk, lapla, med3, med7, sub5, sigima, finalsel = 'blk.fits', 'lapla.fits', 'med3.fits', 'med7.fits', 'sub5.fits',\ 'sigima.fits', 'finalsel.fits' deriv2, noise, sigmap, firstsel, starreject = 'deriv2.fits', 'noise.fits', 'sigmap.fits', 'firstsel.fits',\ 'starreject.fits' inputmask = 'inputmask.fits' # set some parameters in standard IRAF tasks iraf.convolve.bilinear = 'no' iraf.convolve.radsym = 'no' # create Laplacian kernel # laplkernel = np.array([[0.0, -1.0, 0.0], [-1.0, 4.0, -1.0], [0.0, -1.0, 0.0]]) f = open('_kernel', 'w') f.write('0 -1 0;\n-1 4 -1;\n0 -1 0') f.close() # create growth kernel f = open('_gkernel', 'w') f.write('1 1 1;\n1 1 1;\n1 1 1') f.close() gkernel = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]) delete(galaxy) delete(skymod) delete(oldoutput) if not output: output = _input0 else: delete(output) iraf.imcopy(_input0, output, verbose='no') delete('_xxx.fits,_yyy.fits') iraf.imcopy(_input0 + '[*,10:80]', '_xxx.fits', verbose='no') _input = '_xxx.fits' arrayinput, headerinput = floyds.cosmics.fromfits(_input, verbose=False) floyds.cosmics.tofits(outmask, np.float32(arrayinput - arrayinput), headerinput, verbose=False) # subtract object spectra if desired # iraf.fit1d(_input,galaxy,"fit",axis=2,order=5,func="leg",low=4., # high=4.,nav=1,inter='yes',sample="*",niter=3,grow=0,cursor="") # iraf.imarith(_input,"-",galaxy,oldoutput) # iraf.display(oldoutput,1,fill='yes') ####### #Subtract sky lines # iraf.fit1d(oldoutput,skymod,"fit",axis=2,order=5,func="leg",low=4.,high=4., # inter='no',sample="*",nav=1,niter=3,grow=0,cursor="") # iraf.imarith(oldoutput,"-",skymod,oldoutput) # iraf.display(oldoutput,2,fill='yes') ##### iraf.imcopy(_input, oldoutput) arrayoldoutput, headeroldoutput = floyds.cosmics.fromfits(oldoutput, verbose=False) # add object spectra to sky model # iraf.imarith(skymod,"+",galaxy,skymod) delete(med5) # add median of residuals to sky model iraf.median(oldoutput, med5, 5, 5, zlor='INDEF', zhir='INDEF', verbose='no') # m5 = ndimage.filters.median_filter(_inputarray, size=5, mode='mirror') # iraf.imarith(skymod,"+",med5,med5) # take second-order derivative (Laplacian) of input image # kernel is convolved with subsampled image, in order to remove negative # pattern around high pixels delete(blk) delete(lapla) iraf.blkrep(oldoutput, blk, 2, 2) iraf.convolve(blk, lapla, '_kernel') iraf.imreplace(lapla, 0, upper=0, lower='INDEF') delete(deriv2) delete(noise) iraf.blkavg(lapla, deriv2, 2, 2, option="average") # create noise model iraf.imutil.imexpr(expr='sqrt(a*' + str(gain) + '+' + str(readn) + '**2)/' + str(gain), a=med5, output=noise, verbose='no') iraf.imreplace(med5, 0.00001, upper=0, lower='INDEF') # divide Laplacian by noise model delete(sigmap) iraf.imutil.imexpr(expr='(a/b)/2', a=deriv2, b=noise, output=sigmap, verbose='no') # removal of large structure (bright, extended objects) delete(med5) iraf.median(sigmap, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no') iraf.imarith(sigmap, "-", med5, sigmap) # find all candidate cosmic rays # this selection includes sharp features such as stars and HII regions arraysigmap, headersigmap = floyds.cosmics.fromfits(sigmap, verbose=False) arrayf = np.where(arraysigmap < sigclip, 0, arraysigmap) arrayf = np.where(arrayf > 0.1, 1, arrayf) floyds.cosmics.tofits(firstsel, np.float32(arrayf), headersigmap, verbose=False) # compare candidate CRs to median filtered image # this step rejects bright, compact sources from the initial CR list # subtract background and smooth component of objects delete(med3) iraf.median(oldoutput, med3, 3, 3, zlo='INDEF', zhi='INDEF', verbose='no') delete(med7) delete('_' + med3) iraf.median(med3, med7, 7, 7, zlo='INDEF', zhi='INDEF', verbose='no') iraf.imutil.imexpr(expr='(a-b)/c', a=med3, b=med7, c=noise, output='_' + med3, verbose='no') iraf.imreplace('_' + med3, 0.01, upper=0.01, lower='INDEF') # compare CR flux to object flux delete(starreject) iraf.imutil.imexpr(expr='a+b+c', a=firstsel, b=sigmap, c="_" + med3, output=starreject, verbose='no') # discard if CR flux <= objlim * object flux iraf.imreplace(starreject, 0, upper=objlim, lower='INDEF') iraf.imreplace(starreject, 1, lower=0.5, upper='INDEF') iraf.imarith(firstsel, "*", starreject, firstsel) # grow CRs by one pixel and check in original sigma map arrayfirst, headerfirst = floyds.cosmics.fromfits(firstsel, verbose=False) arraygfirst = floyds.cosmics.my_convolve_with_FFT2(arrayfirst, gkernel) arraygfirst = np.where(arraygfirst > 0.5, 1, arraygfirst) arraygfirst = arraygfirst * arraysigmap arraygfirst = np.where(arraygfirst < sigclip, 0, arraygfirst) arraygfirst = np.where(arraygfirst > 0.1, 1, arraygfirst) # grow CRs by one pixel and lower detection limit sigcliplow = sigfrac * sigclip # Finding neighbouring pixels affected by cosmic rays arrayfinal = floyds.cosmics.my_convolve_with_FFT2(arraygfirst, gkernel) arrayfinal = np.where(arrayfinal > 0.5, 1, arrayfinal) arrayfinal = arrayfinal * arraysigmap arrayfinal = np.where(arrayfinal < sigcliplow, 0, arrayfinal) arrayfinal = np.where(arrayfinal > 0.1, 1, arrayfinal) # determine number of CRs found in this iteration arraygfirst = (1 - (arrayfinal - arrayfinal)) * arrayfinal npix = [str(int(np.size(np.where(arraygfirst > 0.5)) / 2.))] # create cleaned output image; use 3x3 median with CRs excluded arrayoutmask = np.where(arrayfinal > 1, 1, arrayfinal) floyds.cosmics.tofits(outmask, np.float32(arrayoutmask), headerfirst, verbose=False) delete(inputmask) arrayinputmask = (1 - (10000 * arrayoutmask)) * arrayoldoutput floyds.cosmics.tofits(inputmask, np.float32(arrayinputmask), headerfirst, verbose=False) delete(med5) iraf.median(inputmask, med5, 5, 5, zloreject=-9999, zhi='INDEF', verbose='no') iraf.imarith(outmask, "*", med5, med5) delete('_yyy.fits') iraf.imutil.imexpr(expr='(1-a)*b+c', a=outmask, b=oldoutput, c=med5, output='_yyy.fits', verbose='no') # add sky and object spectra back in #iraf.imarith('_yyy.fits',"+",skymod,'_yyy.fits') # cleanup and get ready for next iteration if npix == 0: stop = yes # delete temp files iraf.imcopy('_yyy.fits', output + '[*,10:80]', verbose='no') delete(blk + "," + lapla + "," + deriv2 + "," + med5) delete(med3 + "," + med7 + "," + noise + "," + sigmap) delete(firstsel + "," + starreject) delete(finalsel + "," + inputmask) delete(oldoutput + "," + skymod + "," + galaxy) delete("_" + med3 + ",_" + sigmap) delete('_kernel' + "," + '_gkernel') delete(outmask) delete('_xxx.fits,_yyy.fits')
def irafflatten(): os.system('cp /Users/rfinn/clusters/spitzer/flatsexfiles/* .') infile=open('InputImageList.txt','r') outfile=open('FlatImageList.txt','w') sky=[] for line in infile: im=line[0:(len(line)-1)] mask='mask'+im skyim='s'+im outline='f'+line iraf.imgets(im,'DRIBKGND') t=iraf.imgets.value sky.append(float(t)) outfile.write(outline) #get object positions using sextractor iraf.imarith(im,'-',t,skyim)#subtract sky before running sextractor (otherwise it doesn't detect any objects - don't know why...) s='sex '+skyim os.system(s) x=[] y=[] catfile=open('test.cat','r') for line in catfile: if line.find('#') > -1: continue t=line.split() x.append(float(t[10])) y.append(float(t[11])) catfile.close() x=N.array(x,'f') y=N.array(y,'f') try:#create image of ones same size as image iraf.imarith(im,'/',im,'mask') except: iraf.imdelete('mask') iraf.imarith(im,'/',im,'mask') print "masking objects" for j in range(len(x)): #mask objects and radius around each position using imreplace, radius=10 pix for k in range(11): y1=int(y[j]+5-k) if y1 < 1: continue if y1 > 128: continue xmin=int(x[j]-5) if xmin < 1: xmin=1 xmax=int(x[j]+5) if xmax > 128: xmax=128 s='mask['+str(xmin)+':'+str(xmax)+","+str(y1)+":"+str(y1)+"]" iraf.imreplace(s,0.) iraf.imrename('mask',mask) print "updating BPM field in header" iraf.hedit(im,fields='BPM',value=mask,add='yes',verify='no',update='yes') outfile.close() infile.close() avesky=N.average(N.array(sky,'f')) lthresh=avesky-1. hthresh=avesky+.6 iraf.imcombine('@InputImageList.txt','flat',combine='average',reject='ccdclip',scale='none',zero='mode',lthreshold=lthresh,hthreshold=hthresh,lsigma=2.,hsigma=2.,rdnoise=5.,gain=5.,blank=1.,grow=12.,masktype='badvalue',maskvalue='0') t=iraf.imstat('flat',fields='mean',format='no',Stdout=1) ave=float(t[0]) iraf.imarith('flat','/',ave,'nflat') iraf.imarith('@InputImageList.txt','/','nflat','@FlatImageList.txt')
def subract(df_image, psf): iraf.imdel('_model*.fits') iraf.imdel('_res*.fits') iraf.imdel('_df_sub') 'subtract the sky value from the dragonfly image header' df_backval = fits.getheader(df_image)['BACKVAL'] iraf.imarith('%s' % df_image, '-', '%s' % df_backval, '_df_sub') 'convolve the model with the Dragonfly PSF' if usemodelpsf: makeallisonspsf() psf = './psf/psf_static_fullframe.fits' ### XXX resample the PSF by a factor of 4 if verbose: print 'VERBOSE: Using %s for the psf convolution.' % psf iraf.stsdas.analysis.fourier.fconvolve('_fluxmod_dragonfly', '%s' % psf, '_model') 'shift the images so they have the same physical coordinates' iraf.stsdas.analysis.dither.crossdriz('_df_sub.fits', '_model.fits', 'cc_images', dinp='no', dref='no') iraf.stsdas.analysis.dither.shiftfind('cc_images.fits', 'shift_values') x_shift = 0 y_shift = 0 with open('shift_values', 'r') as datafile: line = datafile.read().split() x_shift = float(line[2]) y_shift = float(line[4]) print('The shift in x and y are: ' + str(x_shift) + ',' + str(y_shift)) iraf.imshift('_model', '_model_sh', 0 - x_shift, 0 - y_shift) 'scale the model so that roughly the same as the _df_sub image' if usemodelpsf: iraf.imarith( '_model_sh', '/', 16., '_model_sc' ) ## just trying to match it to the original (below) approximately else: iraf.imarith( '_model_sh', '/', 2422535.2, '_model_sc' ) ## difference between the flux of the star used to make the psf in both frames iraf.imarith('_model_sc', '*', 1.5, '_model_sc') 'subtract the model frm the dragonfly cutout' iraf.imarith('_df_sub', '-', '_model_sc', '_res') '????' iraf.imcopy('_model_sc', '_model_mask') iraf.imreplace('_model_mask.fits', 0, upper=50) iraf.imreplace('_model_mask.fits', 1, lower=0.01) iraf.boxcar('_model_mask', '_model_maskb', 5, 5) iraf.imreplace('_model_maskb.fits', 1, lower=0.1) iraf.imreplace('_model_maskb.fits', 0, upper=0.9) iraf.imarith(1, '-', '_model_maskb', '_model_maskb') iraf.imarith('_model_maskb', '*', '_res', '_res_final') return None
r = 555. #template image template = 'qopen0004.fits' try: iraf.imarith(template, '/', template, 'mask') except: iraf.imdelete('mask') iraf.imarith(template, '/', template, 'mask') for y in range(1, yc): xmax = int(512. - N.sqrt(r**2 - (1. * y - yc)**2)) if xmax < 1.: break s = 'mask[1:' + str(xmax) + "," + str(y) + ":" + str(y) + "]" print y, s iraf.imreplace(s, 0.) x1 = int(imxmax - xmax) x2 = int(imxmax) s = 'mask[' + str(x1) + ':' + str(x2) + ',' + str(y) + ':' + str(y) + ']' print y, s iraf.imreplace(s, 0.) y1 = int(imymax - y + 1) y2 = int(imymax) s = 'mask[1:' + str(xmax) + "," + str(y1) + ":" + str(y1) + "]" print y, s iraf.imreplace(s, 0.) s = 'mask[' + str(x1) + ':' + str(x2) + ',' + str(y1) + ':' + str(y1) + ']' print y, s iraf.imreplace(s, 0.) iraf.display('mask', 2)
def imrep(inimage): iraf.imreplace(inimage, value='1')
def prep(df_image,hi_res_image,width_mask=1.5,unmaskgal=False,galvalues = None): print "\n************ Running the preparation steps ************\n" iraf.imdel('_mask.fits') iraf.imdel('_fluxmod_cfht*.fits') iraf.imdel('_df_4*.fits') 'run SExtractor to get bright sources that are easily detected in the high resolution data' subprocess.call('sex %s'%hi_res_image,shell=True) ##### Add in option to change sextractor threshold detect_thresh and analysis_thresh 2/3 'copy the segmentation map to a mask' iraf.imcopy('seg.fits','_mask.fits') ##### Add step to get rid of diffraction spikes if unmaskgal: 'Run SExtractor to get values for the central galaxy' print '\nDoing a second sextractor run to unmask central galaxy. \n' analysis_thresh_lg=2;back_size_lg=128;detect_thresh_lg=2;detect_minarea_lg=60 segname = run_SExtractor(hi_res_image,detect_thresh=detect_thresh_lg,analysis_thresh=analysis_thresh_lg,back_size=back_size_lg,detect_minarea=detect_minarea_lg) print '\nSegmentation map is named: '+segname 'Open up the data' seg2data = fits.getdata(segname) segdata,segheader = fits.getdata('_mask.fits',header=True) 'Detect sources from the segmentation map' from photutils import detect_sources segrefdata = detect_sources(seg2data, 3, npixels=5)#, filter_kernel=kernel) segrefdata = segrefdata.data segrefname = re.sub('.fits','_ds.fits',segname) writeFITS(segrefdata,segheader,segrefname) print '\nSource separated segmentation map is named: '+segrefname 'Use detected source seg map to mask galaxies' #galvalues = [3531,5444,5496] print 'Unmask the galaxies in the mask from the original segmap. \n' segdatanew = unmaskgalaxy(segdata,'_mask.fits',segrefname=segrefname,segref=segrefdata,galvalues=galvalues) writeFITS(segdatanew,segheader,'_mask.fits') if verbose: print_verbose_string('Carrying on') 'replace the values in the segments in the segmentation map (i.e. stars) all to 1, all the background is still 0' iraf.imreplace('_mask.fits',1,lower=0.5) 'multiply the mask (with 1s at the stars) by the high res image to get the star flux back - now have the flux model' iraf.imarith('_mask.fits','*','%s'%hi_res_image,'_fluxmod_cfht') 'smooth the flux model' 'increase size of mask, so more is subtracted' 'the "1.5" in the next line should be a user-defined parameter that is given' 'to the script; it controls how much of the low surface brightness' 'emission in the outskirts of galaxies is subtracted. This choice' 'depends on the science application' iraf.gauss('_fluxmod_cfht','_fluxmod_cfht_smoothed',width_mask,nsigma=4.) iraf.imreplace('_fluxmod_cfht_smoothed', -1, lower=0, upper=0) iraf.imreplace('_fluxmod_cfht_smoothed', 1, lower=-0.5) iraf.imreplace('_fluxmod_cfht_smoothed', 0, upper=-0.5) iraf.imarith('%s'%hi_res_image, '*','_fluxmod_cfht_smoothed', '_fluxmod_cfht_new') ' this is the key new step! we"re registering the CFHT image' ' to a frame that is 4x finer sampled than the Dragonfly image.' ' this avoids all the pixelation effects we had before.' ' (4x seems enough; but we could have it as a free parameter - need' ' to be careful as it occurs elsewhere in the scripts too) ' iraf.blkrep('%s'%df_image,'_df_4',4,4) 'register the flux model onto the same pixel scale as the dragonfly image' iraf.wregister('_fluxmod_cfht_new','_df_4','_fluxmod_dragonfly',interpo='linear',fluxcon='yes') return None
def stacking(cllist,zpofflist,ref,zprefoff=0.0,stackname='stack',shiftsize=400): """ """ #Reset the IRAF tasks used in this routine. iraf.unlearn('imcalc') iraf.unlearn('imcombine') iraf.unlearn('imreplace') iraf.unlearn('xyxymatch') iraf.unlearn('geomap') iraf.unlearn('geotran') iraf.unlearn('imcopy') #Find reference image in reference directory. Check to make #sure that it is actually the image and not the mask file! #Grab the mask for adding to the mask list now. (refimg,refmask,expmap)=classify(ref+'/tu*.fits') zpref=pf.open(refimg)[0].header['MAGZERO'] # zprefoff=NewfirmZPoffset[ref.split('/')[-1]] zprefoff=float(zprefoff) #Get 2MASS PSC positions for reference cluster image. catalog=get2masspsc(refimg) foo=file_check(ref+'/2mass_ref_stars.cdt',delete=True) foo=open(ref+'/2mass_ref_stars.cdt','w') for y in catalog: data=y.split() foo.write(data[6]+'\t'+data[7]+'\n') foo.close() #Create lists for files to be input into the stacking routine. foo=file_check('matchlist',delete=True) foo=file_check('scalelist',delete=True) foo=file_check('shiftlist',delete=True) foo=file_check('masklist',delete=True) foo=file_check('shiftmask',delete=True) foo=file_check('expmaplist',delete=True) (matchlist,scalelist,shiftlist,masklist, shiftmask,finalmasks,stacklist,stackmask, finalmasks2,expmaplist,shiftexp,expmaplist2)=(open('matchlist','w'),open('scalelist','w'), open('shiftlist','w'),open('masklist','w'), open('shiftmask','w'),open('finalmasks','w'), open('stacklist','w'),open('stackmask','w'), open('finalmasks2','w'),open('expmaplist','w'), open('shiftexp','w'),open('expmaplist2','w')) (xsize,ysize)=(np.array([]),np.array([])) #Step through all of the input cluster directories. i=0 for x in cllist: #Find the image, mask, and exposure map files. Get zeropoints and #scale image to the reference image. scaleimg=x+'/scaled_to_'+ref.split('/')[-1]+'.fits' foo=file_check(scaleimg,delete=True) (img,mask,expmap)=classify(x+'/tu*.fits') imgzp=pf.open(img)[0].header['MAGZERO'] (xs,ys)=(pf.open(img)[0].header['NAXIS1'],pf.open(img)[0].header['NAXIS2']) (xsize,ysize)=(np.append(xsize,xs),np.append(ysize,ys)) imgzpoff=float(zpofflist[i]) # imgzpoff=NewfirmZPoffset[x.split('/')[-1]] scale=scalecounts(imgzp+imgzpoff,zpref+zprefoff) iraf.imcalc(img,scaleimg,'im1*'+str(scale)) #Get X,Y pixel positions of 2MASS sources from the 2MASS PSC #in the image. Use these to compute shifts relative to the #reference image using IRAF task geomap. foo=file_check(x+'/2mass_ref_stars.cdt',delete=True) foo=open(x+'/2mass_ref_stars.cdt','w') catalog=get2masspsc(scaleimg) for y in catalog: data=y.split() foo.write(data[6]+'\t'+data[7]+'\n') foo.close() #Match the 2MASS PSC positions with stars in the reference #image using xyxymatch. The matched source list is then fed #into geomap to get the X and Y shifts. foo=file_check(x+'/2mass_matched.cdt',delete=True) iraf.xyxymatch(x+'/2mass_ref_stars.cdt',ref+'/2mass_ref_stars.cdt', x+'/2mass_matched.cdt','200.0',verbose='no') #Append all of the names of the files for the input and output filename #lists to be passed to IRAF tasks further down the line. matchlist.write(x+'/2mass_matched.cdt\n') scalelist.write(scaleimg+'\n') foo=file_check(x+'/scaled_and_shifted.fits',delete=True) shiftlist.write(x+'/scaled_and_shifted.fits['+str(shiftsize)+':'+\ str(int(np.max(xsize))+shiftsize)+','+str(shiftsize)+':'+\ str(int(np.max(ysize))+shiftsize)+']\n') stacklist.write(x+'/scaled_and_shifted.fits\n') file_check(x+'/mask_tmp.fits',delete=True) file_check(x+'/expmap_tmp.fits',delete=True) iraf.imarith(mask+'[1]','*',1000.0,x+'/mask_tmp.fits',pixtype='real') iraf.imarith(expmap+'[1]','*',1.0,x+'/expmap_tmp.fits',pixtype='real') offset=2.558435 file_check(x+'/mask_tmp2.fits',delete=True) iraf.imcalc(x+'/mask_tmp.fits',x+'/mask_tmp2.fits','im1+'+str(offset)) os.remove(x+'/mask_tmp.fits') masklist.write(x+'/mask_tmp2.fits\n') file_check(x+'/mask_shift.fits',delete=True) shiftmask.write(x+'/mask_shift.fits['+str(shiftsize)+':'+\ str(int(np.max(xsize))+shiftsize)+','+str(shiftsize)+':'+\ str(int(np.max(ysize))+shiftsize)+']\n') stackmask.write(x+'/mask_shift.fits\n') finalmasks.write(x+'/mask_final.fits\n') finalmasks2.write(x+'/mask_final.fits[0]\n') expmaplist.write(x+'/expmap_tmp.fits[0]\n') shiftexp.write(x+'/expmap_shift.fits['+str(shiftsize)+':'+\ str(int(np.max(xsize))+shiftsize)+','+str(shiftsize)+':'+\ str(int(np.max(ysize))+shiftsize)+']\n') expmaplist2.write(x+'/expmap_shift.fits\n') i += 1 #Close all of the input and output filename lists to be passed to IRAF tasks. matchlist.close() scalelist.close() stacklist.close() masklist.close() shiftmask.close() finalmasks.close() shiftlist.close() stackmask.close() finalmasks2.close() expmaplist.close() expmaplist2.close() shiftexp.close() #Get the shifts between all input files (including the reference) and the #reference image itself. foo=file_check('shift.db',delete=True) iraf.geomap('@matchlist','shift.db',1.0,np.max(xsize), 1.0,np.max(ysize),fitgeometry='shift',interactive='no', maxiter=2,function='legendre',verbose='no') #Shift the input images (including the reference) and associated mask files #to a common pixel grid. Add some padding around the individual frames (-99 #in the images, 1 in the bad pixel masks) to ensure that the images will #combine properly. (maxx,maxy)=(np.max(xsize)+shiftsize+100.0,np.max(ysize)+shiftsize+100.0) iraf.geotran('@scalelist','@shiftlist','shift.db','@matchlist',geometry='linear', boundary='constant',nlines=maxy,ncols=maxx,constant=-99.0) iraf.geotran('@masklist','@shiftmask','shift.db','@matchlist',geometry='linear', boundary='constant',nlines=maxy,ncols=maxx,constant=1000.0, nxblock=10000,nyblock=10000) iraf.geotran('@expmaplist','@shiftexp','shift.db','@matchlist',geometry='linear', boundary='constant',nlines=maxy,ncols=maxx,constant=0.) for x in cllist: file_check(x+'/mask_final.fits',delete=True) shutil.copy(x+'/mask_shift.fits',x+'/mask_final.fits') iraf.hedit(x+'/scaled_and_shifted.fits[0]','BPM',x+'/mask_final.fits[0]', add='yes',update='yes',verify='no') iraf.imreplace('@finalmasks2',0,upper=offset) iraf.imreplace('@finalmasks2',1,lower=offset) file_check(stackname,delete=True) file_check(stackname[:-5]+'_mask.pl',delete=True) file_check(stackname[:-5]+'_expmap.fits',delete=True) iraf.imcombine('@stacklist',stackname,bpmasks=stackname[:-5]+'_bpm', masktype='goodval',reject='none',mclip='yes',lthresh='INDEF',hthresh='INDEF', hsigma=10.0,lsigma='INDEF',nrejmasks=stackname[:-5]+'_nrej', sigmas=stackname[:-5]+'_sigma',grow=2.5,nkeep=1,blank=-99.0,gain=8.0,rdnoise=35.0) iraf.imcombine('@expmaplist2',stackname[:-5]+'_expmap.fits',combine='sum') hdu=pf.open(stackname,mode='update') hdu[0].header['BPM']=stackname.split('/')[-1][:-5]+'_mask.pl' hdu[0].header['MAGZERO']=zpref+zprefoff hdu.close() #Fix the WCS information in the stacked image. copyhead(stackname,refimg,offset=shiftsize) applywcs(stackname,stackname[:-5]+'_wcs.fits') trash=['matchlist','scalelist','shiftlist','masklist','shiftmask','finalmasks', 'shift.db','stacklist','finalmasks2','stackmask','tmp_wcs.fits','expmaplist', 'expmaplist2','shiftexp'] for x in trash: os.remove(x)
def deimos_standard(standard): '''Extract standard star and calculate sensitivit function.''' bstd = "%s_01_B" % standard rstd = "%s_01_R" % standard # Extract standard if get_head("%s.fits" % bstd, "SKYSUB"): iraf.apall(bstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none") iraf.apall(rstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none") else: iraf.apall(bstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit") iraf.apall(rstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit") # Fit the continuum #iraf.continuum("%s.ms" % bstd, output="s%s.ms" % bstd, lines=1, bands=1, # type="fit", sample="*", naverage=1, function="chebyshev", # order=15, low_reject=3.0, high_reject=5.0, niterate=10, grow=1.0, # interac=yes) #iraf.continuum("%s.ms" % rstd, output="s%s.ms" % rstd, lines=1, bands=1, # type="fit", sample="*", naverage=1, function="chebyshev", # order=15, low_reject=3.0, high_reject=5.0, niterate=10, grow=1.0, # interac=yes) # Create telluric iraf.splot("%s.ms" % bstd) # Remove telluric and absorption, save as a%s.ms iraf.sarith("%s.ms" % bstd, "/", "a%s.ms" % bstd, "telluric.B.%s.fits" % bstd) iraf.imreplace("telluric.B.%s.fits" % bstd, 1.0, lower=0.0, upper=0.0) iraf.splot("telluric.B.%s.fits" % bstd) # Remove stellar features and resave iraf.splot("%s.ms" % rstd) # Remove telluric and absorption, save as a%s.ms iraf.sarith("%s.ms" % rstd, "/", "a%s.ms" % rstd, "telluric.R.%s.fits" % rstd) iraf.imreplace("telluric.R.%s.fits" % rstd, 1.0, lower=0.0, upper=0.0) iraf.splot("telluric.R.%s.fits" % rstd) # Remove stellar features and resave # Create smoothed standard iraf.gauss("a%s.ms[*,1,1]" % bstd, "s%s.ms" % bstd, 5.0) iraf.sarith("%s.ms" % bstd, "/", "s%s.ms" % bstd, "ds%s.ms" % bstd) iraf.gauss("a%s.ms[*,1,1]" % rstd, "s%s.ms" % rstd, 5.0) iraf.sarith("%s.ms" % rstd, "/", "s%s.ms" % rstd, "ds%s.ms" % rstd) # Divide through by smoothed standard #iraf.sarith("%s.ms" % bstd, "/", "s%s.ms" % bstd, "ds%s.ms" % bstd) #iraf.sarith("%s.ms" % rstd, "/", "s%s.ms" % rstd, "ds%s.ms" % rstd) # Create and apply telluric correction #iraf.splot("%s.ms" % bstd) # Remove telluric, save as a%s.ms #iraf.splot("%s.ms" % rstd) # Remove telluric, save as a%s.ms #iraf.sarith("%s.ms" % bstd, "/", "a%s.ms" % bstd, "telluric.B.fits") #iraf.sarith("%s.ms" % rstd, "/", "a%s.ms" % rstd, "telluric.R.fits") #iraf.imreplace("telluric.B.fits", 1.0, lower=0.0, upper=0.0) #iraf.imreplace("telluric.R.fits", 1.0, lower=0.0, upper=0.0) iraf.telluric("ds%s.ms" % bstd, "tds%s.ms" % bstd, "telluric.B.%s.fits" % bstd, xcorr=no, tweakrms=no, interactive=no, sample='6850:6950,7575:7700') iraf.telluric("ds%s.ms" % rstd, "tds%s.ms" % rstd, "telluric.R.%s.fits" % rstd, xcorr=no, tweakrms=no, interactive=no, sample='6850:6950,7575:7700') # Define bandpasses for standard star calculation iraf.standard("tds%s.ms" % bstd, "%s.B.std" % standard, extinction='home$extinct/maunakeaextinct.dat', caldir='home$standards/', observatory='Keck', interac=yes, star_name=standard, airmass='', exptime='') iraf.standard("tds%s.ms" % rstd, "%s.R.std" % standard, extinction='home$extinct/maunakeaextinct.dat', caldir='home$standards/', observatory='Keck', interac=yes, star_name=standard, airmass='', exptime='') # Determine sensitivity function iraf.sensfunc('%s.B.std' % standard, '%s.B.sens' % standard, extinction='home$extinct/maunakeaextinct.dat', newextinction='extinct.dat', observatory='Keck', function='legendre', order=4, interactive=yes) iraf.sensfunc('%s.R.std' % standard, '%s.R.sens' % standard, extinction='home$extinct/maunakeaextinct.dat', newextinction='extinct.dat', observatory='Keck', function='legendre', order=4, interactive=yes) return
def deimos_standard(standard): '''Extract standard star and calculate sensitivit function.''' bstd = "%s_01_B" % standard; rstd = "%s_01_R" % standard # Extract standard if get_head("%s.fits" % bstd, "SKYSUB"): iraf.apall(bstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none") iraf.apall(rstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none") else: iraf.apall(bstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit") iraf.apall(rstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit") # Fit the continuum #iraf.continuum("%s.ms" % bstd, output="s%s.ms" % bstd, lines=1, bands=1, # type="fit", sample="*", naverage=1, function="chebyshev", # order=15, low_reject=3.0, high_reject=5.0, niterate=10, grow=1.0, # interac=yes) #iraf.continuum("%s.ms" % rstd, output="s%s.ms" % rstd, lines=1, bands=1, # type="fit", sample="*", naverage=1, function="chebyshev", # order=15, low_reject=3.0, high_reject=5.0, niterate=10, grow=1.0, # interac=yes) # Create telluric iraf.splot("%s.ms" % bstd) # Remove telluric and absorption, save as a%s.ms iraf.sarith("%s.ms" % bstd, "/", "a%s.ms" % bstd, "telluric.B.%s.fits" % bstd) iraf.imreplace("telluric.B.%s.fits" % bstd, 1.0, lower=0.0, upper=0.0) iraf.splot("telluric.B.%s.fits" % bstd) # Remove stellar features and resave iraf.splot("%s.ms" % rstd) # Remove telluric and absorption, save as a%s.ms iraf.sarith("%s.ms" % rstd, "/", "a%s.ms" % rstd, "telluric.R.%s.fits" % rstd) iraf.imreplace("telluric.R.%s.fits" % rstd, 1.0, lower=0.0, upper=0.0) iraf.splot("telluric.R.%s.fits" % rstd) # Remove stellar features and resave # Create smoothed standard iraf.gauss("a%s.ms[*,1,1]" % bstd, "s%s.ms" % bstd, 5.0) iraf.sarith("%s.ms" % bstd, "/", "s%s.ms" % bstd, "ds%s.ms" % bstd) iraf.gauss("a%s.ms[*,1,1]" % rstd, "s%s.ms" % rstd, 5.0) iraf.sarith("%s.ms" % rstd, "/", "s%s.ms" % rstd, "ds%s.ms" % rstd) # Divide through by smoothed standard #iraf.sarith("%s.ms" % bstd, "/", "s%s.ms" % bstd, "ds%s.ms" % bstd) #iraf.sarith("%s.ms" % rstd, "/", "s%s.ms" % rstd, "ds%s.ms" % rstd) # Create and apply telluric correction #iraf.splot("%s.ms" % bstd) # Remove telluric, save as a%s.ms #iraf.splot("%s.ms" % rstd) # Remove telluric, save as a%s.ms #iraf.sarith("%s.ms" % bstd, "/", "a%s.ms" % bstd, "telluric.B.fits") #iraf.sarith("%s.ms" % rstd, "/", "a%s.ms" % rstd, "telluric.R.fits") #iraf.imreplace("telluric.B.fits", 1.0, lower=0.0, upper=0.0) #iraf.imreplace("telluric.R.fits", 1.0, lower=0.0, upper=0.0) iraf.telluric("ds%s.ms" % bstd, "tds%s.ms" % bstd, "telluric.B.%s.fits" % bstd, xcorr=no, tweakrms=no, interactive=no, sample='6850:6950,7575:7700') iraf.telluric("ds%s.ms" % rstd, "tds%s.ms" % rstd, "telluric.R.%s.fits" % rstd, xcorr=no, tweakrms=no, interactive=no, sample='6850:6950,7575:7700') # Define bandpasses for standard star calculation iraf.standard("tds%s.ms" % bstd, "%s.B.std" % standard, extinction='home$extinct/maunakeaextinct.dat', caldir='home$standards/', observatory='Keck', interac=yes, star_name=standard, airmass='', exptime='') iraf.standard("tds%s.ms" % rstd, "%s.R.std" % standard, extinction='home$extinct/maunakeaextinct.dat', caldir='home$standards/', observatory='Keck', interac=yes, star_name=standard, airmass='', exptime='') # Determine sensitivity function iraf.sensfunc('%s.B.std' % standard, '%s.B.sens' % standard, extinction='home$extinct/maunakeaextinct.dat', newextinction='extinct.dat', observatory='Keck', function='legendre', order=4, interactive=yes) iraf.sensfunc('%s.R.std' % standard, '%s.R.sens' % standard, extinction='home$extinct/maunakeaextinct.dat', newextinction='extinct.dat', observatory='Keck', function='legendre', order=4, interactive=yes) return
def mkcombmask(combimg, inlist, sspref='sdfr', outpref='mc', minpix=250, hsigma=3, lsigma=10, conv='block 3 3', bpm='none', sigmap='none'): # load IRAF package iraf.nproto() # chceck combined image if not os.access(combimg, os.R_OK): print >> sys.stderr, 'cannot open combined image (%s)' % combimg return 1 # open input list and check if it exists inimg_arr = check_input(inlist, sspref) if isinstance(inimg_arr, int): return 1 # check output images outimg_arr = check_outpref(outpref, inimg_arr) if isinstance(outimg_arr, int): return 1 if lsigma <= 0: print >> sy.stderr, 'invalid lsigma value (%f)' % lsigma return 1 if hsigma <= 0: print >> sys.stderr, 'invalid hsigma value (%f)' % hsigma return 1 # check convolution kernel name ret = 1 param = conv.split() if len(param) == 1: if os.access(param[0], os.R_OK): conv = '@' + conv ret = 0 else: print >> sys.stderr, 'convolution kernel file (%s) does not exist' % param[ 0] elif param[0] == 'block' and len(param) == 3: if param[1].isdigit() and param[2].isdigit(): ret = 0 elif param[0] == 'bilinear' and len(param) == 3: if param[1].isdigit() and param[2].isdigit() and int( param[1]) > 0 and int(param[2]) > 0: ret = 0 elif param[0] == 'gauss' and len(param) == 5: if param[1].isdigit() and param[2].isdigit() and int( param[1]) > 0 and int(param[2]) > 0: if isfloat(param[3]) and isfloat( param[4]) and float(param[3]) > 0 and float(param[4]) > 0: ret = 0 if ret > 0: print >> sys.stderr, 'invalid convolve parameter (%s)' % conv 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() # check sigma map if sigmap.lower() == 'none': tmp_sigma = '' tmp_mask = '' else: if os.access(sigmap, os.R_OK) == False: print >> sys.stderr, 'cannot read sigma map image (%s)' % sigmap return 1 else: tmp_mask = tmp_prefix + 'mask' + os.path.basename(sigmap) tmp_sigma = tmp_prefix + 'sigma' + os.path.basename(sigmap) iraf.imcopy(sigmap, tmp_sigma, verbose='no') ret = iraf.imstat(sigmap, nclip=50, field='mode', format='no', Stdout=1) sigma_mode = float(ret[0]) iraf.imreplace(tmp_sigma, sigma_mode, upper=0) iraf.imarith(sigmap, '*', -1, tmp_mask, verbose='no') iraf.imreplace(tmp_mask, 1, lower=0) iraf.imreplace(tmp_mask, 0, upper=0) iraf.imarith(tmp_mask, '*', -1, tmp_mask) iraf.imarith(tmp_mask, '+', 1, tmp_mask) # tmp_precombmask = tmp_prefix + 'premask' + os.path.basename(combimg) tmp_combmask = tmp_prefix + 'mask' + os.path.basename(combimg) iraf.unlearn('objmasks') try: iraf.objmasks(combimg, tmp_precombmask, omtype='boolean', masks='', convolve=conv, lsigma=lsigma, hsigma=hsigma, hdetect='yes', ldetect='yes', minpix=minpix, sigmas=tmp_sigma) iraf.objmasks(combimg, tmp_combmask, omtype='boolean', masks=tmp_precombmask + '[pl]', convolve=conv, lsigma=lsigma, hsigma=hsigma, hdetect='yes', ldetect='yes', minpix=minpix, sigmas=tmp_sigma) os.remove(tmp_precombmask) except: print >> sys.stderr, 'failed to execute iraf objmasks task for %s' % combimg remove_temp_all(tmp_prefix) return 1 # remove the area with no sigma value tmp_combmask2 = tmp_prefix + 'mask2' + os.path.basename(combimg) if sigmap.lower() == 'none': iraf.imcopy(tmp_combmask + '[pl]', tmp_combmask2, verbose='no') else: iraf.imarith(tmp_combmask + '[pl]', '*', tmp_mask, tmp_combmask2, verbose='no') os.remove(tmp_combmask) # check if input images exist and fix bad pixel if requested iraf.unlearn('imcopy') iraf.unlearn('imshift') for i in range(len(inimg_arr)): # get offset values from header im = pyfits.open(inimg_arr[i]) try: dx = float(im[0].header['dx']) dy = float(im[0].header['dy']) dxc = float(im[0].header['dxc']) dyc = float(im[0].header['dyc']) nx = int(im[0].header['NAXIS1']) ny = int(im[0].header['NAXIS2']) except: print >> sys.stderr, 'failed to get offset values from the header of %s' % inimg_arr[ i] remove_temp_all(tmp_prefix) return 1 im.close() # shift combined mask tmp_shiftimg = tmp_prefix + os.path.basename(outimg_arr[i]) iraf.imshift(tmp_combmask2, tmp_shiftimg, dxc, dyc, boundary_typ='constant', constant=0.0) tmp_shiftimg_reg = '%s[1:%d,1:%d]' % (tmp_shiftimg, nx, ny) #iraf.imcopy(tmp_shiftimg_reg, outimg_arr[i]+'[pl]',verbose='no') iraf.imcopy(tmp_shiftimg_reg, outimg_arr[i], verbose='no') if bpmex == 1: iraf.imarith(outimg_arr[i], '+', bpm, outimg_arr[i], verbose='no') convert_maskfits_int(outimg_arr[i], outimg_arr[i]) os.remove(tmp_shiftimg) # remove all temporary files remove_temp_all(tmp_prefix) return 0
def maskStars(imageName): #read in the fits data and header imageHeader = fits.open(imageName+".fits")[0] #Read in the skysigma, skyv, and seeing (fwhm) from the header and set up other relevant values sigma = imageHeader.header['SKYSIGMA'] threshold = 3.0 * sigma skyv = imageHeader.header['SKY'] fwhm = imageHeader.header['SEEING'] aper = 3.0 * fwhm annul = 5 * fwhm #Read in the galcut file galcut = open("galcut.file") galcutString = file.read(galcut) galcut.close() #Parse the galcut file into its variables galList = galcutString.split(' ') xCenter = float(galList[0]) yCenter = float(galList[1]) a = float(galList[2]) #Semimajor Axis eccent = float(galList[3]) #Eccentricity posAngle = float(galList[4]) #Position angle posAngleRad = (posAngle+90)*3.14159/180 b = a * eccent #Semiminor Axis na = -1*a nb = -1*b #Create a version of the r image with the sky added back in for the daofind procedure iraf.imdelete("withsky") iraf.imarith(imageName,'+',skyv,"withsky") # Load daophot package iraf.digiphot() iraf.daophot() print("A. Find stars using daofind") #DAOFIND searches the IRAF images image for local density maxima, # with a full-width half-maxima of datapars.fwhmpsf, and a peak # amplitude greater than findpars.threshold * datapars.sigma above # the local background, and writes a list of detected objects in the # file output. # Here we set the fwhm and sigma equal to those listed in the header. # We set the datamin to -3*sigma = -1*threshold defined above # The findpars.threshold is set to 4.5*sigma by default. You may have # to alter this value for images where too few/many stars are found. #Configure 'datapars', 'findpars', and 'daofind' iraf.datapars.fwhmpsf=fwhm iraf.datapars.sigma=sigma iraf.datapars.datamax='indef' iraf.datapars.datamin=(-1)*threshold iraf.datapars.ccdread='RDNOISE' iraf.datapars.gain="GAIN" iraf.datapars.exposure="EXPTIME" iraf.datapars.airmass="AIRMASS" iraf.datapars.filter="FILTER" iraf.datapars.obstime="TIME-OBS" iraf.findpars.threshold=4.5 iraf.findpars.roundhi=3 iraf.findpars.roundlo=-3 iraf.daofind.verify='no' iraf.daofind.verbose='no' #create the variable for the coordinate file allStars = imageName+'.coo' #Remove a previous coordinate file if one exists silentDelete(imageName+'.coo') #Find the stars in the aligned R image iraf.daofind("withsky",allStars) print("The file containing the data of the stars in the aligned R-image is ",allStars) print(" ") if os.path.getsize(allStars) > 2239 : print("B. Separate stars inside and outside galaxy") #Delete existing files for f in ("temp.file","maskfile","maskfile.sat"): silentDelete(f) #Extract the coordinates from the allStars file, redirecting stdout to do so sys.stdout=open("temp.file","w") iraf.pdump(allStars,"xcenter,ycenter",'yes') sys.stdout = sys.__stdout__ #Read the file to get the xy coordinate pairs in a list coords = open("temp.file") coordList = list(coords) countout=0 #Create strings of xy pairs to write to files outGalString = "" for pair in coordList: #for each entry in the list, parse it into xy value integer pairs values = pair.split(" ") x = float(values[0]) y = float(values[1]) #Determine if the star lies within the galaxy-centered ellipse inGalaxy = False deltaX = x - xCenter deltaY = y - yCenter cdx = abs(deltaX) cdy = abs(deltaY) if (cdx < a and cdy < a): xt=(deltaX*math.cos(posAngleRad))+(deltaY*math.sin(posAngleRad)) yt=(deltaY*math.cos(posAngleRad))-(deltaX*math.sin(posAngleRad)) if(xt <= a and xt >= na and yt <= b and yt >= nb): inGalaxy = True if (not inGalaxy): outGalString += str(x) + " " + str(y) + "\n" countout=countout+1 #Write the coordinate list to a file mask = open("maskfile","w") mask.write(outGalString) mask.close() print('') print(" ",countout," stars found outside galaxy") print('') if countout!=0 : print("C. Do photometry to find saturated stars") for f in ("maskfile.mag","maskfile.satmag","maskfile.sat"): silentDelete(f) #Configure 'centerpars', 'fitskypars','photpars' iraf.datapars.datamax=150000 iraf.datapars.datamin='indef' iraf.centerpars.calgorithm="none" iraf.fitskypars.salgorithm="mode" iraf.fitskypars.annulus=annul iraf.fitskypars.dannulus=10 iraf.photpars.apertures=fwhm iraf.phot.verify='no' iraf.phot.verbose='no' #Call 'phot' to get the data of the stars iraf.phot (imageName,"maskfile","maskfile.mag") boexpr="PIER==305" iraf.pselect("maskfile.mag","maskfile.satmag",boexpr) sys.stdout=open("maskfile.sat","w") iraf.pdump("maskfile.satmag","xcenter,ycenter","yes") sys.stdout = sys.__stdout__ print("D. Make mask of stars outside galaxy") #Delete the rmask if one exists silentDelete("rmask.fits") #Configure 'imedit' iraf.imedit.cursor="maskfile" iraf.imedit.display='no' iraf.imedit.autodisplay='no' iraf.imedit.aperture="circular" iraf.imedit.value=-1000 iraf.imedit.default="e" #Replace the pixel values near the star coordinates with value -1000 using imedit iraf.imedit(imageName,"redit",radius=aper) iraf.imcopy.verbose='no' #Do the same for the saturated stars, but with larger apertures satMaskSize = os.path.getsize("maskfile.sat") if satMaskSize < 1 : print(" No saturated stars found") if (satMaskSize!=0): iraf.imedit.cursor="maskfile.sat" iraf.imedit.radius=2*aper iraf.imedit("redit","rmask") else: iraf.imcopy("redit","rmask") #Replace good pixels with value 0 in 'rmask' iraf.imreplace.lower=-999 iraf.imreplace.upper='indef' iraf.imreplace("rmask",0) #Replace bad pixels with value 1 in 'rmask' iraf.imreplace.lower='indef' iraf.imreplace.upper=-1000 iraf.imreplace("rmask",1) #Remove the mask file if one already exists silentDelete(imageName+"mask.fits") iraf.imcopy("rmask",imageName+"mask") print(" ") print("The mask image is ",imageName+"mask") print("The masked image is ",imageName+"Masked") print(" ") else : print("No stars found outside galaxy, no mask created.") if os.path.getsize(allStars) < 2314 : print("No stars found, no mask created.") #Delete intermediate images for f in ("rmask.fits","redit.fits","maskimage.fits","maskfile.mag","maskfile.satmag","temp.file","withsky.fits","mask.fits"): silentDelete(f)
def maskStars(imageName): #read in the fits data and header imageHeader = fits.open(imageName + ".fits")[0] #Read in the skysigma, skyv, and seeing (fwhm) from the header and set up other relevant values sigma = imageHeader.header['SKYSIGMA'] threshold = 3.0 * sigma skyv = imageHeader.header['SKY'] fwhm = imageHeader.header['SEEING'] aper = 3.0 * fwhm annul = 5 * fwhm #Read in the galcut file galcut = open("galcut.file") galcutString = file.read(galcut) galcut.close() #Parse the galcut file into its variables galList = galcutString.split(' ') xCenter = float(galList[0]) yCenter = float(galList[1]) a = float(galList[2]) #Semimajor Axis eccent = float(galList[3]) #Eccentricity posAngle = float(galList[4]) #Position angle posAngleRad = (posAngle + 90) * 3.14159 / 180 b = a * eccent #Semiminor Axis na = -1 * a nb = -1 * b #Create a version of the r image with the sky added back in for the daofind procedure iraf.imdelete("withsky") iraf.imarith(imageName, '+', skyv, "withsky") # Load daophot package iraf.digiphot() iraf.daophot() print("A. Find stars using daofind") #DAOFIND searches the IRAF images image for local density maxima, # with a full-width half-maxima of datapars.fwhmpsf, and a peak # amplitude greater than findpars.threshold * datapars.sigma above # the local background, and writes a list of detected objects in the # file output. # Here we set the fwhm and sigma equal to those listed in the header. # We set the datamin to -3*sigma = -1*threshold defined above # The findpars.threshold is set to 4.5*sigma by default. You may have # to alter this value for images where too few/many stars are found. #Configure 'datapars', 'findpars', and 'daofind' iraf.datapars.fwhmpsf = fwhm iraf.datapars.sigma = sigma iraf.datapars.datamax = 'indef' iraf.datapars.datamin = (-1) * threshold iraf.datapars.ccdread = 'RDNOISE' iraf.datapars.gain = "GAIN" iraf.datapars.exposure = "EXPTIME" iraf.datapars.airmass = "AIRMASS" iraf.datapars.filter = "FILTER" iraf.datapars.obstime = "TIME-OBS" iraf.findpars.threshold = 4.5 iraf.findpars.roundhi = 3 iraf.findpars.roundlo = -3 iraf.daofind.verify = 'no' iraf.daofind.verbose = 'no' #create the variable for the coordinate file allStars = imageName + '.coo' #Remove a previous coordinate file if one exists silentDelete(imageName + '.coo') #Find the stars in the aligned R image iraf.daofind("withsky", allStars) print( "The file containing the data of the stars in the aligned R-image is ", allStars) print(" ") if os.path.getsize(allStars) > 2239: print("B. Separate stars inside and outside galaxy") #Delete existing files for f in ("temp.file", "maskfile", "maskfile.sat"): silentDelete(f) #Extract the coordinates from the allStars file, redirecting stdout to do so sys.stdout = open("temp.file", "w") iraf.pdump(allStars, "xcenter,ycenter", 'yes') sys.stdout = sys.__stdout__ #Read the file to get the xy coordinate pairs in a list coords = open("temp.file") coordList = list(coords) countout = 0 #Create strings of xy pairs to write to files outGalString = "" for pair in coordList: #for each entry in the list, parse it into xy value integer pairs values = pair.split(" ") x = float(values[0]) y = float(values[1]) #Determine if the star lies within the galaxy-centered ellipse inGalaxy = False deltaX = x - xCenter deltaY = y - yCenter cdx = abs(deltaX) cdy = abs(deltaY) if (cdx < a and cdy < a): xt = (deltaX * math.cos(posAngleRad)) + (deltaY * math.sin(posAngleRad)) yt = (deltaY * math.cos(posAngleRad)) - (deltaX * math.sin(posAngleRad)) if (xt <= a and xt >= na and yt <= b and yt >= nb): inGalaxy = True if (not inGalaxy): outGalString += str(x) + " " + str(y) + "\n" countout = countout + 1 #Write the coordinate list to a file mask = open("maskfile", "w") mask.write(outGalString) mask.close() print('') print(" ", countout, " stars found outside galaxy") print('') if countout != 0: print("C. Do photometry to find saturated stars") for f in ("maskfile.mag", "maskfile.satmag", "maskfile.sat"): silentDelete(f) #Configure 'centerpars', 'fitskypars','photpars' iraf.datapars.datamax = 150000 iraf.datapars.datamin = 'indef' iraf.centerpars.calgorithm = "none" iraf.fitskypars.salgorithm = "mode" iraf.fitskypars.annulus = annul iraf.fitskypars.dannulus = 10 iraf.photpars.apertures = fwhm iraf.phot.verify = 'no' iraf.phot.verbose = 'no' #Call 'phot' to get the data of the stars iraf.phot(imageName, "maskfile", "maskfile.mag") boexpr = "PIER==305" iraf.pselect("maskfile.mag", "maskfile.satmag", boexpr) sys.stdout = open("maskfile.sat", "w") iraf.pdump("maskfile.satmag", "xcenter,ycenter", "yes") sys.stdout = sys.__stdout__ print("D. Make mask of stars outside galaxy") #Delete the rmask if one exists silentDelete("rmask.fits") #Configure 'imedit' iraf.imedit.cursor = "maskfile" iraf.imedit.display = 'no' iraf.imedit.autodisplay = 'no' iraf.imedit.aperture = "circular" iraf.imedit.value = -1000 iraf.imedit.default = "e" #Replace the pixel values near the star coordinates with value -1000 using imedit iraf.imedit(imageName, "redit", radius=aper) iraf.imcopy.verbose = 'no' #Do the same for the saturated stars, but with larger apertures satMaskSize = os.path.getsize("maskfile.sat") if satMaskSize < 1: print(" No saturated stars found") if (satMaskSize != 0): iraf.imedit.cursor = "maskfile.sat" iraf.imedit.radius = 2 * aper iraf.imedit("redit", "rmask") else: iraf.imcopy("redit", "rmask") #Replace good pixels with value 0 in 'rmask' iraf.imreplace.lower = -999 iraf.imreplace.upper = 'indef' iraf.imreplace("rmask", 0) #Replace bad pixels with value 1 in 'rmask' iraf.imreplace.lower = 'indef' iraf.imreplace.upper = -1000 iraf.imreplace("rmask", 1) #Remove the mask file if one already exists silentDelete(imageName + "mask.fits") iraf.imcopy("rmask", imageName + "mask") print(" ") print("The mask image is ", imageName + "mask") print("The masked image is ", imageName + "Masked") print(" ") else: print("No stars found outside galaxy, no mask created.") if os.path.getsize(allStars) < 2314: print("No stars found, no mask created.") #Delete intermediate images for f in ("rmask.fits", "redit.fits", "maskimage.fits", "maskfile.mag", "maskfile.satmag", "temp.file", "withsky.fits", "mask.fits"): silentDelete(f)
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 process(): ovrsc = '[1:4,*]' trim = '[200:2046,50:200]' imglist = glob.glob('FORS2*.fits') # objdict = {} # objdict.clear() for img in imglist: hdu = pyfits.getheader(img) if 'OBJECT' in hdu: obj = hdu['OBJECT'] if obj not in objdict: objdict[obj] = [img] else: objdict[obj].append(img) if 'STD' not in objdict: os.system('cp /dark/jsamuel/agn/standards/FORS2.2016-05-12T09:15:14.678.fits ./') objdict['STD'] = ['FORS2.2016-05-12T09:15:14.678.fits'] os.system('cp /dark/jsamuel/agn/standards/FORS2.2016-05-12T04:24:38.547.fits ./') objdict['STD'].append('FORS2.2016-05-12T04:24:38.547.fits') stars = {'specphot-LTT7379':'l7379','specphot-LDS749B':'lds749b','specphot-EG274':'eg274','specphot-G138-31':'g13831','specphot-C-32d9927':'cd32','specphot-LTT9491':'l9491','specphot-LTT7987':'l7987'} i = 0 for key in objdict.keys(): if 'STD' in key: for img in objdict[key]: i = i + 1 numstars = len(objdict['STD']) hds = pyfits.getheader(img) _starname = stars[hds['HIERARCH ESO OBS NAME']] if _starname in ['lds749b','g13831']: print 'Bad standard, copying from 2016-05-12' if not os.path.isdir('badstd'): os.mkdir('badstd') os.system('mv '+img+' ./badstd') if i >= numstars: objdict.pop('STD') os.system('cp /dark/jsamuel/agn/standards/FORS2.2016-05-12T09:15:14.678.fits ./') objdict['STD'] = ['FORS2.2016-05-12T09:15:14.678.fits'] os.system('cp /dark/jsamuel/agn/standards/FORS2.2016-05-12T04:24:38.547.fits ./') objdict['STD'].append('FORS2.2016-05-12T04:24:38.547.fits') if os.path.isfile('biaslist'): os.remove('biaslist') if os.path.isfile('masterbias.fits'): os.remove('masterbias.fits') f = open('biaslist','w') for img in objdict['BIAS']: f.write(img+'\n') f.close() imglist = '@biaslist' name = 'masterbias.fits' hdb = pyfits.getheader(objdict['BIAS'][0]) _gain = hdb['HIERARCH ESO DET OUT1 GAIN'] _ron = hdb['HIERARCH ESO DET OUT1 RON'] iraf.zerocombine(imglist,output=name,combine='average',reject='minmax',ccdtype='none',process='no',gain=_gain,rdnoise=_ron,Stdout=1) if os.path.isfile('flatlist'): os.remove('flatlist') if os.path.isfile('sciflatlist'): os.remove('sciflatlist') if os.path.isfile('stdflatlist'): os.remove('stdflatlist') if os.path.isfile('masterflat.fits'): os.remove('masterflat.fits') if os.path.isfile('scimasterflat.fits'): os.remove('scimasterflat.fits') if os.path.isfile('stdmasterflat.fits'): os.remove('stdmasterflat.fits') f = open('sciflatlist','w') for img in objdict['FLAT,LAMP']: hdu = pyfits.getheader(img) if hdu['HIERARCH ESO DPR TECH'] == 'SPECTRUM': f.write(img+'\n') f.close() j = 0 f = open('stdflatlist','w') for img in objdict['FLAT,LAMP']: hdu = pyfits.getheader(img) if hdu['HIERARCH ESO DPR TECH'] == 'MOS': f.write(img+'\n') j = j + 1 f.close() imglist = '@sciflatlist' name = 'scimasterflat.fits' hdf = pyfits.getheader(objdict['FLAT,LAMP'][0]) _gain = hdf['HIERARCH ESO DET OUT1 GAIN'] _ron = hdf['HIERARCH ESO DET OUT1 RON'] iraf.flatcombine(imglist,output=name,combine='average',reject='avsigclip',ccdtype='none',process='no',subsets='yes',delete='no',clobber='no',gain=_gain,rdnoise=_ron,Stdout=1) if j == 0: imglist = '@sciflatlist' elif j >= 1: imglist = '@stdflatlist' name = 'stdmasterflat.fits' hdf = pyfits.getheader(objdict['FLAT,LAMP'][0]) _gain = hdf['HIERARCH ESO DET OUT1 GAIN'] _ron = hdf['HIERARCH ESO DET OUT1 RON'] iraf.flatcombine(imglist,output=name,combine='average',reject='avsigclip',ccdtype='none',process='no',subsets='yes',delete='no',clobber='no',gain=_gain,rdnoise=_ron,Stdout=1) if os.path.isfile('tmasterbias.fits'): os.remove('tmasterbias.fits') fits = 'masterbias.fits' name = 'tmasterbias.fits' zcor = 'no' fcor = 'no' _zero = '' _flat = '' iraf.ccdproc(fits,output=name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero=_zero,flat=_flat,Stdout=1) if os.path.isfile('tmasterflat.fits'): os.remove('tmasterflat.fits') if os.path.isfile('tscimasterflat.fits'): os.remove('tscimasterflat.fits') if os.path.isfile('tstdmasterflat.fits'): os.remove('tstdmasterflat.fits') fits = 'scimasterflat.fits' name = 'tscimasterflat.fits' zcor = 'yes' fcor = 'no' _zero = 'tmasterbias.fits' _flat = '' iraf.ccdproc(fits,output=name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero=_zero,flat=_flat,Stdout=1) fits = 'stdmasterflat.fits' name = 'tstdmasterflat.fits' zcor = 'yes' fcor = 'no' _zero = 'tmasterbias.fits' _flat = '' iraf.ccdproc(fits,output=name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero=_zero,flat=_flat,Stdout=1) if os.path.isfile('ntmasterflat.fits'): os.remove('ntmasterflat.fits') if os.path.isfile('ntscimasterflat.fits'): os.remove('ntscimasterflat.fits') if os.path.isfile('ntstdmasterflat.fits'): os.remove('ntstdmasterflat.fits') cal = 'tscimasterflat.fits' resp = 'ntscimasterflat.fits' _order = 100 iraf.response(calibration=cal,normalization=cal,response=resp,interactive='no',function='legendre',order=_order,graphics='stdgraph') cal = 'tstdmasterflat.fits' resp = 'ntstdmasterflat.fits' _order = 100 iraf.response(calibration=cal,normalization=cal,response=resp,interactive='no',function='legendre',order=_order,graphics='stdgraph') ########## ovrsc = '[1:4,*]' trim = '[200:2046,50:200]' fimg = 'ntscimasterflat' _area = '[1:150,*]' _value = 1.0 iraf.imreplace(images=fimg+_area,value=_value) fimg = 'ntstdmasterflat' _area = '[1:150,*]' _value = 1.0 iraf.imreplace(images=fimg+_area,value=_value) alist = glob.glob('*AGN*.fits') for f in alist: os.remove(f) slist = glob.glob('STD*.fits') for f in slist: os.remove(f) llist = glob.glob('LAMP*.fits') for f in llist: os.remove(f) zcor = 'yes' fcor = 'yes' _zero = 'tmasterbias.fits' sciflat = 'ntscimasterflat.fits' stdflat = 'ntstdmasterflat.fits' for key in objdict.keys(): if 'AGN' in key: for img in objdict[key]: hds = pyfits.getheader(img) if 'EXPTIME' in hds: if hds['EXPTIME'] >= 50.0: num = img.rsplit('.',2)[1] name = key+'_'+num ''' iraf.ccdproc(img,output='trim_'+name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor='no',darkcor='no',flatcor='no',illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero='',flat='',Stdout=1) _gain = hds['HIERARCH ESO DET OUT1 GAIN'] _ron = hds['HIERARCH ESO DET OUT1 RON'] cosmics.lacos('trim_'+name+'.fits', output='c_'+name+'.fits', gain=_gain, readn=_ron, xorder=9, yorder=9, sigclip=4.5, sigfrac=0.5, objlim=1, verbose=True, interactive=False) iraf.ccdproc('c_'+name,output=name,ccdtype='',noproc='no',fixpix='no',overscan='no',trim='no',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec='',trimsec='',zero=_zero,flat=sciflat,Stdout=1) ''' iraf.ccdproc(img,output=name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero=_zero,flat=sciflat,Stdout=1) else: pass elif 'STD' in key: for img in objdict[key]: num = img.rsplit('.',2)[1] name = key+'_'+num iraf.ccdproc(img,output=name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero=_zero,flat=stdflat,Stdout=1) elif 'WAVE' in key: for img in objdict[key]: num = img.rsplit('.',2)[1] name = 'LAMP_'+num iraf.ccdproc(img,output=name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero=_zero,flat=stdflat,Stdout=1)
continue #starting loop from top filename_list = filter(lambda x: x[0] == unique_filter, zip(filters, filenames)) fin = open(object_list, 'w+') fout = open(out_object_list, 'w+') for fn in filename_list: print >> fin, fn[1] + '[1]' print >> fout, 'r' + fn[1] fin.close() fout.close() if do_flat: #Making sure no division by zero occurs iraf.imreplace(flat_file, value=0.00001, upper=0, radius=0) iraf.noao.imred.ccdred.ccdproc.output = '@' + out_object_list iraf.noao.imred.ccdred.ccdproc.overscan = overboo iraf.noao.imred.ccdred.ccdproc.trim = trimboo iraf.noao.imred.ccdred.ccdproc.zerocor = biasboo iraf.noao.imred.ccdred.ccdproc.flatcor = flatboo iraf.noao.imred.ccdred.ccdproc.zero = join(biasdir, bias_file) iraf.noao.imred.ccdred.ccdproc.flat = flat_file iraf.noao.imred.ccdred.ccdproc('@' + object_list) print bold("\nImreduce ended successfully") ###################################################################################################
def lris_standard(standards, xcorr=yes): '''Extract standard stars and calculate sensitivity functions.''' for ofile, nstd in standards: shutil.copy(ofile, "%s.fits" % nstd) # Extract standard if get_head("%s.fits" % nstd, "SKYSUB"): iraf.apall(nstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none") else: iraf.apall(nstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit") if xcorr: # Cross correlate to tweak wavelength iraf.xcsao("%s.ms" % nstd, templates=XCTEMPLATE, correlate="wavelength", logfiles="%s.xcsao" % ofile) # If a solution was found, apply shift try: xcsaof = open("%s.xcsao" % ofile) shift = float(xcsaof.readlines()[6].split()[14]) except: shift = 0.0 iraf.specshift("%s.ms" % nstd, -shift) # Create telluric iraf.splot("%s.ms" % nstd) # Remove telluric and absorption, save as a%s.ms iraf.sarith("%s.ms" % nstd, "/", "a%s.ms" % nstd, "telluric.%s.fits" % nstd) iraf.imreplace("telluric.%s.fits" % nstd, 1.0, lower=0.0, upper=0.0) iraf.splot("telluric.%s.fits" % nstd) # Remove stellar features and resave # Create smoothed standard iraf.gauss("a%s.ms[*,1,1]" % nstd, "s%s.ms" % nstd, 5.0) iraf.sarith("%s.ms" % nstd, "/", "s%s.ms" % nstd, "ds%s.ms" % nstd) # Apply telluric correction iraf.telluric( "ds%s.ms" % nstd, "tds%s.ms" % nstd, "telluric.%s.fits" % nstd, xcorr=no, tweakrms=no, interactive=no, sample='4000:4010,6850:6975,7150:7350,7575:7725,8050:8400,8900:9725' ) # Define bandpasses for standard star calculation obj = get_head("%s.fits" % nstd, "OBJECT") iraf.standard("tds%s.ms" % nstd, "%s.std" % nstd, extinction='home$extinct/maunakeaextinct.dat', caldir='home$standards/', observatory='Keck', interac=yes, star_name=obj, airmass='', exptime='') # Determine sensitivity function iraf.sensfunc('%s.std' % nstd, '%s.sens' % nstd, extinction='home$extinct/maunakeaextinct.dat', newextinction='extinct.dat', observatory='Keck', function='legendre', order=4, interactive=yes) return
def combine_images(images, output, reference=None, method='average', zscale=1, fitgeometry='general', function='polynomial', xxorder=3, yyorder=3, xyorder=3, yxorder=3, objects=None, trim=0, interactive=1, maxiter=5, reject=3, sigma=None, xrange=None, yrange=None, rectify=1, thresh=2.0, scale=0.125, fwhm=4.0, debug=0, aoffset=0, creject=None): '''This Routine will take a list of images and use rectify_image to rectify each with respect to referece. It will then use imcombine to combine the images into one and output to "output". Default method is 'average', but you can use any available to imcombine. The rest of the arguments are sent to rectify_images. If zscale=1, all the images are scaled to a common zmag (30). Also, if a file with _sigma.fits is found for all input files, we combine them too. If you specify a sigma, that file will hold the standard deviations from the files.''' names = get_files(images) if reference is None: reference = names[0] names = names[1:] pclip = -0.5 lsigma = 3. hsigma = 3. nkeep = -2 if creject: creject = "pclip" else: creject = "none" # Check to see if we have _sigma files for each input file if os.path.isfile(reference.replace('.fits', '_sigma.fits')): do_sigmas = 1 else: do_sigmas = 0 sigmas = [] for name in names: if os.path.isfile(name.replace('.fits', '_sigma.fits')): sigmas.append(name.replace('.fits', '_sigma.fits')) else: do_sigmas = 0 break # Put the reference image first, to ensure that all positional header keywords are # valid. if do_sigmas: ref_sig = reference.replace('.fits', '_sigma.fits') update = 0 if zscale: zmag = get_header(reference, "ZMAG", float) if zmag is not None and zmag != 26: update = 1 zmagfac = num.power(10, (zmag - 30) / 2.5) if zmagfac != 1: print 'zmagfac = ', zmagfac iraf.imarith(reference, '/', zmagfac, reference) iraf.hedit(reference, "ZMAG", 30.0, add=1, verify=0) if do_sigmas: iraf.imarith(ref_sig, '/', zmagfac, ref_sig) iraf.hedit(ref_sig, "ZMAG", 30.0, add=1, verify=0) print "Using %s as reference" % (reference) if rectify: nuke(reference.replace('.fits', '_rec.fits')) iraf.imcopy(reference, reference.replace('.fits', '_rec.fits')) if do_sigmas: nuke(reference.replace('.fits', '_rec_sigma.fits')) iraf.imcopy(reference.replace('.fits', '_sigma.fits'), reference.replace('.fits', '_rec_sigma.fits')) temps = [reference.replace('.fits', '_rec.fits')] if do_sigmas: sig_temps = [reference.replace('.fits', '_rec_sigma.fits')] else: temps = [reference] if do_sigmas: sig_temps = [ref_sig] i = 0 for name in names: print name if rectify: temp = name.replace('.fits', '_rec.fits') nuke(temp) if do_sigmas: (x0, x1, y0, y1) = rectify_image(name, reference, output=temp, fitgeometry=fitgeometry, function=function, xxorder=xxorder, yyorder=yyorder, xyorder=xyorder, yxorder=yxorder, objects=objects, interactive=interactive, maxiter=maxiter, reject=reject, xrange=xrange, yrange=yrange, thresh=thresh, scale=scale, fwhm=fwhm, image_sigma=sigmas[i], reference_sigma=ref_sig, debug=debug, aoffset=aoffset) else: (x0, x1, y0, y1) = rectify_image(name, reference, output=temp, fitgeometry=fitgeometry, function=function, xxorder=xxorder, yyorder=yyorder, xyorder=xyorder, yxorder=yxorder, objects=objects, interactive=interactive, maxiter=maxiter, reject=reject, xrange=xrange, yrange=yrange, thresh=thresh, scale=scale, fwhm=fwhm, debug=debug, aoffset=aoffset) else: temp = name bpm = get_header(name, 'BPM', str) if bpm and rectify: bpm_fits = bpm.replace('.pl', '.fits') temp_bpm = bpm.replace('.pl', '_rec.fits') temp_bpm_pl = bpm.replace('.pl', '_rec.pl') nuke(bpm_fits) nuke(temp_bpm) nuke(temp_bpm_pl) iraf.imcopy(bpm, bpm_fits) iraf.imarith(bpm_fits, "*", 10.0, bpm_fits) if objects is None: sigmaobjects = 'geomap.objects' iraf.geotran(input=bpm_fits, output=temp_bpm, database='geomap.db', transforms=sigmaobjects, geometry='geometric', interpolant="linear") iraf.imreplace(temp_bpm, lower=0.02, upper="INDEF", value=1.) iraf.imcopy(temp_bpm, temp_bpm_pl) iraf.hedit(temp, 'BPM', temp_bpm_pl, update=1, verify=0) if do_sigmas: if rectify: temp_sig = sigmas[i].replace('_sigma.fits', '_rec_sigma.fits') nuke(temp_sig) if objects is None: sigmaobjects = 'geomap.objects' else: sigmaobjects = objects iraf.geotran(input=sigmas[i], output=temp_sig, database='geomap.db', transforms=sigmaobjects, geometry='geometric', interpolant="linear") else: temp_sig = sigmas[i] if rectify: if i == 0: xmin = x0 ymin = y0 xmax = x1 ymax = y1 else: xmin = max(xmin, x0) ymin = max(ymin, y0) xmax = min(xmax, x1) ymax = min(ymax, y1) if zscale: zmag = get_header(name, "ZMAG", float) if zmag is not None and zmag != 26: zmagfac = num.power(10, (zmag - 30) / 2.5) if zmagfac != 1: print 'zmagfac = ', zmagfac iraf.imarith(temp, '/', zmagfac, temp) iraf.hedit(temp, 'ZMAG', 30.0, add=1, verify=0) if do_sigmas: iraf.imarith(temp_sig, '/', zmagfac, temp_sig) iraf.hedit(temp_sig, 'ZMAG', 30.0, add=1, verify=0) temps.append(temp) if do_sigmas: sig_temps.append(temp_sig) i = i + 1 Nimages = len(temps) temps = string.join(temps, ',') print "Combining ", temps nuke(output) if sigma is not None: iraf.imcombine(temps, output, combine=method, sigma=sigma, masktyp='badvalue', maskval=1., reject=creject, lsigma=lsigma, hsigma=hsigma, nkeep=nkeep) else: iraf.imcombine(temps, output, combine=method, masktyp='badvalue', maskval=1., reject=creject, lsigma=lsigma, hsigma=hsigma, nkeep=nkeep) if do_sigmas: Ns = [] for this in sig_temps: N = this.replace('.fits', '_N.fits') nuke(N) iraf.imcopy(this, N) iraf.imreplace(N, lower=0.01, upper="INDEF", value=1) Ns.append(N) sig_temps = string.join(sig_temps, ',') Ns = string.join(Ns, ',') iraf.imfunction(sig_temps, sig_temps, 'square') file = output.replace('.fits', '_sigma.fits') nuke(file) iraf.imcombine(sig_temps, file, combine='sum') if method == 'average': nuke("N.fits") iraf.imcombine(Ns, 'N.fits', combine='sum') iraf.imfunction('N.fits', 'N.fits', 'square') iraf.imarith(file, '/', 'N.fits', file) iraf.imfunction(file, file, 'sqrt') iraf.imfunction(sig_temps, sig_temps, 'sqrt') if update: iraf.hedit(output, "ZMAG", 30.0, verify=0, add=1) #nuke('temp?.fits') # Now, let's clean up the gain and rdnoise headers, as they don't seem to # be handled in the PANIC pipeline. NOTE: we really need a noise map, but # for now, we'll deal with just global values. gain = get_header(output, "gain", float) rdnoise = get_header(output, "rdnoise", float) if gain is None: # have to compute it from scratch egain = get_header(output, "egain", float) enoise = get_header(output, "enoise", float) nloop = get_header(output, "nloops", int) i = 1 key = "" # This seems to be the only safe way to compute this. Can't trust # NDITHERS while key is not None: key = get_header(output, "imcmb%03d" % (i), str) i = i + 1 ndither = i - 1 print "N, ndither, nloop, egain, enoise = ", Nimages, ndither, nloop, egain, enoise gain = egain * Nimages * nloop * ndither rdnoise = enoise * num.sqrt(Nimages * nloop * ndither) else: gain = gain * Nimages rdnoise = rdnoise * num.sqrt(Nimages) iraf.hedit(output, "gain", gain, verify=0, add=1) iraf.hedit(output, "rdnoise", rdnoise, verify=0, add=1) # If requested, trim to the common regions of the combined images if rectify and trim and Nimages > 1: iraf.imcopy(output + "[%d:%d,%d:%d]" % (xmin, xmax, ymin, ymax), output.replace('.fits', '_trim.fits'))
def lacos_im(_input, _output='clean.fits', outmask='mask.fits', gain=1.3, readn=9, xorder=9, yorder=9, sigclip=4.5, sigfrac=0.5, objlim=1, skyval=0, niter=2, verbose=True, interactive=False): import floyds from floyds.util import delete import sys, re, os, string from pyraf import iraf import numpy as np iraf.convolve.bilinear = 'no' iraf.convolve.radsym = 'no' # make temporary files oldoutput, galaxy, skymod, med5 = 'oldoutput.fits', 'galaxy.fits', 'skymod.fits', 'med5.fits' blk, lapla, med3, med7, sub5, sigima, finalsel = 'blk.fits', 'lapla.fits', 'med3.fits', 'med7.fits', 'sub5.fits',\ 'sigima.fits', 'finalsel.fits' deriv2, noise, sigmap, firstsel, starreject = 'deriv2.fits', 'noise.fits', 'sigmap.fits', 'firstsel.fits',\ 'starreject.fits' inputmask, gfirstsel = 'inputmask.fits', 'gfirstsel.fits' f = open('_kernel', 'w') f.write('0 -1 0;\n-1 4 -1;\n0 -1 0') f.close() # create growth kernel f = open('_gkernel', 'w') f.write('1 1 1;\n1 1 1;\n1 1 1') f.close() gkernel = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]) # initialize loop usegain = gain i = 1 stop = 'no' previous = 0 if not _output: _output = _input arrayinput, headerinput = floyds.cosmics.fromfits(_input, verbose=False) floyds.cosmics.tofits(outmask, np.float32(arrayinput - arrayinput), headerinput, verbose=False) delete(oldoutput) if skyval > 0: arrayoldoutput = arrayinput + skyval else: arrayoldoutput = arrayinput floyds.cosmics.tofits(oldoutput, np.float32(arrayoldoutput), headerinput, verbose=False) # start iterations while stop == 'no': # take second-order derivative (Laplacian) of input image # kernel is convolved with subsampled image, in order to remove negative # pattern around high pixels delete(blk) delete(lapla) delete(deriv2) iraf.blkrep(oldoutput, blk, 2, 2) iraf.convolve(blk, lapla, '_kernel') iraf.imreplace(lapla, 0, upper=0, lower='INDEF', radius=0) iraf.blkavg(lapla, deriv2, 2, 2, option="average") delete(med5) # create model of background flux - 5x5 box should exclude all CRs iraf.median(oldoutput, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no') iraf.imreplace(med5, 0.0001, upper=0, lower='INDEF', radius=0) # create noise model delete(noise) iraf.imutil.imexpr(expr='sqrt(a*' + str(usegain) + '+' + str(readn) + '**2)/' + str(usegain), a=med5, output=noise, verbose='no') # divide Laplacian by noise model delete(sigmap) iraf.imarith(deriv2, "/", noise, sigmap) # Laplacian of blkreplicated image counts edges twice: iraf.imarith(sigmap, "/", 2., sigmap) # removal of large structure (bright, extended objects) delete(med5) iraf.median(sigmap, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no') arraysigmap, headersigmap = floyds.cosmics.fromfits(sigmap, verbose=False) arraymed5, headermed5 = floyds.cosmics.fromfits(med5, verbose=False) arraysigmap = arraysigmap - arraymed5 iraf.imarith(sigmap, "-", med5, sigmap) # find all candidate cosmic rays # this selection includes sharp features such as stars and HII regions delete(firstsel) iraf.imcopy(sigmap, firstsel, verbose='no') iraf.imreplace(firstsel, 0, upper=sigclip, lower='INDEF', radius=0) iraf.imreplace(firstsel, 1, lower=0.1, upper='INDEF', radius=0) # arraygfirst=arraysigmap # arraygfirst = np.where(arraygfirst<sigclip,0,arraygfirst) # arraygfirst = np.where(arraygfirst>0.1,1,arraygfirst) # compare candidate CRs to median filtered image # this step rejects bright, compact sources from the initial CR list # subtract background and smooth component of objects delete(med3) delete(med7) iraf.median(oldoutput, med3, 3, 3, zlo='INDEF', zhi='INDEF', verbose='no') iraf.median(med3, med7, 7, 7, zlo='INDEF', zhi='INDEF', verbose='no') iraf.imarith(med3, "-", med7, med3) iraf.imarith(med3, "/", noise, med3) iraf.imreplace(med3, 0.01, upper=0.01, lower='INDEF', radius=0) # compare CR flux to object flux delete(starreject) iraf.imutil.imexpr(expr="(a*b)/c", a=firstsel, b=sigmap, c=med3, output=starreject, verbose='no') # discard if CR flux <= objlim * object flux iraf.imreplace(starreject, 0, upper=objlim, lower='INDEF', radius=0) iraf.imreplace(starreject, 1, lower=0.5, upper='INDEF', radius=0) iraf.imarith(firstsel, "*", starreject, firstsel) # grow CRs by one pixel and check in original sigma map delete(gfirstsel) iraf.convolve(firstsel, gfirstsel, '_gkernel') iraf.imreplace(gfirstsel, 1, lower=0.5, upper='INDEF', radius=0) iraf.imarith(gfirstsel, "*", sigmap, gfirstsel) iraf.imreplace(gfirstsel, 0, upper=sigclip, lower='INDEF', radius=0) iraf.imreplace(gfirstsel, 1, lower=0.1, upper='INDEF', radius=0) # grow CRs by one pixel and lower detection limit sigcliplow = sigfrac * sigclip delete(finalsel) iraf.convolve(gfirstsel, finalsel, '_gkernel') iraf.imreplace(finalsel, 1, lower=0.5, upper='INDEF', radius=0) iraf.imarith(finalsel, "*", sigmap, finalsel) iraf.imreplace(finalsel, 0, upper=sigcliplow, lower='INDEF', radius=0) iraf.imreplace(finalsel, 1, lower=0.1, upper='INDEF', radius=0) # determine number of CRs found in this iteration delete(gfirstsel) iraf.imutil.imexpr(expr="(1-b)*a", a=outmask, b=finalsel, output=gfirstsel, verbose='no') npix = iraf.imstat(gfirstsel, fields="npix", lower=0.5, upper='INDEF', Stdout=1) # create cleaned output image; use 3x3 median with CRs excluded delete(med5) iraf.imarith(outmask, "+", finalsel, outmask) iraf.imreplace(outmask, 1, lower=1, upper='INDEF', radius=0) delete(inputmask) iraf.imutil.imexpr(expr="(1-10000*a)", a=outmask, output=inputmask, verbose='no') iraf.imarith(oldoutput, "*", inputmask, inputmask) delete(med5) iraf.median(inputmask, med5, 5, 5, zloreject=-9999, zhi='INDEF', verbose='no') iraf.imarith(outmask, "*", med5, med5) if i > 1: delete(_output) delete(_output) iraf.imutil.imexpr(expr="(1.-b)*a+c", a=oldoutput, b=outmask, c=med5, output=_output, verbose='no') # cleanup and get ready for next iteration delete(oldoutput) iraf.imcopy(_output, oldoutput, verbose='no') if npix == 0: stop = 'yes' i = i + 1 if i > niter: stop = 'yes' # delete temp files delete(blk + "," + lapla + "," + deriv2 + "," + med5) delete(med3 + "," + med7 + "," + noise + "," + sigmap) delete(firstsel + "," + starreject + "," + gfirstsel) delete(finalsel + "," + inputmask) if skyval > 0: iraf.imarith(_output, "-", skyval, _output) delete('_kernel' + "," + '_gkernel') delete(oldoutput)
def lacos_im(_input, _output='clean.fits', outmask='mask.fits', gain=1.3, readn=9, xorder=9, yorder=9, sigclip=4.5, sigfrac=0.5, objlim=1, skyval=0, niter=2, verbose=True, interactive=False): # print "LOGX:: Entering `lacos_im` method/function in %(__file__)s" % # globals() import ntt from ntt.util import delete import sys import re import os import string from pyraf import iraf import numpy as np iraf.convolve.bilinear = 'no' iraf.convolve.radsym = 'no' # make temporary files oldoutput, galaxy, skymod, med5 = 'oldoutput.fits', 'galaxy.fits', 'skymod.fits', 'med5.fits' blk, lapla, med3, med7, sub5, sigima, finalsel = 'blk.fits', 'lapla.fits', 'med3.fits', 'med7.fits', 'sub5.fits', 'sigima.fits', 'finalsel.fits' deriv2, noise, sigmap, firstsel, starreject = 'deriv2.fits', 'noise.fits', 'sigmap.fits', 'firstsel.fits', 'starreject.fits' inputmask, gfirstsel = 'inputmask.fits', 'gfirstsel.fits' f = open('_kernel', 'w') f.write('0 -1 0;\n-1 4 -1;\n0 -1 0') f.close() # create growth kernel f = open('_gkernel', 'w') f.write('1 1 1;\n1 1 1;\n1 1 1') f.close() gkernel = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]) # initialize loop usegain = gain i = 1 stop = 'no' previous = 0 if not _output: _output = _input arrayinput, headerinput = ntt.cosmics.fromfits(_input, verbose=False) ntt.cosmics.tofits(outmask, np.float32( arrayinput - arrayinput), headerinput, verbose=False) delete(oldoutput) if skyval > 0: arrayoldoutput = arrayinput + skyval else: arrayoldoutput = arrayinput ntt.cosmics.tofits(oldoutput, np.float32( arrayoldoutput), headerinput, verbose=False) # start iterations while stop == 'no': # take second-order derivative (Laplacian) of input image # kernel is convolved with subsampled image, in order to remove negative # pattern around high pixels delete(blk) delete(lapla) delete(deriv2) iraf.blkrep(oldoutput, blk, 2, 2) iraf.convolve(blk, lapla, '_kernel') iraf.imreplace(lapla, 0, upper=0, lower='INDEF', radius=0) iraf.blkavg(lapla, deriv2, 2, 2, option="average") delete(med5) # create model of background flux - 5x5 box should exclude all CRs iraf.median(oldoutput, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no') iraf.imreplace(med5, 0.0001, upper=0, lower='INDEF', radius=0) # create noise model delete(noise) iraf.imutil.imexpr(expr='sqrt(a*' + str(usegain) + '+' + str(readn) + '**2)/' + str(usegain), a=med5, output=noise, verbose='no') # divide Laplacian by noise model delete(sigmap) iraf.imarith(deriv2, "/", noise, sigmap) # Laplacian of blkreplicated image counts edges twice: iraf.imarith(sigmap, "/", 2., sigmap) # removal of large structure (bright, extended objects) delete(med5) iraf.median(sigmap, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no') arraysigmap, headersigmap = ntt.cosmics.fromfits(sigmap, verbose=False) arraymed5, headermed5 = ntt.cosmics.fromfits(med5, verbose=False) arraysigmap = arraysigmap - arraymed5 iraf.imarith(sigmap, "-", med5, sigmap) # find all candidate cosmic rays # this selection includes sharp features such as stars and HII regions delete(firstsel) iraf.imcopy(sigmap, firstsel, verbose='no') iraf.imreplace(firstsel, 0, upper=sigclip, lower='INDEF', radius=0) iraf.imreplace(firstsel, 1, lower=0.1, upper='INDEF', radius=0) # arraygfirst=arraysigmap # arraygfirst = np.where(arraygfirst<sigclip,0,arraygfirst) # arraygfirst = np.where(arraygfirst>0.1,1,arraygfirst) # compare candidate CRs to median filtered image # this step rejects bright, compact sources from the initial CR list # subtract background and smooth component of objects delete(med3) delete(med7) iraf.median(oldoutput, med3, 3, 3, zlo='INDEF', zhi='INDEF', verbose='no') iraf.median(med3, med7, 7, 7, zlo='INDEF', zhi='INDEF', verbose='no') iraf.imarith(med3, "-", med7, med3) iraf.imarith(med3, "/", noise, med3) iraf.imreplace(med3, 0.01, upper=0.01, lower='INDEF', radius=0) # compare CR flux to object flux delete(starreject) iraf.imutil.imexpr(expr="(a*b)/c", a=firstsel, b=sigmap, c=med3, output=starreject, verbose='no') # discard if CR flux <= objlim * object flux iraf.imreplace(starreject, 0, upper=objlim, lower='INDEF', radius=0) iraf.imreplace(starreject, 1, lower=0.5, upper='INDEF', radius=0) iraf.imarith(firstsel, "*", starreject, firstsel) # grow CRs by one pixel and check in original sigma map delete(gfirstsel) iraf.convolve(firstsel, gfirstsel, '_gkernel') iraf.imreplace(gfirstsel, 1, lower=0.5, upper='INDEF', radius=0) iraf.imarith(gfirstsel, "*", sigmap, gfirstsel) iraf.imreplace(gfirstsel, 0, upper=sigclip, lower='INDEF', radius=0) iraf.imreplace(gfirstsel, 1, lower=0.1, upper='INDEF', radius=0) # grow CRs by one pixel and lower detection limit sigcliplow = sigfrac * sigclip delete(finalsel) iraf.convolve(gfirstsel, finalsel, '_gkernel') iraf.imreplace(finalsel, 1, lower=0.5, upper='INDEF', radius=0) iraf.imarith(finalsel, "*", sigmap, finalsel) iraf.imreplace(finalsel, 0, upper=sigcliplow, lower='INDEF', radius=0) iraf.imreplace(finalsel, 1, lower=0.1, upper='INDEF', radius=0) # determine number of CRs found in this iteration delete(gfirstsel) iraf.imutil.imexpr(expr="(1-b)*a", a=outmask, b=finalsel, output=gfirstsel, verbose='no') npix = iraf.imstat(gfirstsel, fields="npix", lower=0.5, upper='INDEF', Stdout=1) # create cleaned output image; use 3x3 median with CRs excluded delete(med5) iraf.imarith(outmask, "+", finalsel, outmask) iraf.imreplace(outmask, 1, lower=1, upper='INDEF', radius=0) delete(inputmask) iraf.imutil.imexpr(expr="(1-10000*a)", a=outmask, output=inputmask, verbose='no') iraf.imarith(oldoutput, "*", inputmask, inputmask) delete(med5) iraf.median(inputmask, med5, 5, 5, zloreject=- 9999, zhi='INDEF', verbose='no') iraf.imarith(outmask, "*", med5, med5) if i > 1: delete(_output) delete(_output) iraf.imutil.imexpr(expr="(1.-b)*a+c", a=oldoutput, b=outmask, c=med5, output=_output, verbose='no') # cleanup and get ready for next iteration delete(oldoutput) iraf.imcopy(_output, oldoutput, verbose='no') if npix == 0: stop = 'yes' i = i + 1 if i > niter: stop = 'yes' # delete temp files delete(blk + "," + lapla + "," + deriv2 + "," + med5) delete(med3 + "," + med7 + "," + noise + "," + sigmap) delete(firstsel + "," + starreject + "," + gfirstsel) delete(finalsel + "," + inputmask) if skyval > 0: iraf.imarith(_output, "-", skyval, _output) delete('_kernel' + "," + '_gkernel') delete(oldoutput)
reject='minmax', first=no, w1=INDEF, w2=INDEF, dw=INDEF, nw=INDEF, log=no, scale='none', weight='none', zero='none', nlow='0', nhigh='90', nkeep='1') ### Shouldnt be necessary, but incase there are huge spikes cut them off. iraf.imreplace(images='fullspec*', value=50, lower=50, upper=INDEF) iraf.imreplace(images='fullspec*', value=-10, lower=INDEF, upper=-10) ### Fix exposure times. They are off by the number of orders in the trace ### from the summing of orders part norders = 91.0 #115.0 updated to reflect ignoring edge orders flist = open('targets_extracted') for ifile in flist: ifile = 'fullspec' + ifile.strip() exptime = iraf.hsel(ifile, 'exptime', 'yes', Stdout=1)[0] print ifile, exptime iraf.hedit(ifile, 'exptime', value=float(exptime) / norders,
fields='mean', format='no', Stdout=1)[0] median = iraf.imstat('master$flat-' + filter, fields='midpt', format='no', Stdout=1)[0] iraf.imarith(operand1='master$flat-' + filter, op='/', operand2=median, result='master$norm-flat-' + filter) # Replace the values up to 0.5 with 0 iraf.imreplace('master$norm-flat-' + filter, value=0.0, lower='INDEF', upper=0.5) ########################################################################### # Make comment if not config.noReduce: iraf.hedit('master$norm-flat-' + filter, 'OBS_I', 'Combined flats, bias removed', add='yes', ver='no') ########################################################################### print