1
votes

I have a list of NetCDF files that I would like to open with the xarray.open_mfdataset function.

This would normally be trivial, however I am running into an issue because the files I cam trying to open do not have any "time" dimension included in them:

data
Out[51]: 
<xarray.Dataset>
Dimensions:  (lat: 850, lon: 1500)
Coordinates:
  * lat      (lat) float64 54.98 54.94 54.9 54.86 ... 21.14 21.1 21.06 21.02
  * lon      (lon) float64 -126.0 -125.9 -125.9 -125.9 ... -66.1 -66.06 -66.02
Data variables:
    Data     (lat, lon) float32 ...

When I try to open my list of files with open_mfdataset, I of course get an error:

xr.open_mfdataset(files)

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

I however do have a list of dates corresponding to each file:

dates
Out[54]: 
array([datetime.datetime(2009, 1, 1, 0, 0),
       datetime.datetime(2009, 1, 2, 0, 0),
       datetime.datetime(2009, 1, 3, 0, 0), ...,
       datetime.datetime(2019, 12, 29, 0, 0),
       datetime.datetime(2019, 12, 30, 0, 0),
       datetime.datetime(2019, 12, 31, 0, 0)], dtype=object)

I assume there is some way I add a time dimension to each file and open them all with open_mfdataset, possibly with the "preprocess" argument.

Thanks for any help.

1
Could you try ds = xarray.open_mfdataset(files, concat_dim="time"). And then use ds = ds.assign_coords(time=dates)mathause
I get the same error when using open_mfdataset: ValueError: Could not find any dimension coordinates to use to order the datasets for concatenationhm8

1 Answers

3
votes

Here is my solution:

Create a function which adds a time dimension to a DataArray, and fill it with a arbitrary date:

def add_time_dim(xda):
    xda = xda.expand_dims(time = [datetime.now()])
    return xda

Then, pass this function to the preprocess argument when running the open_mfdataset functions:

data = xr.open_mfdataset(files, preprocess = add_time_dim)

Finally, fill the time dimension with my dates:

data['time'] = dates