combine_by_coords() unexpectedly fails due to non-monotonic global indexes #5722
-
I have a collection of datasets in which I think should be successfully combined into a single dataset using
Here is the code to reproduce the problem: import xarray as xr
import pathlib
files = pathlib.Path('.').glob('*.nc')
data = list()
for f in files:
data.append(xr.open_dataset(f))
combined_data = xr.combine_by_coords(data,combine_attrs='drop_conflicts') The NetCDF files are attached: |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
One workaround (as suggested in #5194) is to use import xarray as xr
import pathlib
files = pathlib.Path('.').glob('*.nc')
data = list()
for f in files:
data.append(xr.open_dataset(f))
# combine_nested() works
nested_list = [[data[0],data[1]],
[data[2],data[3]]]
combined_data = xr.combine_nested(nested_list,
concat_dim=['tune_freq','ref_level'],
combine_attrs='drop_conflicts')
combined_data = combined_data.sortby(['ref_level','tune_freq'])
combined_data.evm.plot()
But I still can't understand why this doesn't work with |
Beta Was this translation helpful? Give feedback.
-
Could I clarify the question — is this a feature request for combining non-monotonic indexes with |
Beta Was this translation helpful? Give feedback.
-
Hi @jrmagers - thanks for this clear and reproducible bug report. Whilst the coordinates are monotonic (the easiest way to check is to extract the underlying pandas index via
The combining is failing when it tries to decide how to align each tile, because the tiles are of different sizes, so they can't simply be lined up next to one another like you probably expected. How it handles alignment is dictated through the options that get sent down to If you try again but passing Traceback (most recent call last):
File "combine.py", line 16, in <module>
combined_data = xr.combine_by_coords(data, join='exact', combine_attrs='drop_conflicts')
File "/home/tom/Documents/Work/Code/xarray/xarray/core/combine.py", line 897, in combine_by_coords
concatenated = _combine_single_variable_hypercube(
File "/home/tom/Documents/Work/Code/xarray/xarray/core/combine.py", line 614, in _combine_single_variable_hypercube
concatenated = _combine_nd(
File "/home/tom/Documents/Work/Code/xarray/xarray/core/combine.py", line 218, in _combine_nd
combined_ids = _combine_all_along_first_dim(
File "/home/tom/Documents/Work/Code/xarray/xarray/core/combine.py", line 254, in _combine_all_along_first_dim
new_combined_ids[new_id] = _combine_1d(
File "/home/tom/Documents/Work/Code/xarray/xarray/core/combine.py", line 277, in _combine_1d
combined = concat(
File "/home/tom/Documents/Work/Code/xarray/xarray/core/concat.py", line 242, in concat
return f(
File "/home/tom/Documents/Work/Code/xarray/xarray/core/concat.py", line 440, in _dataset_concat
align(*datasets, join=join, copy=False, exclude=[dim], fill_value=fill_value)
File "/home/tom/Documents/Work/Code/xarray/xarray/core/alignment.py", line 323, in align
raise ValueError(f"indexes along dimension {dim!r} are not equal")
ValueError: indexes along dimension 'ref_level' are not equal I'm not quite sure how you wanted
This was hard to spot from the error raised though - we could probably do with having either clearer error reporting, or more strict defaults. I wonder if we should make |
Beta Was this translation helpful? Give feedback.
-
Thanks for the explanation. Ultimately, my "tiles" look like this: Which explains why Thus, given this arrangement of the tiles, |
Beta Was this translation helpful? Give feedback.
Hi @jrmagers - thanks for this clear and reproducible bug report.
Whilst the coordinates are monotonic (the easiest way to check is to extract the underlying pandas index via
ds.ref_level.to_index().is_monotonic
), I think the problem is caused by the fact that your datasets do not have consistent lengths alongref_level
. In factThe combining is failing when it tries to decide how to align each tile, because the tiles are of different sizes, so they can't simply be lined up next to one another like you probably expecte…