xarry.sel 'nearest' 方法如何处理多个条件?

How does the xarry.sel 'nearest' method work with multiple conditions?

我正在处理一些 xarray 数据数组,这些数组在特定的 latitude/longitude 坐标处有一些数据。

对于数据数组 1 (da1) 中的每个 lat/long 坐标对,我想在数据数组 2 (da2) 中找到最接近的 lat/long 对。

遵循 this Whosebug answer 一个似乎有效的解决方案是:


lats = xr.DataArray(da1.latitude.data, dims='z') #'z' is an arbitrary name placeholder
lons = xr.DataArray(da1.longitude.data, dims='z')
data = da2.sel(latitude = lats, longitude = lons, method = 'nearest') 

returns data 数据集,其长度与 da1 相同。

我的问题是:

例如,可以想象一种情况,经度上的匹配非常接近,而纬度上的匹配差一点,与经度上的匹配不太接近的相反情况相比,但纬度上的匹配非常接近。 'nearest' 方法根据什么指标来判断?

xarray 的选择算法确实对数据的每个维度独立工作。 'nearness' 匹配由每个索引的 query 方法处理; xarray 中的大多数索引都是由 xr.core.indexes.PandasIndex object, the query method of which simply calls the underlying pandas Index object's get_loc 方法包装的各种类型的 Pandas 索引。来自 pandas API 参考:

nearest: use the NEAREST index value if no exact match. Tied distances are broken by preferring the larger index value.

请注意,此匹配是在笛卡尔 space 中完成的(例如,仅基于数字)。因此,即使不考虑您关于多维的观点,如果您的 x 和 y 坐标未线性映射到物理距离(例如,如果您的 x 和 y点代表某些投影上的像素)。

这对于近似最近邻很好的许多应用程序非常有用,或者如果您乐于使用笛卡尔 space。如果没有,您可能应该使用地理空间库或其他一些明确处理您正在使用的坐标 space 的库来查找最近点的坐标。