{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nes\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define first grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "projection = 'regular'\n",
    "lat_orig = 41.1\n",
    "lon_orig = 1.8\n",
    "inc_lat = 0.2\n",
    "inc_lon = 0.2\n",
    "n_lat = 100\n",
    "n_lon = 100\n",
    "nessy = nes.create_nes(projection=projection, lat_orig=lat_orig, lon_orig=lon_orig, inc_lat=inc_lat, inc_lon=inc_lon,\n",
    "                       n_lat=n_lat, n_lon=n_lon)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "nessy.variables = {'var_aux': {'data': np.ones((len(nessy.time), len(nessy.lev['data']),\n",
    "                                                nessy.lat['data'].shape[0], nessy.lon['data'].shape[-1]))}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'var_aux': {'data': array([[[[1., 1., 1., ..., 1., 1., 1.],\n",
       "           [1., 1., 1., ..., 1., 1., 1.],\n",
       "           [1., 1., 1., ..., 1., 1., 1.],\n",
       "           ...,\n",
       "           [1., 1., 1., ..., 1., 1., 1.],\n",
       "           [1., 1., 1., ..., 1., 1., 1.],\n",
       "           [1., 1., 1., ..., 1., 1., 1.]]]])}}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy.variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define second grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "nessy_2 = nessy.copy(copy_vars=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "for var_name in nessy_2.variables.keys():\n",
    "    nessy_2.variables[var_name]['data'] *= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'var_aux': {'data': array([[[[2., 2., 2., ..., 2., 2., 2.],\n",
       "           [2., 2., 2., ..., 2., 2., 2.],\n",
       "           [2., 2., 2., ..., 2., 2., 2.],\n",
       "           ...,\n",
       "           [2., 2., 2., ..., 2., 2., 2.],\n",
       "           [2., 2., 2., ..., 2., 2., 2.],\n",
       "           [2., 2., 2., ..., 2., 2., 2.]]]])}}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy_2.variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sum variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "nessy_3 = nessy + nessy_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'var_aux': {'data': array([[[[3., 3., 3., ..., 3., 3., 3.],\n",
       "           [3., 3., 3., ..., 3., 3., 3.],\n",
       "           [3., 3., 3., ..., 3., 3., 3.],\n",
       "           ...,\n",
       "           [3., 3., 3., ..., 3., 3., 3.],\n",
       "           [3., 3., 3., ..., 3., 3., 3.],\n",
       "           [3., 3., 3., ..., 3., 3., 3.]]]])}}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nessy_3.variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rank 000: Creating sum.nc\n",
      "Rank 000: NetCDF ready to write\n",
      "Rank 000: Dimensions done\n",
      "Rank 000: Writing var_aux var (1/1)\n",
      "Rank 000: Var var_aux created (1/1)\n",
      "Rank 000: Filling var_aux)\n",
      "Rank 000: Var var_aux data (1/1)\n",
      "Rank 000: Var var_aux completed (1/1)\n"
     ]
    }
   ],
   "source": [
    "nessy_3.to_netcdf(\"sum.nc\", info=True)"
   ]
  }
 ],
 "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
}
