"""Handling of .pickle/.db CIA files."""importpickle# noqa: S403importtypingastfrompathlibimportPathimportnumpyasnpimportnumpy.typingasnptfromtaurex.util.mathimportinterp_lin_onlyfrom.ciaimportCIA
[docs]classPickleCIA(CIA):""" Class for using pickled (``.db``) collisionally induced absorptions Very simple since the format is simple Parameters ---------- filename : str Path to pickle pair_name : str , optional Whilst the name of the pair is determined by the pickle filename since these can be different you can optionally force the name through this parameter """def__init__(self,filename:str,pair_name:t.Optional[np.float64]=None):ifpair_nameisNone:pair_name=Path(filename).stemsuper().__init__("PickleCIA",pair_name)self._filename=filenameself._molecule_name=Noneself._spec_dict=Noneself._load_pickle_file(filename)def_load_pickle_file(self,filename:str)->None:"""Loads pickle file. Parameters ---------- filename : str Path to pickle cia file """# Load the pickle fileself.info("Loading cia cross section from %s",filename)withopen(filename,"rb")asf:self._spec_dict=pickle.load(f,encoding="latin1")# noqa: S301self._wavenumber_grid=self._spec_dict["wno"]self._temperature_grid=self._spec_dict["t"]self._xsec_grid=self._spec_dict["xsecarr"]@propertydefwavenumberGrid(self)->npt.NDArray[np.float64]:# noqa: N802""" Returns ------- :obj:`array` Native wavenumber grid """returnself._wavenumber_grid@propertydeftemperatureGrid(self)->npt.NDArray[np.float64]:# noqa: N802""" Returns ------- :obj:`array` Native temperature grid in Kelvin """returnself._temperature_grid
[docs]deffind_closest_temperature_index(self,temperature:float)->t.Tuple[int,int]:""" Finds the nearest indices for a particular temperature Parameters ---------- temperature : float Temeprature in Kelvin Returns ------- t_min : int index on temprature grid to the left of ``temperature`` t_max : int index on temprature grid to the right of ``temperature`` """fromtaurex.utilimportfind_closest_pairt_min,t_max=find_closest_pair(self.temperatureGrid,temperature)returnt_min,t_max
[docs]definterp_linear_grid(self,temperature:float,t_idx_min:int,t_idx_max:int)->float:""" For a given temperature and indicies. Interpolate the cross-sections linearly from temperature grid to temperature ``T`` Parameters ---------- temperature : float Temeprature in Kelvin t_min : int index on temprature grid to the left of ``temperature`` t_max : int index on temprature grid to the right of ``temperature`` Returns ------- out : :obj:`array` Interpolated cross-section """iftemperature>self._temperature_grid.max():returnself._xsec_grid[-1]eliftemperature<self._temperature_grid.min():returnself._xsec_grid[0]temp_max=self._temperature_grid[t_idx_max]temp_min=self._temperature_grid[t_idx_min]fx0=self._xsec_grid[t_idx_min]fx1=self._xsec_grid[t_idx_max]returninterp_lin_only(fx0,fx1,temperature,temp_min,temp_max)
[docs]defcompute_cia(self,temperature:float)->npt.NDArray[np.float64]:""" Computes the collisionally induced absorption cross-section using our native temperature and cross-section grids Parameters ---------- temperature : float Temperature in Kelvin Returns ------- out : :obj:`array` Temperature interpolated cross-section """indicies=self.find_closest_temperature_index(temperature)returnself.interp_linear_grid(temperature,*indicies)