{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to create LCC grids"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from nes import *\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import geopandas as gpd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Create dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define grid details\n",
    "\n",
    "This corresponds to a regular grid for CALIOPE IP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "projection='lcc'\n",
    "lat_1 = 37\n",
    "lat_2 = 43\n",
    "lon_0 = -3\n",
    "lat_0 = 40\n",
    "nx = 397\n",
    "ny = 397\n",
    "inc_x = 4000\n",
    "inc_y = 4000\n",
    "x_0 = -807847.688\n",
    "y_0 = -797137.125"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "nessy = create_nes(comm=None, info=False, projection=projection,\n",
    "                   lat_1=lat_1, lat_2=lat_2, lon_0=lon_0, lat_0=lat_0, \n",
    "                   nx=nx, ny=ny, inc_x=inc_x, inc_y=inc_y, x_0=x_0, y_0=y_0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank 000: Creating lcc_grid.nc\n",
      "Rank 000: NetCDF ready to write\n",
      "Rank 000: Dimensions done\n"
     ]
    }
   ],
   "source": [
    "nessy.to_netcdf('lcc_grid.nc', info=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read projection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Proj('+proj=lcc +lat_0=40 +lon_0=-3 +lat_1=37 +lat_2=43 +x_0=0 +y_0=0 +R=6356752.3142 +units=m +no_defs', preserve_units=True)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.projection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'grid_mapping_name': 'lambert_conformal_conic',\n",
       " 'standard_parallel': [37, 43],\n",
       " 'longitude_of_central_meridian': -3,\n",
       " 'latitude_of_projection_origin': 40,\n",
       " 'x_0': -807847.688,\n",
       " 'y_0': -797137.125,\n",
       " 'inc_x': 4000,\n",
       " 'inc_y': 4000,\n",
       " 'nx': 397,\n",
       " 'ny': 397}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.projection_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "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>POLYGON ((-11.58393 32.47507, -11.54169 32.478...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>POLYGON ((-11.54169 32.47851, -11.49944 32.481...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>POLYGON ((-11.49944 32.48192, -11.45719 32.485...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>POLYGON ((-11.45719 32.48533, -11.41494 32.488...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>POLYGON ((-11.41494 32.48871, -11.37268 32.492...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>157604</th>\n",
       "      <td>POLYGON ((6.95490 46.70274, 7.00684 46.69873, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>157605</th>\n",
       "      <td>POLYGON ((7.00684 46.69873, 7.05878 46.69470, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>157606</th>\n",
       "      <td>POLYGON ((7.05878 46.69470, 7.11071 46.69066, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>157607</th>\n",
       "      <td>POLYGON ((7.11071 46.69066, 7.16264 46.68659, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>157608</th>\n",
       "      <td>POLYGON ((7.16264 46.68659, 7.21456 46.68250, ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>157609 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 geometry\n",
       "FID                                                      \n",
       "0       POLYGON ((-11.58393 32.47507, -11.54169 32.478...\n",
       "1       POLYGON ((-11.54169 32.47851, -11.49944 32.481...\n",
       "2       POLYGON ((-11.49944 32.48192, -11.45719 32.485...\n",
       "3       POLYGON ((-11.45719 32.48533, -11.41494 32.488...\n",
       "4       POLYGON ((-11.41494 32.48871, -11.37268 32.492...\n",
       "...                                                   ...\n",
       "157604  POLYGON ((6.95490 46.70274, 7.00684 46.69873, ...\n",
       "157605  POLYGON ((7.00684 46.69873, 7.05878 46.69470, ...\n",
       "157606  POLYGON ((7.05878 46.69470, 7.11071 46.69066, ...\n",
       "157607  POLYGON ((7.11071 46.69066, 7.16264 46.68659, ...\n",
       "157608  POLYGON ((7.16264 46.68659, 7.21456 46.68250, ...\n",
       "\n",
       "[157609 rows x 1 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.create_shapefile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 164564 characters]\n",
      "text/plain": [
       "<Figure size 1368x504 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(1, figsize=(19, 7))\n",
    "nessy.shapefile.plot(ax=ax, facecolor='blue', edgecolor='blue')\n",
    "countries = gpd.read_file('/esarchive/shapefiles/gadm_country_mask/gadm_country_ISO3166.shp')\n",
    "countries.plot(ax=ax, facecolor='none', edgecolor='black', linewidth=0.3)\n",
    "ax.margins(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Write dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Write NetCDF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "nessy.to_netcdf('lcc_grid.nc')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Write shapefile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/esarchive/scratch/avilanova/software/NES/nes/nc_projections/default_nes.py:3058: UserWarning: No vertical level has been specified. The first one will be selected.\n",
      "  warnings.warn(msg)\n",
      "/esarchive/scratch/avilanova/software/NES/nes/nc_projections/default_nes.py:3069: UserWarning: No time has been specified. The first one will be selected.\n",
      "  warnings.warn(msg)\n"
     ]
    }
   ],
   "source": [
    "nessy.to_shapefile('lcc_grid_shp')"
   ]
  }
 ],
 "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.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
