def test_read_write_spectrum(tmpdir): sfile = "spectrum_test.dat" tmp_file = "{}/{}".format(tmpdir, sfile) wl = np.linspace(1.1, 1.7, 7) * 1e-4 spectrum = np.ones(7) io.write_spectrum(wl, spectrum, filename=tmp_file, type='transit', wlunits='um') # Take a look at the output file: assert sfile in os.listdir(str(tmpdir)) with open(tmp_file, 'r') as f: assert f.readline() == '# Wavelength (Rp/Rs)**2\n' assert f.readline() == '# um unitless\n' wn, flux = io.read_spectrum(tmp_file) np.testing.assert_allclose(wn, np.array([ 9090.90909091, 8333.33333333, 7692.30769231, 7142.85714286, 6666.66666667, 6250., 5882.35294118 ]), rtol=1e-7) np.testing.assert_equal(flux, np.ones(7)) wl, flux = io.read_spectrum(tmp_file, wn=False) np.testing.assert_allclose(wl, np.linspace(1.1, 1.7, 7)) np.testing.assert_equal(flux, np.ones(7))
def test_band_integrate(): wn = np.arange(1500, 5000.1, 1.0) sflux = ps.bbflux(wn, 1800.0) wn1, irac1 = io.read_spectrum( pc.ROOT+"pyratbay/data/filters/spitzer_irac1_sa.dat") wn2, irac2 = io.read_spectrum( pc.ROOT+"pyratbay/data/filters/spitzer_irac2_sa.dat") bandfluxes = ps.band_integrate(sflux, wn, [irac1,irac2], [wn1, wn2]) np.testing.assert_allclose(bandfluxes, [98527.148526, 84171.417692])
def test_band_integrate_multiple(): wn = np.arange(1500, 5000.1, 1.0) signal = np.ones_like(wn) wn1, irac1 = io.read_spectrum( pc.ROOT+"pyratbay/data/filters/spitzer_irac1_sa.dat") wn2, irac2 = io.read_spectrum( pc.ROOT+"pyratbay/data/filters/spitzer_irac2_sa.dat") bandflux = ps.band_integrate(signal, wn, [irac1, irac2], [wn1, wn2]) np.testing.assert_allclose(bandflux, [1.0, 1.0])
def test_read_spectrum_custom_header(tmpdir, header): ffile = 'filter_test.dat' tmp_file = "{}/{}".format(tmpdir, ffile) data = '1.0 1.0\n2.0 1.5\n4.0 1.0\n' with open(tmp_file, 'w') as f: f.write(header + data) wn, spec = io.read_spectrum(tmp_file) np.testing.assert_allclose(wn, np.array([10000.0, 5000.0, 2500.0]))
def test_read_write_spectrum_filter(tmpdir): ffile = 'filter_test.dat' tmp_file = "{}/{}".format(tmpdir, ffile) # Assert write: wl = np.linspace(1.4, 1.5, 20) transmission = np.array(np.abs(wl - 1.45) < 0.035, np.double) io.write_spectrum(wl * pc.um, transmission, tmp_file, 'filter') assert ffile in os.listdir(str(tmpdir)) with open(tmp_file, 'r') as f: assert f.readline() == '# Wavelength transmission\n' assert f.readline() == '# um unitless\n' # Assert read: wn, trans = io.read_spectrum(tmp_file) np.testing.assert_equal(trans, transmission) np.testing.assert_allclose(1e4 / wn, np.array([ 1.4, 1.40526, 1.41053, 1.41579, 1.42105, 1.42632, 1.43158, 1.43684, 1.44211, 1.44737, 1.45263, 1.45789, 1.46316, 1.46842, 1.47368, 1.47895, 1.48421, 1.48947, 1.49474, 1.5 ]), atol=1e-7)
if __name__ == "__main__": # Prep up TauREx: setup_cache() # Prep up ARIEL filters: filter_dir = '../inputs/filters/' filters = [ f'{filter_dir}{ffile}' for ffile in os.listdir(filter_dir) if ffile.startswith('ARIEL') ] bin_wl = [ffile.split('_')[-1].split('um')[0] for ffile in filters] wn_sort = np.argsort(np.array(bin_wl, float)) filters = [filters[idx] for idx in wn_sort] band_trans, band_wn = [], [] for filter_file in filters: bwn, btrans = io.read_spectrum(filter_file) band_trans.append(btrans) band_wn.append(bwn) # Generate synthetic ARIEL transmission observations with TauREx: planet_data = np.loadtxt('../inputs/retrieval_benchmark_transmission.txt', dtype=str) planets = planet_data[:, 0] sys_params = np.array(planet_data[:, 1:7], np.double) qplanets = np.array(planet_data[:, 7:11], np.double) pclouds = np.array(planet_data[:, 11], np.double) noise_floor = 10**np.array(planet_data[:, 12], np.double) nplanets = len(planets) sim_file = open('ariel_taurex_synthetic_transmission.dat', 'w') for i in range(nplanets):
'NIRSpec G395H', # 'NIRCam F322W2', # 'NIRCam F444W', ] # SOSS gives lower noise than PRISM # PRISM reaches 0.6 um, but there's too little flux already. # G395H is enough to complement SOSS # So, I'll stitch those two filters = sorted([ f'../inputs/filters/{filt}' for filt in os.listdir('../inputs/filters') if filt.startswith('JWST') ]) filter_wn, filter_trans = [], [] for filt in filters: wn, trans = io.read_spectrum(filt) filter_wn.append(wn) filter_trans.append(trans) nwave_obs = len(filters) flux_ratio = spectra / starflux * rprs**2 noiseless_flux_ratio = np.zeros((nmodels, nphase, nwave_obs)) for i in range(nmodels): for j in range(nphase): noiseless_flux_ratio[i, j] = ps.band_integrate(flux_ratio[i, j], 1e4 / wl, filter_trans, filter_wn) # Note, pandexo's resolution is R = 0.5 * lambda/dlambda resolution = 100.0