Metadata-Version: 2.4
Name: obpg-writel2
Version: 1.0.2
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
Requires-Dist: pyproj
Requires-Dist: shapely

# 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

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

    # 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

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