Skip to content

Commit

Permalink
Updates to the thermal yield:
Browse files Browse the repository at this point in the history
- The cooling curves don't go below 10 M_earth, so we just assigned a thermal equilibrium model to those planets with albedo=0.5
- For all other planets, we pick which ever is larger: cooling curves or thermal equilibrium.
  • Loading branch information
maxwellmb committed Jul 6, 2019
1 parent b9a5a4e commit dc63c27
Showing 1 changed file with 184 additions and 22 deletions.
206 changes: 184 additions & 22 deletions Tutorials/example_thermal_yield.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
from psisim import telescope,instrument,observation,spectrum,universe,plots
import numpy as np
import matplotlib.pylab as plt

import copy
import time

tmt = telescope.TMT()
psi_red = instrument.PSI_Red()
psi_red.set_observing_mode(3600,2,'M',10, np.linspace(4.2,4.8,3)) #60s, 40 exposures,z-band, R of 10
psi_red.set_observing_mode(3600,2,'M',10, np.linspace(4.2,4.8,3)) #3600s, 2 exposures,M-band, R of 10

######################################
######## Generate the universe #######
######################################

exosims_config_filename = "forBruceandDimitri_EXOCAT1.json" #Some filename here
uni = universe.ExoSims_Universe(exosims_config_filename)
uni.simulate_EXOSIMS_Universe()

##############################
######## Lots of setup #######
##############################

min_iwa = np.min(psi_red.current_wvs)*1e-6/tmt.diameter*206265
planet_table = uni.planets
planet_table = planet_table[np.where(planet_table['PlanetMass'] > 10)]
# planet_table = planet_table[np.where(planet_table['PlanetMass'] > 10)]
planet_table = planet_table[planet_table['AngSep']/1000 > min_iwa]
planet_table = planet_table[planet_table['Flux Ratio'] > 1e-10]

n_planets = len(planet_table)

planet_types = []
planet_spectra = []
planet_spectra = [] #The spectrum from the cooling tracks
planet_eq_spectra = [] #The spectrum from the equilibrium thermal emission
planet_ages = []

n_planets_now = n_planets
Expand All @@ -45,37 +54,70 @@
#Generate the model wavelenths
model_wvs = np.linspace(model_wv_low, model_wv_high, n_model_wv) #Choose some wavelengths

#####################################
######## Generate the Spectra #######
#####################################


print("\n Starting to generate planet spectra")
for planet in planet_table[rand_planets]:

#INSERT PLANET SELECTION RULES HERE
planet_type = "Gas"
planet_types.append(planet_type)

age = np.random.random() * 5e9 # between 0 and 5 Gyr
planet_ages.append(age)
if planet['PlanetMass'] < 10:
#If the planet is < 10 M_Earth, we don't trust bex. So we'll be pessimistic and just report its thermal equilibrium.
planet_type = "blackbody"
planet_types.append(planet_type)

#The bond albedo
atmospheric_parameters = 0.5
planet_spectrum = spectrum.simulate_spectrum(planet, model_wvs, intermediate_R, atmospheric_parameters, package='blackbody')
planet_spectra.append(planet_spectrum)
planet_eq_spectra.append(planet_spectrum)

else:
planet_type = "Gas"
planet_types.append(planet_type)

age = np.random.random() * 5e9 # between 0 and 5 Gyr
planet_ages.append(age)

time1 = time.time()
time1 = time.time()

#Here we're going to generate the spectrum as the addition of cooling models and a blackbody (i.e. equilibrium Temperature)
#Generate the spectrum from cooling models and downsample to intermediate resolution
atmospheric_parameters = age, 'M', True
planet_spectrum = spectrum.simulate_spectrum(planet, model_wvs, intermediate_R, atmospheric_parameters, package='bex-cooling')
### Here we're going to generate the spectrum as the addition of cooling models and a blackbody (i.e. equilibrium Temperature)
## Generate the spectrum from cooling models and downsample to intermediate resolution
atmospheric_parameters = age, 'M', True
planet_spectrum = spectrum.simulate_spectrum(planet, model_wvs, intermediate_R, atmospheric_parameters, package='bex-cooling')

#The bond albedo
atmospheric_parameters = 0.5
planet_spectrum += np.array(spectrum.simulate_spectrum(planet, model_wvs, intermediate_R, atmospheric_parameters, package='blackbody'))

planet_spectra.append(planet_spectrum)

time2 = time.time()
print('Spectrum took {0:.3f} s'.format((time2-time1)))
## Generate the spectrum from a blackbody
atmospheric_parameters = 0.5#The bond albedo
planet_eq_spectrum = np.array(spectrum.simulate_spectrum(planet, model_wvs, intermediate_R, atmospheric_parameters, package='blackbody'))

planet_spectra.append(planet_spectrum)
planet_eq_spectra.append(planet_eq_spectrum)

time2 = time.time()
print('Spectrum took {0:.3f} s'.format((time2-time1)))

print("Done generating planet spectra")
print("\n Starting to simulate observations")

#Here we take the biggest of either the planet cooling spectrum or the planet equilibrium spectrum
#Kind of hacky, but is a good start
final_spectra = np.array(copy.deepcopy(planet_spectra))
planet_eq_spectra = np.array(planet_eq_spectra)
planet_spectra = np.array(planet_spectra)
final_spectra[planet_eq_spectra > planet_spectra] = planet_eq_spectra[planet_eq_spectra > planet_spectra]


##########################################
######## Simulate the observations #######
##########################################

post_processing_gain=10
sim_F_lambda, sim_F_lambda_errs,sim_F_lambda_stellar, noise_components = observation.simulate_observation_set(tmt, psi_red,
planet_table[rand_planets], planet_spectra, model_wvs, intermediate_R, inject_noise=False,
planet_table[rand_planets], final_spectra, model_wvs, intermediate_R, inject_noise=False,
post_processing_gain=post_processing_gain,return_noise_components=True)

speckle_noises = np.array([s[0] for s in noise_components])
Expand All @@ -87,6 +129,11 @@

detected = psi_red.detect_planets(planet_table[rand_planets],snrs,tmt)


########################################
######## Make the contrast Plot ########
########################################

#Choose which wavelength you want to plot the detections at:
wv_index = 1

Expand All @@ -101,9 +148,124 @@
print("Planets detected: {}".format(len(np.where(detected[:,wv_index])[0])))
print("Planets not detected: {}".format(len(np.where(~detected[:,wv_index])[0])))
print("Post-processing gain: {}".format(post_processing_gain))
plt.show()

########################################
######## Make the magnitude Plot #######
########################################

## Choose which wavelength you want to plot the detections at:
# wv_index = 1

# fig, ax = plots.plot_detected_planet_magnitudes(planet_table[rand_planets],wv_index,
# detected,flux_ratios,psi_red,tmt,show=False)

# #The user can now adjust the plot as they see fit.
# #e.g. Annotate the plot
# # ax.text(4e-2,1e-5,"Planets detected: {}".format(len(np.where(detected[:,wv_index])[0])),color='k')
# # ax.text(4e-2,0.5e-5,"Planets not detected: {}".format(len(np.where(~detected[:,wv_index])[0])),color='k')
# # ax.text(4e-2,0.25e-5,"Post-processing gain: {}".format(post_processing_gain),color='k')
# print("Planets detected: {}".format(len(np.where(detected[:,wv_index])[0])))
# print("Planets not detected: {}".format(len(np.where(~detected[:,wv_index])[0])))
# print("Post-processing gain: {}".format(post_processing_gain))
# plt.show()

###########################################
######## Recalculate the magnitudes #######
###########################################


# dMags = -2.5*np.log10(flux_ratios[:,wv_index])

# band = psi_red.current_filter
# if band == 'R':
# bexlabel = 'CousinsR'
# starlabel = 'StarRmag'
# elif band == 'I':
# bexlabel = 'CousinsI'
# starlabel = 'StarImag'
# elif band == 'J':
# bexlabel = 'SPHEREJ'
# starlabel = 'StarJmag'
# elif band == 'H':
# bexlabel = 'SPHEREH'
# starlabel = 'StarHmag'
# elif band == 'K':
# bexlabel = 'SPHEREKs'
# starlabel = 'StarKmag'
# elif band == 'L':
# bexlabel = 'NACOLp'
# starlabel = 'StarKmag'
# elif band == 'M':
# bexlabel = 'NACOMp'
# starlabel = 'StarKmag'
# else:
# raise ValueError("Band needs to be 'R', 'I', 'J', 'H', 'K', 'L', 'M'. Got {0}.".format(band))

# stellar_mags = planet_table[starlabel]
# stellar_mags = np.array(stellar_mags)

# planet_mag = stellar_mags+dMags

# plt.figure()
# plt.plot(planet_mag,np.log10(masses),'o',alpha=0.3)
# plt.xlabel("M-band Magnitude")
# plt.ylabel("Log10(Mass)")
# plt.show()

# plt.figure()
# plt.plot(np.log10(flux_ratios[:,wv_index]),np.log10(masses),'o',alpha=0.3)
# plt.xlabel("log10(Flux Ratios")
# plt.ylabel("Log10(Mass)")
# plt.show()

# sim_F_lambda

# plt.figure()
# plt.plot((sim_F_lambda[:,wv_index]),np.log10(masses),'o',alpha=0.3)
# plt.xlabel("log10(sim_F_lambda)")
# plt.ylabel("Log10(Mass)")
# plt.xlim(0,1e8)
# plt.show()


############################################################
######## Histogram of detections and non-detections ########
############################################################

### And now we'll make a simple histogram of detected vs. generated planet mass.
masses = [planet['PlanetMass'] for planet in planet_table]
detected_masses = [planet['PlanetMass'] for planet in planet_table[detected[:,1]]] #Picking 4.5 micron

bins = np.logspace(np.log10(1),np.log10(1000),20)
fig,axes = plt.subplots(2,1)
hist_detected = axes[0].hist(detected_masses,bins=bins,color='darkturquoise',histtype='step',label="Detected",linewidth=3.5)
hist_all = axes[0].hist(masses,color='k',bins=bins,histtype='step',label="Full Sample",linewidth=3.5)
axes[0].set_xscale("log")
axes[0].set_ylabel("Number of planets")
axes[0].set_xlabel(r"Planet Mass [$M_{\oplus}$]")
axes[0].legend()

efficiency = hist_detected[0]/hist_all[0]
efficiency[efficiency!=efficiency]=0
axes[1].step(np.append(hist_detected[1][0],hist_detected[1]),np.append(0,np.append(efficiency,0)),where='post',linewidth=3)
axes[1].set_ylabel("Detection Efficiency")
#Stupid hack
axes[1].set_xscale("log")
axes[1].set_xlabel(r"Planet Mass [$M_{\oplus}$]")
fig.suptitle(r"Thermal Emission Detections at {:.1f}$\mu m$".format(psi_red.current_wvs[1]))
plt.show()

import pdb; pdb.set_trace()
##################################
######## Save the results ########
##################################

from astropy.io import fits
planet_table[rand_planets].write("thermal_planet_table.csv")
ps_hdu = fits.PrimaryHDU(planet_spectra)
ps_hdu.writeto("thermal_planet_spectra.fits",overwrite=True)
flux_hdu = fits.PrimaryHDU([sim_F_lambda, sim_F_lambda_errs,np.array(sim_F_lambda_stellar)])
flux_hdu.writeto("thermal_Observation_set.fits",overwrite=True)
noise_components_hdu = fits.PrimaryHDU(noise_components)
noise_components_hdu.writeto("thermal_noise_components.fits",overwrite=True)

0 comments on commit dc63c27

Please sign in to comment.