Note
Go to the end to download the full example code.
Flux Post-Processing Chain - NEE Partitioning (Level 4.2)¶
After gap-filling (Level 4.1) the chain can split the gap-filled net ecosystem exchange (NEE) into its gross components, gross primary production (GPP) and ecosystem respiration (RECO), at Level 4.2.
Four faithful partitioning ports are wired in, each its own composable callable (mirroring the four L4.1 gap-fillers):
run_level42_nighttime_oneflux->*_NT_OF(Reichstein 2005, ONEFlux)run_level42_nighttime_reddyproc->*_NT_RP(Reichstein 2005, REddyProc)run_level42_daytime_reddyproc->*_DT_RP(Lasslop 2010, REddyProc)run_level42_daytime_oneflux->*_DT_OF(Lasslop 2010, ONEFlux)
Partitioning runs once per USTAR scenario; each variant’s output columns get
the scenario label appended (RECO_NT_OF_CUT_50) so all scenarios coexist.
The nighttime variants need a gap-filled NEE for the GPP residual and read
it from the L4.1 method named by partition_gapfill_method (default
'mds'). The daytime variants fit measured NEE. Daytime ONEFlux also
reads the gap-filled NEE, where L4.1 ran, for windows without measured
nighttime NEE.
To run one variant only, set partition_method='nighttime' or
'daytime' with partition_mode='oneflux' (default, the ICOS and
FLUXNET2015 approach) or 'reddyproc'. On the composable path the same
switches are run_level42_partitioning(data, method=..., mode=...).
This example uses the single-call run_chain path; the same four callables
are available on the composable per-level API (see
postprocessingchain_composable.py).
Imports¶
from diive.configs.exampledata import load_exampledata_parquet_lae_level1_30MIN
from diive.flux.postprocessingchain import (
FluxConfig,
add_driver,
init_flux_data,
run_chain,
)
Load data¶
The full CH-LAE 2024 year — partitioning fits the temperature / light
response over time, so it needs a real seasonal cycle (a single month would
leave most records unpartitioned). We drop the reserved SW_IN_POT /
DAYTIME / NIGHTTIME columns so init_flux_data can recompute them.
df = load_exampledata_parquet_lae_level1_30MIN()
df = df.drop(columns=[c for c in ('SW_IN_POT', 'DAYTIME', 'NIGHTTIME')
if c in df.columns])
> Loaded .parquet file
/home/docs/checkouts/diive/checkouts/latest/diive/configs/exampledata/exampledat
a_PARQUET_CH-LAE_22.4_FLUXES_LEVEL-1_IRGA72+METEO7_2024_SUBSET.parquet (0.007
seconds).
Register VPD in kPa¶
Both MDS (L4.1) and the daytime partitioning variants want VPD in kPa,
which the input VPD here already is. Register it once via add_driver so the
column lands in data.full_df where the levels read their drivers (divide
by 10 first if your VPD is in hPa).
data = init_flux_data(
df=df,
fluxcol="FC",
site_lat=47.478333, # CH-LAE
site_lon=8.364389,
utc_offset=1,
nighttime_threshold=20,
daytime_accept_qcf_below=2,
nighttime_accept_qcf_below=2,
)
vpd_kpa = df['VPD_T1_47_1_gfXG'].rename('VPD_kPa')
data = add_driver(data, vpd_kpa)
────────────────────────── Initializing flux data: FC ──────────────────────────
> Detected base variable CO2 for FC. (CO2 was used to calculate FC.)
> Potential radiation calculated (SW_IN_POT)
> Day/night flags: DAYTIME, NIGHTTIME
Build the FluxConfig¶
We gap-fill with MDS only (fast) and enable all four partitioning variants.
The partition_* driver fields point at columns in data.full_df:
partition_ta/partition_sw_in— measured TA / SW_IN (used for the day/night split and the fits in three of the four variants). This LAE subset ships only the gap-filled meteo, so we reuse the gap-filled columns here; a real workflow would point these at the raw measured series.partition_ta_f/partition_sw_in_f/partition_vpd_f— gap-filled meteo drivers.
The nighttime variants additionally consume the L4.1 gap-filled NEE selected
by partition_gapfill_method (here MDS).
cfg = FluxConfig(
fluxcol='FC',
ustar_thresholds=[0.07],
ustar_labels=['CUT_50'],
level2_test_settings={'ssitc': {'apply': True, 'setflag_timeperiod': None}},
# L4.1 — MDS only (gap-filled NEE for the nighttime GPP residual).
gapfill_mds=True,
gapfill_rf=False,
gapfill_xgb=False,
mds_swin='SW_IN_T1_47_1_gfXG',
mds_ta='TA_T1_47_1_gfXG',
mds_vpd='VPD_kPa',
# L4.2 — all four partitioning variants.
partition_nighttime_oneflux=True,
partition_nighttime_reddyproc=True,
partition_daytime_reddyproc=True,
partition_daytime_oneflux=True,
partition_gapfill_method='mds', # which L4.1 NEE feeds the nighttime variants
partition_ta='TA_T1_47_1_gfXG', # measured TA (stand-in: gap-filled here)
partition_sw_in='SW_IN_T1_47_1_gfXG', # measured SW_IN (stand-in: gap-filled here)
partition_ta_f='TA_T1_47_1_gfXG', # gap-filled TA
partition_sw_in_f='SW_IN_T1_47_1_gfXG', # gap-filled SW_IN
partition_vpd_f='VPD_kPa', # gap-filled VPD (kPa)
)
Run the chain¶
One call drives L2 -> L3.1 -> L3.2 -> L3.3 -> L4.1 -> L4.2. The summary includes a partitioning section (RECO / GPP valid-record counts per variant and USTAR scenario).
data = run_chain(data, cfg)
print(f"\nlevels run: {data.level_ids}")
print(data.summary())
─────────────────────── run_chain: full pipeline for FC ────────────────────────
─────────────────────── Level 2: Quality Flag Expansion ────────────────────────
> SSITC TEST: Generated new flag variable FLAG_L2_FC_SSITC_TEST, values taken
from output variable FC_SSITC_TEST ...
──────────────────────── Level 3.1: Storage Correction ─────────────────────────
> Calculating storage-corrected flux NEE_L3.1 from flux FC + storage term
SC_SINGLE
> ++Added flag column FLAG_L3.2_NEE_L3.1_QCF_OUTLIER_HAMPEL_TEST to flag data
──────────────────── Level 3.2: Stepwise Outlier Detection ─────────────────────
────────────────────────── Level 3.3: USTAR Filtering ──────────────────────────
───────────────────────── Level 4.1: Gap-Filling (MDS) ─────────────────────────
─────── Level 4.2: NEE Partitioning (Nighttime ONEFlux, Reichstein 2005) ───────
────── Level 4.2: NEE Partitioning (Nighttime REddyProc, Reichstein 2005) ──────
──────── Level 4.2: NEE Partitioning (Daytime REddyProc, Lasslop 2010) ─────────
───────── Level 4.2: NEE Partitioning (Daytime ONEFlux, Lasslop 2010) ──────────
levels run: ['L2', 'L3.1', 'L3.2', 'L3.3', 'L4.1', 'L4.2']
Data availability summary — flux: 'FC'
Site: lat=47.478333, lon=8.364389, UTC+1
QCF thresholds: daytime accept QCF < 2 | nighttime accept QCF < 2
(QCF=0: all tests pass; QCF=1: soft warnings; QCF=2: hard failure)
Total records: 17568
Daytime records: 8748
Nighttime records: 8820
L2 (after QC flags) 13251 valid ( 75.4%) | day: 6882 night: 6369
L2 (QCF=0 only, high-quality) 5856 valid ( 33.3%) | day: 3430 night: 2426
L3.1 (storage-corrected) 13251 valid ( 75.4%) | day: 6882 night: 6369
L3.2 (outlier-cleaned) 12813 valid ( 72.9%) | day: 6694 night: 6119
L3.3 USTAR=CUT_50 12697 valid ( 72.3%) | day: 6660 night: 6037
L3.3 USTAR=CUT_50 (QCF=0 only) 5741 valid ( 32.7%) | day: 3379 night: 2362
Gap-filling (L4.1) [measured / gap-filled / fallback]:
mds CUT_50 total: 12697 | measured: 12697 | gap-filled: 0 (0.0%)
Partitioning (L4.2) [RECO / GPP valid records]:
nt_of CUT_50 RECO: 17568 | GPP: 17568
nt_rp CUT_50 RECO: 17568 | GPP: 17568
dt_rp CUT_50 RECO: 17568 | GPP: 17568
dt_of CUT_50 RECO: 17568 | GPP: 17568
fpc_df columns (66): FC, USTAR, SW_IN_POT, DAYTIME, NIGHTTIME, FLAG_L2_FC_MISSING_TEST, FLAG_L2_FC_SSITC_TEST, SUM_L2_FC_HARDFLAGS, SUM_L2_FC_SOFTFLAGS, SUM_L2_FC_FLAGS, FLAG_L2_FC_QCF, FC_L2_QCF, FC_L2_QCF0, SC_SINGLE, SC_SINGLE_gfRMED_L3.1, FLAG_SC_SINGLE_gfRMED_L3.1_ISFILLED, NEE_L3.1, SUM_L3.1_NEE_HARDFLAGS, SUM_L3.1_NEE_SOFTFLAGS, SUM_L3.1_NEE_FLAGS, FLAG_L3.1_NEE_QCF, NEE_L3.1_QCF, NEE_L3.1_QCF0, FLAG_L3.2_NEE_L3.1_QCF_OUTLIER_HAMPEL_TEST, SUM_L3.2_NEE_L3.1_HARDFLAGS, SUM_L3.2_NEE_L3.1_SOFTFLAGS, SUM_L3.2_NEE_L3.1_FLAGS, FLAG_L3.2_NEE_L3.1_QCF, NEE_L3.1_L3.2_QCF, NEE_L3.1_L3.2_QCF0, FLAG_L3.3_CUT_50_NEE_L3.1_USTAR_TEST, SUM_L3.3_CUT_50_NEE_L3.1_HARDFLAGS, SUM_L3.3_CUT_50_NEE_L3.1_SOFTFLAGS, SUM_L3.3_CUT_50_NEE_L3.1_FLAGS, FLAG_L3.3_CUT_50_NEE_L3.1_QCF, NEE_L3.1_L3.3_CUT_50_QCF, NEE_L3.1_L3.3_CUT_50_QCF0, NEE_L3.1_L3.3_CUT_50_QCF_gfMDS, FLAG_NEE_L3.1_L3.3_CUT_50_QCF_gfMDS_ISFILLED, NEE_NIGHT_OF_CUT_50, RECO_NT_OF_CUT_50, RECO_NT_OF_ROB_CUT_50, GPP_NT_OF_CUT_50, GPP_NT_OF_ROB_CUT_50, RREF_NT_OF_CUT_50, E0_NT_OF_CUT_50, NEE_NIGHT_RP_CUT_50, RECO_NT_RP_CUT_50, GPP_NT_RP_CUT_50, RREF_NT_RP_CUT_50, E0_NT_RP_CUT_50, RECO_DT_RP_CUT_50, GPP_DT_RP_CUT_50, K_DT_RP_CUT_50, BETA_DT_RP_CUT_50, ALPHA_DT_RP_CUT_50, RREF_DT_RP_CUT_50, E0_DT_RP_CUT_50, RECO_DT_OF_CUT_50, GPP_DT_OF_CUT_50, SE_GPP_DT_OF_CUT_50, ALPHA_DT_OF_CUT_50, BETA_DT_OF_CUT_50, K_DT_OF_CUT_50, RREF_DT_OF_CUT_50, E0_DT_OF_CUT_50
Discover the partitioning columns¶
partitioned_cols() returns {variant: {ustar_scenario: [column, ...]}}
— the canonical way to find the RECO / GPP columns in fpc_df without
digging through the instances. data.levels.level42_<variant>[scenario]
holds each fitted partitioning instance for inspection.
nt_of / CUT_50: ['NEE_NIGHT_OF_CUT_50', 'RECO_NT_OF_CUT_50', 'RECO_NT_OF_ROB_CUT_50', 'GPP_NT_OF_CUT_50', 'GPP_NT_OF_ROB_CUT_50', 'RREF_NT_OF_CUT_50', 'E0_NT_OF_CUT_50']
nt_rp / CUT_50: ['NEE_NIGHT_RP_CUT_50', 'RECO_NT_RP_CUT_50', 'GPP_NT_RP_CUT_50', 'RREF_NT_RP_CUT_50', 'E0_NT_RP_CUT_50']
dt_rp / CUT_50: ['RECO_DT_RP_CUT_50', 'GPP_DT_RP_CUT_50', 'K_DT_RP_CUT_50', 'BETA_DT_RP_CUT_50', 'ALPHA_DT_RP_CUT_50', 'RREF_DT_RP_CUT_50', 'E0_DT_RP_CUT_50']
dt_of / CUT_50: ['RECO_DT_OF_CUT_50', 'GPP_DT_OF_CUT_50', 'SE_GPP_DT_OF_CUT_50', 'ALPHA_DT_OF_CUT_50', 'BETA_DT_OF_CUT_50', 'K_DT_OF_CUT_50', 'RREF_DT_OF_CUT_50', 'E0_DT_OF_CUT_50']
Compare RECO across the four methods¶
The four ports do not produce identical numbers (different reference
implementations and day/night splits), so comparing their daily-mean RECO is
a useful sanity check. Headless here (showplot left to the caller).
import matplotlib.pyplot as plt
reco_cols = {
'Nighttime ONEFlux': 'RECO_NT_OF_CUT_50',
'Nighttime REddyProc': 'RECO_NT_RP_CUT_50',
'Daytime REddyProc': 'RECO_DT_RP_CUT_50',
'Daytime ONEFlux': 'RECO_DT_OF_CUT_50',
}
fig, ax = plt.subplots(figsize=(13, 5))
for label, col in reco_cols.items():
if col in data.fpc_df.columns:
ax.plot(data.fpc_df[col].resample('D').mean(), lw=1.2, label=label)
ax.axhline(0, color='#455A64', lw=0.8)
ax.set_ylabel('RECO (umol m-2 s-1)')
ax.set_title('L4.2 RECO by partitioning method - CH-LAE 2024 daily means (CUT_50)')
ax.legend(ncol=2, frameon=False)
# plt.show() # disabled for headless example runs

<matplotlib.legend.Legend object at 0x76aa8e802b70>
Total running time of the script: (0 minutes 8.220 seconds)