Xarray沿坐标在数据集中附加值

时间:2018-06-29 04:44:13

标签: python-xarray

尝试将值附加到数据集 我需要创建一个具有两个以上维度的空白xarray数据集,其中一个是固定的,另一个正在递增至两个以上的输出集

代码示例类似于

import numpy as np
import xarray as xr

#fixed coordinte diminsion 
time=np.linspace(0, 4*np.pi)

#dummy data to insert
#ith is the moving coordainte 
amp={i:i*np.sin(time) for i in range(0, 10)}
phase={i:i*np.cos(time) for i in range(0, 10)}

#create the xr dataset
Data=xr.Dataset(coords={'time':time, 'ith':0})
#create the holding loctions to incoming data
Data['amp']=None; Data['phase']=None


#now need to increase i in the dataset and append data
#and this is where I am stuck
for i in range(0, 10):
   Data['amp'].loc[dict(ith=i)]=amp[i]
   Data['phase'].loc[dict(ith=i)]=phase[i]

这对于一年中每天24小时从天气传感器获取数据并将其附加到数据集中很有用

1 个答案:

答案 0 :(得分:0)

您可以将元组(维度,值)分配给数据集

In [4]: amp = time * np.sin(time)
In [6]: Data['amp'] = ('time', ), amp  # dimensions, value
In [8]: phase = time * np.cos(time)
In [9]: Data['phase'] = ('time', ), phase

In [10]: Data
Out[10]: 
<xarray.Dataset>
Dimensions:  (time: 50)
Coordinates:
  * time     (time) float64 0.0 0.2565 0.5129 0.7694 1.026 1.282 1.539 1.795...
    ith      int64 0
Data variables:
    amp      (time) float64 0.0 0.06505 0.2517 0.5352 0.8772 1.229 1.538 ...
    phase    (time) float64 0.0 0.2481 0.4469 0.5527 0.5318 0.3648 0.04932 ...