def vhelio_correct(WORK_DIR): print '\n + VHELIO correction\n' for obj in observations[WORK_DIR]['objects']: obj = obj+'.ec' utm = iraf.hselect(images=obj, fields='UTMIDDLE', Stdout=1, expr='yes') year = utm[0][:4] month = utm[0][5:7] day = utm[0][8:10] h = int(utm[0][11:13]) m = int(utm[0][14:16]) s = int(utm[0][17:19]) ra = iraf.hselect(images=obj, fields='RA', Stdout=1, expr='yes')[0].split(':') dec = iraf.hselect(images=obj, fields='DEC', Stdout=1, expr='yes')[0].split(':') ra = float(ra[0])+int(ra[1])/60.+float(ra[2])/3600. dec = float(dec[0])+int(dec[1])/60.+float(dec[2])/3600. shutil.copy(obj+'.fits', obj+'.vh.fits') iraf.hedit(images=obj+'.vh', fields='UT', value=h+m/60.+s/3600., add='yes', verify='no') iraf.hedit(images=obj+'.vh', fields='EPOCH', value=year, add='yes', verify='no') iraf.rvcorrect(images=obj+'.vh', imupdat='yes', epoch=year, observa='ekar', year=year, month=month, day=day, ut=h+m/60.+s/3600., ra=ra, dec=dec) iraf.dopcor(input=obj+'.vh', redshift='-vhelio', isveloc='yes', dispers='yes') obj = obj+'.nosky' shutil.copy(obj+'.fits', obj+'.vh.fits') iraf.hedit(images=obj+'.vh', fields='UT', value=h+m/60.+s/3600., add='yes', verify='no') iraf.hedit(images=obj+'.vh', fields='EPOCH', value=year, add='yes', verify='no') iraf.rvcorrect(images=obj+'.vh', imupdat='yes', epoch=year, observa='ekar', year=year, month=month, day=day, ut=h+m/60.+s/3600., ra=ra, dec=dec) iraf.dopcor(input=obj+'.vh', redshift='-vhelio', isveloc='yes', dispers='yes')
def Dopcortask(self, InputFile, OutputFile, Fits_Folder, z, Suffix = 'z'): if OutputFile == None: OutputFile = self.outputNameGenerator(InputFile, Suffix) self.deleteIrafFiles(Fits_Folder + OutputFile) dopcor_conf = self.DopcorAttributes(InputFile, OutputFile, Fits_Folder, z) iraf.dopcor(**dopcor_conf) return OutputFile
def barycor(filelist_new): iraf.reset(obsdb='home$obsdb.dat') for i in range(len(filelist_new)): hdulist = fits.open(filelist_new[i]) header_time_of_observation = hdulist[0].header['DATE-OBS'] year_of_observation = int(header_time_of_observation[:4]) month_of_observation = int(header_time_of_observation[5:7]) day_of_observation = int(header_time_of_observation[8:10]) right_ascension = hdulist[0].header['RA'] declination = hdulist[0].header['DEC'] try: ut_of_observation = hdulist[0].header['UT'] except KeyError: ut_of_observation = int(header_time_of_observation[11:13]) + int( header_time_of_observation[14:16]) / 60 + int( header_time_of_observation[17:19]) / 3600 exposure_time = hdulist[0].header['EXP_TIME'] output_filename_dummy = filelist_new[i].replace("norm.", "norm.dummy.") output_filename_dummyI = output_filename_dummy.replace( "norm-1", "norm-1.dummy.") output_filename = output_filename_dummyI.replace( "merged", "merged.dummy.") iraf.scopy(filelist_new[i], output_filename) iraf.hedit(images=output_filename, fields="UT", value=ut_of_observation) iraf.hedit(images=output_filename, fields="EPOCH", value="2000") iraf.hedit(images=output_filename, fields="EXP-TIME", value=exposure_time) iraf.rvcorrect(images=output_filename, year=year_of_observation, month=month_of_observation, day=day_of_observation, ut=ut_of_observation, ra=right_ascension, dec=declination) output_filename_final = output_filename.replace("dummy.", "rvcorrect.") print(output_filename, output_filename_final) iraf.dopcor(output_filename, output_filename_final, redshift="-VHELIO", isvelocity="yes") os.remove(output_filename) hdulist.close()
p = line.split() wavefilter_rest.append(float(p[0])) transmission_rest.append(float(p[1])) wavefilter_restv = array(wavefilter_rest) transmission_restv = array(transmission_rest) fil_rest_min= min(wavefilter_restv) fil_rest_max= int(max(wavefilter_restv)) #integer is needed for a sharper cut-off ############################################################# spec = sn + "[*,1,1]" # generally multidimension iraf.imcopy(sn+'[*,1,1]','sn.fits',verbose='no') # to create a onedimension fit to use during the script # redshift correction without absorption print '\033[34m*** correcting the spectrum for redshift without Milky Way absorption *** \033[0m' try: iraf.dopcor('sn.fits','sn_dez.fits', redshift=redshift, isveloc='no', flux='no',factor=3) iraf.dopcor('sn.fits','sn_dez_err1.fits', redshift=redserrspace1, isveloc='no', flux='no',factor=3) iraf.dopcor('sn.fits','sn_dez_err2.fits', redshift=redserrspace2, isveloc='no', flux='no',factor=3) except: print ' WARNING: Problem to redshift the spectrum' if ebvh == 0.0: # galaxy and host reddening correction print '\033[31m'+'*** correcting spectrum for galactic reddening ***\033[0m' ebv = ebvg+ebvh print '\033[31m Total E(B-V) = galactic E(B-V) = \033[0m',ebv try: iraf.unlearn("deredden") iraf.dered('sn_dez.fits',"sn_dez_dered.fits", value=ebv, R=3.1, type='E(B-V)',overrid='yes',uncorre='no') iraf.dered('sn_dez_err1.fits',"sn_dez_dered_err1.fits", value=ebv, R=3.1, type='E(B-V)',overrid='yes',uncorre='no') iraf.dered('sn_dez_err2.fits',"sn_dez_dered_err2.fits", value=ebv, R=3.1, type='E(B-V)',overrid='yes',uncorre='no')
def speccombine(fs=None): iraf.cd('work') if fs is None: fs = glob('trm/sci*c?.fits') if len(fs)==0: print("No flux calibrated images to combine.") iraf.cd('..') return #diagnostic() nsteps = 8001 lamgrid = np.linspace(2000.0, 10000.0, nsteps) nfs = len(fs) # for each aperture # get all of the science images specs = np.zeros((nfs, nsteps)) specerrs = np.zeros((nfs, nsteps)) ap = 0 for i, f in enumerate(fs): hdu = pyfits.open(f) # print ('---hdu.data---') # print (hdu[0].data) w=WCS(f) # print ('-----w-----') # print(w) # get the wavelengths of the pixels npix = hdu[0].data.shape[2] # print('-----npix-----') # print(npix) lam = w.all_pix2world(np.linspace(0, npix - 1, npix), 0, 0, 0)[0] # print('-----lam-----') # print(lam) # interpolate each spectrum onto a comman wavelength scale specs[i] = interp(lamgrid, lam, hdu[0].data[0][ap], left=0.0, right=0.0) # Also calculate the errors. Right now we assume that the variances # interpolate linearly. This is not stricly correct but it should be # close. Also we don't include terms in the variance for the # uncertainty in the wavelength solution. specerrs[i] = interp(lamgrid, lam, hdu[0].data[3][ap] ** 2.0) ** 0.5 #print ('-----specs-----') #print (specs) # minimize the chi^2 given free parameters are multiplicative factors # We could use linear or quadratic, but for now assume constant p0 = np.ones(nfs) results = optimize.minimize(combine_spec_chi2, p0, args=(lamgrid, specs, specerrs), method='Nelder-Mead', options={'maxfev': 1e5, 'maxiter': 1e5}) # write the best fit parameters into the headers of the files # Dump the list of spectra into a string that iraf can handle iraf_filelist = str(fs).replace('[', '').replace(']', '').replace("'", '') # write the best fit results into a file lines = [] for p in results['x']: lines.append('%f\n' % (1.0 / p)) f = open('flx/scales.dat', 'w') f.writelines(lines) f.close() # run scombine after multiplying the spectra by the best fit parameters combfile = 'sci_com.fits' if os.path.exists(combfile): os.remove(combfile) iraf.scombine(iraf_filelist, combfile, scale='@flx/scales.dat', reject='avsigclip', lthreshold=-2e-16) # Remove the other apertures [TBD] # remove the sky and arc bands from the combined spectra. (or add back?? TBD) # remove some header keywords that don't make sense in the combined file delkws = ['GR-ANGLE','FILTER','BANDID2','BANDID3','BANDID4'] for kw in delkws: pyfits.delval(combfile,kw) # combine JD (average), AIRMASS (average), EXPTIME (sum) # we assume there is a c1.fits file for each image c1fs = [f for f in fs if 'c1.fits' in f] avgjd = np.mean([pyfits.getval(f,'JD') for f in c1fs]) pyfits.setval(combfile,'JD',value=avgjd, comment='average of multiple exposures') print "average JD = " + str(avgjd) sumet = np.sum([pyfits.getval(f,'EXPTIME') for f in c1fs]) pyfits.setval(combfile,'EXPTIME',value=sumet,comment='sum of multiple exposures') print "total EXPTIME = " + str(sumet) avgam = np.mean([pyfits.getval(f,'AIRMASS') for f in c1fs]) pyfits.setval(combfile,'AIRMASS',value=avgam,comment='average of multiple exposures') print "avg AIRMASS = " + str(avgam) # update this to used avg jd midpoint of all exposures? print "barycentric velocity correction (km/s) = ", iraf.bcvcorr(spectra=combfile,keytime='UTC-OBS',keywhen='mid', obslong="339:11:16.8",obslat="-32:22:46.2",obsalt='1798',obsname='saao', savebcv='yes',savejd='yes',printmode=2) pyfits.setval(combfile,'UTMID',comment='added by RVSAO task BCVCORR') pyfits.setval(combfile,'GJDN',comment='added by RVSAO task BCVCORR') pyfits.setval(combfile,'HJDN',comment='added by RVSAO task BCVCORR') pyfits.setval(combfile,'BCV',comment='added by RVSAO task BCVCORR (km/s)') pyfits.setval(combfile,'HCV',comment='added by RVSAO task BCVCORR (km/s)') iraf.dopcor(input=combfile,output='',redshift=-iraf.bcvcorr.bcv,isvelocity='yes', add='no',dispersion='yes',flux='no',verbose='yes') pyfits.setval(combfile,'DOPCOR01',comment='barycentric velocity correction applied') iraf.cd('..')
w2 = "INDEF",\ apertures = order,\ bands = "*",\ beams = "*",\ format = "multispec",\ renumber = 1,\ offset = 0,\ clobber = 1,\ merge = 0,\ rebin = 1,\ verbose = 1) iraf.dopcor( input = outdir+"temp/"+str(count)+".fits",\ output = outdir+"temp/"+str(count)+".fits",\ redshift = RV_val,\ isvelocity=1,\ add = 1,\ dispersion = 1,\ flux = 0) count = count + 1 order = order + 1 except iraf.IrafError: count = count order = order + 1 print "No order exist" print "Combining spectra" os.chdir(outdir+"temp")
def contAndStack(directory='', fileTag=''): # Function goes through all the files in the passed directory, breaks them # into their component orders, and continuum-normalizes each order. It # then re-combines the continuum-ed orders into a single 1-D spectrum. # It then performs a user-assisted Doppler correction. fluxFiles = sorted(glob.glob(directory+'/*_Flux.fits')) spectraCount = 0 # Load the necessary IRAF packages iraf.onedspec() # We're going to keep track of the files for each object separately. # Note: With the potential for multiple exposures, we may have multiple # images to weight and stack for a given object. fileDict = {} for thisFile in fluxFiles: # Reason #93 as to why IRAF sucks...80 character limit for # file paths and names... if len(thisFile) >80: tempName = directory+'/a_{0}.fits'.format(spectraCount) if len(tempName)>80: # make the user rename their path... print('File path/name too long: {0}'.format(thisFile)) print('Skipping this file.') continue os.rename(thisFile, tempName) thisFile = tempName # Resulting file names will use the self-reported object name # (if any) and an index based on the alphabetical order of # the original ("HI.xxxxx...") filename. Individual CCD files # (ie: HI.xxxx..._[1,2,3]_Flux.fits") will be left separate. objName = SD.GetObjectName(thisFile).strip().replace(' ','_') if objName not in fileDict.keys(): fileDict[objName] = [] # Using a "spectraCount" ensures that if multiple exposures of # the same object exist, they are given unique filenames for the # combining process starName = '{0}{1}_{2:02d}'.format(fileTag, objName, spectraCount) spectraCount += 1 # makee 'linearize' breaks a 2-d spectrum into it's component # orders, one order per file. makeeCall = kMakeeLinearCall + thisFile sub.call(makeeCall, shell=True) orderFiles = sorted(glob.glob(directory+'/*_Flux-??.fits')) for order in orderFiles: orderNo = order[-7:-5] if starName == '': fileRoot = order.split('/')[-1] orderFileName = directory+'/temp_'+fileRoot else: orderFileName = directory+'/temp_'+starName+'_'+orderNo+'.fits' # Continuum the orders try: iraf.continuum(input=order, output=orderFileName, functio='chebyshev', interac='no', order=5, low_rej=3.0, high_re=3.5, niterat=10) except: print('Error in continuum {0}. Skipping.'.format(order)) u.clearFile(order) continue u.clearFile(order) fileDict[objName].append(orderFileName) completedList=['H027771','H027859','H028099','H028205','H028344','H028462',\ 'H028992','Hy_vB59=HD28034','Hy_vB64=HD28099','Hy_vB65=HD28205',\ 'Hy_vB73=HD28344','Hy_vB92=HD28805','Hy_vB93=HD28878','Hy_vB97=HD28992',\ 'vA_294','vB_173','vB_180','vB_183','vB__46','vB__49','vB__64','vB__65',\ 'vB__73','vB__92','vB__93','vB__97','RHy_281','VA294','28344','HD_028344'\ 'HD28394','08-U20187'] for objName in fileDict.keys(): if len(fileDict[objName]) == 0: continue if objName in completedList: print('Skipping {0}'.format(objName)) continue # Create a temporary input file list: fpInput = open('input.dat','w') fpWeight = open('weight.dat','w') print('---- {0} ----'.format(objName)) for fn in fileDict[objName]: fpInput.write(fn+'\n') sn = SD.GetSpectraSN(fn) fpWeight.write('{0:6.1f}\n'.format(sn)) print(' {0:40}: {1:5.1f}'.\ format(fn.split('/')[-1], sn)) fpInput.close() fpWeight.close() contFileName = directory+'/wc_'+objName+'.fits' # Stack all the images/orders for this object. iraf.onedspec.scombine(input='@input.dat', output=contFileName,\ weight='@weight.dat', combine='median', \ lthreshold=0.05, hthreshold=2.0, group="all") # iraf.stsdas.hst_calib.tomultispec(input=contFileName, output='ms_'+contFileName) u.clearFile('input.dat') u.clearFile('weight.dat') for fn in fileDict[objName]: u.clearFile(fn) # For now, the automated Doppler correction is good only for very small # corrections (ie: <5km/s), so use the manual version. dopCorVelocity, dopWLs = DCL.dopCor(contFileName, interactive=True) # print('Doppler correction = {0:2.3f}km/s'.format(dopCorVelocity)) dopCorFilename = directory+'/D'+contFileName.split('/')[-1][1:] iraf.dopcor(input=contFileName, output=dopCorFilename, redshift=dopCorVelocity, isveloc='yes', verbose='yes') u.clearFile(contFileName) return
def run_dopcor(lista): inp = '@' + lista iraf.noao() iraf.onedspec() iraf.dopcor(inp, inp, 'RV', isveloc='yes', apertur='*')
length = len(scidata) - 1 w_zero = np.where(scidata == 0) low_gap = min(w_zero[0] - 1) up_gap = max(w_zero[0] + 1) iraf.scopy( star_folders[i] + "/untar/" + analyse['NAME_S1D'][k] + "[0:" + str(low_gap) + "]", star_folders[i] + "/reduction/" + file_name[k] + "_blue.fits") iraf.scopy( star_folders[i] + "/untar/" + analyse['NAME_S1D'][k] + "[" + str(up_gap) + ":" + str(length) + "]", star_folders[i] + "/reduction/" + file_name[k] + "_red.fits") iraf.dopcor(star_folders[i] + "/reduction/" + file_name[k] + "_blue.fits", star_folders[i] + "/reduction/" + file_name[k] + "_blue_d.fits", analyse['CCF_RVC'][k], isveloc="yes") iraf.dopcor(star_folders[i] + "/reduction/" + file_name[k] + "_red.fits", star_folders[i] + "/reduction/" + file_name[k] + "_red_d.fits", analyse['CCF_RVC'][k], isveloc="yes") iraf.continuum(star_folders[i] + "/reduction/" + file_name[k] + "_blue_d.fits", star_folders[i] + "/reduction/" + file_name[k] + "_blue_dn.fits", order=80, nit=7, low_rej=1.0,
vel_shift = str(vel_shift[0][11]) print "Applying pixel shift of (km/s)" print vel_shift ### If it is STILL not working, use vshift = 0km/s if vel_shift == "INDEF": vel_shift = 0.0 if float(vel_shift) > 30: vel_shift = 0.0 ################### ### Apply shift ### ################### os.system("rm temp.fits") iraf.dopcor( input = "norm_" + im_slice + "_" + file_name,\ output = "temp.fits",\ redshift = vel_shift,\ isvelocity = 1,\ add = 1,\ dispersion = 1,\ flux = 0,\ apertures = "*",\ verbose = 1) os.system("rm norm_" + im_slice + "_" + file_name) os.system("mv temp.fits norm_" + im_slice + "_" + file_name)
def remove_molecule(molecule,spec_name,outspec_name): telluric_region = eval("TELLURIC_REGION_"+molecule) telluric_region_list = string.split(telluric_region,",") for n in range(len(telluric_region_list)): region = telluric_region_list[n][1:] region = string.split(region,"-") temp = [] for i in region: temp.append(float(i)) telluric_region_list[n] = temp ### Copy telluric line spectra to working directory telluric_fits = grating + "_"+molecule+".fits" os.system("cp -f "+program_dir+"telluric_fits/" + telluric_fits + " telluric.fits") os.system("rm temp_norm*") os.system("rm temp_spec*") os.system("cp -f "+spec_name+" temp_spec.fits") iraf.continuum( input = spec_name,\ output = "temp_norm.fits",\ lines = "*",\ bands = "*",\ type = "ratio",\ replace = 0,\ wavescale = 1,\ logscale = 0,\ override = 1,\ listonly = 0,\ logfiles = "logfile",\ interactive = 0,\ sample = "*",\ naverage = 1,\ function = "spline3",\ order = 20,\ low_reject = 2.0,\ high_reject = 5.0,\ niterate = 5,\ grow = 2.0,\ ask = "yes") ################## ### Find shift ### ################## shift = run_fxcor("telluric.fits","temp_norm.fits",telluric_region) os.system("rm telluric.dat") iraf.dopcor( input = "telluric.fits",\ output = "telluric.fits",\ redshift = shift,\ isvelocity = 1,\ add = 1,\ dispersion = 1,\ flux = 0,\ verbose = 0) iraf.wspectext( input = "telluric.fits",\ output = "telluric.dat",\ header = 0) ###################################### ### Iterative telluric subtraction ### ###################################### def iterate(): os.system("rm temp_norm.dat") iraf.wspectext( input = "temp_norm.fits",\ output = "temp_norm.dat",\ header = 0) data_spec = transpose(loadtxt("temp_norm.dat")) wave = arange(min(data_spec[0]),max(data_spec[0]),1) data_flux = interpolate.splrep(data_spec[0],data_spec[1]) data_flux = interpolate.splev(wave,data_flux) telluric_spec = transpose(loadtxt("telluric.dat")) telluric_flux = interpolate.splrep(telluric_spec[0],telluric_spec[1]) telluric_flux = interpolate.splev(wave,telluric_flux) ### Loop through telluric scaling telluric_scaling = [] rms_list = [] maxscale = 1.08 for scale in arange(0.0,maxscale,0.01): scatter_list = [] for region in telluric_region_list: temp_flux = [] temp_tel = [] for i in range(len(wave)): if wave[i] > region[0] and wave[i] < region[1]: temp_flux.append(data_flux[i]) temp_tel.append(telluric_flux[i]) if wave[i] > region[1]: break temp_flux = array(temp_flux) temp_tel = array(temp_tel) scatter = temp_flux / (temp_tel * scale + (1-scale)) # plt.plot(scatter) # plt.show() scatter_list.append(std(scatter)) telluric_scaling.append(scale) rms_list.append(average(scatter_list)) telluric_scaling,rms_list = array(telluric_scaling),array(rms_list) best_scale = find_min(telluric_scaling,rms_list) if best_scale > maxscale: best_scale = maxscale if best_scale < 0: best_scale = 0 print "scaling telluric by",best_scale # plt.plot(telluric_scaling,rms_list) # plt.show() os.system("cp telluric.fits telluric_temp.fits") iraf.sarith( input1 = "telluric_temp.fits",\ op = "*",\ input2 = best_scale,\ output = "telluric_temp.fits",\ w1 = "INDEF",\ w2 = "INDEF",\ apertures = "",\ bands = "",\ beams = "",\ apmodulus = 0,\ reverse = 0,\ ignoreaps = 1,\ format = "multispec",\ renumber = 0,\ offset = 0,\ clobber = 1,\ merge = 0,\ rebin = 0,\ verbose = 0) iraf.sarith( input1 = "telluric_temp.fits",\ op = "+",\ input2 = 1-best_scale,\ output = "telluric_temp.fits",\ w1 = "INDEF",\ w2 = "INDEF",\ apertures = "",\ bands = "",\ beams = "",\ apmodulus = 0,\ reverse = 0,\ ignoreaps = 1,\ format = "multispec",\ renumber = 0,\ offset = 0,\ clobber = 1,\ merge = 0,\ rebin = 0,\ verbose = 0) iraf.sarith( input1 = "temp_spec.fits",\ op = "/",\ input2 = "telluric_temp.fits",\ output = "temp_spec",\ w1 = "INDEF",\ w2 = "INDEF",\ apertures = "",\ bands = "",\ beams = "",\ apmodulus = 0,\ reverse = 0,\ ignoreaps = 1,\ format = "multispec",\ renumber = 0,\ offset = 0,\ clobber = 1,\ merge = 0,\ rebin = 0,\ verbose = 0) iraf.sarith( input1 = "temp_norm.fits",\ op = "/",\ input2 = "telluric_temp.fits",\ output = "temp_norm",\ w1 = "INDEF",\ w2 = "INDEF",\ apertures = "",\ bands = "",\ beams = "",\ apmodulus = 0,\ reverse = 0,\ ignoreaps = 1,\ format = "multispec",\ renumber = 0,\ offset = 0,\ clobber = 1,\ merge = 0,\ rebin = 0,\ verbose = 0) return best_scale best_scaling = 1.0 while best_scaling > 0.05: best_scaling = iterate() os.system("cp temp_spec.fits "+outspec_name)