I realize that assigning individual values to a xarray dataset takes much more time than doing the same with a numpy array. Would there be a way to accelerate that ?
Here is everything that I tested by alternatively uncommenting each line in the loop:
import numpy as np
import xarray as xr
import datetime
levels = np.arange(0,3)
simNames = ['9airports_filter0dot7_v22']
airportList = ['Windhoek', 'Atlanta', 'Taipei']
numb_variables = 11
emptyDA = xr.DataArray(np.nan,
coords = [simNames, airportList, np.arange(0, 20428), levels],
dims = ['simName', 'airport', 'profnum' , 'level'])
ds = xr.Dataset({ 'iasi': emptyDA.copy(), 'IM': emptyDA.copy(), 'IMS': emptyDA.copy(), 'err': emptyDA.copy(),
'sigma': emptyDA.copy(), 'temp': emptyDA.copy(), 'dfs': emptyDA.copy(), 'ocf': emptyDA.copy(),
'rcf': emptyDA.copy(), 'time': emptyDA.copy().astype(dtype="datetime64[ns]"), 'surfPres': emptyDA.copy() })
mat3D = np.empty( shape=( len(airportList), 20428, len(levels) ) ) # 20428 is needed for the 9 airports
mat3D[:] = np.nan
mat4D = np.empty( shape=( 1, len(airportList), 20428, len(levels) ) ) # 20428 is needed for the 9 airports
mat4D[:] = np.nan
mat5D = np.empty( shape=( numb_variables, 1, len(airportList), 20428, len(levels) ) ) # 20428 is needed for the 9 airports
mat5D[:] = np.nan
begin_time = datetime.datetime.now()
for i in range(10000):
ds['iasi'][0, 0, 0, 0] = 3.1416 # 1.08 sec
# ds['iasi'].loc['9airports_filter0dot7_v22', 'Windhoek', 0, 0] = 3.1416 # 1.97 sec
# ds['iasi'][0, 0, 0, 0].data = 3.1416 # 0.85 sec
# ds['iasi'][0, 0, 0, 0].values = 3.1416 # 0.85 sec
# ds.iasi[0, 0, 0, 0].values = 3.1416 # 0.88 sec
# a = 3.1416 # 0.0003
# mat4D[0, 0, 0, 0] = 3.1416 # 0.0008 sec
# mat3D[0, 0, 0] = 3.1416 # 0.0008 sec
# mat5D[0, 0, 0, 0, 0] = 3.1416 # 0.0009 sec
print(datetime.datetime.now() - begin_time)
