Note
Go to the end to download the full example code.
Correct Wind Direction Offset¶
Detect and correct wind direction measurement offset by comparing yearly wind direction histograms to a reference histogram. Finds the offset that maximizes correlation with reference distribution.
Load multi-year wind direction data¶
Wind direction measurements can drift over time due to sensor misalignment or calibration issues. We load multi-year wind direction data where some years are known to be correct (reference years).
import diive as dv
from diive.configs.exampledata import load_exampledata_winddir
df = load_exampledata_winddir()
winddir = df['wind_dir'].copy()
# Filter to years with data
winddir = winddir.loc[winddir.index.year <= 2022].dropna()
print("Input wind direction data:")
print(f" Range: {winddir.min():.1f} to {winddir.max():.1f} degrees")
print(f" Years available: {sorted(winddir.index.year.unique())}")
> Loaded .parquet file
/home/docs/checkouts/diive/checkouts/latest/diive/configs/exampledata/exampledat
a_PARQUET_winddirection_degrees_CH-FRU_2005-2022.parquet (0.007 seconds).
Input wind direction data:
Range: 0.0 to 360.0 degrees
Years available: [2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022]
Detect offset using reference years¶
Years 2021 and 2022 are known to be correctly calibrated. We build a reference wind direction histogram from these years, then search for the yearly offset in other years that maximizes correlation.
offset_corrector = dv.corrections.WindDirOffset(
winddir=winddir,
hist_ref_years=[2021, 2022],
offset_start=-50,
offset_end=50,
hist_n_bins=360
)
print("\nOffset detection:")
print(f" Reference years: [2021, 2022]")
print(f" Search range: -50 to +50 degrees")
print(f" Histogram bins: 360 (1 degree resolution)")
Offset detection:
Reference years: [2021, 2022]
Search range: -50 to +50 degrees
Histogram bins: 360 (1 degree resolution)
Extract yearly offsets and corrected values¶
Get the detected offset for each year and apply correction.
winddir_corrected = offset_corrector.get_corrected_wind_directions()
yearly_offsets = offset_corrector.get_yearly_offsets()
print("\nYearly wind direction offsets (degrees):")
print(yearly_offsets.to_string(index=False))
Yearly wind direction offsets (degrees):
YEAR OFFSET
2005.0 -24.0
2006.0 -23.0
2007.0 -26.0
2008.0 -27.0
2009.0 -23.0
2010.0 -25.0
2011.0 -24.0
2012.0 -27.0
2013.0 -26.0
2014.0 -23.0
2015.0 -26.0
2016.0 -24.0
2017.0 -22.0
2018.0 -13.0
2019.0 -13.0
2020.0 -2.0
2021.0 0.0
2022.0 0.0
Verify correction¶
Check the corrected wind direction range and compare to original.
print(f"\nCorrected wind direction data:")
print(f" Range: {winddir_corrected.min():.1f} to {winddir_corrected.max():.1f} degrees")
print(f" Original range: {winddir.min():.1f} to {winddir.max():.1f} degrees")
# Show data availability by year
print(f"\nData availability by year:")
for year in sorted(winddir.index.year.unique()):
n_records = len(winddir[winddir.index.year == year])
offset = yearly_offsets[yearly_offsets['YEAR'] == year]['OFFSET'].values[0]
print(f" {year}: {n_records:5d} records, offset: {offset:+6.1f}°")
Corrected wind direction data:
Range: 0.0 to 359.9 degrees
Original range: 0.0 to 360.0 degrees
Data availability by year:
2005: 7103 records, offset: -24.0°
2006: 17042 records, offset: -23.0°
2007: 15045 records, offset: -26.0°
2008: 17470 records, offset: -27.0°
2009: 16755 records, offset: -23.0°
2010: 15887 records, offset: -25.0°
2011: 16628 records, offset: -24.0°
2012: 15799 records, offset: -27.0°
2013: 16873 records, offset: -26.0°
2014: 16839 records, offset: -23.0°
2015: 17055 records, offset: -26.0°
2016: 17265 records, offset: -24.0°
2017: 16206 records, offset: -22.0°
2018: 17336 records, offset: -13.0°
2019: 17440 records, offset: -13.0°
2020: 17067 records, offset: -2.0°
2021: 16994 records, offset: +0.0°
2022: 17327 records, offset: +0.0°
Total running time of the script: (0 minutes 2.466 seconds)