{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to create Mercator grids"
   ]
  },
  {
   "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"
   ]
  },
  {
   "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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "projection='mercator'\n",
    "lat_ts = -1.5\n",
    "lon_0 = -18.0\n",
    "nx = 210\n",
    "ny = 236\n",
    "inc_x = 50000\n",
    "inc_y = 50000\n",
    "x_0 = -126017.5\n",
    "y_0 = -5407460.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "nessy = nes.create_nes(comm=None, info=False, projection=projection,\n",
    "                       lat_ts=lat_ts, lon_0=lon_0, nx=nx, ny=ny, \n",
    "                       inc_x=inc_x, inc_y=inc_y, x_0=x_0, y_0=y_0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read projection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Other Coordinate Operation Transformer: merc>\n",
       "Description: PROJ-based coordinate operation\n",
       "Area of Use:\n",
       "- undefined"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.projection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'grid_mapping_name': 'mercator',\n",
       " 'standard_parallel': -1.5,\n",
       " 'longitude_of_projection_origin': -18.0,\n",
       " 'x_0': -126017.5,\n",
       " 'y_0': -5407460.0,\n",
       " 'inc_x': 50000,\n",
       " 'inc_y': 50000,\n",
       " 'nx': 210,\n",
       " 'ny': 236}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.projection_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "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 ((-19.13242 -43.82824, -18.68311 -43.8...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>POLYGON ((-18.68311 -43.82824, -18.2338 -43.82...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>POLYGON ((-18.2338 -43.82824, -17.78449 -43.82...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>POLYGON ((-17.78449 -43.82824, -17.33518 -43.8...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>POLYGON ((-17.33518 -43.82824, -16.88587 -43.8...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49555</th>\n",
       "      <td>POLYGON ((72.97625 49.59982, 73.42556 49.59982...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49556</th>\n",
       "      <td>POLYGON ((73.42556 49.59982, 73.87487 49.59982...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49557</th>\n",
       "      <td>POLYGON ((73.87487 49.59982, 74.32418 49.59982...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49558</th>\n",
       "      <td>POLYGON ((74.32418 49.59982, 74.77349 49.59982...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49559</th>\n",
       "      <td>POLYGON ((74.77349 49.59982, 75.2228 49.59982,...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>49560 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                geometry\n",
       "FID                                                     \n",
       "0      POLYGON ((-19.13242 -43.82824, -18.68311 -43.8...\n",
       "1      POLYGON ((-18.68311 -43.82824, -18.2338 -43.82...\n",
       "2      POLYGON ((-18.2338 -43.82824, -17.78449 -43.82...\n",
       "3      POLYGON ((-17.78449 -43.82824, -17.33518 -43.8...\n",
       "4      POLYGON ((-17.33518 -43.82824, -16.88587 -43.8...\n",
       "...                                                  ...\n",
       "49555  POLYGON ((72.97625 49.59982, 73.42556 49.59982...\n",
       "49556  POLYGON ((73.42556 49.59982, 73.87487 49.59982...\n",
       "49557  POLYGON ((73.87487 49.59982, 74.32418 49.59982...\n",
       "49558  POLYGON ((74.32418 49.59982, 74.77349 49.59982...\n",
       "49559  POLYGON ((74.77349 49.59982, 75.2228 49.59982,...\n",
       "\n",
       "[49560 rows x 1 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.create_shapefile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 251036 characters]",
      "text/plain": [
       "<Figure size 1900x700 with 1 Axes>"
      ]
     },
     "metadata": {},
     "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('/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",
    "ax.margins(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Write dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Write NetCDF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank 000: Creating mercator_grid.nc\n",
      "Rank 000: NetCDF ready to write\n",
      "Rank 000: Dimensions done\n"
     ]
    }
   ],
   "source": [
    "nessy.to_netcdf('mercator_grid.nc', info=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Write shapefile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "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"
     ]
    }
   ],
   "source": [
    "nessy.to_shapefile('mercator_grid_shp')"
   ]
  }
 ],
 "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
}
