Data Corrections Examples

Examples demonstrating data corrections including sensor offset detection, bias removal, and value clipping.

7 examples covering physical corrections, calibration drift detection, and data validation.

Contents by Correction Type

Sensor Offset & Bias Corrections

  • correction_relativehumidity_offset.py — Fix RH saturation issues (measurements >100%)

  • correction_radiation_offset.py — Correct radiation nighttime offset (non-zero readings at night)

  • correction_measurement_offset_replicate.py — Detect constant bias between two instruments

  • correction_winddir_offset.py — Correct wind direction calibration drift

Value Replacement & Clipping

  • correction_set_exact_values_to_missing.py — Replace exact values with NaN (error codes, sentinel values)

  • correction_setto_value.py — Replace values in specific periods (e.g., known malfunction times)

  • correction_setto_threshold.py — Clip values to physically realistic min/max bounds

Use Cases

Fix humidity oversaturation:

import diive as dv

# RH sensor drifts >100% due to aging/contamination.
# The daily mean excess is removed as offset, values are capped at 100%.
corrected = dv.corrections.remove_relativehumidity_offset(series=df['RH'], showplot=True)

Remove nighttime radiation offset:

import diive as dv

# Radiation sensor reads non-zero at night (thermal offset).
# Nighttime is derived from solar geometry, so the site location is needed.
corrected = dv.corrections.remove_nighttime_zero_offset(
    series=df['SW_IN'],
    lat=47.478333,
    lon=8.364389,
    utc_offset=1,
    showplot=True
)

Detect instrument offset:

import diive as dv

# Two sensors show constant bias, offset found by brute-force search
offset_corrector = dv.corrections.MeasurementOffsetFromReplicate(
    measurement=df['TA_primary'],
    replicate=df['TA_reference'],
    offset_start=-10,
    offset_end=10,
    offset_stepsize=0.1
)
offset = offset_corrector.get_offset()  # Detected offset, added to correct
corrected = offset_corrector.get_corrected_measurement()

Mask known problems:

import numpy as np
import diive as dv

# Instrument malfunction from 2024-01-15 through 2024-01-17.
# Each entry in *dates* is either a single timestamp or a [start, end] range,
# both ends included. A bare date covers the whole day, so this range runs
# to the last record of 2024-01-17.
corrected = dv.corrections.setto_value(
    series=df['CO2'],
    dates=[['2024-01-15', '2024-01-17']],
    value=np.nan,
    verbose=1
)

Running Examples

# Sensor bias corrections
uv run python examples/preprocessing/corrections/correction_relativehumidity_offset.py
uv run python examples/preprocessing/corrections/correction_radiation_offset.py
uv run python examples/preprocessing/corrections/correction_measurement_offset_replicate.py
uv run python examples/preprocessing/corrections/correction_winddir_offset.py

# Value replacement & clipping
uv run python examples/preprocessing/corrections/correction_set_exact_values_to_missing.py
uv run python examples/preprocessing/corrections/correction_setto_value.py
uv run python examples/preprocessing/corrections/correction_setto_threshold.py

# Run all correction examples
uv run python examples/run_all_examples.py

Set Exact Values to Missing (NaN)

Set Exact Values to Missing (NaN)

Correct Relative Humidity Saturation

Correct Relative Humidity Saturation

Set Time Range Values to Constant

Set Time Range Values to Constant

Clip Values to Minimum/Maximum Limits

Clip Values to Minimum/Maximum Limits

Correct Radiation Zero Offset (Nighttime)

Correct Radiation Zero Offset (Nighttime)

Correct Wind Direction Offset

Correct Wind Direction Offset

Correct Measurement Offset from Replicate

Correct Measurement Offset from Replicate