Fix map_blocks for DataArray with cftime and sdba_encode_cf #1674
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Checklist:
number
) and pull request (:pull:number
) has been addedWhat kind of change does this PR introduced
tl;dr : me fix issue.
I think this issue stems from a change in recent xarray, but the case was untested by xclim. The solution was to iterate over a list that is "disconnected" from the dictionary.
The issue occurs when a DataArray that has a coordinate made of
cftime
objects is passed to amap_blocks
-wrapped function and xclim's global optionsdba_encode_cf
is True.The previous encoding code was iterating over the coordinates with
coords.items()
. When it would modify a the cftime coord, this would fail as you can't modify a dictionary while it is being iterated upon.Only happens if the option to encode is activated, only happens if there is a cftime coord and only happens on
DataArray
objects. I'm not 100% sure why, but I thinkDataset.coords.items
does not map directly to a dict. We usually useDataset
objects in function wrapped bymap_blocks
(ormap_groups
). The culprit here was the polynomial's trend computation which uses aDataArray
.Reason for the apparent dask-dependency
The interesting part is that this bug does not depend on the fact that the data is using dask or not. But it led me to discover another "issue" or, rather, "edgecase" .
In
xscen.adjust
we wrap the xclim call with options:Here, for DQM, the
adjust
method is wrapped bymap_blocks
. Whensim_sel
uses dask, the actual adjustment is, of course, not executed here. Only when the computation is triggered will the adjustment function be run, and only then will it call the polynomial detrending. But at that moment, thesdba_encode_cf
option is not set anymore! So the issue was not raised for the "dask" case.When
sim_sel
was not using dask, all the code was executed under the option statement and thus the issue was raised. Which is a bit dumb because the encoding is a micro-optimization made for dask, it's no use otherwise.Does this PR introduce a breaking change?
No