{
 "metadata": {
  "name": "",
  "signature": "sha256:b2e4fc657d79ba1ed1f437d2f7fe86d968186190cec3ad88e90a7f0dfffe9518"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##Function: Fetch historic and current ambient conditions, from a wunderground weather station.  \n",
      "\n",
      "Note: To make this code work, you have to make a (free) account on wunderground and obtain an API key. \n",
      "A Wunderground cumulus account is free of charge, and supports 500 calls per day, 10 per minute. \n",
      "Request it here: http://api.wunderground.com/members/signup.asp\n",
      "\n",
      "Then fill in your assigned developer apikey either in script settings below, or store your key in the opengrid.cfg file, with header [Wunderground] (see opengrid.cfg-example)\n",
      "\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import os, sys\n",
      "import inspect\n",
      "import numpy as np\n",
      "import matplotlib.pyplot as plt\n",
      "from matplotlib.dates import HourLocator, DateFormatter, AutoDateLocator\n",
      "import datetime as dt\n",
      "import pytz\n",
      "import pandas as pd\n",
      "import pdb\n",
      "\n",
      "\n",
      "# add the sccript path to opengrid to sys.path\n",
      "script_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))\n",
      "sys.path.append(os.path.join(script_dir, os.pardir, os.pardir))\n",
      "\n",
      "from opengrid.library import config\n",
      "from opengrid.library import wundergroundapi as WGapi\n",
      "\n",
      "%matplotlib inline\n",
      "plt.rcParams['figure.figsize']=14,8"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##Script settings"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "c = config.Config()\n",
      "\n",
      "key = c.get('Wunderground', 'apikey')\n",
      "#apikey = 'SET_CUSTOM_KEY_HERE'\n",
      "path_to_data = c.get('data', 'folder')\n",
      "#path_to_data = 'SET_CUSTOM_PATH_HERE'\n",
      "\n",
      "if not os.path.exists(path_to_data):\n",
      "    raise IOError(\"Provide your path to the data.  This is a folder containing a 'zip' and 'csv' subfolder.\")\n",
      "else:\n",
      "    path_to_csv = os.path.join(path_to_data, 'csv')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "key # print key to see if it is valid"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "reload(WGapi)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Create an object of the Wounderground class and get current weather conditions\n",
      "We set a small time delay to test the caching. In practice, I think time delays of 15 minutes are fine?"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "wg = WGapi.Wunderground(apikey=key, city='Leuven', time_delay=10)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Try running the cell below several times to see how the caching works. "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for prop in ['temp_c', 'solarradiation', 'wind_kph']:\n",
      "    curr_value , timestamp = wg.get_current(prop)\n",
      "    print(\"Current {} in {} is: {} (on {})\".format(prop, \n",
      "                                                   wg.city,\n",
      "                                                   curr_value,\n",
      "                                                   pd.Timestamp(timestamp, unit='s', tz='Europe/Brussels')))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "wg.get_current('wind_kph')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "When we ask a property that is not known, a list with know properties is printed and a ValueError is raised"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "wg.get_current('relhum')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Example for CURRENT weather detail readings\n",
      "\n",
      "location = 'Leuven'\n",
      "Some_property_options =[ 'relative_humidity','solarradiation', 'temp_c', 'temp_f','temperature_string','visibility_km','lotsmore']\n",
      "#(full list: see pprint(parsed_json))\n",
      "Property ='temp_c'\n",
      "curr_value , currdate = WGapi.fetch_curr_conditions(key,location, prop= Property)\n",
      "print \"Current %s (on %s) in %s is: %s\" % (Property, currdate, location, curr_value)\n",
      "\n",
      "Property = 'solarradiation'\n",
      "curr_value2 , currdate = WGapi.fetch_curr_conditions(key,location,prop=Property)\n",
      "print \"Current %s (on %s) in %s is: %s\" % (Property, currdate, location, curr_value2)\n",
      "\n",
      "#Todo: add units to this example\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Examples for HISTORIC weather detail readings: x days ago\n",
      "city = 'Leuven'\n",
      "\n",
      "TempDF = WGapi.details_xdaysago(key,city,5,prop = 'tempm',columnname ='T_out [degC]')\n",
      "relhum = WGapi.details_xdaysago(key,city,5,prop = 'hum',columnname ='Rel humidity [%]')\n",
      "\n",
      "df = TempDF.join(relhum)\n",
      "df.plot(subplots=True)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#full historic weather details\n",
      "Tout_h = WGapi.fetch_historic_tempYMD(key,'Leuven',2014,9,8,prop='tempm', columnname = u'T_out [\u00b0C]') # for 8th of September, thus YYYYMMDD\n",
      "hum_h = WGapi.fetch_historic_tempYMD(key,'Leuven',2014,9,8,prop='hum',columnname= 'RelHumidity [%]') # for 8th of September, thus YYYYMMDD\n",
      "#solrad = WGapi.fetch_historic_tempYMD(key,'Leuven',2014,9,8,prop='solarradiation',columnname= u'Solar radiation [W/m\u00b2]') # for 8th of September, thus YYYYMMDD\n",
      "\n",
      "df = pd.concat([Tout_h, hum_h], axis=1)\n",
      "df.plot(subplots=True)\n",
      "df.head()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Examples for Historic day-AVERAGE weather fetching\n",
      "#for more options, uncomment pprint(parsed_json[\"history\"]['dailysummary'])\n",
      "#just one average temp\n",
      "out2 =WGapi.fetch_historic_dayaverage(key,'Leuven',2014,8,10, prop ='meantempm',columnname ='T_out') # for 10th of august, thus YYYMMDD\n",
      "#or some day summaries: \n",
      "HDD =WGapi.fetch_historic_dayaverage(key,'Leuven',2014,8,10, prop ='heatingdegreedays',columnname ='HDD') # for 10th of august, thus YYYYMMDD\n",
      "CDD =WGapi.fetch_historic_dayaverage(key,'Leuven',2014,8,10, prop ='coolingdegreedays',columnname ='CDD') # for 10th of august, thus YYYYMMDD\n",
      "print out2.join(HDD.join(CDD)) #keep less than 10 calls per min..."
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# WIP / Todo\n",
      "##WIP-part  (WORK IN PROGRESS)\n",
      "# 0) Add script to give list of possible query options.  \n",
      "# 1) Add script to find weather station component.  \n",
      "# \n",
      "# A) (low prio) fix figure X-axis in date & make some more fancy graphs (improved date-axis)\n",
      "# B) (high prio) Multiple calls (such as Average_Temp_Xdaysago could be grouped in single call too. (api call that gets longer list of data).\n",
      "# C) (high prio) make a usefull example: e.g. Current solar radiation vs PV production , or (historic) HDD vs gas usage.\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#A)fancy graphs (improved date-axis)\n",
      "import matplotlib.dates\n",
      "fig = plt.figure()\n",
      "ax = fig.gca() \n",
      " \n",
      "# Plotting stuff here ...\n",
      "ax.plot_date(dates, temps, 'b.-')\n",
      " \n",
      "# Set major x ticks on Mondays.\n",
      "ax.xaxis.set_major_locator(\n",
      "    matplotlib.dates.WeekdayLocator(byweekday=matplotlib.dates.MO)\n",
      ")\n",
      "ax.xaxis.set_major_formatter(\n",
      "    matplotlib.dates.DateFormatter('%a %d\\n%b %Y')\n",
      ")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#WIP B)\n",
      "def WGapi.Average_Temp_Xdaysago(key,city,ndays = 5,prop = \"temp_c\",PDcolumnname ='T_out', averaged =True ,plot_data = True, \n",
      "                          startdate = dt.datetime.today()):\n",
      "    '''\n",
      "    Gives and/or plots list of (average or detailed) outdoor temperatures over a longer (fixed) number of days, \n",
      "    starting from day before today (or startdate).\n",
      "\n",
      "    Inputs:\n",
      "    * key: API token\n",
      "    * city: name of city, string\n",
      "    * ndays: number of fetched days before startdate . Note: Please set ndays<10 because of nr of calls allowed per minute (==10)!!\n",
      "    * averaged: True => Daily-averaged, False =>  detailed (20 minute) profile\n",
      "    * plot: on or off.\n",
      "    * startdate: defaults to today, but can be used to fetch historic period too.\n",
      "    '''\n",
      "    print 'Detailed ambient temperature in ',city,' for last', str(ndays-1),' days'\n",
      "    \n",
      "    i = 1 #starting from day before startdate.\n",
      "    date_i =  startdate- dt.timedelta(days=i)\n",
      "    if averaged:\n",
      "        Texdf = WGapi.Fetch_historic_dayaverage_By_date(key,city,date_i,prop,PDcolumnname)\n",
      "    else:\n",
      "        Texdf = WGapi.Fetch_historic_temp_ByDate(key,city,date_i,prop,PDcolumnname) \n",
      "    \n",
      "    # one average: Fetch_historic_dayaverage_By_date(key,city,date_i)\n",
      "    # = pd.DataFrame( data=Tyest['T_out'].values, index= Tyest.index)\n",
      "    for i in range(2,ndays,1): \n",
      "        date_i =  startdate- datetime.timedelta(days=i)\n",
      "        if averaged:\n",
      "            new_df = WGapi.Fetch_historic_dayaverage_By_date(key,city,date_i,prop,PDcolumnname)\n",
      "        else:\n",
      "            new_df = WGapi.Fetch_historic_temp_ByDate(key,city,date_i,prop,PDcolumnname) \n",
      "    \n",
      "        Texdf = pd.DataFrame.combine_first(Texdf,new_df)\n",
      "\n",
      "\n",
      "    if plot_data:\n",
      "    # sorted trough combine_first\n",
      "        temps = []\n",
      "        dates = []\n",
      "        temps = Texdf.values\n",
      "        dates = Texdf.index\n",
      "        title = 'Details on subhourly outdoor temperatures over multiple days'\n",
      "        fig=figure\n",
      "        f1 = plt.plot(dates,temps)\n",
      "        x_label = 'Ambient temperature (\u00b0C)'\n",
      "        plt.ylabel([x_label])\n",
      "        plt.title(title)\n",
      "    return Texdf\n",
      "    \n",
      "#Example:\n",
      "Temps=WGapi.Average_Temp_Xdaysago(key,'Leuven',ndays = 5, averaged =False,plot_data = True, startdate = dt.datetime.today() )"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}