"""Spectra loaded from an array."""importtypingastimportnumpyasnpimportnumpy.typingasnptfromtaurex.utilimportwnwidth_to_wlwidthfrom.spectrumimportBaseSpectrum
[docs]classArraySpectrum(BaseSpectrum):"""Loads an observed spectrum from an array Loads spectrum and computes bin edges and bin widths. Spectrum shape(nbins, 3-4) with 3-4 columns with ordering: 1. wavelength (um) 2. spectral data 3. error 4. (optional) bin width If no bin width is present then they are computed. """def__init__(self,spectrum:t.Optional[npt.NDArray[np.float64]]=None):"""Initialize. Parameters ----------- spectrum: Array of shape (nbins, 3-4) with 3-4 columns, with ordering: 1. wavelength (um) 2. spectral data 3. error 4. (optional) bin width """super().__init__(self.__class__.__name__)self._obs_spectrum=spectrumself._bin_widths=Noneself._bin_edges=Noneself._sort_spectrum()self._process_spectrum()self._wnwidths=wnwidth_to_wlwidth(self.wavelengthGrid,self._bin_widths)def_sort_spectrum(self)->None:"""Sorts the spectrum by wavelength"""self._obs_spectrum=self._obs_spectrum[self._obs_spectrum[:,0].argsort(axis=0)[::-1]]def_process_spectrum(self)->None:"""Seperate out the observed data, error, grid and binwidths If bin widths are not present then they are calculated here """ifself.rawData.shape[1]==4:self._bin_widths=self._obs_spectrum[:,3]obs_wl=self.wavelengthGrid[::-1]obs_bw=self._bin_widths[::-1]bin_edges=np.zeros(shape=(len(self._bin_widths)*2,))bin_edges[0::2]=obs_wl-obs_bw/2bin_edges[1::2]=obs_wl+obs_bw/2# bin_edges[-1] = obs_wl[-1]-obs_bw[-1]/2.self._bin_edges=bin_edges[::-1]else:self.manual_binning()@propertydefrawData(self)->npt.NDArray[np.float64]:# noqa: N802"""Data read from file."""returnself._obs_spectrum@propertydefspectrum(self)->npt.NDArray[np.float64]:"""The spectrum itself."""returnself._obs_spectrum[:,1]@propertydefwavelengthGrid(self)->npt.NDArray[np.float64]:# noqa: N802"""Wavelength grid in microns."""returnself.rawData[:,0]@propertydefwavenumberGrid(self)->npt.NDArray[np.float64]:# noqa: N802"""Wavenumber grid in cm-1"""return10000/self.wavelengthGrid@propertydefbinEdges(self)->npt.NDArray[np.float64]:# noqa: N802"""Bin edges."""return10000/self._bin_edges@propertydefbinWidths(self)->npt.NDArray[np.float64]:# noqa: N802"""bin widths."""returnself._wnwidths@propertydeferrorBar(self)->npt.NDArray[np.float64]:# noqa: N802"""Error bars for the spectrum."""returnself.rawData[:,2]
[docs]defmanual_binning(self)->None:"""Performs the calculation of bin edges when none are present."""fromtaurex.utilimportcompute_bin_edgesself._bin_edges,self._bin_widths=compute_bin_edges(self.wavelengthGrid)
[docs]@classmethoddefinput_keywords(cls)->t.Tuple[str,...]:"""Input keywords for this class."""return("array",)