Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 519
user308827 Avatar asked Feb 08 '17 23:02

user308827


1 Answers

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

like image 161
jhamman Avatar answered Nov 06 '22 17:11

jhamman