{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%load_ext autoreload\n",
    "#%autoreload 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tutorial: Lorenz-96 \n",
    "\n",
    "This notebook runs the Lorenz96 example using ICESEE's data assimilation driver."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, sys\n",
    "import platform\n",
    "import numpy as np\n",
    "from pathlib import Path\n",
    "\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup and preprocessing:\n",
    "\n",
    "# Current path\n",
    "Path = os.path.realpath(\".\")\n",
    "#print ('Path: %s\\n' %Path)\n",
    "\n",
    "# Parent path\n",
    "Parentpath = os.path.dirname(Path)\n",
    "#print ('Parentpath: %s\\n' %Parentpath)\n",
    "\n",
    "# Add to PYTHONPATH\n",
    "sys.path.append(Parentpath)\n",
    "\n",
    "# Tool path\n",
    "Toolpath = os.path.dirname(Parentpath)\n",
    "#print ('Toolpath: %s\\n' %Toolpath)\n",
    "\n",
    "import utils\n",
    "from utils.configuration import VERBOSE\n",
    "\n",
    "if VERBOSE:\n",
    "    \n",
    "    print (\"Operating System Platform: \" + platform.system(), platform.release())\n",
    "    print (\"\\n\")\n",
    "\n",
    "    print (\"os.environ['PATH']: \", os.environ['PATH'])\n",
    "    print (\"sys.path: \", sys.path)\n",
    "    print (\"\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    function scroll_to_top() {\n",
       "        Jupyter.notebook.scroll_to_top();\n",
       "    } \n",
       "    $( window ).on( \"load\", scroll_to_top() );\n",
       "</script>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#https://api.jquery.com/ready/\n",
    "HTML('''\n",
    "<script>\n",
    "    function scroll_to_top() {\n",
    "        Jupyter.notebook.scroll_to_top();\n",
    "    } \n",
    "    $( window ).on( \"load\", scroll_to_top() );\n",
    "</script>\n",
    "''')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": "IPython.OutputArea.prototype._should_scroll = function(lines) {\n    return false;\n}\n",
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%javascript\n",
    "IPython.OutputArea.prototype._should_scroll = function(lines) {\n",
    "    return false;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Directory tree of\n",
      "/Users/bkyanjo3/da_project/ICESEE-GHUB:\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print ('\\nDirectory tree of\\n%s:\\n' %Toolpath)\n",
    "from utils.Tree import Tree\n",
    "results_tree = Tree(Toolpath)\n",
    "# results_tree.print_tree()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ICESEE bootstrap] external path enabled: /Users/bkyanjo3/da_project/ICESEE-GHUB/external\n",
      "[ICESEE bootstrap] ICESEE loaded from: /Users/bkyanjo3/da_project/ICESEE-GHUB/external/ICESEE/__init__.py\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "PosixPath('/Users/bkyanjo3/da_project/ICESEE-GHUB/external')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from _bootstrap._bootstrap import enable_icesee\n",
    "enable_icesee()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ICESEE] Running default parallel mode\n",
      "[ICESEE] Generating true state ...\n",
      "[ICESEE] Generating nurged state ...\n",
      "[ICESEE] Generating synthetic observations ...\n",
      "[ICESEE] observation times requested: [0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5 1.7 1.9]\n",
      "[ICESEE] observation model indices ind_m: [ 10  30  50  70  90 110 130 150 170 190], total m_obs=10\n",
      "[ICESEE] bed_snaps: []\n",
      "[ICESEE] bed_snap_cols: []\n",
      "[ICESEE] Initializing the ensemble ...\n",
      "[ICESEE] Launching lorenz with data assimilation using the EnKF filter across 1 MPI ranks.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[ICESEE] Assimilation progress (1 ranks):  98%|█████████▊| 976/1000 [00:11<00:00, 177.88it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ICESEE] Existing file results/true-wrong-lorenz.h5 removed.\n",
      "[ICESEE] Writing data to results/true-wrong-lorenz.h5\n",
      "[ICESEE] Dataset 't' written to file\n",
      "[ICESEE] Dataset 'b_io' written to file\n",
      "[ICESEE] Dataset 'Lxy' written to file\n",
      "[ICESEE] Dataset 'nxy' written to file\n",
      "[ICESEE] Dataset 'obs_max_time' written to file\n",
      "[ICESEE] Dataset 'obs_index' written to file\n",
      "[ICESEE] Dataset 'run_mode' written to file\n",
      "[ICESEE] Data successfully written to results/true-wrong-lorenz.h5\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[ICESEE] Assimilation progress (1 ranks): 100%|██████████| 1000/1000 [00:11<00:00, 85.96it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# --- call the lorenz96 data assimilation example ---\n",
    "# go to external/ICESEE\n",
    "# os.system('cd ../../external/ICESEE/applications/lorenz_model/examples/lorenz96')\n",
    "import os\n",
    "os.system('python3 -m ICESEE.applications.lorenz_model.examples.lorenz96.run_da_lorenz96')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# make  read results and postprocess notebook\n",
    "# from utils.notebook_runner import find_repo_root, run_postprocess_to_wrapper\n",
    "\n",
    "# root = find_repo_root()\n",
    "\n",
    "# postprocess_nb = (\n",
    "#     root / \"external\" / \"ICESEE\"\n",
    "#     / \"applications\" / \"lorenz_model\" / \"examples\" / \"lorenz96\"\n",
    "#     / \"read_results.ipynb\"\n",
    "# )\n",
    "\n",
    "# out_nb = run_postprocess_to_wrapper(\n",
    "#     postprocess_nb,\n",
    "#     wrapper_out_dir=\"icesee_jupyter_book/_static/generated\",\n",
    "#     out_name=\"read_results_report.ipynb\",\n",
    "# )\n",
    "\n",
    "# print(\"Executed report notebook:\", out_nb)\n",
    "# print(\"Mirrored new/changed artifacts into: icesee_jupyter_book/_static/generated/<example>/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # Display generated figures\n",
    "# from pathlib import Path\n",
    "# from IPython.display import display, Image\n",
    "# from utils.notebook_runner import find_repo_root\n",
    "\n",
    "# root = find_repo_root()\n",
    "# # same convention as runner: _static/generated/<tag>/figures\n",
    "# tag = Path.cwd().name\n",
    "# fig_dir = root / \"icesee_jupyter_book\" / \"_static\" / \"generated\" / tag / \"figures\"\n",
    "\n",
    "# pngs = sorted(fig_dir.glob(\"*.png\"))\n",
    "# print(f\"Found {len(pngs)} saved figures in:\", fig_dir)\n",
    "\n",
    "# for p in pngs[-12:]:\n",
    "#     display(Image(filename=str(p)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from IPython.display import Image, display\n",
    "# from pathlib import Path\n",
    "\n",
    "# for f in sorted((Path(\"results\")/\"figures\").glob(\"*.png\")):\n",
    "#     display(Image(filename=str(f)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python3 (icesee1)",
   "language": "python",
   "name": "icesee1"
  },
  "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.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}