{ "cells": [ { "cell_type": "markdown", "id": "8b364e86-fc6c-4765-ba0a-fcb43cdf3e18", "metadata": {}, "source": [ "# `CRO` fitting with CMIP6 models\n", "\n", "This tutorial illustrates how to use the `pyCRO` to estimate RO parameters from all CMIP6 models\n", "\n", "This notebook was contributed by [Sen Zhao](https://senzhao.netlify.app/).\n" ] }, { "cell_type": "markdown", "id": "0f4e2cb2-dd0b-4bd2-a3c0-2ef8ce14f7e2", "metadata": {}, "source": [ "## Load library" ] }, { "cell_type": "code", "execution_count": 1, "id": "e89a1089", "metadata": { "execution": { "iopub.execute_input": "2026-07-06T02:51:26.631710Z", "iopub.status.busy": "2026-07-06T02:51:26.631573Z", "iopub.status.idle": "2026-07-06T02:51:28.234319Z", "shell.execute_reply": "2026-07-06T02:51:28.233856Z", "shell.execute_reply.started": "2026-07-06T02:51:26.631696Z" }, "lines_to_next_cell": 2 }, "outputs": [], "source": [ "%config IPCompleter.greedy = True\n", "%matplotlib inline\n", "%config InlineBackend.figure_format='retina'\n", "\n", "%load_ext autoreload\n", "%autoreload 2\n", "\n", "import os\n", "import sys\n", " \n", "import numpy as np\n", "import xarray as xr\n", "import matplotlib.pyplot as plt\n", "\n", "## comment this if you install pyCRO already\n", "sys.path.append(os.path.abspath(\"../../../\"))\n", "\n", "import pyCRO" ] }, { "cell_type": "markdown", "id": "376b849b-f178-4ac1-b2ef-5b642efca915", "metadata": {}, "source": [ "## Load ENSO timeseries from CMIP6 models" ] }, { "cell_type": "code", "execution_count": 2, "id": "66ff7c12", "metadata": { "execution": { "iopub.execute_input": "2026-07-06T02:51:28.235118Z", "iopub.status.busy": "2026-07-06T02:51:28.234828Z", "iopub.status.idle": "2026-07-06T02:51:29.077771Z", "shell.execute_reply": "2026-07-06T02:51:29.077368Z", "shell.execute_reply.started": "2026-07-06T02:51:28.235101Z" }, "lines_to_next_cell": 2 }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 1MB\n",
       "Dimensions:  (model: 48, time: 1332)\n",
       "Coordinates:\n",
       "  * time     (time) datetime64[ns] 11kB 1900-01-01 1900-02-01 ... 2010-12-01\n",
       "  * model    (model) <U15 3kB 'ACCESS-CM2' 'ACCESS-ESM1-5' ... 'UKESM1-0-LL'\n",
       "Data variables:\n",
       "    Nino3    (model, time) float32 256kB ...\n",
       "    Nino34   (model, time) float32 256kB ...\n",
       "    Hm       (model, time) float32 256kB ...\n",
       "    Hw       (model, time) float32 256kB ...
" ], "text/plain": [ " Size: 1MB\n", "Dimensions: (model: 48, time: 1332)\n", "Coordinates:\n", " * time (time) datetime64[ns] 11kB 1900-01-01 1900-02-01 ... 2010-12-01\n", " * model (model) 'ac_0'\n", " len=3 -> 'ac_1'\n", " len=5 -> 'ac_2'\n", " Empty lists are replaced with np.nan (float).\n", "\n", " Parameters\n", " ----------\n", " fit_dict : dict\n", " Dictionary where keys are parameter names and values are lists.\n", "\n", " Returns\n", " -------\n", " xr.Dataset\n", " \"\"\"\n", " data_vars = {}\n", "\n", " for k, v in fit_dict.items():\n", " if len(v) == 0:\n", " arr = np.array([np.nan], dtype=float)\n", " dim = \"ac_0\"\n", " elif len(v) == 1:\n", " arr = np.array(v, dtype=float)\n", " dim = \"ac_0\"\n", " elif len(v) == 3:\n", " arr = np.array(v, dtype=float)\n", " dim = \"ac_1\"\n", " elif len(v) == 5:\n", " arr = np.array(v, dtype=float)\n", " dim = \"ac_2\"\n", " else:\n", " # Other lengths: convert to float, pad with nan if needed\n", " arr = np.array(v, dtype=float)\n", " dim = f\"ac_{len(v)}\"\n", "\n", " data_vars[k] = (dim, arr)\n", "\n", " return xr.Dataset(data_vars)\n", "\n", "\n", "def CRO_cmip6_fitting(par_option_T, par_option_h, par_option_noise, verbose=False):\n", " \"\"\"\n", " fit all cmip6 models with the same T, h, and noise options\n", " \"\"\"\n", "\n", " for i, model in enumerate(cmip_ds.model.values):\n", " par_tmp = pyCRO.RO_fitting(cmip_ds['Nino34'].sel(model=model), cmip_ds['Hm'].sel(model=model), \n", " par_option_T, par_option_h, par_option_noise, verbose=verbose) \n", " par_ds = fit_dict_to_dataset(par_tmp).assign_coords({'model': model})\n", "\n", " if i==0:\n", " par_cmip = par_ds\n", " else:\n", " par_cmip = xr.concat([par_cmip, par_ds], dim='model')\n", " return par_cmip\n" ] }, { "cell_type": "markdown", "id": "c6719e72-7047-49a3-89c4-6f8248a8a80a", "metadata": {}, "source": [ "### Type 1: Linear-White-Additive\n", "\n", "Fit linear RO with white and additive noise \n" ] }, { "cell_type": "code", "execution_count": 4, "id": "dd801730-5301-4117-8c93-3af9f034fd46", "metadata": { "execution": { "iopub.execute_input": "2026-07-06T02:51:29.098876Z", "iopub.status.busy": "2026-07-06T02:51:29.098732Z", "iopub.status.idle": "2026-07-06T02:51:29.620216Z", "shell.execute_reply": "2026-07-06T02:51:29.619841Z", "shell.execute_reply.started": "2026-07-06T02:51:29.098863Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 9kB\n",
       "Dimensions:  (model: 48, ac_0: 1)\n",
       "Coordinates:\n",
       "  * model    (model) <U15 3kB 'ACCESS-CM2' 'ACCESS-ESM1-5' ... 'UKESM1-0-LL'\n",
       "Dimensions without coordinates: ac_0\n",
       "Data variables: (12/16)\n",
       "    R        (model, ac_0) float64 384B -0.0277 0.04347 ... 0.07818 0.006757\n",
       "    F1       (model, ac_0) float64 384B 0.03008 0.0195 ... 0.03125 0.02193\n",
       "    F2       (model, ac_0) float64 384B 1.138 1.671 1.259 ... 1.106 1.602 1.103\n",
       "    epsilon  (model, ac_0) float64 384B 0.08306 0.1292 0.1211 ... 0.1831 0.09856\n",
       "    b_T      (model, ac_0) float64 384B nan nan nan nan nan ... nan nan nan nan\n",
       "    c_T      (model, ac_0) float64 384B nan nan nan nan nan ... nan nan nan nan\n",
       "    ...       ...\n",
       "    B        (model, ac_0) float64 384B nan nan nan nan nan ... nan nan nan nan\n",
       "    m_T      (model, ac_0) float64 384B nan nan nan nan nan ... nan nan nan nan\n",
       "    m_h      (model, ac_0) float64 384B nan nan nan nan nan ... nan nan nan nan\n",
       "    n_T      (model, ac_0) float64 384B 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0\n",
       "    n_h      (model, ac_0) float64 384B 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0\n",
       "    n_g      (model, ac_0) float64 384B 2.0 2.0 2.0 2.0 2.0 ... 2.0 2.0 2.0 2.0
" ], "text/plain": [ " Size: 9kB\n", "Dimensions: (model: 48, ac_0: 1)\n", "Coordinates:\n", " * model (model) \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 9kB\n",
       "Dimensions:  (model: 48, ac_0: 1)\n",
       "Coordinates:\n",
       "  * model    (model) <U15 3kB 'ACCESS-CM2' 'ACCESS-ESM1-5' ... 'UKESM1-0-LL'\n",
       "Dimensions without coordinates: ac_0\n",
       "Data variables: (12/16)\n",
       "    R        (model, ac_0) float64 384B -0.02369 0.06183 ... 0.1074 0.0235\n",
       "    F1       (model, ac_0) float64 384B 0.03023 0.01987 ... 0.0325 0.02317\n",
       "    F2       (model, ac_0) float64 384B 1.129 1.654 1.259 ... 1.105 1.605 1.109\n",
       "    epsilon  (model, ac_0) float64 384B 0.08266 0.1288 0.1332 ... 0.187 0.1002\n",
       "    b_T      (model, ac_0) float64 384B 0.01088 -0.002446 ... 0.03614 0.03063\n",
       "    c_T      (model, ac_0) float64 384B 0.0006461 0.00857 ... 0.005134 0.002772\n",
       "    ...       ...\n",
       "    B        (model, ac_0) float64 384B nan nan nan nan nan ... nan nan nan nan\n",
       "    m_T      (model, ac_0) float64 384B nan nan nan nan nan ... nan nan nan nan\n",
       "    m_h      (model, ac_0) float64 384B nan nan nan nan nan ... nan nan nan nan\n",
       "    n_T      (model, ac_0) float64 384B 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0\n",
       "    n_h      (model, ac_0) float64 384B 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0\n",
       "    n_g      (model, ac_0) float64 384B 2.0 2.0 2.0 2.0 2.0 ... 2.0 2.0 2.0 2.0
" ], "text/plain": [ " Size: 9kB\n", "Dimensions: (model: 48, ac_0: 1)\n", "Coordinates:\n", " * model (model) " ] }, "metadata": { "image/png": { "height": 411, "width": 811 } }, "output_type": "display_data" } ], "source": [ "fig, axes = plt.subplots(1, 2, figsize=(8, 4), sharey=True, layout='compressed')\n", "\n", "ax = axes[0]\n", "ax.scatter((par_cmip_LWA['R']-par_cmip_LWA['epsilon'])*0.5, cmip_SD, label='Linear RO')\n", "ax.scatter((par_cmip_NWA['R']-par_cmip_NWA['epsilon'])*0.5, cmip_SD, label='Nonlinear RO')\n", "ax.set_xlabel('BJ (month$^{-1}$)')\n", "ax.set_ylabel(r\"Nino34 index variance (K$^2$)\")\n", "ax.legend(fontsize=9)\n", "\n", "ax = axes[1]\n", "ax.scatter(par_cmip_LWA['sigma_T'], cmip_SD, label='Linear RO')\n", "ax.scatter(par_cmip_NWA['sigma_T'], cmip_SD, label='Nonlinear RO')\n", "ax.set_xlabel(r\"$\\sigma_T$ (K month$^{-1/2}$)\")\n" ] }, { "cell_type": "markdown", "id": "fbc24925-7f1f-429b-897e-d8d23f1bbe58", "metadata": {}, "source": [ "**The ENSO amplitude is highly related the growth rate (BJ index) and a less degree to noise amplitude.**\n" ] }, { "cell_type": "code", "execution_count": null, "id": "c5938c9e-707f-423d-8009-ae7ef6abdb7e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" }, "kernelspec": { "display_name": "Python 3 (geodata)", "language": "python", "name": "env_geodata" }, "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.13" } }, "nbformat": 4, "nbformat_minor": 5 }