{
  "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\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 the temporary directory.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "with tempfile.TemporaryDirectory() as tmpdir:\n    df.to_csv(f\"{tmpdir}/sample_tswc.csv\", index=True)\n\n    # %%\n    # Parse .csv to TSWC\n    # ------------------------\n    # Now, we can parse the CSV file to create a Time Series Wind Climate (TSWC) object using\n    # the :py:func:`read_tswc` function. We have to specify the spatial coordinate system (CRS),\n    # coordinates for the west-east and south-north axes,\n    # the time column, and the height to columns mapping. In this case, we assume that the\n    # wind speed and direction data are at a height of 50 meters, and we map them accordingly.\n\n    tswc = wk.read_tswc(\n        f\"{tmpdir}/sample_tswc.csv\",\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\n    print(tswc)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Alternatively, we can also use the :py:func:`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)"
      ]
    }
  ],
  "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
}