{
"cells": [
{
"cell_type": "markdown",
"id": "fa561492",
"metadata": {
"tags": []
},
"source": [
"
\n",
"\n",
"
\n",
" \n",
" Exercise: \n",
" In this exercise, we will develop the plugin step by step. Some parts will be provided, and you will be asked to implement the necessary missing components:\n",
" \n",
" \n",
" Access the surface pressure ( | \n",
" \n",
" ![]() | \n",
"
scripts/comin_plugin_P3.py
, step by step. This will happen automatically as we run the cells. To do this, we'll define a custom Jupyter cell magic command in append_magic.py
."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6dc53ee8-efc4-4d7a-b225-88cf85dfd658",
"metadata": {},
"outputs": [],
"source": [
"%load_ext append_magic"
]
},
{
"cell_type": "markdown",
"id": "1f7b7404-b1a1-4b92-9371-212f873bb544",
"metadata": {},
"source": [
"First steps:\n",
"\n",
"- We need to import the necessary packages, most importantly, `yac` and `comin`.\n",
"- We get the neccessary descriptive data.\n",
"- We query the YAC instance ID. This is the predefined identifier used to reference the YAC instance for defining components, grids, and times in the coupling framework."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "54e0951d-bf3c-442a-be61-fc73a40a2a11",
"metadata": {},
"outputs": [],
"source": [
"%%append_to_script scripts/comin_plugin_P3.py --reset\n",
"\n",
"from yac import (\n",
" YAC, Reg2dGrid, Location, Field, TimeUnit, Reduction,\n",
" InterpolationStack, NNNReductionType, UnstructuredGrid\n",
")\n",
"import comin\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
" \n",
"# === GET DESCRIPTIVE DATA STRUCTURE ===\n",
"\n",
"domain = comin.descrdata_get_domain(1) # DOMAIN 1\n",
"glob = comin.descrdata_get_global()\n",
"rank = comin.parallel_get_host_mpi_rank()\n",
"\n",
"assert glob.yac_instance_id != -1, \"The host-model is not configured with YAC\"\n",
"yac = YAC.from_id(glob.yac_instance_id)"
]
},
{
"cell_type": "markdown",
"id": "b389fd43-82ef-489c-896e-683cf9290899",
"metadata": {},
"source": [
"We then define the source grid and component. **YAC components** are the individual Earth system model components - such as atmosphere, ocean, or an output writer - that are coupled together to exchange data fields through the YAC library. \n",
"\n",
"The source component will be based on ICON's computational grid and the following code snippets show how to define such an unstructured mesh with the YAC coupler."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a79926b9-05be-4615-ab82-7b24d459e110",
"metadata": {},
"outputs": [],
"source": [
"%%append_to_script scripts/comin_plugin_P3.py\n",
"\n",
"# === DEFINE SOURCE GRID AND COMPONENT ===\n",
"\n",
"source_component = yac.predef_comp(\"comin_example_source\")\n",
"\n",
"ncells = domain.cells.ncells\n",
"nverts = domain.verts.nverts\n",
"\n",
"# the \"connectivity\" field contains the three vertices for each cell:\n",
"connectivity = (np.asarray(domain.cells.vertex_blk) - 1) * glob.nproma + (\n",
" np.asarray(domain.cells.vertex_idx) - 1\n",
")\n",
"\n",
"# We also provide the geographical coordinates for each vertex.\n",
"# Please note that we need to specify the 'order=F' argument\n",
"# because Fortran (ICON) uses column-major ordering.\n",
"icon_grid = UnstructuredGrid(\n",
" \"icon_grid\",\n",
" np.ones(domain.cells.ncells) * 3,\n",
" np.ravel(domain.verts.vlon, order='F')[: domain.verts.nverts],\n",
" np.ravel(domain.verts.vlat, order='F')[: domain.verts.nverts],\n",
" np.ravel(np.swapaxes(connectivity, 0, 1))[: 3 * domain.cells.ncells],\n",
")\n",
"\n",
"# \"def_points\": This method is used to specify the locations (such as corners,\n",
"# centers, or edges) of grid points for coupling fields between components.\n",
"icon_cell_centers = icon_grid.def_points(\n",
" Location.CELL,\n",
" np.ravel(domain.cells.clon)[:ncells],\n",
" np.ravel(domain.cells.clat)[:ncells],\n",
")"
]
},
{
"cell_type": "markdown",
"id": "590b8e57-863f-4e64-a568-3e52cb60e0d3",
"metadata": {},
"source": [
"pres_sfc_2021-07-14T12:00:00.000.png
looks like this: