parcels icon indicating copy to clipboard operation
parcels copied to clipboard

How to deal with out-of-array indexing in the interpolation functions?

Open erikvansebille opened this issue 1 year ago • 3 comments

With the support of user-provided interpolation methods, we should also think how best to capture/deal with out-of-array indexing in these methods. While OutOfBounds Errors (both space and time) are thrown in search_indices (so before the interpolation function is evaluated) and we can thus be confident that data[ti, zi, yi, xi] is a valid indexing, this is not necessarily true for data[ti+1, zi+1, yi+1, xi+1] or any combination of index and index+1.

The is now captured explicitly in #1895: https://github.com/OceanParcels/Parcels/blob/4502c12a4892b9cfaf86b3f80d37488474f405b7/parcels/_interpolation.py#L142-L147

But it would be nicer to fix this under the hood. Question is: how??

erikvansebille avatar Mar 03 '25 13:03 erikvansebille

Maybe we could bake in the concept of "boundary elements" and "interior elements" and not apply interpolation to boundary elements - instead, we'd need "extrapolation" to handle boundaries. Interpolation would then only apply to interior elements.

With unstructured grids, we have the boundary_face_indices that is quite useful. Although in unstructured we only have the face id and the connectivity tables, I suspect some folks may be needing to distinguish between interior and boundary points..

fluidnumerics-joe avatar Mar 03 '25 13:03 fluidnumerics-joe

Thanks for this insightful feedback, @fluidnumerics-joe. Could indeed be a good approach to differentiate between boundary elements and interior elements (although the last available time-step would also be a "boundary-in-time" element). How would we implement this in structured grids, @VeckoTheGecko?

erikvansebille avatar Mar 03 '25 14:03 erikvansebille

I think the suggestion by @fluidnumerics-joe is good, though I'm not entirely sure what exact the implementation would look like (partly since this is closely related to how we handle spatial periodicity, and perhaps even temporal periodicity - though the latter we'll likely implement with xarray).

Note if we want halos (like in v3), xarray.DataArray.pad(mode='wrap') would be of interest

VeckoTheGecko avatar Mar 10 '25 09:03 VeckoTheGecko

This is now fixed in v4-dev by checking for ouot-of bounds before the Interpolator

https://github.com/OceanParcels/Parcels/blob/d343b64473fa10cde87496e0bd669744d6b678cd/parcels/_core/field.py#L219-L222

erikvansebille avatar Oct 02 '25 14:10 erikvansebille