{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "623cbc23-45bf-4dfd-9f49-8d814d0394ba",
   "metadata": {
    "papermill": {
     "duration": 0.003627,
     "end_time": "2025-10-28T22:33:37.631558",
     "exception": false,
     "start_time": "2025-10-28T22:33:37.627931",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# ILAMB\n",
    "\n",
    "The output from ILAMB is in the link below.\n",
    "\n",
    "\n",
    "Note that ILAMB is currently run by users via the following process:\n",
    "1) Install ILAMB\n",
    "2) Use the `CUPiD/helper_scripts/generate_ilamb_config_files.py` script to generate ILAMB config files based on a CUPiD configuration file.\n",
    "   * `cd CUPiD/examples/external_diag_packages`\n",
    "   * `../../helper_scripts/generate_ilamb_config_files.py --cupid-config-loc . --run-type RUN_TYPE`\n",
    "4) Run ILAMB with the newly created configuration file.\n",
    "   * `qinteractive -l select=1:ncpus=1:mpiprocs=1:mem=100G -l walltime=06:00:00` (specific for NCAR users)\n",
    "   * follow the printed instructions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "82af7435-3849-4fd1-bb2d-2e062719ec5b",
   "metadata": {
    "editable": true,
    "execution": {
     "iopub.execute_input": "2025-10-28T22:33:37.638353Z",
     "iopub.status.busy": "2025-10-28T22:33:37.638034Z",
     "iopub.status.idle": "2025-10-28T22:33:37.646288Z",
     "shell.execute_reply": "2025-10-28T22:33:37.645759Z"
    },
    "papermill": {
     "duration": 0.012577,
     "end_time": "2025-10-28T22:33:37.647307",
     "exception": false,
     "start_time": "2025-10-28T22:33:37.634730",
     "status": "completed"
    },
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide-cell"
    ]
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from IPython.core.display import HTML, Image\n",
    "from IPython.display import display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2a759f1b-eca7-4eec-a16a-eede1a6ab36c",
   "metadata": {
    "editable": true,
    "execution": {
     "iopub.execute_input": "2025-10-28T22:33:37.653669Z",
     "iopub.status.busy": "2025-10-28T22:33:37.653570Z",
     "iopub.status.idle": "2025-10-28T22:33:37.655945Z",
     "shell.execute_reply": "2025-10-28T22:33:37.655504Z"
    },
    "papermill": {
     "duration": 0.006234,
     "end_time": "2025-10-28T22:33:37.656523",
     "exception": false,
     "start_time": "2025-10-28T22:33:37.650289",
     "status": "completed"
    },
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "parameters",
     "hide-cell"
    ]
   },
   "outputs": [],
   "source": [
    "ilamb_root = \".\"\n",
    "case_name = None\n",
    "base_case_name = None\n",
    "start_date = \"\"\n",
    "end_date = \"\"\n",
    "base_start_date = None\n",
    "base_end_date = None\n",
    "key_plots = None\n",
    "print_table = False\n",
    "# ilamb_root will be external_diag_packages/computed_notebooks/ILAMB/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9e2bfd54",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-10-28T22:33:37.659744Z",
     "iopub.status.busy": "2025-10-28T22:33:37.659540Z",
     "iopub.status.idle": "2025-10-28T22:33:37.665763Z",
     "shell.execute_reply": "2025-10-28T22:33:37.665554Z"
    },
    "papermill": {
     "duration": 0.008341,
     "end_time": "2025-10-28T22:33:37.666198",
     "exception": false,
     "start_time": "2025-10-28T22:33:37.657857",
     "status": "completed"
    },
    "tags": [
     "injected-parameters"
    ]
   },
   "outputs": [],
   "source": [
    "# Parameters\n",
    "case_name = \"b.e30_alpha07c_cesm.B1850C_LTso.ne30_t232_wgx3.232\"\n",
    "base_case_name = \"b.e30_alpha07c_cesm.B1850C_LTso.ne30_t232_wgx3.228\"\n",
    "CESM_output_dir = \"/glade/campaign/cesm/development/cross-wg/diagnostic_framework/CESM_output_for_testing\"\n",
    "start_date = \"0001-01-01\"\n",
    "end_date = \"0021-01-01\"\n",
    "base_start_date = \"0001-01-01\"\n",
    "base_end_date = \"0045-01-01\"\n",
    "obs_data_dir = (\n",
    "    \"/glade/campaign/cesm/development/cross-wg/diagnostic_framework/CUPiD_obs_data\"\n",
    ")\n",
    "ts_dir = None\n",
    "lc_kwargs = {\"threads_per_worker\": 1}\n",
    "serial = False\n",
    "ilamb_root = \"../../examples/key_metrics/ILAMB_output\"\n",
    "key_plots = [\n",
    "    \"EcosystemandCarbonCycle/GrossPrimaryProductivity/FLUXCOM/*_global_bias.png\",\n",
    "    \"EcosystemandCarbonCycle/LeafAreaIndex/AVHRR/*1_global_bias.png\",\n",
    "    \"EcosystemandCarbonCycle/GlobalNetEcosystemCarbonBalance/GCP/*_global_accumulate.png\",\n",
    "    \"HydrologyCycle/Permafrost/Brown2002/*_global_bias.png\",\n",
    "]\n",
    "print_table = True\n",
    "subset_kwargs = {}\n",
    "product = \"/glade/work/richling/CUPid_pr_test/CUPiD/examples/key_metrics/computed_notebooks//lnd/ILAMB.ipynb\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "81bf4573-8806-486f-a2f6-79e244415c07",
   "metadata": {
    "editable": true,
    "execution": {
     "iopub.execute_input": "2025-10-28T22:33:37.669149Z",
     "iopub.status.busy": "2025-10-28T22:33:37.669061Z",
     "iopub.status.idle": "2025-10-28T22:33:37.676068Z",
     "shell.execute_reply": "2025-10-28T22:33:37.675713Z"
    },
    "papermill": {
     "duration": 0.00929,
     "end_time": "2025-10-28T22:33:37.676892",
     "exception": false,
     "start_time": "2025-10-28T22:33:37.667602",
     "status": "completed"
    },
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide-cell"
    ]
   },
   "outputs": [],
   "source": [
    "# Want some base case parameter defaults to equal control case values\n",
    "if base_case_name is not None:\n",
    "    if base_start_date is None:\n",
    "        base_start_date = start_date\n",
    "\n",
    "    if base_end_date is None:\n",
    "        base_end_date = end_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4104f878-a8c3-4d0f-8c00-555e6b2b3273",
   "metadata": {
    "editable": true,
    "execution": {
     "iopub.execute_input": "2025-10-28T22:33:37.682047Z",
     "iopub.status.busy": "2025-10-28T22:33:37.681838Z",
     "iopub.status.idle": "2025-10-28T22:33:37.686502Z",
     "shell.execute_reply": "2025-10-28T22:33:37.686118Z"
    },
    "papermill": {
     "duration": 0.007954,
     "end_time": "2025-10-28T22:33:37.687083",
     "exception": false,
     "start_time": "2025-10-28T22:33:37.679129",
     "status": "completed"
    },
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide-cell"
    ]
   },
   "outputs": [],
   "source": [
    "# convert start-date and end-date to year range\n",
    "case_year_range = [int(start_date.split(\"-\")[0]), int(end_date.split(\"-\")[0]) - 1]\n",
    "\n",
    "base_case_year_range = [\n",
    "    int(base_start_date.split(\"-\")[0]),\n",
    "    int(base_end_date.split(\"-\")[0]) - 1,\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ee0774cc-c68a-4385-b94c-08fa806f28c0",
   "metadata": {
    "editable": true,
    "execution": {
     "iopub.execute_input": "2025-10-28T22:33:37.690117Z",
     "iopub.status.busy": "2025-10-28T22:33:37.690029Z",
     "iopub.status.idle": "2025-10-28T22:33:37.701453Z",
     "shell.execute_reply": "2025-10-28T22:33:37.700694Z"
    },
    "papermill": {
     "duration": 0.013955,
     "end_time": "2025-10-28T22:33:37.702440",
     "exception": false,
     "start_time": "2025-10-28T22:33:37.688485",
     "status": "completed"
    },
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "Full ILAMB output"
      ],
      "text/plain": [
       ""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(HTML('Full ILAMB output'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9234aec-1fe5-4bbf-ad6c-fb565297afbe",
   "metadata": {
    "papermill": {
     "duration": 0.001859,
     "end_time": "2025-10-28T22:33:37.707664",
     "exception": false,
     "start_time": "2025-10-28T22:33:37.705805",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Key Metrics from ILAMB\n",
    "\n",
    "Some important things to look at from ILAMB:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "be819352-c723-4f48-afac-d93f6d192f96",
   "metadata": {
    "editable": true,
    "execution": {
     "iopub.execute_input": "2025-10-28T22:33:37.711173Z",
     "iopub.status.busy": "2025-10-28T22:33:37.710916Z",
     "iopub.status.idle": "2025-10-28T22:33:37.714845Z",
     "shell.execute_reply": "2025-10-28T22:33:37.714282Z"
    },
    "papermill": {
     "duration": 0.006432,
     "end_time": "2025-10-28T22:33:37.715412",
     "exception": false,
     "start_time": "2025-10-28T22:33:37.708980",
     "status": "completed"
    },
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "for path_to_key_plot in key_plots:\n",
    "    full_path = os.path.join(ilamb_root, path_to_key_plot)\n",
    "    if os.path.isfile(full_path):\n",
    "        print(full_path)\n",
    "        display(Image(full_path))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "de0bc64c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-10-28T22:33:37.718612Z",
     "iopub.status.busy": "2025-10-28T22:33:37.718519Z",
     "iopub.status.idle": "2025-10-28T22:33:37.725947Z",
     "shell.execute_reply": "2025-10-28T22:33:37.725451Z"
    },
    "papermill": {
     "duration": 0.009592,
     "end_time": "2025-10-28T22:33:37.726510",
     "exception": false,
     "start_time": "2025-10-28T22:33:37.716918",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/glade/work/richling/conda-envs/cupid-analysis/lib/python3.11/site-packages/IPython/core/display.py:447: UserWarning: Consider using IPython.display.IFrame instead\n",
      "  warnings.warn(\"Consider using IPython.display.IFrame instead\")\n"
     ]
    },
    {
     "data": {
      "text/html": [
       ""
      ],
      "text/plain": [
       ""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "score_chart = os.path.join(ilamb_root, \"index.html\")\n",
    "\n",
    "if print_table:\n",
    "    display(HTML(f''))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cupid-analysis",
   "language": "python",
   "name": "cupid-analysis"
  },
  "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.11.4"
  },
  "papermill": {
   "duration": 0.901387,
   "end_time": "2025-10-28T22:33:37.946303",
   "exception": null,
   "input_path": "/glade/derecho/scratch/richling/tmp/tmpm9bid1mb.ipynb",
   "output_path": "/glade/work/richling/CUPid_pr_test/CUPiD/examples/key_metrics/computed_notebooks/lnd/ILAMB.ipynb",
   "parameters": {
    "CESM_output_dir": "/glade/campaign/cesm/development/cross-wg/diagnostic_framework/CESM_output_for_testing",
    "base_case_name": "b.e30_alpha07c_cesm.B1850C_LTso.ne30_t232_wgx3.228",
    "base_end_date": "0045-01-01",
    "base_start_date": "0001-01-01",
    "case_name": "b.e30_alpha07c_cesm.B1850C_LTso.ne30_t232_wgx3.232",
    "end_date": "0021-01-01",
    "ilamb_root": "../../examples/key_metrics/ILAMB_output",
    "key_plots": [
     "EcosystemandCarbonCycle/GrossPrimaryProductivity/FLUXCOM/*_global_bias.png",
     "EcosystemandCarbonCycle/LeafAreaIndex/AVHRR/*1_global_bias.png",
     "EcosystemandCarbonCycle/GlobalNetEcosystemCarbonBalance/GCP/*_global_accumulate.png",
     "HydrologyCycle/Permafrost/Brown2002/*_global_bias.png"
    ],
    "lc_kwargs": {
     "threads_per_worker": 1
    },
    "obs_data_dir": "/glade/campaign/cesm/development/cross-wg/diagnostic_framework/CUPiD_obs_data",
    "print_table": true,
    "product": "/glade/work/richling/CUPid_pr_test/CUPiD/examples/key_metrics/computed_notebooks//lnd/ILAMB.ipynb",
    "serial": false,
    "start_date": "0001-01-01",
    "subset_kwargs": {},
    "ts_dir": null
   },
   "start_time": "2025-10-28T22:33:37.044916"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}