{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Component Sum Equations for Irradiance Data\n\nEstimate GHI, DHI, and DNI using the component sum equations, with\nnighttime corrections.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Estimating GHI, DHI, and DNI using the component sum equations is useful\nif the associated field is missing, or as a comparison to an existing\nphysical data stream.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pvanalytics\nfrom pvanalytics.quality.irradiance import calculate_component_sum_series\nimport pvlib\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport pathlib"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, read in data from the RMIS NREL system. This data set contains\n5-minute right-aligned data. It includes POA, GHI,\nDNI, DHI, and GNI measurements.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pvanalytics_dir = pathlib.Path(pvanalytics.__file__).parent\nrmis_file = pvanalytics_dir / 'data' / 'irradiance_RMIS_NREL.csv'\ndata = pd.read_csv(rmis_file, index_col=0, parse_dates=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now generate solar zenith estimates for the location,\nbased on the data's time zone and site latitude-longitude\ncoordinates. This is done using the\n:py:func:`pvlib.solarposition.get_solarposition` function.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "latitude = 39.742\nlongitude = -105.18\ntime_zone = \"Etc/GMT+7\"\ndata = data.tz_localize(time_zone)\nsolar_position = pvlib.solarposition.get_solarposition(data.index,\n                                                       latitude,\n                                                       longitude)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get the clearsky DNI values associated with the current location, using\nthe :py:func:`pvlib.solarposition.get_solarposition` function. These clearsky\nvalues are used to calculate DNI data.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "site = pvlib.location.Location(latitude, longitude, tz=time_zone)\nclearsky = site.get_clearsky(data.index)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use :py:func:`pvanalytics.quality.irradiance.calcuate_ghi_component`\nto estimate GHI measurements using DHI and DNI measurements\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "component_sum_ghi = calculate_component_sum_series(\n    solar_zenith=solar_position['zenith'],\n    dhi=data['irradiance_dhi__7983'],\n    dni=data['irradiance_dni__7982'],\n    zenith_limit=90,\n    fill_night_value='equation')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot the 'irradiance_ghi__7981' data stream against the estimated component\nsum GHI, for comparison\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data['irradiance_ghi__7981'].plot()\ncomponent_sum_ghi.plot()\nplt.legend(labels=[\"RMIS GHI\", \"Component Sum GHI\"],\n           loc=\"upper left\")\nplt.xlabel(\"Date\")\nplt.ylabel(\"GHI (W/m^2)\")\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use :py:func:`pvanalytics.quality.irradiance.calcuate_dhi_component`\nto estimate DHI measurements using GHI and DNI measurements\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "component_sum_dhi = calculate_component_sum_series(\n    solar_zenith=solar_position['zenith'],\n    dni=data['irradiance_dni__7982'],\n    ghi=data['irradiance_ghi__7981'],\n    zenith_limit=90,\n    fill_night_value='equation')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot the 'irradiance_dhi__7983' data stream against the estimated component\nsum GHI, for comparison\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data['irradiance_dhi__7983'].plot()\ncomponent_sum_dhi.plot()\nplt.legend(labels=[\"RMIS DHI\", \"Component Sum DHI\"],\n           loc=\"upper left\")\nplt.xlabel(\"Date\")\nplt.ylabel(\"GHI (W/m^2)\")\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use :py:func:`pvanalytics.quality.irradiance.calcuate_dni_component`\nto estimate DNI measurements using GHI and DHI measurements\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "component_sum_dni = calculate_component_sum_series(\n    solar_zenith=solar_position['zenith'],\n    dhi=data['irradiance_dhi__7983'],\n    ghi=data['irradiance_ghi__7981'],\n    dni_clear=clearsky['dni'],\n    zenith_limit=90,\n    fill_night_value='equation')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot the 'irradiance_dni__7982' data stream against the estimated component\nsum GHI, for comparison\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data['irradiance_dni__7982'].plot()\ncomponent_sum_dni.plot()\nplt.legend(labels=[\"RMIS DNI\", \"Component Sum DNI\"],\n           loc=\"upper left\")\nplt.xlabel(\"Date\")\nplt.ylabel(\"DNI (W/m^2)\")\nplt.tight_layout()\nplt.show()"
      ]
    }
  ],
  "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.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}