Skip to content

Commit

Permalink
Merge pull request #175 from nsidc/allow_non_lastday_monthly_melt
Browse files Browse the repository at this point in the history
allow melt from not-last-day-of-month
  • Loading branch information
trey-stafford authored Nov 7, 2024
2 parents 14eeb3c + 782689e commit d820fae
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
19 changes: 15 additions & 4 deletions seaice_ecdr/intermediate_monthly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions seaice_ecdr/melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion seaice_ecdr/tests/unit/test_monthly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
),
)
Expand Down

0 comments on commit d820fae

Please sign in to comment.