From 71fbe09a16ffbbfb210913492343e0eb98308478 Mon Sep 17 00:00:00 2001 From: Sam Rabin Date: Thu, 14 Nov 2024 15:52:52 -0700 Subject: [PATCH 1/3] cropcal_module.py import_output(): Handle "instantaneous files." --- python/ctsm/crop_calendars/cropcal_module.py | 33 +++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/python/ctsm/crop_calendars/cropcal_module.py b/python/ctsm/crop_calendars/cropcal_module.py index 719d352665..28c40e2133 100644 --- a/python/ctsm/crop_calendars/cropcal_module.py +++ b/python/ctsm/crop_calendars/cropcal_module.py @@ -443,10 +443,7 @@ def import_output( ) # Convert time axis to integer year, saving original as 'cftime' - this_ds_gs = this_ds_gs.assign_coords( - {"cftime": this_ds["time_bounds"].isel({"hist_interval": 0})} - ) - this_ds_gs = this_ds_gs.assign_coords({"time": [t.year for t in this_ds_gs["cftime"].values]}) + this_ds_gs = convert_time_to_int_year(filename, this_ds, this_ds_gs) # Get number of harvests this_ds_gs["NHARVESTS"] = (this_ds_gs["GDDHARV_PERHARV"] > 0).sum(dim="mxharvests") @@ -457,6 +454,34 @@ def import_output( return this_ds_gs, any_bad +def convert_time_to_int_year(filename, this_ds, this_ds_gs): + """ + Convert time axis to integer year, saving original as 'cftime' + """ + if "time_bounds" in this_ds: + # Always true before PR #2838, when even files with all instantaneous variables got + # time_bounds saved. After that PR (and before the segregation of instantaneous and other + # variables onto separate files), files with an instantaneous variable first in their list + # do not get time_bounds saved. + this_ds_gs = this_ds_gs.assign_coords( + {"cftime": this_ds["time_bounds"].isel({"hist_interval": 0})} + ) + this_ds_gs = this_ds_gs.assign_coords( + {"time": [t.year for t in this_ds_gs["cftime"].values]} + ) + elif this_ds["time"].attrs["long_name"] == "time at end of time step": + # This is an "instantaneous file." + this_ds_gs = this_ds_gs.assign_coords({"cftime": this_ds["time"]}) + this_ds_gs = this_ds_gs.assign_coords( + {"time": [t.year - 1 for t in this_ds_gs["cftime"].values]} + ) + else: + raise RuntimeError( + f"{filename} is neither an instantaneous nor a combined/non-instantaneous file." + ) + + return this_ds_gs + def handle_zombie_crops(this_ds): """ From a51816e0de380300b69db9fc3e2c7fa83b267b64 Mon Sep 17 00:00:00 2001 From: Sam Rabin Date: Thu, 14 Nov 2024 21:10:19 -0700 Subject: [PATCH 2/3] Reformat with black. --- python/ctsm/crop_calendars/cropcal_module.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/ctsm/crop_calendars/cropcal_module.py b/python/ctsm/crop_calendars/cropcal_module.py index 28c40e2133..3ea084e1d2 100644 --- a/python/ctsm/crop_calendars/cropcal_module.py +++ b/python/ctsm/crop_calendars/cropcal_module.py @@ -454,6 +454,7 @@ def import_output( return this_ds_gs, any_bad + def convert_time_to_int_year(filename, this_ds, this_ds_gs): """ Convert time axis to integer year, saving original as 'cftime' From 3220dbbdb9cd5bcf25e9325e5b0f2ea56978bdeb Mon Sep 17 00:00:00 2001 From: Sam Rabin Date: Thu, 14 Nov 2024 21:11:18 -0700 Subject: [PATCH 3/3] Add previous commit to .git-blame-ignore-revs. --- .git-blame-ignore-revs | 1 + 1 file changed, 1 insertion(+) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index fca4a8315b..32e53646d7 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -27,6 +27,7 @@ d866510188d26d51bcd6d37239283db690af7e82 e096358c832ab292ddfd22dd5878826c7c788968 475831f0fb0e31e97f630eac4e078c886558b61c fd5f177131d63d39e79a13918390bdfb642d781e +a51816e0de380300b69db9fc3e2c7fa83b267b64 # Ran SystemTests and python/ctsm through black python formatter 5364ad66eaceb55dde2d3d598fe4ce37ac83a93c 8056ae649c1b37f5e10aaaac79005d6e3a8b2380