Python xarray:在不同位置使用 method='nearest' 循环处理数据
Python xarray: Processing data for a loop with method='nearest' at different locations
是否可以有一个包含多个列且坐标相同的 xarray?在下面的示例中,我创建了一个 xarray,然后我想提取不同位置的时间序列数据。但是,为此我必须创建一个 numpy 数组来存储此数据及其坐标。
#Sample from the data in the netCDF file
ds['temp'] = xr.DataArray(data=np.random.rand(2,3,4), dims=['time','lat','lon'],
coords=dict(time=pd.date_range('1900-1-1',periods=2,freq='D'),
lat=[25.,26.,27.],lon=[-85.,-84.,-83.,-82.]))
display(ds)
#lat and lon locations to extract temp values
locations=np.array([[25.6, -84.7], [26, -83], [26.5, -84.1]])
#Extract time series at different locations
temp=np.empty([ds.shape[0], len(locations)])
lat_lon=np.empty([len(locations),2])
for n in range(locations.shape[0]):
lat_lon[n,0]=ds.sel(lat=locations[n,0],
lon=locations[n,1], method='nearest').coords['lat'].values
lat_lon[n,1]=ds.sel(lat=locations[n,0],
lon=locations[n,1], method='nearest').coords['lon'].values
temp[:,n]=ds.sel(lat=locations[n,0],
lon=locations[n,1], method='nearest')
print(temp)
print(lat_lon)
#Find maximum temp for all locations:
temp=temp.max(1)
这段代码的输出是:
array([[[0.67465371, 0.0710136 , 0.03263631, 0.41050204],
[0.26447469, 0.46503577, 0.5739435 , 0.33725726],
[0.20353832, 0.01441925, 0.26728572, 0.70531547]],
[[0.75418953, 0.20321738, 0.41129902, 0.96464691],
[0.53046103, 0.88559914, 0.20876142, 0.98030988],
[0.48009467, 0.7906767 , 0.09548439, 0.61088112]]])
Coordinates:
time (time) datetime64[ns] 1900-01-01 1900-01-02
lat (lat) float64 25.0 26.0 27.0
lon (lon) float64 -85.0 -84.0 -83.0 -82.0
temp (time, lat, lon) float64 0.09061 0.6634 ... 0.5696 0.4438
Attributes: (0)
[[0.26447469 0.5739435 0.01441925]
[0.53046103 0.20876142 0.7906767 ]]
[[ 26. -85.]
[ 26. -83.]
[ 27. -84.]]
更简单地说,有没有一种方法可以在不创建中间 temp
数组的情况下为每个时间戳找到所有位置的最高温度?
创建示例数据时,您指定了 3 个纬度值和 4 个经度值。这意味着在 2D 网格上总共有 12 个值(如果我们添加时间,则为 3D)。
当您想查询3个特定点的值时,您必须单独查询每个点。据我所知,有两种方法可以做到这一点:
- 编写一个循环并将结果存储在中间数组中(您的解决方案)
- 堆叠维度并同时查询经纬度。
首先,您必须将您的位置表示为 list/array 个元组:
locations=np.array([[25.6, -84.7], [26, -83], [26.5, -84.1]])
coords=[(coord[0], coord[1]) for coord in locations]
print(coords)
[(25.6, -84.7), (26.0, -83.0), (26.5, -84.1)]
然后您将指定位置的数据插值,将纬度和经度堆叠到新维度 coord
,select 您的点。
(ds
.interp(lon=locations[:,1], lat=locations[:,0], method='linear') # interpolate on the grid
.stack(coord=['lat','lon']) # from 3x3 grid to list of 9 points
.sel(coord=coords)) # select your three points
.temp.max(dim='coord') # get largest temp value from the coord dimension
)
array([0.81316195, 0.56967184]) # your largest values at both timestamps
缺点是 xarray 不支持对未标记的多索引进行插值,这就是为什么首先需要对纬度和经度集上的网格进行插值(而不是简单地找到最近的邻居)。
是否可以有一个包含多个列且坐标相同的 xarray?在下面的示例中,我创建了一个 xarray,然后我想提取不同位置的时间序列数据。但是,为此我必须创建一个 numpy 数组来存储此数据及其坐标。
#Sample from the data in the netCDF file
ds['temp'] = xr.DataArray(data=np.random.rand(2,3,4), dims=['time','lat','lon'],
coords=dict(time=pd.date_range('1900-1-1',periods=2,freq='D'),
lat=[25.,26.,27.],lon=[-85.,-84.,-83.,-82.]))
display(ds)
#lat and lon locations to extract temp values
locations=np.array([[25.6, -84.7], [26, -83], [26.5, -84.1]])
#Extract time series at different locations
temp=np.empty([ds.shape[0], len(locations)])
lat_lon=np.empty([len(locations),2])
for n in range(locations.shape[0]):
lat_lon[n,0]=ds.sel(lat=locations[n,0],
lon=locations[n,1], method='nearest').coords['lat'].values
lat_lon[n,1]=ds.sel(lat=locations[n,0],
lon=locations[n,1], method='nearest').coords['lon'].values
temp[:,n]=ds.sel(lat=locations[n,0],
lon=locations[n,1], method='nearest')
print(temp)
print(lat_lon)
#Find maximum temp for all locations:
temp=temp.max(1)
这段代码的输出是:
array([[[0.67465371, 0.0710136 , 0.03263631, 0.41050204],
[0.26447469, 0.46503577, 0.5739435 , 0.33725726],
[0.20353832, 0.01441925, 0.26728572, 0.70531547]],
[[0.75418953, 0.20321738, 0.41129902, 0.96464691],
[0.53046103, 0.88559914, 0.20876142, 0.98030988],
[0.48009467, 0.7906767 , 0.09548439, 0.61088112]]])
Coordinates:
time (time) datetime64[ns] 1900-01-01 1900-01-02
lat (lat) float64 25.0 26.0 27.0
lon (lon) float64 -85.0 -84.0 -83.0 -82.0
temp (time, lat, lon) float64 0.09061 0.6634 ... 0.5696 0.4438
Attributes: (0)
[[0.26447469 0.5739435 0.01441925]
[0.53046103 0.20876142 0.7906767 ]]
[[ 26. -85.]
[ 26. -83.]
[ 27. -84.]]
更简单地说,有没有一种方法可以在不创建中间 temp
数组的情况下为每个时间戳找到所有位置的最高温度?
创建示例数据时,您指定了 3 个纬度值和 4 个经度值。这意味着在 2D 网格上总共有 12 个值(如果我们添加时间,则为 3D)。
当您想查询3个特定点的值时,您必须单独查询每个点。据我所知,有两种方法可以做到这一点:
- 编写一个循环并将结果存储在中间数组中(您的解决方案)
- 堆叠维度并同时查询经纬度。
首先,您必须将您的位置表示为 list/array 个元组:
locations=np.array([[25.6, -84.7], [26, -83], [26.5, -84.1]])
coords=[(coord[0], coord[1]) for coord in locations]
print(coords)
[(25.6, -84.7), (26.0, -83.0), (26.5, -84.1)]
然后您将指定位置的数据插值,将纬度和经度堆叠到新维度 coord
,select 您的点。
(ds
.interp(lon=locations[:,1], lat=locations[:,0], method='linear') # interpolate on the grid
.stack(coord=['lat','lon']) # from 3x3 grid to list of 9 points
.sel(coord=coords)) # select your three points
.temp.max(dim='coord') # get largest temp value from the coord dimension
)
array([0.81316195, 0.56967184]) # your largest values at both timestamps
缺点是 xarray 不支持对未标记的多索引进行插值,这就是为什么首先需要对纬度和经度集上的网格进行插值(而不是简单地找到最近的邻居)。