{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Horizontal Mean difference and RMS: model versus observations\n", "\n", "`mom6_tools.horizontalMean` collection of functions for computing horizontal mean of **difference** and **rms** (model versus obs). This notebook servers as an example on how to compute the following operations:\n", "\n", "\n", "$$diff(t,z)= A_{TOT}(z)^{-1}\\sum_{i=1}^n (y_i(z) - \\hat{y_i(x,y,z)}) A_i(z),$$\n", "\n", "$$rms(t,z)= [A_{TOT}(z)^{-1}\\sum_{i=1}^n (y_i(z) - \\hat{y_i}(x,y,z))^2 A_i(z)]^{1/2},$$\n", "\n", "where $y$(z) is the model output at point $i$ and level $z$, $\\hat{y}(z)$ is the observation at point $i$ and level $z$, $n$ is the total number of grid points in the horizontal (i.e., NX x NY), $A_{i}(z)$ is the area of grid cell $i$ at level $z$, and $A_{TOT}(z) = \\sum_{i=1}^n A_i(z)$ is the total ocean area at level z. \n", "\n", "**Important**:\n", "\n", "With the porpuses of calculating T and S changes at specific regions, $A_{i}(z)$ is multiplied by basin masks generated via ``mom6_tools.m6toolbox.genBasinMasks``. See [notebook](https://mom6-tools.readthedocs.io/en/latest/examples/region_masks.html) showing how to generate these masks. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "ERROR 1: PROJ: proj_create_from_database: Open of /glade/work/gmarques/conda-envs/mom6-tools/share/proj failed\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Basemap module not found. Some regional plots may not function properly\n" ] } ], "source": [ "import warnings\n", "warnings.filterwarnings(\"ignore\")\n", "from mom6_tools.MOM6grid import MOM6grid\n", "from mom6_tools.drift import HorizontalMeanDiff_da \n", "from mom6_tools.m6plot import ztplot\n", "from ncar_jobqueue import NCARCluster\n", "from dask.distributed import Client\n", "from mom6_tools.m6toolbox import genBasinMasks, request_workers \n", "from mom6_tools.m6toolbox import weighted_temporal_mean, add_global_attrs\n", "from mom6_tools.m6toolbox import cime_xmlquery\n", "from IPython.display import display, Markdown, Latex\n", "import yaml, intake, os\n", "import xarray as xr\n", "import matplotlib\n", "import numpy as np\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Read in the yaml file\n", "diag_config_yml_path = \"diag_config.yml\"\n", "diag_config_yml = yaml.load(open(diag_config_yml_path,'r'), Loader=yaml.Loader)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Output directory is: /glade/derecho/scratch/gmarques/archive/g.e30_a03c.GJRAv4.TL319_t232_wgx3_hycom1_N75.2024.079/ocn/hist/\n", "Casename is: g.e30_a03c.GJRAv4.TL319_t232_wgx3_hycom1_N75.2024.079\n" ] } ], "source": [ "caseroot = diag_config_yml['Case']['CASEROOT']\n", "casename = cime_xmlquery(caseroot, 'CASE')\n", "DOUT_S = cime_xmlquery(caseroot, 'DOUT_S')\n", "if DOUT_S:\n", " OUTDIR = cime_xmlquery(caseroot, 'DOUT_S_ROOT')+'/ocn/hist/'\n", "else:\n", " OUTDIR = cime_xmlquery(caseroot, 'RUNDIR')\n", "\n", "print('Output directory is:', OUTDIR)\n", "print('Casename is:', casename)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [ "parameters" ] }, "outputs": [], "source": [ "# The following parameters must be set accordingly\n", "######################################################\n", "\n", "# create an empty class object\n", "class args:\n", " pass\n", "\n", "# set avg dates\n", "avg = diag_config_yml['Avg']\n", "\n", "args.start_date = avg['start_date']\n", "args.end_date = avg['end_date']\n", "args.casename = casename\n", "args.obs = \"woa-2018-tx2_3v2-annual-all\"\n", "args.z = casename+diag_config_yml['Fnames']['z']\n", "args.static = casename+diag_config_yml['Fnames']['static']\n", "args.geom = casename+diag_config_yml['Fnames']['geom']\n", "args.savefigs = False\n", "args.nw = 6 # requesting 6 workers" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "MOM6 grid successfully loaded... \n", "\n" ] } ], "source": [ "# read grid info\n", "geom_file = OUTDIR+'/'+args.geom\n", "if os.path.exists(geom_file):\n", " grd = MOM6grid(OUTDIR+'/'+args.static, geom_file, xrformat=True)\n", "else:\n", " grd = MOM6grid(OUTDIR+'/'+args.static, xrformat=True)\n", "\n", "try:\n", " depth = grd.depth_ocean.values\n", "except:\n", " depth = grd.deptho.values\n", "\n", "try:\n", " area = grd.area_t.where(grd.wet > 0)\n", "except:\n", " area = grd.areacello.where(grd.wet > 0)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# remote Nan's, otherwise genBasinMasks won't work\n", "depth[np.isnan(depth)] = 0.0\n", "basin_code = genBasinMasks(grd.geolon.values, grd.geolat.values, depth, xda=True)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Client-8bf804b7-9e39-11ef-8874-3cecef1b11de
\n", "| Connection method: Cluster object | \n", "Cluster type: dask_jobqueue.PBSCluster | \n", " \n", "
| \n", " Dashboard: https://jupyterhub.hpc.ucar.edu/stable/user/gmarques/High-mem/proxy/39829/status\n", " | \n", "\n", " |
ebae306e
\n", "| \n", " Dashboard: https://jupyterhub.hpc.ucar.edu/stable/user/gmarques/High-mem/proxy/39829/status\n", " | \n", "\n", " Workers: 0\n", " | \n", "
| \n", " Total threads: 0\n", " | \n", "\n", " Total memory: 0 B\n", " | \n", "
Scheduler-53d463cd-de04-4f6f-a257-a46f54ad84cf
\n", "| \n", " Comm: tcp://128.117.208.100:45269\n", " | \n", "\n", " Workers: 0\n", " | \n", "
| \n", " Dashboard: https://jupyterhub.hpc.ucar.edu/stable/user/gmarques/High-mem/proxy/39829/status\n", " | \n", "\n", " Total threads: 0\n", " | \n", "
| \n", " Started: Just now\n", " | \n", "\n", " Total memory: 0 B\n", " | \n", "