{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# QCrad Consistency for Irradiance Data\n\nCheck consistency of GHI, DHI and DNI using QCRad criteria.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Identifying and filtering out invalid irradiance data is a\nuseful way to reduce noise during analysis. In this example,\nwe use\n:py:func:`pvanalytics.quality.irradiance.check_irradiance_consistency_qcrad`\nto check the consistency of GHI, DHI and DNI data using QCRad criteria.\nFor this example we will use data from the RMIS weather system located\non the NREL campus in Colorado, USA.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pvanalytics\nfrom pvanalytics.quality.irradiance import check_irradiance_consistency_qcrad\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.\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": [
        "Use\n:py:func:`pvanalytics.quality.irradiance.check_irradiance_consistency_qcrad`\nto generate the QCRAD consistency mask.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "qcrad_consistency_mask = check_irradiance_consistency_qcrad(\n    solar_zenith=solar_position['zenith'],\n    ghi=data['irradiance_ghi__7981'],\n    dhi=data['irradiance_dhi__7983'],\n    dni=data['irradiance_dni__7982'])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot the GHI, DHI, and DNI data streams with the QCRAD\nconsistency mask overlay. This mask applies to all 3 data streams.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig = data[['irradiance_ghi__7981', 'irradiance_dhi__7983',\n            'irradiance_dni__7982']].plot()\n# Highlight periods where the QCRAD consistency mask is True\nfig.fill_between(data.index, fig.get_ylim()[0], fig.get_ylim()[1],\n                 where=qcrad_consistency_mask[0], alpha=0.4)\nfig.legend(labels=[\"RMIS GHI\", \"RMIS DHI\", \"RMIS DNI\", \"QCRAD Consistent\"],\n           loc=\"upper center\")\nplt.xlabel(\"Date\")\nplt.ylabel(\"Irradiance (W/m^2)\")\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot the GHI, DHI, and DNI data streams with the diffuse\nratio limit mask overlay. This mask is true when the\nDHI / GHI ratio passes the limit test.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig = data[['irradiance_ghi__7981', 'irradiance_dhi__7983',\n            'irradiance_dni__7982']].plot()\n# Highlight periods where the GHI ratio passes the limit test\nfig.fill_between(data.index, fig.get_ylim()[0], fig.get_ylim()[1],\n                 where=qcrad_consistency_mask[1], alpha=0.4)\nfig.legend(labels=[\"RMIS GHI\", \"RMIS DHI\", \"RMIS DNI\",\n                   \"Within Diffuse Ratio Limit\"],\n           loc=\"upper center\")\nplt.xlabel(\"Date\")\nplt.ylabel(\"Irradiance (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
}