Skip to content

Commit

Permalink
Update RXCROPMATURITY for compatibility w/ new history files.
Browse files Browse the repository at this point in the history
  • Loading branch information
samsrabin committed Jan 23, 2025
1 parent d0c4026 commit 9c48035
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
12 changes: 8 additions & 4 deletions python/ctsm/crop_calendars/cropcal_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ctsm.crop_calendars.check_rx_obeyed import check_rx_obeyed
from ctsm.crop_calendars.cropcal_constants import DEFAULT_GDD_MIN
from ctsm.crop_calendars.import_ds import import_ds
from ctsm.utils import is_instantaneous

MISSING_RX_GDD_VAL = -1

Expand All @@ -31,7 +32,12 @@ def check_and_trim_years(year_1, year_n, ds_in):

# Remove years outside range of interest
### Include an extra year at the end to finish out final seasons.
ds_in = utils.safer_timeslice(ds_in, slice(f"{year_1+1}-01-01", f"{year_n+2}-01-01"))
slice_yr_1 = year_1
slice_yr_n = year_n + 1
if is_instantaneous(ds_in["time"]):
slice_yr_1 += 1
slice_yr_n += 1
ds_in = utils.safer_timeslice(ds_in, slice(f"{slice_yr_1}-01-01", f"{slice_yr_n}-01-01"))

# Make sure you have the expected number of timesteps (including extra year)
n_years_expected = year_n - year_1 + 2
Expand Down Expand Up @@ -454,9 +460,7 @@ def convert_time_to_int_year(filename, this_ds, this_ds_gs):
# 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({"cftime": this_ds["time_bounds"].isel({"nbnd": 0})})
this_ds_gs = this_ds_gs.assign_coords(
{"time": [t.year for t in this_ds_gs["cftime"].values]}
)
Expand Down
2 changes: 1 addition & 1 deletion python/ctsm/crop_calendars/import_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def compute_derived_vars(ds_in, var):
and "HDATES" in ds_in
and ds_in.HDATES.dims == ("time", "mxharvests", "patch")
):
year_list = np.array([np.float32(x.year - 1) for x in ds_in.time.values])
year_list = np.array([np.float32(x.year) for x in ds_in.time.values])
hyears = ds_in["HDATES"].copy()
hyears.values = np.tile(
np.expand_dims(year_list, (1, 2)),
Expand Down
12 changes: 12 additions & 0 deletions python/ctsm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,15 @@ def parse_isoduration(iso_string):
# Convert all to timedelta
delta_t = timedelta(days=int(days) + 365 * int(years) + 30 * int(months))
return int(delta_t.total_seconds() / 86400)


def is_instantaneous(time_var):
"""
Check whether a time variable came from an instantaneous file
"""
long_name = time_var.attrs["long_name"]
if "time at end of" in long_name:
return True
if "time at exact middle" in long_name:
return False
raise RuntimeError(f"Does this long_name mean instantaneous or not? {long_name}")

0 comments on commit 9c48035

Please sign in to comment.