Skip to main content

writel2

Helpers for writing standard-layout L2 NetCDF4 ocean color products from Python, and for reading metadata back out of an existing L2 file.

  • writel2.L2Writer creates the standard l2gen group layout (sensor_band_parameters, scan_line_attributes, geophysical_data, geolocation_data, processing_control/input_parameters [/flag_percentages]), preloads the static global attributes, and writes variables into any of those groups via write_product(name, data, group=...), whose attributes (long_name, units, valid_min/valid_max, scale_factor/add_offset, _FillValue, flag_values/flag_meanings, reference, comment, ...) are resolved from share/common/product.xml - the same metadata l2gen itself uses, regardless of which group a variable belongs to.
  • writel2.L2Reader is a read-only helper for pulling metadata back out of an existing L2 (or L1/L3) file, mainly so L2Writer can seed a derived product's global attributes from its source file.
  • writel2.ProductTable resolves a product name (e.g. "chlor_a", "Rrs_443") against product.xml into a ProductInfo record.
  • writel2.GlobalMetadata loads the static, institution-wide global attributes (license, naming authority, creator/publisher info, ...) from $OCDATAROOT/common/global_metadata.json.

Install

pip install $OCSSWROOT/src/scripts/writel2

This installs as obpg-writel2, importable as writel2.

Usage

See examples/demo_write_l2.py for a complete, runnable example that writes a small file touching every group:

python $OCSSWROOT/src/scripts/writel2/examples/demo_write_l2.py /tmp/demo_l2.nc

The short version:

import numpy as np
from writel2 import L2Writer

with L2Writer("out.L2.nc", title="Demo Level-2 Data") as writer:
    # geophysical_data: resolved against product.xml for long_name, units,
    # valid_min/valid_max, scale_factor/add_offset, _FillValue, ...
    flags = np.zeros((4, 3), dtype=np.int32)
    writer.write_product("l2_flags", flags)

    # processing_control/input_parameters and /flag_percentages
    writer.set_input_parameters({"ifile": "input.L1B.nc", "l2prod": "l2_flags"})
    writer.set_flag_percentages({"ATMFAIL": 0.0, "LAND": 0.49})

    # Global attribute (group=None, the default) or group-level attribute
    # (group=writer.groups[...]).
    writer.set_group_attrs({"scene_id": "DEMO-0001"})

Datatype and attributes for any group's variables are resolved from product.xml and applied automatically by write_product(), including appropriate scaling - pass group= to target sensor_band_parameters, scan_line_attributes, or geolocation_data instead of the default geophysical_data. The one exception is geophysical_data's own local wavelength coordinate variable (used alongside 3D products), which l2gen writes directly rather than resolving against product.xml; the calling script must set its datatype and attributes manually. See the demo script for the full pattern, including how to register dimensions shared across groups.

navigation_data is still accepted as a standard group name for latitude/longitude (and still triggers geospatial_bounds), but it's deprecated - writing to it logs a warning recommending geolocation_data instead.

Public API

L2Writer:

  • write_product(name, data, dim_names=(...), group="geophysical_data") - resolve name against product.xml and write it into group; returns the created h5netcdf.Variable.
  • set_group_attrs(attrs, group=None) - write attributes to a group; with group=None (the default) writes global (file-level) attributes.
  • set_input_parameters(params) - write attributes to processing_control/input_parameters.
  • set_flag_percentages(percentages, group_name="flag_percentages") - write attributes to processing_control/<group_name>.
  • flag_percentages_group(name="flag_percentages") - get (creating if needed) the named child group of processing_control.
  • copy_global_attrs_from(source, names=None, overrides=None) - copy global attributes from an L2Reader, optionally restricted to names and/or overridden by overrides.
  • close() - close the underlying file (also called on __exit__).

L2Reader:

  • global_attrs (property) - the source file's global attributes as a dict.
  • group(name) - the named h5netcdf.Group, or None if absent.
  • is_l2() - whether the source file looks like an L2 file.
  • copy_global_attrs_to(dest, names=None) - copy global attributes onto dest (an h5netcdf.File or Group), optionally restricted to names.
  • close() - close the underlying file (also called on __exit__).

ProductTable.find(name, wavelengths=None) resolves a product name into a ProductInfo record. GlobalMetadata loads $OCDATAROOT/common/global_metadata.json and exposes its contents as a dict-like object.

Tests

pip install $OCSSWROOT/src/scripts/writel2
python -m unittest discover -s $OCSSWROOT/src/scripts/writel2/tests

Metadata

Release files for obpg-writel2 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for obpg-writel2 1.0.2
File Interpreter ABI Platform Actions
obpg_writel2-1.0.2-py3-none-any.whl Python 3 none any Details for obpg_writel2-1.0.2-py3-none-any.whl

Release files / obpg_writel2-1.0.2-py3-none-any.whl

Download URL obpg_writel2-1.0.2-py3-none-any.whl
Size 20.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0a0637fcec46d3986a51ef4dccf3b5131598d9ef2bf793bc53443103b54d1d0c
BLAKE2b-256 checksum
How to use checksums
97f8d3f79dbe4b70ebcee57da54ec8adbd17be27184f308cd0cd11d9a2a56ef1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for obpg-writel2 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.2 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page