{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### This script shows how to use the existing code in opengrid to create an interactive timeseries plot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import pytz\n",
    "BXL = pytz.timezone('Europe/Brussels')\n",
    "import pandas as pd\n",
    "import charts\n",
    "from time import time\n",
    "\n",
    "from opengrid import config\n",
    "c=config.Config()\n",
    "DEV = c.get('env', 'type') == 'dev' # DEV is True if we are in development environment, False if on the droplet\n",
    "\n",
    "from opengrid.library import houseprint"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Script settings\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "number_of_days = 7"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### We create a dataframe per sensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "hp = houseprint.Houseprint()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start = pd.Timestamp(time() - number_of_days*86400, unit='s')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sensors = hp.get_sensors()\n",
    "#sensors.remove('b325dbc1a0d62c99a50609e919b9ea06')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for sensor in sensors:\n",
    "    s = sensor.get_data(head=start)\n",
    "    # do an ugly operation to get localtime.  A simple tz_convert does not work: charts will always plot \n",
    "    # in UTC.\n",
    "    new_index_list = []\n",
    "    for index_utc in s.index:\n",
    "        new_index_list.append(index_utc + index_utc.tz_convert(BXL).utcoffset())\n",
    "    s.index = pd.DatetimeIndex(new_index_list)\n",
    "    try:    \n",
    "        # plot with charts (don't show it) and save html\n",
    "        charts.plot(pd.DataFrame(s), stock=True, \n",
    "                    save=os.path.join(c.get('data', 'folder'), 'figures', 'TimeSeries_'+sensor.key+'.html'), show=False)\n",
    "    except Exception as e:\n",
    "        print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "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
}
