{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## This script shows the visualization of electricity, water and gas consumption using carpet plots\n",
    "\n",
    "To get started, first run the 'Synchronize data' script\n",
    "\n",
    "#### Imports and paths"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import pytz\n",
    "import time\n",
    "import inspect\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import datetime as dt\n",
    "import matplotlib.pyplot as plt\n",
    "import tmpo\n",
    "\n",
    "from opengrid import config\n",
    "from opengrid.library import plotting\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",
    "# configuration for the plots\n",
    "DEV = c.get('env', 'type') == 'dev' # DEV is True if we are in development environment, False if on the droplet\n",
    "print(\"Environment configured for development: {}\".format(DEV))\n",
    "if not DEV:\n",
    "    # production environment: don't try to display plots\n",
    "    import matplotlib\n",
    "    matplotlib.use('Agg')\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.dates import MinuteLocator, HourLocator, DateFormatter, AutoDateLocator, num2date\n",
    "\n",
    "if DEV:\n",
    "    if c.get('env', 'plots') == 'inline':\n",
    "        %matplotlib inline\n",
    "    else:\n",
    "        %matplotlib qt\n",
    "else:\n",
    "    pass # don't try to render plots\n",
    "\n",
    "plt.rcParams['figure.figsize'] = 16,8\n",
    "\n",
    "# path to data\n",
    "path_to_data = c.get('data', 'folder')\n",
    "if not os.path.exists(path_to_data):\n",
    "    raise IOError(\"Provide your path to the data in your config.ini file. \")\n",
    "else:\n",
    "    path_to_fig = os.path.join(path_to_data, 'figures')\n",
    "    if not os.path.isdir(path_to_fig): os.makedirs(path_to_fig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "c.get('data','folder')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Loading meta data and user variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "hp = houseprint.Houseprint()\n",
    "\n",
    "end = pd.Timestamp(time.time(), unit='s')\n",
    "start = end - pd.Timedelta('21 days')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "hp.save('new_houseprint.pkl')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#hp.sync_tmpos()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Water sensors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "water_sensors = hp.get_sensors(sensortype='water')\n",
    "print(\"{} water sensors\".format(len(water_sensors)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "for sensor in water_sensors:\n",
    "    ts = sensor.get_data(head=start, tail=end)\n",
    "    if not ts.dropna().empty:\n",
    "        plotting.carpet(ts, title=' - '.join([sensor.device.key, sensor.description, sensor.key]), zlabel=r'Flow [l/min]')\n",
    "        plt.savefig(os.path.join(path_to_fig, 'carpet_'+sensor.type+'_'+sensor.key), dpi=100)\n",
    "        if not DEV:\n",
    "            plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Gas sensors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "gas_sensors = hp.get_sensors(sensortype=('gas'))\n",
    "print(\"{} gas sensors\".format(len(gas_sensors)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "for sensor in gas_sensors:\n",
    "    ts = sensor.get_data(head=start, tail=end)\n",
    "    if not ts.dropna().empty:\n",
    "        plotting.carpet(ts, title=' - '.join([sensor.device.key, sensor.description, sensor.key]), zlabel=r'Gas consumption [W]')\n",
    "        plt.savefig(os.path.join(path_to_fig, 'carpet_'+sensor.type+'_'+sensor.key), dpi=100)\n",
    "        if not DEV:\n",
    "            plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Electricity sensors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "elec_sensors = hp.get_sensors(sensortype=('electricity'))\n",
    "print(\"{} electricity sensors\".format(len(elec_sensors)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "for sensor in elec_sensors:\n",
    "    ts = sensor.get_data(head=start, tail=end)\n",
    "    if not ts.dropna().empty:\n",
    "        plotting.carpet(ts, title=' - '.join([sensor.device.key, sensor.description, sensor.key]), zlabel=r'Power [W]')\n",
    "        plt.savefig(os.path.join(path_to_fig, 'carpet_'+sensor.type+'_'+sensor.key), dpi=100)\n",
    "        if not DEV:\n",
    "            plt.close()"
   ]
  },
  {
   "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
}
