From 57cba7958c68a46f9856ab4938dc8e53cb010cbf Mon Sep 17 00:00:00 2001 From: Carsten Ehbrecht Date: Tue, 12 Nov 2024 18:16:35 +0100 Subject: [PATCH] added notebook for decadal calendar fix --- notebooks/decadal_calendar_fix.ipynb | 1033 ++++++++++++++++++++++++++ 1 file changed, 1033 insertions(+) create mode 100644 notebooks/decadal_calendar_fix.ipynb diff --git a/notebooks/decadal_calendar_fix.ipynb b/notebooks/decadal_calendar_fix.ipynb new file mode 100644 index 0000000..37c3f37 --- /dev/null +++ b/notebooks/decadal_calendar_fix.ipynb @@ -0,0 +1,1033 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "0f81de60-a0a2-4fc3-b704-e47b1aaab3ce", + "metadata": {}, + "source": [ + "# Decadal Calendar Fix" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "06856aa1-30cb-4edd-a461-b1a08fde8ec5", + "metadata": {}, + "outputs": [], + "source": [ + "import xarray as xr" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c696b650-d309-4337-9c0e-b6285fb318bf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.Dataset>\n",
+       "Dimensions:      (time: 2, realization: 10, bnds: 2, lat: 256, lon: 512)\n",
+       "Coordinates:\n",
+       "  * time         (time) datetime64[ns] 2020-11-16 2020-12-16T12:00:00\n",
+       "  * lat          (lat) float64 -89.46 -88.77 -88.07 -87.37 ... 88.07 88.77 89.46\n",
+       "  * lon          (lon) float64 0.3516 1.055 1.758 2.461 ... 358.2 358.9 359.6\n",
+       "    reftime      datetime64[ns] ...\n",
+       "    leadtime     (time) timedelta64[ns] ...\n",
+       "  * realization  (realization) int32 10 1 2 3 4 5 6 7 8 9\n",
+       "Dimensions without coordinates: bnds\n",
+       "Data variables:\n",
+       "    time_bnds    (realization, time, bnds) datetime64[ns] ...\n",
+       "    lat_bnds     (realization, time, lat, bnds) float64 ...\n",
+       "    lon_bnds     (realization, time, lon, bnds) float64 ...\n",
+       "    psl          (realization, time, lat, lon) float32 ...\n",
+       "Attributes: (12/52)\n",
+       "    Conventions:                 CF-1.7 CMIP-6.2\n",
+       "    activity_id:                 DCPP\n",
+       "    branch_method:               no parent\n",
+       "    branch_time:                 0.0\n",
+       "    branch_time_in_child:        0.0\n",
+       "    branch_time_in_parent:       0.0\n",
+       "    ...                          ...\n",
+       "    license:                     CMIP6 model data produced by EC-Earth-Consor...\n",
+       "    cmor_version:                3.4.0\n",
+       "    forcing_description:         f1, CMIP6 historical forcings\n",
+       "    physics_description:         physics from the standard model configuratio...\n",
+       "    initialization_description:  Atmosphere initialization based on full-fiel...\n",
+       "    startdate:                   s202011
" + ], + "text/plain": [ + "\n", + "Dimensions: (time: 2, realization: 10, bnds: 2, lat: 256, lon: 512)\n", + "Coordinates:\n", + " * time (time) datetime64[ns] 2020-11-16 2020-12-16T12:00:00\n", + " * lat (lat) float64 -89.46 -88.77 -88.07 -87.37 ... 88.07 88.77 89.46\n", + " * lon (lon) float64 0.3516 1.055 1.758 2.461 ... 358.2 358.9 359.6\n", + " reftime datetime64[ns] ...\n", + " leadtime (time) timedelta64[ns] ...\n", + " * realization (realization) int32 10 1 2 3 4 5 6 7 8 9\n", + "Dimensions without coordinates: bnds\n", + "Data variables:\n", + " time_bnds (realization, time, bnds) datetime64[ns] ...\n", + " lat_bnds (realization, time, lat, bnds) float64 ...\n", + " lon_bnds (realization, time, lon, bnds) float64 ...\n", + " psl (realization, time, lat, lon) float32 ...\n", + "Attributes: (12/52)\n", + " Conventions: CF-1.7 CMIP-6.2\n", + " activity_id: DCPP\n", + " branch_method: no parent\n", + " branch_time: 0.0\n", + " branch_time_in_child: 0.0\n", + " branch_time_in_parent: 0.0\n", + " ... ...\n", + " license: CMIP6 model data produced by EC-Earth-Consor...\n", + " cmor_version: 3.4.0\n", + " forcing_description: f1, CMIP6 historical forcings\n", + " physics_description: physics from the standard model configuratio...\n", + " initialization_description: Atmosphere initialization based on full-fiel...\n", + " startdate: s202011" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ds = xr.open_dataset(\"/Users/pingu/Desktop/69de4961fa0f43f54a40b106b808959d/psl_Amon_EC-Earth3_dcppB-forecast_r10i1p1f1_gr_20201116-20201216.nc\")\n", + "ds" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "be531b88-5606-4e7f-9ea8-5a72aa8ab362", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'proleptic_gregorian'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ds.time.encoding.get(\"calendar\")" + ] + }, + { + "cell_type": "markdown", + "id": "a6c267dd-930b-4544-9bd8-eabaa9f6685d", + "metadata": {}, + "source": [ + "## apply calendar fix" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9f441577-023d-4ca8-a201-4cda18db4e63", + "metadata": {}, + "outputs": [], + "source": [ + "from rook.utils import decadal_fixes" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "ff22c3b9-9419-4850-b8a9-7e1653726f2c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.Dataset>\n",
+       "Dimensions:      (time: 2, realization: 10, bnds: 2, lat: 256, lon: 512)\n",
+       "Coordinates:\n",
+       "  * time         (time) datetime64[ns] 2020-11-16 2020-12-16T12:00:00\n",
+       "  * lat          (lat) float64 -89.46 -88.77 -88.07 -87.37 ... 88.07 88.77 89.46\n",
+       "  * lon          (lon) float64 0.3516 1.055 1.758 2.461 ... 358.2 358.9 359.6\n",
+       "    reftime      datetime64[ns] ...\n",
+       "    leadtime     (time) timedelta64[ns] ...\n",
+       "  * realization  (realization) int32 10 1 2 3 4 5 6 7 8 9\n",
+       "Dimensions without coordinates: bnds\n",
+       "Data variables:\n",
+       "    time_bnds    (realization, time, bnds) datetime64[ns] ...\n",
+       "    lat_bnds     (realization, time, lat, bnds) float64 ...\n",
+       "    lon_bnds     (realization, time, lon, bnds) float64 ...\n",
+       "    psl          (realization, time, lat, lon) float32 ...\n",
+       "Attributes: (12/52)\n",
+       "    Conventions:                 CF-1.7 CMIP-6.2\n",
+       "    activity_id:                 DCPP\n",
+       "    branch_method:               no parent\n",
+       "    branch_time:                 0.0\n",
+       "    branch_time_in_child:        0.0\n",
+       "    branch_time_in_parent:       0.0\n",
+       "    ...                          ...\n",
+       "    license:                     CMIP6 model data produced by EC-Earth-Consor...\n",
+       "    cmor_version:                3.4.0\n",
+       "    forcing_description:         f1, CMIP6 historical forcings\n",
+       "    physics_description:         physics from the standard model configuratio...\n",
+       "    initialization_description:  Atmosphere initialization based on full-fiel...\n",
+       "    startdate:                   s202011
" + ], + "text/plain": [ + "\n", + "Dimensions: (time: 2, realization: 10, bnds: 2, lat: 256, lon: 512)\n", + "Coordinates:\n", + " * time (time) datetime64[ns] 2020-11-16 2020-12-16T12:00:00\n", + " * lat (lat) float64 -89.46 -88.77 -88.07 -87.37 ... 88.07 88.77 89.46\n", + " * lon (lon) float64 0.3516 1.055 1.758 2.461 ... 358.2 358.9 359.6\n", + " reftime datetime64[ns] ...\n", + " leadtime (time) timedelta64[ns] ...\n", + " * realization (realization) int32 10 1 2 3 4 5 6 7 8 9\n", + "Dimensions without coordinates: bnds\n", + "Data variables:\n", + " time_bnds (realization, time, bnds) datetime64[ns] ...\n", + " lat_bnds (realization, time, lat, bnds) float64 ...\n", + " lon_bnds (realization, time, lon, bnds) float64 ...\n", + " psl (realization, time, lat, lon) float32 ...\n", + "Attributes: (12/52)\n", + " Conventions: CF-1.7 CMIP-6.2\n", + " activity_id: DCPP\n", + " branch_method: no parent\n", + " branch_time: 0.0\n", + " branch_time_in_child: 0.0\n", + " branch_time_in_parent: 0.0\n", + " ... ...\n", + " license: CMIP6 model data produced by EC-Earth-Consor...\n", + " cmor_version: 3.4.0\n", + " forcing_description: f1, CMIP6 historical forcings\n", + " physics_description: physics from the standard model configuratio...\n", + " initialization_description: Atmosphere initialization based on full-fiel...\n", + " startdate: s202011" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ds_fix = decadal_fixes.decadal_fix_calendar(None, ds)\n", + "ds_fix" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "575f2c42-cffd-45b0-bfdd-848a7c0feb69", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'standard'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ds_fix.time.encoding.get(\"calendar\")" + ] + } + ], + "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.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}