{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exploring ERA5 dataset using CDS and Magics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## First import Magics, cdsapi and the other libraries\n",
    "At ECMWF make sure that Magics and cdsapi are loaded.  \n",
    "Documentation about **cdsapi** can be find [here](https://cds.climate.copernicus.eu/api-how-to.com \"CDS API how to\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "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": "markdown",
   "metadata": {},
   "source": [
    "`menus.py` contains the code got menus to choose parameter, type, date and time..  \n",
    "We're just gonna run it here so our nice widgets work, but the code is not important for this excersize."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2d07b2095a3a49aa9fbdefa9a6854510",
       "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": "55baa1d7f8fd49aabfc7f012c51d8f92",
       "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": [
    "%run -i 'menus.py'\n",
    "\n",
    "display(group)\n",
    "display(VBox([box_auto]))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we have code for CDS API request.  \n",
    "It uses parameters we have chosen before, so we don't have to change the code and so we reduce the posibility of an error.  \n",
    "At the moment this request only works for surface fields, and there is separate Notebook for pressure levels fields. In future it might be merged in one Notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2018-10-09 14:30:18,321 INFO Sending request to https://cds.climate.copernicus.eu/api/v2/resources/reanalysis-era5-single-levels\n",
      "2018-10-09 14:30:18,347 INFO Request is queued\n",
      "2018-10-09 14:30:19,355 INFO Request is running\n",
      "2018-10-09 14:30:20,863 INFO Request is completed\n",
      "2018-10-09 14:30:20,863 INFO Downloading http://136.156.132.110/cache-compute-0003/cache/data1/adaptor.mars.internal-1539091819.7059243-20582-12-71f28140-9992-4e41-a6ea-acb5648b33a1.grib to download.grib (3.1M)\n",
      "2018-10-09 14:30:20,936 INFO Download rate 43.2M/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": "markdown",
   "metadata": {},
   "source": [
    "### Load grib data:\n",
    "More about loading grib files in Magics can be found [here](https://confluence.ecmwf.int/display/MAGP/Grib+Input \"GRIB input\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "data =  magics.mgrib(grib_input_file_name  = 'download.grib')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load predefined ECMWF contour setting:\n",
    "Setting the parameter **contour_automatic_setting** to **'ecmwf'** will give us style developed at ECMWF for this parameter.  \n",
    "\n",
    "More about contour settings in Magics can be found [here](https://confluence.ecmwf.int/display/MAGP/Contouring \"Contouring\")  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 76592 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "contour = magics.mcont(contour_automatic_setting = 'ecmwf')\n",
    "\n",
    "magics.plot(data, contour)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some ERA5 parameters (that have representation in ecCharts) have more than one built in predefined styles. Run the next cell and choose a style."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "55b49f7ca7e14efaba0d57598a0f7748",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Dropdown(description='Choose a style:', options=('sh_blured_f05t300lst', 'sh_blured_f1t100lst', 'sh_blured_f1t…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "styles = magics.wmsstyles(data, )\n",
    "all_styles = []\n",
    "for i in styles['styles']:\n",
    "    all_styles.append(i[\"name\"])\n",
    "\n",
    "One_style = widgets.Dropdown(options=all_styles, value = all_styles[0], description='Choose a style:', style=style)\n",
    "display(One_style)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To use this new feature we need to set the parameter **contour_automatic_setting** to **'style_name'**, and add a new parameter **contour_style_name** with the chosen style. In this example it takes value choosen by the previous menu."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 80252 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "contour = magics.mcont(contour_automatic_setting = 'style_name',\n",
    "                      contour_style_name = One_style.value)\n",
    "\n",
    "magics.plot(data, contour)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Adding coastlines, gridlines and default title\n",
    "Unfortunately, previous image lacks coastlines, so in order to continue lets add default coastline definition.  \n",
    "We can also add the default title. It is useful as it's showing us basic info about our grib field without having to use additional software or write code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 176760 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "magics.plot(data, contour, magics.mcoast(), magics.mtext())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also define our own coastline style by adding boundaries, rivers, setting coastline resolution, grid frequency and many other things.  \n",
    "All **mcoast** parameters, as well as some examples can be found [here](https://confluence.ecmwf.int/display/MAGP/Coastlines \"Coastlines\")   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "coast = magics.mcoast(map_grid =  \"on\",\n",
    "                      map_grid_colour  =  \"charcoal\",\n",
    "                      map_coastline  =  'on',\n",
    "                      map_coastline_colour =  'navy',\n",
    "                      map_boundaries = 'on',\n",
    "                      map_boundaries_colour = 'navy',\n",
    "                      map_coastline_resolution = 'high'\n",
    "            )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Magics also has a number of predefined areas.  \n",
    "To use them, set **subpage_map_library_area** to **\"on\"** and then give **subpage_map_area_name** the value of chosen area.    \n",
    "\n",
    "Run the next cell and choose the area from dropdown menu, and it will be set to subpage_map_area_name.  \n",
    "\n",
    "Then run the cell below to plot data in your chosen area and projection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4c804b2c805f4f1bb11a00c130828709",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Dropdown(description='Predefined areas:', index=15, options=('north_pole', 'eurasia', 'global', 'south_west_eu…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "areas = magics.predefined_areas()\n",
    "Area = widgets.Dropdown(options=areas, value = 'europe', description='Predefined areas:', style=style)\n",
    "display(Area)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 167524 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "projection = magics.mmap(\n",
    "    subpage_map_library_area = \"on\",\n",
    "    subpage_map_area_name = Area.value\n",
    ")\n",
    "\n",
    "magics.plot(projection, data, contour, coast)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we don't like any of the areas, we can define our own projection.  \n",
    "All **mmap** parameters, as well as some examples can be found [here](https://confluence.ecmwf.int/display/MAGP/Subpage+-+Projection \"Projections\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 230680 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "projection = magics.mmap(subpage_lower_left_longitude = -37.27,\n",
    "     subpage_upper_right_longitude = 65.00,\n",
    "     subpage_upper_right_latitude = 51.28,\n",
    "     subpage_map_projection = \"polar_stereographic\",\n",
    "     subpage_lower_left_latitude = 21.51)\n",
    "\n",
    "magics.plot(projection, data, contour, coast)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### To conclude, to inspect grib data using Magics this is all the code you need:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 66680 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import Magics.macro as magics\n",
    "\n",
    "data =  magics.mgrib(grib_input_file_name  = 'download.grib', grib_field_position = 1)\n",
    "\n",
    "contour = magics.mcont(contour_automatic_setting = 'ecmwf')\n",
    "\n",
    "projection = magics.mmap(\n",
    "    subpage_map_library_area = \"on\",\n",
    "    subpage_map_area_name = \"south_west_europe\"\n",
    ")\n",
    "\n",
    "magics.plot(projection, data, contour, magics.mcoast(), magics.mtext())"
   ]
  }
 ],
 "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
