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

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


Attributes
----------

.. autoapisummary::

   petitRADTRANS.chemistry.utils._solar_elemental_abundances


Functions
---------

.. autoapisummary::

   petitRADTRANS.chemistry.utils._compute_h_ratios
   petitRADTRANS.chemistry.utils._compute_z_ratios
   petitRADTRANS.chemistry.utils.abundance_curvature_prior
   petitRADTRANS.chemistry.utils.compute_pressure_nodes
   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.define_abundance_node_list
   petitRADTRANS.chemistry.utils.define_pressure_node_list
   petitRADTRANS.chemistry.utils.fill_atmosphere
   petitRADTRANS.chemistry.utils.fill_atmospheric_layer
   petitRADTRANS.chemistry.utils.fixed_length_amr
   petitRADTRANS.chemistry.utils.get_solar_elemental_abundances
   petitRADTRANS.chemistry.utils.linear_spline_profile
   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: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:: compute_pressure_nodes(pressure_array, mode='even', nnodes: int | None = 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:: compute_mean_molar_masses(abundances: dict[str, numpy.typing.NDArray], 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'.

   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, nnodes=0)

   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.
       nnodes (int, optional): Number of nodes to use in the spline interpolation.

   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:: 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) -> numpy.typing.NDArray[numpy.floating]

   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, numpy.typing.NDArray[numpy.floating]], filling_species: dict, fill_layer: int = 'all') -> dict[str, numpy.typing.NDArray[numpy.floating]]

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

   See fill_atmospheric_layer for more details.

   Args:
       mass_fractions:
           Dictionary with the species as keys and the mass fraction in all layers as values.
       filling_species:
           Dictionary with the filling species as keys and the weights of the mass fractions as values. Unweighted
           filling species are represented with None.
       fill_layer:
           If 'all', fill all layers and return the filled mass fractions at every layer.
           If it is an integer, fill only the corresponding layer and return the mass fractions at that layer only.
   Returns:
       A dictionary of the mass fractions with the filling species. The sum of the mass fractions is 1.


.. py:function:: fill_atmospheric_layer(mass_fractions: dict[str, float], filling_species: dict, sanity_check: bool = False) -> dict[str, float]

   Fill an atmospheric layer with filling species, so that the sum of the mass fractions is 1.
   The filling species values are weights that are used to fill the 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.
   Mixing weighted and unweighted filling species raises a ValueError.

   Args:
       mass_fractions:
           Dictionary with the species as keys and the mass fraction in one layer as values.
       filling_species:
           Dictionary with the filling species as keys and the weights of the mass fractions as values. Unweighted
           filling species are represented with None.
       sanity_check:
           If True, make a sanity check to ensure that the filling was consistently done.
   Returns:
       A dictionary of the mass fractions with the filling species. The sum of the mass fractions is 1.


.. py:function:: fixed_length_amr(p_clouds, pressures, scaling=10, width=3)

   This function takes in the cloud base pressures for each cloud,
   and returns an array of pressures with a high resolution mesh
   in the region where the clouds are located.

   Author:  Francois Rozet.

   The output length is always
       len(pressures[::scaling]) + len(p_clouds) * width * (scaling - 1)

   Args:
       p_clouds : numpy.ndarray
           The cloud base pressures in bar
       pressures : np.ndarray
           The high resolution pressure array.
       scaling : int
           The factor by which the low resolution pressure array is scaled
       width : int
           The number of low resolution bins to be replaced for each cloud layer.


.. 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, nnodes=0)

   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.
       nnodes (int, optional): Number of nodes to use in the spline interpolation.

   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:: 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, numpy.typing.NDArray[numpy.floating]], mean_molar_masses: numpy.typing.NDArray[numpy.floating], 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, along with a curvature prior.

   Args:
       pressure_array (array-like): An array or list of pressure levels which is used to calculate the spectrum.
       transition_pressures: # TODO complete docstring
       abundance_points (array-like): An array or list of abundances for each pressure node.

   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:: 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, numpy.typing.NDArray[numpy.floating]], neglect_he: bool = False, only_atmospheric_species_for_solar_metallicity: bool = False) -> tuple[float, dict[int, dict[str, str]]]

   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)


