Caching Modules (taurex.cache)

Singleton

Just contains a singleton class. Pretty useful

class Singleton(*args, **kwds)[source]

Bases: object

A singleton for your usage.

When inheriting do not implement __init__ instead override init().

init(*args, **kwds)[source]

Override to act as an init

OpacityCache

Contains caching class for Molecular cross section files.

class OpacityCache(*args, **kwds)[source]

Bases: Singleton

Implements a lazy load of opacities.

A singleton thatloads and caches xsections as they are needed. Calling:

>>> opt = OpacityCache()
... opt2 = OpacityCache()

Reveals that:

>>> opt == opt2
True

Importantly this class will automatically search directories for cross-sections set using the set_opacity_path() method:

>>> opt.set_opacity_path('path/to/crossections')

Multiple paths can be set as well

>>> opt.set_opacity_path(
... ['/path/to/crosssections','/another/path/to/crosssections'])

To get the cross-section object for a particular molecule use the square bracket operator:

>>> opt['H2O']
<taurex.opacity.pickleopacity.PickleOpacity at 0x107a60be0>

This returns a Opacity object for you to compute H2O cross sections from. When called for the first time, a directory search is performed and, if found, the appropriate cross-section is loaded. Subsequent calls will immediately return the already loaded object:

>>> h2o_a = opt['H2O']
>>> h2o_b = opt['H2O']
>>> h2o_a == h2o_b
True

If you have any plugins that include new opacity formats, the cache will automatically detect them.

Lastly you can manually add an opacity directly for a molecule into the cache:

>>> new_h2o = MyNewOpacityFormat()
>>> new_h2o.molecule
H2O
>>> opt.add_opacity(new_h2o)
>>> opt['H2O']
<MyNewOpacityFormat at 0x107a60be0>

Now TauREx3 will use it instead in all calculations!

add_opacity(opacity: object, molecule_filter: List[str] | None = None)[source]

Adds a Opacity object to the cache.

Parameters:
  • opacity (Opacity) – Opacity object to add to the cache

  • molecule_filter (list of str , optional) – If provided, the opacity object will only be included if its molecule is in the list. Mostly used by the __getitem__() for filtering

clear_cache() None[source]

Clears all currently loaded cross-sections.

enable_radis(enable: bool) None[source]

Enables/Disables use of RADIS to fill in missing molecules.

RADIS fills in missing molecules using HITRAN.

Warning

This is extremely unstable and crashes frequently. It is also very slow as it requires the computation of the Voigt profile for every temperature. We recommend leaving it as False unless necessary.

Parameters:

enable (bool) – Whether to enable RADIS functionality (default = False)

find_list_of_molecules() None[source]

Find molecules available to load.

force_active(molecules: List[str])[source]

Allows some molecules to be forced as active.

Useful when using other radiative codes to do the calculation

Parameters:

molecules (obj:list) – List of molecules

init()[source]

Initialise the cache.

load_opacity(opacities: object | List[object] | None = None, opacity_path: str | List[str] | None = None, molecule_filter: List[str] | None = None)[source]

Main function to use when loading molecular opacities. Handles both cross sections and paths. Handles lists of either so lists of Opacity objects or lists of paths can be used to load multiple files/objects

Parameters:

opacities (Opacity or)

:param list of Opacity: Object(s) to include in cache :param optional: Object(s) to include in cache :param opacity_path: search path(s) to look for molecular opacities :type opacity_path: str or list of str, optional :param molecule_filter: If provided, the opacity will only be loaded

if its molecule is in this list. Mostly used by the __getitem__() for filtering

load_opacity_from_path(path: str, molecule_filter: List[str] | None = None)[source]

Searches path for molecular cross-section files.

Creates and loads opacities into the cache .pickle will be loaded as PickleOpacity

Parameters:
  • path (str) – Path to search for molecular cross-section files

  • molecule_filter (list of str , optional) – If provided, the opacity will only be loaded if its molecule is in this list. Mostly used by the __getitem__() for filtering

set_interpolation(interpolation_mode: Literal['linear', 'exp'])[source]

Sets the interpolation mode for cross-sections.

Can either be linear for linear interpolation of both temeprature and pressure:

>>> OpacityCache().set_interpolation('linear')

or exp for natural exponential interpolation of temperature and linear for pressure

>>> OpacityCache().set_interpolation('exp')
Parameters:

interpolation_mode (str) – Either linear for bilinear interpolation or exp for exp-linear interpolation.

set_memory_mode(in_memory: bool) None[source]

Sets HDF5 opacity load mode.

For HDF5, whether to stream opacities from file (slower, less memory) or load them into memory (faster, more memory)

Parameters:

in_memory (bool) – Whether HDF5 files should be streamed (False) or loaded into memory (True, default)

set_opacity_path(opacity_path: str | List[str]) None[source]

Set the path(s) that will be searched for opacities.

Opacities in this path must be of supported types:

  • HDF5 opacities

  • .pickle opacities

  • ExoTransmit opacities.

Parameters:

opacity_path (str or list of str, optional) – search path(s) to look for molecular opacities

Raises:

NotADirectoryError – If (any) path does not exist

set_radis_wavenumber(wn_start: float, wn_end: float, wn_points: int) None[source]

Sets the wavenumber grid for RADIS.

CIACache

Contains caching class for Collisionally Induced Absorption files.

class CIACache(*args, **kwds)[source]

Bases: Singleton

Implements a lazy load of collisionally induced absorpiton cross-sections Supports pickle files and HITRAN cia files. Functionally behaves the same as OpacityCache except the keys are now cia pairs e.g:

>>> CIACache()['H2-H2']
<taurex.cia.picklecia.PickleCIA at 0x107a60be0>

Pickle .db and HITRAN .cia files are supported and automatically loaded. with priority given to .db files

add_cia(cia: CIA, pair_filter: List[str] | None = None)[source]

Adds a CIA object to the cache to then be used by Taurex 3

Parameters:
  • cia (CIA) – CIA object to add to the cache

  • pair_filter (list of str , optional) – If provided, the cia object will only be included if its pairname is in the list. Mostly used by the __getitem__() for filtering

init()[source]

Override to act as an init

load_cia(cia_xsec: CIA | List[CIA] | None = None, cia_path: str | List[str] | None = None, pair_filter: List[str] | None = None)[source]

Main function to use when loading CIA files. Handles both cross sections and paths. Handles lists of either so lists of CIA objects or lists of paths can be used to load multiple files/objects

Parameters:

cia_xsec (CIA or)

:param list of CIA: Object(s) to include in cache :param optional: Object(s) to include in cache :param cia_path: search path(s) to look for cias :type cia_path: str or list of str, optional :param pair_filter: If provided, the cia will only be loaded

if its pair name is in this list. Mostly used by the __getitem__() for filtering

load_cia_from_path(path: str, pair_filter: List[str] | None = None)[source]

Searches path for CIA files, creates and loads them into the cache .db will be loaded as PickleCIA and .cia files will be loaded as HitranCIA

Parameters:
  • path (str) – Path to search for CIA files

  • pair_filter (list of str , optional) – If provided, the cia will only be loaded if its pairname is in the list. Mostly used by the __getitem__() for filtering

set_cia_path(cia_path: str)[source]

Sets the path to search for CIA files

Parameters:

cia_path (str or list of str) – Either a single path or a list of paths that contain CIA files