In [1]:
# Makes print and division act like Python 3
from __future__ import print_function, division
# Import the usual libraries
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
# Enable inline plotting
%matplotlib inline
from IPython.display import display, Latex, clear_output
from matplotlib.backends.backend_pdf import PdfPages
In [2]:
import pynrc
from pynrc import nrc_utils
from pynrc.nrc_utils import S, source_spectrum
# from pynrc.obs_nircam import model_to_hdulist, obs_hci
# from pynrc.obs_nircam import plot_contrasts, plot_contrasts_mjup, planet_mags, plot_planet_patches
pynrc.setup_logging('WARNING', verbose=False)
In [3]:
# Observation Definitions
from pynrc.nb_funcs import make_key, model_info, obs_wfe, obs_optimize
# Functions to run a series of operations
from pynrc.nb_funcs import do_opt, do_contrast, do_gen_hdus, do_sat_levels
# Plotting routines
from pynrc.nb_funcs import do_plot_contrasts, plot_images, plot_images_swlw, do_plot_contrasts2
In [4]:
# Various Bandpasses
bp_v = S.ObsBandpass('v')
bp_k = pynrc.bp_2mass('k')
bp_w1 = pynrc.bp_wise('w1')
bp_w2 = pynrc.bp_wise('w2')
In [5]:
# source, dist, age, sptype, Teff, [Fe/H], log_g, mag, band, fov
args_sources = [('HD10647', 17.34, 1400, 'F9V', 5954, +0.00, 4.7, 4.34, bp_k, 13),
('HD107146', 27.47, 200, 'G2V', 5850, +0.00, 4.5, 5.54, bp_k, 13),
('HD181327', 48.21, 12, 'F6V', 6449, +0.29, 4.4, 5.91, bp_k, 7),
('HD61005', 36.49, 100, 'G8V', 5500, +0.00, 4.5, 6.45, bp_k, 7),
('HD32297', 132.79, 30, 'A7V', 7800, -0.76, 3.8, 7.59, bp_k, 5),
]
ref_sources = [('iotHor', 'F8V', 6080, +0.15, 4.5, 4.14, bp_k),
('HD111398', 'G5V', 5689, +0.07, 4.5, 5.53, bp_k),
('HR7297', 'F7V', 6500, -0.10, 4.2, 5.10, bp_k),
('HD56161', 'G5IV', 5337, +0.00, 4.3, 4.91, bp_k),
('HD31411', 'A0V', 9500, +0.00, 4.0, 6.42, bp_k)]
In [6]:
# Directory housing VOTables
# http://vizier.u-strasbg.fr/vizier/sed/
votdir = 'votables/'
# Directory to save plots and figures
outdir = 'DebrisDisks/'
In [7]:
# List of filters
args_filter = [('F300M', 'MASK335R', 'CIRCLYOT'),
#('F335M', 'MASK335R', 'CIRCLYOT'),
('F444W', 'MASK335R', 'CIRCLYOT')]
subsize = 320
#args_filter = [('F335M', 'MASK335R', 'CIRCLYOT'),
# ('F444W', 'MASK335R', 'CIRCLYOT')]
filt_keys = []
for filt,mask,pupil in args_filter:
filt_keys.append(make_key(filt, mask=mask, pupil=pupil))
In [8]:
save_figs = True
In [9]:
# Fit spectrum to SED photometry
i=0
name_sci, dist_sci, age, spt_sci, Teff_sci, feh_sci, logg_sci, mag_sci, bp_sci, fov = args_sources[i]
vot = votdir + name_sci.replace(' ' ,'') + '.vot'
args = (name_sci, spt_sci, mag_sci, bp_sci, vot)
kwargs = {'Teff':Teff_sci, 'metallicity':feh_sci, 'log_g':logg_sci}
src = source_spectrum(*args, **kwargs)
src.fit_SED(use_err=False, robust=True, wlim=[1,4])
# Final source spectrum
sp_sci = src.sp_model
In [10]:
# Do the same for the reference source
name_ref, spt_ref, Teff_ref, feh_ref, logg_ref, mag_ref, bp_ref = ref_sources[i]
vot = votdir + name_ref.replace(' ' ,'') + '.vot'
args = (name_ref, spt_ref, mag_ref, bp_ref, vot)
kwargs = {'Teff':Teff_ref, 'metallicity':feh_ref, 'log_g':logg_ref}
ref = nrc_utils.source_spectrum(*args, **kwargs)
ref.fit_SED(use_err=True, robust=True)
# Final reference spectrum
sp_ref = ref.sp_model
In [11]:
# Plot spectra
fig, axes = plt.subplots(1,2, figsize=(14,4.5))
src.plot_SED(ax=axes[0], xr=[0.5,30])
ref.plot_SED(ax=axes[1], xr=[0.5,30])
axes[0].set_title('Science Specta -- {} ({})'.format(src.name, spt_sci))
axes[1].set_title('Reference Specta -- {} ({})'.format(ref.name, spt_ref))
#for ax in axes:
# ax.set_xscale('linear')
# ax.xaxis.set_minor_locator(AutoMinorLocator())
fig.tight_layout()
fig.subplots_adjust(top=0.85, bottom=0.1 , left=0.05, right=0.97)
if save_figs:
fig.savefig(outdir+'{}_SEDs.pdf'.format(name_sci.replace(' ','')))
In [12]:
cols = plt.rcParams['axes.prop_cycle'].by_key()['color']
# Plot the two spectra with bandpasses
fig, ax = plt.subplots(1,1, figsize=(8,5))
xr = [2.5,5.5]
for sp in [sp_sci, sp_ref]:
w = sp.wave / 1e4
ind = (w>=xr[0]) & (w<=xr[1])
ind2 = (w>=3) & (w<=5)
sp.convert('photlam')
f = sp.flux / np.mean(sp.flux[ind2])
ax.plot(w[ind], f[ind], lw=1, label=sp.name)
sp.convert('flam')
ax.set_xlim(xr)
ax.set_ylim([0,ax.get_ylim()[1]])
ax.set_xlabel(r'Wavelength ($\mu m$)')
ax.set_ylabel(r'Normalized Flux (ph/s/wave)')
ax.set_title('{} Spectra'.format(sp_sci.name))
# Overplot Filter Bandpass
ax2 = ax.twinx()
for i, af in enumerate(args_filter):
bp = pynrc.read_filter(*af)
ax2.plot(bp.wave/1e4, bp.throughput, color=cols[i+2], label=bp.name+' Bandpass')
ax2.set_ylim([0,ax2.get_ylim()[1]])
ax2.set_xlim(xr)
ax2.set_ylabel('Bandpass Throughput')
ax.legend(loc='upper left')
ax2.legend(loc='upper right')
fig.tight_layout()
if save_figs:
fig.savefig(outdir+'{}_SEDs_bps.pdf'.format(name_sci.replace(' ','')))
In [13]:
# Create a dictionary that holds the obs_coronagraphy class for each filter
wfe_drift = 0
obs_dict = obs_wfe(wfe_drift, args_filter, sp_sci, dist_sci, sp_ref=sp_ref, args_disk='auto',
wind_mode='WINDOW', subsize=subsize, verbose=False)
In [14]:
# Optimize readout parameters
tacq = 4200
# patterns=['BRIGHT2', 'SHALLOW4', 'MEDIUM8']
# do_opt(tacq, patterns=patterns, ng_min=5, ng_max=10, tacq_frac=0.1, well_levels=[2], even_nints=True)
In [15]:
# Update multiaccum info
for key in filt_keys:
obs = obs_dict[key]
read_mode='MEDIUM8'
ng, nint_sci, nint_ref = (8,21,20)
# Double up for SW, because fewer number of paired fitlers
if obs.bandpass.avgwave()/1e4 < 2.5:
nint_sci *= 2
nint_ref *= 2
obs.update_detectors(read_mode=read_mode, ngroup=ng, nint=nint_sci)
obs.nrc_ref.update_detectors(read_mode=read_mode, ngroup=ng, nint=nint_ref)
# print(key)
# print(obs.multiaccum_times)
# _ = obs.sensitivity(nsig=5, units='vegamag', verbose=True)
# print('')
In [16]:
sat_dict = {}
for k in filt_keys:
print('\n{}'.format(k))
obs = obs_dict[k]
dsat_asec = do_sat_levels(obs, satval=0.9, plot=False)
sat_dict[k] = dsat_asec
In [17]:
# Disk Images
wfe_drift = 3
wfe_roll_drift = 1
hdu_dict = do_gen_hdus(obs_dict, filt_keys, wfe_drift, wfe_roll_drift,
PA1=-5, PA2=+5, opt_diff=False, use_cmask=True,
verbose=True)
# Save FITS files
for k in filt_keys:
hdul = hdu_dict[k]
if save_figs: hdul.writeto(outdir+'{}_{}.fits'.format(name_sci,k), overwrite=True)
In [18]:
if len(filt_keys)==2:
plot_images(obs_dict, hdu_dict, filt_keys, wfe_drift, fov=fov, save_fig=save_figs, outdir=outdir)
else:
plot_images_swlw(obs_dict, hdu_dict, filt_keys, wfe_drift, fov=fov, save_fig=save_figs, outdir=outdir)
In [19]:
# Determine contrast curves for various WFE drift values
wfe_list = [0,2,5,10]
nsig = 5
roll = 10
# (Roll1 - Ref) + (Roll2 - Ref)
curves_ref = do_contrast(obs_dict, wfe_list, filt_keys[-1:], nsig=nsig, roll_angle=roll, opt_diff=False,
exclude_disk=False)
# (Roll1 - Roll2) + (Roll2 - Roll1)
curves_roll = do_contrast(obs_dict, wfe_list, filt_keys[-1:], nsig=nsig, roll_angle=roll, opt_diff=False,
exclude_disk=False, no_ref=True)
In [20]:
for k in filt_keys[-1:]:
key1 = key2 = k
lab1 = 'Ref Sub ({})'.format(obs_dict[k].filter)
lab2 = 'Roll Sub ({})'.format(obs_dict[k].filter)
fig, axes_all = do_plot_contrasts2(key1, key2, curves_ref, nsig, obs_dict, wfe_list, age,
sat_dict=sat_dict, label1=lab1, label2=lab2, yr=[22,10],
yscale2='log', yr2=[1e-1, 100], curves_all2=curves_roll)
fname = "{}_{}.pdf".format(name_sci.replace(" ", ""), k)
if save_figs:
fig.savefig(outdir+fname)
In [21]:
# Fit spectrum to SED photometry
i=1
name_sci, dist_sci, age, spt_sci, Teff_sci, feh_sci, logg_sci, mag_sci, bp_sci, fov = args_sources[i]
vot = votdir + name_sci.replace(' ' ,'') + '.vot'
args = (name_sci, spt_sci, mag_sci, bp_sci, vot)
kwargs = {'Teff':Teff_sci, 'metallicity':feh_sci, 'log_g':logg_sci}
src = source_spectrum(*args, **kwargs)
src.fit_SED(use_err=False, robust=True, wlim=[1,4])
# Final source spectrum
sp_sci = src.sp_model
In [22]:
# Do the same for the reference source
name_ref, spt_ref, Teff_ref, feh_ref, logg_ref, mag_ref, bp_ref = ref_sources[i]
vot = votdir + name_ref.replace(' ' ,'') + '.vot'
args = (name_ref, spt_ref, mag_ref, bp_ref, vot)
kwargs = {'Teff':Teff_ref, 'metallicity':feh_ref, 'log_g':logg_ref}
ref = nrc_utils.source_spectrum(*args, **kwargs)
ref.fit_SED(use_err=True, robust=True)
# Final reference spectrum
sp_ref = ref.sp_model
In [23]:
# Plot spectra
fig, axes = plt.subplots(1,2, figsize=(14,4.5))
src.plot_SED(ax=axes[0], xr=[0.5,30])
ref.plot_SED(ax=axes[1], xr=[0.5,30])
axes[0].set_title('Science Specta -- {} ({})'.format(src.name, spt_sci))
axes[1].set_title('Reference Specta -- {} ({})'.format(ref.name, spt_ref))
#for ax in axes:
# ax.set_xscale('linear')
# ax.xaxis.set_minor_locator(AutoMinorLocator())
fig.tight_layout()
fig.subplots_adjust(top=0.85, bottom=0.1 , left=0.05, right=0.97)
if save_figs:
fig.savefig(outdir+'{}_SEDs.pdf'.format(name_sci.replace(' ','')))
In [24]:
cols = plt.rcParams['axes.prop_cycle'].by_key()['color']
# Plot the two spectra with bandpasses
fig, ax = plt.subplots(1,1, figsize=(8,5))
xr = [2.5,5.5]
for sp in [sp_sci, sp_ref]:
w = sp.wave / 1e4
ind = (w>=xr[0]) & (w<=xr[1])
ind2 = (w>=3) & (w<=5)
sp.convert('photlam')
f = sp.flux / np.mean(sp.flux[ind2])
sp.convert('flam')
ax.plot(w[ind], f[ind], lw=1, label=sp.name)
ax.set_xlim(xr)
ax.set_ylim([0,ax.get_ylim()[1]])
ax.set_xlabel(r'Wavelength ($\mu m$)')
ax.set_ylabel(r'Normalized Flux (ph/s/wave)')
ax.set_title('{} Spectra'.format(sp_sci.name))
# Overplot Filter Bandpass
ax2 = ax.twinx()
for i, af in enumerate(args_filter):
bp = pynrc.read_filter(*af)
ax2.plot(bp.wave/1e4, bp.throughput, color=cols[i+2], label=bp.name+' Bandpass')
ax2.set_ylim([0,ax2.get_ylim()[1]])
ax2.set_xlim(xr)
ax2.set_ylabel('Bandpass Throughput')
ax.legend(loc='upper left')
ax2.legend(loc='upper right')
fig.tight_layout()
if save_figs:
fig.savefig(outdir+'{}_SEDs_bps.pdf'.format(name_sci.replace(' ','')))
In [25]:
# Create a dictionary that holds the obs_coronagraphy class for each filter
wfe_drift = 0
obs_dict = obs_wfe(wfe_drift, args_filter, sp_sci, dist_sci, sp_ref=sp_ref, args_disk='auto',
wind_mode='WINDOW', subsize=subsize, verbose=False)
In [26]:
# Optimize readout parameters
tacq = 4200
#do_opt(tacq, patterns='MEDIUM8', ng_min=8, ng_max=8, tacq_frac=0.1, well_levels=[2], even_nints=True)
In [27]:
for key in filt_keys:
obs = obs_dict[key]
read_mode='MEDIUM8'
ng, nint_sci, nint_ref = (10,17,15)
if obs.bandpass.avgwave()/1e4 < 2.5:
nint_sci *= 2
nint_ref *= 2
obs.update_detectors(read_mode=read_mode, ngroup=ng, nint=nint_sci)
obs.nrc_ref.update_detectors(read_mode=read_mode, ngroup=ng, nint=nint_ref)
#print(key)
#print(obs.multiaccum_times)
#_ = obs.sensitivity(nsig=5, units='vegamag', verbose=True)
#print('')
In [28]:
sat_dict = {}
for k in filt_keys:
print('\n{}'.format(k))
obs = obs_dict[k]
dsat_asec = do_sat_levels(obs, satval=0.9, plot=False)
sat_dict[k] = dsat_asec
In [29]:
# Disk Images
wfe_drift = 3
wfe_roll_drift = 1
hdu_dict = do_gen_hdus(obs_dict, filt_keys, wfe_drift, wfe_roll_drift,
PA1=-5, PA2=+5, opt_diff=False, use_cmask=True,
verbose=True)
In [30]:
# Save FITS files
for k in filt_keys:
hdul = hdu_dict[k]
if save_figs:
hdul.writeto(outdir+'{}_{}.fits'.format(name_sci,k), overwrite=True)
In [31]:
if len(filt_keys)==2:
plot_images(obs_dict, hdu_dict, filt_keys, wfe_drift, fov=fov, save_fig=save_figs, outdir=outdir)
else:
plot_images_swlw(obs_dict, hdu_dict, filt_keys, wfe_drift, fov=fov, save_fig=save_figs, outdir=outdir)
In [32]:
# Determine contrast curves for various WFE drift values
wfe_list = [0,2,5,10]
nsig = 5
roll = 10
# (Roll1 - Ref) + (Roll2 - Ref)
curves_ref = do_contrast(obs_dict, wfe_list, filt_keys[-1:], nsig=nsig, roll_angle=roll, opt_diff=False,
exclude_disk=False)
# (Roll1 - Roll2) + (Roll2 - Roll1)
curves_roll = do_contrast(obs_dict, wfe_list, filt_keys[-1:], nsig=nsig, roll_angle=roll, opt_diff=False,
exclude_disk=False, no_ref=True)
In [33]:
for k in filt_keys[-1:]:
key1 = key2 = k
lab1 = 'Ref Sub ({})'.format(obs_dict[k].filter)
lab2 = 'Roll Sub ({})'.format(obs_dict[k].filter)
fig, axes_all = do_plot_contrasts2(key1, key2, curves_ref, nsig, obs_dict, wfe_list, age,
sat_dict=sat_dict, label1=lab1, label2=lab2, yr=[22,10],
yscale2='log', yr2=[1e-1, 100], curves_all2=curves_roll)
fname = "{}_{}.pdf".format(name_sci.replace(" ", ""), k)
if save_figs:
fig.savefig(outdir+fname)
In [34]:
# Fit spectrum to SED photometry
i=2
name_sci, dist_sci, age, spt_sci, Teff_sci, feh_sci, logg_sci, mag_sci, bp_sci, fov = args_sources[i]
vot = votdir + name_sci.replace(' ' ,'') + '.vot'
args = (name_sci, spt_sci, mag_sci, bp_sci, vot)
kwargs = {'Teff':Teff_sci, 'metallicity':feh_sci, 'log_g':logg_sci}
src = source_spectrum(*args, **kwargs)
src.fit_SED(use_err=False, robust=True, wlim=[1,4])
# Final source spectrum
sp_sci = src.sp_model
In [35]:
# Do the same for the reference source
name_ref, spt_ref, Teff_ref, feh_ref, logg_ref, mag_ref, bp_ref = ref_sources[i]
vot = votdir + name_ref.replace(' ' ,'') + '.vot'
args = (name_ref, spt_ref, mag_ref, bp_ref, vot)
kwargs = {'Teff':Teff_ref, 'metallicity':feh_ref, 'log_g':logg_ref}
ref = nrc_utils.source_spectrum(*args, **kwargs)
ref.fit_SED(use_err=True, robust=True)
# Final reference spectrum
sp_ref = ref.sp_model
In [36]:
# Plot spectra
fig, axes = plt.subplots(1,2, figsize=(14,4.5))
src.plot_SED(ax=axes[0], xr=[0.5,30])
ref.plot_SED(ax=axes[1], xr=[0.5,30])
axes[0].set_title('Science Specta -- {} ({})'.format(src.name, spt_sci))
axes[1].set_title('Reference Specta -- {} ({})'.format(ref.name, spt_ref))
#for ax in axes:
# ax.set_xscale('linear')
# ax.xaxis.set_minor_locator(AutoMinorLocator())
fig.tight_layout()
fig.subplots_adjust(top=0.85, bottom=0.1 , left=0.05, right=0.97)
if save_figs:
fig.savefig(outdir+'{}_SEDs.pdf'.format(name_sci.replace(' ','')))
In [37]:
cols = plt.rcParams['axes.prop_cycle'].by_key()['color']
# Plot the two spectra with bandpasses
fig, ax = plt.subplots(1,1, figsize=(8,5))
xr = [2.5,5.5]
for sp in [sp_sci, sp_ref]:
w = sp.wave / 1e4
ind = (w>=xr[0]) & (w<=xr[1])
ind2 = (w>=3) & (w<=5)
sp.convert('photlam')
f = sp.flux / np.mean(sp.flux[ind2])
sp.convert('flam')
ax.plot(w[ind], f[ind], lw=1, label=sp.name)
ax.set_xlim(xr)
ax.set_ylim([0,ax.get_ylim()[1]])
ax.set_xlabel(r'Wavelength ($\mu m$)')
ax.set_ylabel(r'Normalized Flux (ph/s/wave)')
ax.set_title('{} Spectra'.format(sp_sci.name))
# Overplot Filter Bandpass
ax2 = ax.twinx()
for i, af in enumerate(args_filter):
bp = pynrc.read_filter(*af)
ax2.plot(bp.wave/1e4, bp.throughput, color=cols[i+2], label=bp.name+' Bandpass')
ax2.set_ylim([0,ax2.get_ylim()[1]])
ax2.set_xlim(xr)
ax2.set_ylabel('Bandpass Throughput')
ax.legend(loc='upper left')
ax2.legend(loc='upper right')
fig.tight_layout()
if save_figs:
fig.savefig(outdir+'{}_SEDs_bps.pdf'.format(name_sci.replace(' ','')))
In [38]:
# Create a dictionary that holds the obs_coronagraphy class for each filter
wfe_drift = 0
obs_dict = obs_wfe(wfe_drift, args_filter, sp_sci, dist_sci, sp_ref=sp_ref, args_disk='auto',
wind_mode='WINDOW', subsize=subsize, verbose=False)
In [39]:
# Optimize readout parameters
tacq = 4200
#do_opt(tacq, patterns='MEDIUM8', ng_min=8, ng_max=8, tacq_frac=0.1, well_levels=[2], even_nints=True)
In [40]:
for key in filt_keys:
obs = obs_dict[key]
read_mode='MEDIUM8'
ng, nint_sci, nint_ref = (10,11,15)
if obs.bandpass.avgwave()/1e4 < 2.5:
nint_sci *= 2
nint_ref *= 2
obs.update_detectors(read_mode=read_mode, ngroup=ng, nint=nint_sci)
obs.nrc_ref.update_detectors(read_mode=read_mode, ngroup=ng, nint=nint_ref)
#print(key)
#print(obs.multiaccum_times)
#_ = obs.sensitivity(nsig=5, units='vegamag', verbose=True)
#print('')
In [41]:
sat_dict = {}
for k in filt_keys:
print('\n{}'.format(k))
obs = obs_dict[k]
dsat_asec = do_sat_levels(obs, satval=0.9, plot=False)
sat_dict[k] = dsat_asec
In [42]:
# Disk Images
wfe_drift = 3
wfe_roll_drift = 1
hdu_dict = do_gen_hdus(obs_dict, filt_keys, wfe_drift, wfe_roll_drift,
PA1=-5, PA2=+5, opt_diff=False, use_cmask=True,
verbose=True)
In [43]:
# Save FITS files
for k in filt_keys:
hdul = hdu_dict[k]
if save_figs:
hdul.writeto(outdir+'{}_{}.fits'.format(name_sci,k), overwrite=True)
In [44]:
save_fig = True
if len(filt_keys)==2:
plot_images(obs_dict, hdu_dict, filt_keys, wfe_drift, fov=fov, save_fig=save_fig, outdir=outdir)
else:
plot_images_swlw(obs_dict, hdu_dict, filt_keys, wfe_drift, fov=fov, save_fig=save_fig, outdir=outdir)
In [45]:
# Determine contrast curves for various WFE drift values
wfe_list = [0,2,5,10]
nsig = 5
roll = 10
# (Roll1 - Ref) + (Roll2 - Ref)
curves_ref = do_contrast(obs_dict, wfe_list, filt_keys[-1:], nsig=nsig, roll_angle=roll, opt_diff=False,
exclude_disk=False)
# (Roll1 - Roll2) + (Roll2 - Roll1)
curves_roll = do_contrast(obs_dict, wfe_list, filt_keys[-1:], nsig=nsig, roll_angle=roll, opt_diff=False,
exclude_disk=False, no_ref=True)
In [46]:
for k in filt_keys[-1:]:
key1 = key2 = k
lab1 = 'Ref Sub ({})'.format(obs_dict[k].filter)
lab2 = 'Roll Sub ({})'.format(obs_dict[k].filter)
fig, axes_all = do_plot_contrasts2(key1, key2, curves_ref, nsig, obs_dict, wfe_list, age,
sat_dict=sat_dict, label1=lab1, label2=lab2, yr=[22,10],
yscale2='log', yr2=[1e-1, 100], curves_all2=curves_roll)
fname = "{}_{}.pdf".format(name_sci.replace(" ", ""), k)
if save_figs:
fig.savefig(outdir+fname)
In [47]:
# Fit spectrum to SED photometry
i=3
name_sci, dist_sci, age, spt_sci, Teff_sci, feh_sci, logg_sci, mag_sci, bp_sci, fov = args_sources[i]
vot = votdir + name_sci.replace(' ' ,'') + '.vot'
args = (name_sci, spt_sci, mag_sci, bp_sci, vot)
kwargs = {'Teff':Teff_sci, 'metallicity':feh_sci, 'log_g':logg_sci}
src = source_spectrum(*args, **kwargs)
src.fit_SED(use_err=False, robust=True, wlim=[1,4])
# Final source spectrum
sp_sci = src.sp_model
In [48]:
# Do the same for the reference source
name_ref, spt_ref, Teff_ref, feh_ref, logg_ref, mag_ref, bp_ref = ref_sources[i]
vot = votdir + name_ref.replace(' ' ,'') + '.vot'
args = (name_ref, spt_ref, mag_ref, bp_ref, vot)
kwargs = {'Teff':Teff_ref, 'metallicity':feh_ref, 'log_g':logg_ref}
ref = nrc_utils.source_spectrum(*args, **kwargs)
ref.fit_SED(use_err=True, robust=True)
# Final reference spectrum
sp_ref = ref.sp_model
In [49]:
# Plot spectra
fig, axes = plt.subplots(1,2, figsize=(14,4.5))
src.plot_SED(ax=axes[0], xr=[0.5,30])
ref.plot_SED(ax=axes[1], xr=[0.5,30])
axes[0].set_title('Science Specta -- {} ({})'.format(src.name, spt_sci))
axes[1].set_title('Reference Specta -- {} ({})'.format(ref.name, spt_ref))
#for ax in axes:
# ax.set_xscale('linear')
# ax.xaxis.set_minor_locator(AutoMinorLocator())
fig.tight_layout()
fig.subplots_adjust(top=0.85, bottom=0.1 , left=0.05, right=0.97)
if save_figs:
fig.savefig(outdir+'{}_SEDs.pdf'.format(name_sci.replace(' ','')))
In [50]:
cols = plt.rcParams['axes.prop_cycle'].by_key()['color']
# Plot the two spectra with bandpasses
fig, ax = plt.subplots(1,1, figsize=(8,5))
xr = [2.5,5.5]
for sp in [sp_sci, sp_ref]:
w = sp.wave / 1e4
ind = (w>=xr[0]) & (w<=xr[1])
ind2 = (w>=3) & (w<=5)
sp.convert('photlam')
f = sp.flux / np.mean(sp.flux[ind2])
sp.convert('flam')
ax.plot(w[ind], f[ind], lw=1, label=sp.name)
ax.set_xlim(xr)
ax.set_ylim([0,ax.get_ylim()[1]])
ax.set_xlabel(r'Wavelength ($\mu m$)')
ax.set_ylabel(r'Normalized Flux (ph/s/wave)')
ax.set_title('{} Spectra'.format(sp_sci.name))
# Overplot Filter Bandpass
ax2 = ax.twinx()
for i, af in enumerate(args_filter):
bp = pynrc.read_filter(*af)
ax2.plot(bp.wave/1e4, bp.throughput, color=cols[i+2], label=bp.name+' Bandpass')
ax2.set_ylim([0,ax2.get_ylim()[1]])
ax2.set_xlim(xr)
ax2.set_ylabel('Bandpass Throughput')
ax.legend(loc='upper left')
ax2.legend(loc='upper right')
fig.tight_layout()
if save_figs:
fig.savefig(outdir+'{}_SEDs_bps.pdf'.format(name_sci.replace(' ','')))
In [51]:
# Create a dictionary that holds the obs_coronagraphy class for each filter
wfe_drift = 0
obs_dict = obs_wfe(wfe_drift, args_filter, sp_sci, dist_sci, sp_ref=sp_ref, args_disk='auto',
wind_mode='WINDOW', subsize=subsize, verbose=False)
In [52]:
# Optimize readout parameters
tacq = 4200
#do_opt(tacq, patterns='MEDIUM8', ng_min=8, ng_max=8, tacq_frac=0.1, well_levels=[2], even_nints=True)
In [53]:
for key in filt_keys:
obs = obs_dict[key]
read_mode='MEDIUM8'
ng, nint_sci, nint_ref = (10,14,15)
if obs.bandpass.avgwave()/1e4 < 2.5:
nint_sci *= 2
nint_ref *= 2
obs.update_detectors(read_mode=read_mode, ngroup=ng, nint=nint_sci)
obs.nrc_ref.update_detectors(read_mode=read_mode, ngroup=ng, nint=nint_ref)
#print(key)
#print(obs.multiaccum_times)
#_ = obs.sensitivity(nsig=5, units='vegamag', verbose=True)
#print('')
In [54]:
sat_dict = {}
for k in filt_keys:
print('\n{}'.format(k))
obs = obs_dict[k]
dsat_asec = do_sat_levels(obs, satval=0.9, plot=False)
sat_dict[k] = dsat_asec
In [55]:
# Disk Images
wfe_drift = 3
wfe_roll_drift = 1
hdu_dict = do_gen_hdus(obs_dict, filt_keys, wfe_drift, wfe_roll_drift,
PA1=-5, PA2=+5, opt_diff=False, use_cmask=True,
verbose=True)
In [56]:
# Save FITS files
for k in filt_keys:
hdul = hdu_dict[k]
if save_figs:
hdul.writeto(outdir+'{}_{}.fits'.format(name_sci,k), overwrite=True)
In [57]:
save_fig = True
if len(filt_keys)==2:
plot_images(obs_dict, hdu_dict, filt_keys, wfe_drift, fov=fov, save_fig=save_fig, outdir=outdir)
else:
plot_images_swlw(obs_dict, hdu_dict, filt_keys, wfe_drift, fov=fov, save_fig=save_fig, outdir=outdir)
In [58]:
# Determine contrast curves for various WFE drift values
wfe_list = [0,2,5,10]
nsig = 5
roll = 10
# (Roll1 - Ref) + (Roll2 - Ref)
curves_ref = do_contrast(obs_dict, wfe_list, filt_keys[-1:], nsig=nsig, roll_angle=roll, opt_diff=False,
exclude_disk=False)
# (Roll1 - Roll2) + (Roll2 - Roll1)
curves_roll = do_contrast(obs_dict, wfe_list, filt_keys[-1:], nsig=nsig, roll_angle=roll, opt_diff=False,
exclude_disk=False, no_ref=True)
In [59]:
for k in filt_keys[-1:]:
key1 = key2 = k
lab1 = 'Ref Sub ({})'.format(obs_dict[k].filter)
lab2 = 'Roll Sub ({})'.format(obs_dict[k].filter)
fig, axes_all = do_plot_contrasts2(key1, key2, curves_ref, nsig, obs_dict, wfe_list, age,
sat_dict=sat_dict, label1=lab1, label2=lab2, yr=[22,10],
yscale2='log', yr2=[1e-1, 100], curves_all2=curves_roll)
fname = "{}_{}.pdf".format(name_sci.replace(" ", ""), k)
if save_figs:
fig.savefig(outdir+fname)
In [60]:
# Fit spectrum to SED photometry
i=4
name_sci, dist_sci, age, spt_sci, Teff_sci, feh_sci, logg_sci, mag_sci, bp_sci, fov = args_sources[i]
vot = votdir + name_sci.replace(' ' ,'') + '.vot'
args = (name_sci, spt_sci, mag_sci, bp_sci, vot)
kwargs = {'Teff':Teff_sci, 'metallicity':feh_sci, 'log_g':logg_sci}
src = source_spectrum(*args, **kwargs)
src.fit_SED(use_err=False, robust=True, wlim=[1,4])
# Final source spectrum
sp_sci = src.sp_model
In [61]:
# Do the same for the reference source
name_ref, spt_ref, Teff_ref, feh_ref, logg_ref, mag_ref, bp_ref = ref_sources[i]
vot = votdir + name_ref.replace(' ' ,'') + '.vot'
args = (name_ref, spt_ref, mag_ref, bp_ref, vot)
kwargs = {'Teff':Teff_ref, 'metallicity':feh_ref, 'log_g':logg_ref}
ref = nrc_utils.source_spectrum(*args, **kwargs)
ref.fit_SED(use_err=True, robust=True)
# Final reference spectrum
sp_ref = ref.sp_model
In [62]:
# Plot spectra
fig, axes = plt.subplots(1,2, figsize=(14,4.5))
src.plot_SED(ax=axes[0], xr=[0.5,30])
ref.plot_SED(ax=axes[1], xr=[0.5,30])
axes[0].set_title('Science Specta -- {} ({})'.format(src.name, spt_sci))
axes[1].set_title('Reference Specta -- {} ({})'.format(ref.name, spt_ref))
#for ax in axes:
# ax.set_xscale('linear')
# ax.xaxis.set_minor_locator(AutoMinorLocator())
fig.tight_layout()
fig.subplots_adjust(top=0.85, bottom=0.1 , left=0.05, right=0.97)
fig.savefig(outdir+'{}_SEDs.pdf'.format(name_sci.replace(' ','')))
In [63]:
cols = plt.rcParams['axes.prop_cycle'].by_key()['color']
# Plot the two spectra with bandpasses
fig, ax = plt.subplots(1,1, figsize=(8,5))
xr = [2.5,5.5]
for sp in [sp_sci, sp_ref]:
w = sp.wave / 1e4
ind = (w>=xr[0]) & (w<=xr[1])
ind2 = (w>=3) & (w<=5)
sp.convert('photlam')
f = sp.flux / np.mean(sp.flux[ind2])
sp.convert('flam')
ax.plot(w[ind], f[ind], lw=1, label=sp.name)
ax.set_xlim(xr)
ax.set_ylim([0,ax.get_ylim()[1]])
ax.set_xlabel(r'Wavelength ($\mu m$)')
ax.set_ylabel(r'Normalized Flux (ph/s/wave)')
ax.set_title('{} Spectra'.format(sp_sci.name))
# Overplot Filter Bandpass
ax2 = ax.twinx()
for i, af in enumerate(args_filter):
bp = pynrc.read_filter(*af)
ax2.plot(bp.wave/1e4, bp.throughput, color=cols[i+2], label=bp.name+' Bandpass')
ax2.set_ylim([0,ax2.get_ylim()[1]])
ax2.set_xlim(xr)
ax2.set_ylabel('Bandpass Throughput')
ax.legend(loc='upper left')
ax2.legend(loc='upper right')
fig.tight_layout()
if save_figs:
fig.savefig(outdir+'{}_SEDs_bps.pdf'.format(name_sci.replace(' ','')))
In [64]:
# Create a dictionary that holds the obs_coronagraphy class for each filter
wfe_drift = 0
obs_dict = obs_wfe(wfe_drift, args_filter, sp_sci, dist_sci, sp_ref=sp_ref, args_disk='auto',
wind_mode='WINDOW', subsize=subsize, verbose=False)
In [65]:
# Optimize readout parameters
tacq = 4200
#do_opt(tacq, patterns='MEDIUM8', ng_min=8, ng_max=8, tacq_frac=0.1, well_levels=[2], even_nints=True)
In [66]:
for key in filt_keys:
obs = obs_dict[key]
read_mode='MEDIUM8'
ng, nint_sci, nint_ref = (10,15,15)
if obs.bandpass.avgwave()/1e4 < 2.5:
nint_sci *= 2
nint_ref *= 2
obs.update_detectors(read_mode=read_mode, ngroup=ng, nint=nint_sci)
obs.nrc_ref.update_detectors(read_mode=read_mode, ngroup=ng, nint=nint_ref)
#print(key)
#print(obs.multiaccum_times)
#_ = obs.sensitivity(nsig=5, units='vegamag', verbose=True)
#print('')
In [67]:
sat_dict = {}
for k in filt_keys:
print('\n{}'.format(k))
obs = obs_dict[k]
dsat_asec = do_sat_levels(obs, satval=0.9, plot=False)
sat_dict[k] = dsat_asec
In [68]:
# Disk Images
wfe_drift = 3
wfe_roll_drift = 1
hdu_dict = do_gen_hdus(obs_dict, filt_keys, wfe_drift, wfe_roll_drift,
PA1=-5, PA2=+5, opt_diff=False, use_cmask=True,
verbose=True)
In [69]:
# Save FITS files
for k in filt_keys:
hdul = hdu_dict[k]
if save_figs:
hdul.writeto(outdir+'{}_{}.fits'.format(name_sci,k), overwrite=True)
In [70]:
save_fig = True
if len(filt_keys)==2:
plot_images(obs_dict, hdu_dict, filt_keys, wfe_drift, fov=fov, save_fig=save_fig, outdir=outdir)
else:
plot_images_swlw(obs_dict, hdu_dict, filt_keys, wfe_drift, fov=fov, save_fig=save_fig, outdir=outdir)
In [71]:
# Determine contrast curves for various WFE drift values
wfe_list = [0,2,5,10]
nsig = 5
roll = 10
# (Roll1 - Ref) + (Roll2 - Ref)
curves_ref = do_contrast(obs_dict, wfe_list, filt_keys[-1:], nsig=nsig, roll_angle=roll, opt_diff=False,
exclude_disk=False)
# (Roll1 - Roll2) + (Roll2 - Roll1)
curves_roll = do_contrast(obs_dict, wfe_list, filt_keys[-1:], nsig=nsig, roll_angle=roll, opt_diff=False,
exclude_disk=False, no_ref=True)
In [72]:
for k in filt_keys[-1:]:
key1 = key2 = k
lab1 = 'Ref Sub ({})'.format(obs_dict[k].filter)
lab2 = 'Roll Sub ({})'.format(obs_dict[k].filter)
fig, axes_all = do_plot_contrasts2(key1, key2, curves_ref, nsig, obs_dict, wfe_list, age,
sat_dict=sat_dict, label1=lab1, label2=lab2, yr=[22,10],
yscale2='log', yr2=[1e-1, 100], curves_all2=curves_roll)
fname = "{}_{}.pdf".format(name_sci.replace(" ", ""), k)
if save_figs:
fig.savefig(outdir+fname)
In [ ]: