60
60
from xarray .core .types import PDDatetimeUnitOptions
61
61
from xarray .core .utils import module_available
62
62
from xarray .namedarray .pycompat import array_type
63
+ from xarray .structure .alignment import AlignmentError
63
64
from xarray .tests import (
64
65
assert_allclose ,
65
66
assert_array_equal ,
@@ -4795,19 +4796,19 @@ class TestOpenMFDatasetWithDataVarsAndCoordsKw:
4795
4796
var_name = "v1"
4796
4797
4797
4798
@contextlib .contextmanager
4798
- def setup_files_and_datasets (self , fuzz = 0 ):
4799
+ def setup_files_and_datasets (self , * , fuzz = 0 , new_combine_kwargs : bool = False ):
4799
4800
ds1 , ds2 = self .gen_datasets_with_common_coord_and_time ()
4800
4801
4801
4802
# to test join='exact'
4802
4803
ds1 ["x" ] = ds1 .x + fuzz
4803
4804
4804
- with set_options (use_new_combine_kwarg_defaults = True ):
4805
- with create_tmp_file () as tmpfile1 :
4806
- with create_tmp_file () as tmpfile2 :
4807
- # save data to the temporary files
4808
- ds1 .to_netcdf (tmpfile1 )
4809
- ds2 .to_netcdf (tmpfile2 )
4805
+ with create_tmp_file () as tmpfile1 :
4806
+ with create_tmp_file () as tmpfile2 :
4807
+ # save data to the temporary files
4808
+ ds1 .to_netcdf (tmpfile1 )
4809
+ ds2 .to_netcdf (tmpfile2 )
4810
4810
4811
+ with set_options (use_new_combine_kwarg_defaults = new_combine_kwargs ):
4811
4812
yield [tmpfile1 , tmpfile2 ], [ds1 , ds2 ]
4812
4813
4813
4814
def gen_datasets_with_common_coord_and_time (self ):
@@ -4850,7 +4851,7 @@ def test_open_mfdataset_does_same_as_concat(
4850
4851
combine = combine ,
4851
4852
concat_dim = concat_dim ,
4852
4853
join = join ,
4853
- compat = "no_conflicts " ,
4854
+ compat = "equals " ,
4854
4855
) as ds :
4855
4856
ds_expect = xr .concat (
4856
4857
[ds1 , ds2 ], data_vars = opt , dim = "t" , join = join , compat = "equals"
@@ -4935,25 +4936,29 @@ def test_open_mfdataset_dataset_attr_by_coords(self) -> None:
4935
4936
ds .close ()
4936
4937
ds .to_netcdf (f )
4937
4938
4938
- with xr .open_mfdataset (files , combine = "nested" , concat_dim = "t" ) as ds :
4939
- assert ds .test_dataset_attr == 10
4939
+ with set_options (use_new_combine_kwarg_defaults = True ):
4940
+ with xr .open_mfdataset (files , combine = "nested" , concat_dim = "t" ) as ds :
4941
+ assert ds .test_dataset_attr == 10
4940
4942
4941
4943
def test_open_mfdataset_dataarray_attr_by_coords (self ) -> None :
4942
4944
"""
4943
4945
Case when an attribute of a member DataArray differs across the multiple files
4944
4946
"""
4945
- with self .setup_files_and_datasets () as (files , [ds1 , ds2 ]):
4947
+ with self .setup_files_and_datasets (new_combine_kwargs = True ) as (
4948
+ files ,
4949
+ [ds1 , ds2 ],
4950
+ ):
4946
4951
# Give the files an inconsistent attribute
4947
4952
for i , f in enumerate (files ):
4948
4953
ds = open_dataset (f ).load ()
4949
4954
ds ["v1" ].attrs ["test_dataarray_attr" ] = i
4950
4955
ds .close ()
4951
4956
ds .to_netcdf (f )
4952
4957
4953
- with xr .open_mfdataset (
4954
- files , data_vars = "minimal" , combine = "nested" , concat_dim = "t"
4955
- ) as ds :
4956
- assert ds ["v1" ].test_dataarray_attr == 0
4958
+ with xr .open_mfdataset (
4959
+ files , data_vars = "minimal" , combine = "nested" , concat_dim = "t"
4960
+ ) as ds :
4961
+ assert ds ["v1" ].test_dataarray_attr == 0
4957
4962
4958
4963
@pytest .mark .parametrize (
4959
4964
"combine, concat_dim" , [("nested" , "t" ), ("by_coords" , None )]
@@ -4980,9 +4985,13 @@ def test_open_mfdataset_dataarray_attr_by_coords(self) -> None:
4980
4985
def test_open_mfdataset_exact_join_raises_error (
4981
4986
self , combine , concat_dim , kwargs
4982
4987
) -> None :
4983
- with self .setup_files_and_datasets (fuzz = 0.1 ) as (files , _ ):
4988
+ with self .setup_files_and_datasets (fuzz = 0.1 , new_combine_kwargs = True ) as (
4989
+ files ,
4990
+ _ ,
4991
+ ):
4984
4992
if combine == "by_coords" :
4985
4993
files .reverse ()
4994
+
4986
4995
with pytest .raises (
4987
4996
ValueError , match = "cannot align objects with join='exact'"
4988
4997
):
@@ -4997,17 +5006,15 @@ def test_open_mfdataset_exact_join_raises_error(
4997
5006
def test_open_mfdataset_defaults_with_exact_join_warns_as_well_as_raising (
4998
5007
self ,
4999
5008
) -> None :
5000
- with self .setup_files_and_datasets (fuzz = 0.1 ) as (files , _ ):
5001
- with set_options (use_new_combine_kwarg_defaults = False ):
5002
- files .reverse ()
5003
- with pytest .warns (
5004
- FutureWarning ,
5005
- match = "will change from data_vars='all' to data_vars='minimal'" ,
5006
- ):
5007
- with pytest .raises (
5008
- ValueError , match = "cannot align objects with join='exact'"
5009
- ):
5010
- open_mfdataset (files , combine = "by_coords" , join = "exact" )
5009
+ with self .setup_files_and_datasets (fuzz = 0.1 , new_combine_kwargs = True ) as (
5010
+ files ,
5011
+ _ ,
5012
+ ):
5013
+ files .reverse ()
5014
+ with pytest .raises (
5015
+ ValueError , match = "cannot align objects with join='exact'"
5016
+ ):
5017
+ open_mfdataset (files , combine = "by_coords" )
5011
5018
5012
5019
def test_common_coord_when_datavars_all (self ) -> None :
5013
5020
opt : Final = "all"
@@ -5030,7 +5037,10 @@ def test_common_coord_when_datavars_all(self) -> None:
5030
5037
def test_common_coord_when_datavars_minimal (self ) -> None :
5031
5038
opt : Final = "minimal"
5032
5039
5033
- with self .setup_files_and_datasets () as (files , [ds1 , ds2 ]):
5040
+ with self .setup_files_and_datasets (new_combine_kwargs = True ) as (
5041
+ files ,
5042
+ [ds1 , ds2 ],
5043
+ ):
5034
5044
# open the files using data_vars option
5035
5045
with open_mfdataset (
5036
5046
files , data_vars = opt , combine = "nested" , concat_dim = "t"
@@ -5065,15 +5075,18 @@ def test_invalid_data_vars_value_should_fail(self) -> None:
5065
5075
def test_open_mfdataset_warns_when_kwargs_set_to_different (
5066
5076
self , combine , concat_dim , kwargs
5067
5077
) -> None :
5068
- with self .setup_files_and_datasets () as (files , [ds1 , ds2 ]):
5078
+ with self .setup_files_and_datasets (new_combine_kwargs = True ) as (
5079
+ files ,
5080
+ [ds1 , ds2 ],
5081
+ ):
5069
5082
if combine == "by_coords" :
5070
5083
files .reverse ()
5071
5084
with pytest .raises (
5072
- ValueError , match = "Previously the default was compat='no_conflicts'"
5085
+ ValueError , match = "Previously the default was ` compat='no_conflicts'` "
5073
5086
):
5074
5087
open_mfdataset (files , combine = combine , concat_dim = concat_dim , ** kwargs )
5075
5088
with pytest .raises (
5076
- ValueError , match = "Previously the default was compat='equals'"
5089
+ ValueError , match = "Previously the default was ` compat='equals'` "
5077
5090
):
5078
5091
xr .concat ([ds1 , ds2 ], dim = "t" , ** kwargs )
5079
5092
@@ -5357,9 +5370,7 @@ def test_encoding_mfdataset(self) -> None:
5357
5370
ds2 .t .encoding ["units" ] = "days since 2000-01-01"
5358
5371
ds1 .to_netcdf (tmp1 )
5359
5372
ds2 .to_netcdf (tmp2 )
5360
- with open_mfdataset (
5361
- [tmp1 , tmp2 ], combine = "nested" , compat = "no_conflicts" , join = "outer"
5362
- ) as actual :
5373
+ with open_mfdataset ([tmp1 , tmp2 ], combine = "nested" , concat_dim = "t" ) as actual :
5363
5374
assert actual .t .encoding ["units" ] == original .t .encoding ["units" ]
5364
5375
assert actual .t .encoding ["units" ] == ds1 .t .encoding ["units" ]
5365
5376
assert actual .t .encoding ["units" ] != ds2 .t .encoding ["units" ]
@@ -5382,30 +5393,20 @@ def test_encoding_mfdataset_new_defaults(self) -> None:
5382
5393
ds1 .to_netcdf (tmp1 )
5383
5394
ds2 .to_netcdf (tmp2 )
5384
5395
5385
- with set_options (use_new_combine_kwarg_defaults = False ):
5386
- with pytest .warns (
5387
- FutureWarning ,
5388
- match = "will change from join='outer' to join='exact'" ,
5389
- ):
5390
- with pytest .warns (
5391
- FutureWarning ,
5392
- match = "will change from compat='no_conflicts' to compat='override'" ,
5393
- ):
5394
- with open_mfdataset ([tmp1 , tmp2 ], combine = "nested" ) as old :
5395
- assert (
5396
- old .t .encoding ["units" ]
5397
- == original .t .encoding ["units" ]
5398
- )
5399
- assert (
5400
- old .t .encoding ["units" ] == ds1 .t .encoding ["units" ]
5401
- )
5402
- assert (
5403
- old .t .encoding ["units" ] != ds2 .t .encoding ["units" ]
5404
- )
5396
+ for setting in [True , False ]:
5397
+ with set_options (use_new_combine_kwarg_defaults = setting ):
5398
+ with open_mfdataset (
5399
+ [tmp1 , tmp2 ], combine = "nested" , concat_dim = "t"
5400
+ ) as old :
5401
+ assert (
5402
+ old .t .encoding ["units" ] == original .t .encoding ["units" ]
5403
+ )
5404
+ assert old .t .encoding ["units" ] == ds1 .t .encoding ["units" ]
5405
+ assert old .t .encoding ["units" ] != ds2 .t .encoding ["units" ]
5405
5406
5406
5407
with set_options (use_new_combine_kwarg_defaults = True ):
5407
5408
with pytest .raises (
5408
- ValueError , match = "Error might be related to new default "
5409
+ AlignmentError , match = "If you are intending to concatenate "
5409
5410
):
5410
5411
open_mfdataset ([tmp1 , tmp2 ], combine = "nested" )
5411
5412
@@ -7083,20 +7084,20 @@ def test_zarr_safe_chunk_region(self, mode: Literal["r+", "a"]):
7083
7084
@requires_h5netcdf
7084
7085
@requires_fsspec
7085
7086
def test_h5netcdf_storage_options () -> None :
7086
- with set_options (use_new_combine_kwarg_defaults = True ):
7087
- with create_tmp_files (2 , allow_cleanup_failure = ON_WINDOWS ) as (f1 , f2 ):
7088
- ds1 = create_test_data ()
7089
- ds1 .to_netcdf (f1 , engine = "h5netcdf" )
7087
+ with create_tmp_files (2 , allow_cleanup_failure = ON_WINDOWS ) as (f1 , f2 ):
7088
+ ds1 = create_test_data ()
7089
+ ds1 .to_netcdf (f1 , engine = "h5netcdf" )
7090
7090
7091
- ds2 = create_test_data ()
7092
- ds2 .to_netcdf (f2 , engine = "h5netcdf" )
7091
+ ds2 = create_test_data ()
7092
+ ds2 .to_netcdf (f2 , engine = "h5netcdf" )
7093
7093
7094
- files = [f"file://{ f } " for f in [f1 , f2 ]]
7095
- with xr .open_mfdataset (
7096
- files ,
7097
- engine = "h5netcdf" ,
7098
- concat_dim = "time" ,
7099
- combine = "nested" ,
7100
- storage_options = {"skip_instance_cache" : False },
7101
- ) as ds :
7102
- assert_identical (xr .concat ([ds1 , ds2 ], dim = "time" ), ds )
7094
+ files = [f"file://{ f } " for f in [f1 , f2 ]]
7095
+ with xr .open_mfdataset (
7096
+ files ,
7097
+ engine = "h5netcdf" ,
7098
+ concat_dim = "time" ,
7099
+ data_vars = "all" ,
7100
+ combine = "nested" ,
7101
+ storage_options = {"skip_instance_cache" : False },
7102
+ ) as ds :
7103
+ assert_identical (xr .concat ([ds1 , ds2 ], dim = "time" ), ds )
0 commit comments