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   9.743766  113.243837
2023-01-01 01:00:00  13.118488  156.759630
2023-01-01 02:00:00   8.976591  246.589094
2023-01-01 03:00:00  10.284349  120.045583
2023-01-01 04:00:00  14.598357   74.204209

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:
    west_east       (stacked_point) float64 8B 0.0
    south_north     (stacked_point) float64 8B 0.0
    crs             int8 1B 0
  * time            (time) datetime64[ns] 70kB 2023-01-01 ... 2023-12-31T23:0...
  * height          (height) int64 8B 50
Dimensions without coordinates: stacked_point
Data variables:
    wind_speed      (time, height, stacked_point) float64 70kB 9.744 ... 6.334
    wind_direction  (time, height, stacked_point) float64 70kB 113.2 ... 157.9
Attributes:
    history:          2025-07-21T09:49:26+00:00:\twindkit==1.0.2\twk.tswc_fro...
    Conventions:      CF-1.8
    Package name:     windkit
    Package version:  1.0.2
    Creation date:    2025-07-21T09:49:26+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()
crs = 0, sector_ceil = 285.0 [degree], sector_f...
<xarray.Dataset> Size: 4kB
Dimensions:       (wsbin: 30, sector: 12, height: 1, stacked_point: 1)
Coordinates:
    crs           int8 1B 0
    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_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
    west_east     (stacked_point) float64 8B 0.0
    south_north   (stacked_point) float64 8B 0.0
  * height        (height) int64 8B 50
  * wsbin         (wsbin) float64 240B 0.5 1.5 2.5 3.5 ... 26.5 27.5 28.5 29.5
  * sector        (sector) float64 96B 0.0 30.0 60.0 90.0 ... 270.0 300.0 330.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.07934 ... 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 0x7d7ad8693ed0>]

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

Gallery generated by Sphinx-Gallery