def get_stat_from_files(input, stat): iraf.images() result = [] for image in input: result.append( float(iraf.imstat(image, fields=stat, format=0, Stdout=1)[0])) return result
def mosfire_geoxytrans(x_kfp, y_kfp, transform="final.pix2mm.4.972.120k", database="ale/10March2011.4.972.db", direction="forward"): '''Conveninece wrapper around IRAF geoxytran''' iraf.images() path = os.path.join(os.environ["MOSPATH"], "platescale", "10March2011.4.972.db") database = path pars = iraf.geoxytran.getParList() iraf.geoxytran.unlearn() ins = [] for i in range(len(x_kfp)): ins.append("%f %f" % (x_kfp[i], y_kfp[i])) results = iraf.geoxytran("STDIN", "STDOUT", Stdin=ins, Stdout=1, database=database, transform=transform, direction=direction) iraf.geoxytran.setParList(pars) poss = [] for result in results: poss.append(np.array(result.split(), dtype=np.float64)) return np.array(poss)
def imarith(operand1, op, operand2, result, doAirmass=False): from pyraf import iraf iraf.images() pars = iraf.imarith.getParList() iraf.imcombine.unlearn() try: os.remove(result) except: pass print "%s %s %s -> %s" % (operand1, op, operand2, result) iraf.imarith(operand1=operand1, op=op, operand2=operand2, result=result) iraf.imarith.setParList(pars) if doAirmass: # Adjust FITS header with pf.open(operand1) as f: am1 = f[0].header['airmass'] with pf.open(operand2) as f: am2 = f[0].header['airmass'] of = pf.open(result) of[0].header['airmass1'] = am1 of[0].header['airmass2'] = am2 of.writeto(result, clobber=True)
def mosfire_geoxytran(x_kfp, y_kfp, transform="final.pix2mm.4.972.120k", database="/platescale/10March2011.4.972.db", direction="forward"): '''Conveninece wrapper around IRAF geoxytran''' iraf.images() path = os.path.join(os.environ["MOSPATH"], "platescale", "10March2011.4.972.db") database = path pars = iraf.geoxytran.getParList() iraf.geoxytran.unlearn() t = iraf.geoxytran("STDIN", "STDOUT", Stdin=["%f %f" % (x_kfp, y_kfp)], Stdout=1, database=database, transform=transform, direction=direction) iraf.geoxytran.setParList(pars) (x, y) = np.array(t[0].split(), dtype=np.float64) return (x, y)
def reduce_images(progargs): """Top level function to perform the reduction of data images. The tasks are performed sequentially: generate all masterbias, generate all masterflats and finally reduce data images. Args: progargs: Program arguments. """ logging.info("Starting the reduction of images ...") # Load the images package and does not show any output. iraf.images(_doprint=0) # Generate all the average bias. generate_all_masterbias(progargs.target_dir, progargs.bias_directory) # Generate all the average dark. generate_all_masterdark(progargs.target_dir, progargs.dark_directory, progargs.bias_directory) # Generate all the average flat. generate_all_masterflats(progargs.target_dir, progargs.flat_directory, progargs.dark_directory, progargs.bias_directory) # Reduce all the data images applying the average bias and flats. reduce_data_images(progargs.target_dir, progargs.light_directory, progargs.dark_directory, progargs.bias_directory, progargs.flat_directory) logging.info("Finished the reduction of images.")
def test_whereis(tmpdir): iraf.plot(_doprint=0) iraf.images(_doprint=0) outfile = str(tmpdir.join('output.txt')) cases = ("pw", "lang", "st", "std", "stdg", "stdpl", "star", "star man", "vi", "noao", "impl", "ls", "surf", "surf man", "smart man", "img", "prot", "pro", "prow", "prows", "prowss", "dis", "no") # Test the whereis function for arg in cases: args = arg.split(" ") # convert to a list iraf.printf("--> whereis " + arg + '\n', StdoutAppend=outfile) kw = {'StderrAppend': outfile} iraf.whereis(*args, **kw) # catches stdout+err with open(outfile) as f: for line in f: if 'task not found' in line: continue row = line.split() if line.startswith('-->'): cmd = row[2] if len(row) > 3: cmd2 = row[3] elif row[0] == 'user.man' and cmd2 == 'man': pass else: assert all([s.split('.')[1].startswith(cmd) for s in row]), \ '{}'.format(row)
def __init__(self, datatype=None): self.datatype=datatype self.fn_pattern=re.compile(".*") iraf.images() iraf.proto() self.statarea=StatArea return
def imcombine(filelist, out, options, bpmask=None, reject="none", nlow=None, nhigh=None): '''Convenience wrapper around IRAF task imcombine Args: filelist: The list of files to imcombine out: The full path to the output file options: Options dictionary bpmask: The full path to the bad pixel mask reject: none, minmax, sigclip, avsigclip, pclip nlow,nhigh: Parameters for minmax rejection, see iraf docs Returns: None Side effects: Creates the imcombined file at location `out' ''' #TODO: REMOVE Iraf and use python instead. STSCI Python has # A builtin routine. from pyraf import iraf iraf.images() filelist = [("%s[0]" % f) for f in filelist] pars = iraf.imcombine.getParList() iraf.imcombine.unlearn() path = "flatcombine.lst" f = open(path, "w") for file in filelist: f.write(file + "\n") f.close() s = ("%s," * len(filelist))[0:-1] s = s % tuple(filelist) f = open("flatcombinelog.txt", "w") if reject == 'minmax': t = iraf.imcombine("@%s" % path, out, Stdout=f, reject=reject, nlow=nlow, nhigh=nhigh) else: t = iraf.imcombine(s, out, Stdin=filelist, Stdout=f, reject=reject) f.close() f = open("flatcombinelog.txt") for line in f: info(line.rstrip("\n")) f.close() iraf.imcombine.setParList(pars)
def display_image(img,frame,_z1,_z2,scale,_xcen=0.5,_ycen=0.5,_xsize=1,_ysize=1,_erase='yes'): goon='True' import glob, subprocess, os, time ds9 = subprocess.Popen("ps -U {:d} u | grep -v grep | grep ds9".format(os.getuid()),shell=True,stdout=subprocess.PIPE).stdout.readlines() if len(ds9)== 0 : subproc = subprocess.Popen('ds9',shell=True) time.sleep(3) if glob.glob(img): from pyraf import iraf iraf.images(_doprint=0) iraf.tv(_doprint=0) import string,os if _z2: try: sss=iraf.display(img + '[0]', frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,\ fill='yes', zscale='no', zrange='no', z1=_z1, z2=_z2,Stdout=1) except: print '' print '### ERROR: PROBLEM OPENING DS9' print '' goon='False' else: try: sss=iraf.display(img + '[0]', frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase, fill='yes', Stdout=1) except: print '' print '### ERROR: PROBLEM OPENING DS9' print '' goon=False if scale and goon: answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ') if not answ0: answ0='y' elif answ0=='no' or answ0=='NO': answ0='n' while answ0=='n': _z11=float(string.split(string.split(sss[0])[0],'=')[1]) _z22=float(string.split(string.split(sss[0])[1],'=')[1]) z11 = raw_input('>>> z1 = ? ['+str(_z11)+'] ? ') z22 = raw_input('>>> z2 = ? ['+str(_z22)+'] ? ') if not z11: z11=_z11 else: z11=float(z11) if not z22: z22=_z22 else: z22=float(z22) print z11,z22 sss=iraf.display(img + '[0]',frame,fill='yes', xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,\ zrange='no', zscale='no', z1=z11, z2=z22, Stdout=1) answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ') if not answ0: answ0='y' elif answ0=='no' or answ0=='NO': answ0='n' if goon: _z1,_z2=string.split(string.split(sss[0])[0],'=')[1],string.split(string.split(sss[0])[1],'=')[1] else: print 'Warning: image '+str(img)+' not found in the directory ' return _z1,_z2,goon
def rejectbias(lista, _interactive, nn=10): # print "LOGX:: Entering `rejectbias` method/function in %(__file__)s" % # globals() import numpy as np import ntt from pyraf import iraf iraf.images(_doprint=0) listgood = [] f = open('_listgoodbias', 'w') for img in lista: f.write(img + '\n') f.close() biasstd = np.array(iraf.imstat( '@_listgoodbias', fields='stddev', format='no', Stdout=1), float) # biasmedian=np.array(iraf.imstat('@_listgoodbias', fields='mean',format='no',Stdout=1),float) # print lista median = np.median(biasstd) sigma = (np.percentile(biasstd, 75) - np.percentile(biasstd, 25)) * 1.349 lista1 = np.compress((biasstd < (median + nn * sigma)) & (biasstd > (median - nn * sigma)), np.array(lista)) # lista1=np.compress((np.mean(biasstd)-2*np.std(biasstd)<=biasstd)&(np.mean(biasstd)+2*np.std(biasstd)>=biasstd)& # (np.mean(biasmedian)-2*np.std(biasmedian)<=biasmedian)& # (np.mean(biasmedian)+2*np.std(biasmedian)>=biasmedian), np.array(lista)) ntt.util.delete('_listgoodbias') for i in range(0, len(lista)): if lista[i] not in lista1: print lista[i] + ' ' + str(biasstd[i]) + ' rejected' else: print lista[i], biasstd[i] for img in lista1: if _interactive: aa, bb, cc = ntt.util.display_image(img, 1, '', '', False) titolo, result = iraf.imstat(img, Stdout=1) print titolo[1:] print result answ = 'nn' while answ not in ['g', 'G', 'b', 's']: answ = raw_input('good/bad [[g]/b/G(all good)/s(stop) ] ? ') if not answ: answ = 'g' if answ not in ['g', 'G', 'b', 's']: print 'warning: value not valid, try again' if answ == 'g': listgood.append(img) elif answ == 'G': listgood = lista1 break elif answ == 's': break else: listgood.append(img) return listgood
def saveset_peakcoords(basename, threshold=100, hwhm=3): """ Attempts to find the peak of each saveset in an already tprepared image. Parameters: ----------- basename : string Everything before the .fits threshold : number Detection threshold for images.imcoords.starfind in counts Returns: -------- c : numpy.array Row stacked coordinates for each of the savesets """ #deleting files from previous runs iraf.delete('*fits*obj*') iraf.delete('images_ext.lst') iraf.images() iraf.imcoords() exts = len(pf.open(basename + '.fits')) - 1 f = open('images_ext.lst', 'w') for i in range(exts): f.write(basename + '.fits[' + str(i + 1) + '][*,*,3]\n') f.close() iraf.starfind(r'@images_ext.lst', 'default', hwhm, threshold) a = [basename + '.fits' + str(i + 1) + '.obj.1' for i in range(exts)] b = [loadtxt(a[i]) for i in range(exts) if len(loadtxt(a[i])) != 0] c = row_stack(b) f = open(a[0], 'r') d = f.readlines() f.close() header = [i for i in d if i[0] == '#'] f = open(basename + '_coords.dat', 'w') for i in header: f.write(i) f.write('\n') savetxt(f, c, fmt='%.3f') f.close() iraf.delete('*fits*obj*') iraf.delete('images_ext.lst') return c
def imarith(operand1, op, operand2, result): from pyraf import iraf iraf.images() pars = iraf.imarith.getParList() iraf.imcombine.unlearn() print "%s %s %s -> %s" % (operand1, op, operand2, result) iraf.imarith(operand1=operand1, op=op, operand2=operand2, result=result) iraf.imarith.setParList(pars)
def imcombine(filelist, out, options, bpmask=None, reject="none", nlow=None, nhigh=None): '''Convenience wrapper around IRAF task imcombine Args: filelist: The list of files to imcombine out: The full path to the output file options: Options dictionary bpmask: The full path to the bad pixel mask reject: none, minmax, sigclip, avsigclip, pclip nlow,nhigh: Parameters for minmax rejection, see iraf docs Returns: None Side effects: Creates the imcombined file at location `out' ''' #TODO: REMOVE Iraf and use python instead. STSCI Python has # A builtin routine. from pyraf import iraf iraf.images() filelist = [("%s[0]" % f) for f in filelist] pars = iraf.imcombine.getParList() iraf.imcombine.unlearn() path = "flatcombine.lst" f = open(path, "w") for file in filelist: f.write(file + "\n") f.close() s = ("%s," * len(filelist))[0:-1] s = s % tuple(filelist) f = open("flatcombinelog.txt", "w") if reject == 'minmax': t = iraf.imcombine("@%s" % path, out, Stdout=f, reject=reject, nlow=nlow, nhigh=nhigh) else: t = iraf.imcombine(s, out, Stdin=filelist, Stdout=f, reject=reject) f.close() f=open("flatcombinelog.txt") for line in f: info(line.rstrip("\n")) f.close() iraf.imcombine.setParList(pars)
def get_ao_performance_data(): # Retrieve information from image headers outfile = workdir + 'data/ao_perform.txt' ir.images() ir.imutil() ir.hselect('*_img.fits', 'DATAFILE,DATE-OBS,UTC,AOLBFWHM,LGRMSWF', 'yes', Stdout=outfile) # Retrieve MASS/DIMM data files # get_mass_dimm.go('20081021') # this one doesn't exist get_mass_dimm.go('20100815') get_mass_dimm.go('20100828') get_mass_dimm.go('20100829')
def imcombine(filelist, out, listfile=None, bpmask=None, reject="none", nlow=None, nhigh=None): """Convenience wrapper around IRAF task imcombine Args: filelist (list of str): The list of files to imcombine out (str): The full path to the output file bpmask (str): The full path to the bad pixel mask reject (str): none, minmax, sigclip, avsigclip, pclip nlow,nhigh (int,int): Parameters for minmax rejection, see iraf docs Returns: None Side effects: Creates the imcombined file at location `out` """ #TODO: REMOVE Iraf and use python instead. STSCI Python has # A builtin routine. from pyraf import iraf iraf.images() filelist = [("%s[0]" % f) for f in filelist] pars = iraf.imcombine.getParList() iraf.imcombine.unlearn() if listfile is None: path = "flatcombine.lst" else: path = listfile f = open(path, "w") for file in filelist: f.write(file + "\n") f.close() s = ("%s," * len(filelist))[0:-1] s = s % tuple(filelist) if reject == 'minmax': t = iraf.imcombine("@%s" % path, out, combine="average", reject=reject, nlow=nlow, nhigh=nhigh) elif reject == 'sigclip': t = iraf.imcombine("@%s" % path, out, combine="average", reject=reject, lsigma=nlow, hsigma=nhigh) else: t = iraf.imcombine(s, out, Stdin=filelist, Stdout=1, combine="average", reject=reject) iraf.imcombine.setParList(pars)
def sub_bias(image, combined_bias, image_b): # Import IRAF modules: iraf.images(_doprint=0) iraf.imutil(_doprint=0) parList = "bias_subtraction_imarith.par" # Check input file and combined_bias frame exists before proceeding: if os.path.isfile(image) == True: if os.path.isfile(combined_bias) == True: if os.path.isfile(parList) == True: # Subtract combined bias frame from input frame (object or flat) # using IRAF task imarithmetic: iraf.imarith.setParList(ParList="bias_subtraction_imarith.par") iraf.imarith(operand1=image, operand2=combined_bias, result=image_b) print ' ' print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print 'Bias frame ' + str(combined_bias) print 'subtracted from input ' + str(image) print 'to create ' + str(image_b) print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print ' ' else: print ' ' print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print 'Bias frame subtraction IRAF .par file ' print str(parList) print 'does not exist. Exiting script. ' print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print ' ' print ' ' sys.exit() else: print ' ' print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print 'Combined bias frame ' print str(combined_bias) print 'does not exist. Exiting script. ' print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print ' ' print ' ' sys.exit() else: print ' ' print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print 'Input frame ' print str(image) print 'does not exist. Exiting script. ' print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print ' ' print ' ' sys.exit()
def image_shift(targetdir): """ Shift all B-A images by 48 pixels in y-direction """ print 'Target directory is ' + targetdir print 'Shifting images...' innames, outnames = [], [] for n in os.listdir(targetdir): if (n.endswith('.fit')) & (n.startswith('dfcr')): outnames.append(os.path.join(targetdir,'s' + n ) ) innames.append(os.path.join(targetdir,n) ) if os.path.exists( os.path.join(targetdir,'s' + n) ): os.remove(os.path.join(targetdir,'s' + n)) print 'Removing file ' + os.path.join(targetdir,'s' + n) with open(os.path.join(targetdir,'input.list'), 'w') as f: for name in innames: print name.replace(targetdir + '/','') f.write( name + '\n' ) with open(os.path.join(targetdir,'output.list'), 'w') as f: for name in outnames: f.write( name + '\n' ) names = np.array([name.replace('dfcr','r') + '[1]' for name in innames]) decoff = np.asarray([iraf.hselect(f , 'DECOFF', 'yes', Stdout=1)[0] for f in names],dtype='float') with open(os.path.join(targetdir,'shifts.lst'),'w') as f: for d in decoff: if d >= -1.0: yshift = '0.0' else: yshift = '-48.0' f.write('0.0 ' + yshift + '\n') iraf.images(_doprint=0) iraf.imgeom(_doprint=0) iraf.imshift.setParam('input', '@' + os.path.join(targetdir, 'input.list')) iraf.imshift.setParam('output', '@' + os.path.join(targetdir, 'output.list')) iraf.imshift.setParam('shifts_file', os.path.join(targetdir,'shifts.lst') ) iraf.imshift() return None
def marksn2(img,fitstab,frame=1,fitstab2='',verbose=False): from pyraf import iraf from numpy import array #,log10 import lsc iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.images(_doprint=0) iraf.imcoords(_doprint=0) iraf.proto(_doprint=0) iraf.set(stdimage='imt1024') hdr=lsc.util.readhdr(fitstab) _filter=lsc.util.readkey3(hdr,'filter') column=lsc.lscabsphotdef.makecatalogue([fitstab])[_filter][fitstab] rasex=array(column['ra0'],float) decsex=array(column['dec0'],float) if fitstab2: hdr=lsc.util.readhdr(fitstab2) _filter=lsc.util.readkey3(hdr,'filter') _exptime=lsc.util.readkey3(hdr,'exptime') column=lsc.lscabsphotdef.makecatalogue([fitstab2])[_filter][fitstab2] rasex2=array(column['ra0'],float) decsex2=array(column['dec0'],float) iraf.set(stdimage='imt1024') iraf.display(img + '[0]',frame,fill=True,Stdout=1) vector=[] for i in range(0,len(rasex)): vector.append(str(rasex[i])+' '+str(decsex[i])) xy = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\ formats='%10.1f %10.1f',verbose='yes')[3:] iraf.tvmark(frame,'STDIN',Stdin=list(xy),mark="circle",number='yes',label='no',radii=10,nxoffse=5,nyoffse=5,color=207,txsize=2) if verbose: # print 2.5*log10(_exptime) for i in range(0,len(column['ra0'])): print xy[i],column['ra0'][i],column['dec0'][i],column['magp3'][i],column['magp4'][i],column['smagf'][i],column['magp2'][i] if fitstab2: vector2=[] for i in range(0,len(rasex2)): vector2.append(str(rasex2[i])+' '+str(decsex2[i])) xy1 = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector2,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\ formats='%10.1f %10.1f',verbose='yes')[3:] iraf.tvmark(frame,'STDIN',Stdin=list(xy1),mark="cross",number='yes',label='no',radii=10,nxoffse=5,nyoffse=5,color=205,txsize=2)
def Geoxy(path): print "Starting Geoxy..." filename = path os.chdir(filename) corrected = glob.glob("*.xy") #Removing existing files filelist = glob.glob("*.done") for f in filelist: os.remove(f) #Transforming coordinates print "Saving files with the new coordinates." for i in corrected: iraf.images(_doprint=0) iraf.immatch(_doprint=0) iraf.geoxy(input=i, output=i+'.done', database='data3', transforms='trans3')
def rejectflat(lista, _interactive): from numpy import where, size from ntt.util import display_image from pyraf import iraf iraf.images(_doprint=0) import os import string listone = [] for img in lista: dataimg = pyfits.open(img)[0].data numberpixels = size(dataimg) indices = where(dataimg > 60000) numbersaturated = size(dataimg[indices]) indices2 = where(dataimg < 1000) numberlow = size(dataimg[indices2]) if 100 * float(numbersaturated) / float(numberpixels) <= 5 and \ 100 * float(numberlow) / float(numberpixels) <= 10: listone.append(img) listgood = [] if _interactive: for img in listone: aa, bb, cc = display_image(img, 1, '', '', False) titolo, result = iraf.imstat(img, Stdout=1) print titolo[1:] print result answ = 'nn' while answ not in ['g', 'G', 'b', 's']: answ = raw_input('good/bad [[g]/b/G(all good)/s(stop)]? ') if not answ: answ = 'g' if answ not in ['g', 'G', 'b', 's']: print 'warning: value not valid, try again' if answ == 'g': listgood.append(img) elif answ == 'G': listgood = listone break elif answ == 's': break else: listgood = listone return listgood
def test_which(tmpdir): iraf.plot(_doprint=0) iraf.images(_doprint=0) outfile = str(tmpdir.join('output.txt')) # To Test: normal case, disambiguation, ambiguous, not found, multiple # inputs for a single command cases = ("pw", "lang", "stdg", "stdp", "star", "star man", "vi", "noao", "impl", "ls", "surf", "surface", "img", "pro", "prot", "prow", "prows", "prowss", "dis") # Test the which function for arg in cases: args = arg.split(" ") # convert to a list iraf.printf("--> which " + arg + '\n', StdoutAppend=outfile) kw = {"StderrAppend": outfile} iraf.which(*args, **kw) # catches stdout+err diff_outputs(outfile, 'data/cli_which_output.ref')
def init_iraf(): """Initializes the pyraf environment. """ # The display of graphics is not used, so skips Pyraf graphics # initialization and run in terminal-only mode to avoid warning messages. os.environ['PYRAF_NO_DISPLAY'] = '1' # Set PyRAF process caching off to avoid errors if spawning multiple # processes. iraf.prcacheOff() # Load iraf packages and does not show any output of the tasks. iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.images(_doprint=0) # Set the iraf.phot routine to scripting mode. iraf.phot.interactive = "no" iraf.phot.verify = "no"
def init_iraf(): """Initializes the pyraf environment. """ # The display of graphics is not used, so skips Pyraf graphics # initialization and run in terminal-only mode to avoid warning messages. os.environ['PYRAF_NO_DISPLAY'] = '1' # Set PyRAF process caching off to avoid errors if spawning multiple # processes. iraf.prcacheOff() # Load iraf packages and does not show any output of the tasks. iraf.digiphot(_doprint = 0) iraf.apphot(_doprint = 0) iraf.images(_doprint = 0) # Set the iraf.phot routine to scripting mode. iraf.phot.interactive = "no" iraf.phot.verify = "no"
def rvsao(bin_sci, task, template_spectra, rvsao_file, rvsao_bin_list, interactive="no", linesig=1.5, czguess=0., st_lambda="INDEF", end_lambda="INDEF", badlines=None): """ Use the rvsao task emsao or xcsao to measure relative velocity from emission or absoption spectra Note: make sure you select the parameters for your desired use. Refer to the IRAF rvsao package help for description of what each parameter is/does """ assert task == 'xcsao' or task == 'emsao', "task is not either 'xcsao' or 'emsao'" if os.path.exists(rvsao_file): print('File {} already exists'.format(rvsao_file)) return fixbad = "no" if badlines: fixbad = "yes" bin_list = [] for i in range(len(glob.glob(bin_sci.format('*')))): # to ensure order is 0-61 (not 0, 1, 10, 11, etc) bin_list.append(bin_sci.format(i)) assert len(bin_list) > 0, 'Absorption/emission(?) bin spectra do not exist: {}'.format(em_bin_sci.format('*')) np.array(bin_list).tofile(rvsao_bin_list, sep='\n') from pyraf import iraf iraf.images() iraf.rvsao() if task == 'xcsao': iraf.xcsao('@{}'.format(rvsao_bin_list), templates=bin_sci.format(template_spectra), report_mode=2, logfiles=rvsao_file, displot=interactive, low_bin=10, top_low=20, top_nrun=80, nrun=211, zeropad="yes", nzpass=1, curmode="no", pkmode=2, s_emchop="no", vel_init="guess", czguess=czguess, st_lambda=st_lambda, end_lambda=end_lambda, fixbad=fixbad, badlines=badlines) elif task == 'emsao': # Run this interactively as the fit can fail often. Depending on S/N you may have to decrease the linesig # to detect the lines, or if lines are matched inconsistently play with the st_lambda, end_lambda parameters iraf.emsao('@{}'.format(rvsao_bin_list), logfiles=rvsao_file, displot=interactive, report_mode=2, contsub_plot="no", st_lambda=st_lambda, end_lambda=end_lambda, # vel_init="guess", czguess=czguess, linesig=linesig, fixbad=fixbad, badlines=badlines)
def mosfire_geoxytran(x_kfp, y_kfp, transform="final.pix2mm.4.972.120k", database="/platescale/10March2011.4.972.db", direction="forward"): '''Conveninece wrapper around IRAF geoxytran''' iraf.images() path = os.path.join(os.environ["MOSPATH"], "platescale", "10March2011.4.972.db") database = path pars = iraf.geoxytran.getParList() iraf.geoxytran.unlearn() t = iraf.geoxytran("STDIN", "STDOUT", Stdin=["%f %f" % (x_kfp, y_kfp)], Stdout=1, database=database, transform=transform, direction=direction) iraf.geoxytran.setParList(pars) (x,y) = np.array(t[0].split(), dtype=np.float64) return (x,y)
def rotate(imlist_name, rotation): """ rotate and shift a list of images Rotate input output rotation Angle of rotation of the image in degrees. Positive angles will rotate the image counter-clockwise from the x axis. In the case of SAO 1-m longslit data, 180 degree rotation is required for easy wavelength calibration. """ import glob import os, sys from pyraf import iraf iraf.images() iraf.imgeom() imlist = glob.glob(imlist_name) imlist.sort() for i in range(len(imlist)): inim = imlist[i] print('Rotate ' + inim + ' with angle of ' + str(round(rotation, 3))) iraf.rotate(input=inim, output='r' + inim, rotation=rotation) print('r' + inim + ' is created.')
def cmd_imageCube(self, the_command): '''This function can be used to pull a series of images from the camera and coadd them in a simple way. This is slightly better process for measuring the position of a star for the purposes of guiding. In essence, this will take 10 images, average them and create a master image for analysis to be perfomed on.''' comands = str.split(the_command) if len(comands) != 3: return 'Please specify the name of the final image and the number of images to median through. Alternatively, specify "high" instead of the number of images to acquire a high enough number of average over scintilation.' if comands[2] == 'high': nims = 30 #3E4/self.set_values[2] else: try: nims = int(comands[2]) except Exception: return 'Unable to convert number of images to integer' #make upperlimit images and average combine them. upperlimit = int(nims) base_filename = comands[1] if base_filename in commands.getoutput('ls program_images/'): os.system('rm program_images/' + base_filename + '*') print 'Starting to capture images' capture = self.capture_images('program_images/' + base_filename, upperlimit, show=False) if not capture: return 'ERROR capturing images' print 'Finished capturing images' self.check_if_file_exists('program_images/' + base_filename + '.fits') self.check_if_file_exists('program_images/inlist') iraf.images(_doprint=0) os.system('ls program_images/' + base_filename + '_*.fits > inlist') try: iraf.imcombine(input='@inlist', output='program_images/' + base_filename + '.fits', combine='average', reject='none', outtype='integer', scale='none', zero='none', weight='none') except Exception: return 'Could not combine images' return 'Final image created. It is image program_images/' + base_filename + '.fits'
def flat_field(object_b, combined_flat_b_n, object_b_fn): # Import IRAF modules: iraf.images(_doprint=0) iraf.imutil(_doprint=0) # Check input file and combined_flat frame exist before proceeding: if os.path.isfile(object_b) == True: if os.path.isfile(combined_flat_b_n) == True: # Divide bias-subtracted object frame by normalized, bias # subtracted combined flat frame using IRAF task imarithmetic: iraf.imarith(operand1=object_b, op="/", operand2=combined_flat_b_n, result=object_b_fn) print ' ' print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print 'Bias-subtracted object frame ' print str(object_b) print 'successfully flat-fielded using division by ' print str(combined_flat_b_n) print 'to create bias-subtracted, normalized flat-fielded ' print str(object_b_fn) + ' frame.' print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print ' ' else: print ' ' print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print 'Combined flat frame ' print str(combined_flat_b_n) print 'does not exist. Exiting script. ' print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print ' ' print ' ' sys.exit() else: print ' ' print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print 'Input frame ' print str(object_b) print 'does not exist. Exiting script. ' print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print ' ' print ' ' sys.exit()
def mosfire_geoxytrans( x_kfp, y_kfp, transform="final.pix2mm.4.972.120k", database="ale/10March2011.4.972.db", direction="forward" ): """Conveninece wrapper around IRAF geoxytran""" iraf.images() path = os.path.join(os.environ["MOSPATH"], "platescale", "10March2011.4.972.db") database = path pars = iraf.geoxytran.getParList() iraf.geoxytran.unlearn() ins = [] for i in range(len(x_kfp)): ins.append("%f %f" % (x_kfp[i], y_kfp[i])) results = iraf.geoxytran( "STDIN", "STDOUT", Stdin=ins, Stdout=1, database=database, transform=transform, direction=direction ) iraf.geoxytran.setParList(pars) poss = [] for result in results: poss.append(np.array(result.split(), dtype=np.float64)) return np.array(poss)
def shift_master(ref, master): """ Find the shift between two images. returns the shift in y direction (along columns) """ #make a narow copy of the reference flat and masterflat iraf.imcopy(input="../"+ref+"[1990:2010,*]", output="tmp/masterflat_ref.fitscut",Stdout="/dev/null") iraf.imcopy(input="tmp/masterflat.fits[1990:2010,*]", output="tmp/masterflat.fitscut",Stdout="/dev/null") #find the shift with correlation iraf.images(_doprint=0,Stdout="/dev/null") iraf.immatch(_doprint=0,Stdout="/dev/null") shift=iraf.xregister(input="tmp/masterflat_ref.fitscut, tmp/masterflat.fitscut", reference="tmp/masterflat_ref.fitscut", regions='[*,*]', shifts="tmp/mastershift", output="", databasefmt="no", correlation="fourier", xwindow=3, ywindow=51, xcbox=21, ycbox=21, Stdout=1) shift=float(shift[-2].split()[-2]) #Shift the list of apertures f=open(master, "r") o=open("database/aptmp_masterflat_s", "w") for line in f: l=line.split() if len(l)>0 and l[0]=='begin': l[-1]=str(float(l[-1])-shift) o.write(l[0]+"\t"+l[1]+"\t"+"tmp/masterflat_s"+"\t"+l[3]+"\t"+l[4]+"\t"+l[5]+"\n") elif len(l)>0 and l[0]=='center': l[-1]=str(float(l[-1])-shift) o.write("\t"+l[0]+"\t"+l[1]+"\t"+l[2]+"\n") elif len(l)>0 and l[0]=='image': o.write("\t"+"image"+"\t"+"tmp/masterflat_s"+"\n") else: o.write(line) f.close() o.close()
def makeflat(lista): # print "LOGX:: Entering `makeflat` method/function in %(__file__)s" % # globals() flat = '' import datetime import glob import os import ntt from ntt.util import readhdr, readkey3, delete, name_duplicate, updateheader, correctcard from pyraf import iraf iraf.images(_doprint=0) iraf.imutil(_doprint=0) iraf.imgeom(_doprint=0) # iraf.blkavg(_doprint=0) iraf.noao(_doprint=0) iraf.imred(_doprint=0) iraf.generic(_doprint=0) toforget = ['imgeom.blkavg', 'imutil.imarith', 'immatch.imcombine', 'noao.imred'] for t in toforget: iraf.unlearn(t) import datetime MJDtoday = 55927 + (datetime.date.today() - datetime.date(2012, 01, 01)).days _date = readkey3(readhdr(lista[0]), 'date-night') _filter = readkey3(readhdr(lista[0]), 'filter') output = name_duplicate( lista[3], 'flat_' + str(_date) + '_' + str(_filter) + '_' + str(MJDtoday), '') if os.path.isfile(output): answ = raw_input('file already prooduced, do again [y/[n]] ? ') if not answ: answ = 'n' else: answ = 'y' if answ in ['yes', 'y', 'YES', 'Y', 'Yes']: delete("temp_off.fits,temp_off_mask.fits,temp_on_mask.fits,temp_on.fits") iraf.image.immatch.imcombine( lista[0] + ',' + lista[7], output="temp_off.fits") iraf.image.immatch.imcombine( lista[1] + ',' + lista[6], output="temp_off_mask.fits") iraf.image.immatch.imcombine( lista[2] + ',' + lista[5], output="temp_on_mask.fits") iraf.image.immatch.imcombine( lista[3] + ',' + lista[4], output="temp_on.fits") # create the bias correction for the flat-on according to the # Lidman technique0 delete("temp_onA.fits,temp_onC.fits,temp_onB.fits,temp_onAC.fits,temp_onACB.fits,temp_onACB_2D.fits") delete("temp_on_bias.fits") iraf.imgeom.blkavg( input="temp_on.fits[500:600,*]", output="temp_onA.fits", option="average", b1=101, b2=1) iraf.imgeom.blkavg( input="temp_on_mask.fits[500:600,*]", output="temp_onC.fits", option="average", b1=101, b2=1) iraf.imgeom.blkavg( input="temp_on_mask.fits[50:150,*]", output="temp_onB.fits", option="average", b1=101, b2=1) iraf.imutil.imarith("temp_onA.fits", "-", "temp_onC.fits", "temp_onAC.fits") iraf.imutil.imarith("temp_onAC.fits", "+", "temp_onB.fits", "temp_onACB.fits") iraf.imgeom.blkrep(input="temp_onACB.fits", output="temp_onACB_2D.fits", b1=1024, b2=1) iraf.imutil.imarith("temp_on.fits", "-", "temp_onACB_2D.fits", "temp_on_bias.fits") # same as above for the flat-off delete("temp_offA.fits,temp_offC.fits,temp_offB.fits,temp_offAC.fits,temp_offACB.fits,temp_offACB_2D.fits") delete("temp_off_bias.fits") iraf.imgeom.blkavg( input="temp_off.fits[500:600,*]", output="temp_offA.fits", option="average", b1=101, b2=1) iraf.imgeom.blkavg( input="temp_off_mask.fits[500:600,*]", output="temp_offC.fits", option="average", b1=101, b2=1) iraf.imgeom.blkavg( input="temp_off_mask.fits[50:150,*]", output="temp_offB.fits", option="average", b1=101, b2=1) iraf.imutil.imarith("temp_offA.fits", "-", "temp_offC.fits", "temp_offAC.fits") iraf.imutil.imarith("temp_offAC.fits", "+", "temp_offB.fits", "temp_offACB.fits") iraf.imgeom.blkrep(input="temp_offACB.fits", output="temp_offACB_2D.fits", b1=1024, b2=1) iraf.imutil.imarith("temp_off.fits", "-", "temp_offACB_2D.fits", "temp_off_bias.fits") # create the corrected flat-field # output=name_duplicate("temp_on_bias.fits",'flat_'+str(_date)+'_'+str(_filter)+'_'+str(MJDtoday),'') output = name_duplicate( lista[3], 'flat_' + str(_date) + '_' + str(_filter) + '_' + str(MJDtoday), '') # print lista[0],'flat_'+str(_date)+'_'+str(_filter)+'_'+str(MJDtoday) delete(output) iraf.imutil.imarith("temp_on_bias.fits", "-", "temp_off_bias.fits", output) iraf.noao.imred.generic.normalize(output) # normalize the flat-field correctcard(output) delete("temp_on*.fits") # delete the temporary images delete("temp_off*.fits") print 'flat -> ' + str(output) else: print 'skip redoing the flat' return output
import shutil import numpy as np import matplotlib.pyplot as plt from datetime import date import operator import time import traceback from lmfit import minimize, Parameters, report_fit #import cosmics iraf.noao(_doprint=0,Stdout="/dev/null") iraf.rv(_doprint=0,Stdout="/dev/null") iraf.imred(_doprint=0,Stdout="/dev/null") iraf.ccdred(_doprint=0,Stdout="/dev/null") iraf.images(_doprint=0,Stdout="/dev/null") iraf.immatch(_doprint=0,Stdout="/dev/null") iraf.onedspec(_doprint=0,Stdout="/dev/null") iraf.twodspec(_doprint=0,Stdout="/dev/null") iraf.apextract(_doprint=0,Stdout="/dev/null") iraf.imutil(_doprint=0,Stdout="/dev/null") iraf.echelle(_doprint=0,Stdout="/dev/null") iraf.astutil(_doprint=0,Stdout="/dev/null") iraf.apextract.dispaxi=1 iraf.echelle.dispaxi=1 #fixes a bug with latest versions of iraf iraf.ccdred.instrum='blank.txt' os.environ['PYRAF_BETA_STATUS'] = '1'
def run_imstat(input): iraf.images() for image in input: iraf.imstat(image)
def telluric_atmo(imgstd): # print "LOGX:: Entering `telluric_atmo` method/function in %(__file__)s" # % globals() import numpy as np import ntt from pyraf import iraf try: import pyfits except: from astropy.io import fits as pyfits iraf.images(_doprint=0) iraf.noao(_doprint=0) iraf.twodspec(_doprint=0) iraf.longslit(_doprint=0) iraf.onedspec(_doprint=0) toforget = [ 'imfilter.gauss', 'specred.apall', 'longslit.identify', 'longslit.reidentify', 'specred.standard', 'onedspec.wspectext' ] for t in toforget: iraf.unlearn(t) _grism = ntt.util.readkey3(ntt.util.readhdr(imgstd), 'grism') imgout = 'invers_atmo_' + imgstd ntt.util.delete(imgout) iraf.set(direc=ntt.__path__[0] + '/') _cursor = 'direc$standard/ident/cursor_sky_0' iraf.noao.onedspec.bplot(imgstd, cursor=_cursor, spec2=imgstd, new_ima=imgout, overwri='yes') xxstd, ffstd = ntt.util.readspectrum(imgout) if _grism in ['Gr13', 'Gr16']: llo2 = np.compress( (np.array(xxstd) >= 7550) & (np.array(xxstd) <= 7750), np.array(xxstd)) llh2o = np.compress( (np.array(xxstd) >= 7100) & (np.array(xxstd) <= 7500), np.array(xxstd)) ffo2 = np.compress( (np.array(xxstd) >= 7550) & (np.array(xxstd) <= 7750), np.array(ffstd)) ffh2o = np.compress( (np.array(xxstd) >= 7100) & (np.array(xxstd) <= 7500), np.array(ffstd)) elif _grism in ['Gr11']: llo2 = np.compress( (np.array(xxstd) >= 6830) & (np.array(xxstd) <= 7100), np.array(xxstd)) llh2o = np.compress( (np.array(xxstd) >= 7100) & (np.array(xxstd) <= 7500), np.array(xxstd)) ffo2 = np.compress( (np.array(xxstd) >= 6830) & (np.array(xxstd) <= 7100), np.array(ffstd)) ffh2o = np.compress( (np.array(xxstd) >= 7100) & (np.array(xxstd) <= 7500), np.array(ffstd)) if _grism in ['Gr13', 'Gr16', 'Gr11']: _skyfileh2o = 'direc$standard/ident/ATLAS_H2O.fits' _skyfileo2 = 'direc$standard/ident/ATLAS_O2.fits' atlas_smooto2 = '_atlas_smoot_o2.fits' atlas_smooth2o = '_atlas_smoot_h2o.fits' _sigma = 200 ntt.util.delete(atlas_smooto2) ntt.util.delete(atlas_smooth2o) iraf.imfilter.gauss(_skyfileh2o, output=atlas_smooth2o, sigma=_sigma) iraf.imfilter.gauss(_skyfileo2, output=atlas_smooto2, sigma=_sigma) llskyh2o, ffskyh2o = ntt.util.readspectrum(atlas_smooth2o) llskyo2, ffskyo2 = ntt.util.readspectrum(atlas_smooto2) ffskyo2cut = np.interp(llo2, llskyo2, ffskyo2) ffskyh2ocut = np.interp(llh2o, llskyh2o, ffskyh2o) _scaleh2o = [] integral_h2o = [] for i in range(1, 21): j = 0.6 + i * 0.04 _ffskyh2ocut = list((np.array(ffskyh2ocut) * j) + 1 - j) diff_h2o = abs(_ffskyh2ocut - ffh2o) integraleh2o = np.trapz(diff_h2o, llh2o) integral_h2o.append(integraleh2o) _scaleh2o.append(j) _scaleo2 = [] integral_o2 = [] for i in range(1, 21): j = 0.6 + i * 0.04 _ffskyo2cut = list((np.array(ffskyo2cut) * j) + 1 - j) diff_o2 = abs(_ffskyo2cut - ffo2) integraleo2 = np.trapz(diff_o2, llo2) integral_o2.append(integraleo2) _scaleo2.append(j) sh2o = _scaleh2o[np.argmin(integral_h2o)] so2 = _scaleo2[np.argmin(integral_o2)] telluric_features = ((np.array(ffskyh2o) * sh2o) + 1 - sh2o) + ( (np.array(ffskyo2) * so2) + 1 - so2) - 1 telluric_features = np.array([1] + list(telluric_features) + [1]) llskyo2 = np.array([1000] + list(llskyo2) + [15000]) telluric_features_cut = np.interp(xxstd, llskyo2, telluric_features) _imgout = 'atmo_' + imgstd data1, hdr = pyfits.getdata(imgstd, 0, header=True) data1[0] = np.array(telluric_features_cut) data1[1] = data1[1] / data1[1] data1[2] = data1[2] / data1[2] data1[3] = data1[3] / data1[3] ntt.util.delete(_imgout) pyfits.writeto(_imgout, np.float32(data1), hdr) ntt.util.delete(atlas_smooto2) ntt.util.delete(atlas_smooth2o) ntt.util.delete(imgout) else: _imgout = '' print '### telluric correction with model not possible ' return _imgout
import pyraf from pyraf import iraf import copy, os, shutil, glob, sys, string, re, math, operator, time import pyfits from types import * from mx.DateTime import * from iqpkg import * import ephem # Necessary packages iraf.images() iraf.immatch() iraf.imfilter() iraf.noao() iraf.imred() iraf.ccdred() iraf.digiphot() iraf.apphot() yes=iraf.yes no=iraf.no INDEF=iraf.INDEF hedit=iraf.hedit imgets=iraf.imgets imcombine=iraf.imcombine pyrafdir="python/pyraf/" pyrafdir_key='PYRAFPARS' if os.environ.has_key(pyrafdir_key):
# ------------------------------------------------------------------------------------------------------------------- # # Paths Of Files & Directories To Be Used # ------------------------------------------------------------------------------------------------------------------- # DIR_CURNT = os.getcwd() DIR_HOME = "/home/Avinash/Dropbox/PyCharm/Reduction_Pipeline/" DIR_PHOT = "/home/Avinash/Supernovae_Data/Photometry/" DIR_SPECS = "/home/Avinash/Supernovae_Data/Final_Spectra/" FILE_BANDPASS = os.path.join(DIR_HOME, "Filter_BPF.asc") list_paths = [DIR_HOME, DIR_PHOT, DIR_SPECS, FILE_BANDPASS] # ------------------------------------------------------------------------------------------------------------------- # # ------------------------------------------------------------------------------------------------------------------- # # Load IRAF Packages # ------------------------------------------------------------------------------------------------------------------- # iraf.noao(_doprint=0) iraf.images(_doprint=0) iraf.twodspec(_doprint=0) iraf.onedspec(_doprint=0) # ------------------------------------------------------------------------------------------------------------------- # # ------------------------------------------------------------------------------------------------------------------- # # Functions For File Handling # ------------------------------------------------------------------------------------------------------------------- # def remove_file(file_name): """ Removes the file "file_name" in the constituent directory. Args: file_name : Name of the file to be removed from the current directory Returns:
#! /usr/bin/env python from pyraf import iraf iraf.images() iraf.imstat("dev$pix")
def plant_kbos(filename, psf, kbos, shifts, prefix): """ Add KBOs to an image :param filename: name of the image to add KBOs to :param psf: Point Spread Function in IRAF/DAOPHOT format, used by ADDSTAR :param kbos: list of KBOs to add, has format as returned by KBOGenerator :param shifts: dictionary to transform coordinates to reference frame. :param prefix: an estimate FWHM of the image, used to determine trailing. :return: None """ iraf.set(uparm="./") iraf.digiphot() iraf.apphot() iraf.daophot(_doprint=0) iraf.images() if shifts['nmag'] < 4: logging.warning("Mag shift based on fewer than 4 common stars.") fd = open("plant.WARNING", 'a') fd.write("Mag shift based on fewer than 4 common stars.") fd.close() if shifts['emag'] > 0.05: logging.warning("Mag shift has large uncertainty.") fd = open("plant.WARNING", 'a') fd.write("Mag shift hsa large uncertainty.") fd.close() addstar = tempfile.NamedTemporaryFile(suffix=".add", mode='w') # transform KBO locations to this frame using the shifts provided. w = get_wcs(shifts) header = fits.open(filename)[0].header # set the rate of motion in units of pixels/hour instead of ''/hour scale = header['PIXSCAL1'] rate = kbos['sky_rate'] / scale # compute the location of the KBOs in the current frame. # offset magnitudes from the reference frame to the current one. mag = kbos['mag'] - shifts['dmag'] angle = radians(kbos['angle']) # Move the x/y locations to account for the sky motion of the source. x = kbos['x'] - rate * 24.0 * shifts['dmjd'] * cos(angle) y = kbos['y'] - rate * 24.0 * shifts['dmjd'] * sin(angle) x, y = w.wcs_world2pix(x, y, 1) # Each source will be added as a series of PSFs so that a new PSF is # added for each pixel the source moves. itime = float(header['EXPTIME']) / 3600.0 npsf = fabs(rint(rate * itime)) + 1 mag += 2.5 * log10(npsf) dt_per_psf = itime / npsf # Build an addstar file to be used in the planting of source. idx = 0 for record in transpose([x, y, mag, npsf, dt_per_psf, rate, angle]): x = record[0] y = record[1] mag = record[2] npsf = record[3] dt = record[4] rate = record[5] angle = record[6] for i in range(int(npsf)): idx += 1 x += dt * rate * math.cos(angle) y += dt * rate * math.sin(angle) addstar.write("{} {} {} {}\n".format(x, y, mag, idx)) addstar.flush() fk_image = prefix + filename try: os.unlink(fk_image) except OSError as err: if err.errno == errno.ENOENT: pass else: raise # add the sources to the image. if os.access(f'{fk_image}.art', os.R_OK): os.unlink(f'{fk_image}.art') iraf.daophot.addstar(filename, addstar.name, psf, fk_image, simple=True, verify=False, verbose=False) # convert the image to short integers. iraf.images.chpix(fk_image, fk_image, 'ushort')
def image_combine(targetdir): """ Combine all images. Need to run image_shift before. """ print 'Target directory is ' + targetdir print 'Combining images...' innames = [] for n in os.listdir(targetdir): if (n.endswith('.fit')) & (n.startswith('sdfcr')): innames.append(os.path.join(targetdir,n) ) with open(os.path.join(targetdir,'input.list'), 'w') as f: for name in innames: print name.replace(targetdir + '/','') f.write( name + '\n' ) if os.path.exists( os.path.join(targetdir,'imcomb.fit')): os.remove(os.path.join(targetdir,'imcomb.fit')) print 'Removing file ' + os.path.join(targetdir,'imcomb.fit') iraf.images(_doprint=0) iraf.immatch(_doprint=0) iraf.imcombine.setParam('input','@' + os.path.join(targetdir, 'input.list')) iraf.imcombine.setParam('output',os.path.join(targetdir,'imcomb.fit') ) iraf.imcombine.setParam('combine','average') iraf.imcombine.setParam('reject','sigclip') iraf.imcombine.setParam('lsigma',3.0) iraf.imcombine.setParam('hsigma',3.0) iraf.imcombine() # Or, combine without background subtraction if os.path.exists( os.path.join(targetdir,'imcomb+bkgd.fit')): os.remove( os.path.join(targetdir, 'imcomb+bkgd.fit') ) print 'Removing file ' + os.path.join(targetdir, 'imcomb+bkgd.fit') hdulist = fits.open( os.path.join( targetdir, 'imcomb.fit')) data = hdulist[0].data hdulist.close() # import matplotlib.pyplot as plt # from spectra.range_from_zscale import range_from_zscale # fig, ax = plt.subplots() # z1, z2, iteration = range_from_zscale(data) # ax.imshow(data,cmap='gray',vmin=z1,vmax=z2) # plt.gca().invert_yaxis() data = np.concatenate( ( data[32:345,:], data[520:650,:], data[740:940,:])) sigmaspec = np.std( data, axis = 0) # fig, ax = plt.subplots() # ax.plot( sigmaspec, lw=1 ) # Convert sky from adu to electron skyspec = (4 * sigmaspec)**2 sky_adu = skyspec / 4.0 # add to each pixel hdulist = fits.open( os.path.join( targetdir, 'imcomb.fit')) data = hdulist[0].data hdulist.close() sky_adu = np.repeat(sky_adu, np.shape(data)[0] ).reshape( np.shape(data)[::-1] ).T data = data + sky_adu hdu = fits.PrimaryHDU(data) hdu.writeto( os.path.join( targetdir, 'imcomb+bkgd.fit') ) return None
def execute(): ################NGC1023#################################################### ## use GALFIT to fit and to create a model disk vs bulge## ##galfit.feedme## iraf.images() # path to fits files: total = config.iraf_input_dir+config.totalfits bulge = config.iraf_input_dir+config.bulgefits fraction = config.iraf_tmp_dir+config.fractionfits input_dir = config.iraf_input_dir tmp_dir = config.iraf_tmp_dir if not os.path.exists(tmp_dir): os.mkdir(tmp_dir) if os.path.exists(fraction): iraf.imdelete(fraction) print fraction print bulge # Makes the fraction image by dividing the bulge by the total: iraf.imarith(bulge, "/", total, fraction) """ LLT: I don't think we need this anymore: if os.path.exists(tmp_dir+"snb.fits"): iraf.module.imdelete(tmp_dir+"snb.fits") if os.path.exists(tmp_dir+"tnb.fits"): iraf.module.imdelete(tmp_dir+"tnb.fits") iraf.module.imcopy(bulge, tmp_dir+"snb.fits") iraf.module.imcopy(total, tmp_dir+"tnb.fits") """ ########################################################################## ##Note: the coordinate in pixel as obtained from RA and Dec differ from the ##pixels one because of distorsion corrections as done with astrometry==> ##RA and Dec coordinate in arcsec are the correct one and don't need do be ##transfer in pixels ############################################################################ ##Consistency check #displ(image="../fits_inputs/galaxy.fits",frame=1) #tvmark(frame=1,coords="Kall.dat",color=205,lab-,num+) #displ(image="f.fits",zrange=no,zscale=no,ztrans="linear",z1=0,z2=1,frame=3) #tvmark(frame=3,coords="Kall.dat",color=201,mark="rectangle",lengths="6",lab-,num-) ########################################################################### ##Use program overplot.sm to create both Kall.dat & compagna.dat ##in the RA & Dec of PNS there is a rotation of 90deg respect to the pixel ## x-->-x ##after long reflexion I decide the right PA is 90-47.11=42.89 (just turning the PNe as observed on the R image) ########################################################################## ############################################################################ ## to measure the relative flux use phot (daophot) ##with centerpars=none!!!### ##I used sigma=stdv(skys)=0 ===> no bg ##fwhmpsf=2.27 as in galfit psf2.fits (imexam A enclosed) ##skyvalue=0 ===> no bg ##no centerpars ##apertures=3.0 pixels (=1.8 arcsec) ##zmag=25 ##snb.fits & tnb.fits without bg (when I made the model) # filenames silentremove(config.bulgemag) silentremove(config.bulgetxt) silentremove(config.totalmag) silentremove(config.totaltxt) silentremove(config.fractionmag) silentremove(config.fractiontxt) iraf.noao(_doprint=False) iraf.digiphot(_doprint=False) iraf.daophot(_doprint=False) iraf.phot(bulge,config.position_file, config.bulgemag, skyfile="", datapars="",scale=1.,fwhmpsf=2.27,emissio="yes",sigma=0.0,datamin=-100, datamax=1e5,noise="poisson", centerpars="",calgorithm="none",cbox=0,cthreshold=0.,minsnratio=1., cmaxiter=0,maxshift=0,clean="no",rclean=0.,rclip=0.,kclean=0., mkcenter="no", fitskypars="",salgorithm="constant",annulus=10,dannulus=10,skyvalue=0., smaxiter=10,sloclip=0.,shiclip=0.,snreject=50,sloreject=3., shireject=3.,khist=3.,binsize=0.1,smooth="no",rgrow=0., mksky="no", photpars="",weighting="constant",apertures=3,zmag=0.,mkapert="no", interactive="no",radplots="no",verify="no",update="no",verbose="no", graphics="stdgraph",display="stdimage",icommands="",gcommands="") # LLT: Corrected to pyraf, use stdout = instead of >> iraf.txdump(config.bulgemag, "XCENTER,YCENTER,STDEV,FLUX,SUM,AREA,ID", "yes", headers="no",paramet="no", Stdout=config.bulgetxt) iraf.phot(total, config.position_file, config.totalmag, skyfile="", datapars="",scale=1.,fwhmpsf=2.27,emissio="yes",sigma=0.0,datamin=-100, datamax=1e5,noise="poisson", centerpars="",calgorithm="none",cbox=0,cthreshold=0.,minsnratio=1., cmaxiter=0,maxshift=0.,clean="no",rclean=0.,rclip=0.,kclean=0., mkcenter="no", fitskypars="",salgorithm="constant",annulus=10,dannulus=10,skyvalue=0., smaxiter=10,sloclip=0.,shiclip=0.,snreject=50,sloreject=3., shireject=3.,khist=3.,binsize=0.1,smooth="no",rgrow=0., mksky="no", photpars="",weighting="constant",apertures=3,zmag=25.,mkapert="no", interactive="no",radplots="no",verify="no",update="no",verbose="no", graphics="stdgraph",display="stdimage",icommands="",gcommands="") iraf.txdump(config.totalmag, "XCENTER,YCENTER,STDEV,FLUX,SUM,AREA,ID", "yes", headers="no",paramet="no", Stdout=config.totaltxt) iraf.phot(fraction,config.position_file, config.fractionmag, skyfile="", datapars="", scale=1., fwhmpsf=2.27, emissio="no", sigma=0.0, datamin=-100, datamax=1e5, noise="poisson", centerpars="", calgorithm="none", cbox=0, cthreshold=0., minsnratio=1., cmaxiter=0, maxshift=0., clean="no", rclean=0., rclip=0., kclean=0., mkcenter="no", fitskypars="", salgorithm="constant", annulus=10, dannulus=10, skyvalue=0., smaxiter=10, sloclip=0., shiclip=0., snreject=50, sloreject=3., shireject=3., khist=3., binsize=0.1, smooth="no", rgrow=0., mksky="no", photpars="", weighting="constant", apertures=3, zmag=25., mkapert="no", interactive="no", radplots="no", verify="no", update="no", verbose="no", graphics="stdgraph", display="stdimage", icommands="", gcommands="") iraf.txdump(config.fractionmag, "XCENTER,YCENTER,STDEV,FLUX,SUM,AREA,ID", "yes", headers="no",paramet="no", Stdout=config.fractiontxt)
def telluric_atmo(imgstd): # print "LOGX:: Entering `telluric_atmo` method/function in %(__file__)s" # % globals() import numpy as np import ntt from pyraf import iraf try: import pyfits except: from astropy.io import fits as pyfits iraf.images(_doprint=0) iraf.noao(_doprint=0) iraf.twodspec(_doprint=0) iraf.longslit(_doprint=0) iraf.onedspec(_doprint=0) toforget = ['imfilter.gauss', 'specred.apall', 'longslit.identify', 'longslit.reidentify', 'specred.standard', 'onedspec.wspectext'] for t in toforget: iraf.unlearn(t) _grism = ntt.util.readkey3(ntt.util.readhdr(imgstd), 'grism') imgout = 'invers_atmo_' + imgstd ntt.util.delete(imgout) iraf.set(direc=ntt.__path__[0] + '/') _cursor = 'direc$standard/ident/cursor_sky_0' iraf.noao.onedspec.bplot(imgstd, cursor=_cursor, spec2=imgstd, new_ima=imgout, overwri='yes') xxstd, ffstd = ntt.util.readspectrum(imgout) if _grism in ['Gr13', 'Gr16']: llo2 = np.compress((np.array(xxstd) >= 7550) & ( np.array(xxstd) <= 7750), np.array(xxstd)) llh2o = np.compress((np.array(xxstd) >= 7100) & ( np.array(xxstd) <= 7500), np.array(xxstd)) ffo2 = np.compress((np.array(xxstd) >= 7550) & ( np.array(xxstd) <= 7750), np.array(ffstd)) ffh2o = np.compress((np.array(xxstd) >= 7100) & ( np.array(xxstd) <= 7500), np.array(ffstd)) elif _grism in ['Gr11']: llo2 = np.compress((np.array(xxstd) >= 6830) & ( np.array(xxstd) <= 7100), np.array(xxstd)) llh2o = np.compress((np.array(xxstd) >= 7100) & ( np.array(xxstd) <= 7500), np.array(xxstd)) ffo2 = np.compress((np.array(xxstd) >= 6830) & ( np.array(xxstd) <= 7100), np.array(ffstd)) ffh2o = np.compress((np.array(xxstd) >= 7100) & ( np.array(xxstd) <= 7500), np.array(ffstd)) if _grism in ['Gr13', 'Gr16', 'Gr11']: _skyfileh2o = 'direc$standard/ident/ATLAS_H2O.fits' _skyfileo2 = 'direc$standard/ident/ATLAS_O2.fits' atlas_smooto2 = '_atlas_smoot_o2.fits' atlas_smooth2o = '_atlas_smoot_h2o.fits' _sigma = 200 ntt.util.delete(atlas_smooto2) ntt.util.delete(atlas_smooth2o) iraf.imfilter.gauss(_skyfileh2o, output=atlas_smooth2o, sigma=_sigma) iraf.imfilter.gauss(_skyfileo2, output=atlas_smooto2, sigma=_sigma) llskyh2o, ffskyh2o = ntt.util.readspectrum(atlas_smooth2o) llskyo2, ffskyo2 = ntt.util.readspectrum(atlas_smooto2) ffskyo2cut = np.interp(llo2, llskyo2, ffskyo2) ffskyh2ocut = np.interp(llh2o, llskyh2o, ffskyh2o) _scaleh2o = [] integral_h2o = [] for i in range(1, 21): j = 0.6 + i * 0.04 _ffskyh2ocut = list((np.array(ffskyh2ocut) * j) + 1 - j) diff_h2o = abs(_ffskyh2ocut - ffh2o) integraleh2o = np.trapz(diff_h2o, llh2o) integral_h2o.append(integraleh2o) _scaleh2o.append(j) _scaleo2 = [] integral_o2 = [] for i in range(1, 21): j = 0.6 + i * 0.04 _ffskyo2cut = list((np.array(ffskyo2cut) * j) + 1 - j) diff_o2 = abs(_ffskyo2cut - ffo2) integraleo2 = np.trapz(diff_o2, llo2) integral_o2.append(integraleo2) _scaleo2.append(j) sh2o = _scaleh2o[np.argmin(integral_h2o)] so2 = _scaleo2[np.argmin(integral_o2)] telluric_features = ((np.array(ffskyh2o) * sh2o) + 1 - sh2o) + ((np.array(ffskyo2) * so2) + 1 - so2) - 1 telluric_features = np.array([1] + list(telluric_features) + [1]) llskyo2 = np.array([1000] + list(llskyo2) + [15000]) telluric_features_cut = np.interp(xxstd, llskyo2, telluric_features) _imgout = 'atmo_' + imgstd data1, hdr = pyfits.getdata(imgstd, 0, header=True) data1[0] = np.array(telluric_features_cut) data1[1] = data1[1] / data1[1] data1[2] = data1[2] / data1[2] data1[3] = data1[3] / data1[3] ntt.util.delete(_imgout) pyfits.writeto(_imgout, np.float32(data1), hdr) ntt.util.delete(atlas_smooto2) ntt.util.delete(atlas_smooth2o) ntt.util.delete(imgout) else: _imgout = '' print '### telluric correction with model not possible ' return _imgout
def makeillumination(lista,flatfield):#,outputfile,illum_frame): import os,glob,string,re from astropy.io import fits as pyfits import ntt from ntt.util import readhdr, readkey3, delete, display_image, defsex, name_duplicate, correctcard from numpy import compress, array, argmax, argmin, min, argsort, float32 import datetime MJDtoday = 55927 + (datetime.date.today() - datetime.date(2012, 01, 01)).days _date = readkey3(readhdr(lista[0]), 'date-night') _filter = readkey3(readhdr(lista[0]), 'filter') illum_frame = name_duplicate( lista[0], 'illum_' + _date + '_' + _filter + '_' + str(MJDtoday), '') from pyraf import iraf iraf.images(_doprint=0) iraf.imutil(_doprint=0) iraf.utilities(_doprint=0) iraf.noao(_doprint=0) iraf.imred(_doprint=0) iraf.ccdred(_doprint=0) iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.generic(_doprint=0) toforget = ['digiphot.daophot', 'imutil.imarith', 'image', 'utilities.surfit'] for t in toforget: iraf.unlearn(t) n = len(lista) # start loop to read image names from the input file lista1 = [] iraf.ccdred.verbose = 'no' ff = open('templist.lst', 'w') for i in range(0, len(lista)): ff.write('C' + lista[i] + '\n') delete('C' + lista[i]) delete('C' + re.sub('.fits', '_sub.fits', lista[i])) ntt.sofiphotredudef.crosstalk(lista[i], 'C' + lista[i]) iraf.noao.imred.ccdred.ccdproc('C' + lista[i], output='', overscan="no", trim="yes", ccdtype='', darkcor='no', fixpix='no', zerocor="no", flatcor='yes', illumco='no', trimsec='[1:1024,1:1007]', biassec='', flat=flatfield, illum='') correctcard('C' + lista[i]) lista1.append('C' + lista[i]) ff.close() print '\n### prereducing STD frames to compute illumination correction ........' lista2, skyfile = ntt.sofiphotredudef.skysub(lista1, readkey3( readhdr(lista1[0]), 'ron'), readkey3(readhdr(lista1[0]), 'gain'), True) lista2 = ntt.sofiphotredudef.sortbyJD(lista2) print '\n### use x on the star and q to continue....' display_image(lista2[0], 2, '', '', False) delete('tmpone.coo') iraf.image.tv.imexamine(lista2[0], 2, logfile='tmpone.coo', keeplog='yes', xformat='', yformat='', wcs='logical') iraf.tvmark(2, 'tmpone.coo', mark="circle", number='yes', label='no', radii=8, nxoffse=5, nyoffse=5, color=204, txsize=2) xycoo = iraf.proto.fields('tmpone.coo', '1,2', Stdout=1) x0, y0 = string.split(xycoo[0]) x0 = float(x0) y0 = float(y0) xcum0 = readkey3(readhdr(lista2[0]), 'xcum') ycum0 = readkey3(readhdr(lista2[0]), 'ycum') iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.noao.digiphot.daophot.datapars.datamin = -1000 iraf.noao.digiphot.daophot.datapars.datamax = 60000 iraf.noao.digiphot.daophot.daopars.function = 'gauss' iraf.noao.digiphot.daophot.photpars.zmag = 0 namesex = defsex('default.sex') for i in range(0, len(lista2)): j = i + 1 xcum = readkey3(readhdr(lista2[i]), 'xcum') ycum = readkey3(readhdr(lista2[i]), 'ycum') xx = x0 - xcum0 + xcum yy = y0 - ycum0 + ycum # sex objects os.system('sex ' + lista2[i] + ' -c ' + namesex + '> _logsex') delete('_logsex') xpix = iraf.proto.fields('detections.cat', fields='2', Stdout=1) ypix = iraf.proto.fields('detections.cat', fields='3', Stdout=1) cm = iraf.proto.fields('detections.cat', fields='4', Stdout=1) cm = compress((array(xpix) != ''), array(cm, float)) ypix = compress((array(xpix) != ''), array(ypix, float)) xpix = compress((array(xpix) != ''), array(xpix, float)) if len(xpix) > 300: num = 300 else: num = len(xpix) - 1 xpix = xpix[argsort(cm)][0:num] ypix = ypix[argsort(cm)][0:num] distance = (ypix - yy)**2 + (xpix - xx)**2 xx1, yy1 = xpix[argmin(distance)], ypix[argmin(distance)] f = open('tmpone.coo', 'w') f.write(str(xx1) + ' ' + str(yy1) + '\n') f.close() display_image(lista2[i], 1, '', '', False) iraf.tvmark(1, 'tmpone.coo', mark="circle", number='yes', label='no', radii=8, nxoffse=5, nyoffse=5, color=204, txsize=2) answ = 'n' while answ != 'y': answ = raw_input('selected the right one [[y]/n] ?') if not answ: answ = 'y' if answ in ['y', 'YES', 'yes', 'Y']: print lista2[i] delete('pippo.' + str(j) + '.mag') gggg = iraf.digiphot.daophot.phot( lista2[i], "tmpone.coo", output="pippo." + str(j) + ".mag", verify='no', interac='no', Stdout=1) try: float(string.split(gggg[0])[3]) answ = 'y' except: print '\n### warning' answ = 'n' else: print '\n### select the std star' display_image(lista2[i], 1, '', '', False) iraf.image.tv.imexamine(lista2[ i], 1, logfile='tmpone.coo', keeplog='yes', xformat='', yformat='', wcs='logical') xycoo = iraf.proto.fields('tmpone.coo', '1,2', Stdout=1) x2, y2 = string.split(xycoo[0]) f = open('tmpone.coo', 'w') f.write(str(x2) + ' ' + str(y2) + '\n') f.close() delete('pippo.' + str(j) + '.mag') print '###### new selection ' + str(x2), str(y2) gggg = iraf.digiphot.daophot.phot( lista2[i], "tmpone.coo", output='pippo.' + str(j) + '.mag', verify='no', interac='no', Stdout=1) try: float(string.split(gggg[0])[3]) answ = 'y' except: print '\n### warning' answ = 'n' os.system('ls pippo.*.mag > tempmag.lst') tmptbl0 = iraf.txdump(textfile="@tempmag.lst", fields="XCENTER,YCENTER,FLUX", expr='yes', Stdout=1) ff = open('magnitudini', 'w') for i in tmptbl0: ff.write(i + '\n') ff.close() # delete the temporary images and files delete("temp*.fits") delete('temp*.lst') delete(illum_frame) print '\n### fitting the illumination surface...' aaa = iraf.utilities.surfit('magnitudini', image=illum_frame, function="polynomial", xorder=2, yorder=2, xterms="full", ncols=1024, nlines=1024, Stdout=1) iraf.noao.imred.generic.normalize(illum_frame) correctcard(lista[0]) data, hdr = pyfits.getdata(illum_frame, 0, header=True) data0, hdr0 = pyfits.getdata(lista[0], 0, header=True) delete(illum_frame) pyfits.writeto(illum_frame, float32(data), hdr0) flatfield0 = string.split(flatfield, '/')[-1] ntt.util.updateheader( illum_frame, 0, {'MKILLUM': [flatfield0, 'flat field']}) display_image(illum_frame, 1, '', '', False) for i in range(0, len(lista)): # in lista: img = lista[i] delete('pippo.' + str(i) + '.mag') delete('C' + img) delete('C' + re.sub('.fits', '_sky.fits', img)) # delete('C*.fits.mag.1') # iraf.hedit(illum_frame,'MKILLUM','Illum. corr. created '+flatfield,add='yes',update='yes',verify='no') return illum_frame
def rectify_image(image, reference, output, refout=None, fitgeometry='general', function='polynomial', xxorder=3, yyorder=3, xyorder=3, yxorder=3, clobber=1, objects=None, interactive=1, maxiter=5, reject=3, xrange=None, yrange=None, thresh=2.0, scale=0.125, fwhm=4, image_sigma=None, reference_sigma=None, debug=0, aoffset=0): '''This task will take an image and a reference image and apply geomap/ geotran to aligned them. image is registered and output to output. You can specify the fit geometry (shift, xyscale, rotate, rscale, rxyscale, general). You can also choose the geenral function (legendre, chebyshev, polynomial). You can specfy the order of all the terms (xxorder, yyorder, xyorder, yxorder). If you choose clobber=1 (the default), new images overwrite older ones. Lastly, if you want to use a pre-existing list of objects, specify them as the objects keyword.''' # First, if we already have objects to use, save time by not finding them # again! if objects is None: sex(image, output='image.cat', thresh=thresh, scale=scale, fwhm=fwhm, sigma_map=image_sigma) sex(reference, output='reference.cat', thresh=thresh, scale=scale, fwhm=fwhm, sigma_map=reference_sigma) im_masks1 = get_mask(image) im_masks2 = get_mask(reference) if aoffset == 0: # Check the ROTANG header field to see if we need to give a rotation # offset to the object matcher. rotang1 = get_header(reference, "ROTANG", float) rotang2 = get_header(image, "ROTANG", float) if rotang1 is not None and rotang2 is not None: if abs(rotang1 - rotang2) > 0.1: aoffset = rotang1 - rotang2 # Run the matchum routine to get corresponding objects: (x2, y2, x1, y1, o1, o2) = matchum('image.cat', 'reference.cat', im_masks1=im_masks1, im_masks2=im_masks2, xrange=xrange, yrange=yrange, debug=debug, aoffset=aoffset) f = open('geomap.objects', 'w') for i in range(len(x1)): f.write('%.2f %.2f %.2f %.2f\n' % (x1[i], y1[i], x2[i], y2[i])) f.close() objects = "geomap.objects" else: data = pygplot.columns(objects) x1 = data[0] y1 = data[1] x2 = data[2] y2 = data[3] xshift = int(stats.bwt(x2 - x1)[0]) yshift = int(stats.bwt(y2 - y1)[0]) # We now have some selected objects to use with geomap. Let's figure out # the area of validity for the data using ths shifts computed above. (r_xmax, r_ymax) = get_image_size(reference) if xshift < 0: xmin = -xshift xmax = r_xmax else: xmin = 1 xmax = r_xmax - xshift if yshift < 0: ymin = -yshift ymax = r_ymax else: ymin = 1 ymax = r_ymax - yshift #xmin = min(x1); xmax = max(x1) #ymin = min(y1); ymax = max(y1) print xshift, yshift, xmin, xmax, ymin, ymax iraf.images() if os.path.isfile('geomap.db'): os.unlink('geomap.db') # We may not have enough points to do a general fit, so we do them in order of # complexity. Hopefully, we have at least ONE point (for the shift) fitgeometries = [fitgeometry, 'rxyscale', 'rotate', 'shift'] success = 0 for fitgeo in fitgeometries: print "using fitgeometry = ", fitgeo res = iraf.geomap(input=objects, database='geomap.db', xmin=1, ymin=1, xmax=r_xmax, ymax=r_ymax, fitgeometry=fitgeo, function=function, xxorder=xxorder, yyorder=yyorder, xyorder=xyorder, yxorder=yxorder, results='', transforms=objects, interactive=interactive, maxiter=maxiter, reject=reject, Stdout=1) # assume all went well success = 1 for line in res: print line if re.search('Too few points', line): success = 0 if success: break if clobber and os.path.isfile(output): iraf.imdel(output) iraf.geotran(input=image, output='temp.rectify', database='geomap.db', transforms=objects, geometry='geometric', interpolant="linear") section = "[%d:%d,%d:%d]" % (xmin, xmax, ymin, ymax) print 'Valid section of data:', section iraf.imcopy(input='temp.rectify', output=output) iraf.imdel('temp.rectify') return (xmin, xmax, ymin, ymax)
def display_image(img, frame, _z1, _z2, scale, _xcen=0.5, _ycen=0.5, _xsize=1, _ysize=1, _erase='yes'): # print "LOGX:: Entering `display_image` method/function in %(__file__)s" # % globals() goon = 'True' import glob import subprocess import time import os ds9 = subprocess.Popen("ps -U" + str(os.getuid()) + "|grep -v grep | grep ds9", shell=True, stdout=subprocess.PIPE).stdout.readlines() if len(ds9) == 0: subproc = subprocess.Popen('ds9', shell=True) time.sleep(3) if glob.glob(img): from pyraf import iraf iraf.images(_doprint=0) iraf.tv(_doprint=0) import string import os if _z2: try: sss = iraf.display(img, frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase, fill='yes', zscale='no', zrange='no', z1=_z1, z2=_z2, Stdout=1) except: print '' print '### ERROR: PROBLEM OPENING DS9' print '' goon = 'False' else: try: sss = iraf.display(img, frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase, fill='yes', Stdout=1) except: print '' print '### ERROR: PROBLEM OPENING DS9' print '' goon = False if scale and goon: answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ') if not answ0: answ0 = 'y' elif answ0 == 'no' or answ0 == 'NO': answ0 = 'n' while answ0 == 'n': _z11 = float(string.split(string.split(sss[0])[0], '=')[1]) _z22 = float(string.split(string.split(sss[0])[1], '=')[1]) z11 = raw_input('>>> z1 = ? [' + str(_z11) + '] ? ') z22 = raw_input('>>> z2 = ? [' + str(_z22) + '] ? ') if not z11: z11 = _z11 else: z11 = float(z11) if not z22: z22 = _z22 else: z22 = float(z22) print z11, z22 sss = iraf.display(img, frame, fill='yes', xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase, zrange='no', zscale='no', z1=z11, z2=z22, Stdout=1) answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ') if not answ0: answ0 = 'y' elif answ0 == 'no' or answ0 == 'NO': answ0 = 'n' if goon: _z1, _z2 = string.split(string.split(sss[0])[0], '=')[ 1], string.split(string.split(sss[0])[1], '=')[1] else: print 'Warning: image ' + str(img) + ' not found in the directory ' return _z1, _z2, goon
def marksn2(img, fitstab, frame=1, fitstab2='', verbose=False): from pyraf import iraf from numpy import array #,log10 import lsc iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.images(_doprint=0) iraf.imcoords(_doprint=0) iraf.proto(_doprint=0) iraf.set(stdimage='imt1024') hdr = lsc.util.readhdr(fitstab) _filter = lsc.util.readkey3(hdr, 'filter') column = lsc.lscabsphotdef.makecatalogue([fitstab])[_filter][fitstab] rasex = array(column['ra0'], float) decsex = array(column['dec0'], float) if fitstab2: hdr = lsc.util.readhdr(fitstab2) _filter = lsc.util.readkey3(hdr, 'filter') _exptime = lsc.util.readkey3(hdr, 'exptime') column = lsc.lscabsphotdef.makecatalogue([fitstab2])[_filter][fitstab2] rasex2 = array(column['ra0'], float) decsex2 = array(column['dec0'], float) iraf.set(stdimage='imt1024') iraf.display(img + '[0]', frame, fill=True, Stdout=1) vector = [] for i in range(0, len(rasex)): vector.append(str(rasex[i]) + ' ' + str(decsex[i])) xy = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\ formats='%10.1f %10.1f',verbose='yes')[3:] iraf.tvmark(frame, 'STDIN', Stdin=list(xy), mark="circle", number='yes', label='no', radii=10, nxoffse=5, nyoffse=5, color=207, txsize=2) if verbose: # print 2.5*log10(_exptime) for i in range(0, len(column['ra0'])): print xy[i], column['ra0'][i], column['dec0'][i], column['magp3'][ i], column['magp4'][i], column['smagf'][i], column['magp2'][i] if fitstab2: vector2 = [] for i in range(0, len(rasex2)): vector2.append(str(rasex2[i]) + ' ' + str(decsex2[i])) xy1 = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector2,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\ formats='%10.1f %10.1f',verbose='yes')[3:] iraf.tvmark(frame, 'STDIN', Stdin=list(xy1), mark="cross", number='yes', label='no', radii=10, nxoffse=5, nyoffse=5, color=205, txsize=2)
def escut(image, pos_file, fwhm, peak): # input image file name, file name with matched source positions, **np.array of fwhm measurements for each source import numpy as np import matplotlib.pyplot as plt from scipy import stats from pyraf import iraf # import sewpy import os from matplotlib.path import Path iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) iraf.unlearn(iraf.phot, iraf.datapars, iraf.photpars, iraf.centerpars, iraf.fitskypars) iraf.apphot.phot.setParam('interactive', "no") iraf.apphot.phot.setParam('verify', "no") iraf.datapars.setParam('datamax', 50000.) iraf.datapars.setParam('gain', "gain") iraf.datapars.setParam('ccdread', "rdnoise") iraf.datapars.setParam('exposure', "exptime") iraf.datapars.setParam('airmass', "airmass") iraf.datapars.setParam('filter', "filter") iraf.datapars.setParam('obstime', "time-obs") # iraf.datapars.setParam('obstime',"date-obs") iraf.datapars.setParam('sigma', "INDEF") iraf.photpars.setParam('zmag', 0.) iraf.centerpars.setParam('cbox', 9.) iraf.centerpars.setParam('maxshift', 3.) iraf.fitskypars.setParam('salgorithm', "median") iraf.fitskypars.setParam('dannulus', 10.) # clean up the indefs so we can actually do stats, but reassign them to 99999 so we don't lose track of things # keep a separate list without them to do the median (we need floats) indefs = np.where(fwhm == 'INDEF') good = np.where(fwhm != 'INDEF') fwhm[indefs] = 99.999 fwhm = fwhm.astype(float) fwhm_good = fwhm[good].astype(float) indefs = np.where(peak == 'INDEF') peak[indefs] = -999.999 peak = peak.astype(float) peak_good = peak[good].astype(float) if not os.path.isfile(image[0:-5] + '.txdump'): # findavgfwhm = sewpy.SEW( # params = ["X_IMAGE", "Y_IMAGE", "FWHM_IMAGE", "FLAGS"], # config = {"DETECT_THRESH":200.0}, # sexpath = "sex" # ) # # out = findavgfwhm(image)["table"] # # fwhms = out['FWHM_IMAGE'] # This is an astropy table. # flags = out['FLAGS'] # get a really rough estimate of the stellar FWHM in the image to set apertures # use the input fwhm measurement # ap1x = fwhm_est # xpos = datatable['X_IMAGE'] # ypos = datatable['Y_IMAGE'] # fwhm = datatable['FWHM_IMAGE'] # flags = datatable['FLAGS'] # idno = datatable['NUMBER'] ap1x = np.median( fwhm_good ) # only use isolated detections of stars, this is the 1x aperture # print ap1x ap2x = 2.0 * ap1x # these = [ i for i,id in enumerate(idno) if (flags[i] == 0)] # with open(image[0:-5]+'.escut.pos','w+') as f: # for j in range(len(xpos)): # print >> f, xpos[j], ypos[j], fwhm[j], idno[j] iraf.datapars.setParam('fwhmpsf', ap1x) iraf.photpars.setParam('apertures', repr(ap1x) + ', ' + repr(ap2x)) iraf.fitskypars.setParam('annulus', 4. * ap1x) iraf.apphot.phot(image=image, coords=pos_file, output=image[0:-5] + '.phot') with open(image[0:-5] + '.txdump', 'w+') as txdump_out: iraf.ptools.txdump( textfiles=image[0:-5] + '.phot', fields= "id,mag,merr,msky,stdev,rapert,xcen,ycen,ifilter,xairmass,image", expr= 'MAG[1] != INDEF && MERR[1] != INDEF && MAG[2] != INDEF && MERR[2] != INDEF', headers='no', Stdout=txdump_out) mag1x, mag2x = np.loadtxt(image[0:-5] + '.txdump', usecols=(1, 2), unpack=True) iraf_id = np.loadtxt(image[0:-5] + '.txdump', usecols=(0, ), dtype=int, unpack=True) # idno = np.loadtxt(image[0:-5]+'.escut.pos', usecols=(3,), dtype=int, unpack=True) xpos, ypos = np.loadtxt(pos_file, usecols=(0, 1), unpack=True) keepIndex = iraf_id - 1 xpos, ypos, fwhm, peak = xpos[keepIndex], ypos[keepIndex], fwhm[ keepIndex], peak[keepIndex] # print idno.size, iraf_id.size, xpos.size diff = mag2x - mag1x diffCut = diff magCut = mag2x xCut = xpos #[good] yCut = ypos #[good] idCut = iraf_id fwhmCut = fwhm #_good peakCut = peak print(peakCut.size, magCut.size, diffCut.size) print(diffCut.size, 0, np.median(diffCut), diffCut.std()) nRemoved = 1 # plt.clf() # plt.scatter(peakCut, magCut, edgecolor='none') # plt.savefig('peaktest.pdf') plt.clf() # plt.hlines(bin_edges, -2, 1, colors='red', linestyle='dashed') plt.scatter(diff, mag2x, edgecolor='none', facecolor='black', s=4) # plt.scatter(diffCut, magCut, edgecolor='none', facecolor='blue', s=4) magdiff = list( zip(magCut.tolist(), diffCut.tolist(), peakCut.tolist(), idCut.tolist())) dtype = [('mag', float), ('diff', float), ('peak', float), ('id', int)] magdiff = np.array(magdiff, dtype=dtype) magSort = np.sort(magdiff, order='peak') peakRange = (magSort['peak'] > 20000.0) & (magSort['peak'] < 40000.0) peakVal = np.median((magSort['diff'])[np.where(peakRange)]) # peakVal = np.median(diffCut) print(peakVal) plt.scatter((magSort['diff'])[np.where(peakRange)], (magSort['mag'])[np.where(peakRange)], edgecolor='none', facecolor='blue', s=4) while nRemoved != 0: nBefore = diffCut.size diffCheck = np.where( abs(peakVal - diffCut) < 2.0 * diffCut.std()) #[i for i,d in enumerate(diff) if (-0.5 < d < 0.0)] # diffCut = diffCut[diffCheck] nRemoved = nBefore - diffCut.size magCut = magCut[diffCheck] xCut = xCut[diffCheck] yCut = yCut[diffCheck] idCut = idCut[diffCheck] fwhmCut = fwhmCut[diffCheck] print(diffCut.size, nRemoved, np.median(diffCut), diffCut.std()) if 0.05 < diffCut.std() < 0.06: nRemoved = 0 # plt.fill_betweenx(bin_centers, bin_meds+3.0*bin_stds, bin_meds-3.0*bin_stds, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region') # with open('escutSTD_i.pos','w+') as f: # for i,blah in enumerate(xCut): # print >> f, xCut[i], yCut[i], diffCut[i] bin_meds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic='median', bins=24, range=(-12, 0)) bin_stds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic=np.std, bins=24, range=(-12, 0)) bin_width = (bin_edges[1] - bin_edges[0]) bin_centers = bin_edges[1:] - bin_width / 2 # print bin_meds, bin_stds bin_hw = np.zeros_like(bin_stds) for i, bin_std in enumerate(bin_stds): if bin_std > 0.025: bin_hw[i] = 3.0 * bin_std else: bin_hw[i] = 0.075 # print len(binnumber) # for i,bin_hwi in enumerate(bin_hw): left_edge = np.array(list(zip(peakVal - bin_hw, bin_centers))) right_edge = np.flipud(np.array(list(zip(peakVal + bin_hw, bin_centers)))) # print left_edge, right_edge verts = np.vstack((left_edge, right_edge)) # print verts # verts = np.delete(verts, np.array([0,1,2,22,23,24,25,45,46,47]), axis=0) # DON'T USE A PATH BECAUSE APPARENTLY IT CAN SELECT THE INVERSE SET!! WTF # print verts esRegion = Path(verts) sources = esRegion.contains_points(list(zip(diff, mag2x))) # print sources with open('escutREG_i.pos', 'w+') as f: for i, blah in enumerate(xpos[sources]): print((xpos[sources])[i], (ypos[sources])[i], (diff[sources])[i], file=f) magCut2 = mag2x[sources] magCut1 = mag1x[sources] fwhmCut = fwhm[sources] xCut = xpos[sources] yCut = ypos[sources] diffCut = diff[sources] # find the sources that are in the std method but not the region method # print idCut, idno[sources] # extrasSTD = np.setdiff1d(idno[sources], idCut) # print extrasSTD.size # print extrasSTD # with open('escutUNIQUE.pos','w+') as f: # for i,blah in enumerate(extrasSTD): # print >> f, xpos[blah-1], ypos[blah-1] # fwhmcheck = np.loadtxt('testfwhmREG.log', usecols=(10,), unpack=True) fwhmchk2 = np.where((magCut2 < -4) & (fwhmCut < 90.0)) print(np.median(fwhmCut[fwhmchk2]), np.std(fwhmCut[fwhmchk2])) fwchk = np.where( np.abs(fwhmCut - np.median(fwhmCut[fwhmchk2])) > 10.0 * np.std(fwhmCut[fwhmchk2])) drop = np.abs(fwhmCut - np.median(fwhmCut[fwhmchk2])) > 10.0 * np.std( fwhmCut[fwhmchk2]) keep = np.abs(fwhmCut - np.median(fwhmCut[fwhmchk2])) <= 10.0 * np.std( fwhmCut[fwhmchk2]) with open('escutVBAD_i.pos', 'w+') as f: for i, blah in enumerate(xCut[fwchk]): print((xCut[fwchk])[i], (yCut[fwchk])[i], file=f) with open('escut_i.pos', 'w+') as f: for i, blah in enumerate(xCut): if not drop[i]: print(xCut[i], yCut[i], magCut2[i], fwhmCut[i], magCut1[i], file=f) with open('escut_g.pos', 'w+') as f: for i, blah in enumerate(xCut): if not drop[i]: print(xCut[i], yCut[i], magCut2[i], fwhmCut[i], magCut1[i], file=f) plt.fill_betweenx(bin_centers, peakVal + bin_hw, peakVal - bin_hw, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region') plt.scatter(diffCut[fwchk], magCut2[fwchk], edgecolor='none', facecolor='red', s=4) plt.ylim(0, -12) plt.xlabel('$m_{2x} - m_{1x}$') plt.ylabel('$m_{2x}$') plt.xlim(-2, 1) plt.savefig('testmagiraf.pdf') plt.clf() plt.scatter(magCut2, fwhmCut, edgecolor='none', facecolor='black') plt.scatter(magCut2[fwchk], fwhmCut[fwchk], edgecolor='none', facecolor='red') plt.hlines([np.median(fwhmCut)], -12, 0, colors='red', linestyle='dashed') plt.hlines([ np.median(fwhmCut) + fwhmCut.std(), np.median(fwhmCut) - fwhmCut.std() ], -12, 0, colors='red', linestyle='dotted') plt.ylim(0, 20) plt.xlim(-12, 0) plt.ylabel('fwhm') plt.xlabel('$m_{2x}$') plt.savefig('fwhmcheck.pdf') return fwhmCut[keep]
def ASYM(cutimage, maskimage, ini_xcntr, ini_ycntr, pa, one_minus_eg_sq, r50, background, extraction_radius, angle, flag_image, ABS_ZSUM): iraf.images(_doprint=0) iraf.images.imgeom(_doprint=0) iraf.images.imutil(_doprint=0) co = n.cos(pa * n.pi / 180.0) si = n.sin(pa * n.pi / 180.0) Aabs = n.zeros([9]) Aabs = Aabs.astype(n.float32) rot_sum = n.zeros([9]) rot_sum = rot_sum.astype(n.float32) abs_zsum = n.zeros([9]) abs_zsum = abs_zsum.astype(n.float32) sh_sum = n.zeros([9]) sh_sum = sh_sum.astype(n.float32) absres_sum = n.zeros([9]) absres_sum = absres_sum.astype(n.float32) f=pyfits.open(cutimage) z = f[0].data z = z - background f.close() z = n.swapaxes(z, 0, 1) iraf.rotate("".join(maskimage), output="MRotated.fits",\ rotation=180, xin = ini_xcntr + 1.0,\ yin = ini_ycntr + 1.0, \ xout = ini_xcntr + 1.0, yout = ini_ycntr + 1.0, \ ncols= "INDEF",\ nlines = "INDEF", interpo= "linear", \ boundar="nearest", verbose="no") # iraf.imarith("".join(maskimage), op="*", operand2="MRotated.fits", \ # result="AMask.fits") # f=pyfits.open("AMask.fits") # mask = f[0].data # f.close() f=pyfits.open("MRotated.fits") mask1 = f[0].data f.close() mask1 = n.swapaxes(mask1, 0, 1) f=pyfits.open(maskimage) mask2 = f[0].data f.close() mask2 = n.swapaxes(mask2, 0, 1) mask = mask1 + mask2 maskedgalaxy = ma.masked_array(z, mask) z = ma.filled(maskedgalaxy, value = background) hdu = pyfits.PrimaryHDU(n.swapaxes(z, 0, 1).astype(n.float32)) hdu.writeto('MaskedGalaxy.fits') NXPTS = z.shape[0] NYPTS = z.shape[1] #r50=c.r50 center_rad = 0.01 * r50 # print 'center_rad', center_rad # center_rad = 0.5#0.01*r50 The centering correction has to be done by calculating asymmetric parameters at different points 0.1% of r50 away around the center flag_center = 0 #flag_center=1 if the program finds the exact center else center=0 #extraction_radius=total_rad ie. 1.5 times the radius at eta=0.2 x = n.reshape(n.arange(NXPTS * NYPTS), (NXPTS, NYPTS)) / NYPTS x = x.astype(n.float32) y = n.reshape(n.arange(NXPTS * NYPTS), (NXPTS, NYPTS)) % NYPTS y = y.astype(n.float32) nn = 0 #This nn is given here and the following loop run either it finds the minimum asymmetry or n=20 while flag_center == 0: flag_out = 0 #x and y coordinates of different center points in 1-d array xcntr = n.reshape(n.array([[ini_xcntr] * 3, [ini_xcntr - center_rad] *\ 3, [ini_xcntr + center_rad] * 3]), (9, )) xcntr = xcntr.astype(n.float32) ycntr = n.array([ini_ycntr, ini_ycntr - center_rad, ini_ycntr + \ center_rad]*3) ycntr = ycntr.astype(n.float32) for iter in range(9):#The below is done in order to keep the extraction radius inside the image. If the extraction radius goes outside the image then flag_out=1 if(flag_image):#flag_image will tell whether the input is image (flag_image=1) or background (flag_image=0) if(xcntr[iter] - 2 - extraction_radius < 0): extraction_radius = xcntr[iter] - 2 flag_out = 1 if(ycntr[iter] - 2 - extraction_radius < 0): extraction_radius = ycntr[iter] - 2 flag_out = 1 if(xcntr[iter] + 2 + extraction_radius > NXPTS): extraction_radius = NXPTS - xcntr[iter] - 2 flag_out = 1 if(ycntr[iter] + 2 + extraction_radius > NYPTS): extraction_radius = NYPTS - ycntr[iter] - 2 flag_out = 1 tx = (x - xcntr[iter]) * co + (y - ycntr[iter]) * si ty = (xcntr[iter] - x) * si + (y - ycntr[iter]) * co R = n.sqrt(tx**2.0 + ty**2.0 / one_minus_eg_sq) iraf.rotate("".join("MaskedGalaxy.fits"), output="Rotated.fits",\ rotation = 180, xin = xcntr[iter] + 1.0, \ yin = ycntr[iter] + 1.0, \ xout = xcntr[iter] + 1.0, yout = ycntr[iter] + 1.0, \ ncols = "INDEF",\ nlines = "INDEF", interpo = "linear", \ boundar = "nearest", verbose = "no") f = pyfits.open("Rotated.fits") rz = f[0].data f.close() rz = n.swapaxes(rz, 0, 1) res = z - rz for myfile in ['Rotated.fits', 'Residual.fits', 'AResidual.fits']: if os.access(myfile, os.F_OK): os.remove(myfile) sh_sum[iter] = z[n.where(R <= extraction_radius)].sum() rot_sum[iter] = rz[n.where(R <= extraction_radius)].sum() abs_zsum[iter] = abs(z[n.where(R <= extraction_radius)]).sum() absres_sum[iter] = abs(res[n.where(R <= extraction_radius)]).sum() if(flag_image): Aabs[iter] = absres_sum[iter] / (abs_zsum[iter]) else: Aabs[iter] = absres_sum[iter] / (ABS_ZSUM) Aabso = Aabs.min() index = Aabs.argmin() if(nn > 20): xcntr[index] = ini_xcntr ycntr[index] = ini_ycntr if(xcntr[index] == ini_xcntr and ycntr[index] == ini_ycntr): iraf.rotate("".join("MaskedGalaxy.fits"), output = "Rotated.fits",\ rotation = 180, xin = xcntr[iter] + 1.0, \ yin = ycntr[iter] + 1.0, \ xout = xcntr[iter] + 1.0, yout = ycntr[iter] + 1.0, \ ncols = "INDEF",\ nlines = "INDEF", interpo = "linear", \ boundar = "nearest", verbose = "no") f = pyfits.open("Rotated.fits") rz = f[0].data f.close() rz = n.swapaxes(rz, 0, 1) res = z - rz hdu = pyfits.PrimaryHDU(n.swapaxes(res, 0, 1).astype(n.float32)) hdu.writeto('AResidual.fits') flag_center = 1 rot_sum = rot_sum[index] abs_zsum = abs_zsum[index] if(flag_image == 0): abs_zsum = ABS_ZSUM sh_sum = sh_sum[index] absres_sum = absres_sum[index] if(flag_image): error_asym = n.sqrt( Aabso**2*( ((sh_sum + rot_sum + 4 * \ background * \ R[n.where(R <= n.floor(extraction_radius))].size) \ / absres_sum**2.0) + ((sh_sum + 2.0 * background *\ R[n.where(R <= n.floor(extraction_radius))].size) \ / abs_zsum**2) ) ) else: error_asym = n.sqrt( Aabso**2 * (((sh_sum+rot_sum + 2 * \ background * \ R[n.where(R <= n.floor(extraction_radius))].size) \ / absres_sum**2.0) + ((sh_sum + 2.0 * background \ * R[n.where(R <= n.floor(extraction_radius))].size) \ / abs_zsum**2) ) ) else: ini_xcntr = xcntr[index] ini_ycntr = ycntr[index] nn += 1 for myfile in ['MaskedGalaxy.fits', 'MRotated.fits']: if os.access(myfile, os.F_OK): os.remove(myfile) return Aabso, error_asym, ini_xcntr, ini_ycntr, nn, flag_out,\ abs_zsum, sh_sum, extraction_radius
def perform_subtraction(input, template_dict, path, bump): log = open(path + "log.txt", "a+") iraf.images() # load iraf images package for imstat for inim in input: if os.path.exists("%sdiff_%s" % (path, inim.split("/")[-1])): print "[*] %sdiff_%s already exists." % (path, inim.split("/")[-1]) continue print inim hdulist = fits.open(inim) filter = hdulist[0].header["HIERARCH FPA.FILTER"][0] tmplim = template_dict[filter] try: dao_params = extract_dao_params(inim.strip(".fits") + "_dao_params.txt") except IOError: print "[-] params file does not exist!" continue sky = dao_params[0] print get_imstats(inim), get_imstats(tmplim) iu, il = get_imstats(inim) offset = round_up(np.abs(il), 500) if bump: try: iraf.imarith( operand1=inim, op="+", operand2=offset, result=inim.strip(".fits") + "_p%s.fits" % (str(offset)) ) except: pass inim = inim.strip(".fits") + "_p%s.fits" % (str(offset)) hdulist = fits.open(tmplim) tu, tl = get_imstats(tmplim) offset = round_up(np.abs(tl), 2000) if bump: try: iraf.imarith( operand1=tmplim, op="+", operand2=offset, result=tmplim.strip(".fits") + "_p%s.fits" % (str(offset)) ) except: pass tmplim = tmplim.strip(".fits") + "_p%s.fits" % (str(offset)) print get_imstats(inim), get_imstats(tmplim) tu, tl = get_imstats(tmplim) iu, il = get_imstats(inim) # tl = round_down(tl,100) # il = round_down(tl,10) # tu = round_up(tu,1000) # iu = round_up(iu,100) # cmd = "hotpants -n i -c t -inim %s -tmplim %s -outim %sdiff_%s -tu %d -iu %d" % (inim, tmplim, path, inim.split("/")[-1], tu, iu) # cmd = "hotpants -n i -c t -inim %s -tmplim %s -outim %sdiff_%s -tu %f -tl %f -iu %f" % (inim, tmplim, path, inim.split("/")[-1], tu, tl, iu) # cmd = "hotpants -n i -c t -inim %s -tmplim %s -outim %sdiff_%s -tu %d -tl %d -iu %d -il %.3f" % (inim, tmplim, path, inim.split("/")[-1], tu, tl, iu, il) cmd = "hotpants -n i -c t -inim %s -tmplim %s -outim %sdiff_%s -tl %d -il %.3f" % ( inim, tmplim, path, inim.split("/")[-1], tl, il, ) log.write(cmd + "\n\n") print cmd subprocess.call(cmd, shell=True) print log.close()
def main(argv): home_root = os.environ['HOME'] iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) for file_ in os.listdir("./"): if file_.endswith("_i.fits"): fits_file_i = file_ title_string = file_[0:9] if file_.endswith("_g.fits"): fits_file_g = file_ fits_h_i = fits.open(fits_file_i) fits_h_g = fits.open(fits_file_g) fwhm_i = fits_h_i[0].header['FWHMPSF'] fwhm_g = fits_h_g[0].header['FWHMPSF'] print 'Image FWHM :: g = {0:5.3f} : i = {1:5.3f}'.format(fwhm_g,fwhm_i) # if not os.path.isdir('psf'): # make a psf subfolder if it doesn't exist # os.mkdir('psf') # # and then copy in the data files if they don't already exist as files # if not os.path.isfile('psf/'+fits_file_i): # iraf.images.imcopy(fits_file_i,'psf/'+fits_file_i,verbose="yes") # # if not os.path.isfile('psf/'+fits_file_g) : # iraf.images.imcopy(fits_file_g,'psf/'+fits_file_g,verbose="yes") # # # also copy in the apcor star lists # if not (os.path.isfile('psf/apcor_stars_i.txt') or os.path.islink('psf/apcor_stars_i.txt')) : # shutil.copyfile('apcor_stars_i.txt','psf/apcor_stars_i.txt') # # if not (os.path.isfile('psf/apcor_stars_g.txt') or os.path.islink('psf/apcor_stars_g.txt')) : # shutil.copyfile('apcor_stars_g.txt','psf/apcor_stars_g.txt') # # # and change to the psf folder # os.chdir('psf') iraf.unlearn(iraf.phot,iraf.datapars,iraf.photpars,iraf.centerpars,iraf.fitskypars) iraf.apphot.phot.setParam('interactive',"no") iraf.apphot.phot.setParam('verify',"no") iraf.datapars.setParam('datamax',"INDEF") iraf.datapars.setParam('gain',"gain") iraf.datapars.setParam('ccdread',"rdnoise") iraf.datapars.setParam('exposure',"exptime") iraf.datapars.setParam('airmass',"airmass") iraf.datapars.setParam('filter',"filter") iraf.datapars.setParam('obstime',"time-obs") iraf.datapars.setParam('sigma',"INDEF") iraf.photpars.setParam('zmag',0.) iraf.centerpars.setParam('cbox',9.) iraf.centerpars.setParam('maxshift',3.) iraf.fitskypars.setParam('salgorithm',"median") iraf.fitskypars.setParam('dannulus',10.) iraf.datapars.setParam('fwhmpsf',fwhm_g) iraf.photpars.setParam('apertures',iraf.nint(4.*fwhm_g)) iraf.fitskypars.setParam('annulus',(6.*fwhm_g)) iraf.apphot.phot(image=fits_file_g, coords='apcor_stars_g.txt', output="g_psfstars.mag.1") iraf.datapars.setParam('fwhmpsf',fwhm_i) iraf.photpars.setParam('apertures',iraf.nint(4.*fwhm_i)) iraf.fitskypars.setParam('annulus',(6.*fwhm_i)) iraf.apphot.phot(image=fits_file_i, coords='apcor_stars_i.txt', output="i_psfstars.mag.1") with open("apcor_stars_i.txt") as foo: lines = len(foo.readlines()) iraf.daophot(_doprint=0) iraf.pstselect.setParam('image',fits_file_i) iraf.pstselect.setParam('photfile',"i_psfstars.mag.1") iraf.pstselect.setParam('pstfile',"default") iraf.pstselect.setParam('maxnpsf',lines) iraf.pstselect.setParam('plottype',"mesh") iraf.daophot.pstselect(verify='no', mode='h') with open("apcor_stars_g.txt") as foo: lines = len(foo.readlines()) iraf.pstselect.setParam('image',fits_file_g) iraf.pstselect.setParam('photfile',"g_psfstars.mag.1") iraf.pstselect.setParam('pstfile',"default") iraf.pstselect.setParam('maxnpsf',lines) iraf.pstselect.setParam('plottype',"mesh") iraf.daophot.pstselect() iraf.datapars.setParam('datamax',"INDEF") iraf.datapars.setParam('gain',"gain") iraf.datapars.setParam('ccdread',"rdnoise") iraf.datapars.setParam('exposure',"exptime") iraf.datapars.setParam('airmass',"airmass") iraf.datapars.setParam('filter',"filter") iraf.datapars.setParam('obstime',"time-obs") iraf.datapars.setParam('sigma',"INDEF") iraf.daopars.setParam('psfrad',iraf.nint(3.*fwhm_i)) iraf.daopars.setParam('fitrad',fwhm_i) iraf.psf.setParam('image',fits_file_i) iraf.psf.setParam('photfile',"i_psfstars.mag.1") iraf.psf.setParam('pstfile',fits_file_i+".pst.1") iraf.psf.setParam('interactive', 'no') iraf.daophot.psf() iraf.psf.setParam('image',fits_file_g) iraf.psf.setParam('photfile',"g_psfstars.mag.1") iraf.psf.setParam('pstfile',fits_file_g+".pst.1") iraf.psf.setParam('interactive', 'no') iraf.daophot.psf()
def absphot(img,_field,_catalogue,_fix,_color,rejection,_interactive,_type='fit',redo=False,show=False,cutmag=-1,database='dataredulco',_calib='sloan'): from astropy.io import fits import lsc import math import sys,re,string,os from lsc.util import readkey3, readhdr from numpy import array, compress, zeros, median, std, asarray, isfinite,mean from pyraf import iraf if show: from pylab import ion,plot,draw,clf import time iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.images(_doprint=0) iraf.imcoords(_doprint=0) iraf.proto(_doprint=0) t = fits.open(img) tbdata = t[1].data hdr2=t[1].header hdr=lsc.util.readhdr(img) _cat=readkey3(hdr,'catalog') _telescope=lsc.util.readkey3(hdr,'telescop') _instrume=lsc.util.readkey3(hdr,'instrume') _filter=lsc.util.readkey3(hdr,'filter') _airmass=lsc.util.readkey3(hdr,'airmass') _exptime=lsc.util.readkey3(hdr,'exptime') _date=lsc.util.readkey3(hdr,'date-obs') _object=lsc.util.readkey3(hdr,'object') _ra=lsc.util.readkey3(hdr,'RA') _dec=lsc.util.readkey3(hdr,'DEC') print _filter if _telescope in ['lsc','1m0-04','1m0-05','1m0-09']: kk=lsc.sites.extintion('ctio') elif _telescope in ['elp','1m0-08']: kk=lsc.sites.extintion('mcdonald') elif _telescope in ['cpt','1m0-12','1m0-10','1m0-13']: kk=lsc.sites.extintion('southafrica') elif _telescope in ['ftn','Faulkes Telescope North']: kk=lsc.sites.extintion('mauna') elif _telescope in ['1m0-03','1m0-11','coj','fts','Faulkes Telescope South']: kk=lsc.sites.extintion('siding') if _calib not in ['sloan','sloanprime','natural','apass','']: colorefisso=lsc.sites.colfix(_instrume) else: colorefisso=lsc.sites.colfix(_instrume,_calib) print redo print _cat if _cat and not redo: print 'already calibrated' else: try: lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]): lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) except: print 'module mysqldef not found' column=makecatalogue([img])[_filter][img] rasex=array(column['ra0'],float) decsex=array(column['dec0'],float) if _type=='fit': magsex=array(column['smagf'],float) magerrsex=array(column['smagerrf'],float) elif _type=='ph': magsex=array(column['magp3'],float) magerrsex=array(column['merrp3'],float) else: sys.exit(_type+' not valid (ph or fit)') print len(rasex) if not cutmag: cutmag=99 #else: cutmag= cutmag-2.5*math.log10(float(_exptime)) if len(compress( array(magsex) < float(cutmag) , magsex)) < 5 : cutmag=99 # not cut if only few object rasex = compress(array(magsex,float)<=cutmag,rasex) decsex = compress(array(magsex,float)<=cutmag,decsex) magerrsex = compress(array(magsex,float)<=cutmag,magerrsex) magsex = compress(array(magsex,float)<=cutmag,array(magsex)) print len(rasex) if _interactive: iraf.set(stdimage='imt1024') iraf.display(re.sub('.sn2','',img),1,fill=True,Stdout=1) vector=[] for i in range(0,len(rasex)): vector.append(str(rasex[i])+' '+str(decsex[i])) xy = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector,Stdout=1,image=img,inwcs='world',units='degrees degrees',outwcs='logical',\ formats='%10.1f %10.1f',verbose='yes')[3:] iraf.tvmark(1,'STDIN',Stdin=list(xy),mark="circle",number='yes',label='no',radii=10,nxoffse=5,nyoffse=5,color=207,txsize=2) # raw_input('here') if _catalogue: ######## use external catalogue if _catalogue[0]=='/': stdcooC=lsc.lscastrodef.readtxt(_catalogue) else: stdcooC=lsc.lscastrodef.readtxt(lsc.__path__[0]+'/standard/cat/'+_catalogue) rastdC,decstdL=array(stdcooC['ra'],float),array(stdcooC['dec'],float) lsc.util.delete('tmp.stdL.pix') colonne=str(stdcooC['rapos'])+' '+str(stdcooC['decpos']) if _catalogue[0]=='/': iraf.wcsctran(_catalogue,'tmp.stdL.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',\ columns=colonne,formats='%10.1f %10.1f',verbose='no') else: iraf.wcsctran(lsc.__path__[0]+'/standard/cat/'+_catalogue,'tmp.stdL.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',\ columns=colonne,formats='%10.1f %10.1f',verbose='no') standardpixC=lsc.lscastrodef.readtxt('tmp.stdL.pix') if _interactive: iraf.tvmark(1,'tmp.stdL.pix',mark="circle",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=204,txsize=2) xstdC=standardpixC['ra'] ystdC=standardpixC['dec'] idstdC=standardpixC['id'] xstdC=compress((array(xstdC,float)<readkey3(hdr,'XDIM'))&(array(xstdC,float)>0)&(array(ystdC,float)>0)&(array(ystdC,float)<readkey3(hdr,'YDIM')),xstdC) xstdL=xstdLL=xstdS=xstdC standardpixL=standardpixLL=standardpixS=standardpixC stdcooL=stdcooLL=stdcooS=stdcooC else: ######## check if it is landolt field stdcooL=lsc.lscastrodef.readtxt(lsc.__path__[0]+'/standard/cat/landolt.cat') rastdL,decstdL=array(stdcooL['ra'],float),array(stdcooL['dec'],float) lsc.util.delete('tmp.stdL.pix') iraf.wcsctran(lsc.__path__[0]+'/standard/cat/landolt.cat','tmp.stdL.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',\ columns='1 2',formats='%10.1f %10.1f',verbose='no') standardpixL=lsc.lscastrodef.readtxt('tmp.stdL.pix') if _interactive: iraf.tvmark(1,'tmp.stdL.pix',mark="circle",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=204,txsize=2) print 'yelow circles sextractor' xstdL=standardpixL['ra'] ystdL=standardpixL['dec'] idstdL=standardpixL['id'] xstdL=compress((array(xstdL,float)<readkey3(hdr,'XDIM'))&(array(xstdL,float)>0)&(array(ystdL,float)>0)&(array(ystdL,float)<readkey3(hdr,'YDIM')),xstdL) ######## check if it is Stetson field stdcooLL=lsc.lscastrodef.readtxt(lsc.__path__[0]+'/standard/cat/StetsonCat.dat') ww=asarray([i for i in range(len(stdcooLL['ra'])) if ( abs(float(stdcooLL['ra'][i])-float(_ra))<.2 and abs(float(stdcooLL['dec'][i])-_dec)<.2 )]) if len(ww)>0: for hh in stdcooLL.keys(): if type(stdcooLL[hh])!=int: if hh not in ['id','ra','dec']: stdcooLL[hh]=array(array(stdcooLL[hh])[ww],float) else: stdcooLL[hh]=array(stdcooLL[hh])[ww] lll=[] for i in range(0,len(stdcooLL['ra'])): lll.append(stdcooLL['ra'][i]+' '+stdcooLL['dec'][i]) rastdLL,decstdLL=array(stdcooLL['ra'],float),array(stdcooLL['dec'],float) lsc.util.delete('tmp.stdLL.pix') iraf.wcsctran('STDIN','tmp.stdLL.pix',img,inwcs='world',Stdin=lll,units='degrees degrees',outwcs='logical',\ columns='1 2',formats='%10.1f %10.1f',verbose='no') if _interactive: iraf.tvmark(1,'tmp.stdLL.pix',mark="cross",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=204,txsize=2) print 'red crosses Stetson' standardpixLL={} for ii in stdcooLL.keys(): standardpixLL[ii]=stdcooLL[ii] standardpixLL['ra']=array(iraf.proto.fields('tmp.stdLL.pix',fields='1',Stdout=1),float) #standardpixLL['ra'] standardpixLL['dec']=array(iraf.proto.fields('tmp.stdLL.pix',fields='2',Stdout=1),float) #standardpixLL['dec'] xstdLL=array(iraf.proto.fields('tmp.stdLL.pix',fields='1',Stdout=1),float) #standardpixLL['ra'] ystdLL=array(iraf.proto.fields('tmp.stdLL.pix',fields='2',Stdout=1),float) #standardpixLL['dec'] idstdLL=standardpixLL['id'] xstdLL=compress((array(xstdLL,float)<readkey3(hdr,'XDIM'))&(array(xstdLL,float)>0)&(array(ystdLL,float)>0)&(array(ystdLL,float)<readkey3(hdr,'YDIM')),xstdLL) ######## check if it is sloan field magsel0,magsel1=12,18 _ids=lsc.lscastrodef.sloan2file(_ra,_dec,20,float(magsel0),float(magsel1),'_tmpsloan.cat') ascifile='_tmpsloan.cat' stdcooS=lsc.lscastrodef.readtxt(ascifile) rastdS,decstdS=array(stdcooS['ra'],float),array(stdcooS['dec'],float) lsc.util.delete('tmp.stdS.pix') iraf.wcsctran(ascifile,'tmp.stdS.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',columns='1 2',formats='%10.1f %10.1f',verbose='no') standardpixS=lsc.lscastrodef.readtxt('tmp.stdS.pix') if _interactive: iraf.tvmark(1,'tmp.stdS.pix',mark="cross",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=205,txsize=2) print 'green cross sloan' xstdS=standardpixS['ra'] ystdS=standardpixS['dec'] idstdS=standardpixS['id'] xstdS=compress((array(xstdS,float)<readkey3(hdr,'XDIM'))&(array(xstdS,float)>0)&(array(ystdS,float)>0)&(array(ystdS,float)<readkey3(hdr,'YDIM')),xstdS) ##############################################3 if not _catalogue and len(xstdLL)>0: xstdL=xstdLL standardpixL=standardpixLL stdcooL=stdcooLL if _filter in ['U', 'B', 'V', 'R','I','Bessell-B','Bessell-V','Bessell-R','Bessell-I']: filters={'U':'U', 'B':'B', 'V':'V', 'R':'R', 'I':'I','Bessell-B':'B','Bessell-V':'V','Bessell-R':'R','Bessell-I':'I'} if _color: colors=lsc.myloopdef.chosecolor(_color,False) if not colors: colors={'U':['UB'],'B':['UB','BV'],'V':['BV','VR'],'R':['VR','RI'],'I':['RI']} else: colors={'U':['UB'],'B':['UB','BV'],'V':['BV','VR'],'R':['VR','RI'],'I':['RI']} if _field=='sloan': standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]} print 'filters and field selected do not match' else: _field='landolt' if len(xstdL)>=1: standardpix=standardpixL stdcoo=stdcooL if not _catalogue: if len(xstdLL)>0: _catalogue='StetsonCat.dat' else: _catalogue='landolt.dat' elif len(xstdS)>=1: if not _catalogue: _catalogue='sdss8' standardpix=standardpixS stdcoo=stdcooS stdcoo=lsc.lscastrodef.transformsloanlandolt(stdcoo) if not _catalogue: _catalogue='sdss8' print '\n### transform sloan in landolt' else: print 'landolt, but catalogue not found' standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]} elif _filter in ['up','gp','rp','ip','zs','SDSS-G','SDSS-R','SDSS-I','Pan-Starrs-Z']: filters={'up':'u','ip':'i','gp':'g','rp':'r','zs':'z','SDSS-G':'g','SDSS-R':'r','SDSS-I':'i','Pan-Starrs-Z':'z'} if _color: colors=lsc.myloopdef.chosecolor(_color,False) if not colors: colors={'i':['ri','iz'],'r':['gr','ri'],'g':['ug','gr'],'z':['iz'],'u':['ug']} else: colors={'i':['ri','iz'],'r':['gr','ri'],'g':['ug','gr'],'z':['iz'],'u':['ug']} if _field=='landolt': standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]} print 'filters and field selected do not match' else: _field='sloan' if len(xstdS)>=1: if not _catalogue: _catalogue='sdss8' standardpix=standardpixS stdcoo=stdcooS elif len(xstdL)>=1: standardpix=standardpixL stdcoo=stdcooL stdcoo=lsc.lscastrodef.transformlandoltsloan(stdcoo) if not _catalogue: _catalogue='landolt.dat' print '\n### transform landolt to sloan' else: print 'sloan, but not in the sdss footprint' standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]} xstd=standardpix['ra'] ystd=standardpix['dec'] idstd=standardpix['id'] rastd,decstd=array(stdcoo['ra'],float),array(stdcoo['dec'],float) xstd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)&(array(ystd,float)<readkey3(hdr,'YDIM')),xstd) if len(xstd0)>1: ######## go only if standard stars are in the field ########## magstd0={} airmass0={} result={} fileph={} print '\n### standard field: '+str(_field) ystd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\ &(array(ystd,float)<readkey3(hdr,'YDIM')),ystd) rastd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\ &(array(ystd,float)<readkey3(hdr,'YDIM')),rastd) decstd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\ &(array(ystd,float)<readkey3(hdr,'YDIM')),decstd) idstd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\ &(array(ystd,float)<readkey3(hdr,'YDIM')),idstd) stdcoo0={} for key in stdcoo.keys(): if key in 'ugrizUBVRI': stdcoo0[key]=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\ &(array(ystd,float)<readkey3(hdr,'YDIM')),stdcoo[key]) ############################################################### # pos0 = standard pos1 = sextractor distvec,pos0,pos1=lsc.lscastrodef.crossmatch(array(rastd0),array(decstd0),array(rasex),array(decsex),10) for key in stdcoo0.keys(): stdcoo0[key]=stdcoo0[key][pos0] rastd0=rastd0[pos0] decstd0=decstd0[pos0] idstd0=idstd0[pos0] rasex=rasex[pos1] decsex=decsex[pos1] # after change in may 2013 mag in sn2.fits file are already at 1s magsex=magsex[pos1]-kk[filters[_filter]]*float(_airmass) # - K x airmass # magsex=magsex[pos1]+2.5*math.log10(float(_exptime))-kk[filters[_filter]]*float(_airmass) # mag exptime - K x airmass ################################################################################# if _field=='landolt': print '\n### landolt system' for _filtlandolt in 'UBVRI': if _filtlandolt==filters[_filter]: airmass0[_filtlandolt]= 0 #_airmass else: airmass0[_filtlandolt]= 0 magstd0[_filtlandolt]=stdcoo0[_filtlandolt] fileph['mU']=zeros(len(rastd0))+999 fileph['mB']=zeros(len(rastd0))+999 fileph['mV']=zeros(len(rastd0))+999 fileph['mR']=zeros(len(rastd0))+999 fileph['mI']=zeros(len(rastd0))+999 fileph['V']=magstd0['V'] fileph['BV']=array(array(magstd0['B'],float)-array(magstd0['V'],float),str) fileph['UB']=array(array(magstd0['U'],float)-array(magstd0['B'],float),str) fileph['VR']=array(array(magstd0['V'],float)-array(magstd0['R'],float),str) fileph['RI']=array(array(magstd0['R'],float)-array(magstd0['I'],float),str) elif _field=='sloan': for _filtsloan in 'ugriz': if _filtsloan==filters[_filter]: airmass0[_filtsloan]= 0 # _airmass else: airmass0[_filtsloan]=0 magstd0[_filtsloan]=stdcoo0[_filtsloan] fileph['mu']=zeros(len(rastd0))+999 fileph['mg']=zeros(len(rastd0))+999 fileph['mr']=zeros(len(rastd0))+999 fileph['mi']=zeros(len(rastd0))+999 fileph['mz']=zeros(len(rastd0))+999 fileph['r']=magstd0['r'] fileph['gr']=array(array(magstd0['g'],float)-array(magstd0['r'],float),str) fileph['ri']=array(array(magstd0['r'],float)-array(magstd0['i'],float),str) fileph['ug']=array(array(magstd0['u'],float)-array(magstd0['g'],float),str) fileph['iz']=array(array(magstd0['i'],float)-array(magstd0['z'],float),str) ######################################################################################## zero=[] magcor=[] fil = open(re.sub('.fits','.ph',img),'w') fil.write(str(_instrume)+' '+str(_date)+'\n') fil.write('*** '+_object+' '+str(len(magsex))+'\n') if _field=='landolt': fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(1),str(1),str(1),str(1),str(1))) # exptime fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(airmass0['U']),str(airmass0['B']),str(airmass0['V']),str(airmass0['R']),str(airmass0['I']))) elif _field=='sloan': fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(1),str(1),str(1),str(1),str(1))) # exptime fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(airmass0['u']),str(airmass0['g']),str(airmass0['r']),str(airmass0['i']),str(airmass0['z']))) for i in range(0,len(magsex)): fileph['m'+filters[_filter]][i]=magsex[i] # instrumental mangitude of std in pos0[i] if _field=='landolt': stringastandard='%12.12s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s' % (idstd0[i],fileph['V'][i],fileph['BV'][i],fileph['UB'][i],\ fileph['VR'][i],fileph['RI'][i]) fil.write('%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%60.60s\n' \ % (str(fileph['mU'][i]),str(fileph['mB'][i]),str(fileph['mV'][i]),str(fileph['mR'][i]),str(fileph['mI'][i]),str(stringastandard))) elif _field=='sloan': stringastandard='%12.12s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s' % (idstd0[i],fileph['r'][i],fileph['gr'][i],fileph['ug'][i],\ fileph['ri'][i],fileph['iz'][i]) fil.write('%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%60.60s\n' \ % (str(fileph['mu'][i]),str(fileph['mg'][i]),str(fileph['mr'][i]),str(fileph['mi'][i]),str(fileph['mz'][i]),str(stringastandard))) zero.append(float(float(magstd0[filters[_filter]][i]))-float(magsex[i])) magcor.append(magsex[i]) fil.close() if show: import time from pylab import ion,plot,draw ion() aa=mean(compress(abs(array(zero))<99,zero)) xxx=compress((abs(array(magcor))<99)&(abs(array(zero))<99),magcor) yyy=compress((abs(array(zero))<99)&(abs(array(magcor))<99),zero) plot(xxx,yyy,'or') plot([min(compress(abs(array(magcor))<99,magcor)),max(compress(abs(array(magcor))<99,magcor))],[aa,aa],'-b') draw() print std(compress(abs(array(zero))<99,zero)) time.sleep(5) colorvec=colors[filters[_filter]] for col in colorvec: col0=magstd0[col[0]] col1=magstd0[col[1]] colstd0=array(col0,float)-array(col1,float) ################## sex ###################### colore=[] for i in range(0,len(pos1)): colore.append(colstd0[i]) colore1=compress(abs(array(zero))<50,array(colore)) zero1=compress(abs(array(zero))<50,array(zero)) zero2=compress(abs(array(colore1))<2,array(zero1)) colore2=compress(abs(array(colore1))<2,array(colore1)) if _fix: fisso=colorefisso[filters[_filter]+col] else: fisso='' if len(colore2)==0: print 'no calibration, '+_filter+' '+_field b,a,sa,sb=9999,9999,0,0 else: if _interactive: a,sa,b,sb=fitcol(colore2,zero2,_filter,col,fisso) else: a,sa,b,sb=fitcol2(colore2,zero2,_filter,col,fisso,show,rejection) print a,sa,b,sb result[filters[_filter]+col]=[a,sa,b,sb] if result: print '\n### zeropoint ..... done at airmass 0' if _catalogue: lsc.util.updateheader(img,0,{'CATALOG':[str(string.split(_catalogue,'/')[-1]),'catalogue source']}) stringa='' for ll in result: for kk in range(0,len(result[ll])): if not isfinite(result[ll][kk]): result[ll][kk]=0.0 valore='%3.3s %6.6s %6.6s %6.6s %6.6s' % (str(ll),str(result[ll][0]),str(result[ll][2]),str(result[ll][1]),str(result[ll][3])) print '### ',valore lsc.util.updateheader(img,0,{'zp'+ll:[str(valore),'a b sa sb in y=a+bx']}) if ll[0]==ll[2]: num=2 elif ll[0]==ll[1]: num=1 else: sys.exit('somthing wrong with color '+ll) print ll,num try: print 'zcol'+str(num),ll[1:],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1] lsc.mysqldef.updatevalue(database,'zcol'+str(num),ll[1:],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'z'+str(num),result[ll][0],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'c'+str(num),result[ll][2],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'dz'+str(num),result[ll][1],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'dc'+str(num),result[ll][3],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]): lsc.mysqldef.updatevalue(database,'zcol'+str(num),ll[1:],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'z'+str(num),result[ll][0],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'c'+str(num),result[ll][2],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'dz'+str(num),result[ll][1],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'dc'+str(num),result[ll][3],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) if result[ll][0]!=9999: print _catalogue lsc.mysqldef.updatevalue(database,'zcat',string.split(_catalogue,'/')[-1],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]): lsc.mysqldef.updatevalue(database,'zcat',string.split(_catalogue,'/')[-1],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) else: lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]): lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) except: print 'module mysqldef not found'
#! /usr/local/bin/python import os, sys import numpy as np from pyraf import iraf from odi_calibrate import download_sdss, js_calibrate from sdss_fit import getVabs from collections import OrderedDict iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) def getHImass(object, dm): # print object, mpc uchvcdb = os.path.dirname(os.path.abspath(__file__))+'/predblist.sort.csv' name, mass = np.loadtxt(uchvcdb, usecols=(1,6), dtype=str, delimiter=',', unpack=True) # find the right row coord = [i for i,this in enumerate(mass) if object.upper() in name[i]][0] # print 'the HI mass of', name[coord], 'is', mass[coord], 'at 1 Mpc' # mpc = mpc/1000. mpc = pow(10,((dm + 5.)/5.))/1000000. logm = float(mass[coord]) mass = mpc*mpc*10**logm # make sure to scale by the distance in Mpc^2 print '{:3.1f}'.format(np.log10(mass))
def display_image(img, frame, _z1, _z2, scale, _xcen=0.5, _ycen=0.5, _xsize=1, _ysize=1, _erase='yes'): goon = 'True' import glob, subprocess, os, time # removing u : this option does not work on mac ds9 = subprocess.Popen("ps -U {:d} | grep -v grep | grep ds9".format( os.getuid()), shell=True, stdout=subprocess.PIPE).stdout.readlines() if len(ds9) == 0: subproc = subprocess.Popen('ds9', shell=True) time.sleep(3) if glob.glob(img): from pyraf import iraf iraf.images(_doprint=0) iraf.tv(_doprint=0) import string, os if _z2: try: sss=iraf.display(img + '[0]', frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,\ fill='yes', zscale='no', zrange='no', z1=_z1, z2=_z2,Stdout=1) except: print '' print '### ERROR: PROBLEM OPENING DS9' print '' goon = 'False' else: try: sss = iraf.display(img + '[0]', frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase, fill='yes', Stdout=1) except: print '' print '### ERROR: PROBLEM OPENING DS9' print '' goon = False if scale and goon: answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ') if not answ0: answ0 = 'y' elif answ0 == 'no' or answ0 == 'NO': answ0 = 'n' while answ0 == 'n': _z11 = float(string.split(string.split(sss[0])[0], '=')[1]) _z22 = float(string.split(string.split(sss[0])[1], '=')[1]) z11 = raw_input('>>> z1 = ? [' + str(_z11) + '] ? ') z22 = raw_input('>>> z2 = ? [' + str(_z22) + '] ? ') if not z11: z11 = _z11 else: z11 = float(z11) if not z22: z22 = _z22 else: z22 = float(z22) print z11, z22 sss=iraf.display(img + '[0]',frame,fill='yes', xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,\ zrange='no', zscale='no', z1=z11, z2=z22, Stdout=1) answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ') if not answ0: answ0 = 'y' elif answ0 == 'no' or answ0 == 'NO': answ0 = 'n' if goon: _z1, _z2 = string.split(string.split(sss[0])[0], '=')[1], string.split( string.split(sss[0])[1], '=')[1] else: print 'Warning: image ' + str(img) + ' not found in the directory ' return _z1, _z2, goon
def escut(image, pos_file, fwhm, peak): # input image file name, file name with matched source positions, **np.array of fwhm measurements for each source import numpy as np import matplotlib.pyplot as plt from scipy import stats from pyraf import iraf # import sewpy import os from matplotlib.path import Path iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) iraf.unlearn(iraf.phot,iraf.datapars,iraf.photpars,iraf.centerpars,iraf.fitskypars) iraf.apphot.phot.setParam('interactive',"no") iraf.apphot.phot.setParam('verify',"no") iraf.datapars.setParam('datamax',50000.) iraf.datapars.setParam('gain',"gain") iraf.datapars.setParam('ccdread',"rdnoise") iraf.datapars.setParam('exposure',"exptime") iraf.datapars.setParam('airmass',"airmass") iraf.datapars.setParam('filter',"filter") iraf.datapars.setParam('obstime',"time-obs") # iraf.datapars.setParam('obstime',"date-obs") iraf.datapars.setParam('sigma',"INDEF") iraf.photpars.setParam('zmag',0.) iraf.centerpars.setParam('cbox',9.) iraf.centerpars.setParam('maxshift',3.) iraf.fitskypars.setParam('salgorithm',"median") iraf.fitskypars.setParam('dannulus',10.) # clean up the indefs so we can actually do stats, but reassign them to 99999 so we don't lose track of things # keep a separate list without them to do the median (we need floats) indefs = np.where(fwhm=='INDEF') good = np.where(fwhm!='INDEF') fwhm[indefs] = 99.999 fwhm = fwhm.astype(float) fwhm_good = fwhm[good].astype(float) indefs = np.where(peak=='INDEF') peak[indefs] = -999.999 peak = peak.astype(float) peak_good = peak[good].astype(float) if not os.path.isfile(image[0:-5]+'.txdump'): # findavgfwhm = sewpy.SEW( # params = ["X_IMAGE", "Y_IMAGE", "FWHM_IMAGE", "FLAGS"], # config = {"DETECT_THRESH":200.0}, # sexpath = "sex" # ) # # out = findavgfwhm(image)["table"] # # fwhms = out['FWHM_IMAGE'] # This is an astropy table. # flags = out['FLAGS'] # get a really rough estimate of the stellar FWHM in the image to set apertures # use the input fwhm measurement # ap1x = fwhm_est # xpos = datatable['X_IMAGE'] # ypos = datatable['Y_IMAGE'] # fwhm = datatable['FWHM_IMAGE'] # flags = datatable['FLAGS'] # idno = datatable['NUMBER'] ap1x = np.median(fwhm_good) # only use isolated detections of stars, this is the 1x aperture # print ap1x ap2x = 2.0*ap1x # these = [ i for i,id in enumerate(idno) if (flags[i] == 0)] # with open(image[0:-5]+'.escut.pos','w+') as f: # for j in range(len(xpos)): # print >> f, xpos[j], ypos[j], fwhm[j], idno[j] iraf.datapars.setParam('fwhmpsf',ap1x) iraf.photpars.setParam('apertures',repr(ap1x)+', '+repr(ap2x)) iraf.fitskypars.setParam('annulus',4.*ap1x) iraf.apphot.phot(image=image, coords=pos_file, output=image[0:-5]+'.phot') with open(image[0:-5]+'.txdump','w+') as txdump_out : iraf.ptools.txdump(textfiles=image[0:-5]+'.phot', fields="id,mag,merr,msky,stdev,rapert,xcen,ycen,ifilter,xairmass,image", expr='MAG[1] != INDEF && MERR[1] != INDEF && MAG[2] != INDEF && MERR[2] != INDEF', headers='no', Stdout=txdump_out) mag1x, mag2x = np.loadtxt(image[0:-5]+'.txdump', usecols=(1,2), unpack=True) iraf_id = np.loadtxt(image[0:-5]+'.txdump', usecols=(0,), dtype=int, unpack=True) # idno = np.loadtxt(image[0:-5]+'.escut.pos', usecols=(3,), dtype=int, unpack=True) xpos, ypos = np.loadtxt(pos_file, usecols=(0,1), unpack=True) keepIndex = iraf_id - 1 xpos, ypos, fwhm, peak = xpos[keepIndex], ypos[keepIndex], fwhm[keepIndex], peak[keepIndex] # print idno.size, iraf_id.size, xpos.size diff = mag2x - mag1x diffCut = diff magCut = mag2x xCut = xpos#[good] yCut = ypos#[good] idCut = iraf_id fwhmCut = fwhm#_good peakCut = peak print peakCut.size, magCut.size, diffCut.size print diffCut.size, 0, np.median(diffCut), diffCut.std() nRemoved = 1 # plt.clf() # plt.scatter(peakCut, magCut, edgecolor='none') # plt.savefig('peaktest.pdf') plt.clf() # plt.hlines(bin_edges, -2, 1, colors='red', linestyle='dashed') plt.scatter(diff, mag2x, edgecolor='none', facecolor='black', s=4) # plt.scatter(diffCut, magCut, edgecolor='none', facecolor='blue', s=4) magdiff = zip(magCut.tolist(), diffCut.tolist(), peakCut.tolist(), idCut.tolist()) dtype = [('mag',float), ('diff', float), ('peak', float), ('id', int)] magdiff = np.array(magdiff, dtype=dtype) magSort = np.sort(magdiff, order='peak') peakRange = (magSort['peak'] > 20000.0) & (magSort['peak'] < 40000.0) peakVal = np.median((magSort['diff'])[np.where(peakRange)]) # peakVal = np.median(diffCut) print peakVal plt.scatter((magSort['diff'])[np.where(peakRange)], (magSort['mag'])[np.where(peakRange)], edgecolor='none', facecolor='blue', s=4) while nRemoved != 0: nBefore = diffCut.size diffCheck = np.where(abs(peakVal-diffCut) < 2.5*diffCut.std())#[i for i,d in enumerate(diff) if (-0.5 < d < 0.0)] # diffCut = diffCut[diffCheck] nRemoved = nBefore - diffCut.size magCut = magCut[diffCheck] xCut = xCut[diffCheck] yCut = yCut[diffCheck] idCut = idCut[diffCheck] fwhmCut = fwhmCut[diffCheck] print diffCut.size, nRemoved, np.median(diffCut), diffCut.std() if 0.05 < diffCut.std() <0.06: nRemoved=0 # plt.fill_betweenx(bin_centers, bin_meds+3.0*bin_stds, bin_meds-3.0*bin_stds, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region') # with open('escutSTD_i.pos','w+') as f: # for i,blah in enumerate(xCut): # print >> f, xCut[i], yCut[i], diffCut[i] bin_meds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic='median', bins=24, range=(magCut.min(),magCut.max())) bin_stds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic=np.std, bins=24, range=(magCut.min(),magCut.max())) bin_width = (bin_edges[1] - bin_edges[0]) bin_centers = bin_edges[1:] - bin_width/2 # print bin_meds, bin_stds left_edge = np.array(zip(peakVal-2.5*bin_stds, bin_centers)) right_edge = np.flipud(np.array(zip(peakVal+2.5*bin_stds, bin_centers))) # print left_edge, right_edge verts = np.vstack((left_edge, right_edge)) # print verts # verts = np.delete(verts, np.array([0,1,2,22,23,24,25,45,46,47]), axis=0) # print verts esRegion = Path(verts) sources = esRegion.contains_points(zip(diff,mag2x)) # print sources with open('escutREG_i.pos','w+') as f: for i,blah in enumerate(xpos[sources]): print >> f, (xpos[sources])[i], (ypos[sources])[i], (diff[sources])[i] magCut = mag2x[sources] fwhmCut = fwhm[sources] xCut = xpos[sources] yCut = ypos[sources] diffCut = diff[sources] # find the sources that are in the std method but not the region method # print idCut, idno[sources] # extrasSTD = np.setdiff1d(idno[sources], idCut) # print extrasSTD.size # print extrasSTD # with open('escutUNIQUE.pos','w+') as f: # for i,blah in enumerate(extrasSTD): # print >> f, xpos[blah-1], ypos[blah-1] # fwhmcheck = np.loadtxt('testfwhmREG.log', usecols=(10,), unpack=True) fwhmchk2 = np.where((magCut<-4) & (fwhmCut<90.0)) print np.median(fwhmCut[fwhmchk2]), np.std(fwhmCut[fwhmchk2]) fwchk = np.where(np.abs(fwhmCut-np.median(fwhmCut[fwhmchk2])) > 10.0*np.std(fwhmCut[fwhmchk2])) drop = np.abs(fwhmCut-np.median(fwhmCut[fwhmchk2])) > 10.0*np.std(fwhmCut[fwhmchk2]) # fwmag = mag2x[sources] with open('escutVBAD_i.pos','w+') as f: for i,blah in enumerate(xCut[fwchk]): print >> f, (xCut[fwchk])[i], (yCut[fwchk])[i] with open('escut_r.pos','w+') as f: for i,blah in enumerate(xCut): if not drop[i]: print >> f, xCut[i], yCut[i] with open('escut_g.pos','w+') as f: for i,blah in enumerate(xCut): if not drop[i]: print >> f, xCut[i], yCut[i] plt.fill_betweenx(bin_centers, peakVal+2.5*bin_stds, peakVal-2.5*bin_stds, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region') plt.scatter(diffCut[fwchk], magCut[fwchk], edgecolor='none', facecolor='red', s=4) plt.ylim(0,-12) plt.xlabel('$m_{2x} - m_{1x}$') plt.ylabel('$m_{2x}$') plt.xlim(-2,1) plt.savefig('testmagiraf.pdf') plt.clf() plt.scatter(magCut, fwhmCut, edgecolor='none', facecolor='black') plt.scatter(magCut[fwchk], fwhmCut[fwchk], edgecolor='none', facecolor='red') plt.hlines([np.median(fwhmCut)], -12, 0, colors='red', linestyle='dashed') plt.hlines([np.median(fwhmCut)+fwhmCut.std(), np.median(fwhmCut)-fwhmCut.std()], -12, 0, colors='red', linestyle='dotted') plt.ylim(0,20) plt.xlim(-12,0) plt.ylabel('fwhm') plt.xlabel('$m_{2x}$') plt.savefig('fwhmcheck.pdf') return True