{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Select function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nes\n",
    "from datetime import datetime"
   ]
  },
  {
   "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",
    "file_path = '/gpfs/projects/bsc32/models/NES_tutorial_data/MONARCH_d01_2022111512.nc'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Select data by time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-11-15 12:00:00 37 2022-11-17 00:00:00\n",
      "CPU times: user 111 ms, sys: 142 ms, total: 253 ms\n",
      "Wall time: 253 ms\n"
     ]
    }
   ],
   "source": [
    "nessy = nes.open_netcdf(file_path)\n",
    "nessy.keep_vars('O3')\n",
    "\n",
    "print(nessy.time[0], len(nessy.time), nessy.time[-1])\n",
    "%time nessy.load()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Hours filter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-11-16 00:00:00 9 2022-11-16 08:00:00\n",
      "CPU times: user 20.1 ms, sys: 38 ms, total: 58 ms\n",
      "Wall time: 58.3 ms\n"
     ]
    }
   ],
   "source": [
    "nessy = nes.open_netcdf(file_path)\n",
    "nessy.keep_vars('O3')\n",
    "\n",
    "nessy.sel(hours_start=12, hours_end=16)\n",
    "\n",
    "print(nessy.time[0], len(nessy.time), nessy.time[-1])\n",
    "%time nessy.load()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Time filter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-11-15 12:00:00 12 2022-11-15 23:00:00\n",
      "CPU times: user 32.4 ms, sys: 43.8 ms, total: 76.2 ms\n",
      "Wall time: 76.5 ms\n"
     ]
    }
   ],
   "source": [
    "nessy = nes.open_netcdf(file_path)\n",
    "nessy.keep_vars('O3')\n",
    "\n",
    "nessy.sel(time_min=datetime(year=2022, month=11, day=15, hour=0), \n",
    "          time_max=datetime(year=2022, month=11, day=15, hour=23))\n",
    "\n",
    "print(nessy.time[0], len(nessy.time), nessy.time[-1])\n",
    "%time nessy.load()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Select data by level"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "CPU times: user 5.56 ms, sys: 27.8 ms, total: 33.4 ms\n",
      "Wall time: 33.5 ms\n"
     ]
    }
   ],
   "source": [
    "nessy = nes.open_netcdf(file_path)\n",
    "nessy.keep_vars('O3')\n",
    "\n",
    "nessy.sel(lev_min=23)\n",
    "\n",
    "print(len(nessy.lev['data']))\n",
    "%time nessy.load()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Select data by coordinates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 727 µs, sys: 13.9 ms, total: 14.6 ms\n",
      "Wall time: 14.7 ms\n"
     ]
    }
   ],
   "source": [
    "nessy = nes.open_netcdf(file_path)\n",
    "nessy.keep_vars('O3')\n",
    "\n",
    "nessy.sel(lat_min=30, lat_max=31, lon_min=-1, lon_max=1)\n",
    "%time nessy.load()"
   ]
  }
 ],
 "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"
  },
  "vscode": {
   "interpreter": {
    "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
