def find_shift(data_type,data_spectrum,teff,logg,feh,start_lambda,end_lambda): ### Import the template spectrum template_spectrum = "template_" + str(teff) + "_" + str(logg) + "_" + str(feh) + ".dat" if data_type=="flux": template_spectrum = functions.read_ascii(model_path_flux + template_spectrum) if data_type=="norm": template_spectrum = functions.read_ascii(model_path_norm + template_spectrum) template_spectrum = functions.read_table(template_spectrum) template_spectrum = transpose(array(template_spectrum)) if data_type=="flux": template_spectrum =spectype_functions.normalise(template_spectrum,flux_normalise_w1,flux_normalise_w2) ### Chop both spectra data_region = spectype_numerical_functions.chop_spectrum(data_spectrum,start_lambda,end_lambda) template_region = spectype_numerical_functions.chop_spectrum(template_spectrum,start_lambda,end_lambda) ### Conform template spectrum to data spectrum -> same wavelength scale template_region = spectype_numerical_functions.conform_spectrum(data_region,template_region) ### Find shift chisq_shift = [] shift_limit = 20 shift = -1*shift_limit while shift <= shift_limit: data_region_shifted,template_region_shifted = spectype_functions.shift_spectrum(data_region,template_region,shift) chisq_shift.append(spectype_numerical_functions.chisq(data_region_shifted,template_region_shifted)) shift = shift + 1 chisq_min = spectype_functions.find_min(chisq_shift) best_shift = chisq_min - shift_limit return best_shift
def test_template(data_type,data_spectrum,teff,logg,feh,start_lambda,end_lambda,shift): ### Import a template spectrum - test template_spectrum = "template_" + str(teff) + "_" + str(logg) + "_" + str(feh) + ".dat" if data_type=="flux": template_spectrum = functions.read_ascii(model_path_flux + template_spectrum) if data_type=="norm": template_spectrum = functions.read_ascii(model_path_norm + template_spectrum) template_spectrum = functions.read_table(template_spectrum) template_spectrum = transpose(array(template_spectrum)) if data_type=="flux": template_spectrum = spectype_functions.normalise(template_spectrum,flux_normalise_w1,flux_normalise_w2) ### Chop both spectra data_region = spectype_numerical_functions.chop_spectrum(data_spectrum,start_lambda,end_lambda) template_region = spectype_numerical_functions.chop_spectrum(template_spectrum,start_lambda,end_lambda) ### Conform template spectrum to data spectrum -> same wavelength scale template_region = spectype_numerical_functions.conform_spectrum(data_region,template_region) ### Find shift data_region_shifted,template_region_shifted = spectype_functions.shift_spectrum(data_region,template_region,shift) chisq = spectype_numerical_functions.chisq(data_region_shifted,template_region_shifted) # plt.clf() # plt.plot(data_region_shifted[0],data_region_shifted[1]) # plt.plot(template_region_shifted[0],template_region_shifted[1]) # plt.title(data_type + " " + str(teff) + " " + str(logg) + " " + str(feh) +" " + str(chisq)) # plt.show() return chisq
def loop_input_spectrum(input_wave,input_flux,folder,teff_space,logg_space,feh_space,w1,w2,perform_normalise): data = [] for teff in teff_space: for logg in logg_space: for feh in feh_space: template_spectrum = "template_" + str(teff) + "_" + str(logg) + "_" + str(feh)+".dat" #print folder + template_spectrum template_spectrum = functions.read_ascii(folder+template_spectrum) template_spectrum = functions.read_table(template_spectrum) template_spectrum = transpose(array(template_spectrum)) if folder == model_path_flux: template_spectrum = spectype_functions.normalise(template_spectrum,flux_normalise_w1,flux_normalise_w2) i1 = w1 - min(input_wave) i2 = w2 - min(input_wave) input_wave_cropped = input_wave[i1:i2] input_flux_cropped = input_flux[i1:i2] template_spectrum = spectype_numerical_functions.chop_spectrum(template_spectrum,w1-10,w2+10) template_interp = interpolate.splrep(template_spectrum[0],template_spectrum[1],s=0) template_flux = interpolate.splev(input_wave_cropped,template_interp,der=0) sigma = 3.0 if perform_normalise: diff_flux = input_flux_cropped/median(input_flux_cropped) - template_flux/median(template_flux) else: diff_flux = input_flux_cropped - template_flux diff_flux = clip(diff_flux,median(diff_flux) - sigma*std(diff_flux),median(diff_flux)+sigma*std(diff_flux)) rms = sqrt(sum(diff_flux**2) /float(len(input_wave_cropped))) # plt.clf() # plt.plot(input_wave_cropped,input_flux_cropped/median(input_flux_cropped)) # plt.plot(input_wave_cropped,template_flux/median(template_flux)) # plt.show() # #sys.exit() #print rms data.append(rms) return data