简体   繁体   中英

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. I created a 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?

The KeyError is a result of the fact that there is not an exact match for the latitude 84.36 in the latitude coordinate. The exact match looks like it would be 84.36244 (or perhaps 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.

In this case it looks like you might want something like method='nearest' :

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

See the documentation for Dataset.sel for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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