Metadata-Version: 2.4
Name: obpg-writel2
Version: 1.0.0
Summary: Helpers for reading L2 metadata and writing derived L2 NetCDF4 products
Author-email: Gwyn Fireman <Gwyn.F.Fireman@nasa.gov>, Claude Sonnet 5 <noreply@anthropic.com>
Requires-Python: >=3.12
Description-Content-Type: text/markdown
Requires-Dist: numpy>=1.26.4
Requires-Dist: h5netcdf>=1.8.1

# 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`,
  `navigation_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

```bash
pip install $OCSSWROOT/src/scripts/writel2
```

This installs as `obpg-writel2`, importable as `writel2`.

## Usage

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

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

The short version:

```python
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})
```

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 `navigation_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.

## Tests

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