pydata / pydata/xarray

Interpolation unstructured data to regular grid

Open
#7,807 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement topic-interpolation
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

Is your feature request related to a problem?

Hello all. First, thanks for this amazing library!

I'm working with unstructured data ( points that are not in a grid ), and I want to interpolate them onto a regular grid.
However, it seems it is not directly supported by xarray and I have to manually call scipy's griddata.
This is annoying because:

  • It's verbose and I'm lazy
  • I loose my array's metadata

It seems the problem is encoutered by some other people as a google search on xarray interp irregular data yields to some results:

Searching for issues related to this topic led me to this single issue https://github.com/pydata/xarray/issues/5281 where the user uses directly scipy's griddata to interpolate.

Here is a sample script to reproduce and illustrate the problem:

from xarray import DataArray
import numpy as np
import matplotlib.pyplot as plt

# Sample 1000 uniform points in [-1,1]
# Thus, those points are unstructured
x, y = points = np.random.uniform(-1, 1, (2, 1000))
x, y = points = DataArray(
    points,
    coords=dict(
        variable=list("XY"), point=np.arange(1000), x=("point", x), y=("point", y)
    ),
    dims=("variable", "point"),
)

# Compute a dummy "function"
z = np.sqrt(x**2 + y**2)
print(z)
# We now have a function sampled on unstructured points
fig, ax = plt.subplots(1, 1)
ax.scatter(x, y, c=z)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_aspect(1)

plt.show()

I get the following value for z:

<xarray.DataArray (point: 1000)>
array([...blablabla whatever...])
Coordinates:
  * point    (point) int64 0 1 2 3 4 5 6 7 8 ... 992 993 994 995 996 997 998 999
    x        (point) float64 -0.4088 -0.7709 -0.7345 ... 0.5191 0.6061 -0.7329
    y        (point) float64 -0.8546 -0.6544 -0.6771 ... -0.8286 -0.04476 -0.574

And it looks like so:
image

So we can see z is correctly annotated and we've got the x and y value for each point, but I still cannot interpolate on a regular grid:

z.interp(x=np.linspace(-1, 1, 100), y=np.linspace(-1, 1, 100))

It raises the following error:

Dimensions {'x', 'y'} do not exist. Expected one or more of Frozen({'point': 1000})
Describe the solution you'd like

It would be nice to allow a direct unstructured interpolation if variables are correctly annotated (I supposed it's done with panda's multi indexes?)

This could be done using Scipy's griddata

A check could be added in interp function to see if the user wants to interpolate on sub-dimensions (i'm not sure if the name is appropriate), and if so, using griddata instead of the other interpolation methods.

Describe alternatives you've considered

Alternative solutions could be to use only scipy grid data by hand, but that would be sad.

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the xarray DataArray.interp entry point and the linked SciPy griddata API. Compare the requested unstructured-point behavior with the current dimension-based interpolation assumptions, including metadata preservation, and define the scope and tests needed before implementation can be considered done.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.