简体   繁体   中英

Extracting nearest lat-lon and time value from netcdf using xarray

I have a netCDF file with foll. structure:

<xarray.Dataset>
Dimensions:    (latitude: 94, longitude: 192, time: 366)
Coordinates:
  * longitude  (longitude) float32 -180.0 -178.125 -176.25 -174.375 -172.5 ...
  * latitude   (latitude) float32 88.5419 86.6532 84.7532 82.8508 80.9474 ...
  * time       (time) datetime64[ns] 2016-01-01 2016-01-02 2016-01-03 ...
Data variables:
    m2t      (time, latitude, longitude) float64 246.5 246.4 246.4 246.4
    pre      (time, latitude, longitude) float64 9.988e-08 9.988e-08 ...
Attributes:
    Conventions: CF-1.0

How do I extract values for a grid cell for a specific latitude and longitude (say 86.45, -156.25) and time (say 2016-01-10)? It is possible that the exact latitude/longitude value is not in the coordinates in which case we want the closest latitude/longitude value

I can extract value for a specific longitude like this:

_hndl_nc.sel(longitude=(_hndl_nc.longitude == -20))

However, since -20 is not present in coordinates for longitude, this does not work.

You are quite close although you don't have quite the right syntax yet:

_hndl_nc.sel(time='2016-01-10', longitude=-170.0, latitude=-20.0, method='nearest')

see also: Read multiple coordinates with xarray

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