From 3a605dd0720465cf0c092bdbd853b5b44fb67a10 Mon Sep 17 00:00:00 2001 From: Pingu Carsti Date: Tue, 19 Nov 2024 16:03:49 +0100 Subject: [PATCH] update notebook --- notebooks/decadal_calendar_fix.ipynb | 1068 ++++++++------------------ 1 file changed, 326 insertions(+), 742 deletions(-) diff --git a/notebooks/decadal_calendar_fix.ipynb b/notebooks/decadal_calendar_fix.ipynb index 6b8a1f9..f006323 100644 --- a/notebooks/decadal_calendar_fix.ipynb +++ b/notebooks/decadal_calendar_fix.ipynb @@ -10,13 +10,28 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 9, "id": "06856aa1-30cb-4edd-a461-b1a08fde8ec5", "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", - "from rooki import rooki" + "from rook.utils.decadal_fixes import apply_decadal_fixes, decadal_fix_calendar\n", + "from roocs_utils.xarray_utils.xarray_utils import open_xr_dataset\n", + "from rooki import rooki\n", + "\n", + "import tempfile" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "b1ecf482-c9a6-46ab-9485-6a022756d16f", + "metadata": {}, + "outputs": [], + "source": [ + "tmp_dir = tempfile.TemporaryDirectory(dir=None)\n", + "tmp_dir.cleanup()" ] }, { @@ -40,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 3, "id": "5462716e-7b3c-4b0b-bfad-34eb3c134434", "metadata": {}, "outputs": [ @@ -50,7 +65,7 @@ "True" ] }, - "execution_count": 22, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -63,43 +78,94 @@ }, { "cell_type": "code", - "execution_count": 23, - "id": "2a5da580-0e23-4b34-a621-8e6e2020b7f6", + "execution_count": 4, + "id": "e172845a-ab17-4c16-8215-e67763d5b499", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['https://data.mips.climate.copernicus.eu/thredds/fileServer/esg_c3s-cmip6/DCPP/EC-Earth-Consortium/EC-Earth3/dcppA-hindcast/s1976-r3i1p1f1/Amon/psl/gr/v20201215/psl_Amon_EC-Earth3_dcppA-hindcast_s1976-r3i1p1f1_gr_198411-198510.nc',\n", + " 'https://data.mips.climate.copernicus.eu/thredds/fileServer/esg_c3s-cmip6/DCPP/EC-Earth-Consortium/EC-Earth3/dcppA-hindcast/s1976-r3i1p1f1/Amon/psl/gr/v20201215/psl_Amon_EC-Earth3_dcppA-hindcast_s1976-r3i1p1f1_gr_198511-198610.nc']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result.download_urls()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "d27ce3eb-9799-4b9e-ba7a-fe9899b3d68d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Downloading to /var/folders/qb/mg0csz190wd4rxybhhnwjln80000gn/T/metalink__vun7bgo/psl_Amon_EC-Earth3_dcppA-hindcast_s1976-r3i1p1f1_gr_198411-198510.nc.\n", - "Downloading to /var/folders/qb/mg0csz190wd4rxybhhnwjln80000gn/T/metalink__vun7bgo/psl_Amon_EC-Earth3_dcppA-hindcast_s1976-r3i1p1f1_gr_198511-198610.nc.\n", - "gregorian\n", - "proleptic_gregorian\n" + "Downloading to /var/folders/5f/t661zdnd181ck1dv429s4p8r0000gn/T/metalink_xp7si7xh/psl_Amon_EC-Earth3_dcppA-hindcast_s1976-r3i1p1f1_gr_198411-198510.nc.\n", + "Downloading to /var/folders/5f/t661zdnd181ck1dv429s4p8r0000gn/T/metalink_xp7si7xh/psl_Amon_EC-Earth3_dcppA-hindcast_s1976-r3i1p1f1_gr_198511-198610.nc.\n" ] } ], "source": [ - "for ds in result.datasets():\n", - " print(ds.time.encoding.get(\"calendar\"))" + "dsets = [\n", + " decadal_fix_calendar(ds_id, open_xr_dataset(file)) for file in result.download()\n", + " # open_xr_dataset(file) for file in result.download()\n", + "]" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 6, "id": "6f89c9bc-1760-427a-91a8-eeef03944d42", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "gregorian\n", + "standard\n" + ] + } + ], "source": [ - "ds_path_g = \"/var/folders/qb/mg0csz190wd4rxybhhnwjln80000gn/T/metalink__vun7bgo/psl_Amon_EC-Earth3_dcppA-hindcast_s1976-r3i1p1f1_gr_198411-198510.nc\"\n", - "\n", - "ds_path_pg = \"/var/folders/qb/mg0csz190wd4rxybhhnwjln80000gn/T/metalink__vun7bgo/psl_Amon_EC-Earth3_dcppA-hindcast_s1976-r3i1p1f1_gr_198511-198610.nc\"\n", - "\n" + "for ds in dsets:\n", + " print(ds.time.encoding.get(\"calendar\"))\n" ] }, { "cell_type": "code", - "execution_count": 4, - "id": "07f7c176-a317-4a8e-add1-89f610614220", + "execution_count": 7, + "id": "6afe997f-91e1-4048-a2af-288818f36a07", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'gregorian'" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ds = xr.concat(dsets, dim=\"time\")\n", + "ds.time.encoding.get(\"calendar\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "172318f6-f484-4854-a0dd-41e2f041b8bc", "metadata": {}, "outputs": [ { @@ -469,31 +535,34 @@ " fill: currentColor;\n", "}\n", "
<xarray.Dataset>\n",
-       "Dimensions:    (time: 12, bnds: 2, lat: 256, lon: 512)\n",
+       "Dimensions:      (time: 24, bnds: 2, lat: 256, lon: 512)\n",
        "Coordinates:\n",
-       "  * time       (time) object 1984-11-16 00:00:00 ... 1985-10-16 12: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.0 0.7031 1.406 2.109 ... 357.2 357.9 358.6 359.3\n",
+       "  * time         (time) object 1984-11-16 00:00:00 ... 1986-10-16 12: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.0 0.7031 1.406 2.109 ... 357.9 358.6 359.3\n",
+       "    reftime      datetime64[ns] 1976-11-01\n",
+       "    leadtime     (time) float64 2.937e+03 2.967e+03 ... 3.606e+03 3.636e+03\n",
        "Dimensions without coordinates: bnds\n",
        "Data variables:\n",
-       "    time_bnds  (time, bnds) object ...\n",
-       "    lat_bnds   (lat, bnds) float64 ...\n",
-       "    lon_bnds   (lon, bnds) float64 ...\n",
-       "    psl        (time, lat, lon) float32 ...\n",
-       "Attributes: (12/48)\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",
-       "    cmor_version:              3.4.0\n",
-       "    variant_info:              Atmosphere initialization based on full-fields...\n",
-       "    sub_experiment_id:         s1976\n",
-       "    history:                   Wed Sep 25 11:00:49 2019: ncatted -O -a sub_ex...\n",
-       "    nominal_resolution:        100 km\n",
-       "    sub_experiment:            initialized near end of year 1976
" + "processed by ece2cmor v{version}, git rev.The cmor-fixer version v1.0 script has been applied.
nominal_resolution :
100 km
sub_experiment :
initialized near end of year 1976
forcing_description :
f1, CMIP6 historical forcings
physics_description :
physics from the standard model configuration, with no additional tuning or different parametrization
initialization_description :
Atmosphere initialization based on full-fields from ERA-Interim (s1979-s2018) or ERA-40 (s1960-s1978); ocean/sea-ice initialization based on full-fields from NEMO/LIM assimilation run nudged towards ORA-S4 (s1960-s2018)
startdate :
s197611
" ], "text/plain": [ "\n", - "Dimensions: (time: 12, bnds: 2, lat: 256, lon: 512)\n", + "Dimensions: (time: 24, bnds: 2, lat: 256, lon: 512)\n", "Coordinates:\n", - " * time (time) object 1984-11-16 00:00:00 ... 1985-10-16 12: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.0 0.7031 1.406 2.109 ... 357.2 357.9 358.6 359.3\n", + " * time (time) object 1984-11-16 00:00:00 ... 1986-10-16 12: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.0 0.7031 1.406 2.109 ... 357.9 358.6 359.3\n", + " reftime datetime64[ns] 1976-11-01\n", + " leadtime (time) float64 2.937e+03 2.967e+03 ... 3.606e+03 3.636e+03\n", "Dimensions without coordinates: bnds\n", "Data variables:\n", - " time_bnds (time, bnds) object ...\n", - " lat_bnds (lat, bnds) float64 ...\n", - " lon_bnds (lon, bnds) float64 ...\n", - " psl (time, lat, lon) float32 ...\n", - "Attributes: (12/48)\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", - " cmor_version: 3.4.0\n", - " variant_info: Atmosphere initialization based on full-fields...\n", - " sub_experiment_id: s1976\n", - " history: Wed Sep 25 11:00:49 2019: ncatted -O -a sub_ex...\n", - " nominal_resolution: 100 km\n", - " sub_experiment: initialized near end of year 1976" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ds_p = xr.open_dataset(ds_path_g, use_cftime=True)\n", - "ds_p" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "0289f5d2-bdac-46e3-8c6b-5f7b71782903", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'gregorian'" + " time_bnds (time, bnds) object 1984-11-01 00:00:00 ... 1986-11-01 00:00:00\n", + " lat_bnds (time, lat, bnds) float64 -90.0 -89.11 -89.11 ... 89.11 90.0\n", + " lon_bnds (time, lon, bnds) float64 -0.3516 0.3516 0.3516 ... 358.9 359.6\n", + " psl (time, lat, lon) float32 9.994e+04 9.994e+04 ... 1.014e+05\n", + " realization int32 3\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", + " nominal_resolution: 100 km\n", + " sub_experiment: initialized near end of year 1976\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: s197611" ] }, - "execution_count": 5, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "ds_p.time.encoding.get(\"calendar\")" + "ds_mod = apply_decadal_fixes(ds_id, ds)\n", + "ds_mod" ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "6fe32883-0f6d-424e-8d4a-59017b505a65", - "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: 12, bnds: 2, lat: 256, lon: 512)\n",
-       "Coordinates:\n",
-       "  * time       (time) object 1985-11-16 00:00:00 ... 1986-10-16 12: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.0 0.7031 1.406 2.109 ... 357.2 357.9 358.6 359.3\n",
-       "Dimensions without coordinates: bnds\n",
-       "Data variables:\n",
-       "    time_bnds  (time, bnds) object ...\n",
-       "    lat_bnds   (lat, bnds) float64 ...\n",
-       "    lon_bnds   (lon, bnds) float64 ...\n",
-       "    psl        (time, lat, lon) float32 ...\n",
-       "Attributes: (12/48)\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-Consorti...\n",
-       "    cmor_version:              3.4.0\n",
-       "    sub_experiment_id:         s1976\n",
-       "    history:                   Wed Sep 25 11:00:52 2019: ncatted -O -a sub_ex...\n",
-       "    nominal_resolution:        100 km\n",
-       "    sub_experiment:            initialized near end of year 1976
" - ], - "text/plain": [ - "\n", - "Dimensions: (time: 12, bnds: 2, lat: 256, lon: 512)\n", - "Coordinates:\n", - " * time (time) object 1985-11-16 00:00:00 ... 1986-10-16 12: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.0 0.7031 1.406 2.109 ... 357.2 357.9 358.6 359.3\n", - "Dimensions without coordinates: bnds\n", - "Data variables:\n", - " time_bnds (time, bnds) object ...\n", - " lat_bnds (lat, bnds) float64 ...\n", - " lon_bnds (lon, bnds) float64 ...\n", - " psl (time, lat, lon) float32 ...\n", - "Attributes: (12/48)\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-Consorti...\n", - " cmor_version: 3.4.0\n", - " sub_experiment_id: s1976\n", - " history: Wed Sep 25 11:00:52 2019: ncatted -O -a sub_ex...\n", - " nominal_resolution: 100 km\n", - " sub_experiment: initialized near end of year 1976" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ds_pg = xr.open_dataset(ds_path_pg, use_cftime=True)\n", - "ds_pg" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "4ab969cb-addb-43c5-9646-6b949d40eae3", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'proleptic_gregorian'" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ds_pg.time.encoding.get(\"calendar\")" - ] - }, - { - "cell_type": "markdown", - "id": "a6c267dd-930b-4544-9bd8-eabaa9f6685d", - "metadata": {}, - "source": [ - "## apply calendar fix" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "9f441577-023d-4ca8-a201-4cda18db4e63", - "metadata": {}, - "outputs": [], - "source": [ - "from rook.utils import decadal_fixes" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "ff22c3b9-9419-4850-b8a9-7e1653726f2c", - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "can only decode Dataset or DataStore objects", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m ds_fix \u001b[38;5;241m=\u001b[39m \u001b[43mdecadal_fixes\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecadal_fix_calendar\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mds_pg\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2\u001b[0m ds_fix\n", - "File \u001b[0;32m~/Documents/GitHub/roocs/rook/src/rook/utils/decadal_fixes.py:143\u001b[0m, in \u001b[0;36mdecadal_fix_calendar\u001b[0;34m(ds_id, ds)\u001b[0m\n\u001b[1;32m 141\u001b[0m calendar \u001b[38;5;241m=\u001b[39m ds\u001b[38;5;241m.\u001b[39mtime\u001b[38;5;241m.\u001b[39mencoding\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcalendar\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstandard\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 142\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m calendar \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mproleptic_gregorian\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[0;32m--> 143\u001b[0m ds[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtime\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[43mxr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode_cf\u001b[49m\u001b[43m(\u001b[49m\u001b[43mds\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtime\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 144\u001b[0m ds\u001b[38;5;241m.\u001b[39mtime\u001b[38;5;241m.\u001b[39mencoding[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcalendar\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstandard\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 145\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m ds\n", - "File \u001b[0;32m~/miniforge3/envs/rook/lib/python3.11/site-packages/xarray/conventions.py:680\u001b[0m, in \u001b[0;36mdecode_cf\u001b[0;34m(obj, concat_characters, mask_and_scale, decode_times, decode_coords, drop_variables, use_cftime, decode_timedelta)\u001b[0m\n\u001b[1;32m 678\u001b[0m encoding \u001b[38;5;241m=\u001b[39m obj\u001b[38;5;241m.\u001b[39mget_encoding()\n\u001b[1;32m 679\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 680\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcan only decode Dataset or DataStore objects\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 682\u001b[0m \u001b[38;5;28mvars\u001b[39m, attrs, coord_names \u001b[38;5;241m=\u001b[39m decode_cf_variables(\n\u001b[1;32m 683\u001b[0m \u001b[38;5;28mvars\u001b[39m,\n\u001b[1;32m 684\u001b[0m attrs,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 691\u001b[0m decode_timedelta\u001b[38;5;241m=\u001b[39mdecode_timedelta,\n\u001b[1;32m 692\u001b[0m )\n\u001b[1;32m 693\u001b[0m ds \u001b[38;5;241m=\u001b[39m Dataset(\u001b[38;5;28mvars\u001b[39m, attrs\u001b[38;5;241m=\u001b[39mattrs)\n", - "\u001b[0;31mTypeError\u001b[0m: can only decode Dataset or DataStore objects" - ] - } - ], - "source": [ - "ds_fix = decadal_fixes.decadal_fix_calendar(None, ds_pg)\n", - "ds_fix" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "7c7864a0-483c-4dba-8ce3-ba3a23d42f9c", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\u001b[0;31mSignature:\u001b[0m\n", - "\u001b[0mxr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconventions\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdecode_cf_datetime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0mnum_dates\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0munits\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'str'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0mcalendar\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'str | None'\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0muse_cftime\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'bool | None'\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0;34m'np.ndarray'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mDocstring:\u001b[0m\n", - "Given an array of numeric dates in netCDF format, convert it into a\n", - "numpy array of date time objects.\n", - "\n", - "For standard (Gregorian) calendars, this function uses vectorized\n", - "operations, which makes it much faster than cftime.num2date. In such a\n", - "case, the returned array will be of type np.datetime64.\n", - "\n", - "Note that time unit in `units` must not be smaller than microseconds and\n", - "not larger than days.\n", - "\n", - "See Also\n", - "--------\n", - "cftime.num2date\n", - "\u001b[0;31mFile:\u001b[0m ~/miniforge3/envs/rook/lib/python3.11/site-packages/xarray/coding/times.py\n", - "\u001b[0;31mType:\u001b[0m function" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "xr.conventions.times.decode_cf_datetime?" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "id": "ef7862a9-38c1-4ef6-81bc-7a91acf65090", - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "int() argument must be a string, a bytes-like object or a real number, not 'cftime._cftime.DatetimeProlepticGregorian'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[17], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m decoded_times \u001b[38;5;241m=\u001b[39m \u001b[43mxr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconventions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtimes\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode_cf_datetime\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 2\u001b[0m \u001b[43m \u001b[49m\u001b[43mds_pg\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtime\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[43m \u001b[49m\u001b[43munits\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mds_pg\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mencoding\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43munits\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\n\u001b[1;32m 4\u001b[0m \u001b[43m \u001b[49m\u001b[43mcalendar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstandard\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 5\u001b[0m \u001b[43m \u001b[49m\u001b[43muse_cftime\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\n\u001b[1;32m 6\u001b[0m \u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/miniforge3/envs/rook/lib/python3.11/site-packages/xarray/coding/times.py:309\u001b[0m, in \u001b[0;36mdecode_cf_datetime\u001b[0;34m(num_dates, units, calendar, use_cftime)\u001b[0m\n\u001b[1;32m 307\u001b[0m dates \u001b[38;5;241m=\u001b[39m cftime_to_nptime(dates)\n\u001b[1;32m 308\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m use_cftime:\n\u001b[0;32m--> 309\u001b[0m dates \u001b[38;5;241m=\u001b[39m \u001b[43m_decode_datetime_with_cftime\u001b[49m\u001b[43m(\u001b[49m\u001b[43mflat_num_dates\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43munits\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcalendar\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 310\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 311\u001b[0m dates \u001b[38;5;241m=\u001b[39m _decode_datetime_with_pandas(flat_num_dates, units, calendar)\n", - "File \u001b[0;32m~/miniforge3/envs/rook/lib/python3.11/site-packages/xarray/coding/times.py:209\u001b[0m, in \u001b[0;36m_decode_datetime_with_cftime\u001b[0;34m(num_dates, units, calendar)\u001b[0m\n\u001b[1;32m 206\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mModuleNotFoundError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo module named \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mcftime\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 207\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m num_dates\u001b[38;5;241m.\u001b[39msize \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 208\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39masarray(\n\u001b[0;32m--> 209\u001b[0m \u001b[43mcftime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnum2date\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnum_dates\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43munits\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcalendar\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43monly_use_cftime_datetimes\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 210\u001b[0m )\n\u001b[1;32m 211\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 212\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39marray([], dtype\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mobject\u001b[39m)\n", - "File \u001b[0;32msrc/cftime/_cftime.pyx:622\u001b[0m, in \u001b[0;36mcftime._cftime.num2date\u001b[0;34m()\u001b[0m\n", - "File \u001b[0;32msrc/cftime/_cftime.pyx:448\u001b[0m, in \u001b[0;36mcftime._cftime.upcast_times\u001b[0;34m()\u001b[0m\n", - "\u001b[0;31mTypeError\u001b[0m: int() argument must be a string, a bytes-like object or a real number, not 'cftime._cftime.DatetimeProlepticGregorian'" - ] - } - ], - "source": [ - "decoded_times = xr.conventions.times.decode_cf_datetime(\n", - " ds_pg.time, \n", - " units=ds_pg.time.encoding[\"units\"], \n", - " calendar=\"standard\",\n", - " use_cftime=True\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "id": "3cde41a6-4ded-4399-bf21-fde1444adba3", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'days since 1986-08-01 00:00:00'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ds_pg.time.encoding[\"units\"]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6c76c2f8-f490-43d4-b27a-a97ffd392ed8", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {