Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Compute — a cloud-masked NDVI pipeline over Lake Tahoe

Open In Colab

You compose library operators into a cloud-masked NDVI pipeline, then add one operator of your own. This is step 3 of the Lake Tahoe story. The find notebook chose the scene, 2024-09-22; the cut notebook chose the 10 km South Shore box used here.

How Sequential and Graph work is explained once in Operator concepts. Here you use them on real pixels.

What you’ll learn:

  1. Chain library operators with | into a Sequential.

  2. Mask cloud and shadow with gz.S2SCL and gz.ApplyMask.

  3. Wire a two-branch pipeline as a Graph.

  4. Compute several indices from one input with gz.Fanout.

  5. Write your own operator and check it with geoops.testing.check_operator.

Python implementation: CPython
Python version       : 3.13.16
IPython version      : 9.13.0

numpy     : 2.4.5
matplotlib: 3.10.9
pipekit   : 0.0.2
geocatalog: 0.4.0
geotoolz  : 0.9.0

Compiler    : GCC 13.3.0
OS          : Linux
Release     : 6.18.44-fc-v80
Machine     : x86_64
Processor   : x86_64
CPU cores   : 4
Architecture: 64bit

1. Load the South Shore box through the catalog

The scene comes from the same catalog search as the other two notebooks. Load green, red and NIR at their native 10 m, plus the 20 m scene classification layer (SCL). SCL holds class labels, so it is upsampled with nearest-neighbour resampling.

scene: (4, 1000, 1000) uint16, crs=EPSG:32610

2. Chain operators with Sequential

a | b builds a Sequential that runs a, then b. Keep the three reflectance bands, decode the stored values (reflectance × 10 000) with gz.DNToReflectance, then compute gz.NDVI.

Sequential([SelectBands(bands=[0, 1, 2], axis=-3), DNToReflectance(scale=0.0001, offset=0.0, axis=-3), NDVI(red=1, nir=2, axis=-3, eps=1e-10)])
ndvi: (1000, 1000) float64, range [-0.27, 0.72]

3. Build a cloud mask from SCL

gz.S2SCL returns a boolean mask where True means drop the pixel, the polarity every geotoolz mask uses (see Mask polarity). By default it drops every class except vegetation, bare soil and water.

DARK_AREA_PIXELS   0.9%
VEGETATION       48.0%
NOT_VEGETATED    16.2%
WATER            33.2%
UNCLASSIFIED      1.7%
dropped: 2.6% of pixels

This late-September scene is nearly cloud-free, so the mask mostly drops dark (shaded) and unclassified pixels. No NO_DATA class appears, so the tile covers the whole box.

4. Wire both branches as a Graph

NDVI and the mask both start from the same scene and meet in gz.ApplyMask. A linear Sequential cannot express that, so call the operators on a symbolic gz.Input and wrap the result in a gz.Graph.

{'ndvi': ((1000, 1000), 'float64'), 'drop': ((1000, 1000), 'bool'), 'clean': ((1000, 1000), 'float64')}
<Figure size 1800x500 with 4 Axes>

5. Several indices from one input with Fanout

gz.Fanout runs each named operator on the same input and returns a dict. Here it adds the McFeeters water index, gz.NDWI, which uses the green band. Water has NDWI above 0, so NDWI > 0 should agree with SCL’s own WATER class.

water: NDWI 33.1%, SCL 33.2%; agree 98.9%
<Figure size 1200x500 with 2 Axes>

6. Write your own operator

The library has no NDVI land-cover classifier, so write one. Subclass gz.Operator, take keyword-only configuration, and put the work in _apply; never override __call__. Rewrap the result with geoops.carrier.wrap_like, which keeps the georeferencing and sets the new fill.

The full walkthrough is Define an operator.

Clear lake water sits near NDVI 0 in this scene, so water_below=0.02 separates it from bare granite.

check_operator runs the library’s own contract checks on your operator. They cover config round-trip, graph mode, fresh attrs, dtype-matched fills and (T, C, H, W) stacks. It raises on the first failure.

contract OK; config = {'water_below': 0.02, 'vegetation_above': 0.25}
{'no data': '2.7%', 'water': '33.5%', 'sparse': '15.3%', 'vegetation': '48.5%'}
<Figure size 1200x500 with 2 Axes>

Summary

  • | chained SelectBands, DNToReflectance and NDVI into one Sequential.

  • S2SCL built a drop mask (True = drop) and ApplyMask wrote NaN into it.

  • A Graph joined the NDVI and mask branches; Fanout added NDWI from the same input.

  • NDVIClasses kept the operator contract, and check_operator proved it.

Next: run this pipeline tile by tile over the whole lake with the patcher, as in the cut notebook. For runtime branching, see Branching pipelines.