使用xarray读取多个坐标 [英] Read multiple coordinates with xarray

查看:415
本文介绍了使用xarray读取多个坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xarray从openDAP服务器读取单点数据,然后将xarray对象转换为dataframe.这很好.我想在一个呼叫中读取多个点,但是我不知道这样做是最好的方法.

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.

这是我用于单点代码:

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()

我要阅读多点内容:

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

这是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:

我的问题是:

  • 这是用xarray检索多点的最佳方法吗?
  • 如何从结果数据帧的单个点提取数据?

提前谢谢!

推荐答案

我认为您想要

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()

这篇关于使用xarray读取多个坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆