1

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?

OblivioN
  • 13
  • 2
  • 1
    Can you add some detail on what you datasets are like? A reproducible example would go a long way. The answer in this issue may help: https://stackoverflow.com/questions/75203572/xarray-mfdataset-combining-files-with-different-variables-using-cfgrib-engine/75227739#75227739 – jhamman Jan 26 '23 at 05:03

1 Answers1

1

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",

)
Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
  • Thanks so much for the answer! Sadly, I am met with the following error: ValueError: concat_dims has length 10 but the datasets passed are nested in a 1-dimensional structure Which doesn't make sense to me. The data is a series of 2d arrays (42 arrays in each .nc file, which I want to read as one), each representing an image (like a CT scan). How can that be 1-dimensional? What I expected to be the outcome of the code you provided was it to see that I wanted to combine in order 0,1,2,3,...,9, as is specified in the concat_dim=line. Any suggestions as to what I can look into next? – OblivioN Jan 26 '23 at 10:40
  • Oops sorry - I forgot the syntax changed. Concat dim should be a list of dims matching the nested shape of the data. Updated. – Michael Delgado Jan 26 '23 at 11:29