0

I want to merge multiple .nc files into one. Each of the .nc files corresponds to a timestep which should be added together to have one .nc file with all data for all dates.

When I execute the following line I'll get the error that no dimensions found although several are available (time, latitude, longitude). Why it doesn't work?

Code:

ds = xr.open_mfdataset(os.getcwd() + '/test/*.nc')

Traceback:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-40-c0e9d8bf97e7> in <module>
----> 1 ds = xr.open_mfdataset(os.getcwd() + '/test/*.nc')

/opt/conda/lib/python3.7/site-packages/xarray/backends/api.py in open_mfdataset(paths, chunks, concat_dim, compat, preprocess, engine, data_vars, coords, combine, parallel, join, attrs_file, combine_attrs, **kwargs)
    945                 coords=coords,
    946                 join=join,
--> 947                 combine_attrs=combine_attrs,
    948             )
    949         else:

/opt/conda/lib/python3.7/site-packages/xarray/core/combine.py in combine_by_coords(data_objects, compat, data_vars, coords, fill_value, join, combine_attrs, datasets)
    903                 compat=compat,
    904                 join=join,
--> 905                 combine_attrs=combine_attrs,
    906             )
    907             concatenated_grouped_by_data_vars.append(concatenated)

/opt/conda/lib/python3.7/site-packages/xarray/core/combine.py in _combine_single_variable_hypercube(datasets, fill_value, data_vars, coords, compat, join, combine_attrs)
    602         )
    603 
--> 604     combined_ids, concat_dims = _infer_concat_order_from_coords(list(datasets))
    605 
    606     if fill_value is None:

/opt/conda/lib/python3.7/site-packages/xarray/core/combine.py in _infer_concat_order_from_coords(datasets)
    128     if len(datasets) > 1 and not concat_dims:
    129         raise ValueError(
--> 130             "Could not find any dimension coordinates to use to "
    131             "order the datasets for concatenation"
    132         )

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

Examples for one of the .nc files I want to merge (all are same):

<xarray.Dataset>
Dimensions:                  (columns: 321, rows: 321, Time: 1)
Coordinates:
    longitude                (columns, rows) float32 ...
    latitude                 (columns, rows) float32 ...
    time                     (Time) datetime64[ns] 2010-01-02T02:00:00
Dimensions without coordinates: columns, rows, Time
Data variables:
    cdr_surface_albedo       (Time, columns, rows) float32 ...
    cdr_surface_temperature  (Time, columns, rows) float32 ...
Attributes:
    standard_name:          surface_albedo
    long_name:              NOAA CDR of surface broadband albedo
    valid_range:            [0. 1.]
    units:                  1
    grid_mapping:           crs
    coverage_content_type:  physicalMeasurement
ScienceNoob
  • 181
  • 1
  • 2
  • 14
  • Is the coordinate variable "time" always with the value `2010-01-02T02:00:00` for every dataset ? If that's the case, it would explain why xarray is unable to find a dimension to use for concatenation. – Abel Feb 28 '22 at 13:20

1 Answers1

0

You could try NCO's ncrcat:

ncrcat in*.nc out.nc
Charlie Zender
  • 5,929
  • 14
  • 19