简体   繁体   English

从使用xarray创建的DataArray中选择零件(python)

[英]select parts out of a DataArray created with xarray (python)

    ds = xr.open_dataset(f)
    hlos = ds.rayleigh_hloswind_windresult_rayleigh_wind_velocity.data
    lats = ds.rayleigh_geolocation_windresult_geolocation_latitude_cog.data 

>>> hlos
array([  419., -1013.,  -584., ..., -6791., 32767., 32767.], dtype=float32)
>>> lats
array([84.36244 , 84.359972, 84.371422, ..., 83.43806 , 83.672662,
       83.513285])

f is a netcdf file with the variable hlos which is the wind velocity and lats which is the latitude. f是一个netcdf文件,其变量hlos是风速,而lats是纬度。 I created a DataArray: 我创建了一个DataArray:

da_lat = xr.DataArray(hlos, dims=['lat'], coords = {'lat': lats})
>>> da_lat
<xarray.DataArray (lat: 19703)>
array([  419., -1013.,  -584., ..., -6791., 32767., 32767.],
      dtype=float32)
Coordinates:
  * lat      (lat) float64 84.36 84.36 84.37 ... 83.67 83.51

Now I would like to select like: da_lat.sel(lat = 84.36).values , but this gives me a KeyError:84.36 Does anyone knoe what's the problem in this case? 现在,我想选择: da_lat.sel(lat = 84.36).values ,但这给了我一个da_lat.sel(lat = 84.36).values :84.36有人知道这种情况是什么问题吗?

The KeyError is a result of the fact that there is not an exact match for the latitude 84.36 in the latitude coordinate. KeyError是由于纬度坐标中的纬度84.36不完全匹配而导致的。 The exact match looks like it would be 84.36244 (or perhaps 84.359972). 精确匹配看起来像是84.36244(或84.359972)。 Xarray offers a nice way of working around this, as you can specify a method in the sel method which controls how it deals with inexact matches. Xarray提供了解决这个工作,你可以指定一个很好的方式methodsel控制如何与非精确匹配交易方法。

In this case it looks like you might want something like method='nearest' : 在这种情况下,您可能需要诸如method='nearest'类的东西:

da_lat.sel(lat=84.36, method='nearest')

See the documentation for Dataset.sel for more details. 有关更多详细信息,请参见Dataset.sel文档

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

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