{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Estimate Net AEP and P90 AEP\n\nExample of estimating the Net AEP and P90 AEP using a loss table and an uncertainty table.\nFor a more detailed overview see `pywasp` Tutorial 6.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, we calculate the Gross AEP and the Potential AEP of the wind farm in the\nSerra Santa Luzia tutorial data.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pywasp as pw\nimport windkit as wk\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.stats import norm\n\nssl = wk.load_tutorial_data(\"serra_santa_luzia\")\n# The wind directions are relative to the grid north of the UTM 29N maps\nbwc = ssl.bwc.assign_attrs(wind_dir_crs=\"EPSG:32629\")\ntopo_map = pw.wasp.TopographyMap(ssl.elev, ssl.rgh)\nwwc = pw.wasp.predict_wwc(bwc, topo_map, ssl.turbines)\n\ngross_aep = pw.gross_aep(wwc, ssl.wtg)\npotential_aep = pw.potential_aep(wwc, ssl.wtg)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Losses towards P50 - Importing a loss table\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "loss_table = wk.get_loss_table(\"dtu_default\")\n\n# Modify loss values as needed\nloss_table.loc[0, \"loss_percentage\"] = 0.5\nloss_table.loc[2, \"loss_percentage\"] = 0.1\n\nprint(loss_table.head())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Losses towards P50 - Calculating the Net AEP\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "net_aep = pw.net_aep(loss_table, potential_aep)\nprint(\"Net AEP:\", net_aep[\"net_aep\"].values.sum(), \"GWh\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Uncertainties towards P90 - Importing a uncertainty table\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "uncertainty_table = wk.get_uncertainty_table()\nprint(uncertainty_table.head())\nprint(uncertainty_table.tail())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. Note:: Terms that are part of the \"energy\" kind are those related to the uncertainties of the technical losses.\n   Terms that are part of the \"wind\" kind are those related to the uncertainties of the predicted wind climate.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Uncertainties towards P90 - Calculating the Px yield\n<div class=\"alert alert-info\"><h4>Note</h4><p>For a P90 value, x = 90.</p></div>\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "p90 = pw.px_aep(uncertainty_table, net_aep, sensitivity_factor=1.5, percentile=90)\nprint(\"The P90 is:\", p90[\"P_90_aep\"].values.sum(), \"Gwh\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "tot_sigma, _, _ = wk.total_uncertainty(uncertainty_table, 1.5)\np50 = np.round(net_aep[\"net_aep\"].values.sum(), 2)\nsigma_tot_dimensional = np.round(tot_sigma / 100 * p50, 2)\n\nplt.figure(figsize=(14, 6))\n\nx = np.linspace(15, 60, 100)\n\nplt.axvline(\n    gross_aep[\"gross_aep\"].values.sum(),\n    color=\"k\",\n    linestyle=\"-.\",\n    linewidth=2,\n    label=\"Gross AEP mean\",\n)\nplt.axvline(\n    potential_aep[\"potential_aep\"].values.sum(),\n    color=\"b\",\n    linestyle=\"--\",\n    alpha=0.7,\n    linewidth=2,\n    label=\"Potential AEP mean\",\n)\nplt.axvline(p50, color=\"r\", linestyle=\"solid\", linewidth=2, label=\"P50\")\nplt.axvline(\n    p50 + sigma_tot_dimensional,\n    color=\"r\",\n    linestyle=\"dashed\",\n    linewidth=1,\n    label=\"P50 -+ \u03c3\",\n)\nplt.axvline(p50 - sigma_tot_dimensional, color=\"r\", linestyle=\"dashed\", linewidth=1)\nplt.axvline(\n    p90[\"P_90_aep\"].values.sum(), color=\"g\", linestyle=\"-.\", linewidth=1.5, label=\"P90\"\n)\npdf = norm.pdf(x, p50, sigma_tot_dimensional)\nplt.plot(x, pdf, color=\"r\", linewidth=2, alpha=0.5)\n\nplt.grid(True)\nplt.legend()\nplt.title(\"Gaussian distributio of the AEP\")\nplt.xlabel(\"AEP [GWh]\")"
      ]
    }
  ],
  "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.13.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}