{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Parse TSWCs from CSV\n\nThis example shows how to parse a Time Series Wind Climate (TSWC) from a .csv file.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Prepare Sample Data\nFirst, we need to prepare the sample data. This is done by creating a\nDataFrame with the wind speed and direction data.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import tempfile\nfrom pathlib import Path\n\nimport numpy as np\nimport pandas as pd\nimport windkit as wk\n\n\nsize = 8760  # 1 year of hourly data\n\ndf = pd.DataFrame(\n    {\n        \"wspd_50\": np.random.uniform(5, 15, size=size),  # Wind speed in m/s\n        \"wdir_50\": np.random.uniform(0, 360, size=size),  # Wind direction in degrees\n    },\n    index=pd.date_range(\"2023-01-01\", periods=size, freq=\"h\"),\n)\ndf.index.name = \"time\"  # Set the index name to 'time'\nprint(df.head())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Save to .csv\nWe save the DataFrame to a .csv file in a temporary directory.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "tmpdir = tempfile.TemporaryDirectory()\ncsv_path = Path(tmpdir.name) / \"sample_tswc.csv\"\ndf.to_csv(csv_path, index=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Parse .csv to TSWC\nNow, we can parse the CSV file to create a Time Series Wind Climate (TSWC) object using\nthe :py:func:`windkit.read_tswc` function. We have to specify the spatial coordinate system (CRS),\ncoordinates for the west-east and south-north axes,\nthe time column, and the height to columns mapping. In this case, we assume that the\nwind speed and direction data are at a height of 50 meters, and we map them accordingly.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "tswc = wk.read_tswc(\n    csv_path,\n    west_east=0.0,\n    south_north=0.0,\n    crs=4326,\n    time_col=0,\n    height_to_columns={50: [\"wspd_50\", \"wdir_50\"]},\n)\n\nprint(tswc)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Alternatively, we can also use the :py:func:`windkit.tswc_from_dataframe` function to parse a\nDataFrame directly into a TSWC object. This is useful if you already have the data in a\nDataFrame format and do not need to read it from a file.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "tswc_from_df = wk.tswc_from_dataframe(\n    df,\n    west_east=0.0,\n    south_north=0.0,\n    crs=4326,\n    height_to_columns={50: [\"wspd_50\", \"wdir_50\"]},\n)\nprint(tswc_from_df)\n\ntmpdir.cleanup()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}