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
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 8.199927 82.792013
2023-01-01 01:00:00 5.463299 209.516506
2023-01-01 02:00:00 14.369749 323.792501
2023-01-01 03:00:00 8.225731 103.482516
2023-01-01 04:00:00 7.499095 290.460999
Save to .csv
We save the DataFrame to a .csv file in the temporary directory.
with tempfile.TemporaryDirectory() as tmpdir:
df.to_csv(f"{tmpdir}/sample_tswc.csv", index=True)
# %%
# Parse .csv to TSWC
# ------------------------
# Now, we can parse the CSV file to create a Time Series Wind Climate (TSWC) object using
# the :py:func:`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.
tswc = wk.read_tswc(
f"{tmpdir}/sample_tswc.csv",
west_east=0.0,
south_north=0.0,
crs=4326,
time_col=0,
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 8.2 ... 13.72
wind_direction (time, height, stacked_point) float64 70kB 82.79 ... 254.4
Attributes:
history: 2025-07-21T09:49:27+00:00:\twindkit==1.0.2\t return ...
Conventions: CF-1.8
Package name: windkit
Package version: 1.0.2
Creation date: 2025-07-21T09:49:27+00:00
Object type: Time Series Wind Climate
author: Default User
author_email: default_email@example.com
institution: Default Institution
Alternatively, we can also use the 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)
<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 8.2 ... 13.72
wind_direction (time, height, stacked_point) float64 70kB 82.79 ... 254.4
Attributes:
history: 2025-07-21T09:49:27+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:27+00:00
Object type: Time Series Wind Climate
author: Default User
author_email: default_email@example.com
institution: Default Institution
Total running time of the script: (0 minutes 0.105 seconds)