{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Write by timestep (to avoid memory issues)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from nes import *\n",
    "import numpy as np\n",
    "from datetime import datetime, timedelta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Create grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "centre_lat = 51\n",
    "centre_lon = 10\n",
    "west_boundary = -35\n",
    "south_boundary = -27\n",
    "inc_rlat = 0.2\n",
    "inc_rlon = 0.2\n",
    "rotated_grid = create_nes(comm=None, info=False, projection='rotated',\n",
    "                          centre_lat=centre_lat, centre_lon=centre_lon,\n",
    "                          west_boundary=west_boundary, south_boundary=south_boundary,\n",
    "                          inc_rlat=inc_rlat, inc_rlon=inc_rlon)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Create variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "rotated_grid.variables = {'var1': {'data': None, 'units': 'kg.s-1', 'dtype': np.float32},\n",
    "                          'var2': {'data': None, 'units': 'kg.s-1', 'dtype': np.float32}}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Change time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Old time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[datetime.datetime(1996, 12, 31, 0, 0)]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rotated_grid.time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### New time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "time_list = [datetime(year=2023, month=1, day=1) + timedelta(hours=x) for x in range (2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "rotated_grid.set_time(time_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[datetime.datetime(2023, 1, 1, 0, 0), datetime.datetime(2023, 1, 1, 1, 0)]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rotated_grid.time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Write dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "rotated_grid.to_netcdf('rotated.nc', keep_open=True, info=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "rotated_grid.variables['var1']['data'] = np.ones((1, 1, rotated_grid.lat['data'].shape[0], rotated_grid.lon['data'].shape[-1]))\n",
    "rotated_grid.variables['var2']['data'] = np.ones((1, 1, rotated_grid.lat['data'].shape[0], rotated_grid.lon['data'].shape[-1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "rotated_grid.append_time_step_data(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "rotated_grid.variables['var1']['data'] = np.ones((1, 1, rotated_grid.lat['data'].shape[0], rotated_grid.lon['data'].shape[-1])) *2\n",
    "rotated_grid.variables['var2']['data'] = np.ones((1, 1, rotated_grid.lat['data'].shape[0], rotated_grid.lon['data'].shape[-1])) *2\n",
    "\n",
    "rotated_grid.append_time_step_data(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "rotated_grid.close()"
   ]
  }
 ],
 "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
}
