name: columbia-glacier
version: "0.2"
category: change-detection
themes: [cryosphere]
family: glacier-terminus-retreat
description: >
  Two-epoch ice extent of Columbia Glacier, Alaska, from Landsat NDSI. One of
  the fastest-retreating tidewater glaciers on record: its terminus has pulled
  back more than 20 km since the early 1980s and the fjord it vacated is now
  open water.

# WHAT THIS MEASURES, AND WHAT IT DOES NOT. READ BEFORE CITING A NUMBER.
#   NDSI > threshold separates SNOW AND ICE FROM MOST OTHER SURFACES; it does
#   not separate glacier ice from seasonal snow, and thin cloud can pass it.
#   The answer below is a count of proxy-ice pixels at 30 m (0.09 ha each), not
#   a mapped glacier or shelf area, and it carries no uncertainty estimate.
#
#   No per-year axis. Each epoch is ONE median composite over its window, so the
#   answer is a two-epoch comparison, not an annual series. A genuine annual
#   series needs a per-year grouping reduction, and no shipped op provides one
#   (recipe-library tracker, defect 63).
#
#   UPSTREAM BUG (recipe-library tracker, defects 61/62). Some Landsat 7 (LE07)
#   assets on the Planetary Computer answer an HTML error page where a COG
#   should be, and the read dies as "Unknown byte order marker: [3C, 21]" (`<!`).
#   Confirmed 2026-08-17 on LE07_L2SP_036035_20221027_02_T1. There is no
#   platform filter to exclude L7 with: the source params carry none, and the
#   `query.platform` block the previous version of this recipe declared was
#   silently ignored. The only lever an author has is the calendar. Windows that
#   overlap 1999-2023 can pull L7 and can therefore hit this.
#
#   No month filter. The engine's source params carry no month selector, so the
#   seasonal windows below are calendar spans and off-season scenes do enter the
#   median. A median over many scenes is robust to that; a mean would not be.

settings:
  ndsi_threshold: 0.4        # NDSI > this → snow/ice (the conventional cutoff)
  cloud_cover_max_pct: 30    # eo:cloud_cover upper bound on the STAC search
  pixel_area_m2: 900         # 30 m Landsat pixel — multiply `count` by this for area


inputs:
  aoi:
    bbox: [-147.4, 60.85, -146.75, 61.35]
    crs_target: EPSG:32606
    notes: >
      Columbia Bay and the lower glacier, Prince William Sound. Late-summer
      windows only: winter snow covers the whole scene and the proxy saturates.


  baseline_epoch:
    start: "1986-07-15"
    end: "1990-09-30"
    notes: Landsat 5 TM, before the retreat accelerated.

  recent_epoch:
    start: "2021-07-15"
    end: "2025-09-30"
    notes: Landsat 8/9 OLI, after ~20 km of terminus retreat.

  collection:
    uri: "@usgs/landsat/c2-l2"

layers:

  # ── SOURCE ──────────────────────────────────────────────────────────────────
  #
  # Bands are CANONICAL concept codes from registry/datasets/usgs/landsat/c2-l2/
  # _dataset.yaml (G = green, S1 = swir-1, QA = qa-pixel). Each distribution maps them onto its own
  # producer asset keys, so ONE band list is correct across TM, ETM+ and OLI —
  # which the previous SR_Bx spelling was not (SR_B5 is SWIR-1 on TM/ETM+ and
  # NIR on OLI, so the old files asked for a different physical band per sensor).
  #
  # The FUSED per-scene form (`scenes: true` + `expression:` + `mask_band:`) is
  # what makes the index correct: the distribution's declared scale/offset is
  # applied per band before the expression, fill DNs become NaN, and QA_PIXEL is
  # the platform mask band, excluded from the b1..bN binding. `temporal_reduce`
  # then takes the median across the surviving scenes.
  #
  # Do NOT collapse this to a bare `bands: [A, B, QA]` source and difference the
  # result. A source read emits the DECLARED BANDS (measured 2026-08-17: three
  # bands out for a three-band request), so `b2 - b1` there is raw band math,
  # not a normalized-difference change. It plans green and answers plausibly.

  source/ndsi-baseline-scenes:
    type: raster
    description: Per-scene NDSI, QA-masked, baseline epoch.
    uri: "@usgs/landsat/c2-l2"
    params:
      scenes: true
      bands: [G, S1, QA]
      mask_band: QA
      expression: "(b1 - b2) / (b1 + b2)"
      bbox: [-147.4, 60.85, -146.75, 61.35]
      temporal: ["1986-07-15", "1990-09-30"]
      cloud_lt: 30
      resolution: 30
      limit: 60

  source/ndsi-recent-scenes:
    type: raster
    description: Per-scene NDSI, QA-masked, recent epoch.
    uri: "@usgs/landsat/c2-l2"
    params:
      scenes: true
      bands: [G, S1, QA]
      mask_band: QA
      expression: "(b1 - b2) / (b1 + b2)"
      bbox: [-147.4, 60.85, -146.75, 61.35]
      temporal: ["2021-07-15", "2025-09-30"]
      cloud_lt: 30
      resolution: 30
      limit: 60

  # ── COMPUTE ─────────────────────────────────────────────────────────────────

  compute/ndsi-baseline:
    type: raster
    description: Median NDSI over the baseline epoch's scenes.
    compute:
      op: temporal_reduce
      inputs:
        time_series: { layer: source/ndsi-baseline-scenes }
      params:
        method: median

  compute/ndsi-recent:
    type: raster
    description: Median NDSI over the recent epoch's scenes.
    compute:
      op: temporal_reduce
      inputs:
        time_series: { layer: source/ndsi-recent-scenes }
      params:
        method: median

  compute/ndsi-pair:
    type: raster
    description: The two NDSI composites stacked into one 2-band raster (b1 = baseline, b2 = recent).
    compute:
      op: stack_concat
      inputs:
        layers:
          - { layer: compute/ndsi-baseline }
          - { layer: compute/ndsi-recent }

  compute/ndsi-change:
    type: raster
    description: >
      dNDSI = recent - baseline. Strongly negative where ice became open water
      or bare rock — the vacated fjord.
    compute:
      op: raster_calc
      inputs:
        raster: { layer: compute/ndsi-pair }
      params:
        expression: "b2 - b1"
        output_band: dndsi

  compute/ice-baseline:
    type: raster
    description: >
      Proxy snow/ice footprint. Ice keeps the value 1; everything else becomes
      NaN (`b1 / 0.0`), which the stats kernel skips — so the answer layer's
      `count` is the ice-pixel count.
    compute:
      op: raster_calc
      inputs:
        raster: { layer: compute/ndsi-baseline }
      params:
        expression: "where(b1 > ${settings.ndsi_threshold}, 1.0, b1 / 0.0)"
        output_band: ice

  compute/ice-recent:
    type: raster
    description: >
      Proxy snow/ice footprint. Ice keeps the value 1; everything else becomes
      NaN (`b1 / 0.0`), which the stats kernel skips — so the answer layer's
      `count` is the ice-pixel count.
    compute:
      op: raster_calc
      inputs:
        raster: { layer: compute/ndsi-recent }
      params:
        expression: "where(b1 > ${settings.ndsi_threshold}, 1.0, b1 / 0.0)"
        output_band: ice

  # ── ANSWERS ─────────────────────────────────────────────────────────────────

  answer/ice-baseline:
    type: answer
    description: >
      Proxy snow/ice pixel count, baseline epoch. Area = count x 900 m².
    compute:
      op: summary
      inputs:
        raster: { layer: compute/ice-baseline }
      params:
        stats: [count]

  answer/ice-recent:
    type: answer
    description: >
      Proxy snow/ice pixel count, recent epoch. Area = count x 900 m².
    compute:
      op: summary
      inputs:
        raster: { layer: compute/ice-recent }
      params:
        stats: [count]

outputs:
  ice_baseline:
    layer: answer/ice-baseline
    value_kind: answer
    description: Mid-1980s proxy ice pixel count.
  ice_recent:
    layer: answer/ice-recent
    value_kind: answer
    description: Recent proxy ice pixel count. THE answer, read against the baseline.
  ndsi_change:
    layer: compute/ndsi-change
    value_kind: raster
    description: dNDSI raster, recent minus baseline (show-your-work).


license:
  recipe: Apache-2.0
  output_dataset: CC-BY-4.0
  upstream_data:
    - { source: "@usgs/landsat/c2-l2", license: usgs-public-domain }
