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
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")
using EasyHybrid
using CairoMakieData
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.
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.
df = flux_features(raw);
first(df, 5)| Row | DateTime | NEE | TA | SW_IN | SW_IN_POT | SW_IN_POT_diff | RH_F | NEE_night_movmean | NEE_light_night_diff | TA_night_movmean | NEE_SWin_lowlight_lm_slope | NEE_RUE_at_max_SW | SWC_capa_1 | SWC_capa_5 | SWC_capa_25 | SWC_capa_125 | SWC_capa_625 | cosHR | sinHR | sin_1.0_year | cos_1.0_year | sin_2.0_year | cos_2.0_year | sin_3.0_year | cos_3.0_year | sin_4.0_year | cos_4.0_year | sin_5.0_year | cos_5.0_year | RECO_NT_VUT_USTAR50 | GPP_NT_VUT_USTAR50 | RECO_DT_VUT_USTAR50 | GPP_DT_VUT_USTAR50 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| DateTime | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | Float32 | |
| 1 | 2018-01-01T00:00:00 | 1.363 | 7.133 | 0.0 | 0.0 | 0.0 | 0.78682 | 1.507 | 7.644 | 5.843 | -0.0325059 | -0.021297 | 0.999137 | 4.99914 | 24.9991 | 124.999 | 624.999 | 1.0 | 0.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 1.50658 | 0.143584 | 1.42089 | 0.0 |
| 2 | 2018-01-01T00:30:00 | NaN | 6.37 | 0.0 | 0.0 | 0.0 | 0.818767 | 1.507 | 7.7285 | 5.843 | -0.0325176 | -0.0213084 | 0.999005 | 4.99901 | 24.999 | 124.999 | 624.999 | 0.991445 | 0.130526 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 1.4107 | -0.208342 | 1.37812 | 0.0 |
| 3 | 2018-01-01T01:00:00 | NaN | 5.843 | 0.0 | 0.0 | 0.0 | 0.815737 | 1.507 | 7.813 | 5.843 | -0.0325292 | -0.0213152 | 0.998295 | 4.9983 | 24.9983 | 124.998 | 624.998 | 0.965926 | 0.258819 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 1.34653 | -0.277466 | 1.34862 | 0.0 |
| 4 | 2018-01-01T01:30:00 | 2.238 | 7.0 | 0.0 | 0.0 | 0.0 | 0.795362 | 1.507 | 8.2675 | 5.843 | -0.032532 | -0.0213219 | 0.994038 | 4.99404 | 24.994 | 124.994 | 624.994 | 0.92388 | 0.382683 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 1.48962 | -0.748384 | 1.41343 | 0.0 |
| 5 | 2018-01-01T02:00:00 | 2.633 | 7.097 | 0.0 | 0.0 | 0.0 | 0.7989 | 1.507 | 8.053 | 5.843 | -0.0325348 | -0.0213152 | 0.99199 | 4.99199 | 24.992 | 124.992 | 624.992 | 0.866025 | 0.5 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 1.50198 | -1.13102 | 1.41887 | 0.0 |
Process-based Model
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)
endflux_part_mechanistic_model (generic function with 1 method)Parameter specification: (default, lower_bound, upper_bound)
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
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 dataTraining
Rows without measured NEE are dropped.
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.91477674Partitioned 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.
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:833plot_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.
daily = combine_flux_partition(raw, pred; scale = :daily, labels = ["UN"]);
fig = plot_flux_partition(raw, pred; scale = :daily, labels = ["UN"])
Diurnal cycle
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⁻¹]",
)