Skip to content

Allow open bounds in select_time - #2374

Draft
baptistehamon wants to merge 25 commits into
Ouranosinc:mainfrom
baptistehamon:select-time-bounds
Draft

Allow open bounds in select_time#2374
baptistehamon wants to merge 25 commits into
Ouranosinc:mainfrom
baptistehamon:select-time-bounds

Conversation

@baptistehamon

Copy link
Copy Markdown
Contributor

Pull Request Checklist:

  • This PR addresses an already opened issue (for bug fixes / features)
  • Tests for the changes have been added (for bug fixes / features)
    • (If applicable) Documentation has been added / updated (for bug fixes / features)
  • CHANGELOG.rst has been updated (with summary of main changes)
    • Link to issue (:issue:number) and pull request (:pull:number) has been added

What kind of change does this PR introduce?

  • Rename xclim.core.calendar.mask_between_doys to xclim.core.calendar.select_between_doys
  • Allow open date_bounds and doy_bounds (i.e., None) in xclim.core.calendar.select_time

Does this PR introduce a breaking change?

Yes ! I'll details the breaking changes when I update tests.

Other information:

@github-actions

Copy link
Copy Markdown

Warning

This Pull Request is coming from a fork and must be manually tagged approved in order to perform additional testing.

@Zeitsperre Zeitsperre left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll let others weigh in on this, but (correct me if I'm wrong) I think the logic tells me the changes may run into problems dealing with 360-day calendars. Is that possible?

Comment thread src/xclim/core/calendar.py Outdated
Comment thread src/xclim/core/calendar.py Outdated
Comment thread src/xclim/core/calendar.py Outdated
Zeitsperre and others added 4 commits July 16, 2026 15:12
Co-authored-by: Trevor James Smith <10819524+Zeitsperre@users.noreply.github.com>
Signed-off-by: Baptiste Hamon <119306054+baptistehamon@users.noreply.github.com>
@baptistehamon

Copy link
Copy Markdown
Contributor Author

You're right, the logic does not work with a 360-day calendar. However, it is not the result of this PR changes as I just realised that the current behaviour of xclim's select_time (and underlying mask_between_doys) does not handle 360-day xr.DataArray doy bounds neither. Is it an acceptable issue with this PR that can be fixed later, or do you want this PR to fix that?

@Zeitsperre

Copy link
Copy Markdown
Collaborator

@baptistehamon I didn't realize that that functionality wasn't already there. My suggestion would be to open an issue at most. There may be a justification as to why that might be the case, but I'd let my colleague @aulemahal weigh in there as he wrote much of the calendar-handling functionality.

Perhaps for here if you want to focus solely on ensuring that the feature supports all existing functionality, that would be enough.

Most the team is currently on vacation, but some of them should be back by next week. Thanks again!

@baptistehamon

Copy link
Copy Markdown
Contributor Author

I'm open an issue (#2393) regarding the error with 360_days calendar.

@baptistehamon

Copy link
Copy Markdown
Contributor Author

I've edited the code to fix small issues and to minimise breaking changes. I added back the old behaviour for closed date_bounds in select_time to avoid an issue with the 360_days calendar; the new behaviour applies only to open bounds (i.e., None). As a result, the PR introduces only minor breaking changes (detailed in the PR description), and I updated the relevant tests. I'm going to add more tests to cover the new functionality (open bounds) in the coming couple of days, but otherwise the PR is almost ready :)

Comment on lines +1256 to +1257
start = start.expand_dims(time=bnds.time)
end = end.expand_dims(time=bnds.time)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a case where "time" not in start.dims but "time" in end.dims ? Should we handle this too ?

end = doy_to_days_since(end.convert_calendar(**calkws))

# Fill missing values in start and end bounds
if include_nans or start.isnull().all():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't there be a case where start was passed as an array full of NaN and include_nans=False ? I feel like the second part of this condition should test if start was passed a None explicitly.

Also, as far as I understand, I think this is the only compute-triggering line in the function ? Avoiding start.isnull().all() would allow start to be a dask array, no ?

"Passing open `date_bounds` (i.e., with None) is not supported for 360_day calendars."
)

bnds = time_bnds(da.time.resample(time=bounds_freq if bounds_freq is not None else "YS"))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bnds = time_bnds(da.time.resample(time=bounds_freq if bounds_freq is not None else "YS"))
bnds = time_bnds(da.time.resample(time=bounds_freq or "YS"))

)

bnds = time_bnds(da.time.resample(time=bounds_freq if bounds_freq is not None else "YS"))
cal = get_calendar(da)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cal = get_calendar(da)
cal = da.time.dt.calendar

@aulemahal

Copy link
Copy Markdown
Collaborator

Thanks @baptistehamon ! I made a first pass, nothing major to say here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possibility of using open bounds doy in select_time

3 participants