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.982707   56.317794
2023-01-01 01:00:00  14.173033  103.206820
2023-01-01 02:00:00  12.462985  282.860784
2023-01-01 03:00:00   8.518299   83.396654
2023-01-01 04:00:00  14.359151  297.667737

Create TSWC#

tswc = wk.tswc_from_dataframe(
    df,
    west_east=0.0,
    south_north=0.0,
    crs=4326,
    height_to_columns={50: ["wspd_50", "wdir_50"]},
)
print(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.983 ... 12.38
    wind_direction  (time, height, stacked_point) float64 70kB 56.32 ... 331.2
Attributes:
    history:          2026-04-23T06:09:38+00:00:\twindkit==2.1.0\twk.tswc_fro...
    Conventions:      CF-1.8
    Package name:     windkit
    Package version:  2.1.0
    Creation date:    2026-04-23T06:09:38+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, west_east = 0.0 [degrees_east], so...
<xarray.Dataset> Size: 4kB
Dimensions:       (height: 1, stacked_point: 1, sector: 12, wsbin: 30)
Coordinates:
  * height        (height) int64 8B 50
    west_east     (stacked_point) float64 8B 0.0
    south_north   (stacked_point) float64 8B 0.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
  * 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.08276 ... 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 0x7254eadc67b0>]

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

Gallery generated by Sphinx-Gallery