{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Infer Array Tilt/Azimuth - PVWatts Method\n\nInfer the azimuth and tilt of a system using PVWatts-based methods\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Identifing and/or validating  the azimuth and tilt information for a\nsystem is important, as these values must be correct for degradation\nand system yield analysis. This example shows how to use\n:py:func:`pvanalytics.system.infer_orientation_fit_pvwatts` to estimate\na fixed-tilt system's azimuth and tilt, using the system's known\nlatitude-longitude coordinates and an associated AC power time series.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pvanalytics\nimport matplotlib.pyplot as plt\nfrom pvanalytics import system\nimport pandas as pd\nimport pathlib\nimport pvlib"
      ]
    },
    {
      "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.\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' / '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()\n\n# Outline the ground truth metadata associated with the system\nlatitude = 39.742\nlongitude = -105.1727\nactual_azimuth = 158\nactual_tilt = 45"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Next, we import the PSM3 data generated via the\n:py:func:`pvlib.iotools.get_psm3` function, using\nsite latitude-longitude coordinates. To generate the\nPSM3 data, you must first register for NREL's NSDRB API at the\nfollowing link: https://developer.nrel.gov/signup/.\nPSM3 data can then be retrieved using :py:func:`pvlib.iotools.get_psm3`.\nThe PSM3 data has been resampled to 15 minute intervals, to match the AC\npower data.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "psm3_file = pvanalytics_dir / 'data' / 'serf_east_psm3_data.csv'\npsm3 = pd.read_csv(psm3_file, index_col=0, parse_dates=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Filter the PSM3 data to only include clearsky periods\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "is_clear = (psm3.ghi_clear == psm3.ghi)\nis_daytime = (psm3.ghi > 0)\ntime_series_clearsky = time_series[is_clear & is_daytime]\ntime_series_clearsky = time_series_clearsky.dropna()\npsm3_clearsky = psm3.loc[time_series_clearsky.index]\n\n# Get solar azimuth and zenith from pvlib, based on\n# lat-long coords\nsolpos_clearsky = pvlib.solarposition.get_solarposition(\n    time_series_clearsky.index, latitude, longitude)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Run the pvlib data and the sensor-based time series data through\nthe :py:func:`pvanalytics.system.infer_orientation_fit_pvwatts` function.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "best_tilt, best_azimuth, r2 = system.infer_orientation_fit_pvwatts(\n    time_series_clearsky,\n    psm3_clearsky.ghi_clear,\n    psm3_clearsky.dhi_clear,\n    psm3_clearsky.dni_clear,\n    solpos_clearsky.zenith,\n    solpos_clearsky.azimuth,\n    temperature=psm3_clearsky.temp_air,\n)\n\n# Compare actual system azimuth and tilt to predicted azimuth and tilt\nprint(\"Actual Azimuth: \" + str(actual_azimuth))\nprint(\"Predicted Azimuth: \" + str(best_azimuth))\nprint(\"Actual Tilt: \" + str(actual_tilt))\nprint(\"Predicted Tilt: \" + str(best_tilt))"
      ]
    }
  ],
  "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
}