def __init__(self, filename=None): super().__init__() self._spectrum = np.loadtxt(filename) self._wlgrid = self._spectrum[:, 0] sortedwl = self._wlgrid.argsort()[::-1] self._wlgrid = self._wlgrid[sortedwl] self._wngrid = 10000 / self._wlgrid self._noise = self._spectrum[sortedwl, 1] try: self._wlwidths = self._spectrum[sortedwl, 2] except IndexError: from taurex.util.util import compute_bin_edges self._wlwidths - compute_bin_edges(self._wlgrid)[-1] self.create_wn_widths() self._binner = FluxBinner(self._wngrid, wngrid_width=self._wnwidths)
def __init__(self, noise_scale=1): super().__init__() self._scale = noise_scale # Wavelength and widths for WFC3 wfc3_grid = np.array([ 1.126, 1.156, 1.184, 1.212, 1.238, 1.265, 1.292, 1.318, 1.345, 1.372, 1.399, 1.428, 1.457, 1.487, 1.518, 1.551, 1.586, 1.623, ]) wfc3_wlwidths = np.array([ 3.079e-2, 2.930e-2, 2.790e-2, 2.689e-2, 2.649e-2, 2.689e-2, 2.670e-2, 2.629e-2, 2.649e-2, 2.739e-2, 2.800e-2, 2.849e-2, 2.940e-2, 3.079e-2, 3.180e-2, 3.370e-2, 3.600e-2, 3.899e-2, ]) # convert to wavenumber widths wfc3_wnwidths = wnwidth_to_wlwidth(wfc3_grid, wfc3_wlwidths) self._wfc3_size = wfc3_grid.shape[0] # Create our grid resampler self._binner = FluxBinner(wngrid=10000/wfc3_grid, wngrid_width=wfc3_wnwidths)
def test_fluxbinner(spectra): wngrid = np.linspace(400, 20000, 100) fb = FluxBinner(wngrid=wngrid) wn, sp, _, _ = fb.bindown(*spectra) assert(wngrid.shape[0] == wn.shape[0]) assert np.mean(sp) == pytest.approx(np.mean(spectra[1]), rel=0.1)
class InstrumentFile(Instrument): """ Loads a 2-3 column file with wlgrid and noise and maybe wlgrid Parameters ---------- filename: str Filename of file containing the binning and error """ def __init__(self, filename=None): super().__init__() self._spectrum = np.loadtxt(filename) self._wlgrid = self._spectrum[:, 0] sortedwl = self._wlgrid.argsort()[::-1] self._wlgrid = self._wlgrid[sortedwl] self._wngrid = 10000 / self._wlgrid self._noise = self._spectrum[sortedwl, 1] try: self._wlwidths = self._spectrum[sortedwl, 2] except IndexError: from taurex.util.util import compute_bin_edges self._wlwidths - compute_bin_edges(self._wlgrid)[-1] self.create_wn_widths() self._binner = FluxBinner(self._wngrid, wngrid_width=self._wnwidths) def create_wn_widths(self): self._wnwidths = wnwidth_to_wlwidth(self._wlgrid, self._wlwidths) def model_noise(self, model, model_res=None, num_observations=1): if model_res is None: model_res = model.model() wngrid, spectrum, error, grid_width = self._binner.bin_model(model_res) return wngrid, spectrum, self._noise / math.sqrt( num_observations), grid_width @classmethod def input_keywords(self): return [ 'file', 'fromfile', ]
def create_binner(self): """ Creates the appropriate binning object """ from taurex.binning import FluxBinner return FluxBinner(wngrid=self.wavenumberGrid, wngrid_width=self.binWidths)
def test_wngrid_clip(self): from taurex.util.util import clip_native_to_wngrid from taurex.binning import FluxBinner total_values = 1000 wngrid = np.linspace(100, 10000, total_values) values = np.random.rand(total_values) test_grid = wngrid[(wngrid > 4000) & (wngrid < 8000)] fb = FluxBinner(wngrid=test_grid) true = fb.bindown(wngrid, values) clipped = clip_native_to_wngrid(wngrid, test_grid) interp_values = np.interp(clipped, wngrid, values) clipped_flux = fb.bindown(clipped, interp_values) np.testing.assert_array_equal(true[1], clipped_flux[1])
class ExampleInstrument(Instrument): """ An example implementation of an instrument An instument function thats uses the WFC3 spectral grid and applies a gaussian noise with a scale ``noise_scale`` for each spectrum point. """ def __init__(self, noise_scale=1): super().__init__() self._scale = noise_scale # Wavelength and widths for WFC3 wfc3_grid = np.array([ 1.126, 1.156, 1.184, 1.212, 1.238, 1.265, 1.292, 1.318, 1.345, 1.372, 1.399, 1.428, 1.457, 1.487, 1.518, 1.551, 1.586, 1.623, ]) wfc3_wlwidths = np.array([ 3.079e-2, 2.930e-2, 2.790e-2, 2.689e-2, 2.649e-2, 2.689e-2, 2.670e-2, 2.629e-2, 2.649e-2, 2.739e-2, 2.800e-2, 2.849e-2, 2.940e-2, 3.079e-2, 3.180e-2, 3.370e-2, 3.600e-2, 3.899e-2, ]) # convert to wavenumber widths wfc3_wnwidths = wnwidth_to_wlwidth(wfc3_grid, wfc3_wlwidths) self._wfc3_size = wfc3_grid.shape[0] # Create our grid resampler self._binner = FluxBinner(wngrid=10000/wfc3_grid, wngrid_width=wfc3_wnwidths) def model_noise(self, model, model_res=None, num_observations=1): if model_res is None: model_res = model.model() # Generate our noise noise = np.random.normal(scale=self._scale, size=self._wfc3_size) # Bin down our model wngrid, depth, _, widths = self._binner.bin_model(model_res) return wngrid, depth, noise, widths
def _generic_plot(self, wlgrid, native_grid, spectra, resolution, color=None, error=False, alpha=1.0, label=None): binned_error = None if resolution is not None: from taurex.binning import FluxBinner from taurex.util.util import create_grid_res, wnwidth_to_wlwidth _grid = create_grid_res(resolution, wlgrid.min() * 0.9, wlgrid.max() * 1.1) bin_wlgrid = _grid[:, 0] bin_wngrid = 10000 / _grid[:, 0] bin_sort = bin_wngrid.argsort() bin_wlgrid = bin_wlgrid[bin_sort] bin_wngrid = bin_wngrid[bin_sort] bin_wnwidth = wnwidth_to_wlwidth(bin_wlgrid, _grid[bin_sort, 1]) wlgrid = _grid[bin_sort, 0] binner = FluxBinner(bin_wngrid, bin_wnwidth) native_spectra = spectra['native_spectrum'][...] binned_spectrum = binner.bindown(native_grid, native_spectra)[1] try: native_error = spectra['native_std'] except KeyError: native_error = None if native_error is not None: binned_error = binner.bindown(native_grid, native_error)[1] else: try: binned_spectrum = spectra['binned_spectrum'][...] except KeyError: try: binned_spectrum = spectra['bin_spectrum'][...] except KeyError: binned_spectrum = spectra['native_spectrum'][...] try: binned_error = spectra['binned_std'][...] except KeyError: binned_error = None plt.plot(wlgrid, binned_spectrum, label=label, alpha=alpha) if binned_error is not None: plt.fill_between(wlgrid, binned_spectrum - binned_error, binned_spectrum + binned_error, alpha=0.5, zorder=-2, color=color, edgecolor='none') # 2 sigma plt.fill_between(wlgrid, binned_spectrum - 2 * binned_error, binned_spectrum + 2 * binned_error, alpha=0.2, zorder=-3, color=color, edgecolor='none')
# ============================================================================= data = ascii.read("0000_HD 209458 b.csv") wlgrid0, noise0 = (np.array([]) for k in range(2)) for i in range(len(data)): wlgrid0 = np.append(wlgrid0, data[i][1]) noise0 = np.append(noise0, data[i][6]) # noise on transit floor ###################### NUMBER OF TRANSITS ####################### num_transits = 1 noise0 = noise0 / np.sqrt(num_transits) ################################################################# from taurex.binning import FluxBinner wngrid3 = 10000 / wlgrid0 wngrid3 = np.sort(wngrid3) bn = FluxBinner(wngrid=wngrid3) bin_wn, bin_rprs3, _, _ = bn.bin_model(tm.model(wngrid=wngrid3)) x = np.flip(10000 / bin_wn) y = np.flip(bin_rprs3) #%% # ============================================================================= # BINDOWN FROM TIER3 TO TIER2 # ============================================================================= # Load the spectral data, the first column is wavelength, second flux density and third flux density uncertainty tier2file = np.loadtxt('tier2-grid.txt') wl = x wlbinned = tier2file[:, 0] spectrum = y