.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/tswc_from_csv.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_tswc_from_csv.py: Parse TSWCs from CSV ====================== This example shows how to parse a Time Series Wind Climate (TSWC) from a .csv file. .. GENERATED FROM PYTHON SOURCE LINES 10-14 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. .. GENERATED FROM PYTHON SOURCE LINES 14-33 .. code-block:: Python 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()) .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. GENERATED FROM PYTHON SOURCE LINES 34-37 Save to .csv ------------------------ We save the DataFrame to a .csv file in the temporary directory. .. GENERATED FROM PYTHON SOURCE LINES 37-61 .. code-block:: Python 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) .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. GENERATED FROM PYTHON SOURCE LINES 62-65 Alternatively, we can also use the :py:func:`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. .. GENERATED FROM PYTHON SOURCE LINES 65-74 .. code-block:: Python 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) .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.105 seconds) .. _sphx_glr_download_auto_examples_tswc_from_csv.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: tswc_from_csv.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: tswc_from_csv.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: tswc_from_csv.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_