{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import Magics.macro as magics\n",
    "import cdsapi\n",
    "import datetime\n",
    "import ipywidgets as widgets\n",
    "from ipywidgets import Layout, Button, Box, VBox"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "t_press = ['2m_dewpoint_temperature','2m_temperature','ice_temperature_layer_1',\n",
    "            'ice_temperature_layer_2','ice_temperature_layer_3','ice_temperature_layer_4',\n",
    "            'maximum_2m_temperature_since_previous_post_processing','mean_sea_level_pressure','minimum_2m_temperature_since_previous_post_processing',\n",
    "            'sea_surface_temperature','skin_temperature','surface_pressure']\n",
    "clouds = ['cloud_base_height','high_cloud_cover','low_cloud_cover',\n",
    "            'medium_cloud_cover','total_cloud_cover','total_column_cloud_ice_water',\n",
    "            'total_column_cloud_liquid_water','vertical_integral_of_divergence_of_cloud_frozen_water_flux','vertical_integral_of_divergence_of_cloud_liquid_water_flux',\n",
    "            'vertical_integral_of_eastward_cloud_frozen_water_flux','vertical_integral_of_eastward_cloud_liquid_water_flux','vertical_integral_of_northward_cloud_frozen_water_flux',\n",
    "            'vertical_integral_of_northward_cloud_liquid_water_flux']\n",
    "lakes = ['lake_bottom_temperature','lake_cover','lake_depth',\n",
    "            'lake_ice_depth','lake_ice_temperature','lake_mix_layer_depth',\n",
    "            'lake_mix_layer_temperature','lake_shape_factor','lake_total_layer_temperature']\n",
    "prec = ['convective_precipitation','convective_rain_rate','instantaneous_large_scale_surface_precipitation_fraction',\n",
    "            'large_scale_precipitation','large_scale_precipitation_fraction','large_scale_rain_rate',\n",
    "            'maximum_total_precipitation_rate_since_previous_post_processing','minimum_total_precipitation_rate_since_previous_post_processing','precipitation_type',\n",
    "            'total_column_rain_water','total_precipitation']\n",
    "snow = ['convective_snowfall','convective_snowfall_rate_water_equivalent','large_scale_snowfall',\n",
    "            'large_scale_snowfall_rate_water_equivalent','snow_albedo','snow_density',\n",
    "            'snow_depth','snow_evaporation','snowfall',\n",
    "            'snowmelt','temperature_of_snow_layer','total_column_snow_water']\n",
    "parameter_groups = {'Temperature and pressure' : t_press, \n",
    "                    'Clouds' : clouds, \n",
    "                    'Lakes' : lakes,\n",
    "                    'Precipitation and rain' : prec,\n",
    "                    'Snow' : snow}\n",
    "types = ['ensemble_mean','ensemble_members','ensemble_spread',\n",
    "            'reanalysis']\n",
    "style = {'description_width': 'initial'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2c8403aa069e436c9910cf6b06a97768",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Dropdown(description='Group:', options={'Temperature and pressure': ['2m_dewpoint_temperature', '2m_temperatur…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "39d764f4393344e1a488c2a0f4ebb50c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Box(children=(Dropdown(description='Choose parameter:', options=('2m_dewpoint_temperature', '2m…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "group = widgets.Dropdown(options=parameter_groups,value = t_press,description='Group:', style=style)\n",
    "\n",
    "parameters = widgets.Dropdown(options = t_press,description='Choose parameter:', style=style)\n",
    "\n",
    "\n",
    "Type = widgets.Dropdown(options=types, value = 'reanalysis', description='Product types:', style=style)\n",
    "\n",
    "times = widgets.BoundedIntText(\n",
    "    value=12,\n",
    "    min=0,\n",
    "    max=24,\n",
    "    step=1,\n",
    "    description='Hour:',\n",
    "    disabled=False\n",
    ")\n",
    "\n",
    "d = datetime.datetime(2017, 1, 1, 12)\n",
    "\n",
    "dates = widgets.DatePicker(\n",
    "    description='Pick a Date',\n",
    "    value=d,\n",
    "    disabled=False\n",
    ")\n",
    "\n",
    "items_auto = [parameters, Type, dates, times]\n",
    "\n",
    "box_layout = Layout(display='flex',\n",
    "                    flex_flow='row',\n",
    "                    align_items='stretch',\n",
    "                    width='100%')\n",
    "box_auto = Box(children=items_auto, layout=box_layout)\n",
    "display(group)\n",
    "display(VBox([box_auto]))\n",
    "\n",
    "def on_value_change(change):\n",
    "        parameters.options=change[\"new\"]\n",
    "    \n",
    "\n",
    "    \n",
    "\n",
    "group.observe(on_value_change, names='value')\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f93baee4ce044a61b313407e00a75d91",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Box(children=(Dropdown(description='Choose parameter:', options=('2m_dewpoint_temperature', '2m…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "one_group = group.value\n",
    "parameters = widgets.Dropdown(options = one_group,description='Choose parameter:', style=style)\n",
    "\n",
    "Type = widgets.Dropdown(options=types, value = 'reanalysis', description='Product types:', style=style)\n",
    "\n",
    "times = widgets.BoundedIntText(\n",
    "    value=12,\n",
    "    min=0,\n",
    "    max=24,\n",
    "    step=1,\n",
    "    description='Hour:',\n",
    "    disabled=False\n",
    ")\n",
    "\n",
    "d = datetime.datetime(2017, 1, 1, 12)\n",
    "\n",
    "dates = widgets.DatePicker(\n",
    "    description='Pick a Date',\n",
    "    value=d,\n",
    "    disabled=False\n",
    ")\n",
    "\n",
    "items_auto = [parameters, Type, dates, times]\n",
    "    \n",
    "box_layout = Layout(display='flex',\n",
    "                    flex_flow='row',\n",
    "                    align_items='stretch',\n",
    "                    width='100%')\n",
    "box_auto = Box(children=items_auto, layout=box_layout)\n",
    "\n",
    "VBox([box_auto])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2018-10-08 17:41:48,183 INFO Sending request to https://cds.climate.copernicus.eu/api/v2/resources/reanalysis-era5-single-levels\n",
      "2018-10-08 17:41:48,219 INFO Request is completed\n",
      "2018-10-08 17:41:48,220 INFO Downloading http://136.156.132.110/cache-compute-0001/cache/data1/adaptor.mars.internal-1538987153.1609852-32006-9-c7f81a2d-9eed-4493-a68b-8f353281a2d9.grib to download.grib (3.1M)\n",
      "2018-10-08 17:41:48,377 INFO Download rate 19.7M/s\n"
     ]
    }
   ],
   "source": [
    "c = cdsapi.Client()\n",
    "\n",
    "a = c.retrieve(\n",
    "    'reanalysis-era5-single-levels',\n",
    "    {\n",
    "        'variable':[\n",
    "            parameters.value\n",
    "        ],\n",
    "            'grid': ['0.2', '0.2'],\n",
    "            'product_type': Type.value,\n",
    "            'year': dates.value.year,\n",
    "            'month': dates.value.month,\n",
    "            'day': dates.value.day,\n",
    "            'time': times.value,\n",
    "        'format':'grib'\n",
    "    },\n",
    "                \"download.grib\"\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "data =  magics.mgrib(grib_input_file_name  = 'download.grib')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "coast = magics.mcoast(map_grid =  \"on\",\n",
    "                      map_grid_colour  =  \"charcoal\",\n",
    "                      map_coastline_land_shade  =  'off',\n",
    "                      map_coastline  =  'on',\n",
    "                      map_coastline_land_shade_colour  =  'grey',\n",
    "                      map_coastline_colour =  'charcoal',\n",
    "            )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "contour = magics.mcont(contour_automatic_setting = 'ecmwf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 300176 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "magics.plot(data, contour)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "projection = magics.mmap(subpage_frame = \"off\",\n",
    "                    subpage_upper_right_longitude = 40.,\n",
    "                    subpage_upper_right_latitude = 75,\n",
    "                    subpage_map_projection = \"cylindrical\",\n",
    "                    subpage_lower_left_longitude = -25.,\n",
    "                    subpage_lower_left_latitude = 35\n",
    "                        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 134976 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "magics.plot(projection, data, contour, magics.mcoast())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "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.6.5"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {
     "105f2de5b9944e869c3c792be61aa640": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.2.0",
      "model_name": "DropdownModel",
      "state": {
       "_options_labels": [
        "2m_dewpoint_temperature",
        "2m_temperature",
        "ice_temperature_layer_1",
        "ice_temperature_layer_2",
        "ice_temperature_layer_3",
        "ice_temperature_layer_4",
        "maximum_2m_temperature_since_previous_post_processing",
        "mean_sea_level_pressure",
        "minimum_2m_temperature_since_previous_post_processing",
        "sea_surface_temperature",
        "skin_temperature",
        "surface_pressure"
       ],
       "description": "Choose parameter:",
       "index": 0,
       "layout": "IPY_MODEL_9571dd91a1c340b3be8cb9d94c90f8f0",
       "style": "IPY_MODEL_834588e4f2ee404c954157f1de6239f3"
      }
     },
     "11f1c07be9f14c41983e30bd80b5d0ea": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.0.0",
      "model_name": "LayoutModel",
      "state": {
       "align_items": "stretch",
       "display": "flex",
       "flex_flow": "row",
       "width": "100%"
      }
     },
     "14c7977924a349f0ace1190f75a39fa1": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.2.0",
      "model_name": "DatePickerModel",
      "state": {
       "description": "Pick a Date",
       "disabled": false,
       "layout": "IPY_MODEL_cf709a55a3a546c39b008fcf22794679",
       "style": "IPY_MODEL_e57ce872c47c4c63ae93d5c506602adc",
       "value": {
        "date": 1,
        "month": 0,
        "year": 2017
       }
      }
     },
     "2495324681c04a879604c37a104cf9b7": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.2.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "2ab0fd3d7493478286f1b27a70998d9f": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.0.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "302d480d4a9e4a12a85c36447543e578": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.2.0",
      "model_name": "BoundedIntTextModel",
      "state": {
       "description": "Hour:",
       "layout": "IPY_MODEL_f9b26800c0384d9fa8e1d0fadcf21a24",
       "max": 24,
       "style": "IPY_MODEL_2495324681c04a879604c37a104cf9b7",
       "value": 12
      }
     },
     "36f0fc74801741938eb23fe55e657edb": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.2.0",
      "model_name": "DropdownModel",
      "state": {
       "_options_labels": [
        "ensemble_mean",
        "ensemble_members",
        "ensemble_spread",
        "reanalysis"
       ],
       "description": "Product types:",
       "index": 3,
       "layout": "IPY_MODEL_c28f8e71cef14f17a04e4802806cd843",
       "style": "IPY_MODEL_cac4cfad67054c0eb1f5e3b64a9d6f98"
      }
     },
     "3c8f5228b22e413980b99d644766d1da": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.2.0",
      "model_name": "BoxModel",
      "state": {
       "children": [
        "IPY_MODEL_105f2de5b9944e869c3c792be61aa640",
        "IPY_MODEL_36f0fc74801741938eb23fe55e657edb",
        "IPY_MODEL_14c7977924a349f0ace1190f75a39fa1",
        "IPY_MODEL_302d480d4a9e4a12a85c36447543e578"
       ],
       "layout": "IPY_MODEL_11f1c07be9f14c41983e30bd80b5d0ea"
      }
     },
     "3f4cc36dbf36410dbda10ead09603339": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.2.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": "initial"
      }
     },
     "834588e4f2ee404c954157f1de6239f3": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.2.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": "initial"
      }
     },
     "9571dd91a1c340b3be8cb9d94c90f8f0": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.0.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "9e614bbf451444f585bad4fec99b7ccf": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.2.0",
      "model_name": "VBoxModel",
      "state": {
       "children": [
        "IPY_MODEL_3c8f5228b22e413980b99d644766d1da"
       ],
       "layout": "IPY_MODEL_2ab0fd3d7493478286f1b27a70998d9f"
      }
     },
     "c28f8e71cef14f17a04e4802806cd843": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.0.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "cac4cfad67054c0eb1f5e3b64a9d6f98": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.2.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": "initial"
      }
     },
     "cf709a55a3a546c39b008fcf22794679": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.0.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "d488943000c84fdd98790a60edc49e8c": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.0.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "e57ce872c47c4c63ae93d5c506602adc": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.2.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "ea673582f6e146beadd27232009bb244": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.2.0",
      "model_name": "DropdownModel",
      "state": {
       "_options_labels": [
        "Temperature and pressure",
        "Clouds",
        "Lakes",
        "Precipitation and rain",
        "Snow"
       ],
       "description": "Group:",
       "index": 0,
       "layout": "IPY_MODEL_d488943000c84fdd98790a60edc49e8c",
       "style": "IPY_MODEL_3f4cc36dbf36410dbda10ead09603339"
      }
     },
     "f9b26800c0384d9fa8e1d0fadcf21a24": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.0.0",
      "model_name": "LayoutModel",
      "state": {}
     }
    },
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
