{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Clear-Sky Detection\n\nIdentifying periods of clear-sky conditions using measured irradiance.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Identifying and filtering for clear-sky conditions is a useful way to\nreduce noise when analyzing measured data.  This example shows how to\nuse :py:func:`pvanalytics.features.clearsky.reno` to identify clear-sky\nconditions using measured GHI data.  For this example we'll use\nGHI measurements from NREL in Golden CO.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pvanalytics\nfrom pvanalytics.features.clearsky import reno\nimport pvlib\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport pathlib"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, read in the GHI measurements.  For this example we'll use an example\nfile included in pvanalytics covering a single day, but the same process\napplies to data of any length.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pvanalytics_dir = pathlib.Path(pvanalytics.__file__).parent\nghi_file = pvanalytics_dir / 'data' / 'midc_bms_ghi_20220120.csv'\ndata = pd.read_csv(ghi_file, index_col=0, parse_dates=True)\n\n# or you can fetch the data straight from the source using pvlib:\n# date = pd.to_datetime('2022-01-20')\n# data = pvlib.iotools.read_midc_raw_data_from_nrel('BMS', date, date)\n\nmeasured_ghi = data['Global CMP22 (vent/cor) [W/m^2]']"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now model clear-sky irradiance for the location and times of the\nmeasured data:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "location = pvlib.location.Location(39.742, -105.18)\nclearsky = location.get_clearsky(data.index)\nclearsky_ghi = clearsky['ghi']"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally, use :py:func:`pvanalytics.features.clearsky.reno` to identify\nmeasurements during clear-sky conditions:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "is_clearsky = reno(measured_ghi, clearsky_ghi)\n\n# clear-sky times indicated in black\nmeasured_ghi.plot()\nmeasured_ghi[is_clearsky].plot(ls='', marker='o', ms=2, c='k')\nplt.ylabel('Global Horizontal Irradiance [W/m2]')\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
}