Skip to content

Commit

Permalink
cropcal_module.py import_output(): Handle "instantaneous files."
Browse files Browse the repository at this point in the history
  • Loading branch information
samsrabin committed Nov 14, 2024
1 parent c42cd12 commit 71fbe09
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions python/ctsm/crop_calendars/cropcal_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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):
"""
Expand Down

0 comments on commit 71fbe09

Please sign in to comment.