# Alpine Treeline Ecotone (ATE) Detection — Method Comparison
#
# Compares two approaches to detecting alpine treeline ecotones:
#
#   1. ATEI (Alpine Treeline Ecotone Index) — Wei et al. (2020)
#      Index-based detection using three NDVI/elevation gradient components
#      combined via logistic regression.
#
#   2. ML Classification (Random Forest) — after Wang et al. (2026)
#      Supervised pixel classification using spectral bands, vegetation
#      indices, and topographic features.
#
# Validated against 268 independent field-identified treeline elevations
# from Malanson & Skibbe (2023) across 66 mountain ranges in the Western
# US and Canada. Each site's treeline was identified visually from Google
# Earth imagery — fully independent of both ATEI and RF.
#
# The each: block fans out ATEI computation across all 268 validation
# sites, estimating treeline elevation at each one. Results are compared
# against the field observations to produce accuracy metrics (r², RMSE).
#
# References:
#   Wei, C., Karger, D.N., Wilson, A.M. (2020). Spatial detection of
#     alpine treeline ecotones in the Western US. RSE, 240, 111672.
#   Wang, G.-G. et al. (2026). ML-based ATE detection on Xue Mountain
#     in Taiwan. Biogeosciences, 23, 623-638.
#   Malanson, G.P. & Skibbe, A.M. (2023). Potential alpine habitat in
#     the western USA based on treeline elevation [Dataset]. Dryad.
#     doi:10.5061/dryad.sqv9s4n9m
#   Wei, C. & Wilson, A.M. (2024). Global Alpine Treeline Elevational
#     Transects [Dataset]. Zenodo. doi:10.5281/zenodo.10047965
#
# Pipeline:
#
#   data/weiss_268_treeline.csv ── validation/ground-truth
#                                       │
#   For EACH of 268 sites:              │
#     Landsat 8 → NDVI ──┐              │
#     USGS NED DEM ───────┤              │
#       C1 (gradient mag) ┤              │
#       C2 (Gaussian NDVI)┤              │
#       C3 (covariation)  ┤              │
#       → ATEI → treeline elev estimate  │
#                    │                   │
#                    └───── validation/comparison
#                           (predicted vs observed, r², RMSE)

name: ate-analysis
version: "2.0"
description: >
  Alpine treeline ecotone detection validated against 268 independent
  field sites. Computes ATEI (Wei et al. 2020) at each site using
  folia's each: fan-out, then compares predicted treeline elevation
  against Malanson & Skibbe (2023) ground truth.

# ============================================================
# Reproduction metadata (ADR-0032)
#
# This block declares provenance, quality tier, and the registry
# operations used to reproduce the paper's methodology. It does
# not affect pipeline execution — it is consumed by the catalog
# ingestion layer when publishing to data.folia.sh.
# ============================================================
reproduction:
  paper_doi: "10.1016/j.rse.2020.111672"
  paper_title: "Wei et al. 2020 — Spatial detection of alpine treeline ecotones in the Western US"
  quality_tier: human          # recipe | agent | human | peer | canonical
  validation_status: validated
  method_ops:
    - buffer_point
    - landsat8_ndvi
    - load_dem
    - atei_components
    - atei_index
    - atei_weighted_elevation
    - table_join
  validation_sites: 268
  validation_source: "Malanson & Skibbe (2023), doi:10.5061/dryad.sqv9s4n9m"
  additional_papers:
    - doi: "10.5194/bg-23-623-2026"
      title: "Wang et al. 2026 — ML-based ATE detection (RF comparison)"
    - doi: "10.5061/dryad.sqv9s4n9m"
      title: "Malanson & Skibbe 2023 — Ground truth dataset (Dryad)"
    - doi: "10.5281/zenodo.10047965"
      title: "Wei & Wilson 2024 — Global alpine treeline transects (Zenodo)"

settings:
  default_crs: EPSG:4326
  year_start: 2013
  year_end: 2022

layers:

  # ============================================================
  # GROUND TRUTH — Malanson & Skibbe (2023)
  #
  # 268 treeline elevations across 66 mountain ranges in the
  # Western US + Canada. Identified from Google Earth imagery
  # by visual interpretation. Independent of ATEI/NDVI methods.
  # ============================================================

  validation/ground-truth:
    type: table
    uri: data/weiss_268_treeline.csv
    description: >
      268 field-identified treeline elevations from Malanson & Skibbe
      (2023, Dryad doi:10.5061/dryad.sqv9s4n9m). Each point is the
      treeline position on one aspect (N/E/S/W) of a mountain range,
      identified visually from Google Earth high-resolution imagery.
      Columns: site_id, lat, lon, treeline_elev (m), IDW, kriging.

  # ============================================================
  # ATEI AT EACH VALIDATION SITE (each: fan-out)
  #
  # For each of the 268 sites, we:
  #   1. Buffer the point by 5km to create a local study area
  #   2. Load Landsat 8 NDVI and USGS NED DEM
  #   3. Compute ATEI (C1 + C2 + C3 → logistic regression)
  #   4. Estimate treeline elevation as the ATEI-weighted mean
  #      elevation (Eq. 6 in Wei et al. 2020)
  #
  # This produces one estimated treeline elevation per site,
  # directly comparable to the ground truth.
  # ============================================================

  atei/per-site:
    type: table
    each:
      source:
        type: csv
        path: data/weiss_268_treeline.csv
      key: site_id
    compute:
      engine: gee
      steps:
        # Buffer site point → 5km study area
        - op: buffer_point
          params:
            lat: $each.lat
            lon: $each.lon
            radius_km: 5

        # Landsat 8 C2 L2 — greenest pixel composite
        - op: landsat8_ndvi
          params:
            year_start: "${year_start}"
            year_end: "${year_end}"
            smooth_kernel: { type: circle, radius: 10, units: pixels }

        # USGS NED elevation
        - op: load_dem
          params:
            source: USGS/NED
            smooth_kernel: { type: circle, radius: 10, units: pixels }

        # ATEI components (Wei et al. 2020)
        - op: atei_components
          params:
            gaussian_b: 0.44     # C2 peak NDVI
            gaussian_c: 0.06     # C2 width
            covariation_n: 10    # C3 exponent
            theta_smooth: 3      # C3 direction smoothing radius

        # ATEI logistic regression
        - op: atei_index
          params:
            beta_0: -1.47
            beta_1: 0.44
            beta_2: 0.58
            beta_3: 0.56
            threshold: 0.357

        # Estimate treeline elevation (Wei et al. 2020, Eq. 6)
        # ATEE_j = Σ(E_ij × ATEI_ij) / Σ(ATEI_ij)
        # ATEI-weighted average elevation within the 5km buffer
        - op: atei_weighted_elevation
    reduce:
      mode: concat

  # ============================================================
  # VALIDATION — Predicted vs Observed
  # ============================================================

  validation/comparison:
    type: table
    description: >
      Join ATEI-estimated treeline elevation with ground truth.
      Metrics: Pearson's r, RMSE, mean absolute error.
      Wei et al. (2020) achieved r = 0.98 and RMSE ≈ 170m at
      22 sites from Weiss et al. (2015). This extends validation
      to 268 sites from Malanson & Skibbe (2023).
    compute:
      op: table_join
      inputs:
        left: { layer: atei/per-site }
        right: { layer: validation/ground-truth }
      params:
        on: site_id
        metrics: [pearson_r, rmse, mae, bias]
    style:
      chart:
        type: scatter
        x: treeline_elev        # observed (ground truth)
        y: atei_elev_estimate   # predicted (ATEI)
        identity_line: true
        xLabel: "Field-Observed Treeline Elevation (m)"
        yLabel: "ATEI-Estimated Treeline Elevation (m)"
        title: "ATEI Validation: 268 Sites (Malanson & Skibbe 2023)"

  # ============================================================
  # SINGLE-SITE DETAIL VIEW (Glacier NP)
  #
  # For interactive exploration of the ATEI components and
  # RF comparison at one representative location.
  # ============================================================

  detail/ndvi:
    type: raster
    description: >
      Landsat 8 smoothed annual max NDVI at Glacier NP for
      interactive inspection of the ATEI input data.
    compute:
      engine: gee
      steps:
        - op: landsat8_ndvi
          params:
            bbox: [-113.8, 48.55, -113.2, 48.85]
            year_start: "${year_start}"
            year_end: "${year_end}"
            smooth_kernel: { type: circle, radius: 10, units: pixels }
    style:
      colorRamp:
        - { value: 0.0, color: "#d7191c" }
        - { value: 0.2, color: "#fdae61" }
        - { value: 0.4, color: "#ffffbf" }
        - { value: 0.6, color: "#a6d96a" }
        - { value: 0.8, color: "#1a9641" }

  detail/atei:
    type: raster
    description: >
      ATEI probability surface at Glacier NP. Treeline should
      appear as a band of high ATEI (>0.357) around 1960-2040m,
      matching the Malanson ground truth points in this area.
    compute:
      engine: gee
      steps:
        - op: atei_full
          params:
            bbox: [-113.8, 48.55, -113.2, 48.85]
            year_start: "${year_start}"
            year_end: "${year_end}"
    style:
      colorRamp:
        - { value: 0.0, color: "#2166ac" }
        - { value: 0.2, color: "#67a9cf" }
        - { value: 0.357, color: "#fddbc7" }
        - { value: 0.5, color: "#ef8a62" }
        - { value: 0.8, color: "#b2182b" }
