{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to read and write observational datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nes\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import geopandas as gpd\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Non-GHOST"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1. Open dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Original path: /esarchive/obs/nilu/ebas/daily/pm10/pm10_201507.nc\n",
    "# Points grid from EBAS network (non-GHOST)\n",
    "path = '/gpfs/projects/bsc32/models/NES_tutorial_data/pm10_201507.nc'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<nes.nc_projections.points_nes.PointsNes at 0x7f6ea0a79310>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy = nes.open_netcdf(path=path, info=True, parallel_method='X')\n",
    "nessy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Read coordinates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[datetime.datetime(2015, 7, 1, 0, 0),\n",
       " datetime.datetime(2015, 7, 1, 1, 0),\n",
       " datetime.datetime(2015, 7, 1, 2, 0),\n",
       " datetime.datetime(2015, 7, 1, 3, 0),\n",
       " datetime.datetime(2015, 7, 1, 4, 0),\n",
       " datetime.datetime(2015, 7, 1, 5, 0),\n",
       " datetime.datetime(2015, 7, 1, 6, 0),\n",
       " datetime.datetime(2015, 7, 1, 7, 0),\n",
       " datetime.datetime(2015, 7, 1, 8, 0),\n",
       " datetime.datetime(2015, 7, 1, 9, 0)]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.time[0:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': array([0]), 'units': ''}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.lev"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "        17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n",
       "        34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,\n",
       "        51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,\n",
       "        68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83])}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.station"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Variables are built with `station` and `time`as main dimensions, but the coordinates `lat` and `lon` remain accessible"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': masked_array(data=[46.81305694580078, 47.479766845703125,\n",
       "                    47.049468994140625, 47.06740951538086,\n",
       "                    35.03916549682617, 56.35444259643555,\n",
       "                    56.711666107177734, 55.69410705566406,\n",
       "                    59.49444580078125, 58.3761100769043, 39.54694366455078,\n",
       "                    42.72055435180664, 39.87527847290039,\n",
       "                    37.237220764160156, 43.43916702270508,\n",
       "                    41.274166107177734, 42.31916809082031,\n",
       "                    38.4727783203125, 39.08277893066406,\n",
       "                    41.238887786865234, 41.393890380859375,\n",
       "                    42.63472366333008, 37.051944732666016,\n",
       "                    51.57107925415039, 55.79216003417969,\n",
       "                    46.96666717529297, 51.93333435058594, 64.0999984741211,\n",
       "                    45.81444549560547, 58.43299865722656,\n",
       "                    65.83333587646484, 62.78333282470703,\n",
       "                    60.36666488647461, 51.81444549560547,\n",
       "                    50.73638916015625, 54.75416564941406,\n",
       "                    54.125274658203125, 43.40399932861328,\n",
       "                    63.8488883972168, 56.02805709838867,\n",
       "                    58.801109313964844, 57.393611907958984,\n",
       "                    45.5613899230957, 48.942222595214844,\n",
       "                    49.04222106933594, 78.90666961669922,\n",
       "                    52.802223205566406, 52.08333206176758,\n",
       "                    53.66211700439453, 27.289989471435547,\n",
       "                    51.54111099243164, 47.8386116027832, 40.38444519042969,\n",
       "                    47.766666412353516, 51.97444534301758,\n",
       "                    49.573394775390625, 54.43333435058594,\n",
       "                    56.16194534301758, 59.77916717529297,\n",
       "                    40.33580017089844, 49.733333587646484,\n",
       "                    54.900001525878906, 52.116668701171875,\n",
       "                    43.150001525878906, 35.31666564941406,\n",
       "                    55.3761100769043, 47.91472244262695, 53.16666793823242,\n",
       "                    42.099998474121094, 60.53002166748047,\n",
       "                    51.5301399230957, 52.86861038208008, 55.375,\n",
       "                    54.92555618286133, 50.650001525878906,\n",
       "                    49.06666564941406, 31.34273910522461, 68.0,\n",
       "                    46.54750061035156, 52.29999923706055,\n",
       "                    46.67777633666992, 52.17807388305664,\n",
       "                    53.33388900756836, 38.87630081176758],\n",
       "              mask=[False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False],\n",
       "        fill_value=np.float64(1e+20),\n",
       "             dtype=float32),\n",
       " 'dimensions': ('station',),\n",
       " 'dtype': dtype('float32'),\n",
       " 'units': 'degrees_north',\n",
       " 'long_name': 'latitude',\n",
       " 'standard_name': 'latitude',\n",
       " 'axis': 'Y'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.lat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': masked_array(data=[6.944468975067139, 8.904696464538574,\n",
       "                    6.979203224182129, 8.463338851928711,\n",
       "                    33.05805969238281, 9.603058815002441,\n",
       "                    11.533614158630371, 12.088668823242188,\n",
       "                    25.93056869506836, 21.845012664794922,\n",
       "                    -4.350555896759033, -8.92361068725586,\n",
       "                    4.316389083862305, -3.5341670513153076,\n",
       "                    -4.849999904632568, -3.1424999237060547,\n",
       "                    3.31583309173584, -6.923611164093018,\n",
       "                    -1.1011110544204712, -5.897500038146973,\n",
       "                    0.7347220182418823, -7.704721927642822,\n",
       "                    -6.5552778244018555, -1.3252860307693481,\n",
       "                    -3.2428998947143555, 19.549999237060547,\n",
       "                    -10.233333587646484, -21.016944885253906,\n",
       "                    8.637225151062012, 8.269000053405762,\n",
       "                    13.916666984558105, 8.883333206176758,\n",
       "                    11.066666603088379, 21.971389770507812,\n",
       "                    15.73972225189209, 17.53444480895996,\n",
       "                    22.038055419921875, 21.94700050354004,\n",
       "                    15.335577011108398, 13.149457931518555,\n",
       "                    17.3819580078125, 11.914179801940918,\n",
       "                    14.862789154052734, 19.59223175048828,\n",
       "                    22.25973129272461, 11.888333320617676,\n",
       "                    10.759444236755371, 6.566667079925537,\n",
       "                    17.934017181396484, 33.74988555908203,\n",
       "                    5.853610992431641, 14.441389083862305,\n",
       "                    44.260581970214844, 16.766666412353516,\n",
       "                    4.923611164093018, 15.080278396606445,\n",
       "                    12.733332633972168, 21.17305564880371,\n",
       "                    21.377222061157227, 18.124500274658203,\n",
       "                    16.049999237060547, 37.79999923706055,\n",
       "                    5.199999809265137, 19.133333206176758,\n",
       "                    25.666667938232422, 21.030555725097656,\n",
       "                    7.908610820770264, 13.033332824707031,\n",
       "                    12.633333206176758, 27.667539596557617,\n",
       "                    12.933859825134277, -6.924722194671631,\n",
       "                    -7.342778205871582, 8.309721946716309,\n",
       "                    10.766667366027832, 13.600000381469727,\n",
       "                    27.217775344848633, 24.23722267150879,\n",
       "                    7.985000133514404, 4.5, 12.972222328186035,\n",
       "                    -6.364523887634277, 6.277222156524658,\n",
       "                    16.232200622558594],\n",
       "              mask=[False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False, False, False, False, False,\n",
       "                    False, False, False, False],\n",
       "        fill_value=np.float64(1e+20),\n",
       "             dtype=float32),\n",
       " 'dimensions': ('station',),\n",
       " 'dtype': dtype('float32'),\n",
       " 'units': 'degrees_east',\n",
       " 'long_name': 'longitude',\n",
       " 'standard_name': 'longitude',\n",
       " 'axis': 'X'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.lon"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Read variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we want to load all variables, we don't use the function `keep_vars`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank 000: Loading station_start_date var (1/17)\n",
      "Rank 000: Loaded station_start_date var ((84,))\n",
      "Rank 000: Loading station_zone var (2/17)\n",
      "Rank 000: Loaded station_zone var ((84,))\n",
      "Rank 000: Loading street_type var (3/17)\n",
      "Rank 000: Loaded street_type var ((84,))\n",
      "Rank 000: Loading country_code var (4/17)\n",
      "Rank 000: Loaded country_code var ((84,))\n",
      "Rank 000: Loading ccaa var (5/17)\n",
      "Rank 000: Loaded ccaa var ((84,))\n",
      "Rank 000: Loading station_name var (6/17)\n",
      "Rank 000: Loaded station_name var ((84,))\n",
      "Rank 000: Loading station_area var (7/17)\n",
      "Rank 000: Loaded station_area var ((84,))\n",
      "Rank 000: Loading city var (8/17)\n",
      "Rank 000: Loaded city var ((84,))\n",
      "Rank 000: Loading pm10 var (9/17)\n",
      "Rank 000: Loaded pm10 var ((31, 84))\n",
      "Rank 000: Loading station_emep var (10/17)\n",
      "Rank 000: Loaded station_emep var ((84,))\n",
      "Rank 000: Loading station_type var (11/17)\n",
      "Rank 000: Loaded station_type var ((84,))\n",
      "Rank 000: Loading country var (12/17)\n",
      "Rank 000: Loaded country var ((84,))\n",
      "Rank 000: Loading altitude var (13/17)\n",
      "Rank 000: Loaded altitude var ((84,))\n",
      "Rank 000: Loading station_code var (14/17)\n",
      "Rank 000: Loaded station_code var ((84,))\n",
      "Rank 000: Loading station_end_date var (15/17)\n",
      "Rank 000: Loaded station_end_date var ((84,))\n",
      "Rank 000: Loading station_rural_back var (16/17)\n",
      "Rank 000: Loaded station_rural_back var ((84,))\n",
      "Rank 000: Loading station_ozone_classification var (17/17)\n",
      "Rank 000: Loaded station_ozone_classification var ((84,))\n"
     ]
    }
   ],
   "source": [
    "nessy.load()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As `variables` is a dictionary, we can access each variable using square brackets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': array([[17.9 , 15.4 , 16.2 , ...,   nan, 14.83,   nan],\n",
       "        [22.9 , 16.8 , 20.5 , ...,   nan, 24.8 ,   nan],\n",
       "        [24.1 , 22.8 , 20.2 , ...,   nan, 13.92,   nan],\n",
       "        ...,\n",
       "        [ 6.3 ,  7.2 ,  4.5 , ...,   nan,  8.32,  9.3 ],\n",
       "        [ 7.6 ,  8.9 ,  8.2 , ...,   nan, 13.02,  9.3 ],\n",
       "        [11.  ,  9.8 ,  8.8 , ...,   nan, 14.14, 12.7 ]], dtype=float32),\n",
       " 'dimensions': ('time', 'station'),\n",
       " 'dtype': dtype('float32'),\n",
       " 'units': 'µg/m3',\n",
       " 'long_name': 'pm10_mass pm10'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.variables['pm10']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2. Plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here it makes no sense to use `pcolormesh` to plot the data, we need to generate a shapefile instead"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>geometry</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FID</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>POINT (6.94447 46.81306)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>POINT (8.9047 47.47977)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>POINT (6.9792 47.04947)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>POINT (8.46334 47.06741)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>POINT (33.05806 35.03917)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>POINT (4.5 52.3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>80</th>\n",
       "      <td>POINT (12.97222 46.67778)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>81</th>\n",
       "      <td>POINT (-6.36452 52.17807)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82</th>\n",
       "      <td>POINT (6.27722 53.33389)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>83</th>\n",
       "      <td>POINT (16.2322 38.8763)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>84 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                      geometry\n",
       "FID                           \n",
       "0     POINT (6.94447 46.81306)\n",
       "1      POINT (8.9047 47.47977)\n",
       "2      POINT (6.9792 47.04947)\n",
       "3     POINT (8.46334 47.06741)\n",
       "4    POINT (33.05806 35.03917)\n",
       "..                         ...\n",
       "79            POINT (4.5 52.3)\n",
       "80   POINT (12.97222 46.67778)\n",
       "81   POINT (-6.36452 52.17807)\n",
       "82    POINT (6.27722 53.33389)\n",
       "83     POINT (16.2322 38.8763)\n",
       "\n",
       "[84 rows x 1 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.create_shapefile()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At this point, the shapefile only contains the geometries and we need to add data to it. In this case we are adding PM10 from all stations for the first timestep `[0, :]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>geometry</th>\n",
       "      <th>pm10</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>POINT (6.94447 46.81306)</td>\n",
       "      <td>17.900000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>POINT (8.9047 47.47977)</td>\n",
       "      <td>15.400000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>POINT (6.9792 47.04947)</td>\n",
       "      <td>16.200001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>POINT (8.46334 47.06741)</td>\n",
       "      <td>15.200000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>POINT (33.05806 35.03917)</td>\n",
       "      <td>17.700001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>POINT (4.5 52.3)</td>\n",
       "      <td>21.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>80</th>\n",
       "      <td>POINT (12.97222 46.67778)</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>81</th>\n",
       "      <td>POINT (-6.36452 52.17807)</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82</th>\n",
       "      <td>POINT (6.27722 53.33389)</td>\n",
       "      <td>14.830000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>83</th>\n",
       "      <td>POINT (16.2322 38.8763)</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>84 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                      geometry       pm10\n",
       "FID                                      \n",
       "0     POINT (6.94447 46.81306)  17.900000\n",
       "1      POINT (8.9047 47.47977)  15.400000\n",
       "2      POINT (6.9792 47.04947)  16.200001\n",
       "3     POINT (8.46334 47.06741)  15.200000\n",
       "4    POINT (33.05806 35.03917)  17.700001\n",
       "..                         ...        ...\n",
       "79            POINT (4.5 52.3)  21.500000\n",
       "80   POINT (12.97222 46.67778)        NaN\n",
       "81   POINT (-6.36452 52.17807)        NaN\n",
       "82    POINT (6.27722 53.33389)  14.830000\n",
       "83     POINT (16.2322 38.8763)        NaN\n",
       "\n",
       "[84 rows x 2 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.shapefile['pm10'] = nessy.variables['pm10']['data'][0, :].ravel()\n",
    "nessy.shapefile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 183740 characters]",
      "text/plain": [
       "<Figure size 1900x700 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(1, figsize=(19, 7))\n",
    "ax.set_xlim([-25, 45])\n",
    "ax.set_ylim([30, 70])\n",
    "countries = gpd.read_file('/gpfs/projects/bsc32/models/NES_tutorial_data/gadm_country_mask/gadm_country_ISO3166.shp')\n",
    "countries.plot(ax=ax, facecolor='none', edgecolor='black', linewidth=0.3)\n",
    "nessy.shapefile.plot(ax=ax, column='pm10', cmap='jet', edgecolor='black', linewidth=0.5, legend=True,\n",
    "                     legend_kwds={'label': 'PM10 (µg/m3)'})\n",
    "ax.margins(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3. Write dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Write NetCDF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable station_start_date. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable station_zone. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable street_type. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable country_code. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable ccaa. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable station_name. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable station_area. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable city. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable station_emep. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable station_type. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable country. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable station_code. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable station_end_date. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable station_rural_back. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/points_nes.py:368: UserWarning: WARNING!!! Different data types for variable station_ozone_classification. Input dtype=<class 'str'>. Data dtype=object.\n",
      "  warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank 000: Creating points_non_ghost.nc\n",
      "Rank 000: NetCDF ready to write\n",
      "Rank 000: Dimensions done\n",
      "Rank 000: Writing station_start_date var (1/17)\n",
      "Rank 000: Var station_start_date created (1/17)\n",
      "Rank 000: Filling station_start_date)\n",
      "Rank 000: Var station_start_date data (1/17)\n",
      "Rank 000: Var station_start_date completed (1/17)\n",
      "Rank 000: Writing station_zone var (2/17)\n",
      "Rank 000: Var station_zone created (2/17)\n",
      "Rank 000: Filling station_zone)\n",
      "Rank 000: Var station_zone data (2/17)\n",
      "Rank 000: Var station_zone completed (2/17)\n",
      "Rank 000: Writing street_type var (3/17)\n",
      "Rank 000: Var street_type created (3/17)\n",
      "Rank 000: Filling street_type)\n",
      "Rank 000: Var street_type data (3/17)\n",
      "Rank 000: Var street_type completed (3/17)\n",
      "Rank 000: Writing country_code var (4/17)\n",
      "Rank 000: Var country_code created (4/17)\n",
      "Rank 000: Filling country_code)\n",
      "Rank 000: Var country_code data (4/17)\n",
      "Rank 000: Var country_code completed (4/17)\n",
      "Rank 000: Writing ccaa var (5/17)\n",
      "Rank 000: Var ccaa created (5/17)\n",
      "Rank 000: Filling ccaa)\n",
      "Rank 000: Var ccaa data (5/17)\n",
      "Rank 000: Var ccaa completed (5/17)\n",
      "Rank 000: Writing station_name var (6/17)\n",
      "Rank 000: Var station_name created (6/17)\n",
      "Rank 000: Filling station_name)\n",
      "Rank 000: Var station_name data (6/17)\n",
      "Rank 000: Var station_name completed (6/17)\n",
      "Rank 000: Writing station_area var (7/17)\n",
      "Rank 000: Var station_area created (7/17)\n",
      "Rank 000: Filling station_area)\n",
      "Rank 000: Var station_area data (7/17)\n",
      "Rank 000: Var station_area completed (7/17)\n",
      "Rank 000: Writing city var (8/17)\n",
      "Rank 000: Var city created (8/17)\n",
      "Rank 000: Filling city)\n",
      "Rank 000: Var city data (8/17)\n",
      "Rank 000: Var city completed (8/17)\n",
      "Rank 000: Writing pm10 var (9/17)\n",
      "Rank 000: Var pm10 created (9/17)\n",
      "Rank 000: Filling pm10)\n",
      "Rank 000: Var pm10 data (9/17)\n",
      "Rank 000: Var pm10 completed (9/17)\n",
      "Rank 000: Writing station_emep var (10/17)\n",
      "Rank 000: Var station_emep created (10/17)\n",
      "Rank 000: Filling station_emep)\n",
      "Rank 000: Var station_emep data (10/17)\n",
      "Rank 000: Var station_emep completed (10/17)\n",
      "Rank 000: Writing station_type var (11/17)\n",
      "Rank 000: Var station_type created (11/17)\n",
      "Rank 000: Filling station_type)\n",
      "Rank 000: Var station_type data (11/17)\n",
      "Rank 000: Var station_type completed (11/17)\n",
      "Rank 000: Writing country var (12/17)\n",
      "Rank 000: Var country created (12/17)\n",
      "Rank 000: Filling country)\n",
      "Rank 000: Var country data (12/17)\n",
      "Rank 000: Var country completed (12/17)\n",
      "Rank 000: Writing altitude var (13/17)\n",
      "Rank 000: Var altitude created (13/17)\n",
      "Rank 000: Filling altitude)\n",
      "Rank 000: Var altitude data (13/17)\n",
      "Rank 000: Var altitude completed (13/17)\n",
      "Rank 000: Writing station_code var (14/17)\n",
      "Rank 000: Var station_code created (14/17)\n",
      "Rank 000: Filling station_code)\n",
      "Rank 000: Var station_code data (14/17)\n",
      "Rank 000: Var station_code completed (14/17)\n",
      "Rank 000: Writing station_end_date var (15/17)\n",
      "Rank 000: Var station_end_date created (15/17)\n",
      "Rank 000: Filling station_end_date)\n",
      "Rank 000: Var station_end_date data (15/17)\n",
      "Rank 000: Var station_end_date completed (15/17)\n",
      "Rank 000: Writing station_rural_back var (16/17)\n",
      "Rank 000: Var station_rural_back created (16/17)\n",
      "Rank 000: Filling station_rural_back)\n",
      "Rank 000: Var station_rural_back data (16/17)\n",
      "Rank 000: Var station_rural_back completed (16/17)\n",
      "Rank 000: Writing station_ozone_classification var (17/17)\n",
      "Rank 000: Var station_ozone_classification created (17/17)\n",
      "Rank 000: Filling station_ozone_classification)\n",
      "Rank 000: Var station_ozone_classification data (17/17)\n",
      "Rank 000: Var station_ozone_classification completed (17/17)\n"
     ]
    }
   ],
   "source": [
    "nessy.to_netcdf('points_non_ghost.nc', info=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Write shapefile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/default_nes.py:3805: UserWarning: No vertical level has been specified. The first one will be selected.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/default_nes.py:3816: UserWarning: No time has been specified. The first one will be selected.\n",
      "  warn(msg)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/nes/nc_projections/default_nes.py:3756: UserWarning: Column names longer than 10 characters will be truncated when saved to ESRI Shapefile.\n",
      "  self.shapefile.to_file(path)\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/pyogrio/raw.py:709: RuntimeWarning: Normalized/laundered field name: 'station_start_date' to 'station_st'\n",
      "  ogr_write(\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/pyogrio/raw.py:709: RuntimeWarning: Normalized/laundered field name: 'station_zone' to 'station_zo'\n",
      "  ogr_write(\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/pyogrio/raw.py:709: RuntimeWarning: Normalized/laundered field name: 'street_type' to 'street_typ'\n",
      "  ogr_write(\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/pyogrio/raw.py:709: RuntimeWarning: Normalized/laundered field name: 'country_code' to 'country_co'\n",
      "  ogr_write(\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/pyogrio/raw.py:709: RuntimeWarning: Normalized/laundered field name: 'station_name' to 'station_na'\n",
      "  ogr_write(\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/pyogrio/raw.py:709: RuntimeWarning: Normalized/laundered field name: 'station_area' to 'station_ar'\n",
      "  ogr_write(\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/pyogrio/raw.py:709: RuntimeWarning: Normalized/laundered field name: 'station_emep' to 'station_em'\n",
      "  ogr_write(\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/pyogrio/raw.py:709: RuntimeWarning: Normalized/laundered field name: 'station_type' to 'station_ty'\n",
      "  ogr_write(\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/pyogrio/raw.py:709: RuntimeWarning: Normalized/laundered field name: 'station_code' to 'station_co'\n",
      "  ogr_write(\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/pyogrio/raw.py:709: RuntimeWarning: Normalized/laundered field name: 'station_end_date' to 'station_en'\n",
      "  ogr_write(\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/pyogrio/raw.py:709: RuntimeWarning: Normalized/laundered field name: 'station_rural_back' to 'station_ru'\n",
      "  ogr_write(\n",
      "/home/bsc/bsc032781/.local/lib/python3.9/site-packages/pyogrio/raw.py:709: RuntimeWarning: Normalized/laundered field name: 'station_ozone_classification' to 'station_oz'\n",
      "  ogr_write(\n"
     ]
    }
   ],
   "source": [
    "nessy.to_shapefile('points_non_ghost_shp')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. GHOST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Points grid from EBAS network (GHOST)\n",
    "path = '/gpfs/projects/bsc32/AC_cache/obs/ghost/EBAS/1.4/hourly/sconcno2/sconcno2_202003.nc'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1. Read dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<nes.nc_projections.points_nes_ghost.PointsNesGHOST at 0x7f6e90a1b970>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy_ghost = nes.open_netcdf(path=path, info=True, parallel_method='X')\n",
    "nessy_ghost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Read coordinates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[datetime.datetime(2020, 3, 1, 0, 0),\n",
       " datetime.datetime(2020, 3, 1, 1, 0),\n",
       " datetime.datetime(2020, 3, 1, 2, 0),\n",
       " datetime.datetime(2020, 3, 1, 3, 0),\n",
       " datetime.datetime(2020, 3, 1, 4, 0),\n",
       " datetime.datetime(2020, 3, 1, 5, 0),\n",
       " datetime.datetime(2020, 3, 1, 6, 0),\n",
       " datetime.datetime(2020, 3, 1, 7, 0),\n",
       " datetime.datetime(2020, 3, 1, 8, 0),\n",
       " datetime.datetime(2020, 3, 1, 9, 0)]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy_ghost.time[0:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': array([0]), 'units': ''}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy_ghost.lev"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy_ghost.station"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Read variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank 000: Loading country var (1/4)\n",
      "Rank 000: Loaded country var ((12,))\n",
      "Rank 000: Loading land_use var (2/4)\n",
      "Rank 000: Loaded land_use var ((12,))\n",
      "Rank 000: Loading sconcno2 var (3/4)\n",
      "Rank 000: Loaded sconcno2 var ((12, 744))\n",
      "Rank 000: Loading station_name var (4/4)\n",
      "Rank 000: Loaded station_name var ((12,))\n"
     ]
    }
   ],
   "source": [
    "nessy_ghost.keep_vars(['land_use', 'sconcno2', 'station_name', 'country'])\n",
    "nessy_ghost.load()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the variables in GHOST datasets have the dimensions in inverse order as in `(station, time)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': array([[0.29195723, 0.27478325, 0.27821803, ...,        nan,        nan,\n",
       "                nan],\n",
       "        [0.95658916, 0.90334994, 1.6418298 , ..., 4.6730328 , 4.1560965 ,\n",
       "         4.001531  ],\n",
       "        [1.4703113 , 0.97663015, 1.9103314 , ..., 2.2537618 ,        nan,\n",
       "         2.0820467 ],\n",
       "        ...,\n",
       "        [2.52314   , 2.9062793 , 2.1786363 , ..., 0.34128392, 0.18030094,\n",
       "         0.11054165],\n",
       "        [9.422871  , 8.553562  , 7.813041  , ..., 6.514445  , 6.514445  ,\n",
       "         6.9008036 ],\n",
       "        [0.25971922, 0.21357076, 0.1888867 , ..., 0.33699107, 0.30801412,\n",
       "                nan]], dtype=float32),\n",
       " 'dimensions': ('station', 'time'),\n",
       " 'dtype': dtype('float32'),\n",
       " 'standard_name': 'nitrogen dioxide',\n",
       " 'long_name': 'nitrogen dioxide',\n",
       " 'units': 'nmol mol-1',\n",
       " 'description': 'Measured value of surface nitrogen dioxide for the stated temporal resolution.'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy_ghost.variables['sconcno2']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2. Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>geometry</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FID</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>POINT (7.985 46.5475)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>POINT (8.17543 47.18961)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>POINT (15.08028 49.57339)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>POINT (8.30972 54.92556)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>POINT (10.75944 52.80222)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>POINT (7.90861 47.91472)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>POINT (13.03333 53.16667)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>POINT (10.76667 50.65)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>POINT (12.7249 54.4368)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>POINT (24.28333 61.85)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>POINT (8.63333 45.8)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>POINT (10.7 44.18333)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      geometry\n",
       "FID                           \n",
       "0        POINT (7.985 46.5475)\n",
       "1     POINT (8.17543 47.18961)\n",
       "2    POINT (15.08028 49.57339)\n",
       "3     POINT (8.30972 54.92556)\n",
       "4    POINT (10.75944 52.80222)\n",
       "5     POINT (7.90861 47.91472)\n",
       "6    POINT (13.03333 53.16667)\n",
       "7       POINT (10.76667 50.65)\n",
       "8      POINT (12.7249 54.4368)\n",
       "9       POINT (24.28333 61.85)\n",
       "10        POINT (8.63333 45.8)\n",
       "11       POINT (10.7 44.18333)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy_ghost.create_shapefile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>geometry</th>\n",
       "      <th>sconcno2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>POINT (7.985 46.5475)</td>\n",
       "      <td>0.291957</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>POINT (8.17543 47.18961)</td>\n",
       "      <td>0.956589</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>POINT (15.08028 49.57339)</td>\n",
       "      <td>1.470311</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>POINT (8.30972 54.92556)</td>\n",
       "      <td>1.122448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>POINT (10.75944 52.80222)</td>\n",
       "      <td>1.700517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>POINT (7.90861 47.91472)</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>POINT (13.03333 53.16667)</td>\n",
       "      <td>1.223471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>POINT (10.76667 50.65)</td>\n",
       "      <td>1.021437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>POINT (12.7249 54.4368)</td>\n",
       "      <td>1.650011</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>POINT (24.28333 61.85)</td>\n",
       "      <td>2.523140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>POINT (8.63333 45.8)</td>\n",
       "      <td>9.422871</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>POINT (10.7 44.18333)</td>\n",
       "      <td>0.259719</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      geometry  sconcno2\n",
       "FID                                     \n",
       "0        POINT (7.985 46.5475)  0.291957\n",
       "1     POINT (8.17543 47.18961)  0.956589\n",
       "2    POINT (15.08028 49.57339)  1.470311\n",
       "3     POINT (8.30972 54.92556)  1.122448\n",
       "4    POINT (10.75944 52.80222)  1.700517\n",
       "5     POINT (7.90861 47.91472)       NaN\n",
       "6    POINT (13.03333 53.16667)  1.223471\n",
       "7       POINT (10.76667 50.65)  1.021437\n",
       "8      POINT (12.7249 54.4368)  1.650011\n",
       "9       POINT (24.28333 61.85)  2.523140\n",
       "10        POINT (8.63333 45.8)  9.422871\n",
       "11       POINT (10.7 44.18333)  0.259719"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy_ghost.shapefile['sconcno2'] = nessy_ghost.variables['sconcno2']['data'][:, 0].ravel()\n",
    "nessy_ghost.shapefile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1, figsize=(19, 7))\n",
    "ax.set_xlim([-25, 45])\n",
    "ax.set_ylim([30, 70])\n",
    "countries = gpd.read_file('/gpfs/projects/bsc32/models/NES_tutorial_data/gadm_country_mask/gadm_country_ISO3166.shp')\n",
    "countries.plot(ax=ax, facecolor='none', edgecolor='black', linewidth=0.3)\n",
    "nessy_ghost.shapefile.plot(ax=ax, column='sconcno2', cmap='jet', edgecolor='black', lw=0.5, legend=True,\n",
    "                           legend_kwds={'label': 'Surface NO2 (nmol mol-1)'})\n",
    "ax.margins(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3. Write dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Write NetCDF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nessy_ghost.to_netcdf('points_ghost.nc', info=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Write shapefile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nessy_ghost.to_netcdf('points_ghost.nc', info=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
