{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calculate daily statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from nes import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Read dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 571 ms, sys: 93.9 ms, total: 665 ms\n",
      "Wall time: 10.5 s\n"
     ]
    }
   ],
   "source": [
    "# Original path: /esarchive/exp/monarch/a4dd/original_files/000/2022111512/MONARCH_d01_2022111512.nc\n",
    "# Rotated grid from MONARCH\n",
    "cams_file = '/gpfs/projects/bsc32/models/NES_tutorial_data/MONARCH_d01_2022111512.nc'\n",
    "%time nessy = open_netcdf(path=cams_file, info=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<nes.nc_projections.rotated_nes.RotatedNes at 0x7f64aab5b5d0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1. Time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[datetime.datetime(2022, 11, 15, 12, 0),\n",
       " datetime.datetime(2022, 11, 15, 13, 0),\n",
       " datetime.datetime(2022, 11, 15, 14, 0),\n",
       " datetime.datetime(2022, 11, 15, 15, 0),\n",
       " datetime.datetime(2022, 11, 15, 16, 0),\n",
       " datetime.datetime(2022, 11, 15, 17, 0),\n",
       " datetime.datetime(2022, 11, 15, 18, 0),\n",
       " datetime.datetime(2022, 11, 15, 19, 0),\n",
       " datetime.datetime(2022, 11, 15, 20, 0),\n",
       " datetime.datetime(2022, 11, 15, 21, 0),\n",
       " datetime.datetime(2022, 11, 15, 22, 0),\n",
       " datetime.datetime(2022, 11, 15, 23, 0),\n",
       " datetime.datetime(2022, 11, 16, 0, 0),\n",
       " datetime.datetime(2022, 11, 16, 1, 0),\n",
       " datetime.datetime(2022, 11, 16, 2, 0),\n",
       " datetime.datetime(2022, 11, 16, 3, 0),\n",
       " datetime.datetime(2022, 11, 16, 4, 0),\n",
       " datetime.datetime(2022, 11, 16, 5, 0),\n",
       " datetime.datetime(2022, 11, 16, 6, 0),\n",
       " datetime.datetime(2022, 11, 16, 7, 0),\n",
       " datetime.datetime(2022, 11, 16, 8, 0),\n",
       " datetime.datetime(2022, 11, 16, 9, 0),\n",
       " datetime.datetime(2022, 11, 16, 10, 0),\n",
       " datetime.datetime(2022, 11, 16, 11, 0),\n",
       " datetime.datetime(2022, 11, 16, 12, 0),\n",
       " datetime.datetime(2022, 11, 16, 13, 0),\n",
       " datetime.datetime(2022, 11, 16, 14, 0),\n",
       " datetime.datetime(2022, 11, 16, 15, 0),\n",
       " datetime.datetime(2022, 11, 16, 16, 0),\n",
       " datetime.datetime(2022, 11, 16, 17, 0),\n",
       " datetime.datetime(2022, 11, 16, 18, 0),\n",
       " datetime.datetime(2022, 11, 16, 19, 0),\n",
       " datetime.datetime(2022, 11, 16, 20, 0),\n",
       " datetime.datetime(2022, 11, 16, 21, 0),\n",
       " datetime.datetime(2022, 11, 16, 22, 0),\n",
       " datetime.datetime(2022, 11, 16, 23, 0),\n",
       " datetime.datetime(2022, 11, 17, 0, 0)]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2. Level, Latitude, Longitude"
   ]
  },
  {
   "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": [
    "nessy.lev"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': masked_array(\n",
       "   data=[[16.350338, 16.43293 , 16.515146, ..., 16.515146, 16.43293 ,\n",
       "          16.350338],\n",
       "         [16.527426, 16.610239, 16.692677, ..., 16.692677, 16.610243,\n",
       "          16.527426],\n",
       "         [16.704472, 16.787508, 16.870167, ..., 16.870167, 16.78751 ,\n",
       "          16.704472],\n",
       "         ...,\n",
       "         [58.32095 , 58.472683, 58.62431 , ..., 58.62431 , 58.472683,\n",
       "          58.32095 ],\n",
       "         [58.426285, 58.5782  , 58.730026, ..., 58.730026, 58.5782  ,\n",
       "          58.426285],\n",
       "         [58.530792, 58.6829  , 58.83492 , ..., 58.83492 , 58.682903,\n",
       "          58.530792]],\n",
       "   mask=False,\n",
       "   fill_value=1e+20,\n",
       "   dtype=float32),\n",
       " 'dimensions': ('rlat', 'rlon'),\n",
       " 'long_name': 'latitude',\n",
       " 'units': 'degrees_north',\n",
       " 'standard_name': 'latitude',\n",
       " 'coordinates': 'lon lat'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.lat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3. Variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### In lazy mode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['lmp', 'IM', 'JM', 'LM', 'IHRST', 'I_PAR_STA', 'J_PAR_STA', 'NPHS', 'NCLOD', 'NHEAT', 'NPREC', 'NRDLW', 'NRDSW', 'NSRFC', 'AVGMAXLEN', 'MDRMINout', 'MDRMAXout', 'MDIMINout', 'MDIMAXout', 'IDAT', 'DXH', 'SG1', 'SG2', 'DSG1', 'DSG2', 'SGML1', 'SGML2', 'SLDPTH', 'ISLTYP', 'IVGTYP', 'NCFRCV', 'NCFRST', 'FIS', 'GLAT', 'GLON', 'PD', 'VLAT', 'VLON', 'ACPREC', 'CUPREC', 'MIXHT', 'PBLH', 'RLWTOA', 'RSWIN', 'U10', 'USTAR', 'V10', 'RMOL', 'T2', 'relative_humidity_2m', 'T', 'U', 'V', 'SH2O', 'SMC', 'STC', 'AERO_ACPREC', 'AERO_CUPREC', 'AERO_DEPDRY', 'AERO_OPT_R', 'DRE_SW_TOA', 'DRE_SW_SFC', 'DRE_LW_TOA', 'DRE_LW_SFC', 'ENG_SW_SFC', 'ADRYDEP', 'WETDEP', 'PH_NO2', 'HSUM', 'POLR', 'aerosol_optical_depth_dim', 'aerosol_optical_depth', 'satellite_AOD_dim', 'satellite_AOD', 'aerosol_loading_dim', 'aerosol_loading', 'clear_sky_AOD_dim', 'clear_sky_AOD', 'layer_thickness', 'mid_layer_pressure', 'interface_pressure', 'relative_humidity', 'mid_layer_height', 'mid_layer_height_agl', 'air_density', 'dry_pm10_mass', 'dry_pm2p5_mass', 'QC', 'QR', 'QS', 'QG', 'aero_dust_001', 'aero_dust_002', 'aero_dust_003', 'aero_dust_004', 'aero_dust_005', 'aero_dust_006', 'aero_dust_007', 'aero_dust_008', 'aero_ssa_001', 'aero_ssa_002', 'aero_ssa_003', 'aero_ssa_004', 'aero_ssa_005', 'aero_ssa_006', 'aero_ssa_007', 'aero_ssa_008', 'aero_om_001', 'aero_om_002', 'aero_om_003', 'aero_om_004', 'aero_om_005', 'aero_om_006', 'aero_bc_001', 'aero_bc_002', 'aero_so4_001', 'aero_no3_001', 'aero_no3_002', 'aero_no3_003', 'aero_nh4_001', 'aero_unsp_001', 'aero_unsp_002', 'aero_unsp_003', 'aero_unsp_004', 'aero_unsp_005', 'NO2', 'NO', 'O3', 'NO3', 'N2O5', 'HNO3', 'HONO', 'PNA', 'H2O2', 'NTR', 'ROOH', 'FORM', 'ALD2', 'ALDX', 'PAR', 'CO', 'MEPX', 'MEOH', 'FACD', 'PAN', 'PACD', 'AACD', 'PANX', 'OLE', 'ETH', 'IOLE', 'TOL', 'CRES', 'OPEN', 'MGLY', 'XYL', 'ISOP', 'ISPD', 'TERP', 'SO2', 'SULF', 'ETOH', 'ETHA', 'CL2', 'HOCL', 'FMCL', 'HCL', 'BENZENE', 'SESQ', 'NH3', 'DMS', 'SOAP_I', 'SOAP_T', 'SOAP_F', 'SOAP_A', 'O', 'O1D', 'OH', 'HO2', 'XO2', 'XO2N', 'MEO2', 'HCO3', 'C2O3', 'CXO3', 'ROR', 'TO2', 'TOLRO2', 'CRO', 'XYLRO2', 'ISOPRXN', 'TRPRXN', 'SULRXN', 'CL', 'CLO', 'TOLNRXN', 'TOLHRXN', 'XYLNRXN', 'XYLHRXN', 'BENZRO2', 'BNZNRXN', 'BNZHRXN', 'SESQRXN', 'aerosol_extinction_dim', 'aerosol_extinction_DUST_1', 'aerosol_extinction_DUST_2', 'aerosol_extinction_DUST_3', 'aerosol_extinction_DUST_4', 'aerosol_extinction_DUST_5', 'aerosol_extinction_DUST_6', 'aerosol_extinction_DUST_7', 'aerosol_extinction_DUST_8', 'aerosol_extinction_SALT_total', 'aerosol_extinction_OM_total', 'aerosol_extinction_BC_total', 'aerosol_extinction_SO4_total', 'aerosol_extinction_NO3_total', 'aerosol_extinction_NH4_total', 'aerosol_extinction_UNSPC_1', 'aerosol_extinction_UNSPC_2', 'aerosol_extinction_UNSPC_3', 'aerosol_extinction_UNSPC_4', 'aerosol_extinction_UNSPC_5'])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.variables.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'O3': {'data': None,\n",
       "  'dimensions': ('time', 'lm', 'rlat', 'rlon'),\n",
       "  'long_name': 'TRACERS_044',\n",
       "  'units': 'unknown',\n",
       "  'standard_name': 'TRACERS_044',\n",
       "  'coordinates': 'lon lat',\n",
       "  'grid_mapping': 'rotated_pole'}}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Selecting only one variable and descarting the rest.\n",
    "nessy.keep_vars('O3')\n",
    "nessy.variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### After load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank 000: Loading O3 var (1/1)\n",
      "Rank 000: Loaded O3 var ((37, 24, 271, 351))\n",
      "CPU times: user 311 ms, sys: 1.5 s, total: 1.81 s\n",
      "Wall time: 11.3 s\n"
     ]
    }
   ],
   "source": [
    "# Loading variable data from NetCDF file\n",
    "%time nessy.load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(37, 24, 271, 351)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.variables['O3']['data'].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('time', 'lm', 'rlat', 'rlon')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.variables['O3']['dimensions']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 106 ms, sys: 104 ms, total: 210 ms\n",
      "Wall time: 3.03 s\n"
     ]
    }
   ],
   "source": [
    "# Writing NetCDF\n",
    "%time nessy.to_netcdf('o3_test.nc')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Calculate daily statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 74.8 ms, sys: 21 ms, total: 95.8 ms\n",
      "Wall time: 260 ms\n"
     ]
    }
   ],
   "source": [
    "%time nessy.daily_statistic(op=\"mean\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 24, 271, 351)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.variables['O3']['data'].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('time', 'lm', 'rlat', 'rlon')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.variables['O3']['dimensions']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 18.2 ms, sys: 8.24 ms, total: 26.4 ms\n",
      "Wall time: 402 ms\n"
     ]
    }
   ],
   "source": [
    "%time nessy.to_netcdf('o3_daily_mean_test.nc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'time: mean (interval: 1hr)'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# See metadata\n",
    "nessy.variables['O3']['cell_methods']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[datetime.datetime(2022, 11, 15, 0, 0), datetime.datetime(2022, 11, 16, 0, 0)]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# See time\n",
    "nessy.time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[datetime.datetime(2022, 11, 15, 12, 0),\n",
       "  datetime.datetime(2022, 11, 15, 23, 0)],\n",
       " [datetime.datetime(2022, 11, 16, 0, 0),\n",
       "  datetime.datetime(2022, 11, 16, 23, 0)]]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# See bounds\n",
    "nessy.time_bnds"
   ]
  }
 ],
 "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
}
