Note
Go to the end to download the full example code.
TSWC to BWC#
This example demonstrates how to convert a time series wind climate (TSWC) object
to a binned wind climate (BWC) object using the bwc_from_tswc function from the
windkit library.
Prepare Sample Data#
First, we need to prepare the sample data. This is done by creating a DataFrame with the wind speed and direction data.
import numpy as np
import pandas as pd
import windkit as wk
size = 8760 # 1 year of hourly data
df = pd.DataFrame(
{
"wspd_50": np.random.uniform(5, 15, size=size), # Wind speed in m/s
"wdir_50": np.random.uniform(0, 360, size=size), # Wind direction in degrees
},
index=pd.date_range("2023-01-01", periods=size, freq="h"),
)
df.index.name = "time" # Set the index name to 'time'
print(df.head())
wspd_50 wdir_50
time
2023-01-01 00:00:00 6.017143 214.886927
2023-01-01 01:00:00 13.566500 261.612819
2023-01-01 02:00:00 8.023053 90.301051
2023-01-01 03:00:00 7.832558 272.449948
2023-01-01 04:00:00 10.263807 239.604732
Create TSWC#
<xarray.Dataset> Size: 210kB
Dimensions: (time: 8760, height: 1, stacked_point: 1)
Coordinates:
* time (time) datetime64[us] 70kB 2023-01-01 ... 2023-12-31T23:0...
* height (height) int64 8B 50
west_east (stacked_point) float64 8B 0.0
south_north (stacked_point) float64 8B 0.0
crs int8 1B 0
Dimensions without coordinates: stacked_point
Data variables:
wind_speed (time, height, stacked_point) float64 70kB 6.017 ... 12.95
wind_direction (time, height, stacked_point) float64 70kB 214.9 ... 124.6
Attributes:
history: 2026-09-29T09:45:31+00:00:\twindkit==2.2.0\twk.tswc_fro...
Conventions: CF-1.8
Package name: windkit
Package version: 2.2.0
Creation date: 2026-09-29T09:45:31+00:00
Object type: Time Series Wind Climate
Convert TSWC to BWC#
Now, we can convert the time series wind climate (TSWC) object to a binned wind climate (BWC) object
bwc = wk.bwc_from_tswc(tswc)
print(bwc)
bwc.wsfreq.sel(sector=270).plot()
![sector = 270.0 [degree], sector_ceil = 285.0 [d...](../_images/sphx_glr_tswc_to_bwc_001.png)
<xarray.Dataset> Size: 4kB
Dimensions: (wsbin: 30, sector: 12, height: 1, stacked_point: 1)
Coordinates:
* wsbin (wsbin) float64 240B 0.5 1.5 2.5 3.5 ... 26.5 27.5 28.5 29.5
wsceil (wsbin) float64 240B 1.0 2.0 3.0 4.0 ... 27.0 28.0 29.0 30.0
wsfloor (wsbin) float64 240B 0.0 1.0 2.0 3.0 ... 26.0 27.0 28.0 29.0
* sector (sector) float64 96B 0.0 30.0 60.0 90.0 ... 270.0 300.0 330.0
sector_ceil (sector) float64 96B 15.0 45.0 75.0 ... 285.0 315.0 345.0
sector_floor (sector) float64 96B 345.0 15.0 45.0 ... 255.0 285.0 315.0
* height (height) int64 8B 50
west_east (stacked_point) float64 8B 0.0
south_north (stacked_point) float64 8B 0.0
crs int8 1B 0
Dimensions without coordinates: stacked_point
Data variables:
wsfreq (wsbin, sector, height, stacked_point) float64 3kB 0.0 ... 0.0
wdfreq (sector, height, stacked_point) float64 96B 0.08116 ... 0.0...
Attributes: (12/14)
history: 2026-09-29T09:45:31+00:00:\twindkit==2.2.0\twk.tswc...
Conventions: CF-1.8
Package name: windkit
Package version: 2.2.0
Creation date: 2026-09-29T09:45:31+00:00
Object type: Binned Wind Climate
... ...
end_time: 2023-12-31 23:00:00
interval: 0 days 01:00:00
count_expected: 8760
count: 8760
count_missing: 0
recovery_percentage: 100.0
[<matplotlib.lines.Line2D object at 0x70f636a60980>]
Total running time of the script: (0 minutes 0.232 seconds)