Skip to content

CC BY-SA 4.0

EasyHybrid Example: NEE Flux Partitioning at ES-LM1 ​

One year (2018) at ES-LM1 (Majadas de Tiétar, Spain). Measured NEE is partitioned into GPP and RECO by    and   . Two neural networks predict and .

Data from the FLUXNET Shuttle (ONEFlux v1.3, CC-BY-4.0).

using Pkg Pkg.activate("/User/homes/bahrens/BGI/scratch/bahrens/projects/EasyHybrid_Xin/docs") Pkg.develop(path = "/Net/Groups/BGI/scratch/bahrens/projects/EasyHybrid_Xin")

julia
using EasyHybrid
using CairoMakie

Data ​

load_fluxnet_shuttle reads half-hourly fluxes and meteorology for 2018. _F columns are gap-filled. NEE_VUT_USTAR50_QC == 0 marks measured NEE. NT and DT products are the reference.

julia
site = "ES-LM1"
years = 2018:2018

raw = load_fluxnet_shuttle(
    site;
    years,
    variables = [
        :TA_F, :SW_IN_F, :SW_IN_POT, :VPD_F, :P_F, :LE_F_MDS,
        :NEE_VUT_USTAR50, :NEE_VUT_USTAR50_QC,
        :RECO_NT_VUT_USTAR50, :GPP_NT_VUT_USTAR50, :RECO_DT_VUT_USTAR50, :GPP_DT_VUT_USTAR50,
    ],
);
size(raw)
(17520, 14)

Features ​

flux_features builds the predictors: meteorology, daily night-time NEE context, light-response slopes, soil-water buckets (soil_bucket), and diurnal and seasonal harmonics. The target keeps measured NEE only. min_x and min_range drop light-response slopes from windows that stay below 200 W/m² or span less than 50 W/m². Pass min_x = 0, min_range = 0 to keep them.

julia
df = flux_features(raw);
first(df, 5)
5×33 DataFrame
RowDateTimeNEETASW_INSW_IN_POTSW_IN_POT_diffRH_FNEE_night_movmeanNEE_light_night_diffTA_night_movmeanNEE_SWin_lowlight_lm_slopeNEE_RUE_at_max_SWSWC_capa_1SWC_capa_5SWC_capa_25SWC_capa_125SWC_capa_625cosHRsinHRsin_1.0_yearcos_1.0_yearsin_2.0_yearcos_2.0_yearsin_3.0_yearcos_3.0_yearsin_4.0_yearcos_4.0_yearsin_5.0_yearcos_5.0_yearRECO_NT_VUT_USTAR50GPP_NT_VUT_USTAR50RECO_DT_VUT_USTAR50GPP_DT_VUT_USTAR50
DateTimeFloat32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32Float32
12018-01-01T00:00:001.3637.1330.00.00.00.786821.5077.6445.843-0.0325059-0.0212970.9991374.9991424.9991124.999624.9991.00.00.01.00.01.00.01.00.01.00.01.01.506580.1435841.420890.0
22018-01-01T00:30:00NaN6.370.00.00.00.8187671.5077.72855.843-0.0325176-0.02130840.9990054.9990124.999124.999624.9990.9914450.1305260.01.00.01.00.01.00.01.00.01.01.4107-0.2083421.378120.0
32018-01-01T01:00:00NaN5.8430.00.00.00.8157371.5077.8135.843-0.0325292-0.02131520.9982954.998324.9983124.998624.9980.9659260.2588190.01.00.01.00.01.00.01.00.01.01.34653-0.2774661.348620.0
42018-01-01T01:30:002.2387.00.00.00.00.7953621.5078.26755.843-0.032532-0.02132190.9940384.9940424.994124.994624.9940.923880.3826830.01.00.01.00.01.00.01.00.01.01.48962-0.7483841.413430.0
52018-01-01T02:00:002.6337.0970.00.00.00.79891.5078.0535.843-0.0325348-0.02131520.991994.9919924.992124.992624.9920.8660250.50.01.00.01.00.01.00.01.00.01.01.50198-1.131021.418870.0

Process-based Model ​

   and   . Two neural networks predict and . 12.011f0 is the unit conversion factor

julia
function flux_part_mechanistic_model(; SW_IN, RECO, RUE)
    # µmol/m2/s = J/s/m2 * g/MJ / g/mol
    GPP = SW_IN .* RUE ./ 12.011f0
    NEE = RECO .- GPP
    return (; NEE, RECO, GPP, RUE)
end
flux_part_mechanistic_model (generic function with 1 method)

Parameter specification: (default, lower_bound, upper_bound)

julia
parameters = (
    RUE = (0.1f0, 0.0f0, 4.0f0),
    RECO = (1.0f0, 0.0f0, 100.0f0),
)
(RUE = (0.1f0, 0.0f0, 4.0f0), RECO = (1.0f0, 0.0f0, 100.0f0))

Hybrid Model ​

One network each for RECO and RUE. Input features for RECO: -diurnal...,seasonal...: diurnal and seasonal harmonics -soil water...: soil water availability based on a bucket model using a range of bucket sizes given observed P and ET -TA and TA_night_movmean: air temperature (TA) and centered moving median of TA at night -NEE_night_movmean: centered moving median of NEE at night with good turbulence conditions Input features for RUE: -standard meteorology: air temperature (TA), relative humidity (RH), incoming shortwave radiation (SW_IN), potential incoming shortwave radiation (SW_IN_POT), and first derivative of SW_IN_POT (SW_IN_POT_diff) -NEE_RUE_at_max_SW: radiation use efficiency at the light-saturated condition -NEE_SWin_lowlight_lm_slope: radiation use efficiency at the light-limited condition -NEE_light_night_diff: NEE difference between day and night -diurnal...,seasonal...: diurnal and seasonal harmonics -soil water...: soil water availability based on a bucket model using a range of bucket sizes given observed P and ET

julia
soilwater = Symbol.("SWC_capa_" .* string.((1, 5, 25, 125, 625)))
diurnal = [:cosHR, :sinHR]
seasonal = Symbol.(vec(string.(["sin_" "cos_"], 1.0:5.0, "_year")))

predictors = (
    RECO = [:NEE_night_movmean, :TA, :TA_night_movmean, soilwater..., seasonal..., diurnal...],
    RUE = [
        :TA, :RH_F, :SW_IN, :SW_IN_POT, :SW_IN_POT_diff,
        :NEE_SWin_lowlight_lm_slope, :NEE_RUE_at_max_SW, :NEE_light_night_diff,
        soilwater..., diurnal..., seasonal...,
    ],
)

forcing = [:SW_IN]
target = [:NEE]
global_param_names = Symbol[]

hybrid_model = constructHybridModel(
    predictors,                 # Input features per NN
    forcing,                    # Forcing variables
    target,                     # Target variables
    flux_part_mechanistic_model, # Process-based model function
    parameters,                 # Parameter definitions
    global_param_names,         # Global parameters
    scale_nn_outputs = true,
    hidden_layers = [15],
    activation = tanh,
    input_batchnorm = true,
    start_from_default = false,
)
Hybrid Model (Multi NN)
Neural Networks:
  RECO:
    Chain(
        layer_1 = InputBatchNorm(
            layer = BatchNorm(20, affine=false, track_stats=true),
        ),
        layer_2 = Dense(20 => 15, tanh),              # 315 parameters
        layer_3 = Dense(15 => 1),                     # 16 parameters
    )         # Total: 331 parameters,
              #        plus 41 states.
  RUE:
    Chain(
        layer_1 = InputBatchNorm(
            layer = BatchNorm(25, affine=false, track_stats=true),
        ),
        layer_2 = Dense(25 => 15, tanh),              # 390 parameters
        layer_3 = Dense(15 => 1),                     # 16 parameters
    )         # Total: 406 parameters,
              #        plus 51 states.
Configuration:
  predictors:
    RECO = [:NEE_night_movmean, :TA, :TA_night_movmean, :SWC_capa_1, :SWC_capa_5, :SWC_capa_25, :SWC_capa_125, :SWC_capa_625, Symbol("sin_1.0_year"), Symbol("sin_2.0_year"), Symbol("sin_3.0_year"), Symbol("sin_4.0_year"), Symbol("sin_5.0_year"), Symbol("cos_1.0_year"), Symbol("cos_2.0_year"), Symbol("cos_3.0_year"), Symbol("cos_4.0_year"), Symbol("cos_5.0_year"), :cosHR, :sinHR]
    RUE = [:TA, :RH_F, :SW_IN, :SW_IN_POT, :SW_IN_POT_diff, :NEE_SWin_lowlight_lm_slope, :NEE_RUE_at_max_SW, :NEE_light_night_diff, :SWC_capa_1, :SWC_capa_5  …  Symbol("sin_1.0_year"), Symbol("sin_2.0_year"), Symbol("sin_3.0_year"), Symbol("sin_4.0_year"), Symbol("sin_5.0_year"), Symbol("cos_1.0_year"), Symbol("cos_2.0_year"), Symbol("cos_3.0_year"), Symbol("cos_4.0_year"), Symbol("cos_5.0_year")]
  forcing = [:SW_IN]
  targets = [:NEE]
  mechanistic_model = flux_part_mechanistic_model
  neural_param_names = [:RECO, :RUE]
  global_param_names = Symbol[]
  fixed_param_names = Symbol[]
  scale_nn_outputs = true
  start_from_default = false
  config = (; hidden_layers = [15], activation = tanh, scale_nn_outputs = true, input_batchnorm = true, start_from_default = false,)

Parameters:
  ┌──────┬───────┬───────┬───────┬────────┐
  │      │ value │ lower │ upper │  scale │
  ├──────┼───────┼───────┼───────┼────────┤
  │  RUE │    NN │   0.0 │   4.0 │ linear │
  │ RECO │    NN │   0.0 │ 100.0 │ linear │
  └──────┴───────┴───────┴───────┴────────┘
  value: number = fixed; NN/Global = estimated; Forcing = overridden by forcing data

Training ​

Rows without measured NEE are dropped.

julia
out = train(
    hybrid_model,
    df;
    nepochs = 100,
    batchsize = 512,
    opt = Adam(0.03),
    loss_types = [:nse, :mse],
    training_loss = :mse,
    random_seed = 123,
    yscale = identity,
    monitor_names = [:RUE, :RECO, :GPP],
    patience = 10,
    shuffleobs = true,
    show_progress = false,
    plotting = false,
    save_training = false,
    model_name = "FluxPart_$(site)",
);
[ Info: Returning best model from epoch 95 with validation loss: 0.91477674

Partitioned Fluxes ​

Predict every half-hour of 2018. DateTime is left out because the forward pass builds one numeric matrix. Row order matches raw, and measured NEE is read from raw.

julia
pred = hybrid_model(select(df, Not(:DateTime)), out.ps, out.st);
┌ Warning: Only makes sense in test mode, not training!
└ @ EasyHybrid ~/work/EasyHybrid.jl/EasyHybrid.jl/src/models/GenericHybridModel.jl:833

plot_flux_partition shows the same series at daily and hourly scale. Measured NEE uses QC-passed half-hours only, mostly daytime, so a daily mean is biased toward uptake. The time axis is ticked once a month, and the year is written only in front of January.

julia
daily = combine_flux_partition(raw, pred; scale = :daily, labels = ["UN"]);
fig = plot_flux_partition(raw, pred; scale = :daily, labels = ["UN"])

Diurnal cycle ​

julia
qc = isfinite.(df.NEE)
diurnal = DataFrame(
    DateTime = df.DateTime[qc],
    NEE = df.NEE[qc],
    NEE_hybrid = vec(pred.NEE_pred)[qc],
    NEE_DT = (df.RECO_DT_VUT_USTAR50 .- df.GPP_DT_VUT_USTAR50)[qc],
    SW_IN = df.SW_IN[qc],
    SW_IN_POT = df.SW_IN_POT[qc],
);
diurnal.RECO_NT = df.RECO_NT_VUT_USTAR50[qc]
fig_diurnal = diurnalPlot(
    diurnal;
    yvars = [:NEE, :NEE_hybrid, :NEE_DT, :RECO_NT],
    obs = :NEE,
    ylabel = "NEE [μmol m⁻² s⁻¹]",
)