{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Vertical interpolation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from nes import *\n",
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Interpolation with vertical coordinate and variable to interpolate in one file"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1. Read dataset to interpolate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Original path: /esarchive/exp/monarch/a4dd/original_files/000/2022111512/MONARCH_d01_2022111512.nc\n",
    "# Rotated grid from MONARCH\n",
    "source_path = '/gpfs/projects/bsc32/models/NES_tutorial_data/MONARCH_d01_2022111512.nc'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<nes.nc_projections.rotated_nes.RotatedNes at 0x7f0ecdb80810>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "source_grid = open_netcdf(path=source_path, info=True)\n",
    "source_grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "source_grid.sel(time_min=datetime(year=2022, month=11, day=16, hour=0), \n",
    "                time_max=datetime(year=2022, month=11, day=16, hour=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': masked_array(data=[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,\n",
       "                    14, 15, 16, 17, 18, 19, 20, 21, 22, 23],\n",
       "              mask=False,\n",
       "        fill_value=999999,\n",
       "             dtype=int32),\n",
       " 'dimensions': ('lm',),\n",
       " 'units': '',\n",
       " 'long_name': 'layer id'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "source_grid.lev"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "source_grid.keep_vars(['O3', 'mid_layer_height_agl'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank 000: Loading mid_layer_height_agl var (1/2)\n",
      "Rank 000: Loaded mid_layer_height_agl var ((1, 24, 271, 351))\n",
      "Rank 000: Loading O3 var (2/2)\n",
      "Rank 000: Loaded O3 var ((1, 24, 271, 351))\n"
     ]
    }
   ],
   "source": [
    "source_grid.load()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2. Interpolate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "level_list = [0.,50.,100.,250.,500.,750.,1000.,2000.,3000.,5000.]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "source_grid.vertical_var_name = 'mid_layer_height_agl'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tO3 vertical methods\n",
      "\t\tO3 time step 2022-11-16 00:00:00 (1/1))\n"
     ]
    }
   ],
   "source": [
    "interpolated_source_grid = source_grid.interpolate_vertical(level_list, info=True, \n",
    "                                                            kind='linear', extrapolate=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<nes.nc_projections.rotated_nes.RotatedNes at 0x7f0ecdb80810>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interpolated_source_grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': [0.0,\n",
       "  50.0,\n",
       "  100.0,\n",
       "  250.0,\n",
       "  500.0,\n",
       "  750.0,\n",
       "  1000.0,\n",
       "  2000.0,\n",
       "  3000.0,\n",
       "  5000.0],\n",
       " 'long_name': 'Mid-layer height above ground level',\n",
       " 'standard_name': 'height_agl',\n",
       " 'units': 'm',\n",
       " 'coordinates': 'lon lat'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interpolated_source_grid.lev"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Interpolation with vertical coordinate in a separate file"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1. Read data to interpolate (source)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<nes.nc_projections.rotated_nes.RotatedNes at 0x7f0ecdb08690>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "source_data = open_netcdf(path=source_path, info=True)\n",
    "source_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "source_data.sel(time_min=datetime(year=2022, month=11, day=16, hour=0), \n",
    "                time_max=datetime(year=2022, month=11, day=16, hour=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "source_data.keep_vars('O3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank 000: Loading O3 var (1/1)\n",
      "Rank 000: Loaded O3 var ((1, 24, 271, 351))\n"
     ]
    }
   ],
   "source": [
    "source_data.load()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2. Read vertical levels (source)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<nes.nc_projections.rotated_nes.RotatedNes at 0x7f0ecdb50450>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "source_levels = open_netcdf(path=source_path, info=True)\n",
    "source_levels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "source_levels.sel(time_min=datetime(year=2022, month=11, day=16, hour=0), \n",
    "                  time_max=datetime(year=2022, month=11, day=16, hour=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "source_levels.keep_vars(['mid_layer_height_agl'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank 000: Loading mid_layer_height_agl var (1/1)\n",
      "Rank 000: Loaded mid_layer_height_agl var ((1, 24, 271, 351))\n"
     ]
    }
   ],
   "source": [
    "source_levels.load()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3. Interpolate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tO3 vertical methods\n",
      "\t\tO3 time step 2022-11-16 00:00:00 (1/1))\n"
     ]
    }
   ],
   "source": [
    "interpolated_source_grid = source_data.interpolate_vertical(level_list, new_src_vertical=source_levels,\n",
    "                                                            info=True, kind='linear', extrapolate=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<nes.nc_projections.rotated_nes.RotatedNes at 0x7f0ecdb08690>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interpolated_source_grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': [0.0,\n",
       "  50.0,\n",
       "  100.0,\n",
       "  250.0,\n",
       "  500.0,\n",
       "  750.0,\n",
       "  1000.0,\n",
       "  2000.0,\n",
       "  3000.0,\n",
       "  5000.0],\n",
       " 'long_name': 'Mid-layer height above ground level',\n",
       " 'standard_name': 'height_agl',\n",
       " 'units': 'm',\n",
       " 'coordinates': 'lon lat'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interpolated_source_grid.lev"
   ]
  }
 ],
 "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"
  },
  "vscode": {
   "interpreter": {
    "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
