Source code for hypso.api

"""The public API: one immutable :class:`Product` per capture and level.

Examples
--------
>>> import hypso
>>> l1c = (hypso.open("HYPSO2_HSI.20250304T103805.aeronetvenice.L1A.nc")
...            .calibrate()
...            .georeference(latitudes=lats, longitudes=lons)
...            .toa_reflectance())
>>> l1c.write()

Every processing method returns a new Product and leaves the one it was
called on unchanged. Underneath are the tree functions in
:mod:`hypso.processing` and the product tree of :mod:`hypso.io.product`
(:attr:`Product.tree`), which stay available for direct use.
"""
import logging
from pathlib import Path

import numpy as np
import xarray as xr

from hypso.io.naming import product_name
from hypso.io.product import PRODUCT_DIMS, product_name as cube_name, read_product, write_product
from hypso.masks.flags import FLAGS_NAME, decode_flags

logger = logging.getLogger(__name__)

LEVELS = ("L1A", "L1B", "L1C")

#: Logged when an L1B without geolocation is written or asked for.
_NO_GEOLOCATION = ("%s L1B without geolocation: radiance only. Run georeference() before toa_reflectance(), "
                   "regrid() or window().")

_GEOMETRY = ("latitude", "longitude", "sensor_zenith", "sensor_azimuth", "solar_zenith", "solar_azimuth",
             "relative_azimuth")


[docs] class Product: """One capture at one processing level. Create one with :func:`hypso.open` or from a product tree. A Product is immutable: processing methods return a new Product. Parameters ---------- tree : xarray.DataTree A product tree as read by :func:`hypso.io.product.read_product`. Raises ------ ValueError If the tree has no valid ``processing_level`` (L1A-L1C, L2A, L2B). """ __slots__ = ("_tree",) def __init__(self, tree: xr.DataTree): level = tree.attrs.get("processing_level") if level not in LEVELS + ("L2A", "L2B"): raise ValueError(f"not a HYPSO product tree (processing_level={level!r})") object.__setattr__(self, "_tree", tree) def __setattr__(self, name, value): raise AttributeError("Product is immutable; processing methods return a new Product") @classmethod
[docs] def open(cls, path, wavelength_range=None, wavelengths=None, indices=None) -> "Product": """Read a product file of any level and any supported layout. Parameters ---------- path : str or pathlib.Path A current-format, older grouped or ground-station L1A file. wavelength_range, wavelengths, indices : optional Read only some bands (as :meth:`select_bands`); only those are read from the file. Not for L1A. Returns ------- Product """ return cls(read_product(path, wavelength_range=wavelength_range, wavelengths=wavelengths, indices=indices))
# -- contents ----------------------------------------------------------- @property
[docs] def tree(self) -> xr.DataTree: """The underlying product tree, as an :class:`xarray.DataTree`. A shallow copy: adding, replacing or removing variables and attributes does not change this Product, but the arrays are shared (copying the cube on every access would double memory), so do not modify array values in place - assign a new array instead. """ return self._tree.copy()
@property
[docs] def level(self) -> str: """Processing level: ``"L1A"``, ``"L1B"``, ``"L1C"``, ``"L2A"`` or ``"L2B"``.""" return self._tree.attrs["processing_level"]
@property
[docs] def sat_id(self) -> str: """Satellite, e.g. ``"HYPSO-2"``.""" return self._tree.attrs.get("sat_id")
@property
[docs] def target(self) -> str: """Capture target name, e.g. ``"aeronetvenice"``.""" return self._tree.attrs.get("location_description")
@property
[docs] def cube(self) -> xr.DataArray: """The product cube: ``dn``, ``Lt``, ``rhot`` or the L2A variable. Dimensions ``(lines, samples, bands)``, or ``(y, x, bands)`` when gridded, with ``wavelength``, ``radiation_wavelength`` and ``fwhm`` band coordinates. L2B products have none (ValueError); their products are :attr:`variables`. """ return self._tree.dataset[cube_name(self._tree)]
@property
[docs] def shape(self) -> tuple: """Spatial shape: ``(lines, samples)``, or ``(y, x)`` when gridded.""" ds = self._tree.dataset return tuple(ds.sizes[d] for d in self._spatial_dims())
@property
[docs] def wavelengths(self) -> np.ndarray: """Band centre wavelengths in nm, as calibrated (``radiation_wavelength``).""" return self.cube["radiation_wavelength"].values
@property
[docs] def is_georeferenced(self) -> bool: """Whether the product has geolocation (an L1B only after :meth:`georeference`).""" from hypso.io.product import is_georeferenced return is_georeferenced(self._tree)
@property
[docs] def geometry(self) -> xr.Dataset: """Latitude, longitude and sun and sensor angles, as a Dataset. Empty before georeferencing (L1A, a calibrated L1B). Includes the ``*_direct`` variants when direct georeferencing was computed. """ ds = self._tree.dataset names = [n for n in ds.data_vars if n in _GEOMETRY or (n.endswith("_direct") and n[:-len("_direct")] in _GEOMETRY)] return ds[names]
@property
[docs] def flags(self) -> dict: """Masks decoded from ``pixel_flags``: ``{name: boolean DataArray}``. Empty if the product has no flags. """ ds = self._tree.dataset return decode_flags(ds[FLAGS_NAME]) if FLAGS_NAME in ds else {}
@property
[docs] def variables(self) -> xr.Dataset: """Derived 2D products, as a Dataset. Those added with :meth:`with_variables` and those an atmospheric correction tool returns (e.g. ``chla``, ``polymer_flags``); for an L2B product, its products (e.g. ``chlor_a``). """ from hypso.io.product import _extra_variables, has_cube ds = self._tree.dataset return ds[_extra_variables(ds, [cube_name(self._tree)] if has_cube(self._tree) else [])]
@property
[docs] def grid(self): """Name of the map grid (e.g. ``"utm33n_876m"``), or None in sensor geometry. See :meth:`regrid`. """ return self._tree.attrs.get("grid")
def _spatial_dims(self) -> tuple: from hypso.io.product import spatial_dims return spatial_dims(self._tree.dataset) @property
[docs] def provenance(self) -> list: """How the product was made: one dict per processing step, oldest first. From ``metadata/processing``: step name, date, software version and git commit, input file and the step's parameters. """ if "metadata" not in self._tree.children or "processing" not in self._tree["metadata"].children: return [] steps = [{"step": name, **dict(node.attrs)} for name, node in self._tree["metadata/processing"].children.items()] return sorted(steps, key=lambda s: s.get("date_utc", ""))
def __repr__(self): from hypso.io.product import has_cube if not has_cube(self._tree): kind = self._tree.attrs.get("l2b_group") or self._tree.attrs.get("l2a_quantity") return (f"<hypso.Product {self.sat_id} {self.target} {self.level} {kind}: " f"{list(self.variables.data_vars)} {dict(zip(self._spatial_dims(), self.shape))}>") cube = self.cube return (f"<hypso.Product {self.sat_id} {self.target} {self.level}: {cube.name} " f"{dict(cube.sizes)}>") # -- pixels, spectra, geolocation --------------------------------------
[docs] def pixel(self, latitude: float, longitude: float, direct: bool = False) -> tuple: """The pixel nearest to a location. Parameters ---------- latitude, longitude : float Location in degrees. direct : bool, default False Use the direct (ADCS-only) georeferencing instead. Returns ------- tuple of int ``(line, sample)``, or ``(y, x)`` for a gridded product. """ from hypso.geometry.nearest import get_nearest_pixel geometry = self.geometry suffix = "_direct" if direct else "" if f"latitude{suffix}" not in geometry: raise ValueError(f"{self.level} product has no {'direct ' if direct else ''}geolocation") line, sample = get_nearest_pixel(target_latitude=latitude, target_longitude=longitude, latitudes=geometry[f"latitude{suffix}"].values, longitudes=geometry[f"longitude{suffix}"].values) return int(line), int(sample)
[docs] def spectrum(self, latitude: float = None, longitude: float = None, line: int = None, sample: int = None) -> xr.DataArray: """The cube's spectrum at one pixel. Parameters ---------- latitude, longitude : float, optional Location in degrees; the nearest pixel is used. line, sample : int, optional Pixel indices, instead of a location. Returns ------- xarray.DataArray Values over ``bands``, with wavelength coordinates. See Also -------- window : Statistics of a window of pixels around a location. """ if latitude is not None and longitude is not None: line, sample = self.pixel(latitude, longitude) elif line is None or sample is None: raise ValueError("give latitude and longitude, or line and sample") return self.cube.isel(dict(zip(self._spatial_dims(), (line, sample))))
[docs] def window(self, latitude: float, longitude: float, size: int = 3, exclude_flags=None, cv_band_range=(405.0, 570.0)) -> xr.Dataset: """A window of pixels around a location and its statistics, for matchups. Parameters ---------- latitude, longitude : float Location in degrees; the window is centred on the nearest pixel. size : int, default 3 Window width in pixels (odd). Clipped at the image edges. exclude_flags : list of str, optional ``pixel_flags`` names (e.g. ``["land", "cloud"]``) whose pixels are left out. cv_band_range : tuple of float, default (405, 570) Wavelength range (nm) for ``pixel_valid`` and ``cv_median``. Returns ------- xarray.Dataset Per band: ``mean``, ``median``, ``std``, ``cv`` (std / median) and ``valid_count``; ``pixels`` (the window) and ``excluded``. Attributes: centre pixel and its distance to the location, ``box_size``, ``pixel_valid`` and ``percent_valid`` (pixels with any finite band in ``cv_band_range``), ``cv_median`` and the window's mean angles. See :mod:`hypso.window`. """ from hypso.window import window_statistics return window_statistics(self._tree, self.pixel(latitude, longitude), latitude, longitude, size=size, exclude_flags=exclude_flags, cv_band_range=cv_band_range)
@property
[docs] def resolution(self) -> float: """Pixel size: the larger of the mean along- and across-track ground sampling distance, in m. For a gridded product, the grid's pixel size in its CRS's units (m, or degrees for a geographic grid). """ if self.grid is not None: return float(self._tree.attrs["grid_resolution"]) from hypso.grid import resolution return resolution(self._tree)
[docs] def swath_definition(self, direct: bool = False): """The product's geolocation as a :class:`pyresample.geometry.SwathDefinition`. Parameters ---------- direct : bool, default False Use the direct georeferencing instead. """ from hypso.grid import swath_definition return swath_definition(self._tree, direct=direct)
[docs] def footprint(self, mask=None, step: int = 10, block: int = 8): """The ground footprint as a shapely polygon in WGS84 longitude/latitude. See :func:`hypso.maps.footprint` (``mask="data"`` for the pixels with data only). """ from hypso.maps import footprint return footprint(self, mask=mask, step=step, block=block)
[docs] def write_footprint(self, path, mask=None, step: int = 10, block: int = 8): """Write the footprint, with the capture's metadata, as an ESRI Shapefile (``*.shp``, EPSG:4326) or GeoJSON. See :func:`hypso.maps.write_footprints`, which also writes several products to one file. """ from hypso.maps import write_footprints return write_footprints([self], path, mask=mask, step=step, block=block)
[docs] def area_definition(self): """A gridded product's grid as a :class:`pyresample.geometry.AreaDefinition`. Raises ------ ValueError If the product is in sensor geometry. """ from hypso.grid import area_definition return area_definition(self._tree)
[docs] def resample(self, target, method: str = "nearest", radius_of_influence: float = None) -> xr.Dataset: """Resample the product onto any target, as a bare Dataset. Parameters ---------- target : pyresample.geometry.AreaDefinition or SwathDefinition A grid, or a swath such as another sensor's (for matchups). method : {"nearest", "bilinear", "average"}, default "nearest" For the cube and float variables; ``"average"`` only onto grids coarser than the product. Flags are combined as in :class:`hypso.grid.GridOperators`. radius_of_influence : float, optional Search radius in m; default 1.5 times the coarser pixel size. Returns ------- xarray.Dataset The cube, derived variables, each flag as ``flag_<name>`` and the target's latitude and longitude. See Also -------- regrid : The product on a map grid, as a Product. """ from hypso.grid import resample_product return resample_product(self._tree, target, method=method, radius_of_influence=radius_of_influence)
[docs] def regrid(self, target="utm", resolution: float = None, method: str = "nearest", radius_of_influence: float = None, max_gb: float = 4.0) -> "Product": """The product on a map grid, at the same level. Parameters ---------- target : str, int, pyproj.CRS, AreaDefinition or Product, default "utm" ``"utm"`` (the WGS84 UTM zone of the scene centre), any CRS pyproj accepts (e.g. ``"EPSG:3035"``), a pyresample AreaDefinition, or a gridded Product to share its grid. resolution : float, optional Pixel size in the CRS's units. Default: this product's :attr:`resolution`, so nothing is oversampled. Not with an AreaDefinition or Product target. method : {"nearest", "bilinear", "average"}, default "nearest" ``"nearest"`` keeps one measured spectrum per pixel; ``"bilinear"`` blends neighbouring spectra; ``"average"`` takes cell means, for grids at least as coarse as the product. radius_of_influence : float, optional Search radius in m; default 1.5 times the coarser pixel size. max_gb : float, default 4.0 Refuse grids whose cube would be larger. Returns ------- Product Dimensions ``(y, x)`` with CF projection coordinates and a ``crs`` grid mapping; ``pixel_flags`` gains ``outside_swath``. See Also -------- hypso.grid.regrid : The tree function, with the per-variable rules. """ from hypso.grid import regrid if isinstance(target, Product): target = target.area_definition() return Product(regrid(self._tree, target, resolution=resolution, method=method, radius_of_influence=radius_of_influence, max_gb=max_gb))
[docs] def to_satpy(self, direct: bool = False): """The product as a Satpy Scene, one dataset per band. Parameters ---------- direct : bool, default False Use the direct georeferencing for the swath. """ from hypso.satpy.satpy import product_scene return product_scene(self, use_direct_georef=direct)
# -- adding masks and derived products (each returns a new Product) -----
[docs] def with_masks(self, descriptions: dict = None, **masks) -> "Product": """A new Product with masks added to ``pixel_flags``. Parameters ---------- descriptions : dict, optional ``{name: text}`` describing masks, kept in the file. **masks : array_like of bool ``name=mask`` with the product's spatial shape, True where flagged. Names may contain letters, digits and ``_``. Returns ------- Product Existing flags are kept; a mask with an existing name replaces it. Examples -------- >>> flagged = l1c.with_masks(land=land, cloud=cloud) """ from hypso.masks.flags import encode_flags merged = {name: flag.values for name, flag in self.flags.items()} merged.update({name: np.asarray(mask, dtype=bool) for name, mask in masks.items()}) old_descriptions = {} if FLAGS_NAME in self._tree.dataset and "comment" in self._tree.dataset[FLAGS_NAME].attrs: for part in self._tree.dataset[FLAGS_NAME].attrs["comment"].split("; "): name, _, text = part.partition(": ") old_descriptions[name] = text tree = self._tree.copy() root = tree.to_dataset(inherit=False) root[FLAGS_NAME] = encode_flags(merged, {**old_descriptions, **(descriptions or {})}, dims=self._spatial_dims()) tree.dataset = root return Product(tree)
[docs] def with_variables(self, **variables) -> "Product": """A new Product with derived 2D variables added. Parameters ---------- **variables : array_like or xarray.DataArray ``name=values`` with the product's spatial shape; a DataArray's attributes (e.g. ``units``) are kept. Written with the product and read back by :func:`hypso.open`. Returns ------- Product Examples -------- >>> l2a.with_variables(chla=xr.DataArray(chla, attrs={"units": "mg m-3"})) """ dims = self._spatial_dims() tree = self._tree.copy() root = tree.to_dataset(inherit=False) reserved = set(root.variables) for name, value in variables.items(): if name in reserved and name not in self.variables: raise ValueError(f"{name!r} is already a product, geometry or flag variable") data = value if isinstance(value, xr.DataArray) else xr.DataArray(np.asarray(value), dims=dims) if data.shape != self.shape: raise ValueError(f"{name!r} has shape {data.shape}, expected {self.shape}") root[name] = xr.Variable(dims, data.values, dict(data.attrs)) tree.dataset = root return Product(tree)
[docs] def select_bands(self, wavelength_range=None, wavelengths=None, indices=None) -> "Product": """A new Product with only some of the bands (from L1B on). Parameters ---------- wavelength_range : tuple of float, optional ``(min, max)`` in nm: the bands whose centre lies within it. wavelengths : array_like, optional Wavelengths in nm: the nearest band to each. indices : array_like of int, optional Band indices in the sensor's full band set (``band_index``). Returns ------- Product Each band keeps its full-set index, so later steps (TOA reflectance, SRFs) give the same values as on all bands. Raises ------ ValueError On L1A: calibration needs every band. Examples -------- >>> vis = l1b.select_bands(wavelength_range=(450, 700)) """ from hypso.processing.bands import select_bands return Product(select_bands(self._tree, wavelength_range=wavelength_range, wavelengths=wavelengths, indices=indices))
# -- processing (each returns a new Product) ---------------------------- def _require(self, level: str, method: str): if self.level != level: hint = f': use .to("{level}") first' if level in LEVELS and self.level in LEVELS \ and LEVELS.index(self.level) < LEVELS.index(level) else "" raise ValueError(f"{method}() needs an {level} product, this one is {self.level}{hint}")
[docs] def calibrate(self, coeff_type: str = None, version=None, **kwargs) -> "Product": """Calibrate L1A to L1B: top-of-atmosphere radiance ``Lt``. The L1B has no geolocation until :meth:`georeference`. Leave that out when only radiance is needed. Parameters ---------- coeff_type : str, optional Radiometric variant: ``"abs"`` (default), ``"moved"`` or ``"lab"``. version : str or dict, optional Coefficient release: ``"V3.1"`` (default), ``"V3.1v2spec"`` (for OC-SMART), ``"V2"``, ``"V1"``; or ``{correction: release}`` to mix them, e.g. ``{"smile": "V2"}``. See docs/calibration.rst. **kwargs Passed to :func:`hypso.processing.l1a_to_l1b`: ``coeff_files`` (explicit files), ``radiometric``, ``smile``, ``destripe``, ``spectral`` (False to skip a correction), and ``keystone`` (True to apply the sensor's keystone model; unreviewed groundwork). Returns ------- Product L1B; the coefficients used and their checksums are recorded in :attr:`provenance`. """ from hypso.processing import l1a_to_l1b self._require("L1A", "calibrate") product = Product(l1a_to_l1b(self._tree, coeff_type=coeff_type, version=version, **kwargs)) logger.info("Calibrated to L1B without geolocation; georeference() adds it") return product
[docs] def georeference(self, latitudes=None, longitudes=None, direct: bool = True) -> "Product": """Georeference an L1B: latitude, longitude and sun and sensor angles. Parameters ---------- latitudes, longitudes : array_like or path, optional Indirect (GCP-corrected) pixel coordinates, as arrays or paths to raw float32 files. Omit both to use direct georeferencing. direct : bool, default True Also compute direct georeferencing from the ADCS telemetry (``*_direct`` variables). Returns ------- Product """ from hypso.processing import georeference_l1b self._require("L1B", "georeference") return Product(georeference_l1b(self._tree, latitudes=latitudes, longitudes=longitudes, direct=direct))
[docs] def toa_reflectance(self, use_direct_georef: bool = False, ssi_source: str = "tsis", use_unbinned: bool = True) -> "Product": """Convert georeferenced L1B radiance to L1C top-of-atmosphere reflectance ``rhot``. Parameters ---------- use_direct_georef : bool, default False Use the direct georeferencing's solar zenith angle. ssi_source : {"tsis", "thuillier"}, default "tsis" Solar irradiance spectrum: TSIS-1 HSRS or Thuillier (2002). use_unbinned : bool, default True Build the spectral response functions from the unbinned bands. Returns ------- Product L1C, with the spectral response functions in ``metadata/srf``. """ from hypso.processing import l1b_to_l1c self._require("L1B", "toa_reflectance") return Product(l1b_to_l1c(self._tree, use_direct_georef=use_direct_georef, ssi_source=ssi_source, use_unbinned=use_unbinned))
[docs] def correct_atmosphere(self, method: str = "machi", masked: bool = False, mask_flags=None, work_dir=None, config=None, **options) -> "Product": """Atmospheric correction to L2A. Parameters ---------- method : {"machi", "dps", "polymer", "acolite", "ocsmart"}, default "machi" MACHI or dark pixel subtraction (in this process), or an external tool. masked : bool, default False Give the method NaN where ``pixel_flags`` are set. mask_flags : list of str, optional Only these flags, with ``masked=True``. work_dir : str or path, optional External tools: where their input and raw output go (kept); default a temporary directory. config : str, path or dict, optional External tools: the AC tool configuration (:mod:`hypso.ac.config`); default the ``HYPSO_AC_CONFIG`` environment variable. **options Method-specific options, e.g. ``est_min_R=0.05`` for MACHI, ``settings={...}`` overriding the configuration for Polymer or ACOLITE, ``output="l2r"`` for ACOLITE. Returns ------- Product L2A: the method's reflectance on this product's bands, its 2D products and flags in :attr:`variables`. See Also -------- hypso.processing.correct_atmosphere """ from hypso.processing import correct_atmosphere return Product(correct_atmosphere(self._tree, method, masked=masked, mask_flags=mask_flags, work_dir=work_dir, config=config, **options))
@property
[docs] def atmospheric_correction(self): """Atmospheric correction method of an L2A product, e.g. ``"polymer"``; None below L2A.""" return self._tree.attrs.get("atmospheric_correction")
[docs] def to(self, level: str, calibrate: dict = None, georeference: dict | bool = None, toa_reflectance: dict = None, correct_atmosphere: dict = None) -> "Product": """Run the processing steps from this level up to ``level``. Parameters ---------- level : {"L1B", "L1C", "L2A"} Target level. L1B means georeferenced: an L1B without geolocation is georeferenced first. calibrate, georeference, toa_reflectance, correct_atmosphere : dict, optional Options for each step, passed to the method of that name. For L2A, ``correct_atmosphere={"method": ...}`` picks the method (default MACHI). ``georeference=False``, for L1B only, skips georeferencing (with a warning). Returns ------- Product Examples -------- >>> l1c = l1a.to("L1C", calibrate={"version": "V2"}, ... georeference={"latitudes": lats, "longitudes": lons}) >>> l2a = l1c.to("L2A") # MACHI >>> l2a = l1c.to("L2A", correct_atmosphere={"method": "dps"}) """ level = level.upper() order = LEVELS + ("L2A",) if level not in order or order.index(level) < order.index(self.level): raise ValueError(f"cannot go from {self.level} to {level}") if georeference is True: georeference = {} if georeference is False and level != "L1B" and not self.is_georeferenced: raise ValueError(f"georeference=False only goes to L1B: {level} needs geolocation") options = {"calibrate": calibrate or {}, "georeference": georeference or {}, "toa_reflectance": toa_reflectance or {}, "correct_atmosphere": correct_atmosphere or {}} product = self while True: if product.level == "L1B" and not product.is_georeferenced and georeference is not False: step = "georeference" elif product.level == level: break else: step = _STEPS[product.level] product = getattr(product, step)(**options[step]) if level == "L1B" and not product.is_georeferenced: logger.warning(_NO_GEOLOCATION, product.target) return product
# -- output -------------------------------------------------------------
[docs] def filename(self, label: str = None, ac: str = None) -> str: """The product's file name (:mod:`hypso.io.naming`). Parameters ---------- label : str, optional Free-text label for experiments, the last name field. ac : str, optional Override the atmospheric correction field (L2A) or source (L2B). Returns ------- str e.g. ``"HYPSO2_HSI.20250304T103805.aeronetvenice.L1C.nc"``; L2A names carry the quantity (from the product variable) and the method, e.g. ``"...L2A.RHOS.machi.nc"``; L2B names the group and source, e.g. ``"...L2B.BGC.oc4.nc"``. """ from hypso.io.naming import name_method from hypso.processing.capture import acquisition_time from datetime import datetime time = datetime.fromisoformat(acquisition_time(self._tree.attrs)) from hypso.io.product import has_cube quantity = None if self.level == "L2A": ac = name_method(ac or self.atmospheric_correction) quantity = self.cube.name if has_cube(self._tree) else self._tree.attrs["l2a_quantity"] elif self.level == "L2B": ac = ac or self._tree.attrs["l2b_source"] quantity = self._tree.attrs["l2b_group"] return product_name(self.sat_id, time, self.target, self.level, ac=ac, grid=self.grid, label=label, quantity=quantity).filename()
[docs] def write(self, path=None, out_dir=None, label: str = None, datacube: bool = False, significant_bits=14, tool_products: bool = True, intermediates: bool = True) -> Path: """Write the product in the current file format. Parameters ---------- path : str or path, optional Output file. Default: :meth:`filename` in ``out_dir``. out_dir : str or path, optional Directory for the default name; the current directory if omitted. label : str, optional Label for the default name. datacube : bool, default False Write the cube as one 3D variable instead of one per band. significant_bits : int or None, default 14 Keep this many significant mantissa bits of the float bands (L1B-L2A): about 40% smaller L1C bands at most 3.1e-5 relative error; None writes them lossless. tool_products : bool, default True L2A: also write the AC tool's own products (:meth:`tool_products`) as L2B files in the same directory. The L2A file never holds them; False drops them (with a warning). intermediates : bool, default True L2A: also write the AC tool's other per-band outputs (:meth:`intermediates`, e.g. OC-SMART's Lt, Lr, Lrc, tg_sol) as the ``...L2A.AUX.<method>.nc`` file in the same directory. False drops them (with a warning). Returns ------- pathlib.Path The written file (for L2A, the L2A file). """ if self.level == "L1B" and not self.is_georeferenced: logger.warning(_NO_GEOLOCATION, self.target) if path is None: path = Path(out_dir or ".") / self.filename(label=label) path = Path(path) tree = self._tree from hypso.io.product import has_cube if self.level == "L2A" and has_cube(tree): # not the L2A.AUX product itself from hypso.processing.l2b import split_tool_products tree, products = split_tool_products(tree) if tool_products: for product in products.values(): out = Product(product) l2b_path = out.write(out_dir=path.parent, label=label, significant_bits=significant_bits) logger.info("AC tool products written to %s", l2b_path.name) elif products: logger.warning("AC tool products %s not written (tool_products=False)", sorted(products)) from hypso.processing.l2b import split_intermediates tree, aux = split_intermediates(tree) if aux is not None and intermediates: aux_path = Product(aux).write(out_dir=path.parent, label=label, significant_bits=significant_bits) logger.info("AC tool intermediates (%d variables) written to %s", len(aux.dataset.data_vars), aux_path.name) elif aux is not None: logger.warning("AC tool intermediates not written (intermediates=False)") write_product(tree, path, datacube=datacube, significant_bits=significant_bits) return path
[docs] def derive(self, group: str, algorithm: str = None) -> "Product": """An L2B product from one of the package's algorithms (from L2A). Parameters ---------- group : str ``"BGC"`` (chlorophyll-a, suspended matter) or ``"IOP"`` (absorption, backscattering, Kd). algorithm : str, optional BGC: ``"oci"`` (default; NASA's chlor_a, CI and OC4 blend), ``"oc4"``, ``"ci"``, ``"ndci"`` (turbid/eutrophic waters), ``"nechad2010"`` (suspended matter); IOP: ``"qaa"`` (default). See :mod:`hypso.processing.l2b_algorithms`. Returns ------- Product Level L2B, e.g. ``...L2B.BGC.oci.nc``. Examples -------- >>> chl = l2a.derive("BGC") # from an L2A.RRS product >>> chl.variables["chlor_a"] >>> tsm = l2a.derive("BGC", "nechad2010") """ from hypso.processing.l2b import derive return Product(derive(self._tree, group, algorithm))
[docs] def tool_products(self) -> dict: """The AC tool's own geophysical products as L2B products. Polymer's ``chla``, ACOLITE's ``chl_*``/``SPM_*``, OC-SMART's ``chlor_a_*``/``tsm_*``, ... grouped by :data:`hypso.io.naming.L2B_GROUPS`, each with the tool's flags. An L2A file does not keep them: :meth:`write` writes them as L2B files (``...L2B.BGC.polymer.nc``) next to it. Returns ------- dict ``{group: Product}``, e.g. ``{"BGC": ..., "IOP": ...}``; empty for in-process methods (MACHI, DPS). """ from hypso.processing.l2b import split_tool_products if self.level != "L2A": raise ValueError(f"tool products come from an L2A, got {self.level}") return {group: Product(tree) for group, tree in split_tool_products(self._tree)[1].items()}
[docs] def intermediates(self) -> "Product": """The AC tool's other per-band outputs as an L2A.AUX product. OC-SMART's Lt, Lr, Lrc, Lwp, tg_sol and tg_sen (one 2D variable per quantity and band, ``Lt_434``), with the L2A's geometry and flags. :meth:`write` writes them as ``...L2A.AUX.<method>.nc`` next to the L2A; the L2A file does not hold them. Returns ------- Product or None None when the method returned none (in-process methods, Polymer, ACOLITE). """ from hypso.processing.l2b import split_intermediates if self.level != "L2A": raise ValueError(f"intermediates come from an L2A, got {self.level}") aux = split_intermediates(self._tree)[1] return None if aux is None else Product(aux)
# An L1B is georeferenced first when it is not yet (Product.to). _STEPS = {"L1A": "calibrate", "L1B": "toa_reflectance", "L1C": "correct_atmosphere"}
[docs] def open(path, wavelength_range=None, wavelengths=None, indices=None) -> Product: # noqa: A001 """Read a HYPSO product file. Parameters ---------- path : str or pathlib.Path A product file of any level (L1A-L2A), in the current format, the older grouped format, or a ground-station L1A. wavelength_range, wavelengths, indices : optional Read only some bands (see :meth:`Product.select_bands`). Returns ------- Product Examples -------- >>> import hypso >>> l1c = hypso.open("HYPSO2_HSI.20250304T103805.aeronetvenice.L1C.nc") """ return Product.open(path, wavelength_range=wavelength_range, wavelengths=wavelengths, indices=indices)