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 main(): """Main function.""" star = "HD211847" param_file = os.path.join(simulators.paths["parameters"], "{}_params.dat".format(star)) params = parse_paramfile(param_file, path=None) host_params = [params["temp"], params["logg"], params["fe_h"]] # comp_params = [params["comp_temp"], params["logg"], params["fe_h"]] obsnum = 2 chip = 4 obs_name = os.path.join( simulators.paths["spectra"], "{}-{}-mixavg-tellcorr_{}.fits".format(star, obsnum, chip)) logging.info("The observation used is ", obs_name, "\n") closest_host_model = closest_model_params(*host_params) # unpack temp, logg, fe_h with * # closest_comp_model = closest_model_params(*comp_params) # original_model = "Z-0.0/lte05700-4.50-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits" # logging.debug(pv("closest_host_model")) # logging.debug(pv("closest_comp_model")) # logging.debug(pv("original_model")) # Function to find the good models I need # models = find_phoenix_model_names(model_base_dir, original_model) # Function to find the good models I need from parameters # model_par_gen = generate_close_params(closest_host_model) model_pars = list(generate_close_params(closest_host_model)) # Turn to list print("Model parameters", model_pars) # Load observation obs_spec = load_spectrum(obs_name) _obs_spec = barycorr_crires_spectrum(obs_spec, extra_offset=None) obs_spec.flux /= 1.02 # Mask out bad portion of observed spectra ## HACK if chip == 4: # Ignore first 40 pixels obs_spec.wav_select(obs_spec.xaxis[40], obs_spec.xaxis[-1]) import simulators gammas = np.arange(*simulators.sim_grid["gammas"]) #### chi2_grids = bhm_analysis(obs_spec, model_pars, gammas, verbose=True) #### (model_chisqr_vals, model_xcorr_vals, model_xcorr_rv_vals, broadcast_chisqr_vals, broadcast_gamma, broadcast_chisquare) = chi2_grids TEFF = [par[0] for par in model_pars] LOGG = [par[1] for par in model_pars] FEH = [par[2] for par in model_pars] plt.plot(TEFF, broadcast_chisqr_vals, "+", label="broadcast") plt.plot(TEFF, model_chisqr_vals, ".", label="org") plt.title("TEFF vs Broadcast chisqr_vals") plt.legend() plt.show() plt.plot(TEFF, broadcast_gamma, "o") plt.title("TEFF vs Broadcast gamma grid") plt.show() plt.plot(LOGG, broadcast_chisqr_vals, "+", label="broadcast") plt.plot(LOGG, model_chisqr_vals, ".", label="org") plt.title("LOGG verse Broadcast chisqr_vals") plt.legend() plt.show() plt.plot(LOGG, broadcast_gamma, "o") plt.title("LOGG verse Broadcast gamma grid") plt.show() plt.plot(FEH, broadcast_chisqr_vals, "+", label="broadcast") plt.plot(FEH, model_chisqr_vals, ".", label="org") plt.title("FEH vs Broadcast chisqr_vals") plt.legend() plt.show() plt.plot(FEH, broadcast_gamma, "o") plt.title("FEH vs Broadcast gamma grid") plt.show() TEFFS_unique = np.array(set(TEFF)) LOGG_unique = np.array(set(LOGG)) FEH_unique = np.array(set(FEH)) X, Y, Z = np.meshgrid(TEFFS_unique, LOGG_unique, FEH_unique) # set sparse=True for memory efficency print("Teff grid", X) print("Logg grid", Y) print("FEH grid", Z) assert len(TEFF) == sum(len(x) for x in (TEFFS_unique, LOGG_unique, FEH_unique)) chi_ND = np.empty_like(X.shape) print("chi_ND.shape", chi_ND.shape) print("len(TEFFS_unique)", len(TEFFS_unique)) print("len(LOGG_unique)", len(LOGG_unique)) print("len(FEH_unique)", len(FEH_unique)) for i, tf in enumerate(TEFFS_unique): for j, lg in enumerate(LOGG_unique): for k, fh in enumerate(FEH_unique): print("i,j,k", (i, j, k)) print("num = t", np.sum(TEFF == tf)) print("num = lg", np.sum(LOGG == lg)) print("num = fh", np.sum(FEH == fh)) mask = (TEFF == tf) * (LOGG == lg) * (FEH == fh) print("num = tf, lg, fh", np.sum(mask)) chi_ND[i, j, k] = broadcast_chisqr_vals[mask] print("broadcast val", broadcast_chisqr_vals[mask], "\norg val", model_chisqr_vals[mask]) # 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) 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("Meadian 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:]) print("Max Correlation model = ", model_pars[xcorr_argmax_indx]) print("Min Chisqr model = ", model_pars[chisqr_argmin_indx]) limits = [2110, 2160] best_model_params = model_pars[chisqr_argmin_indx] best_model_spec = load_starfish_spectrum(best_model_params, limits=limits, normalize=True) best_xcorr_model_params = model_pars[xcorr_argmax_indx] best_xcorr_model_spec = load_starfish_spectrum(best_xcorr_model_params, limits=limits, normalize=True) close_model_spec = load_starfish_spectrum(closest_model_params, limits=limits, normalize=True) plt.plot(obs_spec.xaxis, obs_spec.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() logging.debug("After plot")
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(): """Main.""" star = "HD211847" obsnum = "2" chip = 1 obs_name, path = select_observation(star, obsnum, chip) # Load observation observed_spectra = load_spectrum(obs_name) if chip == 4: # Ignore first 40 pixels observed_spectra.wav_select(observed_spectra.xaxis[40], observed_spectra.xaxis[-1]) # Load models # host_spectrum_model, companion_spectrum_model = load_PHOENIX_hd30501(limits=[2100, 2200], normalize=True) host_spectrum_model, companion_spectrum_model = load_PHOENIX_hd211847( limits=[2100, 2200], normalize=True) obs_resolution = crires_resolution(observed_spectra.header) # Convolve models to resolution of instrument host_spectrum_model, companion_spectrum_model = convolve_models( (host_spectrum_model, companion_spectrum_model), obs_resolution, chip_limits=None) plot_obs_with_model(observed_spectra, host_spectrum_model, companion_spectrum_model, show=False, title="Before BERV Correction") # Berv Correct # Calculate the star RV relative to synthetic spectum # [mean_val, K1, omega, e, Tau, Period, starmass (Msun), msini(Mjup), i] parameters = { "HD30501": [23.710, 1703.1, 70.4, 0.741, 53851.5, 2073.6, 0.81, 90], "HD211847": [6.689, 291.4, 159.2, 0.685, 62030.1, 7929.4, 0.94, 19.2, 7] } try: host_params = parameters[star] except ValueError: print("Parameters for {} are not in parameters list. Improve this.". format(star)) raise host_params[1] = host_params[1] / 1000 # Convert K! to km/s host_params[2] = np.deg2rad( host_params[2]) # Omega needs to be in radians for ajplanet obs_time = observed_spectra.header["DATE-OBS"] print(obs_time, isinstance(obs_time, str)) print(obs_time.replace("T", " ").split(".")) jd = ephem.julian_date(obs_time.replace("T", " ").split(".")[0]) host_rv = pl_rv_array(jd, *host_params[0:6])[0] print("host_rv", host_rv, "km/s") offset = -host_rv # -22 logging.warning("Non-zero rv offset", pv("offset")) # offset = 0 # -22 _berv_corrected_observed_spectra = barycorr_crires_spectrum( observed_spectra, offset) # Issue with air/vacuum # This introduces nans into the observed spectrum berv_corrected_observed_spectra.wav_select( *berv_corrected_observed_spectra.xaxis[np.isfinite( berv_corrected_observed_spectra.flux)][[0, -1]]) # Shift to star RV plot_obs_with_model(berv_corrected_observed_spectra, host_spectrum_model, companion_spectrum_model, title="After BERV Correction") # print("\nWarning!!!\n BERV is not good have added a offset to get rest working\n") # Chisquared fitting alphas = 10**np.linspace(-4, -0.5, 100) rvs = np.arange(-50, 50, 0.05) # chisqr_store = np.empty((len(alphas), len(rvs))) observed_limits = [ np.floor(berv_corrected_observed_spectra.xaxis[0]), np.ceil(berv_corrected_observed_spectra.xaxis[-1]) ] print("Observed_limits ", observed_limits) n_jobs = 1 if n_jobs is None: n_jobs = mprocess.cpu_count() - 1 start_time = dt.now() if np.all(np.isnan(host_spectrum_model.flux)): print("Host spectrum is all Nans") if np.all(np.isnan(companion_spectrum_model.flux)): print("Companion spectrum is all Nans") print("Now performing the Chisqr grid analaysis") obs_chisqr_parallel = parallel_chisqr( alphas, rvs, berv_corrected_observed_spectra, alpha_model2, (host_spectrum_model, companion_spectrum_model, observed_limits, berv_corrected_observed_spectra), n_jobs=n_jobs) # chisqr_parallel = parallel_chisqr(alphas, rvs, simlulated_obs, alpha_model, (org_star_spec, # org_bd_spec, new_limits), n_jobs=4) end_time = dt.now() print("Time to run parallel chisquared = {}".format(end_time - start_time)) # Plot memmap # plt.subplot(2, 1, 1) x, y = np.meshgrid(rvs, alphas) fig = plt.figure(figsize=(7, 7)) cf = plt.contourf( x, y, np.log10(obs_chisqr_parallel.reshape(len(alphas), len(rvs))), 100) fig.colorbar(cf) plt.title("Sigma chisquared") plt.ylabel("Flux ratio") plt.xlabel("RV (km/s)") plt.show() # Locate minimum and plot resulting model next to observation def find_min_chisquared(x, y, z): """Find minimum vlaue in chisqr grid.""" min_loc = np.argmin(z) print("min location", min_loc) x_sol = x.ravel()[min_loc] y_sol = y.ravel()[min_loc] z_sol = z.ravel()[min_loc] return x_sol, y_sol, z_sol, min_loc rv_solution, alpha_solution, min_chisqr, min_loc = find_min_chisquared( x, y, obs_chisqr_parallel) print("Minium Chisqr value {2}\n RV sol = {0}\nAlpha Sol = {1}".format( rv_solution, alpha_solution, min_chisqr)) solution_model = alpha_model2(alpha_solution, rv_solution, host_spectrum_model, companion_spectrum_model, observed_limits) # alpha_model2(alpha, rv, host, companion, limits, new_x=None): plt.plot(solution_model.xaxis, solution_model.flux, label="Min chisqr solution") plt.plot(berv_corrected_observed_spectra.xaxis, berv_corrected_observed_spectra.flux, label="Observation") plt.legend(loc=0) plt.show() # Dump the results into a pickle file pickle_name = "Chisqr_results_{0}_{1}_chip_{2}.pickle".format( star, obsnum, chip) with open(os.path.join(path, pickle_name), "wb") as f: # Pickle all the necessary parameters to store. pickle.dump( (rvs, alphas, berv_corrected_observed_spectra, host_spectrum_model, companion_spectrum_model, rv_solution, alpha_solution, min_chisqr, min_loc, solution_model), f)