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 7.315907 240.071192
2023-01-01 01:00:00 12.355987 350.416001
2023-01-01 02:00:00 14.835689 118.958768
2023-01-01 03:00:00 10.257354 212.482614
2023-01-01 04:00:00 9.707300 311.425267
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 7.316 ... 11.21
wind_direction (time, height, stacked_point) float64 70kB 240.1 ... 240.9
Attributes:
history: 2026-01-29T09:29:35+00:00:\twindkit==2.0.1\twk.tswc_fro...
Conventions: CF-1.8
Package name: windkit
Package version: 2.0.1
Creation date: 2026-01-29T09:29:35+00:00
Object type: Time Series Wind Climate
author: Default User
author_email: default_email@example.com
institution: Default Institution
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()
![height = 50, crs = 0, sector = 270.0 [degree], ...](../_images/sphx_glr_tswc_to_bwc_001.png)
<xarray.Dataset> Size: 4kB
Dimensions: (height: 1, sector: 12, stacked_point: 1, wsbin: 30)
Coordinates:
* height (height) int64 8B 50
* 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
south_north (stacked_point) float64 8B 0.0
west_east (stacked_point) float64 8B 0.0
* 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
crs int8 1B 0
Dimensions without coordinates: stacked_point
Data variables:
wdfreq (sector, height, stacked_point) float64 96B 0.08014 ... 0.0...
wsfreq (wsbin, sector, height, stacked_point) float64 3kB 0.0 ... 0.0
Attributes: (12/20)
description:
long_name: Horizontal Wind Speed
standard_name: wind_speed
units: m s-1
Conventions: CF-1.8
Package name: windkit
... ...
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 0x792ba256d940>]
Total running time of the script: (0 minutes 0.253 seconds)