{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Detect if a System is Tracking\n\nIdentifying if a system is tracking or fixed tilt\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "It is valuable to identify if a system is fixed tilt or tracking for\nfuture analysis. This example shows how to use\n:py:func:`pvanalytics.system.is_tracking_envelope` to determine if a\nsystem is tracking or not by fitting data to a maximum power or\nirradiance envelope, and fitting this envelope to quadratic and\nquartic curves. The r^2 output from these fits is used to determine\nif the system fits a tracking or fixed-tilt profile.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pvanalytics\nfrom pvanalytics.system import is_tracking_envelope\nfrom pvanalytics.features.clipping import geometric\nfrom pvanalytics.features.daytime import power_or_irradiance\nimport pandas as pd\nimport pathlib\nimport matplotlib.pyplot as plt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, we import an AC power data stream from the SERF East site located at\nNREL. This data set is publicly available via the PVDAQ database in the\nDOE Open Energy Data Initiative (OEDI)\n(https://data.openei.org/submissions/4568), under system ID 50.\nThis data is timezone-localized. This particular data stream is associated\nwith a fixed-tilt system.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pvanalytics_dir = pathlib.Path(pvanalytics.__file__).parent\nac_power_file = pvanalytics_dir / 'data' / \\\n    'serf_east_15min_ac_power.csv'\ndata = pd.read_csv(ac_power_file, index_col=0, parse_dates=True)\ndata = data.sort_index()\ntime_series = data['ac_power']\ntime_series = time_series.asfreq('15T')\n\n# Plot the first few days of the time series to visualize it\ntime_series[:pd.to_datetime(\"2016-07-06 00:00:00-07:00\")].plot()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Run the clipping and the daytime filters on the time series.\nBoth of these masks will be used as inputs to the\n:py:func:`pvanalytics.system.is_tracking_envelope` function.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Generate the daylight mask for the AC power time series\ndaytime_mask = power_or_irradiance(time_series)\n\n# Generate the clipping mask for the time series\nclipping_mask = geometric(time_series)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, we use :py:func:`pvanalytics.system.is_tracking_envelope` to\nidentify if the data stream is associated with a tracking or fixed-tilt\nsystem.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "predicted_mounting_config = is_tracking_envelope(time_series,\n                                                 daytime_mask,\n                                                 clipping_mask)\n\nprint(\"Estimated mounting configuration: \" + predicted_mounting_config.name)"
      ]
    }
  ],
  "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
}