{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "623cbc23-45bf-4dfd-9f49-8d814d0394ba",
   "metadata": {
    "papermill": {
     "duration": 0.002234,
     "end_time": "2025-10-10T23:07:43.364176",
     "exception": false,
     "start_time": "2025-10-10T23:07:43.361942",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# Link to LDF output\n",
    "\n",
    "The output from the stand-alone LDF configuration is in the link below\n",
    "\n",
    "\n",
    "Note that LDF is currently run by users. Temporarily, we suggest users walk through the following process:\n",
    "1) Install LDF and activate cupid-analysis\n",
    "2) Use the `CUPiD/helper_scripts/generate_ldf_config_file.py` script to generate a LDF config file based on a CUPiD configuration file.\n",
    "   * `cd CUPiD/examples/external_diag_packages`\n",
    "   * `../../helper_scripts/generate_ldf_config_file.py --cupid-config-loc . --ldf-template ../../externals/LDF/config_clm_unstructured_plots.yaml --out-file LDF_config.yaml`\n",
    "2) Run LDF with the newly created configuration file.\n",
    "   * `../../externals/LDF/run_adf_diag LDF_config.yaml`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "82af7435-3849-4fd1-bb2d-2e062719ec5b",
   "metadata": {
    "editable": true,
    "execution": {
     "iopub.execute_input": "2025-10-10T23:07:43.369274Z",
     "iopub.status.busy": "2025-10-10T23:07:43.369045Z",
     "iopub.status.idle": "2025-10-10T23:07:46.071802Z",
     "shell.execute_reply": "2025-10-10T23:07:46.071315Z"
    },
    "papermill": {
     "duration": 2.706462,
     "end_time": "2025-10-10T23:07:46.072803",
     "exception": false,
     "start_time": "2025-10-10T23:07:43.366341",
     "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\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2a759f1b-eca7-4eec-a16a-eede1a6ab36c",
   "metadata": {
    "editable": true,
    "execution": {
     "iopub.execute_input": "2025-10-10T23:07:46.077891Z",
     "iopub.status.busy": "2025-10-10T23:07:46.077633Z",
     "iopub.status.idle": "2025-10-10T23:07:46.080389Z",
     "shell.execute_reply": "2025-10-10T23:07:46.080006Z"
    },
    "papermill": {
     "duration": 0.005915,
     "end_time": "2025-10-10T23:07:46.080992",
     "exception": false,
     "start_time": "2025-10-10T23:07:46.075077",
     "status": "completed"
    },
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "parameters",
     "hide-cell"
    ]
   },
   "outputs": [],
   "source": [
    "ldf_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",
    "# ldf_root will be external_diag_packages/computed_notebooks/LDF/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a9598188",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-10-10T23:07:46.085314Z",
     "iopub.status.busy": "2025-10-10T23:07:46.085136Z",
     "iopub.status.idle": "2025-10-10T23:07:46.093010Z",
     "shell.execute_reply": "2025-10-10T23:07:46.092600Z"
    },
    "papermill": {
     "duration": 0.011056,
     "end_time": "2025-10-10T23:07:46.093914",
     "exception": false,
     "start_time": "2025-10-10T23:07:46.082858",
     "status": "completed"
    },
    "tags": [
     "injected-parameters"
    ]
   },
   "outputs": [],
   "source": [
    "# Parameters\n",
    "case_name = \"b.e30_beta02.BLT1850.ne30_t232.104\"\n",
    "base_case_name = \"b.e23_alpha17f.BLT1850.ne30_t232.092\"\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 = \"0101-01-01\"\n",
    "base_start_date = \"0001-01-01\"\n",
    "base_end_date = \"0101-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",
    "ldf_root = \"../../examples/key_metrics/LDF_output/\"\n",
    "key_plots = [\n",
    "    \"GPP_ANN_LatLon_Mean.png\",\n",
    "    \"ELAI_ANN_LatLon_Mean.png\",\n",
    "    \"TOTECOSYSC_GlobalMean_ANN_TimeSeries_Mean.png\",\n",
    "    \"RegionalClimo_Amazonia_RegionalClimo_Mean.png\",\n",
    "]\n",
    "subset_kwargs = {}\n",
    "product = \"/glade/work/richling/CUPid_pr_test/CUPiD/examples/key_metrics/computed_notebooks//lnd/link_to_LDF.ipynb\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "81bf4573-8806-486f-a2f6-79e244415c07",
   "metadata": {
    "editable": true,
    "execution": {
     "iopub.execute_input": "2025-10-10T23:07:46.099341Z",
     "iopub.status.busy": "2025-10-10T23:07:46.099155Z",
     "iopub.status.idle": "2025-10-10T23:07:46.106248Z",
     "shell.execute_reply": "2025-10-10T23:07:46.105762Z"
    },
    "papermill": {
     "duration": 0.010552,
     "end_time": "2025-10-10T23:07:46.107181",
     "exception": false,
     "start_time": "2025-10-10T23:07:46.096629",
     "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-10T23:07:46.111933Z",
     "iopub.status.busy": "2025-10-10T23:07:46.111752Z",
     "iopub.status.idle": "2025-10-10T23:07:46.119450Z",
     "shell.execute_reply": "2025-10-10T23:07:46.118811Z"
    },
    "papermill": {
     "duration": 0.011092,
     "end_time": "2025-10-10T23:07:46.120369",
     "exception": false,
     "start_time": "2025-10-10T23:07:46.109277",
     "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-10T23:07:46.125282Z",
     "iopub.status.busy": "2025-10-10T23:07:46.125068Z",
     "iopub.status.idle": "2025-10-10T23:07:46.133628Z",
     "shell.execute_reply": "2025-10-10T23:07:46.133124Z"
    },
    "papermill": {
     "duration": 0.012054,
     "end_time": "2025-10-10T23:07:46.134469",
     "exception": false,
     "start_time": "2025-10-10T23:07:46.122415",
     "status": "completed"
    },
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "Full LDF output"
      ],
      "text/plain": [
       ""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ldf_comparison_name = f\"{case_name}_{case_year_range[0]}_{case_year_range[1]}_vs_{base_case_name}_{base_case_year_range[0]}_{base_case_year_range[1]}\"\n",
    "ldf_root = os.path.join(ldf_root, ldf_comparison_name)\n",
    "display(\n",
    "    HTML(\n",
    "        f'Full LDF output'\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9234aec-1fe5-4bbf-ad6c-fb565297afbe",
   "metadata": {
    "papermill": {
     "duration": 0.002063,
     "end_time": "2025-10-10T23:07:46.138796",
     "exception": false,
     "start_time": "2025-10-10T23:07:46.136733",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Key Metrics from LDF\n",
    "\n",
    "Some important things to look at from LDF include a comparison table and a few maps:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a5b02451-98ad-4089-b184-75391a262db3",
   "metadata": {
    "editable": true,
    "execution": {
     "iopub.execute_input": "2025-10-10T23:07:46.143746Z",
     "iopub.status.busy": "2025-10-10T23:07:46.143456Z",
     "iopub.status.idle": "2025-10-10T23:07:46.223884Z",
     "shell.execute_reply": "2025-10-10T23:07:46.223380Z"
    },
    "papermill": {
     "duration": 0.083906,
     "end_time": "2025-10-10T23:07:46.224688",
     "exception": false,
     "start_time": "2025-10-10T23:07:46.140782",
     "status": "completed"
    },
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "  \n",
       "    \n",
       "      | variable | \n",
       "      unit | \n",
       "      test | \n",
       "      control | \n",
       "      diff | \n",
       "    
\n",
       "  \n",
       "  \n",
       "    \n",
       "      | TSA | \n",
       "      K | \n",
       "      281.593 | \n",
       "      281.916 | \n",
       "      -0.323 | \n",
       "    
\n",
       "    \n",
       "      | PREC | \n",
       "      mm d$^{-1}$ | \n",
       "      2.412 | \n",
       "      2.392 | \n",
       "      0.02 | \n",
       "    
\n",
       "    \n",
       "      | ELAI | \n",
       "      m${-2}$ m${-2}$ | \n",
       "      1.48 | \n",
       "      1.471 | \n",
       "      0.009 | \n",
       "    
\n",
       "    \n",
       "      | FSDS | \n",
       "      W m$^{-2}$ | \n",
       "      198.272 | \n",
       "      200.666 | \n",
       "      -2.394 | \n",
       "    
\n",
       "    \n",
       "      | FLDS | \n",
       "      W m$^{-2}$ | \n",
       "      299.518 | \n",
       "      302.207 | \n",
       "      -2.689 | \n",
       "    
\n",
       "    \n",
       "      | ASA | \n",
       "      % reflected | \n",
       "      28.186 | \n",
       "      28.186 | \n",
       "      0 | \n",
       "    
\n",
       "    \n",
       "      | RNET | \n",
       "      W m$^{-2}$ | \n",
       "      71.018 | \n",
       "      73.295 | \n",
       "      -2.277 | \n",
       "    
\n",
       "    \n",
       "      | FSH | \n",
       "      W m$^{-2}$ | \n",
       "      30.608 | \n",
       "      32.922 | \n",
       "      -2.314 | \n",
       "    
\n",
       "    \n",
       "      | ET | \n",
       "      W m$^{-2}$ | \n",
       "      39.64 | \n",
       "      39.592 | \n",
       "      0.048 | \n",
       "    
\n",
       "    \n",
       "      | FCTR | \n",
       "      W m$^{-2}$ | \n",
       "      19.773 | \n",
       "      19.957 | \n",
       "      -0.184 | \n",
       "    
\n",
       "    \n",
       "      | FGEV | \n",
       "      W m$^{-2}$ | \n",
       "      12.459 | \n",
       "      12.203 | \n",
       "      0.256 | \n",
       "    
\n",
       "    \n",
       "      | FCEV | \n",
       "      W m$^{-2}$ | \n",
       "      7.408 | \n",
       "      7.431 | \n",
       "      -0.023 | \n",
       "    
\n",
       "    \n",
       "      | QRUNOFF_TO_COUPLER | \n",
       "      mm d$^{-1}$ | \n",
       "      0.995 | \n",
       "      0.974 | \n",
       "      0.021 | \n",
       "    
\n",
       "    \n",
       "      | SNOWDP | \n",
       "      m | \n",
       "      2.223 | \n",
       "      2.241 | \n",
       "      -0.018 | \n",
       "    
\n",
       "    \n",
       "      | GPP | \n",
       "      PgC y$^{-1}$ | \n",
       "      110.783 | \n",
       "      110.926 | \n",
       "      -0.143 | \n",
       "    
\n",
       "    \n",
       "      | NPP | \n",
       "      PgC y$^{-1}$ | \n",
       "      47.122 | \n",
       "      47.036 | \n",
       "      0.086 | \n",
       "    
\n",
       "    \n",
       "      | NEE | \n",
       "      PgC y$^{-1}$ | \n",
       "      -0.678 | \n",
       "      -0.553 | \n",
       "      -0.125 | \n",
       "    
\n",
       "    \n",
       "      | NBP | \n",
       "      PgC y$^{-1}$ | \n",
       "      0.136 | \n",
       "      0.0306 | \n",
       "      0.105 | \n",
       "    
\n",
       "    \n",
       "      | BTRANMN | \n",
       "      unitless | \n",
       "      0.574 | \n",
       "      0.574 | \n",
       "      0 | \n",
       "    
\n",
       "    \n",
       "      | TOTVEGC | \n",
       "      PgC | \n",
       "      741.471 | \n",
       "      722.481 | \n",
       "      18.99 | \n",
       "    
\n",
       "    \n",
       "      | TOTECOSYSC | \n",
       "      gC/m^2 | \n",
       "      16462.9 | \n",
       "      16592 | \n",
       "      -129.106 | \n",
       "    
\n",
       "    \n",
       "      | TOTSOMC_1m | \n",
       "      PgC | \n",
       "      1001.85 | \n",
       "      1026.08 | \n",
       "      -24.231 | \n",
       "    
\n",
       "    \n",
       "      | ALTMAX | \n",
       "      m | \n",
       "      35.226 | \n",
       "      34.692 | \n",
       "      0.534 | \n",
       "    
\n",
       "    \n",
       "      | FAREA_BURNED | \n",
       "      s-1 | \n",
       "      1.58e-09 | \n",
       "      1.68e-09 | \n",
       "      -1e-10 | \n",
       "    
\n",
       "    \n",
       "      | DSTFLXT | \n",
       "      Pg y$^{-1}$ | \n",
       "      48.346 | \n",
       "      46.916 | \n",
       "      1.43 | \n",
       "    
\n",
       "    \n",
       "      | MEG_isoprene | \n",
       "      Tg y$^{-1}$ | \n",
       "      554.114 | \n",
       "      564.054 | \n",
       "      -9.94 | \n",
       "    
\n",
       "  \n",
       "
"
      ],
      "text/plain": [
       ""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "comparison_table = os.path.join(ldf_root, \"amwg_table_comp.csv\")\n",
    "if os.path.isfile(comparison_table):\n",
    "    table = pd.read_csv(comparison_table)\n",
    "    display(HTML(table.to_html(index=False, float_format=\"{:6g}\".format)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "be819352-c723-4f48-afac-d93f6d192f96",
   "metadata": {
    "editable": true,
    "execution": {
     "iopub.execute_input": "2025-10-10T23:07:46.230846Z",
     "iopub.status.busy": "2025-10-10T23:07:46.230613Z",
     "iopub.status.idle": "2025-10-10T23:07:46.233966Z",
     "shell.execute_reply": "2025-10-10T23:07:46.233506Z"
    },
    "papermill": {
     "duration": 0.007215,
     "end_time": "2025-10-10T23:07:46.234766",
     "exception": false,
     "start_time": "2025-10-10T23:07:46.227551",
     "status": "completed"
    },
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "for path_to_key_plot in key_plots:\n",
    "    full_path = os.path.join(ldf_root, path_to_key_plot)\n",
    "    if os.path.isfile(full_path):\n",
    "        display(Image(full_path))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cupid-infrastructure",
   "language": "python",
   "name": "cupid-infrastructure"
  },
  "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": 4.142344,
   "end_time": "2025-10-10T23:07:46.553530",
   "exception": null,
   "input_path": "/glade/derecho/scratch/richling/tmp/tmptbdcjcfv.ipynb",
   "output_path": "/glade/work/richling/CUPid_pr_test/CUPiD/examples/key_metrics/computed_notebooks/lnd/link_to_LDF.ipynb",
   "parameters": {
    "CESM_output_dir": "/glade/campaign/cesm/development/cross-wg/diagnostic_framework/CESM_output_for_testing",
    "base_case_name": "b.e23_alpha17f.BLT1850.ne30_t232.092",
    "base_end_date": "0101-01-01",
    "base_start_date": "0001-01-01",
    "case_name": "b.e30_beta02.BLT1850.ne30_t232.104",
    "end_date": "0101-01-01",
    "key_plots": [
     "GPP_ANN_LatLon_Mean.png",
     "ELAI_ANN_LatLon_Mean.png",
     "TOTECOSYSC_GlobalMean_ANN_TimeSeries_Mean.png",
     "RegionalClimo_Amazonia_RegionalClimo_Mean.png"
    ],
    "lc_kwargs": {
     "threads_per_worker": 1
    },
    "ldf_root": "../../examples/key_metrics/LDF_output/",
    "obs_data_dir": "/glade/campaign/cesm/development/cross-wg/diagnostic_framework/CUPiD_obs_data",
    "product": "/glade/work/richling/CUPid_pr_test/CUPiD/examples/key_metrics/computed_notebooks//lnd/link_to_LDF.ipynb",
    "serial": false,
    "start_date": "0001-01-01",
    "subset_kwargs": {},
    "ts_dir": null
   },
   "start_time": "2025-10-10T23:07:42.411186"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}