Skip to main content

Standalone Python implementation of the optimized 2S-ESS forward model.

Project description

py2sess

py2sess is a Python implementation of the optimized 2S-ESS radiative-transfer model. It supports solar and thermal forward calculations with NumPy and optional torch backends. It does not call the original Fortran code.

Install

python3 -m pip install py2sess

For local development:

python3 -m pip install -e ".[torch,dev]"

The optional native backend uses the installed PyTorch shared libraries. Install PyTorch first when building native wheels from source or when using a backend that needs torch tensors.

Build

py2sess uses CMake through scikit-build-core. By default, source builds prepare the Python package without compiling the optional native backend.

cmake -S . -B build
cmake --build build
python3 -m build

To build a local wheel with the native backend, install PyTorch and build without PEP 517 isolation so CMake can find Torch:

python3 -m pip install build scikit-build-core setuptools-scm torch
python3 -m build --wheel --no-isolation -Ccmake.define.PY2SESS_BUILD_NATIVE=ON

Native wheels link against the PyTorch shared libraries supplied by the installed torch package.

Quick Start

Solar:

import numpy as np
from py2sess import TwoStreamEss, TwoStreamEssOptions

solver = TwoStreamEss(TwoStreamEssOptions(nlyr=3, mode="solar"))
result = solver.forward(
    tau=np.full(3, 0.02),
    ssa=np.full(3, 0.2),
    g=np.full(3, 0.1),
    z=np.array([3.0, 2.0, 1.0, 0.0]),
    angles=[30.0, 20.0, 0.0],  # sza, vza, relative azimuth
    albedo=0.3,
)
print(result.radiance)

Thermal:

solver = TwoStreamEss(TwoStreamEssOptions(nlyr=3, mode="thermal"))
result = solver.forward(
    tau=np.full(3, 0.1),
    ssa=np.zeros(3),
    g=np.zeros(3),
    z=np.array([3.0, 2.0, 1.0, 0.0]),
    angles=20.0,
    planck=np.array([1.0, 1.1, 1.2, 1.3]),
    surface_planck=1.4,
    emissivity=1.0,
)

Batched wavelengths use leading dimensions:

solver = TwoStreamEss(TwoStreamEssOptions(nlyr=3, mode="thermal"))
tau = np.full((100, 3), 0.02)
result = solver.forward(
    tau=tau,
    ssa=np.zeros_like(tau),
    g=np.zeros_like(tau),
    z=np.array([3.0, 2.0, 1.0, 0.0]),
    angles=20.0,
    planck=np.ones((100, 4)),
    surface_planck=np.ones(100),
    emissivity=np.ones(100),
)
print(result.radiance.shape)  # (100,)

Level fluxes use the final axis for TOA-to-BOA levels. This clear absorbing solar case has an analytic Beer-Lambert flux solution:

import numpy as np
from py2sess import TwoStreamEss, TwoStreamEssOptions

sza = 30.0
mu0 = np.cos(np.deg2rad(sza))
fbeam = 1.0
tau = np.array([0.1, 0.2])
z = np.array([2.0, 1.0, 0.0])

solver = TwoStreamEss(
    TwoStreamEssOptions(
        nlyr=tau.size,
        mode="solar",
        plane_parallel=True,
        delta_scaling=False,
        downwelling=True,
        output_levels=True,
        output_fluxes=True,
        fo_flux_n_mu=8,
    )
)
result = solver.forward(
    tau=tau,
    ssa=np.zeros_like(tau),  # pure absorption
    g=np.zeros_like(tau),
    z=z,
    angles=[sza, 0.0, 0.0],
    fbeam=fbeam,
    albedo=0.0,  # black surface: no upward reflected flux
    delta_m_truncation_factor=np.zeros_like(tau),
    include_fo=True,
)

level_tau = np.concatenate(([0.0], np.cumsum(tau)))
analytic_down = fbeam * mu0 * np.exp(-level_tau / mu0)

np.testing.assert_allclose(result.flux_down[0], analytic_down, atol=1.0e-9)
np.testing.assert_allclose(result.flux_up[0], 0.0, atol=1.0e-8)
np.testing.assert_allclose(result.flux_net, result.flux_up - result.flux_down)

print(result.flux_down[0])

Torch CPU float64:

solver = TwoStreamEss(
    TwoStreamEssOptions(nlyr=3, mode="solar", backend="torch", torch_dtype="float64")
)

API Notes

Core inputs are tau, ssa, g, z, angles, and the surface/source terms needed by the selected mode. Solar angles are [sza, vza, raz] in degrees; thermal angles are viewing zenith angles. Heights are in km, ordered top to bottom.

See docs/api_arguments.md for the full argument table and conventions. Level-flux conventions are summarized in docs/level_fluxes.md.

Examples

python3 examples/level_flux_beer_lambert.py
python3 examples/build_thermal_source_from_temperature.py
python3 examples/retrieve_synthetic_spectra.py --case uv --noise-level 0

Scene/profile runs:

from py2sess.scene import load_scene

scene = load_scene(profile="profile.txt", config="scene.yaml")
result = scene.forward(backend="numpy", include_fo=True)

Full-spectrum benchmark details are in docs/full_spectrum_benchmarks.md.

Tests

python3 -m unittest discover -s tests -v
python3 -m ruff check .
python3 -m ruff format --check .

Full-spectrum benchmarks use profile text plus scene YAML inputs and Python optical preprocessing. Keep large local cross-section tables, benchmark bundles, and generated outputs out of git.

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py2sess-0.4.5.tar.gz (13.3 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

py2sess-0.4.5-cp313-cp313-manylinux_2_27_x86_64.whl (11.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64

py2sess-0.4.5-cp313-cp313-macosx_15_0_arm64.whl (825.7 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

py2sess-0.4.5-cp312-cp312-manylinux_2_27_x86_64.whl (11.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64

py2sess-0.4.5-cp312-cp312-macosx_15_0_arm64.whl (825.7 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

py2sess-0.4.5-cp311-cp311-manylinux_2_27_x86_64.whl (11.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64

py2sess-0.4.5-cp311-cp311-macosx_15_0_arm64.whl (825.0 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

py2sess-0.4.5-cp310-cp310-manylinux_2_27_x86_64.whl (11.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64

py2sess-0.4.5-cp310-cp310-macosx_15_0_arm64.whl (823.7 kB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

Details for the file py2sess-0.4.5.tar.gz.

File metadata

  • Download URL: py2sess-0.4.5.tar.gz
  • Upload date:
  • Size: 13.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for py2sess-0.4.5.tar.gz
Algorithm Hash digest
SHA256 df49c6f9bb717f996566783f0598311562692ea6b9911f3e6b66cc9dfb0c4029
MD5 43675111bb257d5199f8b11c728ccca4
BLAKE2b-256 0bbbe492ef53433f5648be39fc70abb7b114bab9406912a29f629a5b1f0d22c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.5.tar.gz:

Publisher: release.yml on happysky19/py2sess

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2sess-0.4.5-cp313-cp313-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.5-cp313-cp313-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 404413f89360dc5c92d619989bf746214886a0ba89049234675bc87c92960491
MD5 a1c31717868fb9a022241fab0bf49f15
BLAKE2b-256 d8dc86a65b676cc6752ca28e07e0fdc772d0d27c8bf8b4dba7452f60cbddbb8e

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.5-cp313-cp313-manylinux_2_27_x86_64.whl:

Publisher: release.yml on happysky19/py2sess

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2sess-0.4.5-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.5-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 a7d0b7efe3731c20102c3db1e11139bf74c15722be99759d29e5712b2f9e9705
MD5 115f70d4ebc826e8aeb317eed5bc478d
BLAKE2b-256 402c2626730d9f4e10a7257ca6746d97794a88c10edc52dec01b9ecb52b05acc

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.5-cp313-cp313-macosx_15_0_arm64.whl:

Publisher: release.yml on happysky19/py2sess

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2sess-0.4.5-cp312-cp312-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.5-cp312-cp312-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 8f2952d5c6c92c391e1057984b500df1cc6677d1c32eae733c89b63819431c92
MD5 c38781bc4357fe151adace912fba93d9
BLAKE2b-256 15a0d59f22c5ec95607708f927fba4425bcfb1f47a9a02fa617a6aea44c7ef9c

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.5-cp312-cp312-manylinux_2_27_x86_64.whl:

Publisher: release.yml on happysky19/py2sess

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2sess-0.4.5-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.5-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 b45700c335c4acafe3aba59c17c1d6c52794b0d5b93503f5ac7d3c8da645b2b3
MD5 f42211e254df7a32d282339ae930408c
BLAKE2b-256 32b6b06bb8f6d29527d19b8786d1071c50b3bb2fbcfc2a005f6480e81e57c9ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.5-cp312-cp312-macosx_15_0_arm64.whl:

Publisher: release.yml on happysky19/py2sess

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2sess-0.4.5-cp311-cp311-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.5-cp311-cp311-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 bda5e3df64abfdb23096e9cb5fde7890154611d537d5fe8801b6fcc07b22f300
MD5 1cb88f472f27d8c81605d210c0193aa7
BLAKE2b-256 395a84aae4c0d0e9eb8fa5a6df2c1cfb69c559cbff7ab394b857a7b73aa8e7e6

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.5-cp311-cp311-manylinux_2_27_x86_64.whl:

Publisher: release.yml on happysky19/py2sess

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2sess-0.4.5-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.5-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 8ef977caea8519480b4d4d51709ad51fdaf4511861e4e2f6c4d3f779017308c7
MD5 b7c0c135605414b1d0a000970c51854f
BLAKE2b-256 cf702b24aa09045fe9d89effe51586aab3e392478da70ecfc328aaa91c947ac8

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.5-cp311-cp311-macosx_15_0_arm64.whl:

Publisher: release.yml on happysky19/py2sess

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2sess-0.4.5-cp310-cp310-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.5-cp310-cp310-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 d632fd9606a1b3ce18756865de3c8b28f86185f5f6d9c44632d37548b8ccd347
MD5 9e6273a755e1567d489ae61774772973
BLAKE2b-256 1425972ef7926b92ce8db2a2e7ee6afa51fe8b2fd9bf0d63ac5faeb6418bba35

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.5-cp310-cp310-manylinux_2_27_x86_64.whl:

Publisher: release.yml on happysky19/py2sess

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2sess-0.4.5-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.5-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 e49ef7a7e65b164fea725894f9a94a5737cc2dea61c6502f42719165e641ff5e
MD5 5f6e1f22a3b6c0df8367ee820387dc71
BLAKE2b-256 c891c42e5132d8025419fa079708b3082c3745cedafbda04ebb6304d05734e21

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.5-cp310-cp310-macosx_15_0_arm64.whl:

Publisher: release.yml on happysky19/py2sess

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page