petitRADTRANS.opacities.correlated_k_binning#

This module re-implements the exo-k correlated-k binning method. Numba compilation has been replaced with JAX to reduce dependency collisions. This version only contains the basic functionality necessary to bin the petitRADTRANS correlated-k tables.

# Exo_k ![Stable Version](https://img.shields.io/pypi/v/exo_k?label=Stable) ![Python Versions](https://img.shields.io/pypi/pyversions/exo_k?label=Python) [![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv) [![Taskfile](https://img.shields.io/badge/Taskfile-grey?logo=task)](https://taskfile.dev/)

Author: Jeremy Leconte (CNRS/LAB/Univ. Bordeaux)

Exo_k is a Python 3 based library to handle radiative opacities from various sources for atmospheric applications.

# Acknowledgements

If you use this library in your research, please acknowledge it by citing [Leconte (2021)](https://ui.adsabs.harvard.edu/abs/2021A%26A…645A..20L/abstract):

  • Spectral binning of precomputed correlated-k coefficients. Astronomy and Astrophysics 645. Leconte, J. 2021. doi:10.1051/0004-6361/202039040

This project has received funding from the European Research Council (ERC) under the European Union’s Horizon 2020 research and innovation programme (grant agreement n° 679030/WHIPLASH).

The framework for this documentation has been developped by Aurelien Falco using Sphinx. The Framework for automatic testing has been developped by Alexandre Mechineau.

Classes#

Spectral_object

A class with some basic functions for all objects with a spectral dimension

Data_table

An abstract class that will serve as a basis for Ktable and Xtable.

Ktable

A class that handles 4D tables of k-coefficients.

Functions#

rm_molec(unit_name)

Removes "/molecule" or "/molec" from a unit string.

rebin(f_fine, fine_grid, coarse_grid)

Computes the binned version of a function on a coarser grid.

bin_down_corrk(new_nw, kdata, old_ggrid, new_ggrid, ...)

bins down a kcoefficient table (see bin_down() for details)

rebin_ind_weights(old_bin_grid, new_bin_grid)

Computes the indices and weights to be used when

is_sorted(a)

Finds out if an array is sorted. Returns True if it is.

unit_convert(quantity[, unit_file, unit_in, unit_out])

Chooses the final unit to use and the conversion factor to apply.

Module Contents#

class petitRADTRANS.opacities.correlated_k_binning.Spectral_object#

Bases: object

A class with some basic functions for all objects with a spectral dimension

wns = None#
wnedges = None#
Nw = None#
wn_unit = 'cm^-1'#
select_spectral_range(wn_range=None, wl_range=None)#

Select spectral range, without restricting the data (but the spectral axes are modified in place). Should use either wn_range OR wl_range, not both.

To be selected, the whole bin must be inside the range.

Parameters#

wn_range: array, np.ndarray

Wavenumber range in cm^-1.

wl_range: array, np.ndarray

Wavelength range in micron.

Returns#

tuple:

iw_min, iw_max the boundary indices of the spectral range

class petitRADTRANS.opacities.correlated_k_binning.Data_table#

Bases: Spectral_object

An abstract class that will serve as a basis for Ktable and Xtable. This class includes all the interpolation and remapping methods.

__array_priority__ = 1000#
filename = None#
mol = None#
isotopolog_id = 0#
pgrid = None#
logpgrid = None#
tgrid = None#
kdata = None#
logk = None#
Np = None#
Nt = None#
Ng = None#
weights = None#
ggrid = None#
gedges = None#
DOI = 'unknown'#
sampling_method = 'unknown'#
Date_ID#
Nx = None#
p_unit = 'unspecified'#
kdata_unit = 'unspecified'#
remove_zeros(deltalog_min_value=10.0)#

Finds zeros in the kdata and set them to (10.**-deltalog_min_value) times the minimum positive value in the table (inplace).

This is to be able to work in logspace.

Parameters#

deltalog_min_value: float

finalize_init(p_unit='unspecified', file_p_unit='unspecified', kdata_unit='unspecified', file_kdata_unit='unspecified', remove_zeros=False)#

Common code at the end of the initialization of inheriting classes put here to avoid duplicates

convert_p_unit(p_unit='unspecified', file_p_unit='unspecified')#

Converts pressure to a new unit (inplace).

Parameters#

p_unit: str

String identifying the pressure units to convert to (e.g. ‘bar’, ‘Pa’, ‘mbar’, or any pressure unit recognized by the astropy.units library). If =’unspecified’, no conversion is done.

file_p_unitstr, optional

String to specify the current pressure unit if it is unspecified or if you have reasons to believe it is wrong (e.g. you just read a file where you know that the pressure grid and the pressure unit do not correspond)

convert_kdata_unit(kdata_unit='unspecified', file_kdata_unit='unspecified')#

Converts kdata to a new unit (inplace).

Parameters#

kdata_unit: str

String to identify the units to convert to. Accepts ‘cm^2’, ‘m^2’ or any surface unit recognized by the astropy.units library. If =’unspecified’, no conversion is done. In general, kdata should be kept in ‘per number’ or ‘per volume’ units (as opposed to ‘per mass’ units) as composition will always be assumed to be a number or volume mixing ratio. Opacities per unit mass are not supported yet. Note that you do not need to specify the ‘/molec’ or ‘/molecule’ in the unit.

file_kdata_unitstr

String to specify the current kdata unit if it is unspecified or if you have reasons to believe it is wrong (e.g. you just read a file where you know that the kdata grid and the kdata unit do not correspond)

class petitRADTRANS.opacities.correlated_k_binning.Ktable(filename=None, p_unit='unspecified', file_p_unit='unspecified', kdata_unit='unspecified', file_kdata_unit='unspecified', remove_zeros=True, mol=None, **kwargs)#

Bases: Data_table

A class that handles 4D tables of k-coefficients.

Based on the Data_table class that handles basic operations common to Xtable.

Based on the Ktable_io class that incorporates all io routines (read/write_xxx for all the xxx supported formats).

filename = None#
read_hdf5(filename=None, mol=None, wn_range=None, wl_range=None)#

Initializes k coeff table and supporting data from an hdf5 file (compatible with Exomol format)

Parameters#

filestr

Name of the input hdf5 file

write_hdf5_common(f, compression='gzip', compression_level=9, p_unit=None)#

Method that writes datasets and attributes that are common to X and Ktables.

Parameters#

f: h5py file instance

The file to write that is created in daughter classes

write_hdf5(filename, compression='gzip', compression_level=9, kdata_unit=None, p_unit=None, exomol_units=False)#

Saves data in a hdf5 format

Parameters#

filename: str

Name of the file to be created and saved

exomol_units: bool (optional)

If True, data are converted back to cm^2 and bar units before being written.

bin_down(wnedges=None, weights=None, ggrid=None, remove_zeros=False, num=300, use_rebin=False, write=0)#

Method to bin down a kcoeff table to a new grid of wavenumbers (inplace).

Parameters#

wnedges: array, np.ndarray

Edges of the new bins of wavenumbers (cm-1) onto which the kcoeff should be binned down. if you want Nwnew bin in the end, wnedges.size must be Nwnew+1 wnedges[0] should be greater than self.wnedges[0] (JL20 not sure anymore) wnedges[-1] should be lower than self.wnedges[-1]

weights: array, np.ndarray, optional

Desired weights for the resulting Ktable.

ggrid: array, np.ndarray, optional

Desired g-points for the resulting Ktable. Must be consistent with provided weights. If not given, they are taken at the midpoints of the array given by the cumulative sum of the weights

petitRADTRANS.opacities.correlated_k_binning.rm_molec(unit_name)#

Removes “/molecule” or “/molec” from a unit string.

Parameters#

unit_name: str

String to be changed.

Returns#

str

The unit name without the ending “/molecule” or “/molec”

petitRADTRANS.opacities.correlated_k_binning.rebin(f_fine, fine_grid, coarse_grid)#

Computes the binned version of a function on a coarser grid. The two grids do not need to have the same boundaries.

Parameters#

f_fine: array, np.ndarray

Function to be rebinned, given on the fine_grid.

fine_grid: array, np.ndarray

The high resolution grid edges we start with.

coarse_grid: array, np.ndarray

The coarser resolution grid edges inside which we want to bin the function f.

petitRADTRANS.opacities.correlated_k_binning.bin_down_corrk(new_nw, kdata, old_ggrid, new_ggrid, gedges, indicestosum, wngrid_filter, wn_weights, wn_weight_counts, num, use_rebin)#

bins down a kcoefficient table (see bin_down() for details)

Parameters#

new_nwint

Number of wavenumber bins in the rebinned table.

kdataarray, np.ndarray

table to bin down.

old_ggridarray, np.ndarray

Grid of old g-point abscissas for kdata.

new_ggridarray, np.ndarray

New g-points for the binned-down k-coefficients.

gedgesarray, np.ndarray

Cumulative sum of the weights. Goes from 0 to 1. Used only if use_rebin=True

indicestosum: list of lists

Indices of wavenumber bins to be used for the averaging

wngrid_filter: array, np.ndarray

Indices of the new table where there will actually be data (zero elsewhere)

wn_weights: array, np.ndarray

Padded weights to be used for the averaging.

wn_weight_counts: array, np.ndarray

Number of valid weights to use in each row of wn_weights.

num: int

Number of points to fine sample the g function in log-k space

use_rebin: boolean

Whether to use rebin or interp method.

petitRADTRANS.opacities.correlated_k_binning.rebin_ind_weights(old_bin_grid, new_bin_grid)#

Computes the indices and weights to be used when rebinning an array of values f of size N in the bins delimited by old_bin_grid (of size N+1) onto new bins delimited by new_bin_grid (of size Nnew+1) Returns indices, padded weights, and the valid weight counts to be used as follows to proceed with the rebinning

>>> f_rebinned = [
...     np.dot(
...         f[indicestosum[ii] - 1 : indicestosum[ii] - 1 + weight_counts[ii]],
...         final_weights[ii, : weight_counts[ii]],
...     )
...     for ii in range(new_bin_grid.size - 1)
... ]
petitRADTRANS.opacities.correlated_k_binning.is_sorted(a)#

Finds out if an array is sorted. Returns True if it is.

petitRADTRANS.opacities.correlated_k_binning.unit_convert(quantity, unit_file='unspecified', unit_in='unspecified', unit_out='unspecified')#

Chooses the final unit to use and the conversion factor to apply.

Parameters#

quantity: str

The name of the pysical quantity handled for potential error messages

unit_file/unit_in/unit_out: str

Respectively:

  • String with the unit found in (or assumed from the format of) the initial data,

  • The unit we think the initial data are in if unit_file is ‘unspecified’ or (we believe) wrong,

  • The unit we want to convert to. If unspecified, we do not convert.

Returns#

unit_to_write: str

Resulting unit.

conversion_factor: float

A multiplicating factor for the data to proceed to the conversion.