def main(): """Visually test the addition of Noise using add_noise function.""" org_flux = np.ones(500) for i, snr in enumerate([50, 100, 200, 300, 50000]): # Test that the standard deviation of the noise is close to the snr level print("Applying a snr of {}".format(snr)) noisey_flux = add_noise(org_flux, snr) spec = Spectrum( flux=copy.copy(org_flux)) # Copy becasue org_flux is mutable. spec.add_noise(snr) std = np.std(noisey_flux) print("Standard deviation of signal = {}".format(std)) snr_est = 1 / std snr_spectrum = 1. / np.std(spec.flux) print( "Estimated SNR from stddev = {}".format(snr_est)) print("Estimated SNR from stddev of Spectrum class = {}".format( snr_spectrum)) plt.plot(noisey_flux + 0.1 * i, label="snr={}".format(snr)) plt.plot(spec.flux + 0.1 * i, "--", label="Spectrum snr={}".format(snr)) # Calculate chisqr from one chi2 = np.sum((noisey_flux - 1)**2 / 1**2) print("Chisqr for SNR {} = \t\t{}".format(snr, chi2)) chi2_spectrum = np.sum((spec.flux - 1)**2 / 1**2) print("Chisqr for snr_spectrum {} = \t\t{}".format(snr, chi2_spectrum)) plt.legend() plt.show()
def load_spectrum(name, corrected=True): """Load in fits file and return as a Spectrum object. Parameters ---------- name: str Filename of spectrum. corrected: bool Use telluric corrected spectra. Default = True. Returns ------- spectrum: Spectrum Spectra loaded into a Spectrum object. """ data = fits.getdata(name) hdr = fits.getheader(name) # Turn into Spectrum # Check for telluric corrected column if corrected: spectrum = Spectrum(xaxis=data["wavelength"], flux=data["Corrected_DRACS"], header=hdr) else: spectrum = Spectrum(xaxis=data["wavelength"], flux=data["Extracted_DRACS"], header=hdr) return spectrum
def fake_obs(simnum, snr=200, suffix=None, plot=False, mode="iam"): snr = 200 params1 = [5300, 4.5, 0.0] params2 = [2500, 4.5, 0.0] gamma = 5 rv = -3 normalization_limits = [2100, 2180] mod1_spec = load_starfish_spectrum(params1, limits=normalization_limits, hdr=True, normalize=False, area_scale=True, flux_rescale=True) mod2_spec = load_starfish_spectrum(params2, limits=normalization_limits, hdr=True, normalize=False, area_scale=True, flux_rescale=True) if broadcast: broadcast_result = inherent_alpha_model(mod1_spec.xaxis, mod1_spec.flux, mod2_spec.flux, rvs=rv, gammas=gamma) broadcast_values = broadcast_result(obs_spec.xaxis) result_spectrum = Spectrum(flux=broadcast_values, xaxis=mod1_spec.xaxis) else: # Manually redo the join mod2_spec.doppler_shift(rv) mod_combine = mod1_spec.copy() mod_combine += mod2_spec mod_combine.doppler_shift(gamma) mod_combine.normalize(method="exponential") mod_combine.interpolate(obs_spec.xaxis) result_spectrum = mod_combine # result_spectrum = Spectrum(flux=broadcast_values, xaxis=obs_spec.xaxis) result_spectrum.add_noise(snr) # Save as # Detector limits dect_limits = [(2112, 2123), (2127, 2137), (2141, 2151), (2155, 2165)] for ii, dect in enumerate(dect_limits): spec = result_spectrum.copy() spec.wav_select(*dect) spec.resample(1024) name = "HDsim-{0}_{1}_snr_{2}".format(sim_num, ii, snr)
def join_with_manual_doppler(mod1, mod2, rv, gamma): mod2 = mod2.copy() x1, y1 = doppler(mod2.xaxis, mod2.flux, rv) combine = mod1.copy() combine += Spectrum(xaxis=x1, flux=y1) x2, y2 = doppler(combine.xaxis, combine.flux, gamma) return Spectrum(xaxis=x2, flux=y2)
def test_continuum_alpha(chip): x = np.linspace(2100, 2180, 100) model1 = Spectrum(xaxis=x, flux=np.ones(len(x))) model2 = model1 * 2 alpha = continuum_alpha(model1, model2, chip) assert np.allclose(alpha, [2])
def join_with_broadcast_spectrum(mod1, mod2, rv, gamma, new_x): broadcast_result = inherent_alpha_model(mod1.xaxis, mod1.flux, mod2.flux, rvs=rv, gammas=gamma) broadcast_values = broadcast_result(new_x) return Spectrum(flux=broadcast_values.squeeze(), xaxis=new_x)
def load_model_spec(pathwave, specpath, limits=None, normalize=False): """Load model spec from given path to file and wavefile.""" w_mod = fits.getdata(pathwave) w_mod /= 10 # turn into nm flux = fits.getdata(specpath) hdr = fits.getheader(specpath) spec = Spectrum(xaxis=w_mod, flux=flux, header=hdr) logging.debug(pv("spec.xaxis")) if limits is not None: """Apply wavelength limits with slicing.""" spec.wav_select(*limits) if normalize: """Apply normalization to loaded spectrum.""" if limits is None: print("Warning! Limits should be given when using normalization") print("specturm for normalize", spec) spec = spec_local_norm(spec) return spec
def __post_init__(self): self.wavelength = np.asarray(self.wavelength) self.flux = np.asarray(self.flux) val_spectroscopy.check_input(self.wavelength, self.flux, self.unit) if self.unit != Wavelength.AA: print('Converting the wavelength to Angstrom') self.wavelength = self.wavelength * self.unit.value['conversion'] if self.unit == Wavelength.ICM: self.wavelength = self.wavelength[::-1] self.flux = self.flux[::-1] self.unit = Wavelength.AA self.spectrum: Spectrum = Spectrum(self.flux, self.wavelength)
def barycorr_crires_spectrum(spectrum, extra_offset=None): """Wrapper to apply barycorr for CRIRES spectra if given a Spectrum object.""" if spectrum.header.get("BARYDONE", False): warnings.warn("Spectrum already berv corrected") if (extra_offset is not None) or (extra_offset != 0): warnings.warn("Only applying the extra offset.") _, nflux = barycorr_crires(spectrum.xaxis, spectrum.flux, {}, extra_offset=extra_offset) else: warnings.warn("Not changing spectrum.") return spectrum else: _, nflux = barycorr_crires(spectrum.xaxis, spectrum.flux, spectrum.header, extra_offset=extra_offset) new_spectrum = Spectrum(flux=nflux, xaxis=spectrum.xaxis, header=spectrum.header) new_spectrum.header["BARYDONE"] = True return new_spectrum
def save_fake_observation(spectrum: Spectrum, star: str, sim_num: int, params1: str, params2: Optional[str] = None, gamma: Optional[int] = None, rv: Optional[int] = None, noise: None = None, replace: bool = False, noplots: bool = False) -> None: # Detector limits detector_limits = [(2112, 2123), (2127, 2137), (2141, 2151), (2155, 2165)] npix = 1024 header = fits.Header.fromkeys({}) for ii, detector in enumerate(detector_limits): spec = spectrum.copy() spec.wav_select(*detector) spec.interpolate1d_to(np.linspace(spec.xaxis[0], spec.xaxis[-1], npix)) if not noplots: plt.plot(spec.xaxis, spec.flux) plt.title("Fake spectrum {0} {1} detector {2}".format( star, sim_num, ii + 1)) plt.show() name = obs_name_template().format(star, sim_num, ii + 1) # name = "{0}-{1}-mixavg-tellcorr_{2}.fits".format(star, sim_num, ii + 1) name = os.path.join(simulators.paths["spectra"], name) # spec.save... hdrkeys = [ "OBJECT", "Id_sim", "num", "chip", "snr", "c_gamma", "cor_rv", "host", "compan" ] hdrvals = [ star, "Fake simulation data", sim_num, ii + 1, noise, gamma, rv, params1, params2 ] if os.path.exists(name) and not replace: print(name, "Already exists") else: if os.path.exists(name): print("Replacing {0}".format(name)) os.remove(name) export_fits(name, spec.xaxis, spec.flux, header, hdrkeys, hdrvals) print("Saved fits to {0}".format(name))
def apply_convolution(model_spectrum, R=None, chip_limits=None): """Apply convolution to spectrum object.""" if chip_limits is None: chip_limits = (np.min(model_spectrum.xaxis), np.max(model_spectrum.xaxis)) if R is None: return copy.copy(model_spectrum) else: ip_xaxis, ip_flux = ip_convolution(model_spectrum.xaxis[:], model_spectrum.flux[:], chip_limits, R, fwhm_lim=5.0, plot=False, progbar=True) new_model = Spectrum(xaxis=ip_xaxis, flux=ip_flux, calibrated=model_spectrum.calibrated, header=model_spectrum.header) return new_model
def load_phoenix_spectrum(phoenix_name, limits=None, normalize=False): wav_dir = simulators.starfish_grid["raw_path"] wav_model = fits.getdata( os.path.join(wav_dir, "WAVE_PHOENIX-ACES-AGSS-COND-2011.fits")) wav_model /= 10 # turn into nanometers flux = fits.getdata(phoenix_name) spec = Spectrum(flux=flux, xaxis=wav_model) # Limit to K band spec.wav_select(2070, 2350) if normalize: spec = spec_local_norm(spec, method="exponential") if limits is not None: spec.wav_select(*limits) return spec
def load_spectrum(name): """Load in fits file and return as a Spectrum object. Tries multiple names for flux column from past implementations. Parameters ---------- name: str Filename of spectrum. Returns ------- spectrum: Spectrum Spectra loaded into a Spectrum object. """ data = fits.getdata(name) hdr = fits.getheader(name) # TODO: log lambda sampling. # see starfish # Turn into Spectrum xaxis = data["wavelength"] try: flux = data["flux"] except KeyError: try: flux = data["Corrected_DRACS"] except KeyError: try: flux = data["Extracted_DRACS"] except KeyError: print("The fits columns are {}".format(data.columns)) raise spectrum = Spectrum(xaxis=xaxis, flux=flux, header=hdr) return spectrum
def main(): """Main function.""" star = "HD30501" host_parameters = load_param_file(star) obsnum = 1 chip = 1 obs_name = select_observation(star, obsnum, chip) # Load observation # uncorrected_spectra = load_spectrum(obs_name) observed_spectra = load_spectrum(obs_name) _observed_spectra = barycorr_crires_spectrum(observed_spectra, extra_offset=None) observed_spectra.flux /= 1.02 obs_resolution = crires_resolution(observed_spectra.header) wav_model = fits.getdata( os.path.join(wav_dir, "WAVE_PHOENIX-ACES-AGSS-COND-2011.fits")) wav_model /= 10 # turn into nm logging.debug(__("Phoenix wav_model = {0}", wav_model)) closest_model = phoenix_name_from_params(model_base_dir, host_parameters) original_model = "Z-0.0/lte05200-4.50-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits" logging.debug(__("closest_model {0}", closest_model)) logging.debug(__("original_model {0}", original_model)) # Function to find the good models I need models = find_phoenix_model_names(model_base_dir, original_model) if isinstance(models, list): logging.debug(__("Number of close models returned {0}", len(models))) model_chisqr_vals = np.empty_like(models) model_xcorr_vals = np.empty_like(models) model_xcorr_rv_vals = np.empty_like(models) for ii, model_name in enumerate(models): mod_flux = fits.getdata(model_name) mod_header = fits.getheader(model_name) mod_spectrum = Spectrum(xaxis=wav_model, flux=mod_flux, header=mod_header, calibrated=True) # Normalize Phoenix Spectrum # mod_spectrum.wav_select(2080, 2200) # limits for simple normalization mod_spectrum.wav_select(2105, 2165) # limits for simple normalization # norm_mod_spectrum = simple_normalization(mod_spectrum) norm_mod_spectrum = spec_local_norm(mod_spectrum, plot=False) # Wav select norm_mod_spectrum.wav_select( np.min(observed_spectra.xaxis) - 5, np.max(observed_spectra.xaxis) + 5) # +- 5nm of obs for convolution # Convolve to resolution of instrument conv_mod_spectrum = convolve_models([norm_mod_spectrum], obs_resolution, chip_limits=None)[0] # Find crosscorrelation RV # # Should run though all models and find best rv to apply uniformly rvoffset, cc_max = xcorr_peak(observed_spectra, conv_mod_spectrum, plot=False) # Interpolate to obs conv_mod_spectrum.spline_interpolate_to(observed_spectra) # conv_mod_spectrum.interpolate1d_to(observed_spectra) model_chi_val = chi_squared(observed_spectra.flux, conv_mod_spectrum.flux) # argmax = np.argmax(cc_max) model_chisqr_vals[ii] = model_chi_val model_xcorr_vals[ii] = cc_max model_xcorr_rv_vals[ii] = rvoffset logging.debug(pv("model_chisqr_vals")) logging.debug(pv("model_xcorr_vals")) chisqr_argmin_indx = np.argmin(model_chisqr_vals) xcorr_argmax_indx = np.argmax(model_xcorr_vals) logging.debug(pv("chisqr_argmin_indx")) logging.debug(pv("xcorr_argmax_indx")) logging.debug(pv("model_chisqr_vals")) print("Minimum Chisqr value =", model_chisqr_vals[chisqr_argmin_indx]) # , min(model_chisqr_vals) print("Chisqr at max correlation value", model_chisqr_vals[chisqr_argmin_indx]) print("model_xcorr_vals = {}".format(model_xcorr_vals)) print("Maximum Xcorr value =", model_xcorr_vals[xcorr_argmax_indx]) # , max(model_xcorr_vals) print("Xcorr at min Chiqsr", model_xcorr_vals[chisqr_argmin_indx]) logging.debug(pv("model_xcorr_rv_vals")) print("RV at max xcorr =", model_xcorr_rv_vals[xcorr_argmax_indx]) # print("Median RV val =", np.median(model_xcorr_rv_vals)) print(pv("model_xcorr_rv_vals[chisqr_argmin_indx]")) # print(pv("sp.stats.mode(np.around(model_xcorr_rv_vals))")) print("Max Correlation model = ", models[xcorr_argmax_indx].split("/")[-2:]) print("Min Chisqr model = ", models[chisqr_argmin_indx].split("/")[-2:]) limits = [2110, 2160] best_model = models[chisqr_argmin_indx] best_model_spec = load_phoenix_spectrum(best_model, limits=limits, normalize=True) best_model_spec = convolve_models([best_model_spec], obs_resolution, chip_limits=None)[0] best_xcorr_model = models[xcorr_argmax_indx] best_xcorr_model_spec = load_phoenix_spectrum(best_xcorr_model, limits=limits, normalize=True) best_xcorr_model_spec = convolve_models([best_xcorr_model_spec], obs_resolution, chip_limits=None)[0] close_model_spec = load_phoenix_spectrum(closest_model[0], limits=limits, normalize=True) close_model_spec = convolve_models([close_model_spec], obs_resolution, chip_limits=None)[0] plt.plot(observed_spectra.xaxis, observed_spectra.flux, label="Observations") plt.plot(best_model_spec.xaxis, best_model_spec.flux, label="Best Model") plt.plot(best_xcorr_model_spec.xaxis, best_xcorr_model_spec.flux, label="Best xcorr Model") plt.plot(close_model_spec.xaxis, close_model_spec.flux, label="Close Model") plt.legend() plt.xlim(*limits) plt.show() print("After plot")
def main(star, obsnum, teff_1, logg_1, feh_1, teff_2, logg_2, feh_2, gamma, rv, plot_name=None): fig, axis = plt.subplots(2, 2, figsize=(15, 8), squeeze=False) for chip, ax in zip(range(1, 5), axis.flatten()): # Get observation data obs_name, params, output_prefix = iam_helper_function( star, obsnum, chip) # Load observed spectrum obs_spec = load_spectrum(obs_name) # Mask out bad portion of observed spectra obs_spec = spectrum_masking(obs_spec, star, obsnum, chip) # Barycentric correct spectrum _obs_spec = barycorr_crires_spectrum(obs_spec, extra_offset=None) # Determine Spectrum Errors # error_off = False # errors = spectrum_error(star, obsnum, chip, error_off=error_off) # Create model with given parameters host = load_starfish_spectrum([teff_1, logg_1, feh_1], limits=[2110, 2165], area_scale=True, hdr=True) if teff_2 is None: assert (logg_2 is None) and (feh_2 is None) and ( rv == 0), "All must be None for bhm case." companion = Spectrum(xaxis=host.xaxis, flux=np.zeros_like(host.flux)) else: companion = load_starfish_spectrum([teff_2, logg_2, feh_2], limits=[2110, 2165], area_scale=True, hdr=True) joint_model = inherent_alpha_model(host.xaxis, host.flux, companion.flux, gammas=gamma, rvs=rv) model_spec = Spectrum(xaxis=host.xaxis, flux=joint_model(host.xaxis).squeeze()) model_spec = model_spec.remove_nans() model_spec = model_spec.normalize("exponential") # plot obs_spec.plot(axis=ax, label="{}-{}".format(star, obsnum)) model_spec.plot(axis=ax, linestyle="--", label="Chi-squared model") ax.set_xlim([obs_spec.xmin() - 0.5, obs_spec.xmax() + 0.5]) ax.set_title("{} obs {} chip {}".format(star, obsnum, chip)) ax.legend() plt.tight_layout() if plot_name is None: fig.show() else: if not (plot_name.endswith(".png") or plot_name.endswith(".pdf")): raise ValueError("plot_name does not end with .pdf or .png") fig.savefig(plot_name) return 0
obs_resolution = crires_resolution(observed_spectra.header) wav_model = fits.getdata(model_base_dir + "WAVE_PHOENIX-ACES-AGSS-COND-2011.fits") wav_model /= 10 # turn into nm temp_store = [] rv_store = [] cc_store = [] chi2_store = [] for model in tqdm(phoenix_names): temp_store.append(int(model.split("/")[-1][3:8])) phoenix_data = fits.getdata(model) phoenix_spectrum = Spectrum(flux=phoenix_data, xaxis=wav_model, calibrated=True) phoenix_spectrum.wav_select(*wl_limits) phoenix_spectrum = spec_local_norm(phoenix_spectrum) phoenix_spectrum = apply_convolution(phoenix_spectrum, R=obs_resolution, chip_limits=wl_limits) rv, cc = observed_spectra.crosscorr_rv(phoenix_spectrum, rvmin=-100., rvmax=100.0, drv=0.1, mode='doppler', skipedge=50) maxind = np.argmax(cc)
bd_model = "/home/jneal/Phd/data/phoenixmodels/" \ "HD30501b-lte02500-5.00-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits" star_model = "/home/jneal/Phd/data/phoenixmodels/" \ "HD30501-lte05200-4.50-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits" i_bdmod = fits.getdata(bd_model) i_star = fits.getdata(star_model) hdr_bd = fits.getheader(bd_model) hdr_star = fits.getheader(star_model) w_mod = fits.getdata(pathwave) w_mod /= 10 # turn into nm test_rv = -15.205 # km/s template_spectrum = Spectrum(xaxis=w_mod, flux=i_star, calibrated=True) template_spectrum.wav_select(2100, 2200) obs_spectrum = Spectrum(xaxis=w_mod, flux=i_star, calibrated=True) obs_spectrum.doppler_shift(test_rv) obs_spectrum.wav_select(2100, 2200) print(len(obs_spectrum.xaxis)) rv, cc = pyasl.crosscorrRV(obs_spectrum.xaxis, obs_spectrum.flux, template_spectrum.xaxis, template_spectrum.flux, rvmin=-60., rvmax=60.0, drv=0.1,
def main(star, sim_num, params1=None, params2=None, gamma=None, rv=None, noise=None, test=False, replace=False, noplots=False, mode="iam", fudge=None, area_scale=True): star = star.upper() if gamma is None: gamma = 0 if rv is None: rv = 0 if params1 is not None: params_1 = [float(par) for par in params1.split(",")] else: raise ValueError("No host parameter given. Use '-p'") if mode == "iam": if params2 is not None: params_2 = [float(par) for par in params2.split(",")] else: raise ValueError( "No companion parameter given. Use '-q', or set '--mode bhm'") if test: testing_noise(star, sim_num, params_1, params_2, gamma, rv) testing_fake_spectrum(star, sim_num, params_1, params_2, gamma, rv, noise=None) else: x_wav, y_wav, header = fake_iam_simulation(None, params_1, params_2, gamma=gamma, rv=rv, noise=noise, header=True, fudge=fudge, area_scale=area_scale) fake_spec = Spectrum(xaxis=x_wav, flux=y_wav, header=header) # save to file save_fake_observation(fake_spec, star, sim_num, params1, params2=params2, gamma=gamma, rv=rv, noise=None, replace=replace, noplots=noplots) elif mode == "bhm": # Do a bhm simulation x_wav, y_wav, header = fake_bhm_simulation(None, params_1, gamma, noise=noise, header=True) fake_spec = Spectrum(xaxis=x_wav, flux=y_wav, header=header) # save to file save_fake_observation(fake_spec, star, sim_num, params1, gamma=gamma, noise=None, replace=replace, noplots=noplots) return None
host_temp = 5300 comp_temp = 2300 # Load in some phoenix data and make a simulation base = simulators.starfish_grid["raw_path"] phoenix_wl = fits.getdata( os.path.join(base, "WAVE_PHOENIX-ACES-AGSS-COND-2011.fits")) / 10 host_phoenix = os.path.join( base, ("Z-0.0/lte{:05d}-4.50-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits" ).format(host_temp)) comp_phoenix = os.path.join( base, ("Z-0.0/lte{:05d}-4.50-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits" ).format(comp_temp)) unnorm_host_spec = Spectrum(flux=fits.getdata(host_phoenix), xaxis=phoenix_wl) unnorm_comp_spec = Spectrum(flux=fits.getdata(comp_phoenix), xaxis=phoenix_wl) min_wav = 2050 max_wav = 2250 unnorm_host_spec.wav_select(min_wav, max_wav) unnorm_comp_spec.wav_select(min_wav, max_wav) # local normalization norm_host_flux = local_normalization(unnorm_host_spec.xaxis, unnorm_host_spec.flux, method="exponential", plot=False) norm_comp_flux = local_normalization(unnorm_comp_spec.xaxis, unnorm_comp_spec.flux,
# In[3]: # Manually load Phoenix spectra #phoenix_path = simulators... phoenix_path = "/home/jneal/Phd/data/PHOENIX-ALL/PHOENIX/" phoenix_1 = "Z-0.0/lte06000-4.50-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits" phoenix_2 = "Z-0.0/lte05800-4.50-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits" wav = fits.getdata(phoenix_path + "WAVE_PHOENIX-ACES-AGSS-COND-2011.fits") phoenix1 = fits.getdata(phoenix_path + phoenix_1) phoenix2 = fits.getdata(phoenix_path + phoenix_2) spec1 = Spectrum(xaxis=wav/10, flux=phoenix1) spec2 = Spectrum(xaxis=wav/10, flux=phoenix2) spec1.wav_select(2000,2200) spec2.wav_select(2000,2200) spec1.plot(label="1") spec2.plot(label="2") plt.legend() plt.show() # In[4]: from Convolution.IP_multi_Convolution import convolve_spectrum
# In[ ]: Artucus = [4300, 1.50, -0.5] HD30501 = [5200, 4.5, 0.0] ACES_bottom = [2300, 4.5, 0.0] Sun = [5800, 4.5, 0.0] # In[ ]: # Teff 5800, 4.5, 0.0 # comparison_plot("Sun", *Sun) from spectrum_overload import Spectrum w_next, f_next, bb_next = load_Allard_Phoenix( "data/lte580-4.5-0.0a+0.0.BT-NextGen.7") next_spec = Spectrum(xaxis=w_next, flux=f_next) w_dusty_spec, f_dusty_spec, bb_dusty_spec = load_Allard_Phoenix( "data/lte058-4.5-0.0.BT-Dusty.spec.7") dusty_spec = Spectrum(xaxis=w_dusty_spec, flux=f_dusty_spec) w_settl, f_settl, bb_settl = load_Allard_Phoenix( "data/lte058.0-4.5-0.0a+0.0.BT-Settl.spec.7") settl_spec = Spectrum(xaxis=w_settl, flux=f_settl) w_cond, f_cond, bb_cond = load_Allard_Phoenix( "data/lte580-4.5-0.0a+0.0.BT-Cond.7") cond_spec = Spectrum(xaxis=w_cond, flux=f_cond) w_aces, f_aces = load_phoenix_aces( "data/lte05800-4.50-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits") aces_spec = Spectrum(xaxis=w_aces, flux=f_aces) w_dusty_fits, f_dusty_fits, bb_dusty_fits = load_Allard_Phoenix( "data/lte5800-4.50-0.0a+0.0.BT-dusty-giant-2013.cf128.sc.spid.fits") w_dusty_fits = w_dusty_fits * 1000
def compare_spectra(table, params): """Plot the min chi2 result against the observations.""" extractor = DBExtractor(table) gamma_df = extractor.simple_extraction(columns=["gamma"]) extreme_gammas = [min(gamma_df.gamma.values), max(gamma_df.gamma.values)] for ii, chi2_val in enumerate(chi2_names[0:-2]): df = extractor.ordered_extraction( columns=["teff_1", "logg_1", "feh_1", "gamma", chi2_val], order_by=chi2_val, limit=1, asc=True) params1 = [ df["teff_1"].values[0], df["logg_1"].values[0], df["feh_1"].values[0] ] params1 = [float(param1) for param1 in params1] gamma = df["gamma"].values obs_name, obs_params, output_prefix = bhm_helper_function( params["star"], params["obsnum"], ii + 1) print(obs_name) obs_spec = load_spectrum(obs_name) # Mask out bad portion of observed spectra # obs_spec = spectrum_masking(obs_spec, params["star"], params["obsnum"], ii + 1) # Barycentric correct spectrum _obs_spec = barycorr_crires_spectrum(obs_spec, extra_offset=None) normalization_limits = [obs_spec.xaxis[0] - 5, obs_spec.xaxis[-1] + 5] # models # print("params for models", params1) mod1 = load_starfish_spectrum(params1, limits=normalization_limits, hdr=True, normalize=False, area_scale=True, flux_rescale=True) bhm_grid_func = one_comp_model(mod1.xaxis, mod1.flux, gammas=gamma) bhm_upper_gamma = one_comp_model(mod1.xaxis, mod1.flux, gammas=extreme_gammas[1]) bhm_lower_gamma = one_comp_model(mod1.xaxis, mod1.flux, gammas=extreme_gammas[0]) bhm_grid_model = bhm_grid_func(obs_spec.xaxis).squeeze() bhm_grid_model_full = bhm_grid_func(mod1.xaxis).squeeze() bhm_upper_gamma = bhm_upper_gamma(obs_spec.xaxis).squeeze() bhm_lower_gamma = bhm_lower_gamma(obs_spec.xaxis).squeeze() model_spec_full = Spectrum(flux=bhm_grid_model_full, xaxis=mod1.xaxis) model_spec = Spectrum(flux=bhm_grid_model, xaxis=obs_spec.xaxis) bhm_upper_gamma = Spectrum(flux=bhm_upper_gamma, xaxis=obs_spec.xaxis) bhm_lower_gamma = Spectrum(flux=bhm_lower_gamma, xaxis=obs_spec.xaxis) model_spec = model_spec.remove_nans() model_spec = model_spec.normalize(method="exponential") model_spec_full = model_spec_full.remove_nans() model_spec_full = model_spec_full.normalize(method="exponential") bhm_lower_gamma = bhm_lower_gamma.remove_nans() bhm_lower_gamma = bhm_lower_gamma.normalize(method="exponential") bhm_upper_gamma = bhm_upper_gamma.remove_nans() bhm_upper_gamma = bhm_upper_gamma.normalize(method="exponential") from mingle.utilities.chisqr import chi_squared chisqr = chi_squared(obs_spec.flux, model_spec.flux) print("Recomputed chi^2 = {0}".format(chisqr)) print("Database chi^2 = {0}".format(df[chi2_val])) fig, ax = plt.subplots(1, 1, figsize=(15, 8)) plt.plot(obs_spec.xaxis, obs_spec.flux + 0.01, label="0.05 + Observation, {}".format(obs_name)) plt.plot(model_spec.xaxis, model_spec.flux, label="Minimum \chi^2 model") plt.plot(model_spec_full.xaxis, model_spec_full.flux, "--", label="Model_full_res") plt.plot(bhm_lower_gamma.xaxis, bhm_lower_gamma.flux, "-.", label="gamma={}".format(extreme_gammas[0])) plt.plot(bhm_upper_gamma.xaxis, bhm_upper_gamma.flux, ":", label="gamma={}".format(extreme_gammas[1])) plt.title("bhm spectrum") plt.legend() fig.tight_layout() name = "{0}-{1}_{2}_{3}_bhm_min_chi2_spectrum_comparison_{4}.png".format( params["star"], params["obsnum"], params["chip"], chi2_val, params["suffix"]) plt.savefig(os.path.join(params["path"], "plots", name)) plt.close() plt.plot(obs_spec.xaxis, obs_spec.flux, label="Observation") plt.show()
def load_starfish_spectrum(params, limits=None, hdr=False, normalize=False, area_scale=False, flux_rescale=False, wav_scale=True): """Load spectrum from hdf5 grid file. Parameters ---------- params: list Model parameters [teff, logg, Z] limits= List[float, float] default=None Wavelength limits. hdr: bool Include the model header. Default False. normalize: bool Locally normalize the spectrum. Default False. area_scale: bool Multiply by stellar surface area pi*R**2 (towards Earth) flux_rescale: bool Convert from /cm to /nm by dividing by 1e7 wav_scale: bool Multiply by wavelength to turn into [erg/s/cm^2] Returns ------- spec: Spectrum The loaded spectrum as Spectrum object. """ my_hdf5 = HDF5Interface() my_hdf5.wl = my_hdf5.wl / 10 # Turn into Nanometer if hdr: flux, myhdr = my_hdf5.load_flux_hdr(np.array(params)) spec = Spectrum(flux=flux, xaxis=my_hdf5.wl, header=myhdr) else: flux = my_hdf5.load_flux(np.array(params)) spec = Spectrum(flux=flux, xaxis=my_hdf5.wl) if flux_rescale: spec = spec * 1e-7 # convert flux unit from /cm to /nm if area_scale: if hdr: emitting_area = phoenix_area(spec.header) spec = spec * emitting_area spec.header["emit_area"] = (emitting_area, "pi*r^2") else: raise ValueError("No header provided for stellar area scaling") if wav_scale: # Convert into photon counts, (constants ignored) spec = spec * spec.xaxis if normalize: spec = spec_local_norm(spec, method="exponential") if limits is not None: if limits[0] > spec.xaxis[-1] or limits[-1] < spec.xaxis[0]: logging.warning( "Warning: The wavelength limits do not overlap the spectrum." "There is no spectrum left... Check your wavelength, or limits." ) spec.wav_select(*limits) return spec
raw_flux = fits.getdata(phoenix) wav = fits.getdata(phoenix_wav) plt.plot(wav, raw_flux, label="raw") plt.plot(wl, flux * 5e7, label="starfish") plt.legend() plt.show() params2 = [5200, 4.5, 0.0] flux = myHDF5.load_flux(np.array(params2)) # Load direct phoenix spectra path = simulators.starfish_grid["raw_path"] phoenix = os.path.join( path, "Z-0.0", "lte{:05d}-{:0.2f}-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits".format( params2[0], params2[1])) phoenix_wav = os.path.join(path, "WAVE_PHOENIX-ACES-AGSS-COND-2011.fits") print(phoenix) raw_flux = fits.getdata(phoenix) wav = fits.getdata(phoenix_wav) phoenix_spec = Spectrum(xaxis=wav, flux=raw_flux) phoenix_spec.wav_select(wl[0], wl[-1]) plt.plot(wav, raw_flux, label="raw") plt.plot(phoenix_spec.xaxis, phoenix_spec.flux, "--", label="spec") plt.plot(wl, flux * 4e6, label="starfish") plt.legend() plt.show()
def compare_spectra(table, params): """Plot the min chi2 result against the observations.""" extractor = DBExtractor(table) for ii, chi2_val in enumerate(chi2_names[0:-2]): df = extractor.minimum_value_of(chi2_val) df = df[["teff_1", "logg_1", "feh_1", "gamma", "teff_2", "logg_2", "feh_2", "rv", chi2_val]] params1 = [df["teff_1"].values[0], df["logg_1"].values[0], df["feh_1"].values[0]] params2 = [df["teff_2"].values[0], df["logg_2"].values[0], df["feh_2"].values[0]] params1 = [float(param1) for param1 in params1] params2 = [float(param2) for param2 in params2] gamma = df["gamma"].values rv = df["rv"].values from simulators.iam_module import iam_helper_function obs_name, obs_params, output_prefix = iam_helper_function(params["star"], params["obsnum"], ii + 1) obs_spec = load_spectrum(obs_name) # Mask out bad portion of observed spectra # obs_spec = spectrum_masking(obs_spec, params["star"], params["obsnum"], ii + 1) # Barycentric correct spectrum _obs_spec = barycorr_crires_spectrum(obs_spec, extra_offset=None) normalization_limits = [obs_spec.xaxis[0] - 5, obs_spec.xaxis[-1] + 5] # models # print("params for models", params1, params2) mod1 = load_starfish_spectrum(params1, limits=normalization_limits, hdr=True, normalize=False, area_scale=True, flux_rescale=True) mod2 = load_starfish_spectrum(params2, limits=normalization_limits, hdr=True, normalize=False, area_scale=True, flux_rescale=True) iam_grid_func = inherent_alpha_model(mod1.xaxis, mod1.flux, mod2.flux, rvs=rv, gammas=gamma) iam_grid_model = iam_grid_func(obs_spec.xaxis).squeeze() iam_grid_model_full = iam_grid_func(mod1.xaxis).squeeze() model_spec_full = Spectrum(flux=iam_grid_model_full, xaxis=mod1.xaxis) model_spec = Spectrum(flux=iam_grid_model, xaxis=obs_spec.xaxis) model_spec = model_spec.remove_nans() model_spec = model_spec.normalize(method="exponential") model_spec_full = model_spec_full.remove_nans() model_spec_full = model_spec_full.normalize(method="exponential") fig, ax = plt.subplots(1, 1) plt.plot(obs_spec.xaxis, obs_spec.flux, label="Observation") plt.plot(model_spec.xaxis, model_spec.flux, label="Minimum \chi^2 model") plt.plot(model_spec_full.xaxis, model_spec_full.flux, "--", label="Model_full_res") plt.legend() fig.tight_layout() name = "{0}-{1}_{2}_{3}_min_chi2_spectrum_comparison_{4}.png".format( params["star"], params["obsnum"], params["chip"], chi2_val, params["suffix"]) plt.savefig(os.path.join(params["path"], "plots", name)) plt.close() plt.plot(obs_spec.xaxis, obs_spec.flux, label="Observation") plt.show()
make_dirs(source_path, output_path) # This is completed now. folders = glob.glob(os.path.join(source_path, "*")) phoenix_wave = os.path.join(source_path, "WAVE_PHOENIX-ACES-AGSS-COND-2011.fits") wave = fits.getdata(phoenix_wave) / 10 # Convert to nm for folder in folders: # Now have a specific directory with phoenix fits files. phoenix_files = glob.glob(os.path.join(folder, "*.fits")) for f in phoenix_files: print(f) # Load in file spectrum = fits.getdata(f) # Wavelenght narrow to K band only 2.07, 2.35 micron phoenix_spectrum = Spectrum(flux=spectrum, xaxis=wave) phoenix_spectrum.wav_select(*band_limits) # Convolutions # Convolution in spectrum overload? # phoenix_spectrum.convolution(R, ...) # Save result to fits file in new directory. new_f = f.replace(".fits", "_R{0:d}k.fits".format(int(resolution / 1000))) new_f = new_f.replace(source_path, output_path) print(new_f)
# Load in some phoenix data and make a simulation path = simulators.starfish_grid["raw_path"] phoenix_wl = fits.getdata( os.path.join(path, "WAVE_PHOENIX-ACES-AGSS-COND-2011.fits")) / 10 host_phoenix = os.path.join( path, "Z-0.0", "lte{:05d}-4.50-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits".format( host_temp)) comp_phoenix = os.path.join( path, "Z-0.0", "lte{:05d}-4.50-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits".format( comp_temp)) unnorm_host_spec = Spectrum(flux=fits.getdata(host_phoenix), xaxis=phoenix_wl) unnorm_comp_spec = Spectrum(flux=fits.getdata(comp_phoenix), xaxis=phoenix_wl) # Waveleght limits. The result is sensitive to these limits. min_wav = 500 max_wav = 3500 unnorm_host_spec.wav_select(min_wav, max_wav) unnorm_comp_spec.wav_select(min_wav, max_wav) # Black body from spectrum temp. norm_host_spec = Spectrum(flux=unnorm_host_spec.flux / blackbody_lambda(phoenix_wl * u_nm, host_temp), xaxis=phoenix_wl) norm_comp_spec = Spectrum(flux=unnorm_comp_spec.flux / blackbody_lambda(phoenix_wl * u_nm, host_temp),
from astro_scripts.plot_fits import get_wavelength, ccf_astro, vac2air from loading_phoenix import load_phoenix_aces, load_Allard_Phoenix, align2model get_ipython().run_line_magic('matplotlib', 'inline') # # Synthethic HD30501 # Using the models with Teff 5200, 4.5 logg, -0.0 [Fe/H] # # HD30501 is in vacuum so will leave the models in vacuum also. # In[ ]: w_aces, f_aces = load_phoenix_aces( "data/lte05200-4.50-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits") aces_spec = Spectrum(xaxis=w_aces, flux=f_aces) w_settl, f_settl, bb_settl = load_Allard_Phoenix( "data/lte052.0-4.5-0.0a+0.0.BT-Settl.spec.7") settl_spec = Spectrum(xaxis=w_settl, flux=f_settl) w_dusty, f_dusty, bb_dusty = load_Allard_Phoenix( "data/lte052-4.5-0.0.BT-Dusty.spec.7") dusty_spec = Spectrum(xaxis=w_dusty, flux=f_dusty) w_next, f_next, bb_next = load_Allard_Phoenix( "data/lte052-4.5-0.0a+0.0.BT-NextGen.7") next_spec = Spectrum(xaxis=w_next, flux=f_next) w_cond, f_cond, bb_cond = load_Allard_Phoenix( "data/lte052-4.5-0.0a+0.0.BT-Cond.7")
def test_spec_max_delta_applies_max_delta_on_xaxis(xaxis, rv, gamma): spec = Spectrum(xaxis=xaxis, flux=np.ones(len(xaxis))) assert spec_max_delta(spec, rv, gamma) == max_delta(xaxis, rv, gamma)