diff --git a/seaice_ecdr/intermediate_monthly.py b/seaice_ecdr/intermediate_monthly.py index 84b08267..0ebf050f 100644 --- a/seaice_ecdr/intermediate_monthly.py +++ b/seaice_ecdr/intermediate_monthly.py @@ -44,6 +44,7 @@ from seaice_ecdr.constants import DEFAULT_BASE_OUTPUT_DIR from seaice_ecdr.days_treated_differently import months_of_cdr_missing_data from seaice_ecdr.intermediate_daily import get_ecdr_filepath +from seaice_ecdr.melt import MELT_SEASON_LAST_DOY from seaice_ecdr.nc_attrs import get_global_attrs from seaice_ecdr.platforms import PLATFORM_CONFIG, SUPPORTED_PLATFORM_ID from seaice_ecdr.tb_data import get_hemisphere_from_crs_da @@ -471,10 +472,20 @@ def calc_cdr_melt_onset_day_monthly( ) -> xr.DataArray: """Create the `cdr_melt_onset_day_monthly` variable.""" # Create `cdr_melt_onset_day_monthly`. This is the value from - # the last day of the month. - cdr_melt_onset_day_monthly = daily_melt_onset_for_month.sel( - time=daily_melt_onset_for_month.time.max() - ) + # the last day of the month unless the month is incomplete. + # xarray uses np.datetime64[ns] for time + doy_list: list[int] = [ + int(date.strftime("%j")) + for date in daily_melt_onset_for_month.time.dt.date.values + ] + if MELT_SEASON_LAST_DOY in doy_list: + day_244_idx = doy_list.index(MELT_SEASON_LAST_DOY) + max_date = daily_melt_onset_for_month.time[day_244_idx].values + logger.info(f"Found non-max date with melt: {max_date}") + else: + max_date = daily_melt_onset_for_month.time.max() + + cdr_melt_onset_day_monthly = daily_melt_onset_for_month.sel(time=max_date) cdr_melt_onset_day_monthly.name = "cdr_melt_onset_day_monthly" cdr_melt_onset_day_monthly = cdr_melt_onset_day_monthly.drop_vars("time") diff --git a/seaice_ecdr/melt.py b/seaice_ecdr/melt.py index d8bb6b24..4c2d87a4 100644 --- a/seaice_ecdr/melt.py +++ b/seaice_ecdr/melt.py @@ -35,8 +35,8 @@ import numpy.typing as npt # Start and end DOYs for the melt season (inclusive) -MELT_SEASON_FIRST_DOY = 60 -MELT_SEASON_LAST_DOY = 244 +MELT_SEASON_FIRST_DOY: int = 60 +MELT_SEASON_LAST_DOY: int = 244 # Flag value for grid cells before melting is detected or if no melt is ever # detected. diff --git a/seaice_ecdr/tests/unit/test_monthly.py b/seaice_ecdr/tests/unit/test_monthly.py index 901f3b46..5e6fc267 100644 --- a/seaice_ecdr/tests/unit/test_monthly.py +++ b/seaice_ecdr/tests/unit/test_monthly.py @@ -505,7 +505,14 @@ def test_calc_cdr_melt_onset_day_monthly(): data=_mock_data, dims=["y", "time"], coords=dict( - time=[dt.date(2022, 3, 1), dt.date(2022, 3, 2), dt.date(2022, 3, 3)], + time=[ + np.datetime64(date) + for date in [ + dt.date(2022, 3, 1), + dt.date(2022, 3, 2), + dt.date(2022, 3, 3), + ] + ], y=list(range(5)), ), )