.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/flux/lowres/flux_hqflux.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_flux_lowres_flux_hqflux.py: ================================================== High-Quality Flux Analysis with Hampel Filtering ================================================== Robust outlier detection for CO2 flux using Hampel filter with day/night separation. Demonstrates robust outlier detection for CO2 flux (NEE) using the Hampel filter (Median Absolute Deviation) with automatic day/night separation based on solar geometry. The Hampel method is ideal for removing measurement spikes while preserving ecosystem signal. Best for: Identifying and removing measurement spikes in CO2 flux data. .. GENERATED FROM PYTHON SOURCE LINES 17-21 Load and prepare CO2 flux data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Load example eddy covariance data and select records with good quality flags. .. GENERATED FROM PYTHON SOURCE LINES 21-39 .. code-block:: Python from diive.configs.exampledata import load_exampledata_parquet_cha from diive.flux.lowres.hqflux import analyze_highest_quality_flux df = load_exampledata_parquet_cha() keeprows = df['FC_SSITC_TEST'] == 0 df = df[keeprows].copy() flux_fc = df['FC'].copy() print("=" * 80) print("Data Summary") print("=" * 80) print(f"\nData: {flux_fc.name}") print(f"Period: {flux_fc.index.min().date()} to {flux_fc.index.max().date()}") print(f"Total records: {len(flux_fc)}") print(f"Valid records: {flux_fc.count()}") print(f"Missing: {flux_fc.isnull().sum()} ({flux_fc.isnull().sum() / len(flux_fc) * 100:.1f}%)") .. rst-class:: sphx-glr-script-out .. code-block:: none > Loaded .parquet file /home/docs/checkouts/diive/checkouts/latest/diive/configs/exampledata/exampledat a_PARQUET_CH-CHA_FP2025.3_subset_30MIN.parquet (0.026 seconds). ================================================================================ Data Summary ================================================================================ Data: FC Period: 2015-01-01 to 2015-12-31 Total records: 5665 Valid records: 5665 Missing: 0 (0.0%) .. GENERATED FROM PYTHON SOURCE LINES 40-46 Apply Hampel filter with day/night separation ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Run high-quality flux analysis using Hampel filter (Median Absolute Deviation). Automatically separates daytime and nighttime using solar elevation angle, with separate strictness thresholds for each period. .. GENERATED FROM PYTHON SOURCE LINES 46-59 .. code-block:: Python results = analyze_highest_quality_flux( flux=flux_fc, lat=47.210222, # CH-CHA tower (Swiss FluxNet site page, WGS84) lon=8.410444, utc_offset=1, # CET window_length=48 * 13, # 13 days at 30-min frequency n_sigma_daytime=5.5, n_sigma_nighttime=5.5, use_differencing=True, # Papale method: isolate spikes from trends showplot=True ) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/flux/lowres/images/sphx_glr_flux_hqflux_001.png :alt: FC filtered by FLAG_FC_OUTLIER_HAMPEL_TEST, n_iterations = 1, n_outliers = 19 :srcset: /auto_examples/flux/lowres/images/sphx_glr_flux_hqflux_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/flux/lowres/images/sphx_glr_flux_hqflux_002.png :alt: Hampel filter daytime/nighttime: FC, n_iterations = 1, n_outliers = 19 :srcset: /auto_examples/flux/lowres/images/sphx_glr_flux_hqflux_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/flux/lowres/images/sphx_glr_flux_hqflux_003.png :alt: Highest-quality fluxes FC after Hampel outlier removal :srcset: /auto_examples/flux/lowres/images/sphx_glr_flux_hqflux_003.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none > >>> Removing outliers from highest-quality fluxes (FC) > >>> Outlier removal method: Hampel filter (MAD-based) > >>> Parameters: window_length=624, n_sigma_daytime=5.5, n_sigma_nighttime=5.5, use_differencing=True > Detect frequency: 30min (timedelta, 67% match) > Regularize gaps: OK [Dt/Nt] ITER #01 | Outliers: 19 ( 0.34%) | Day: 15 | Night: 4 > >>> Outlier Detection Summary: > >>> Total records: 5665 > >>> Measured records: 5665 > >>> Valid records: 5646 (99.7% of measured) > >>> Outliers detected: 19 (0.3% of measured) > >>> Largest non-outlier flux >= 0 DAYTIME: 85.836700 > >>> Smallest non-outlier flux >= 0 DAYTIME: 0.005859 > >>> Largest non-outlier flux < 0 DAYTIME: -0.000095 > >>> Smallest non-outlier flux < 0 DAYTIME: -79.367200 > >>> Largest outlier flux >= 0 DAYTIME: 52.775600 > >>> Smallest outlier flux >= 0 DAYTIME: 12.617900 > >>> Largest outlier flux < 0 DAYTIME: -5.439650 > >>> Smallest outlier flux < 0 DAYTIME: -85.178400 > >>> Largest non-outlier flux >= 0 NIGHTTIME: 61.246300 > >>> Smallest non-outlier flux >= 0 NIGHTTIME: 0.086871 > >>> Largest non-outlier flux < 0 NIGHTTIME: -0.118833 > >>> Smallest non-outlier flux < 0 NIGHTTIME: -49.505600 > >>> Largest outlier flux >= 0 NIGHTTIME: 17.317200 > >>> Smallest outlier flux >= 0 NIGHTTIME: 5.767250 > >>> Largest outlier flux < 0 NIGHTTIME: -3.646830 > >>> Smallest outlier flux < 0 NIGHTTIME: -3.646830 .. GENERATED FROM PYTHON SOURCE LINES 60-64 Examine results ^^^^^^^^^^^^^^^ Inspect the filtered data and understand the Hampel filter method. .. GENERATED FROM PYTHON SOURCE LINES 64-81 .. code-block:: Python print(f"\n" + "=" * 80) print("Results Summary") print("=" * 80) print(f"Filtered data shape: {results.shape}") print(f"Columns: {list(results.columns)}") print(f"\nInterpretation:") print(f" - Rolling median ± 3 SD shows expected CO2 flux range") print(f" - Points outside this range may indicate sensor issues or extreme events") print(f"\nHampel Filter Method:") print(f" - Median Absolute Deviation (MAD) for robust statistics") print(f" - Double-differencing (Papale et al. 2006) to remove biological trends") print(f" - Automatic day/night thresholds based on solar elevation angle") print("\n[OK] High-quality flux analysis complete.") .. rst-class:: sphx-glr-script-out .. code-block:: none ================================================================================ Results Summary ================================================================================ Filtered data shape: (5665, 8) Columns: ['FLUX_DAYTIME', 'ROLLING_MEDIAN_DAYTIME', 'SD_DAYTIME', 'WINSIZE_DAYTIME', 'FLUX_NIGHTTIME', 'ROLLING_MEDIAN_NIGHTTIME', 'SD_NIGHTTIME', 'WINSIZE_NIGHTTIME'] Interpretation: - Rolling median ± 3 SD shows expected CO2 flux range - Points outside this range may indicate sensor issues or extreme events Hampel Filter Method: - Median Absolute Deviation (MAD) for robust statistics - Double-differencing (Papale et al. 2006) to remove biological trends - Automatic day/night thresholds based on solar elevation angle [OK] High-quality flux analysis complete. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.155 seconds) .. _sphx_glr_download_auto_examples_flux_lowres_flux_hqflux.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: flux_hqflux.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: flux_hqflux.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: flux_hqflux.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_