Note
Go to the end to download the full example code.
Detect Timestamp Shifts¶
Detect clock/timestamp errors in meteorological time series by comparing measured shortwave radiation against theoretical potential radiation.
Three detection methods are demonstrated:
FFT phase shift — fast spectral comparison of the 24-hour diurnal cycle
Cross-correlation — high-precision 1-minute lag search via scipy
Noon shift — quick peak-time delta heuristic
All three methods share the same sign convention: positive shift = measured peaks earlier than potential (leading clock), negative shift = measured peaks later (lagging clock).
Load data and prepare the DataFrame¶
The example dataset contains 30-minute ecosystem flux measurements.
Rg_f is gap-filled global radiation in W/m². We use a single year
to keep runtime short.
import matplotlib.pyplot as plt
import diive as dv
from diive.preprocessing.qaqc.detect_timestamp_shifts import DetectTimestampShifts
df = dv.load_exampledata_parquet()
df = df.loc[df.index.year == 2022].copy()
# Keep only the measured radiation column; potential radiation is computed
# automatically from site coordinates inside the constructor.
df = df[['Rg_f']].copy()
print(f"Period : {df.index.min().date()} to {df.index.max().date()}")
print(f"Records : {len(df)}")
print(f"Rg_f : {df['Rg_f'].min():.1f} to {df['Rg_f'].max():.1f} W/m2")
> 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).
Period : 2022-01-01 to 2022-12-31
Records : 17520
Rg_f : 0.0 to 1084.1 W/m2
Construct DetectTimestampShifts¶
col_pot names the potential radiation column. Because it is absent
from df, the constructor computes it automatically using
potrad with the supplied site coordinates.
dts = DetectTimestampShifts(
df=df,
col_meas='Rg_f', # measured shortwave radiation column
col_pot='SW_IN_POT', # potential radiation column (auto-computed when absent)
lat=46.81533, # site latitude, decimal degrees
lon=9.85591, # site longitude, decimal degrees
utc_offset=1, # UTC offset in hours (CET = UTC+1)
)
Alternative: supply a pre-computed potential radiation column¶
If the potential radiation is already in the DataFrame, pass it directly.
No lat/lon required. potrad is what the constructor calls above, so this
gives the same result and only avoids recomputing the column. A column from
elsewhere works too, as long as its solar noon follows the equation of time:
a reference that pins solar noon to a fixed clock time carries its own
~15-minute seasonal swing into the detected shifts.
from diive.variables.radiation import potrad
df_with_pot = df.copy()
df_with_pot['SW_IN_POT'] = potrad(
timestamp_index=df.index,
lat=46.81533,
lon=9.85591,
utc_offset=1,
)
dts_precomputed = DetectTimestampShifts(
df=df_with_pot,
col_meas='Rg_f',
col_pot='SW_IN_POT', # already present; no lat/lon needed
)
Method 1: FFT phase shift¶
Projects each day’s radiation onto the k=1 Fourier basis and compares phase angles. Fast and reliable on clear days.
fft_results = dts.fft_phase_shift(
min_clearness=0.6, # skip days where measured/potential daily sum < 0.6
)
print(fft_results.dropna().describe().round(1))
shift_minutes amplitude_meas
count 164.0 164.0
mean -13.6 8616.1
std 8.3 3002.0
min -42.6 2933.7
25% -18.7 6050.5
50% -14.3 9313.1
75% -9.8 11269.1
max 18.5 12920.4
Plot FFT results¶
Four panels: time series with rolling median, shift histogram, polar plot, monthly boxplot.

Method 2: Cross-correlation¶
Upsamples each day to 1-minute resolution, then uses scipy.signal.correlate to find the lag that maximises the Pearson correlation with potential radiation. More precise than FFT; slower on very long datasets.
crosscorr_results = dts.crosscorr(
max_shift_min=120, # search window: +/- 2 hours
upsample_freq='1min', # upsample to 1-minute resolution
min_clearness_index=0.5, # skip heavy-overcast days
)
print(crosscorr_results.dropna().describe().round(2))
shift_minutes max_corr
count 224.00 224.00
mean -11.27 0.96
std 14.78 0.03
min -56.00 0.81
25% -19.00 0.95
50% -13.50 0.97
75% -5.00 0.98
max 43.00 0.99
Plot cross-correlation results¶
Two panels: scatter coloured by correlation strength, shift histogram.

Method 3: Noon shift¶
Compares the timestamp of the daily radiation peak between measured and potential. Very fast but sensitive to passing clouds near solar noon.
noon_results = dts.noon_shift(
clearness_threshold=0.7, # only include days where measured/potential sum > 0.7
)
print(noon_results.describe().round(1))
count 87.0
mean -2.4
std 23.9
min -60.0
25% 0.0
50% 0.0
75% 0.0
max 90.0
Name: time_shift_minutes, dtype: float64
Plot noon shift results¶
Three panels: time series with rolling median, shift histogram, monthly boxplot.

Supporting plot: monthly diel cycles¶
Overlays multi-year mean diel cycles per calendar month. Systematic offsets between the measured curve and the potential radiation reference (dashed black) indicate a persistent timestamp shift.

Supporting plot: radiation fingerprint¶
Heatmap with calendar days as rows and time-of-day as columns. The parabolic upper boundary represents the potential radiation envelope; any horizontal offset between the measured peak and the envelope suggests a clock error.

Total running time of the script: (0 minutes 3.771 seconds)