python-xarray: open_mfdataset concat along two dimensions
xarray.open_mfdataset
does not support 2d merges. What you will need to do is use concat
along the second dimension:
import os
import xarray as xr
ens_list = []
for num in range(1, 11):
ens = 'ens%d' % num
ens_list.append(xr.open_mfdataset(os.path.join(ens, '*NPac*')))
ds = xr.concat(ens_list, dim='ensemble')
This is a common problem that xarray users run into. It is quite difficult, however, to write a generalized ND concat routine.
I wrote the following function as a workaround for my own use case: https://gist.github.com/jnhansen/fa474a536201561653f60ea33045f4e2
It works with arbitrary dimensions, but currently requires that the same variables exist in each file/dataset.
In my case I have a number of tiles (split along e.g. lat
, lon
, and time
):
ds = auto_merge('data/part*.nc')
This will execute immediately as it returns only a view of the data (just like xarray.open_mfdataset
would do).