contrailcirrus / pycontrails

Python library for modeling contrails and other aviation climate impacts
https://py.contrails.org/
Apache License 2.0
59 stars 18 forks source link

Cython interpolation with 1 non-degenerate dimension #174

Closed thabbott closed 3 months ago

thabbott commented 7 months ago

Description

Linear interpolation with PycontrailsRegularGridInterpolator fails when the field being interpolated only has one non-degenerate dimension (i.e., when evaluate_1d is used).

Note that this bug predates changes made in pycontrails 0.50.1 to accomodate updates in scipy 1.13.0: it's present using e.g. pycontrails 0.49.5 and scipy 1.12.0.

Details

Steps to Reproduce

import pycontrails
import scipy

print(f"pycontrails {pycontrails.__version__}")
print(f"scipy {scipy.__version__}")

from pycontrails.core.interpolation import PycontrailsRegularGridInterpolator
import numpy as np

x = np.array([0.0, 1.0])
y = np.array([0.0])
z = np.linspace(0, 1, 2)
t = np.array([0.0])
values = np.tile(np.linspace(0, 1, 2), (2, 1, 1, 1)).reshape((2, 1, 2, 1))

target = np.array([[0.0, 0.0, 0.5, 0.0]])

rgi = PycontrailsRegularGridInterpolator(
   points=(x, y, z, t),
   values=values,
   method="linear",
   bounds_error=True,
   fill_value = np.nan
)
print(rgi(target))

x = np.array([0.0])
y = np.array([0.0])
z = np.linspace(0, 1, 2)
t = np.array([0.0])
values = np.linspace(0, 1, 2).reshape((1, 1, 2, 1))

rgi = PycontrailsRegularGridInterpolator(
   points=(x, y, z, t),
   values=values,
   method="linear",
   bounds_error=True,
   fill_value = np.nan
)

print(rgi(target))

The first interpolation (with two non-degenerate dimensions) produces the expected result. The second interpolation (with only one non-degenerate dimension) raises ValueError: Buffer has wrong number of dimensions (expected 1, got 2) from evaluate_1d.