{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Select function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from nes import *"
   ]
  },
  {
   "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": 13,
   "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 244 ms, sys: 1.35 s, total: 1.59 s\n",
      "Wall time: 3.33 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\nCPU times: user 1.17 s, sys: 6.79 s, total: 7.96 s\\nWall time: 31.4 s\\n'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy = 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()\n",
    "\"\"\"\n",
    "CPU times: user 244 ms, sys: 1.35 s, total: 1.59 s\n",
    "Wall time: 3.33 s\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Hours filter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "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 70.8 ms, sys: 363 ms, total: 433 ms\n",
      "Wall time: 1.57 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\nCPU times: user 295 ms, sys: 1.47 s, total: 1.76 s\\nWall time: 6.77 s\\n'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy = 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()\n",
    "\"\"\"\n",
    "CPU times: user 70.8 ms, sys: 363 ms, total: 433 ms\n",
    "Wall time: 1.57 s\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Time filter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "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 95.1 ms, sys: 490 ms, total: 585 ms\n",
      "Wall time: 1.47 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\nCPU times: user 274 ms, sys: 1.44 s, total: 1.71 s\\nWall time: 7.53 s\\n'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "nessy = 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()\n",
    "\"\"\"\n",
    "CPU times: user 95.1 ms, sys: 490 ms, total: 585 ms\n",
    "Wall time: 1.47 s\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Select data by level"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "CPU times: user 14.4 ms, sys: 70 ms, total: 84.4 ms\n",
      "Wall time: 5.55 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\nCPU times: user 77.3 ms, sys: 248 ms, total: 325 ms\\nWall time: 17.1 s\\n'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy = 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()\n",
    "\"\"\"\n",
    "CPU times: user 14.4 ms, sys: 70 ms, total: 84.4 ms\n",
    "Wall time: 5.55 s\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Select data by coordinates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 4.11 ms, sys: 22 ms, total: 26.2 ms\n",
      "Wall time: 4.2 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\nCPU times: user 13.9 ms, sys: 74.9 ms, total: 88.8 ms\\nWall time: 16.3 s\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy = 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()\n",
    "\"\"\"\n",
    "CPU times: user 4.11 ms, sys: 22 ms, total: 26.2 ms\n",
    "Wall time: 4.2 s\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Write dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "nessy.to_netcdf(\"test_sel.nc\")"
   ]
  }
 ],
 "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
}
