简体   繁体   English

所选点的 xarray 掩码

[英]xarray mask for selected points

I can use slicing to select a region when opening netcdf files in xarray, using preprocess ie:在 xarray 中打开 netcdf 文件时,我可以使用切片来选择一个区域,使用预处理即:

SSA=dict(lat=slice(-38,-34),lon=slice(138,141))

def Mask(ds):

     return ds.sel(**SSA)

xr.open_mfdataset(filelist, preprocess=Mask)

but what is the most efficient way to extract the data for a list of seperate points by latitude and longitude??但是,按纬度和经度提取单独点列表的数据的最有效方法是什么?

A list of points can be selected using a DataArray as the indexer.可以使用DataArray作为索引器来选择点列表。 This will result in the array being reindexed along the indexer's coordinates.这将导致数组沿索引器的坐标重新索引。

Straight from the docs on More Advanced Indexing :直接来自更高级索引的文档:

In [78]: da = xr.DataArray(np.arange(56).reshape((7, 8)), dims=['x', 'y'])

In [79]: da
Out[79]: 
<xarray.DataArray (x: 7, y: 8)>
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47],
       [48, 49, 50, 51, 52, 53, 54, 55]])
Dimensions without coordinates: x, y

In [80]: da.isel(x=xr.DataArray([0, 1, 6], dims='z'),
   ....:         y=xr.DataArray([0, 1, 0], dims='z'))
   ....: 
Out[80]: 
<xarray.DataArray (z: 3)>
array([ 0,  9, 48])
Dimensions without coordinates: z

The indexing array can also be easily pulled out of a pandas DataFrame , with something like da.sel(longitude=df.longitude.to_xarray(), latitude=df.latitude.to_xarray()) , which will result in the DataArray being reindexed by the DataFrame's index.索引数组也可以很容易地从DataFrame ,类似于da.sel(longitude=df.longitude.to_xarray(), latitude=df.latitude.to_xarray()) ,这将导致DataArray被重新索引通过DataFrame 的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM