{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "No module named ecmwfapi",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-5fea768081a0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mecmwfapi\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mECMWFService\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mMagics\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtoolbox\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mmagics\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mipywidgets\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mwidgets\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mipywidgets\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0minteract\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minteractive\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfixed\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mImportError\u001b[0m: No module named ecmwfapi"
     ]
    }
   ],
   "source": [
    "from ecmwfapi import ECMWFService\n",
    "from Magics import toolbox as magics\n",
    "import ipywidgets as widgets\n",
    "from ipywidgets import interact, interactive, fixed\n",
    "\n",
    "import random\n",
    "from datetime import date, timedelta\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def data(meteogram, parameter):\n",
    "    yesterday = date.today() - timedelta(1)\n",
    "    print yesterday.strftime('%Y%m%d')\n",
    "    print meteogram, parameter\n",
    "    json = \"%s.json\"\n",
    "    key = {\n",
    "            \"url\"   : \"https://api.ecmwf.int/v1\",\n",
    "            \"key\"   : \"83253855c912864513eb33f5fb1de322\",\n",
    "            \"email\" : \"Sylvie.Lamy-Thepaut@ecmwf.int\"\n",
    "          }\n",
    "\n",
    "\n",
    "    lat = random.randrange(-90, 90)\n",
    "    lon = random.randrange(-180, 180)\n",
    "\n",
    "    server = ECMWFService(\"meteogram\", verbose=True, **key)\n",
    "\n",
    "    yesterday = date.today() - timedelta(0)\n",
    "    print yesterday.strftime('%Y%m%d')\n",
    "\n",
    "\n",
    "    request = {\n",
    "      \"meteogram\": meteogram,\n",
    "      \"param\": parameter,\n",
    "      \"location\": \"%f/%f\" % (lat, lon),\n",
    "      \"date\": yesterday.strftime('%Y%m%d'),\n",
    "      \"time\": \"0000\"\n",
    "     }\n",
    "    \n",
    "    server.execute( request, json % parameter )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'widgets' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-2-1f804404d29a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m colour = widgets.Dropdown(\n\u001b[0m\u001b[1;32m      4\u001b[0m      \u001b[0moptions\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'evergreen'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"navy\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'ecmwf_blue'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"green\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'blue'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m     \u001b[0mconcise\u001b[0m\u001b[0;34m=\u001b[0m \u001b[0mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'widgets' is not defined"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "colour = widgets.Dropdown(\n",
    "     options=['evergreen', \"navy\", 'ecmwf_blue', \"green\", 'blue'],\n",
    "    concise= False,\n",
    "    description='Pick a color',\n",
    "    value='blue'\n",
    ")\n",
    "colour.layout.width = \"50%\"\n",
    "meteogram = widgets.Dropdown(\n",
    "    options=['10days', '15days'],\n",
    "    value='10days',\n",
    "    description='meteogran:',\n",
    "    disabled=False,\n",
    "    button_style='' # 'success', 'info', 'warning', 'danger' or ''\n",
    ")\n",
    "\n",
    "parameter = widgets.Dropdown(\n",
    "    options=['2t', \"tcc\", \"10fg6\", \"hcc\", \"lcc\", \"mcc\", \"sf\", \"tp\", \"ws\"],\n",
    "    value='2t',\n",
    "    description='Parameter:',\n",
    "    disabled=False,\n",
    "    button_style='' # 'success', 'info', 'warning', 'danger' or ''\n",
    ")\n",
    "\n",
    "def epsgram(meteogram, parameter, colour):\n",
    "    data(meteogram, parameter)\n",
    "    return  magics.epsgram(parameter, \n",
    "                           \"%s.json\" % parameter, \n",
    "                           title = \"My title for %s\" % (parameter), \n",
    "                           epsgraph = { \"eps_box_colour\" : colour} )\n",
    "\n",
    "from IPython.display import display\n",
    "\n",
    "ui = interactive(epsgram, meteogram=meteogram, parameter = parameter, colour=colour)\n",
    "\n",
    "box = widgets.VBox(ui.children)\n",
    "display(box)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "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.12"
  },
  "widgets": {
   "state": {
    "7af87027d8e44c07a98bac943a6fac9e": {
     "views": [
      {
       "cell_index": 2
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
