Read multiple coordinates with xarray

99封情书 提交于 2019-12-24 07:51:40

问题


I'm using xarray to read single point data from an openDAP server, then I convert the xarray object to dataframe. This works fine. I would like to read multiple points in a single call, but I don't which is the best approach to do so.

This is the code I'm using for a single point:

import pandas as pd
import xarray as xr

url = 'http://nomads.ncep.noaa.gov:9090/dods/gfs_0p25/gfs20161111/gfs_0p25_00z'
lats =  [40.1,40.5,42.3]
lons =  [1.02,1.24,1.84]
vars = ['dswrfsfc', 'tmp2m', 'pressfc']

ds = xr.open_dataset(url)

data_single  = ds.sel(lon=lons[0], lat=lats[0], method='nearest')    
ts_dataframe_single = data_single[vars].to_dataframe()

For reading multiple points I do:

data  = ds.sel(lon=lons, lat=lats, method='nearest')
ts_dataframe = data[vars].to_dataframe()

And this is the output of data.coords:

data.coords
Out[10]: 
Coordinates:
  * time     (time) datetime64[ns] 2016-11-11 2016-11-11T03:00:00 ...
  * lev      (lev) float64 1e+03 975.0 950.0 925.0 900.0 850.0 800.0 750.0 ...
  * lat      (lat) float64 40.0 40.5 42.25
  * lon      (lon) float64 1.0 1.25 1.75

When I convert to a dataframe the resulting object contains a mix of time and coordinates in the timestamp. This is how it looks:

My questions are:

  • Is this the best way to retrieve multiple point with xarray?
  • How do I extract the data from a single point of the resulting dataframe?

Thanks in advance!


回答1:


I think you want sel_points instead of sel. So, something like this:

data  = ds.sel_points(lon=lons, lat=lats, method='nearest')
ts_dataframe = data[vars].to_dataframe()



回答2:


Another method would be slicing

data = ds.sel(lat=slice(40.1,42.3), lon=slice(1.02,1.84))

However, you get more points than you asked for. It's fast, though.



来源:https://stackoverflow.com/questions/40544846/read-multiple-coordinates-with-xarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!