if options.verbose: print 'Input sample file: ', options.inputsample print 'Data directory: ', options.datadir print 'Output calibration matrix: ', options.outputcalibmatrix listOfStarSpectra = mcallib.get_spectrafilepaths(options.datadir) twomassid, id, teff, feh = mcallib.loadSampleOfCalibrators(options.inputsample) Tcal = [] FeHcal = [] ewcal = [] for file in listOfStarSpectra: spc = Spectrum(file) objname = spc.object.replace(" ", "").upper() if options.verbose: print "Processing spectrum: ", file, objname if spc.instrument == 'ESPaDOnS' or spc.instrument == 'NARVAL': spc.resampling(0.01, 4000, 10400) for i in range(len(id)): if objname in id[i].upper() or objname in twomassid[i].upper(): spc.equivalentWidths(override=False, verbose=options.verbose) ewcal_tmp = spc.eqwidths if not np.any(np.isnan(ewcal_tmp)): ewcal.append(ewcal_tmp) Tcal.append(teff[i])
print 'Input directory: ', options.inputdir print 'Object name: ', options.object print 'Spectrum type: ', options.spectype print 'Polar option: ', options.polar print 'Telluric correction: ', options.telluric print 'Heliocentric correction: ', options.helio heliumlines = [447.148, 492.193, 501.568, 587.561, 667.815, 706.518] delta_wl = 0.5 filelist = espectrolib.generateList(options.inputdir, options.object) for filepath in filelist : spc = Spectrum(filepath, options.spectype, options.polar, options.telluric, options.helio) rvvalues = [] for line in heliumlines : wl_line = line wl0, wlf = wl_line-delta_wl, wl_line+delta_wl wl,flux,fluxerr = spc.extractChunk(float(wl0), float(wlf)) chunk = SpectrumChunk(wl,flux,fluxerr) chunk.removeBackground(0.1) init_guess = [wl_line,-0.1, 0.3] wlcen, amp, sigma = chunk.fitgaussian(init_guess) rv = (wlcen[0] - wl_line)*constants.c/wl_line rvvalues.append(rv) median_rv = np.median(rvvalues)
print "Error: check usage with App_extract.py -h " sys.exit(1) if options.verbose: print 'Input spectrum: ', options.input print 'Output spectrum file: ', options.output print 'RV sampling for output spectrum in km/s: ', options.rvsampling print 'Spectrum type: ', options.spectype print 'Polar option: ', options.polar print 'Telluric correction: ', options.telluric print 'Heliocentric correction: ', options.helio print 'File with wavelength ranges: ', options.wavemask spc = Spectrum(options.input, options.spectype, options.polar, options.telluric, options.helio, Sort=False) #spc.binningOrderByOrder(rvsampling_kps=float(options.rvsampling), median=False) if options.wavemask: spc.applyMask(options.wavemask) spc.sortByWavelength() if options.output.endswith(".fits"): wlsampling = (float(options.rvsampling) * 1000.0 / constants.c) * (spc.wl[-1] + spc.wl[0]) / 2.0 spc.resampling(wlsampling)
parser.add_option("-t", action="store_true", dest="telluric", help="telluric correction", default=False) parser.add_option("-r", action="store_true", dest="helio", help="heliocentric correction", default=False) try: options,args = parser.parse_args(sys.argv[1:]) except: print "Error: check usage with App_timeseries.py -h "; sys.exit(1); if options.verbose: print 'Input directory: ', options.inputdir print 'Object name: ', options.object print 'Output wavelength range: ', options.wlrange print 'Spectrum type: ', options.spectype print 'Polar option: ', options.polar print 'Telluric correction: ', options.telluric print 'Heliocentric correction: ', options.helio wl0,wlf = espectrolib.wlrange(options.wlrange, spc) filelist = espectrolib.generateList(options.inputdir, options.object) for filepath in filelist : spc = Spectrum(filepath, options.spectype, options.polar, options.telluric, options.helio) wl,flux,fluxerr = spc.extractChunk(float(wl0), float(wlf)) chunk = SpectrumChunk(wl,flux,fluxerr) chunk.removeBackground(2.0) init_guess = [656.270,2500, 0.5] wlcen, amp, sigma = chunk.fitgaussian(init_guess) print spc.getTimeHJDTT(), wlcen[0], amp[0], sigma[0]
except: print "Error: check usage with extract.py -h " sys.exit(1) if options.verbose: print 'Input spectrum: ', options.input print 'Spectrum type: ', options.spectype print 'Polar option: ', options.polar print 'Telluric correction: ', options.telluric print 'Heliocentric correction: ', options.helio spectrallines = [ 384, 389, 397, 403, 410, 427, 434, 439, 448, 454, 465, 469, 471, 486, 492, 502, 517, 541, 587, 656, 668, 707, 724, 728, 824, 845, 846, 850, 854, 860, 866, 875, 886, 901, 923, 955, 1012 ] telluriclines = [762, 765, 942] lines = np.concatenate((spectrallines, telluriclines), axis=0) spc = Spectrum(options.input, options.spectype, options.polar, options.telluric, options.helio) width = 2.0 spc.maskdata(lines, width) continuum_wlsampling = 10.0 spc.binning(continuum_wlsampling, median=True) spc.printdata()
help="verbose", default=False) parser.add_option("-t", action="store_true", dest="telluric", help="telluric correction", default=False) parser.add_option("-r", action="store_true", dest="helio", help="heliocentric correction", default=False) try: options, args = parser.parse_args(sys.argv[1:]) except: print "Error: check usage with App_espectro -h " sys.exit(1) if options.verbose: print 'Input spectrum: ', options.input print 'Output wavelength range: ', options.wlrange print 'Spectrum type: ', options.spectype print 'Polar option: ', options.polar print 'Telluric correction: ', options.telluric print 'Heliocentric correction: ', options.helio spc = Spectrum(options.input, options.spectype, options.polar, options.telluric, options.helio) spc.printdata()
try: options, args = parser.parse_args(sys.argv[1:]) except: print "Error: check usage with App_extract.py -h " sys.exit(1) if options.verbose: print 'Input spectrum: ', options.input print 'Output wavelength range: ', options.wlrange print 'Spectrum type: ', options.spectype print 'Polar option: ', options.polar print 'Telluric correction: ', options.telluric print 'Heliocentric correction: ', options.helio print 'File with wavelength ranges: ', options.wavemask spc = Spectrum(options.input, options.spectype, options.polar, options.telluric, options.helio) if options.wavemask: spc.applyMask(options.wavemask) minorder, maxorder = spc.getMinMaxOders() for o in range(minorder, maxorder): wl, flux, fluxerr = spc.extractOrder(o) print o, wl[0], wl[-1] plt.plot(wl, flux) #wl0,wlf = espectrolib.wlrange(options.wlrange, spc) #wl,flux,fluxerr = spc.extractChunk(wl0,wlf) #plt.plot(wl,flux) #chunk = SpectrumChunk(wl,flux,fluxerr)
parser.add_option("-r", action="store_true", dest="helio", help="heliocentric correction", default=False) try: options,args = parser.parse_args(sys.argv[1:]) except: print "Error: check usage with App_iSpecTest.py -h "; sys.exit(1); if options.verbose: print 'Input spectrum: ', options.input print 'Output wavelength range: ', options.wlrange print 'Spectrum type: ', options.spectype print 'Polar option: ', options.polar print 'Telluric correction: ', options.telluric print 'Heliocentric correction: ', options.helio spc = Spectrum(options.input, options.spectype, options.polar, options.telluric, options.helio) spc.continuumOrderByOrder(binsize = 30, overlap = 10) spc.normalizeByContinuum(constant=10.0) spc.binningOrderByOrder(rvsampling_kps=2.5, median=False) spc.sortByWavelength() ispectrum = ispec.create_spectrum_structure(spc.wl,spc.flux) resolution = 80000 smoothed_star_spectrum = ispec.convolve_spectrum(ispectrum, resolution) mask_file = ispec_dir + "input/linelists/CCF/Narval.Sun.370_1048nm/mask.lst" ccf_mask = ispec.read_cross_correlation_mask(mask_file) models, ccf = ispec.cross_correlate_with_mask(smoothed_star_spectrum, ccf_mask, \ lower_velocity_limit=-50, upper_velocity_limit=50, \ velocity_step=0.05, mask_depth=0.01, \
print 'File with wavelength ranges: ', options.wavemask print 'Spectrum type: ', options.spectype print 'Polar option: ', options.polar print 'Telluric correction: ', options.telluric print 'Heliocentric correction: ', options.helio useMedian = True # method to bin data (if False -> use Mean) outputRVSampling = 10.0 # in km/s filelist, rvfilelist = espectrolib.generateListWithRVs(options.inputdir, options.object) sourceRVs = espectrolib.loadSourceRVs(rvfilelist) stackspc = Spectrum(filelist[0], "raw", options.polar, options.telluric, options.helio, SourceRV=sourceRVs[0] / 1000.0) stackspc.setAllFluxesToZero() spc = [] for i in range(len(filelist)): print filelist[i] spc.append( Spectrum(filelist[i], options.spectype, options.polar, options.telluric, options.helio,
if options.verbose: print 'Input spectrum: ', options.input lines = [ 402.619, 412.082, 447.148, 471.315, 492.193, 501.568, 587.562, 667.815, 706.519 ] #lines = [447.148, 471.315, 492.193, 501.568, 587.562, 667.815, 706.519] delta_rv_kps = 400 gaussian = lambda x, a, c, w: a * np.exp(-((x - c) / w)**2) gaussfunc = np.vectorize(gaussian) spc = Spectrum(options.input, Sort=False) shift = 0.0 dshift = 0.015 for line in lines: wl_line = line delta_wl = 1000.0 * delta_rv_kps * wl_line / constants.c wl0, wlf = wl_line - delta_wl, wl_line + delta_wl wl, flux, fluxerr = spc.extractChunk(float(wl0), float(wlf)) chunk = SpectrumChunk(wl, flux, fluxerr) chunk.removeBackground(0.1) init_guess = [wl_line, -0.1, 0.3]
parser.add_option("-i", "--input", dest="input", help="Input spectrum file",type='string',default="") parser.add_option("-w", "--wlrange", dest="wlrange", help="Output wavelength range (nm)",type='string',default="") parser.add_option("-p", action="store_true", dest="polar", help="polar",default=False) parser.add_option("-v", action="store_true", dest="verbose", help="verbose",default=False) try: options,args = parser.parse_args(sys.argv[1:]) except: print "Error: check usage with espectro.py -h "; sys.exit(1); if options.verbose: print 'Input spectrum: ', options.input print 'Output wavelength range: ', options.wlrange print 'Polar? ', options.polar wl0, wlf = options.wlrange.split() spc = Spectrum(options.input, polar=options.polar) #spc.info() wl,flux = spc.extractChunk(float(wl0), float(wlf)) dl = 2.0 wlranges = [(float(wl0),float(wl0)+dl),(float(wlf)-dl,float(wlf))] wl,flux = espectrolib.removeBackground(wl,flux,wlranges) for i in range(len(wl)) : print wl[i], flux[i] #spc.printdata()
def loadtemplatespectrum(filename, resolution, telluric_linelist, ccf_mask, velocity_step, wavemask): spc = Spectrum(filename) if wavemask : spc.applyMask(wavemask) barycentricTime = spc.barycentricTime() barycentric_vel = barycentric_velocity(spc) spc.continuumOrderByOrder(binsize = 30, overlap = 10) spc.normalizeByContinuum(constant=10.0) spc.binningOrderByOrder(rvsampling_kps=2.4, median=False) spc.sortByWavelength() ispectrum = ispec.create_spectrum_structure(spc.wl,spc.flux) smoothed_spectrum = ispec.convolve_spectrum(ispectrum, resolution) tel_models, tel_ccf = ispec.cross_correlate_with_mask(smoothed_spectrum, telluric_linelist, \ lower_velocity_limit=-10, upper_velocity_limit=10, \ velocity_step=0.1, mask_depth=0.01, \ fourier = False, only_one_peak = True) tel_rv = tel_models[0].mu() min_vel = -30.0 max_vel = +30.0 # Only the 25% of the deepest ones: dfilter = telluric_linelist['depth'] > np.percentile(telluric_linelist['depth'], 75) tfilter = ispec.create_filter_for_regions_affected_by_tellurics(smoothed_spectrum['waveobs'], \ telluric_linelist[dfilter], min_velocity=-tel_rv+min_vel, \ max_velocity=-tel_rv+max_vel) clean_spectrum = smoothed_spectrum[~tfilter] corrected_spectrum = ispec.correct_velocity(clean_spectrum, tel_rv - barycentric_vel) models, ccf = ispec.cross_correlate_with_mask(clean_spectrum, ccf_mask, \ lower_velocity_limit=-100, upper_velocity_limit=100, \ velocity_step=velocity_step, mask_depth=0.01, \ fourier=False) rv = models[0].mu() clean_corrected_spectrum = ispec.correct_velocity(corrected_spectrum, rv) return clean_corrected_spectrum
#templatefile = "/Users/edermartioli/Desktop/51Peg/testdata/1603681o.m.fits.gz" templatefile = "/Users/edermartioli/Desktop/51Peg/testdata/1604052o.m.fits.gz" if options.template : templatefile = options.template templatespectrum = loadtemplatespectrum(templatefile, resolution, telluric_linelist, ccf_mask, velocity_step, options.wavemask) #--------------- output = [] for filepath in filelist : head, filename = os.path.split(filepath) if options.verbose: print "Loading spectrum file: ", filename spc = Spectrum(filepath, options.spectype) if options.wavemask : spc.applyMask(options.wavemask) snrmask = np.where(spc.fluxerr > 1.0) snr = np.mean(spc.flux[snrmask] / spc.fluxerr[snrmask]) if options.verbose: print "Mean SNR: ",snr barycentricTime = spc.barycentricTime() if options.verbose: print "Barycentric Time: ",barycentricTime barycentric_vel = barycentric_velocity(spc) if options.verbose: print "Barycentric velocity correction: ",barycentric_vel if options.spectype != "norm" :
dest="helio", help="heliocentric correction", default=False) try: options, args = parser.parse_args(sys.argv[1:]) except: print "Error: check usage with App_lsd.py -h " sys.exit(1) if options.verbose: print 'Input spectrum: ', options.input print 'Output spectrum file: ', options.output print 'Lines mask: ', options.linesmask print 'File with wavelength ranges: ', options.wavemask print 'Spectrum type: ', options.spectype print 'RV sampling for output spectrum in km/s: ', options.rvsampling print 'Plot output spectrum: ', options.plot print 'Telluric correction: ', options.telluric print 'Heliocentric correction: ', options.helio if options.verbose: print "Loading spectrum" spc = Spectrum(options.input, options.spectype, False, options.telluric, options.helio, Sort=False)
parser.add_option("-v", action="store_true", dest="verbose", help="verbose", default=False) parser.add_option("-t", action="store_true", dest="telluric", help="telluric correction", default=False) parser.add_option("-r", action="store_true", dest="helio", help="heliocentric correction", default=False) try: options,args = parser.parse_args(sys.argv[1:]) except: print "Error: check usage with App_extract.py -h "; sys.exit(1); if options.verbose: print 'Input spectrum: ', options.input print 'Output wavelength range: ', options.wlrange print 'Spectrum type: ', options.spectype print 'Print header: ', options.header print 'Polar option: ', options.polar print 'Telluric correction: ', options.telluric print 'Heliocentric correction: ', options.helio spc = Spectrum(options.input, options.spectype, options.polar, options.telluric, options.helio) if options.header : spc.info() print "# wavelength(nm) flux flux_err" wl0,wlf = espectrolib.wlrange(options.wlrange, spc) wl,flux,fluxerr = spc.extractChunk(wl0,wlf) chunk = SpectrumChunk(wl,flux,fluxerr) chunk.printdata()
parser.add_option("-r", action="store_true", dest="helio", help="heliocentric correction", default=False) try: options,args = parser.parse_args(sys.argv[1:]) except: print "Error: check usage with App_normalizeFcal.py -h "; sys.exit(1); if options.verbose: print 'Input spectrum: ', options.input print 'Output spectrum file: ', options.output print 'Output spectrum file: ', options.outputcontinuum print 'RV sampling for output spectrum in km/s: ', options.rvsampling print 'Polar option: ', options.polar print 'Telluric correction: ', options.telluric print 'Heliocentric correction: ', options.helio spc = Spectrum(options.input, 'fcal', options.polar, options.telluric, options.helio) spc.applyMask() spc.binning(float(options.rvsampling), median=True) spc.continuum(binsize=200, overlap=100, sigmaclip=3.0, window=3) spc.normalizeByContinuum() spc.saveToFile(options.output) if options.outputcontinuum : spc.saveContinuumToFile(options.outputcontinuum)
if options.verbose: print 'Input spectrum: ', options.input print 'Output spectrum file: ', options.output print 'Lines mask: ', options.linesmask print 'File with wavelength ranges: ', options.wavemask print 'Spectrum type: ', options.spectype print 'RV sampling for output spectrum in km/s: ', options.rvsampling print 'Plot output spectrum: ', options.plot print 'Telluric correction: ', options.telluric print 'Heliocentric correction: ', options.helio if options.verbose: print "Loading spectrum" spc = Spectrum(options.input, options.spectype, False, options.telluric, options.helio, Sort=False) if options.verbose: print "Calculating continuum" spc.continuumOrderByOrder(binsize=30, overlap=10, linesmask=options.linesmask) if options.verbose: print "Normalizing by the continuum" spc.normalizeByContinuum(constant=10.0) if options.verbose: print "Binning data" spc.binningOrderByOrder(rvsampling_kps=float(options.rvsampling), median=False)
try: options,args = parser.parse_args(sys.argv[1:]) except: print "Error: check usage with extract.py -h "; sys.exit(1); if options.verbose: print 'Input spectrum: ', options.input print 'Output wavelength range: ', options.wlrange print 'Spectrum type: ', options.spectype print 'Polar option: ', options.polar print 'Telluric correction: ', options.telluric print 'Heliocentric correction: ', options.helio wl0, wlf = options.wlrange.split() spc = Spectrum(options.input, options.spectype, options.polar, options.telluric, options.helio) #spc.info() wl,flux,fluxerr = spc.extractChunk(float(wl0), float(wlf)) chunk = SpectrumChunk(wl,flux,fluxerr) chunk.snrfilter(15.0) chunk.removeBackground(2.0) #chunk.binning(0.5, median=True) #chunk.fft_filter() init_guess = [656.270,2500, 0.5]
if options.verbose: print 'Data directory: ', options.datadir print 'Calibration matrix: ', options.calibmatrix listOfStarSpectra = mcallib.get_spectrafilepaths(options.datadir) print "--------------------------------------------------" print "File","Object","[Fe/H]_corr","[Fe/H]","e[Fe/H]","Teff_corr","Teff","eTeff","H-alpha", "RV(km/s)" print "--------------------------------------------------" for file in listOfStarSpectra : if options.verbose : print "Processing spectrum: ", file spc = Spectrum(file, FFT=options.fft) if spc.instrument == 'ESPaDOnS' : spc.resampling(0.01,4000,10400) elif spc.instrument == 'NARVAL' : spc.resampling(0.01,4000,10400) spc.equivalentWidths(override=True,verbose=options.verbose) spc.calculateTeffAndFeH(options.calibmatrix, verbose=options.verbose) spc.halphaActivity(verbose=options.verbose) #spc.info() #spc.printdata()