"""Opacities loaded from pickle."""importpathlibimportpickle# noqaimporttypingastimportnumpyasnpimportnumpy.typingasnptfromtaurex.mpiimportallocate_as_sharedfromtaurex.typesimportPathLikefromtaurex.utilimportsanitize_molecule_stringfrom.interpolateopacityimportInterpModeType,InterpolatingOpacity
[docs]classPickleOpacity(InterpolatingOpacity):"""Class for loading opacities from pickle files. This was the original format for TauREx 1/2 opacities. It is now deprecated in favour of HDF5 files. """
[docs]@classmethoddefdiscover(cls)->t.List[t.Tuple[str,t.Tuple[pathlib.Path,str]]]:"""Discover opacities from pickle files in path."""fromtaurex.cacheimportGlobalCachefromtaurex.utilimportsanitize_molecule_stringpath=GlobalCache()["xsec_path"]ifpathisNone:return[]path=pathlib.Path(path)files=path.glob("*.pickle")discovery=[]interp=GlobalCache()["xsec_interpolation"]or"linear"forfinfiles:splits=f.stem.split(".")mol_name=sanitize_molecule_string(splits[0])discovery.append((mol_name,[f,interp]))returndiscovery
def__init__(self,filename:PathLike,interpolation_mode:t.Optional[InterpModeType]="linear",)->None:"""Initialize and load pickle file. Parameters ---------- filename: PathLike Path to pickle file interpolation_mode: Interpolation mode, by default "linear" Raises ------ FileNotFoundError If file is not found """filename=pathlib.Path(filename)super().__init__(f"PickleOpacity:{filename.stem[0:10]}",interpolation_mode=interpolation_mode,)importwarningswarnings.warn("PickleOpacity is deprecated and will be removed"" in a future major version. ""Please use HDF5Opacity instead.",DeprecationWarning,stacklevel=2,)ifnotfilename.exists():raiseFileNotFoundError(f"File {filename} does not exist")self._filename=filenameself._molecule_name=Noneself._spec_dict=Noneself._resolution=Noneself._load_pickle_file(filename)@propertydefmoleculeName(self)->str:# noqa: N802"""Molecule name."""returnself._molecule_name@propertydefxsecGrid(self)->npt.NDArray[np.float64]:# noqa: N802"""Opacity grid."""returnself._xsec_griddef_load_pickle_file(self,filename:pathlib.Path)->None:"""Load pickle file into memory."""# Load the pickle fileself.info(f"Loading opacity from {filename}")try:withopen(filename,"rb")asf:self._spec_dict=pickle.load(f)# noqaexceptUnicodeDecodeError:withopen(filename,"rb")asf:self._spec_dict=pickle.load(f,encoding="latin1")# noqaself._wavenumber_grid=self._spec_dict["wno"]self._temperature_grid=self._spec_dict["t"]self._pressure_grid=self._spec_dict["p"]*1e5self._xsec_grid=allocate_as_shared(self._spec_dict["xsecarr"],logger=self)self._resolution=np.average(np.diff(self._wavenumber_grid))splits=filename.stem.split(".")mol_name=sanitize_molecule_string(splits[0])self._molecule_name=mol_nameself._min_pressure=self._pressure_grid.min()self._max_pressure=self._pressure_grid.max()self._min_temperature=self._temperature_grid.min()self._max_temperature=self._temperature_grid.max()self.clean_molecule_name()
[docs]defclean_molecule_name(self)->None:"""Clean molecule name from underscores used."""splits=self.moleculeName.split("_")self._molecule_name=splits[0]