{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Synchronize the opengrid data to your computer.  \n",
    "\n",
    "There are two different solutions.  \n",
    "\n",
    "1. The first approach is based on the **tmpo data format** and uses a local database. This approach is more elegant, but does not cover historic data from before 23rd of October 2014.\n",
    "2. The second is based on **csv files** that are stored on the opengrid webserver.  These csv files are extracted from the flukso webserver and stored as a zip file per day.  This script will only download missing files and convert all the data into a single csv per sensor. This approach covers all possible opengrid data as for now.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import inspect\n",
    "import tmpo\n",
    "\n",
    "from opengrid.library import fluksoapi\n",
    "from opengrid.library import houseprint\n",
    "from opengrid import config\n",
    "c = config.Config()\n",
    "\n",
    "# for tmpo, only use user-speficied path if a correct path is provided\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"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the cell below if you want to obtain the very last houseprint information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%run cache_anonymous_houseprint.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a tmpo session and load the houseprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "tmpos = tmpo.Session(path_to_tmpo_data)\n",
    "tmpos.debug = True\n",
    "hp = houseprint.load_houseprint_from_file('new_houseprint.pkl')\n",
    "hp.init_tmpo(tmpos)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make sure all known sensors of the houseprint are added to the tmpo database.  Then synchronize all data up to now."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "hp.sync_tmpos()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CSV-based synchronization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# path to data is stored in opengrid.cfg.\n",
    "# for syncing with csv\n",
    "path_to_csv_data = c.get('data', 'folder') \n",
    "# This synchronization can take a while...\n",
    "fluksoapi.synchronize(path_to_csv_data)\n",
    "# see the other notebooks on how to import the data and start analysing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "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.5.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
