{ "cells": [ { "cell_type": "markdown", "id": "f30aa9dc", "metadata": {}, "source": [ "# ocean: 1D\n", "\n", "1D ocean time series data\n", "\n", "```{dropdown} **Branding suffix of datasets**\n", "Datasets with the folloing branding suffix are shown in this page: \\\n", "[time]-[vertical]-[horizontal]-[domain]\n", "```\n", "* tavg-u-hm-sea\n", " - tavg: time average\n", " - u: ocean surface\n", " - hm: horizontal mean/sum\n", " - sea: ocean domain" ] }, { "cell_type": "code", "execution_count": null, "id": "f1f107ce", "metadata": { "tags": [ "remove-input" ] }, "outputs": [], "source": [ "## Import libraries\n", "import glob\n", "import logging\n", "import os\n", "\n", "import matplotlib.pyplot as plt\n", "import xarray as xr\n", "from IPython.display import HTML, display\n", "from pathlib import Path\n", "from pyprojroot import here\n", "from xarray.coders import CFDatetimeCoder\n", "\n", "from cmip7validate.utils import read_compound_names, read_variables\n", "\n", "logging.basicConfig(\n", " filename=\"execute.log\",\n", " level=logging.INFO,\n", " format=\"%(asctime)s %(message)s\",\n", " force=True,\n", ")\n", "log = logging.getLogger(\"ocean_1D\")\n", "log.info(\"Starting ocean_1D.ipynb\")\n", "\n", "# Get project root\n", "root_path = Path(here())" ] }, { "cell_type": "code", "execution_count": null, "id": "7c745d65", "metadata": { "tags": [ "remove-input" ] }, "outputs": [], "source": [ "# load parameters\n", "import yaml\n", "\n", "with open(root_path.joinpath('params.yml'), 'r') as f:\n", " param = yaml.safe_load(f)\n", " cmorout = param['cmorout']\n", " source_id = param['source_id']\n", " experiment_id = param['experiment_id']\n", " variant_label = param['variant_label']\n", " version = param['version']" ] }, { "cell_type": "code", "execution_count": null, "id": "8314513c", "metadata": { "tags": [ "remove-input" ] }, "outputs": [], "source": [ "# data path\n", "data_path = Path(cmorout).joinpath(source_id, experiment_id, version)\n", "\n", "# load methods for plotting and set defaults\n", "methods =read_variables(root_path.joinpath('assets/methods.txt'))\n", "#print(methods.keys())" ] }, { "cell_type": "markdown", "id": "f06c51b7", "metadata": {}, "source": [ "---\n", "```{dropdown} **List of datasets:** \n", "List of compound names of datasets that needs to be cmorized. \\\n", "Those datasets which are not cmorized have no link.\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "67a5a359", "metadata": { "tags": [ "remove-input" ] }, "outputs": [], "source": [ "# load compound names\n", "grid_label = 'g???'\n", "coords = ('tavg-u-hm-sea')\n", "cnames = read_compound_names(root_path.joinpath('assets/variables.nml'))\n", "# examples of compound names:\n", "# cnames = ['ocean.sos.tavg-u-hm-sea.mon.glb', 'ocean.masscello.tavg-u-hm-sea.mon.glb']\n", "for cname in cnames:\n", " realm = cname.split('.')[0]\n", " var = cname.split('.')[1]\n", " coord = cname.split('.')[2]\n", " freq = cname.split('.')[3]\n", " region = cname.split('.')[4]\n", " \n", " if realm != 'ocean' or coord not in coords:\n", " continue\n", " else:\n", " file_pattern = f\"{var}_{coord}_{freq}_{region}_{grid_label}_{source_id}_{experiment_id}_{variant_label}_*.nc\"\n", " if not sorted(data_path.glob(file_pattern)):\n", " print(cname)\n", " continue\n", " else:\n", " display(HTML(f'{cname}'))" ] }, { "cell_type": "markdown", "id": "213c2736", "metadata": {}, "source": [ "---\n", "```{dropdown} **Dataset validated:**\n", "Each validated dataset has some text information and some plots (2D and/or 1D)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "25072041", "metadata": { "tags": [ "remove-input" ] }, "outputs": [], "source": [ "\n", "# loop through compound names and plot\n", "for cname in cnames:\n", " realm = cname.split('.')[0]\n", " var = cname.split('.')[1]\n", " coord = cname.split('.')[2]\n", " freq = cname.split('.')[3]\n", " region = cname.split('.')[4]\n", "\n", " if realm != 'ocean' or coord not in coords:\n", " continue\n", "\n", " file_pattern = f\"{var}_{coord}_{freq}_{region}_{grid_label}_{source_id}_{experiment_id}_{variant_label}_*.nc\"\n", " data_files = sorted(data_path.glob(file_pattern))\n", " if data_files:\n", " log.info(f\"{cname}\")\n", " with xr.open_mfdataset(data_files, decode_times=CFDatetimeCoder(use_cftime=True)) as ds:\n", " if var in ds:\n", " data = ds[var]\n", " else:\n", " continue\n", " else:\n", " continue\n", "\n", " data1d = data\n", " display(HTML(f'
'))\n", " print(f'\\033[1m{cname}\\033[0m')\n", " print(f'long name: {data1d.long_name} ({data1d.units})')\n", " print(f'original_name: {data1d.attrs[\"original_name\"]} -> {var}')\n", " if 'history' in data1d.attrs:\n", " print(f'history: {data1d.attrs[\"history\"]}')\n", " if 'comment' in data1d.attrs:\n", " print(f'comment: {data1d.attrs[\"comment\"]}')\n", "\n", " fig = plt.figure(figsize=(16, 3), dpi=96)\n", " ax2 = fig.add_subplot(111)\n", " if data1d.sizes[\"time\"] < 2:\n", " ax2.text(0.1, 0.8, (f\"global value: {data1d.values[0]} {data1d.units}\"))\n", " ax2.text(0.1, 0.6, (\"less than 2 time steps.\"))\n", " ax2.text(0.1, 0.4, (\"skipping timeseries plot.\"))\n", " else:\n", " data1d.plot(ax=ax2)\n", " plt.suptitle(data1d.long_name)\n", "\n", " plt.tight_layout()\n", " plt.show()\n", "\n", " del data, data1d\n", " del ax2, fig" ] } ], "metadata": { "kernelspec": { "display_name": "cmip7validate-env", "language": "python", "name": "python3" }, "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.13.3" } }, "nbformat": 4, "nbformat_minor": 5 }