{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from collections import OrderedDict\n",
    "from dautil import data\n",
    "from dautil import ts\n",
    "from dautil import plotting\n",
    "import numpy as np\n",
    "import bokeh.plotting as bkh_plt\n",
    "from bokeh.models import HoverTool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def load():\n",
    "    df = data.Weather.load()['TEMP']\n",
    "    return ts.groupby_year_month(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def create_source():\n",
    "    colors = plotting.sample_hex_cmap()\n",
    "\n",
    "    month = []\n",
    "    year = []\n",
    "    color = []\n",
    "    avg = []\n",
    "\n",
    "    for year_month, group in load():\n",
    "        month.append(ts.short_month(year_month[1]))\n",
    "        year.append(str(year_month[0]))\n",
    "        monthly_avg = np.nanmean(group.values)\n",
    "        avg.append(monthly_avg)\n",
    "        color.append(colors[min(int(abs(monthly_avg)) - 2, 8)])\n",
    "\n",
    "    source = bkh_plt.ColumnDataSource(\n",
    "        data=dict(month=month, year=year, color=color, avg=avg)\n",
    "    )\n",
    "\n",
    "    return year, source"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def all_years():\n",
    "    years = set(year)\n",
    "    start_year = min(years)\n",
    "    end_year = max(years)\n",
    "\n",
    "    return [str(y) for y in range(int(start_year), int(end_year), 5)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def plot(year, source):\n",
    "    fig = bkh_plt.figure(title=\"De Bilt, NL Temperature (1901 - 2014)\",\n",
    "                         x_range=all_years(), \n",
    "                         y_range=list(reversed(ts.short_months())),\n",
    "                         toolbar_location=\"left\", \n",
    "                         tools=\"resize,hover,save,pan,box_zoom,wheel_zoom\")\n",
    "\n",
    "    fig.rect(\"year\", \"month\", 1, 1, source=source,\n",
    "        color=\"color\", line_color=None)\n",
    "\n",
    "    fig.xaxis.major_label_orientation = np.pi/3\n",
    "\n",
    "    hover = fig.select(dict(type=HoverTool))\n",
    "    hover.tooltips = OrderedDict([\n",
    "        ('date', '@month @year'),\n",
    "        ('avg', '@avg'),\n",
    "    ])\n",
    "\n",
    "    bkh_plt.output_notebook()\n",
    "    bkh_plt.show(fig)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "year, source = create_source()\n",
    "plot(year, 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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
