{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to add new variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "source": [
    "from nes import *\n",
    "import numpy as np\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import geopandas as gpd"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "source": [
    "%matplotlib inline"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "source": [
    "original_file_path = \"/gpfs/projects/bsc32/models/NES_tutorial_data/MONARCH_d01_2022111512.nc\""
   ],
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Open original file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "source": [
    "# Open file with only the metadata\n",
    "nessy = open_netcdf(original_file_path, parallel_method='Y')"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "source": [
    "# Load variables data\n",
    "nessy.keep_vars(['NO2'])  # If you avoid this part all variables will be loaded\n",
    "nessy.load()"
   ],
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Create new variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1. Create variable my modifying an existen one"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "source": [
    "from copy import deepcopy\n",
    "\n",
    "# Copy the base variable and all the metadata\n",
    "nessy.variables['new_var1'] = deepcopy(nessy.variables['NO2'])\n"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "source": [
    "# Modify the data and the metadata\n",
    "nessy.variables['new_var1']['data'] *= 2\n",
    "nessy.variables['new_var1']['long_name'] = 'Double NO2'\n",
    "nessy.variables['new_var1']['standard_name'] = 'Invented_name'"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "source": [
    "# Create the new file\n",
    "nessy.to_netcdf('new_var1.nc')"
   ],
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1. Create variable from scratch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "source": [
    "nessy = open_netcdf(original_file_path, parallel_method='Y')"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "source": [
    "# Remove all variables information to keep only the metadata\n",
    "nessy.variables = {}"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "source": [
    "# Create metadata\n",
    "nessy.variables['new_var2'] = {'data': None,\n",
    "                               'units': 'kg.m-2',\n",
    "                               'description': 'new variable from scratch',\n",
    "                               'short_name': 'newvar2',\n",
    "                              }\n",
    "\n",
    "# Fill the data\n",
    "shape = (len(nessy.time), len(nessy.lev['data']), nessy.lat['data'].shape[0], nessy.lon['data'].shape[-1])\n",
    "nessy.variables['new_var2']['data'] = np.random.rand(*shape)"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "source": [
    "nessy.to_netcdf('new_var2.nc')"
   ],
   "outputs": []
  }
 ],
 "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
}
