Fix handling of data in "nearest" trajectory interpolate #5062
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.
Closes #4463
As raised in #4463 when regridding with iris.analysis.UnstructuredNearest the dtype wasn't being preserved and the mask was being thrown away. These issues turned out to be linked.
Regarding the dtype, previous changes to the underlying trajectory code chose to preserve the behaviour from the initial Iris commit (10 years ago!) that created a resulting
new_cubewith an empty data array, then filled it.iris/lib/iris/analysis/trajectory.py
Line 240 in 61ac271
But by doing so, it used the data type of the empty array with is (usually) float64.
I don't think it was necessary to preserve this behaviour. All other regrid/interpolation operations AFAIK use the data type from the input cube (e.g. linear trajectory interpolation, or nearest neighbour regrid). So it would be better to be consistent across our operations.
The other issue was the mask not being preserved. This seemed to be a consequence of filling the empty array, which currently looks like:
iris/lib/iris/analysis/trajectory.py
Line 456 in 10f517b
that can cause some problems with missing data, which I believe is what this comment is referring to
iris/lib/iris/analysis/trajectory.py
Lines 448 to 450 in 10f517b
As shown in the example below, if I try to fill any empty array with a masked array, the mask is simply thrown away. This is why I suspect we were first filling with the mdi value
But this won't happen if we just assign cube data to our new data array.
In this PR, in the first commit I have first added a couple unit tests to check the dtype and mask are preserved. The second commit include the fix which makes the tests pass.