I have a series of files named 0.nc, 1.nc, 2.nc, ... and am looking to use open_mfdataset to open them all at once, in order of filename. However, when I run the command:

labels = xarray.open_mfdataset('*.nc') 

I get the error

ValueError: Could not find any dimension coordinates to use to order the datasets for concatenation 

I have tried adding a coordinate by:

for i in range(0,10): labels = xarray.open_dataset(f'{i}.nc') labels = labels.assign_coords({'name' : i}) labels.to_netcdf(f'.{i}_named.nc') 

And then loading those files, but to no avail (same error).

What is a way I can load these files as one dataset where I dont encounter these errors?

1

1 Answer

When you call open_mfdataset without any arguments, xarray attempts to automatically infer the structure of your data and the way you would like to concatenate it. In some cases, such as this one, the automatic inference fails. You can resolve this by providing more explicit instructions to tell xarray how to concatenate the data.

See the docs on combining data, which define the important concepts and describe many of the arguments you’ll use, and then check out the arguments to xr.open_mfdataset - specifically, provide the combine and concat_dim arguments.

For example, you might provide:

ds = xarray.open_mfdataset( [f'{i}.nc' for i in range(10)], concat_dim=[ pd.Index(np.arange(10), name="new_dim"), ], combine="nested", ) 
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.