{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ploting a NetCDF through Xarray and Magics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1) Read the NetCDF in xarray "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<xarray.Dataset>\n",
       "Dimensions:    (latitude: 91, longitude: 180, time: 1)\n",
       "Coordinates:\n",
       "  * longitude  (longitude) float32 0.0 2.0 4.0 6.0 ... 352.0 354.0 356.0 358.0\n",
       "  * latitude   (latitude) float32 90.0 88.0 86.0 84.0 ... -86.0 -88.0 -90.0\n",
       "  * time       (time) datetime64[ns] 2018-09-24\n",
       "Data variables:\n",
       "    t2m        (time, latitude, longitude) float32 dask.array<shape=(1, 91, 180), chunksize=(1, 91, 180)>\n",
       "Attributes:\n",
       "    Conventions:  CF-1.6\n",
       "    history:      2018-10-04 16:23:46 GMT by grib_to_netcdf-2.7.3: grib_to_ne..."
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import xarray as xr\n",
    "\n",
    "ds = xr.open_mfdataset('2t.nc')\n",
    "ds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Data variables:\n",
       "    t2m      (time, latitude, longitude) float32 dask.array<shape=(1, 91, 180), chunksize=(1, 91, 180)>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ds.data_vars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[u'latitude', u'longitude', u'time']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(ds.dims)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OrderedDict([('units', 'K'), ('long_name', '2 metre temperature')])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ds.t2m.attrs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['2018-09-24T00:00:00.000000000'], dtype='datetime64[ns]')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ds.time.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "90.0\n",
      "-2.0\n",
      "0.0\n",
      "2.0\n"
     ]
    }
   ],
   "source": [
    "print(ds.latitude.values[0])\n",
    "print(ds.latitude.values[1] - ds.latitude.values[0])\n",
    "print(ds.longitude.values[0])\n",
    "print(ds.longitude.values[1] - ds.longitude.values[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(ds.t2m.values[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2) Use Magics to plot the content of the Xarray data set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 211512 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import Magics.macro as magics\n",
    "import json\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "def tomagics(dataset, var):\n",
    "    values = dataset[var].values.astype(np.float64)\n",
    "    lat = dataset.latitude.values.astype(np.float64)\n",
    "    lon = dataset.longitude.values.astype(np.float64)\n",
    "\n",
    "    data = magics.minput(\n",
    "        input_field                   = values[0],\n",
    "        input_field_initial_latitude  = lat[0],\n",
    "        input_field_latitude_step     = lat[1] - lat[0],\n",
    "        input_field_initial_longitude = lon[0],\n",
    "        input_field_longitude_step    = lon[1]-lon[0],\n",
    "        input_mars_metadata           = json.dumps(dataset[var].attrs)\n",
    "    )\n",
    "    return data\n",
    "\n",
    "contour = magics.mcont( contour_automatic_setting ='ecmwf')\n",
    "\n",
    "magics.plot(tomagics(ds,\"t2m\"), contour, magics.mcoast())"
   ]
  }
 ],
 "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
