-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
data_vars option added to open_mfdataset #1580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
180cf58
6195fcd
956fbeb
e721620
34b1004
09d25c6
e901a37
3141ce4
8319aa7
fdc940e
96e842e
b033bec
b854ce4
787a98b
4d3c685
1823ba3
b47e665
23f0fc6
05c8391
1f0e763
fadda83
f80fe1f
14dee9d
f1f9d8b
f64c9e3
633eec3
086cf25
b0ca228
e463e37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1267,6 +1267,130 @@ def test_4_open_large_num_files_h5netcdf(self): | |
self.validate_open_mfdataset_large_num_files(engine=['h5netcdf']) | ||
|
||
|
||
@requires_scipy_or_netCDF4 | ||
class OpenMFDatasetWithDataVarsAndCoordsKwTest(TestCase): | ||
coord_name = 'lon' | ||
var_name = 'v1' | ||
|
||
@contextlib.contextmanager | ||
def setup_files_and_datasets(self): | ||
ds1, ds2 = self.gen_datasets_with_common_coord_and_time() | ||
with create_tmp_file() as tmpfile1: | ||
with create_tmp_file() as tmpfile2: | ||
|
||
# save data to the temporary files | ||
ds1.to_netcdf(tmpfile1) | ||
ds2.to_netcdf(tmpfile2) | ||
|
||
yield [tmpfile1, tmpfile2], [ds1, ds2] | ||
|
||
def gen_datasets_with_common_coord_and_time(self): | ||
# create coordinate data | ||
nx = 10 | ||
nt = 10 | ||
x = np.arange(nx) | ||
t1 = np.arange(nt) | ||
t2 = np.arange(nt, 2 * nt, 1) | ||
|
||
v1 = np.random.randn(nt, nx) | ||
v2 = np.random.randn(nt, nx) | ||
|
||
ds1 = Dataset(data_vars={self.var_name: (['t', 'x'], v1), | ||
self.coord_name: ('x', 2 * x)}, | ||
coords={ | ||
't': (['t', ], t1), | ||
'x': (['x', ], x) | ||
}) | ||
|
||
ds2 = Dataset(data_vars={self.var_name: (['t', 'x'], v2), | ||
self.coord_name: ('x', 2 * x)}, | ||
coords={ | ||
't': (['t', ], t2), | ||
'x': (['x', ], x) | ||
}) | ||
|
||
return ds1, ds2 | ||
|
||
def test_open_mfdataset_does_same_as_concat(self): | ||
options = ['all', 'minimal', 'different', ] | ||
|
||
with self.setup_files_and_datasets() as (files, [ds1, ds2]): | ||
for opt in options: | ||
with open_mfdataset(files, data_vars=opt) as ds: | ||
kwargs = dict(data_vars=opt, dim='t') | ||
ds_expect = xr.concat([ds1, ds2], **kwargs) | ||
self.assertDatasetIdentical(ds, ds_expect) | ||
|
||
with open_mfdataset(files, coords=opt) as ds: | ||
kwargs = dict(coords=opt, dim='t') | ||
ds_expect = xr.concat([ds1, ds2], **kwargs) | ||
self.assertDatasetIdentical(ds, ds_expect) | ||
|
||
def test_common_coord_when_datavars_all(self): | ||
opt = 'all' | ||
|
||
with self.setup_files_and_datasets() as (files, [ds1, ds2]): | ||
# open the files with the data_var option | ||
with open_mfdataset(files, data_vars=opt) as ds: | ||
|
||
coord_shape = ds[self.coord_name].shape | ||
coord_shape1 = ds1[self.coord_name].shape | ||
coord_shape2 = ds2[self.coord_name].shape | ||
|
||
var_shape = ds[self.var_name].shape | ||
|
||
# shape pairs to be compared | ||
shape_pairs = [ | ||
(var_shape, coord_shape), | ||
(coord_shape1, coord_shape), | ||
(coord_shape2, coord_shape) | ||
] | ||
# tests to be applied to respective pairs | ||
tests = [self.assertEqual, | ||
self.assertNotEqual, self.assertNotEqual] | ||
|
||
for a_test, a_shape_pair in zip(tests, shape_pairs): | ||
a_test(*a_shape_pair) | ||
|
||
def test_common_coord_when_datavars_minimal(self): | ||
opt = 'minimal' | ||
|
||
with self.setup_files_and_datasets() as (files, [ds1, ds2]): | ||
# open the files using data_vars option | ||
with open_mfdataset(files, data_vars=opt) as ds: | ||
|
||
coord_shape = ds[self.coord_name].shape | ||
coord_shape1 = ds1[self.coord_name].shape | ||
coord_shape2 = ds2[self.coord_name].shape | ||
|
||
var_shape = ds[self.var_name].shape | ||
|
||
# shape pairs to be compared | ||
shape_pairs = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @shoyer : There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have committed the changes, should I open another pull request? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, please. Once a PR is merged you need to open another one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
(var_shape, coord_shape), | ||
(coord_shape1, coord_shape), | ||
(coord_shape2, coord_shape) | ||
] | ||
# tests to be applied to respective pairs | ||
tests = [self.assertNotEqual, | ||
self.assertEqual, self.assertEqual] | ||
|
||
for a_test, a_shape_pair in zip(tests, shape_pairs): | ||
a_test(*a_shape_pair) | ||
|
||
def test_invalid_data_vars_value_should_fail(self): | ||
|
||
with self.setup_files_and_datasets() as (files, _): | ||
with self.assertRaises(ValueError): | ||
with open_mfdataset(files, data_vars='minimum'): | ||
pass | ||
|
||
# test invalid coord parameter | ||
with self.assertRaises(ValueError): | ||
with open_mfdataset(files, coords='minimum'): | ||
pass | ||
|
||
|
||
@requires_dask | ||
@requires_scipy | ||
@requires_netCDF4 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole section should be more explicit, e.g.,
That way, we avoid the confusing loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* but
assert_equal(var_shape, coord_shape)
, in line with the updated test framework!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MaximilianR :
I am comparing shapes there, not dataarrays.
Cheers