def info_bis(wbis): if len(wbis) > 0: with fits.open(wbis) as image: info = pd.DataFrame({"BJD":[image[0].header['HIERARCH ESO DRS BJD']], "CCF_RVC":[image[0].header['HIERARCH ESO DRS CCF RVC']], "FWHM":[image[0].header['HIERARCH ESO DRS CCF RVC']], "MASK":[image[0].header['HIERARCH ESO DRS CCF MASK']]}) key_check=image[0].header.get('HIERARCH ESO DRS DVRMS') if key_check == None: info["DVRMS"]=np.nan else: info["DVRMS"]=image[0].header['HIERARCH ESO DRS DVRMS'] #Estimated RV uncertainty [m/s] else: a=wbis.find('untar/') b=wbis.find('bis') iraf.fxcor(wbis[0:b]+"s1d_A.fits", "Sun.fits",output=wbis[0:a+6]+"RVs",interactive="no", observatory="esovlt") RVel=Table.read(wbis[0:a+6]+"RVs.txt", format='ascii') info = pd.DataFrame({"BJD":[np.nan], "CCF_RVC":[RVel['col12'][0]], "FWHM":[np.nan], "MASK":[np.nan], "DVRMS":[RVel['col14'][0]]}) os.remove(wbis[0:a+6]+"RVs.txt") os.remove(wbis[0:a+6]+"RVs.gki") os.remove(wbis[0:a+6]+"RVs.log") return info
def run_fxcor(input_file,input_rv,lines,output,fitshead_update,npoints,functiontype): iraf.fxcor( objects = input_file, \ templates = input_rv, \ apertures = "*", \ cursor = "",\ continuum = "both",\ filter = "both",\ rebin = "smallest",\ pixcorr = 0,\ osample = lines,\ rsample = lines,\ apodize = 0.2,\ function = functiontype,\ width = npoints,\ height= 0.,\ peak = 0,\ minwidth = npoints,\ maxwidth = npoints,\ weights = 1.,\ background = "INDEF",\ window = 30,\ wincenter = 0,\ output = output,\ verbose = "long",\ imupdate = fitshead_update,\ graphics = "stdgraph",\ interactive = 0,\ autowrite = 1,\ ccftype = "image",\ observatory = "sso",\ continpars = "",\ filtpars = "",\ keywpars = "")
def run_fxcor(lstl, lstu, tpl, tpu, chip): if chip == 'l': templ = tpl lst = lstl else: templ = tpu lst = lstu lista = '@' + lst chip_par = 'rvout_' + chip iraf.noao() iraf.rv() iraf.fxcor(lista, templ, output=chip_par, verbose='txtonly', interac='no', observa='paranal') rv = [] rvlist = open(chip_par + '.txt', "r") for cols in (raw.strip().split() for raw in rvlist): vobs_index = len(cols) - 3 if cols[0][0] != '#': rv.append(float(cols[vobs_index])) rvlist.close() return rv
def run_fxcor(input_file,input_rv,lines): iraf.unlearn(iraf.keywpars) iraf.filtpars.setParam("f_type","square",check=1,exact=1) iraf.filtpars.setParam("cuton",50,check=1,exact=1) iraf.filtpars.setParam("cutoff",2000,check=1,exact=1) os.system("rm fxcor_shift*") iraf.fxcor( objects = input_file, \ templates = input_rv, \ apertures = "*", \ cursor = "",\ continuum = "both",\ filter = "both",\ rebin = "smallest",\ pixcorr = 0,\ osample = lines,\ rsample = lines,\ apodize = 0.2,\ function = "gaussian",\ width = 15,\ height= 0.,\ peak = 0,\ minwidth = 15,\ maxwidth = 15,\ weights = 1.,\ background = "INDEF",\ window = "INDEF",\ wincenter = "INDEF",\ output = "fxcor_shift",\ verbose = "long",\ imupdate = 0,\ graphics = "stdgraph",\ interactive = 0,\ autowrite = 1,\ ccftype = "image",\ observatory = "sso",\ continpars = "",\ filtpars = "",\ keywpars = "") vel_shift = functions.read_ascii("fxcor_shift.txt") vel_shift = functions.read_table(vel_shift) vel_shift = str(vel_shift[0][11]) if vel_shift == "INDEF": vel_shift = 0 print "shifting by ",vel_shift,"km/s" return vel_shift
def run_fxcor(input_file, input_rv, lines, output, fitshead_update, write_ccf): if write_ccf: ccf_command = program_dir + "fxcor_cursor_command.txt" interact = 1 else: ccf_command = "" interact = 0 iraf.fxcor( objects=input_file, templates=input_rv, apertures="*", cursor=ccf_command, # cursor = "",\ continuum="both", filter="both", rebin="smallest", pixcorr=0, osample=lines, rsample=lines, apodize=0.2, function="gaussian", width=9.0, height=0.0, peak=0, minwidth=9.0, maxwidth=9.0, weights=1.0, background="INDEF", window="INDEF", wincenter="INDEF", # window = 500,\ # wincenter = 0.0,\ output=output, verbose="long", imupdate=fitshead_update, graphics="stdgraph", interactive=interact, # interactive = 1,\ autowrite=1, ccftype="text", observatory="sso", continpars="", filtpars="", keywpars="", )
def run_fxcor(input_file,input_rv,lines,output,fitshead_update,write_ccf): if write_ccf: ccf_command = program_dir + "fxcor_cursor_command.txt" if best_aperture == 1: use_apertures = "*" else: use_apertures = str(best_aperture) interact = 1 else: ccf_command = "" use_apertures = "*" interact = 0 iraf.fxcor( objects = input_file, \ templates = input_rv, \ apertures = use_apertures, \ cursor = ccf_command,\ continuum = "both",\ filter = "both",\ rebin = "smallest",\ pixcorr = 0,\ osample = lines,\ rsample = lines,\ apodize = 0.2,\ function = "gaussian",\ width = 7.0,\ height= 0.,\ peak = 0,\ minwidth = 7.0,\ maxwidth = 7.0,\ weights = 1.,\ background = "INDEF",\ window = "INDEF",\ wincenter = "INDEF",\ output = output,\ verbose = "long",\ imupdate = fitshead_update,\ graphics = "stdgraph",\ interactive = interact,\ autowrite = 1,\ ccftype = "text",\ observatory = "sso",\ continpars = "",\ filtpars = "",\ keywpars = "")
def fxcor(spec, task, template_spec, spec_list_file, fxcor_output_file, fxcor_range="4173-5447", interactive='no'): """ Run fxcor on the binned spectra to determine the relative velocity (with respect to the template spectra) INPUT: BIN_SCI, FXCOR_TEMPLATE OUTPUT: FXCOR_BIN_LIST, FXCOR_FILE(s) Note: make sure you select the parameters for your desired use. Refer to the IRAF rv package help for description of what each parameter is/does """ if os.path.exists(fxcor_output_file + '.txt'): print('File {} already exists'.format(fxcor_output_file + '.txt')) return assert task == "abs" or task == "ems", "'task' is neight 'ab' or 'ems'" spec_list = [] for i in range(len(glob.glob(spec.format('*')))): spec_list.append(spec.format(i)) assert len(spec_list) > 0, 'Input files {} do not exist'.format(spec.format('*')) np.array(spec_list).tofile(spec_list_file, sep='\n') from pyraf import iraf iraf.noao() iraf.rv() if task == 'ems': iraf.fxcor('@{}'.format(spec_list_file), spec.format(template_spec), output=fxcor_output_file, continuum="both", interactive=interactive, order=1, high_rej=2, low_rej=2, osample=fxcor_range, rsample=fxcor_range, rebin="smallest", imupdate="no", pixcorr="no", filter="both", f_type="welch", cuton=20, cutoff=1000, fullon=30, fulloff=800, ra="RA", dec="DEC", ut="UTSTART", epoch="EQUINOX", verbose="txtonly") elif task == 'abs': # Run interactively to make sure not fitting noise features, adapet osample/rsample to avoid such noise iraf.fxcor('@{}'.format(spec_list_file), spec.format(template_spec), output=fxcor_output_file, continuum="both", interactive=interactive, order=1, high_rej=2, low_rej=2, osample=fxcor_range, rsample=fxcor_range, rebin="smallest", imupdate="no", pixcorr="no", filter="both", f_type="welch", cuton=20, cutoff=800, fullon=30, fulloff=800, ra="RA", dec="DEC", ut="UTSTART", epoch="EQUINOX", verbose="txtonly") assert os.path.exists(fxcor_output_file + '.txt'), 'Error in iraf.fxcor: File {} was not created'.format( fxcor_output_file + '.txt')
def run_fxcor(st_names, templ, setup): arq = open(setup + '/' + 'speclist', 'w') for i in range(len(st_names)): arq.write(st_names[i] + '.fits\n') arq.close() lista = '@' + setup + '/' + 'speclist' clean_cr(lista) outname = setup + '/' + 'rvout_' + setup iraf.noao() iraf.rv() iraf.fxcor(lista, templ, output=outname, verbose='txtonly', interac='no', observa='paranal') rv = [] rvlist = open(outname + '.txt', "r") for cols in (raw.strip().split() for raw in rvlist): vobs_index = len(cols) - 3 if cols[0][0] != '#': rv.append(float(cols[vobs_index])) rvlist.close() return rv
def get_RV(WORK_DIR, multi_ref=False, multi_sample=False): # list RV ref files if multi_ref: rv_ref_list = glob.glob('../../*_Echelle_RVS.fits') rv_ref_list = [rf.split('/')[-1].split('.')[0] for rf in rv_ref_list] else: rv_ref_list = ['solar'] # rv_ref_list = ['solar_spectra_conv'] # print ' RV ref files list:', rv_ref_list for obj in observations[WORK_DIR]['objects']: for rv_ref in rv_ref_list: ts = time.time() rvs = [] e_rvs = [] # determine barycentric RV with open('RVS_'+obj+'_'+rv_ref+'.txt', 'w+') as file: for order in np.arange(2, 31, 1): try: iraf.wspectext(input=obj+'_vh_norm.fits[*,'+str(order)+',1]', output='template.txt', header='no') template = np.loadtxt('template.txt') wav = template[:, 0] mid1 = int(len(wav)/4.0) mid2 = int(len(wav)/12.0) wav1 = wav[mid1*1] wav2 = wav[mid1*3] #print 'WAV:', wav, mid1, wav1,wav2 # create multiple wav1, wav2 ranges if requested if multi_sample: wav_ranges = [[wav[mid2*2], wav[mid2*7]], [wav[mid2*4], wav[mid2*8]], [wav[mid2*5], wav[mid2*10]]] else: wav_ranges = [[wav1, wav2]] for wav_r in wav_ranges: iraf.unlearn('fxcor') try: os.remove('test.txt') os.remove('test.log') os.remove('test.gki') except: pass try: os.remove('template.txt') except: pass iraf.fxcor(obj+'.ec.vh', '../../'+rv_ref, apertures=order, pixcorr='no', continuum='both', osample='a %s %s' % (wav_r[0], wav_r[1]), rsample='a %s %s' % (wav_r[0], wav_r[1]), interactive='no', output='test', function = 'gaussian', apodize = 0.0, rebin = "smallest") fields = np.loadtxt('test.txt', comments='#', dtype='str') # print wav_r # print fields vrel, verr = fields[-3], fields[-1] # vrel, verr = fields[-3], 0. if vrel == 'INDEF' or np.float(verr) > 10. or np.abs(np.float(vrel)) > 350.: vrel = np.nan verr = np.nan print order, vrel, verr file.write("%s %s %s\n" % (order, vrel, verr)) rvs.append(float(vrel)) e_rvs.append(float(verr)) except: print_exception() # export all values in a list # file.write('\n') # file.write("'rv_echelle':[" + ','.join([str(v) for v in rvs]) + "]\n") # file.write("'e_rv_echelle':[" + ','.join([str(v) for v in e_rvs]) + "]\n") print '%s: RV median: %s, RV mean: %s, RV sigma: %s' % (obj, np.nanmedian(rvs), np.nanmean(rvs), np.nanstd(rvs)) print 'RV fit time: '+str((time.time()-ts)/60.)+' min' print ''
def rv_plot(filelist_new): rv_directory = os.path.dirname(filelist_new[0]) os.chdir(rv_directory) template = "telutemplate.fits" basedir = os.path.dirname(filelist_new[0]) templatefile = os.path.join(basedir, template) if os.path.exists("telushift.txt"): os.remove("telushift.txt") if os.path.exists("rvshift.txt"): os.remove("rvshift.txt") iraf.fxcor(objects=os.path.join(basedir,"*merged.fits"), templates = templatefile, continuum = "both", filter="none", rebin="smallest", \ function = "gaussian", width = "10.0", height="0.0", minwidth ="3.0", maxwidth = "20.0", window = "200.0", background= "0.2", \ output = "telushift", verbose = "txtonly", osample = "5880:5975", rsample = "5880:5975", interactive = "no") rvs = [] time = [] with open("telushift.txt", "r") as defaultfile: for line in defaultfile: if "#" in line: pass else: rvs.append(line[-30:-21]) for i in range(len(filelist_new)): hdulist = fits.open(filelist_new[i]) time.append(hdulist[0].header['JD-START']) plt.plot(time, rvs, '.') template = "mascara_megatemplate.fits" templatefile = os.path.join(basedir, template) ### For A Type Stars iraf.fxcor(objects=os.path.join(basedir,"*rvcorrect_TAC.fits"), templates = templatefile, continuum = "both", filter="template", rebin="smallest", \ function = "center1d", width = "200.0", height="0.0", minwidth ="3.0", maxwidth = "200.0", window = "1000.0", \ output = "rvshift", verbose = "txtonly", osample = "4610:4830, 4893:5679, 6019:6524, 6598:6860", rsample = "4610:4830, 4893:5679, 6019:6524, 6598:6860", interactive = "no") ### For G Type Stars # iraf.fxcor(objects=os.path.join(basedir,"*rvcorrect_TAC.fits"), templates = templatefile, continuum = "both", filter="both", rebin="smallest", \ # function = "gaussian", width = "20.0", height="0.2", minwidth ="10.0", maxwidth = "200.0", window = "200.0", \ # output = "rvshift", verbose = "txtonly", osample = "4900:6450", rsample = "4900:6450", interactive = "no") rvs2 = [] time2 = [] with open("rvshift.txt", "r") as defaultfile: for line in defaultfile: if "#" in line: pass else: rvs2.append(line[-30:-21]) # iraf.fxcor(objects=os.path.join(basedir,"*rvcorrect_TAC.fits"), templates = templatefile, continuum = "both", filter="both", rebin="smallest", \ # function = "gaussian", width = "20.0", height="0.2", minwidth ="10.0", maxwidth = "200.0", window = "200.0", \ # output = "rvshift", verbose = "txtonly", osample = "4900:6450", rsample = "4900:6450", interactive = "no") # rvs3 = [] # with open("rvshift.txt", "r") as defaultfile: # for line in defaultfile: # if "#" in line: # pass # else: # rvs3.append(line[-30:-21]) for i in range(len(filelist_new)): hdulist = fits.open(filelist_new[i]) time2.append(hdulist[0].header['JD-START']) # iraf.dopcor(filelist_new[i], filelist_new[i].replace("merged_TAC","starvelocity"), redshift= rvs2[i], isvelocity = "yes") with open(os.path.join(basedir, "rvshift.dat"), "w") as rvfile: with open(os.path.join(basedir, "telushift.dat"), "w") as telufile: with open(os.path.join(basedir, "rvtelushift.dat"), "w") as rvtelufile: for i in range(len(rvs)): rvfile.write(str(time[i]) + "," + rvs2[i] + "\n") telufile.write(str(time[i]) + "," + rvs[i] + "\n") final_shift = float(rvs2[i]) + float(rvs[i]) rvtelufile.write( str(time[i]) + "," + str(final_shift) + "\n") plt.plot(time2, rvs2, '.') plt.show()
obj.append(fits.open(i)[0].header['OBJECT']) try: template_loc=obj.index(ref_star_name) except ValueError: print "Reference star %s not found, exiting..." % (ref_star_name) sys.exit(1) template_image_id=t[template_loc] print "Ref star %s found as spectrum %s" % (ref_star_name,template_image_id) # loop over the spectra for i in range(0,len(t)): image_id=t[i] if args.blends == 'nblends' and n_traces[i] == 1: outfile="%s.out" % (image_id) iraf.fxcor(objects=image_id,template=template_image_id,osample='6600-6800',rsample='6600-6800',output=outfile) logfile="%s.log" % (outfile) com='' while (com.upper() not in coms): com=raw_input('Single Peaked = SP - Double Peaked = DP - Broad Peak = BP - No Peak = NP\nEnter comment: ') done=updateOutput(logfile,image_id,com) # add this back into the loop later # code up the BLENDs later when analysed the first time if args.blends=='yblends' and n_traces[i] > 1: for j in range(1,n_traces[i]+1): outfile="%s.out%d" % (image_id,j) logfile="%s.log" % (outfile) iraf.fxcor(objects=image_id,template=template_image_id,apertures=str(j),osample='6600-6800',rsample='6600-6800',output=outfile)
def gen_ar(): #cargo resultado de f2c temps, pasos, intensidad = f2c(res_tem="S", step_tem=100) x = pasos y = temps z = intensidad print(x[z.index(max(z))], y[z.index(max(z))]) os.chdir("../") # obtengo parametros para para fxcor obfx = "aB" + str(x[z.index(max(z))]) temp = str(int(y[z.index(max(z))])) if len(temp) == 4: temp = "0" + temp iraf.imdel("ss") iraf.imdel("tem") iraf.sarit("T" + temp, "-", 1, "ss") iraf.sarit("ss", "*", "amortigua", "tem") fig = plt.figure(figsize=(12, 9.0)) fig.subplots_adjust(hspace=0.4, bottom=0.06, top=0.94, left=0.12, right=0.94) #ejecuto y ploteo fxcor ax2 = plt.subplot2grid((4, 1), (0, 0)) # grafico la func, de correlacion program_dir = os.getcwd() + "/" ccf_command = program_dir + "fxcor_cursor_command.txt" #comando para extraeer los datos del grafico interact = 1 iraf.imdel("new_ccf.txt") iraf.fxcor(object=obfx, templates="tem", cursor=ccf_command, ccftype="text", interactive=interact) #fxcor de irad data = asciitable.read("new_ccf.txt") #archivo de salida del fxcor ax2.set_xlim(-2000, 2000) plt.axhline(linewidth=2, color='r') plt.plot(data["col1"], data["col2"], "-", color="black") plt.axvspan(-100, 100, facecolor='grey', alpha=0.5) #plt.axhspan(0.9, 1.00, facecolor='grey', alpha=0.5) ax2.yaxis.set_major_locator(plt.MaxNLocator(5)) ax2.set_xlabel(r'Velocity', fontsize=18) ax2.set_ylabel(r'Correlation', fontsize=18) #datos para scale iraf.imdel("scale") iraf.imdel("tplsca") obscale = "B" + str(x[z.index(max(z))]) temp = "T" + temp w1 = 5900 w2 = 6490 wi = 300 iraf.imdel("scale.txt") iraf.imdel("tplsca.txt") iraf.scale(spec=obscale, tpl=temp, w1=w1, w2=w2, width=wi) iraf.wspectext("scale.fits", "scale.txt", header="no") iraf.wspectext("tplsca.fits", "tplsca.txt", header="no") iraf.wspectext(obscale, "obscale.txt", header="no") #ploteo resultados de f2c ax1 = plt.subplot2grid((4, 1), (1, 0), rowspan=3) # define grid. xi = np.linspace(min(x), max(x), 1000) yi = np.linspace(min(y), max(y), 1000) # grid the data. zi = griddata((x, y), z, (xi[None, :], yi[:, None]), method='linear') # contour the gridded data, plotting dots at the randomly spaced data points. CS = plt.contour(xi, yi, zi, 15, linewidths=0.5, colors='k') CS = plt.contourf(xi, yi, zi, 15, cmap=plt.cm.jet) cb = plt.colorbar(orientation='horizontal', pad=0.1, aspect=30) # draw colorbar cb.set_label('Intensity', fontsize=18) # plot data points. plt.scatter(x, y, marker='o', c='b', s=5) plt.scatter(x[z.index(max(z))], y[z.index(max(z))], marker='o', color="w", s=20) plt.xlabel('Mass ratio') plt.ylabel('Secondary Temperature (K)') ax1.set_xlabel(r'Mass ratio', fontsize=18) ax1.set_ylabel(r'Secondary Temperature (K)', fontsize=18) ax1.set_xlim(min(pasos), max(pasos)) ax1.set_ylim(min(temps), max(temps)) ax1.yaxis.set_major_locator(plt.MaxNLocator(6)) ax1.xaxis.set_major_locator(plt.MaxNLocator(8)) ax2.text(0.03, 0.96, "T=%s K" % y[z.index(max(z))], ha='left', va='top', transform=ax2.transAxes, fontsize=20) ax2.text(0.04, 0.5, "q=%s" % x[z.index(max(z))], ha='left', va='top', transform=ax2.transAxes, fontsize=20) #plot scale fig = plt.figure(figsize=(12, 9.0)) fig.subplots_adjust(hspace=0.4, bottom=0.06, top=0.94, left=0.12, right=0.94) ax2 = plt.subplot2grid((4, 1), (0, 0)) data = asciitable.read("tplsca.txt") #archivo de salida del scale #ax2.set_xlim(min(data["col1"]),max(data["col1"])) plt.plot(data["col1"], data["col2"], "-", color="red") data = asciitable.read("obscale.txt") plt.plot(data["col1"], data["col2"], "-", color="black") ax2.yaxis.set_major_locator(plt.MaxNLocator(5)) ax2.set_xlabel(r'Wavelength ($\AA$)', fontsize=18) ax2.set_ylabel(r'Intensity', fontsize=18) data = asciitable.read("scale.txt") #archivo de salida del scale ax2.set_xlim(min(data["col1"]), max(data["col1"]) - 1) plt.plot(data["col1"], data["col2"] * 100, "-", color="black") ax2.yaxis.set_major_locator(plt.MaxNLocator(5)) ax2.set_xlabel(r'Wavelength ($\AA$)', fontsize=18) ax2.set_ylabel(r'$I_2/I_T$ (%)', fontsize=18) #plt.savefig('mass_temp.pdf',format="pdf", bbox_inches='tight',dpi = 300) plt.show()
if analyse: a = [] for h in np.arange(len(analyse["CCF_RVC"])): a.append(np.float(analyse["CCF_RVC"][h])) analyse["CCF_RVC"] = a w = np.where(np.abs(analyse["CCF_RVC"]) > 1000.) num = len(analyse["CCF_RVC"][w[0]]) if num > 0: for j in w[0]: print analyse['NAME_S1D'][ j], 'I am calculating the RV shift' iraf.fxcor(star_folders[i] + "/untar/" + analyse['NAME_S1D'][j], "Sun.fits", output=star_folders[i] + "/untar/RVs", interactive="no", observatory="esovlt") RVel = Table.read(star_folders[i] + "/untar/RVs.txt", format='ascii') if RVel['col12'][0] == 'INDEF': RVel['col12'][0] = -9999 RVel['col14'][0] = -9999 analyse["CCF_RVC"][j] = RVel['col12'][0] analyse["FWHM"][j] = np.nan analyse["DVRMS"][j] = RVel['col14'][0] os.remove(star_folders[i] + "/untar/" "RVs.txt") os.remove(star_folders[i] + "/untar/" "RVs.gki") os.remove(star_folders[i] + "/untar/" "RVs.log") f = analyse["CCF_RVC"] != -9999