petitRADTRANS.chemistry.utils
=============================

.. py:module:: petitRADTRANS.chemistry.utils


Attributes
----------

.. autoapisummary::

   petitRADTRANS.chemistry.utils._solar_elemental_abundances


Functions
---------

.. autoapisummary::

   petitRADTRANS.chemistry.utils.build_adaptive_pressure_grid
   petitRADTRANS.chemistry.utils._compute_h_ratios
   petitRADTRANS.chemistry.utils._compute_z_ratios
   petitRADTRANS.chemistry.utils.abundance_curvature_prior
   petitRADTRANS.chemistry.utils.get_parameter_value
   petitRADTRANS.chemistry.utils.compute_pressure_nodes
   petitRADTRANS.chemistry.utils._get_cached_species_molar_mass
   petitRADTRANS.chemistry.utils.compute_mean_molar_masses
   petitRADTRANS.chemistry.utils.compute_mean_molar_masses_from_volume_mixing_ratios
   petitRADTRANS.chemistry.utils.cubic_spline_profile
   petitRADTRANS.chemistry.utils.build_abundance_profile
   petitRADTRANS.chemistry.utils.define_abundance_node_list
   petitRADTRANS.chemistry.utils.define_pressure_node_list
   petitRADTRANS.chemistry.utils.fill_atmosphere
   petitRADTRANS.chemistry.utils.get_solar_elemental_abundances
   petitRADTRANS.chemistry.utils.linear_spline_profile
   petitRADTRANS.chemistry.utils.get_base_species_mass_fractions
   petitRADTRANS.chemistry.utils.mass_fractions2volume_mixing_ratios
   petitRADTRANS.chemistry.utils.mass_fractions2metallicity
   petitRADTRANS.chemistry.utils.simplify_species_list
   petitRADTRANS.chemistry.utils.stepped_profile
   petitRADTRANS.chemistry.utils.volume_mixing_ratios2mass_fractions
   petitRADTRANS.chemistry.utils.volume_mixing_ratios2metallicity


Module Contents
---------------

.. py:function:: build_adaptive_pressure_grid(pressures: jax.typing.ArrayLike, cloud_base_pressures: jax.typing.ArrayLike, refinement_strength: float = 4.0, refinement_width_in_cells: float = 3.0)

   Redistribute a fixed number of pressure layers around cloud bases.

   The returned pressure grid preserves both the number of layers and the
   pressure bounds of the input grid. Instead of inserting extra layers, the
   method smoothly compresses the log-pressure coordinate near the supplied
   cloud-base pressures so more points land where cloud placement matters.

   Args:
       pressures:
           Monotonic pressure grid in bar.
       cloud_base_pressures:
           Cloud-base pressures in bar. Invalid or out-of-range values are
           ignored.
       refinement_strength:
           Dimensionless multiplier controlling how strongly the grid is
           compressed near cloud bases.
       refinement_width_in_cells:
           Approximate width of the refinement region measured in units of the
           original log-pressure cell width.

   Returns:
       Pressure grid in bar with the same shape as ``pressures``.


.. py:data:: _solar_elemental_abundances
   :value: Multiline-String

   .. raw:: html

      <details><summary>Show Value</summary>

   .. code-block:: python

      """
      1 12.00 0.00
      2 10.924 0.02
      3 3.27 0.03
      4 1.31 0.04
      5 2.77 0.03
      6 8.47 0.06
      7 7.85 0.12
      8 8.73 0.07
      9 4.61 0.09
      10 8.15 0.10
      11 6.27 0.03
      12 7.52 0.02
      13 6.42 0.03
      14 7.51 0.01
      15 5.43 0.03
      16 7.15 0.03
      17 5.23 0.06
      18 6.50 0.10
      19 5.07 0.02
      20 6.27 0.03
      21 3.04 0.03
      22 4.90 0.03
      23 3.95 0.03
      24 5.63 0.02
      25 5.47 0.03
      26 7.45 0.02
      27 4.86 0.02
      28 6.20 0.03
      29 4.24 0.04
      30 4.61 0.60
      31 3.07 0.02
      32 3.59 0.03
      33 2.29 0.03
      34 3.34 0.03
      35 2.60 0.09
      36 3.22 0.08
      37 2.36 0.03
      38 2.88 0.02
      39 2.15 0.02
      40 2.55 0.04
      41 1.4 0.04
      42 1.92 0.04
      44 1.77 0.01
      45 1.04 0.02
      46 1.65 0.02
      47 1.21 0.02
      48 1.71 0.02
      49 0.76 0.02
      50 2.07 0.03
      51 1.06 0.05
      52 2.18 0.02
      53 1.71 0.15
      54 2.25 0.08
      55 1.08 0.02
      56 2.17 0.02
      57 1.17 0.02
      58 1.58 0.02
      59 0.75 0.01
      60 1.45 0.01
      62 0.94 0.02
      63 0.51 0.02
      64 1.05 0.02
      65 0.31 0.02
      66 1.12 0.02
      67 0.46 0.02
      68 0.92 0.02
      69 0.11 0.02
      70 0.91 0.02
      71 0.09 0.02
      72 0.70 0.03
      73 -0.16 0.02
      74 0.67 0.04
      75 0.23 0.03
      76 1.33 0.03
      77 1.31 0.02
      78 1.60 0.03
      79 0.80 0.03
      80 1.08 0.15
      81 0.76 0.04
      82 2.03 0.03
      83 0.66 0.03
      90 0.04 0.02
      92 -0.54 0.03
      """

   .. raw:: html

      </details>



.. py:function:: _compute_h_ratios(elemental_abundances: dict[int, float]) -> dict[int, float]

   Calculate the ratio over the hydrogen elemental abundance of other elements.


.. py:function:: _compute_z_ratios(elemental_abundances: dict[int, float], sort: bool = True, neglect_he: bool = False) -> float

   Calculate the metal to non-metal abundances from elemental abundances.


.. py:function:: abundance_curvature_prior(press, abundance, gamma)

   Compute a curvature prior for an abundance-pressure profile.

   This function calculates a curvature prior for an abundance-pressure profile,
   penalizing deviations from a smooth, low-curvature profile, based on Line 2015

   Args:
       press (array-like): An array or list of pressure data points.
       abundance (array-like): An array or list of abundance data points.
       gamma (float): The curvature penalization factor.

   Returns:
       float: The curvature prior value.


.. py:function:: get_parameter_value(parameter)

.. py:function:: compute_pressure_nodes(pressure_array, mode='even', nnodes=0, points_list=None)

   Compute the location of nodes for a spline or step profile in pressure space.

   If mode is even, the nodes are evenly spaced in log pressure, with a total of
   nnodes + 2 points (including the top and bottom of the atmospheres).
   If mode is relative, then points list is returned.

   Args:
       pressure_array (array-like): An array or list of pressure levels which is used to calculate the spectrum.
       mode (std): 'even' (evenly spaced), 'relative' (points list is monotonically decreasing in log P) or 'set'
           (points list is user-defined spline nodes)
       nnodes (int): Number of intermediate spline nodes (not counting top and bottom of atmosphere)
       points_list (array_like): if relative, list of differences in log P between each node. If set.
           then this is a user-defined list of spline points, and MUST include top and bottom pressure
           values.

   Returns:
       array: an array of pressure nodes.


.. py:function:: _get_cached_species_molar_mass(species: str) -> float

.. py:function:: compute_mean_molar_masses(abundances: dict[str, jax.typing.ArrayLike], mode: str = 'mmr')

   Calculate the mean molecular weight in each layer, from mass fractions or volume mixing ratios.

   The calculation is slightly different for mass fractions and volume mixing ratios. By default, this function assumes
   that the abundances are mass fractions. To calculate the mean molar masses from volume mixing ratios, set the 'mode'
   argument to 'vmr'.

   This function is JIT-compatible.

   Args:
       abundances : dict
           Dictionary of mass fraction or volume mixing ratio arrays, each array must have the shape of the pressure
           array used in pRT, and contain the abundance at each layer in the atmosphere.
       mode : str
           Can be 'mmr' or 'vmr', to calculate the mean molar masses from mass fraction or volume mixing ratios,
           respectively.


.. py:function:: compute_mean_molar_masses_from_volume_mixing_ratios(volume_mixing_ratios)

   Calculate the mean molecular weight in each layer from volume mixing ratios.

   Args:
       volume_mixing_ratios : dict
           dictionary of volume mixing ratios arrays, each array must have the shape of the pressure array used in pRT,
           and contain the abundance at each layer in the atmosphere.


.. py:function:: cubic_spline_profile(pressure_array, pressure_nodes, abundance_points, gamma)

   Compute a cubic spline profile for abundance based on pressure points.

   This function computes a cubic spline profile for abundance using
   pressure and abundance data points, along with a curvature prior.

   Args:
       pressure_array (array-like): An array or list of pressure levels which is used to calculate the spectrum.
       pressure_nodes (array-like): An array or list of log pressure points at which the spline is fixed
       abundance_points (array-like): An array or list of abundances for each pressure node.
       gamma (float): A parameter controlling the curvature of the spline.

   Returns:
       tuple: A tuple containing two elements:
           - interpolated_temps (array-like): Interpolated abundance values
             based on the cubic spline.
           - prior (array-like): Curvature prior values calculated for the spline.


.. py:function:: build_abundance_profile(pressure_array, pressure_nodes, abundance_points, profile_mode='linear', gamma=0.04)

.. py:function:: define_abundance_node_list(species_short_name, parameters)

   Define the location of pressure nodes, allowing individual nodes to be
   retrieved parameters.

   If mode is even, the nodes are evenly spaced in log pressure, with a total of
   nnodes + 2 points (including the top and bottom of the atmospheres).
   If mode is relative, then points list is returned.

   Args:
       species_short_name (str): which molecular species does this apply to (each can be unique)
       parameters (dict): dictionary of parameters used in a retrieval
   Returns:
       array: an array of pressure nodes


.. py:function:: define_pressure_node_list(pressure_array, species_short_name, parameters)

   Define the location of pressure nodes, allowing individual nodes to be
   retrieved parameters.

   If mode is even, the nodes are evenly spaced in log pressure, with a total of
   nnodes + 2 points (including the top and bottom of the atmospheres).
   If mode is relative, then points list is returned.

   Args:
       pressure_array (array-like): An array or list of pressure levels which is used to calculate the spectrum.
       species_short_name (str): which molecular species does this apply to (each can be unique)
       parameters (dict): dictionary of parameters used in a retrieval
   Returns:
       array: an array of pressure nodes, can be used in calculate_pressure_nodes


.. py:function:: fill_atmosphere(mass_fractions: dict[str, jax.typing.ArrayLike], filling_species: dict, mass_fraction_sum: jax.typing.ArrayLike) -> tuple[dict[str, jax.typing.ArrayLike], jax.typing.ArrayLike]

   Fill an atmosphere with filling species, so that the sum of the mass fractions in all layers is 1.

   The filling species values are weights that are used to fill each atmospheric layer following:
       X_i = w_i / sum(w) * X_f,
   where X_i is the mass fraction of filling species i, w_i is the weight of species i, and X_f is the amount
   of mass fraction missing to get a sum of 1.

   The value X_f is calculated from:
       X_f = 1 - sum(X_nf),
   where X_nf is the sum of mass fractions that are not filling species.

   The filling species can be "weighted" (if the weights are all floats) or "unweighted" (if the weights are all None).
   In weighted mode, w corresponds to the values of `filling_species`.
   In unweighted mode, the mass fractions are used to calculate the weights. All the filling species must be in the
   mass fractions.

   Args:
       mass_fractions: dict
           Dictionary with the species as keys and the mass fraction in all layers as values.
       filling_species: dict
           Dictionary with the filling species as keys and the weights of the mass fractions as values. Unweighted
           filling species are represented with None.
       mass_fraction_sum: ArrayLike
           The sum of the trace species mass fractions in each layer.
   Returns:
       A dictionary of the mass fractions with the filling species. The sum of the mass fractions is 1.


.. py:function:: get_solar_elemental_abundances(keep_list: list[int] = None) -> dict[int, float]

   Parse the solar elemental abundances string.
   Args:
       keep_list:
           List of atomic numbers to keep in the returned dictionary. If None, all atomic numbers are kept.


.. py:function:: linear_spline_profile(pressure_array, pressure_nodes, abundance_points, gamma)

   Compute a linear spline profile for abundance based on pressure points.

   This function computes a linear spline profile for abundance using
   pressure and abundance data points, along with a curvature prior.

   Args:
       pressure_array (array-like): An array or list of pressure levels which is used to calculate the spectrum.
       pressure_nodes (array-like): An array or list of log pressure points at which the spline is fixed
       abundance_points (array-like): An array or list of abundances for each pressure node.
       gamma (float): A parameter controlling the curvature of the spline.

   Returns:
       tuple: A tuple containing two elements:
           - interpolated_temps (array-like): Interpolated abundance values
             based on the linear spline.
           - prior (array-like): Curvature prior values calculated for the spline.


.. py:function:: get_base_species_mass_fractions(mass_fractions: dict, species_basenames: tuple) -> dict

.. py:function:: mass_fractions2volume_mixing_ratios(mass_fractions, mean_molar_masses=None)

   Convert mass fractions to volume mixing ratios.

   Args:
       mass_fractions : dict
           A dictionary of mass fractions
       mean_molar_masses : numpy.ndarray
           An array containing all mass fractions at each pressure level


.. py:function:: mass_fractions2metallicity(mass_fractions: dict[str, jax.typing.ArrayLike], mean_molar_masses: jax.typing.ArrayLike, neglect_he: bool = False, only_atmospheric_species_for_solar_metallicity: bool = False)

   Calculate the metallicity and element-over-hydrogen abundance ratios.

   Args:
       mass_fractions:
           Dictionary of mass fractions for all atmospheric species.
           Dictionary keys are the species names. Values are the mass fractions of each species at each layer.
       mean_molar_masses:
           The atmospheric mean molecular weight in amu, at each atmospheric layer.
       neglect_he:
           If True, helium will be neglected as a non-metal in the metallicity calculation,
           such that everything is defined with respect to hydrogen.
       only_atmospheric_species_for_solar_metallicity:
           If True, only the species also present in the planet atmosphere will be used to calculate the solar
           metallicity. If False, all species present in the sun will be used to calculate the solar metallicity.
   Returns:
       The atmospheric metallicity and a dictionary containing element-over-hydrogen abundance ratios.
       The dictionary keys are the elements atomic numbers. Values are dictionaries containing the plain-text H ratio,
       the atmospheric H ratio ('local' key), and its value relative to the solar H ratio ('relative to solar' key)


.. py:function:: simplify_species_list(species_list: list) -> list

.. py:function:: stepped_profile(pressure_array, transition_pressures, abundance_points)

   Compute a stepped profile for abundance based on pressure points.

   This function computes the steps for chemical abundance using
   pressure and abundance data points.

   Args:
       pressure_array (array-like): An array or list of pressure levels which is used to calculate the spectrum.
       transition_pressures (array-like): An array of transition pressures in log10 space, sorted from low to high
                                          pressure. It should include the top and bottom of the atmosphere.
       abundance_points (array-like): An array or list of abundances for each pressure interval. A legacy
           length matching transition_pressures is also accepted; in that case the last value is used only at the
           bottom boundary.

   Returns:
       array-like: An array of abundance values corresponding to the pressure_array.


.. py:function:: volume_mixing_ratios2mass_fractions(volume_mixing_ratios, mean_molar_masses=None)

   Convert volume mixing ratios to mass fractions.

   Args:
       volume_mixing_ratios : dict
           A dictionary of volume mixing ratios
       mean_molar_masses : numpy.ndarray
           An array containing the mean molecular weight at each pressure level


.. py:function:: volume_mixing_ratios2metallicity(volume_mixing_ratios: dict[str, jax.typing.ArrayLike], neglect_he: bool = False, only_atmospheric_species_for_solar_metallicity: bool = False)

   Calculate the metallicity and element-over-hydrogen abundance ratios.

   Args:
       volume_mixing_ratios:
           Dictionary of volume mixing ratios for all atmospheric species.
           Dictionary keys are the species names. Values are the VMR of each species at each layer.
       neglect_he:
           If True, helium will be neglected as a non-metal in the metallicity calculation,
           such that everything is defined with respect to hydrogen.
       only_atmospheric_species_for_solar_metallicity:
           If True, only the species also present in the planet atmosphere will be used to calculate the solar
           metallicity. If False, all species present in the sun will be used to calculate the solar metallicity.
   Returns:
       The atmospheric metallicity and a dictionary containing element-over-hydrogen abundance ratios.
       The dictionary keys are the elements atomic numbers. Values are dictionaries containing the plain-text H ratio,
       the atmospheric H ratio ('local' key), and its value relative to the solar H ratio ('relative to solar' key)


