xarray选择具有多维坐标的最近经度/经度

问题描述 投票:0回答:1

我有一个xarray数据集,其纬度和经度坐标不规则。我的目标是在最接近特定经度/纬度的点上找到变量的值。

由于xy尺寸不是经/纬度值,因此在这种情况下似乎无法单独使用ds.sel()方法。是否存在以xarray为中心的方法,通过参考多维经纬度尺寸来定位最接近所需纬度/经度的点?例如,我要提取最接近lat=21.2lon=-122.68的SPEED值。

下面是示例数据集...

lats = np.array([[21.138  , 21.14499, 21.15197, 21.15894, 21.16591],
                 [21.16287, 21.16986, 21.17684, 21.18382, 21.19079],
                 [21.18775, 21.19474, 21.20172, 21.2087 , 21.21568],
                 [21.21262, 21.21962, 21.22661, 21.23359, 21.24056],
                 [21.2375 , 21.2445 , 21.25149, 21.25848, 21.26545]])  

lons = np.array([[-122.72   , -122.69333, -122.66666, -122.63999, -122.61331],
                 [-122.7275 , -122.70082, -122.67415, -122.64746, -122.62078],
                 [-122.735  , -122.70832, -122.68163, -122.65494, -122.62825],
                 [-122.7425 , -122.71582, -122.68912, -122.66243, -122.63573],
                 [-122.75001, -122.72332, -122.69662, -122.66992, -122.64321]])

speed = np.array([[10.934007, 10.941321, 10.991583, 11.063932, 11.159435],
                  [10.98778 , 10.975482, 10.990983, 11.042522, 11.131154],
                  [11.013505, 11.001573, 10.997754, 11.03566 , 11.123781],
                  [11.011163, 11.000227, 11.010223, 11.049   , 11.1449  ],
                  [11.015698, 11.026604, 11.030653, 11.076904, 11.201464]])

ds = xarray.Dataset({'SPEED':(('x', 'y'),speed)},
                    coords = {'latitude': (('x', 'y'), lats),
                              'longitude': (('x', 'y'), lons)},
                    attrs={'variable':'Wind Speed'})

ds的值:

<xarray.Dataset>
Dimensions:    (x: 5, y: 5)
Coordinates:
    latitude   (x, y) float64 21.14 21.14 21.15 21.16 ... 21.25 21.26 21.27
    longitude  (x, y) float64 -122.7 -122.7 -122.7 ... -122.7 -122.7 -122.6
Dimensions without coordinates: x, y
Data variables:
SPEED      (x, y) float64 10.93 10.94 10.99 11.06 ... 11.03 11.03 11.08 11.2
Attributes:
    variable:  Wind Speed

同样,ds.sel(latitude=21.2, longitude=-122.68)不起作用,因为纬度和经度不是数据集的维度。

python python-xarray
1个回答
0
投票

我不确定您的尺寸是否正确。您有25个速度值(我假设在不同的位置),但25 x 25 = 125个位置。如果您想将速度映射到某个位置并找到最接近的值,我将执行以下操作:

import numpy as np
import pandas as pd
import xarray

lats = np.array([21.138, 21.14499, 21.15197, 21.15894, 21.16591])

lons = np.array([-122.72, -122.69333, -122.66666, -122.63999, -122.61331])

speed = np.array([[10.934007, 10.941321, 10.991583, 11.063932, 11.159435],
                 [10.98778, 10.975482, 10.990983, 11.042522, 11.131154],
                 [11.013505, 11.001573, 10.997754, 11.03566, 11.123781],
                 [11.011163, 11.000227, 11.010223, 11.049, 11.1449],
                 [11.015698, 11.026604, 11.030653, 11.076904, 11.201464]])

ds = xarray.Dataset({'SPEED': (('latitude', 'longitude'), speed)},
                     coords={'latitude': lats,
                             'longitude': lons},
                     attrs={'variable': 'Wind Speed'})

print(ds)
print()
print(ds.sel(latitude=21.15, longitude=-122.65, method='nearest'))

注意,必须将25个速度值与25个位置相匹配,我必须拆下琴架和lons。

© www.soinside.com 2019 - 2024. All rights reserved.