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 .
python3 -m pip install -e ".[torch,dev]"

The optional native backend requires PyTorch:

python3 -m pip install "py2sess[native]"

For source-tree runs without installation, set PYTHONPATH=src.

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.

Releases are tagged from merged PRs by GitHub Actions. Add a release:major, release:minor, or release:patch label to the PR before merging; the default is release:patch.

PyPI publication uses Trusted Publishing. Configure PyPI to trust happysky19/py2sess, workflow .github/workflows/release.yml, and the pypi GitHub environment, then run the Publish to PyPI GitHub Actions workflow for the tag. The release workflow currently publishes macOS arm64 native wheels and the source distribution.

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.3.tar.gz (13.4 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.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (873.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

py2sess-0.4.3-cp313-cp313-macosx_15_0_arm64.whl (816.8 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

py2sess-0.4.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (872.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

py2sess-0.4.3-cp312-cp312-macosx_15_0_arm64.whl (816.7 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

py2sess-0.4.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (869.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

py2sess-0.4.3-cp311-cp311-macosx_15_0_arm64.whl (816.0 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

py2sess-0.4.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (868.7 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

py2sess-0.4.3-cp310-cp310-macosx_15_0_arm64.whl (814.8 kB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

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

File metadata

  • Download URL: py2sess-0.4.3.tar.gz
  • Upload date:
  • Size: 13.4 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.3.tar.gz
Algorithm Hash digest
SHA256 8f15aaa87bd7bd7432ea1aad684812d7a52d2f11519b16803bc7b9ba475e6a21
MD5 f64acaad3a054889de7c5d1a88535105
BLAKE2b-256 6e28f32fac80831fbc450bddb34e1fb3be40ebf714945525611586071f83efb4

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.3.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.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c6274991f14a6ac9404b8ffcb497c30d1b624f13ef02b219ae506547bc2e3571
MD5 3b91ebb9b163c16bbdfaf83a5b69fbdc
BLAKE2b-256 5e62a8f37416e63fe4fd42ecda9a521c85d46152cc70c11b5355356168dbd237

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_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.3-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.3-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 251a1f46bb2f46e32951aa122974fef2ef0de8ed7462effd8b0baeea1dcef586
MD5 28bb91d815deebd6130bac7880b30723
BLAKE2b-256 1bc3846ea3beeae443daab1ab30307c7c039b9765e214e9215a278ad60e95809

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.3-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.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a593f23dfe36e5d5efa9817a4617549717d73a4e8a25db2fd4ddce24784b0e77
MD5 f72d009ad836ff141e932f98fd0e4fff
BLAKE2b-256 0f722d7c2275041b4353abe2101ddc061dd4b15fea677d5e11f2b1d1908bdc3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_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.3-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.3-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 db9fff5c0b4e4c8c026b4fbd1245053d654b5c34886a3fd7113cfa7f758b7114
MD5 1dbca0836b29f304e5a1a3c9f4d04563
BLAKE2b-256 3895efd8a3002a0e6d87f513ecb18d6b14061fd9e29f5c53d2c0b184fcd74405

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.3-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.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0183a05aea83bf3c25e457a221eb8ca6ffd08658a58787aa3b0dda0d421aeb0a
MD5 6f0f812adcef92812aa09f58a68e8d8a
BLAKE2b-256 c96e0736d5c7e653d61512f4ee70011951327bae5ba6c49853d569e340cba9c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_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.3-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.3-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 cdfd83ead6c242ce4fb8c137db3f5f9f419c57e28aebfd0f90703eb8940f02fb
MD5 6e5f53247bbbee248db20cc67cb92927
BLAKE2b-256 ca882378283c73c5203aca1f4646a53e181591f363b8583f1d26812cd381e599

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.3-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.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3f62d9360099610ad9b7eac618a81843c1e5241dc5baa867fe9f856647ec3e9f
MD5 88203cddbb3d0181e889a4cdd543c90b
BLAKE2b-256 4cf72a0d86bc76f985c361604f2eac75f3c858eb53e02210cd52636effbb264a

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_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.3-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for py2sess-0.4.3-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 45ca8ac686219ecb43f7865872dbfe0a62c2c78c974f81305ce3836476dc71e6
MD5 5ce681f0ce2ceb9cfd2c1273777567e9
BLAKE2b-256 f40e6d98581133d5252afab590a8c6386376d63c0f85e0d958ae8b9b5318dac5

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2sess-0.4.3-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