Note
Go to the end to download the full example code.
Parse TSWCs from CSV#
This example shows how to parse a Time Series Wind Climate (TSWC) from a .csv file.
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 tempfile
from pathlib import Path
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 12.548563 65.613975
2023-01-01 01:00:00 10.577625 3.782416
2023-01-01 02:00:00 9.628012 96.710197
2023-01-01 03:00:00 10.617875 247.375524
2023-01-01 04:00:00 11.593405 47.362309
Save to .csv#
We save the DataFrame to a .csv file in a temporary directory.
tmpdir = tempfile.TemporaryDirectory()
csv_path = Path(tmpdir.name) / "sample_tswc.csv"
df.to_csv(csv_path, index=True)
Parse .csv to TSWC#
Now, we can parse the CSV file to create a Time Series Wind Climate (TSWC) object using
the windkit.read_tswc() function. We have to specify the spatial coordinate system (CRS),
coordinates for the west-east and south-north axes,
the time column, and the height to columns mapping. In this case, we assume that the
wind speed and direction data are at a height of 50 meters, and we map them accordingly.
<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 12.55 ... 7.781
wind_direction (time, height, stacked_point) float64 70kB 65.61 ... 129.1
Attributes:
history: 2026-09-29T09:45:32+00:00:\twindkit==2.2.0\t return ...
Conventions: CF-1.8
Package name: windkit
Package version: 2.2.0
Creation date: 2026-09-29T09:45:32+00:00
Object type: Time Series Wind Climate
Alternatively, we can also use the windkit.tswc_from_dataframe() function to parse a
DataFrame directly into a TSWC object. This is useful if you already have the data in a
DataFrame format and do not need to read it from a file.
tswc_from_df = 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_from_df)
tmpdir.cleanup()
<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 12.55 ... 7.781
wind_direction (time, height, stacked_point) float64 70kB 65.61 ... 129.1
Attributes:
history: 2026-09-29T09:45:32+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:32+00:00
Object type: Time Series Wind Climate
Total running time of the script: (0 minutes 0.079 seconds)