{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## This script shows how to use the existing code in opengrid \n",
    "## to create (a) a timeseries plot and (b) a load curve of gas, water or elektricity usage. \n",
    "\n",
    "# Todo: cleanup this notebook.  See Demo_Units_and_Conversions.ipynb\n",
    "Change numeric \"chosen_type\" to a textual choice, with lookupvalue of UtilityType in Utilitytypes.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import inspect\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.dates import HourLocator, DateFormatter, AutoDateLocator\n",
    "import datetime as dt\n",
    "import pytz\n",
    "import pandas as pd\n",
    "import pdb\n",
    "import tmpo\n",
    "\n",
    "from opengrid import config\n",
    "from opengrid.library import houseprint\n",
    "\n",
    "c = config.Config()\n",
    "\n",
    "try:\n",
    "    if os.path.exists(c.get('tmpo', 'data')):\n",
    "        path_to_tmpo_data = c.get('tmpo', 'data')\n",
    "except:\n",
    "    path_to_tmpo_data = None\n",
    "\n",
    "%matplotlib inline\n",
    "plt.rcParams['figure.figsize']=14,8"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Script settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "hp = houseprint.load_houseprint_from_file('new_houseprint.pkl')\n",
    "hp.init_tmpo(path_to_tmpo_data=path_to_tmpo_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Fill in here (chosen type [0-2]) what type of data you'd like to plot:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "chosen_type = 2\n",
    "#  0 =water, 1 = gas, 2 = electricity\n",
    "\n",
    "UtilityTypes = ['water', 'gas','electricity'] # {'water','gas','electricity'} \n",
    "utility =  UtilityTypes[chosen_type] # here 'electricity'\n",
    "\n",
    "#default values:\n",
    "FL_units = ['l/day', 'm^3/day ~ 10 kWh/day','Ws/day'] #TODO, to be checked!!\n",
    "Base_Units = ['l/min', 'kW','kW']\n",
    "Base_Corr = [1/24.0/60.0, 1/100.0/24.0/3.600 , 3.600/1000.0/24 ] #TODO,check validity of conversions!! # water => (l/day) to (l/hr), gas: (l/day) to (kW), elektr Ws/d to kW\n",
    "\n",
    "tInt_Units = ['l', 'kWh','kWh'] #units after integration\n",
    "tInt_Corr = [1/60, 3600/60, 3600/60] #TODO, to be checked!! # water => (l/hr) to (l_cumul/min), gas: kW to (kWh/min)\n",
    "\n",
    "# units for this utility type\n",
    "bUnit = Base_Units[chosen_type]\n",
    "bCorr = Base_Corr[chosen_type]\n",
    "fl_unit = FL_units[chosen_type]\n",
    "tiUnit = tInt_Units[chosen_type]\n",
    "tiCorr = tInt_Corr[chosen_type]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Available data is loaded in one big dataframe, the columns are the sensors of chosen type.\n",
    "Also, it is rescaled to more \"managable\" units (to be verified!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#load data, only for last 4 weeks\n",
    "start = pd.Timestamp('now') - pd.Timedelta(days=1)\n",
    "print('Loading', utility ,'-data and converting from ',fl_unit ,' to ',bUnit,':')\n",
    "df = hp.get_data(sensortype=utility, resample='min')\n",
    "df = df.diff() #data is cumulative, we need to take the derivative\n",
    "df = df[df>0] #filter out negative values\n",
    "\n",
    "# conversion dependent on type of utility (to be checked!!) \n",
    "df = df*bCorr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# plot timeseries and load duration for each retained sensor\n",
    "\n",
    "for sensor in df.columns:\n",
    "    FL = hp.find_sensor(sensor).device.key\n",
    "    plt.figure()\n",
    "    ax1=plt.subplot(121)\n",
    "    plt.plot_date(df.index, df[sensor], '-', label=\"{}\".format(FL))\n",
    "    plt.ylabel(\"{}-usage [{}]\".format(utility,bUnit) )\n",
    "    plt.legend()\n",
    "    \n",
    "    ax2=plt.subplot(122)\n",
    "    plt.plot(np.sort(df[sensor])[::-1], label=sensor)\n",
    "    plt.ylabel(\"{}-load curve [{}]\".format(utility,bUnit) )\n",
    "    plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#Date/Time library\n",
    "from arrow import Arrow\n",
    "\n",
    "#Prepare NVD3.js dependencies\n",
    "from IPython import display as d\n",
    "import nvd3\n",
    "nvd3.ipynb.initialize_javascript(use_remote=True)\n",
    "\n",
    "#Filter sensors and period\n",
    "sensorlist = ['b28509eb97137e723995838c393d49df', '2923b75daf93e539e37ce5177c0008c5', 'a926bc966f178fc5d507a569a5bfc3d7']\n",
    "df_water= df[sensorlist][Arrow(2015, 4, 1).datetime:Arrow(2015, 4, 2).datetime].dropna()\n",
    "\n",
    "#Prepare chart name and timescale in epoch\n",
    "chart_name = \"{}-usage [{}]\".format(utility,bUnit)\n",
    "df_water[\"epoch\"] = [(Arrow.fromdatetime(o) - Arrow(1970, 1, 1)).total_seconds()*1000 for o in df_water.index]\n",
    "\n",
    "#Create NVD3 chart\n",
    "water_chart = nvd3.lineChart(x_is_date=True,name=chart_name,height=450,width=800)\n",
    "for sensor in sensorlist: # df.columns:\n",
    "    series_name = name=\"{}\".format(hp.find_sensor(sensor).device.key)\n",
    "    water_chart.add_serie(name=series_name, x=list(df_water[\"epoch\"]), y=list(df_water[sensor]))\n",
    "\n",
    "water_chart"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tests with the tmpo-based approach"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "start = pd.Timestamp('20150201')\n",
    "end = pd.Timestamp('20150301')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dfcum = hp.get_data(sensortype='electricity', head= start, tail = end)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dfcum.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dfcum.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dfcum.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dfi = dfcum.resample(rule='900s', how='max')\n",
    "dfi = dfi.interpolate(method='time')\n",
    "dfi=dfi.diff()*3600/900\n",
    "dfi.plot()\n",
    "#dfi.ix['20150701'].plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# This works, but is a bad idea if you have multiple sensors for a FLM: you obtain identical column names.\n",
    "# df.rename(columns = hp.get_flukso_from_sensor, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Getting a single sensor\n",
    "dfi['1a1dac9c2ac155f95c58bf1d4f4b7d01'].plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
