0

Trying to append values to a dataset I need to create a blank xarray dataset with two+ dimensions where one is fixed and the other is incrementing to two+ output sets

The code example is something like

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]

This would be useful for taking data from a weather sensor over 24hours each day of the year and appending to the dataset

SKArm
  • 71
  • 1
  • 7
  • 1
    Does https://stackoverflow.com/questions/33435953/is-it-possible-to-append-to-an-xarray-dataset answer your question? – shoyer Jul 03 '18 at 17:31

1 Answers1

0

You can assign a tuple (dimensions, values) to the dataset,

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 ...
Keisuke FUJII
  • 1,306
  • 9
  • 13
  • No, One your missing the initialization statement of the Dataset `Data` or refrance to the line in my initial code. That may be just my pet peeve, but it is like a math statement with no equality sign to me. Second, what I am talking about is adding values as needed. What you posted does work but not for the usage case I am working for. Notice that the values to enter the Dataset are from a dictionary 'amp={i:i*np.sin(time) for i in range(0, 10)}' meaning that there is a set of them. And each entity needs to be mapped in the Dataset to the `ith` dimension location like a ixN matrix – SKArm Jun 29 '18 at 19:03