Skip to content

Instantly share code, notes, and snippets.

@piotr-florek-mohc
Created February 2, 2024 15:57
Show Gist options
  • Save piotr-florek-mohc/92dbc05da9722497562ec740346b76fc to your computer and use it in GitHub Desktop.
Save piotr-florek-mohc/92dbc05da9722497562ec740346b76fc to your computer and use it in GitHub Desktop.
consolidated version of the cmorisation process for DWD
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "220e5b04-c4b4-48ec-9e70-e4e493d88680",
"metadata": {},
"source": [
"DWD Amon\n",
"========\n",
"\n",
"This notebook contains consolidated code from the earlier example, and demonstrates how to produce a number of monthly atmospheric (`Amon`) variables."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "827ac9bd-68c5-4521-9f61-df6cd05ac217",
"metadata": {},
"outputs": [],
"source": [
"import cftime\n",
"import cmor\n",
"import iris\n",
"import numpy as np\n",
"import re\n",
"import os\n",
"import cf_units\n",
"\n",
"from cmor_utils import create_cmor_json_config, generate_dataset_info, parse_filename, parse_time_unit, load_mip_table, DATASET_ROOT, BASE_TIME_UNIT, MIP_ERA, MIP_TABLES_DIR"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1920c04b-51ff-4a1b-b60f-bdf4947afe52",
"metadata": {},
"outputs": [],
"source": [
"filename_template = '{}_Amon_DWD_s{}_r1-16i1p1f1_gn_climatology-1991-2020.nc'\n",
"\n",
"variable = 'psl'\n",
"init_year = 2021\n",
"\n",
"filename = filename_template.format(variable, init_year)\n",
"dataset = iris.load(os.path.join(DATASET_ROOT, filename))[0]\n",
"\n",
"calendar = dataset.coord('time').units.calendar\n",
"dataset.coord('time').convert_units(cf_units.Unit(BASE_TIME_UNIT, calendar=calendar))\n",
"cftime_points = cftime.num2date(dataset.coord('time').points,\n",
" str(dataset.coord('time').units),\n",
" calendar=calendar)\n",
"time_bounds = []\n",
"for time_point in cftime_points:\n",
" # this needs to handle time bounds at monthly resolution\n",
" start_date = cftime.date2num(cftime.datetime(time_point.year, time_point.month, 1, calendar=calendar), units=BASE_TIME_UNIT, calendar=calendar)\n",
" if time_point.month < 12:\n",
" end_year = time_point.year\n",
" end_month = time_point.month + 1\n",
" else:\n",
" end_year = time_point.year + 1\n",
" end_month = 1\n",
" end_date = cftime.date2num(cftime.datetime(end_year, end_month, 1, calendar=calendar), units=BASE_TIME_UNIT, calendar=calendar)\n",
" time_bounds.append((start_date, end_date,))\n",
"dataset.coord('time').bounds = time_bounds\n",
"dataset.coord('longitude').guess_bounds()\n",
"dataset.coord('latitude').guess_bounds()\n",
"time_coord = dataset.coord('time')\n",
"lon_coord = dataset.coord('longitude')\n",
"lat_coord = dataset.coord('latitude')\n",
"realization_coord = dataset.coord('realization')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "01c73ffb-8001-46de-9eb4-abfecc6a6558",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"\u001b[2;34;47mC Traceback:\n",
"In function: cmor_close_variable\n",
"! \u001b[0m\n",
"\n",
"\u001b[1;34;47m!!!!!!!!!!!!!!!!!!!!!!!!!\n",
"!\n",
"! Warning: while closing variable 0 (pslAnom, table Amon)\n",
"! we noticed you wrote 0 time steps for the variable,\n",
"! but its time axis 0 (time) has 122 time steps\n",
"!\n",
"!!!!!!!!!!!!!!!!!!!!!!!!!\u001b[0m\n",
"\n"
]
}
],
"source": [
"mip_table, institution_id, ensemble_label, grid_label, climatology, initialization_index, physics_index, forcing_index = parse_filename(filename)\n",
"activity_id = \"DCPP\"\n",
"calendar = calendar\n",
"experiment_id = \"dcppB-forecast\"\n",
"source_id = \"ICON-ESM-LR\"\n",
"dataset_info = generate_dataset_info(\n",
" mip_era=MIP_ERA,\n",
" activity_id=activity_id,\n",
" calendar=calendar,\n",
" ensemble_label=ensemble_label,\n",
" experiment_id=experiment_id,\n",
" institution_id=institution_id,\n",
" source_id=source_id,\n",
" source_type='AOGCM',\n",
" grid='grid',\n",
" grid_label=grid_label,\n",
" nominal_resolution='100 km',\n",
" forcing_index=forcing_index,\n",
" initialization_index=initialization_index,\n",
" physics_index=physics_index,\n",
" realization_index=1\n",
")\n",
"config_filepath = 'CMOR_input_dwd.json'\n",
"create_cmor_json_config(dataset_info, config_filepath)\n",
"cmor.setup(inpath=MIP_TABLES_DIR, netcdf_file_action=cmor.CMOR_REPLACE, create_subdirectories=False)\n",
"cmor.dataset_json(config_filepath)\n",
"table = os.path.join(MIP_TABLES_DIR, '{}_{}.json'.format(MIP_ERA, mip_table))\n",
"cmor.load_table(table)\n",
"\n",
"mip_table_json = load_mip_table(MIP_TABLES_DIR, MIP_ERA, mip_table)\n",
"\n",
"time = cmor.axis(table_entry='time', units=str(time_coord.units), coord_vals=time_coord.points, cell_bounds=time_coord.bounds)\n",
"\n",
"base_unit_year, base_unit_month, base_unit_day = parse_time_unit()\n",
"\n",
"date1 = cftime.date2num(cftime.datetime(init_year, 1, 1, calendar=calendar), units=BASE_TIME_UNIT, calendar=calendar)\n",
"date2 = cftime.date2num(cftime.datetime(base_unit_year, base_unit_month, base_unit_day, calendar=calendar), units=BASE_TIME_UNIT, calendar=calendar)\n",
"date_offset = date1 - date2\n",
"\n",
"reftime = cmor.axis(table_entry=\"reftime1\", units=str(time_coord.units), coord_vals=np.array([date_offset]))\n",
"latitude = cmor.axis(table_entry=\"latitude\", units=str(lat_coord.units), coord_vals=lat_coord.points, cell_bounds=lat_coord.bounds)\n",
"longitude = cmor.axis(table_entry=\"longitude\", units=str(lon_coord.units), coord_vals=lon_coord.points, cell_bounds=lon_coord.bounds)\n",
"realization = cmor.axis(table_entry=\"realization\", units=\"1\", coord_vals=realization_coord.points)\n",
"\n",
"data = dataset.data\n",
"\n",
"if variable == 'tas':\n",
" height2m = cmor.axis(table_entry=\"height2m\", units=\"m\", coord_vals=np.array((2.0,)))\n",
" expanded = np.ma.expand_dims(data, axis=[4,5])\n",
" axis_ids = [realization, time, latitude, longitude, reftime, height2m]\n",
"else:\n",
" expanded = np.ma.expand_dims(data, axis=[4])\n",
" axis_ids = [realization, time, latitude, longitude, reftime]\n",
"\n",
"variableAnom = '{}Anom'.format(variable)\n",
"var_id = cmor.variable(table_entry=variableAnom, axis_ids=axis_ids, units=mip_table_json['variable_entry'][variableAnom]['units'])\n",
"cmor.set_deflate(var_id, True, True, 1) # enabling netCDF4 compression\n",
"cmor.write(var_id, expanded)\n",
"filename = cmor.close(var_id, file_name=True)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2311728c-f78d-4e18-a142-bef3531a48a5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
".//pslAnom_Amon_ICON-ESM-LR_dcppB-forecast_r1-16i1p1f1_gn_202111-203112.nc\n"
]
}
],
"source": [
"print(filename)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e7620308-8081-40ae-85ca-ff997ac2b9fd",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment