{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# What's new in the Forecastwrapper"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "• No more seperate objects `Weather_Days` and `Weather_Hours`, just `Weather`\n",
    "\n",
    "• The `Weather` object has methods `days()` and `hours()` which return a dataframe in the desired resolution. (We chose this approach because now you only need 1 call to forecast.io to get both resolutions, in V1 you needed 2 calls if you wanted both)\n",
    "\n",
    "• More verbose and pythonian named arguments. Eg. the flag `heatingDegreeDays` is now called `include_heating_degree_days`\n",
    "\n",
    "• The temperature equivalent is now by default included in the daily dataframe. It can be excluded by using the flag `include_temperature_equivalent = False`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demo of the forecast.io wrapper to get past and future weather data\n",
    "\n",
    "Important: you need to register for an apikey here: https://developer.forecast.io/  Put the key you obtain in the opengrid.cfg file as follows:\n",
    "\n",
    "    [Forecast.io]\n",
    "    apikey: your_key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import inspect\n",
    "import pandas as pd\n",
    "import charts\n",
    "\n",
    "from opengrid import config"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get Forecast.io API key from config file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "config = config.Config()\n",
    "\n",
    "#get Forecast.io API Key\n",
    "api_key = config.get('Forecast.io', 'apikey')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import API wrapper module"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from opengrid.library import forecastwrapper"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Get weather data in daily and hourly resolution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get started, create a Weather object for a certain location and a period"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "start = pd.Timestamp('20150813')\n",
    "end = pd.Timestamp('20150816')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Weather_Ukkel = forecastwrapper.Weather(api_key=api_key, location='Ukkel', start=start, end=end)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can use the methods `days()` and `hours()` to get a dataframe in daily or hourly resolution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Weather_Ukkel.days().info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Weather_Ukkel.hours().info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Degree Days"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Daily resolution has the option of adding degree days.\n",
    "By default, the temperature equivalent and heating degree days with a base temperature of 16.5°C are added.\n",
    "\n",
    "Heating degree days are calculated as follows:\n",
    "\n",
    "$$heatingDegreeDays = max(0 , baseTemp - 0.6 * T_{today} + 0.3 * T_{today-1} + 0.1 * T_{today-2} )$$\n",
    "\n",
    "Cooling degree days are calculated in an analog way:\n",
    "\n",
    "$$coolingDegreeDays = max(0, 0.6 * T_{today} + 0.3 * T_{today-1} + 0.1 * T_{today-2} - baseTemp )$$\n",
    "\n",
    "Add degree days by setting the flag `include_heating_degree_days` or `include_cooling_degree_days` and supplying `heating_base_temperatures` and/or `cooling_base_temperatures` as a list (you can add multiple base temperatures, or just a list of 1 element)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Get some more degree days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Weather_Ukkel.days(include_heating_degree_days = True,\n",
    "                   heating_base_temperatures = [15,18],\n",
    "                   include_cooling_degree_days = True,\n",
    "                   cooling_base_temperatures = [18,24]).filter(like='DegreeDays')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Daytime Averages"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Daily resolution has the option of adding average values during the daytime only (between sunrise and sunset). Implemented are daytimeCloudCover and daytimeTemperature, and are included by default. They can be excluded by setting `include_daytime_cloud_cover = False` or `include_daytime_temperature = False`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, daily weather data is extended with *daytimeCloudCover* and *daytimeTemperature*.  These are the averaged cloud cover and temperature **between sunrise and sunset**. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Weather_Ukkel.days().filter(like='daytime')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Hourly resolution example"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Location can also be coördinates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "start = pd.Timestamp('20150916')\n",
    "end = pd.Timestamp('20150918')\n",
    "Weather_Brussel = forecastwrapper.Weather(api_key=api_key, location=[50.8503396, 4.3517103], start=start, end=end)\n",
    "Weather_Boutersem = forecastwrapper.Weather(api_key=api_key, location='Kapelstraat 1, 3370 Boutersem', start=start, end=end)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df_combined = pd.merge(Weather_Brussel.hours(), Weather_Boutersem.hours(), suffixes=('_Brussel', '_Boutersem'), \n",
    "                       left_index=True, right_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "charts.plot(df_combined.filter(like='cloud'), stock=True, show='inline')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Deal with different timezones"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, all dataframes are localized to the timezone of the specified location.\n",
    "If you want, you can specify the tz argument to get all dataframes localized to a specified timezone."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "start = pd.Timestamp('20150815')\n",
    "end = pd.Timestamp('20150816')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Weather_London = forecastwrapper.Weather(api_key=api_key, location='London', start=start, end=end, tz='Asia/Singapore')\n",
    "Weather_Brussels = forecastwrapper.Weather(api_key=api_key, location='Brussels', start=start, end=end, tz='Asia/Singapore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Weather_London.days()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Weather_Brussels.days()"
   ]
  },
  {
   "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
}
