Note
Go to the end to download the full example code.
Random Uncertainty Estimation (PAS20 Method)¶
Calculate random uncertainty in flux measurements using the PAS20 method.
Demonstrates hierarchical uncertainty quantification for eddy covariance flux data with four methods. Methods 1 and 2 are the ONEFlux (PAS20) methods: the SD of measured fluxes under similar meteorological conditions, then the median of these uncertainties for similar fluxes. Methods 3 and 4 are diive extensions that relax the matching further, so every record gets an estimate.
Best for: Assessing measurement reliability and random error propagation in flux data.
Load data and calculate random uncertainty¶
Initialize RandomUncertaintyPAS20 and run the 4-method hierarchical uncertainty analysis.
import diive as dv
import matplotlib.pyplot as plt
# Load example data
data_df = dv.load_exampledata_parquet()
# Use subset only - March 2013 (smaller dataset for faster demo)
data_df = data_df.loc[(data_df.index.year == 2013) & (data_df.index.month == 3)].copy()
# Prepare subset with required columns
subset = data_df[[
'NEE_CUT_REF_orig', 'NEE_CUT_REF_f',
'Tair_f', 'VPD_f', 'Rg_f'
]].copy()
# The example VPD_f is in hPa; RandomUncertaintyPAS20 expects kPa (diive
# convention, vpd_in_kpa=True), so convert it.
subset['VPD_f'] = subset['VPD_f'].multiply(0.1)
print("=" * 80)
print("Random Uncertainty Estimation (PAS20 Method)")
print("=" * 80)
print(f"\nData shape: {subset.shape}")
print(f"Valid records: {subset['NEE_CUT_REF_orig'].count()}")
print(f"Missing values: {subset['NEE_CUT_REF_orig'].isnull().sum()}")
> Loaded .parquet file
/home/docs/checkouts/diive/checkouts/latest/diive/configs/exampledata/exampledat
a_PARQUET_CH-DAV_FP2022.5_2013-2022_ID20230206154316_30MIN.parquet (0.050
seconds).
================================================================================
Random Uncertainty Estimation (PAS20 Method)
================================================================================
Data shape: (1488, 5)
Valid records: 511
Missing values: 977
Initialize and run uncertainty calculator¶
Run the 4-method hierarchical uncertainty quantification.
randunc = dv.flux.RandomUncertaintyPAS20(
df=subset,
fluxcol='NEE_CUT_REF_orig',
fluxgapfilledcol='NEE_CUT_REF_f',
tacol='Tair_f',
vpdcol='VPD_f',
swincol='Rg_f'
)
print("\nRunning 4-method hierarchical uncertainty quantification...")
randunc.run()
print("[OK] Uncertainty calculation complete\n")
Running 4-method hierarchical uncertainty quantification...
> Calculating random uncertainty with window size +/-7 days and +/-1 hours
(method 1) ...
> Time needed: 0.01s
> Calculating random uncertainty with window size +/-14 days (method 2) ...
> Time needed: 0.03s
> Calculating random uncertainty from similar fluxes (method 3) ...
> Time needed: 0.00s
> Calculating random uncertainty from similar fluxes (method 4) ...
> Time needed: 0.00s
[OK] Uncertainty calculation complete
Examine results¶
Get uncertainty results and view method summary.
randunc_series = randunc.randunc_series
randunc_results = randunc.randunc_results
# Report: Method summary with distribution
randunc.report_method_summary()
================================================================================
RANDOM UNCERTAINTY QUANTIFICATION - 4-METHOD SUMMARY
================================================================================
Total Records: 1,488 | Measured: 511 | Gap-filled: 977
METHOD DISTRIBUTION:
Method Records Percentage
1: Sliding Window (+/-7d, +/-1h) [ONEFlux] 511 34.3%
2: Similar Flux Median (+/-14d) [ONEFlux] 1313 88.2%
3: Similar Flux Median (no window) 2 0.1%
4: Nearest Fluxes 0 0.0%
UNCERTAINTY STATISTICS (umol CO2 m-2 s-1):
Statistic Value (umol CO2 m-2 s-1)
Mean 1.7886
Std Dev 0.5088
Min 0.3143
Max 5.9600
Range 5.6457
================================================================================
Visualize uncertainty distribution¶
Plot the distribution of random uncertainties.
x = randunc_results['NEE_CUT_REF_orig_RANDUNC'].dropna()
fig, ax = plt.subplots(figsize=(11, 5.5))
fig.subplots_adjust(left=0.08, right=0.97, top=0.93, bottom=0.1)
ax.hist(x, bins=25, rwidth=0.85, color='#607c8e', edgecolor='black', alpha=0.8)
ax.axvline(x.mean(), color='red', linestyle='--', linewidth=2.5, label=f'Mean = {x.mean():.4f}')
ax.axvline(x.median(), color='green', linestyle='--', linewidth=2.5, label=f'Median = {x.median():.4f}')
ax.grid(True, alpha=0.3, linestyle=':', axis='y')
ax.set_title('Distribution of Random Uncertainties (NEE)', fontsize=12, fontweight='bold')
ax.set_xlabel('Random Uncertainty (±sigma) (µmol CO2 m-2 s-1)', fontsize=11)
ax.set_ylabel('Frequency (count)', fontsize=11)
ax.legend(loc='upper right', fontsize=10)
fig.show()

Cumulative uncertainty propagation¶
Visualize cumulative flux with uncertainty bounds over time.
df_cumulative = randunc.randunc_results_cumulatives[
['NEE_CUT_REF_f', 'FLUX+UNC', 'FLUX-UNC']
].copy()
df_cumulative.columns = ['Cumulative Flux', 'Upper Bound (+σ)', 'Lower Bound (-σ)']
fig, ax = plt.subplots(figsize=(14, 5.5))
fig.subplots_adjust(left=0.06, right=0.98, top=0.93, bottom=0.1)
ax.plot(df_cumulative.index, df_cumulative['Cumulative Flux'], linewidth=2.5,
label='Cumulative Flux', color='black', zorder=3)
ax.fill_between(df_cumulative.index, df_cumulative['Lower Bound (-σ)'],
df_cumulative['Upper Bound (+σ)'], alpha=0.3, color='red',
label='Uncertainty Range (±σ)', zorder=1)
ax.plot(df_cumulative.index, df_cumulative['Upper Bound (+σ)'], linewidth=1.5,
linestyle='--', color='red', alpha=0.6, label='Bounds', zorder=2)
ax.plot(df_cumulative.index, df_cumulative['Lower Bound (-σ)'], linewidth=1.5,
linestyle='--', color='red', alpha=0.6, zorder=2)
ax.set_xlabel('Datetime', fontsize=11, fontweight='bold')
ax.set_ylabel('Cumulative NEE (µmol CO2 m-2)', fontsize=11, fontweight='bold')
ax.set_title('Cumulative Uncertainty Propagation with Error Bounds', fontsize=12, fontweight='bold')
ax.legend(loc='best', framealpha=0.95)
ax.grid(True, alpha=0.3, linestyle=':')
fig.show()
print("\n[OK] Random uncertainty estimation complete.")

[OK] Random uncertainty estimation complete.
Total running time of the script: (0 minutes 0.345 seconds)