From 6c1fb6b76ebba862a1c5831210ce026160da0065 Mon Sep 17 00:00:00 2001 From: Martin Durant Date: Thu, 7 Sep 2017 14:17:19 -0400 Subject: [PATCH] minimalist zarr test --- xarray/backends/zarr.py | 7 ++++++- xarray/tests/__init__.py | 9 ++++++++- xarray/tests/test_backends.py | 19 ++++++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index bc3be1306d2..0d33c622af2 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -76,6 +76,8 @@ class ZarrStore(AbstractWritableDataStore, DataStorePickleMixin): # need some special secret attributes to tell us the dimensions _dimension_key = '_XARRAY_DIMENSIONS' + default_arguments = dict(compressor='default', order='C', synchronizer=None, + filters=None, cache_metadata=True, kwargs={}) def __init__(self, store=None, overwrite=False, chunk_store=None, synchronizer=None, path=None, writer=None, autoclose=False): @@ -158,8 +160,11 @@ def prepare_variable(self, name, variable, check_encoding=False, # TODO: figure out how to pass along all those other arguments + kwargs = self.default_arguments.copy() + kwargs.update(kwargs.pop('kwargs')) zarr_array = self.ds.create(name, shape=shape, dtype=dtype, - chunks=chunks, fill_value=fill_value) + chunks=chunks, fill_value=fill_value, + **kwargs) zarr_array.attrs[self._dimension_key] = dims _, attributes = _get_zarr_dims_and_attrs(zarr_array, self._dimension_key) diff --git a/xarray/tests/__init__.py b/xarray/tests/__init__.py index 424484b438a..affff9c348d 100644 --- a/xarray/tests/__init__.py +++ b/xarray/tests/__init__.py @@ -83,6 +83,12 @@ except ImportError: has_rasterio = False +try: + import zarr + has_zarr = True +except ImportError: + has_zarr = False + # slighly simpler construction that the full functions. # Generally `pytest.importorskip('package')` inline is even easier requires_matplotlib = pytest.mark.skipif( @@ -105,7 +111,8 @@ not has_bottleneck, reason='requires bottleneck') requires_rasterio = pytest.mark.skipif( not has_rasterio, reason='requires rasterio') - +requires_zarr = pytest.mark.skipif( + not has_zarr, reason='requires zarr') try: _SKIP_FLAKY = not pytest.config.getoption("--run-flaky") diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index f5bd615cfe2..9c6249f0fa3 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -21,13 +21,15 @@ open_mfdataset, backends, save_mfdataset) from xarray.backends.common import robust_getitem from xarray.backends.netCDF4_ import _extract_nc4_variable_encoding +from xarray.backends.zarr import ZarrStore from xarray.core import indexing from xarray.core.pycompat import iteritems, PY2, ExitStack, basestring from . import (TestCase, requires_scipy, requires_netCDF4, requires_pydap, requires_scipy_or_netCDF4, requires_dask, requires_h5netcdf, requires_pynio, has_netCDF4, has_scipy, assert_allclose, - flaky, network, requires_rasterio, assert_identical) + flaky, network, requires_rasterio, assert_identical, + requires_zarr) from .test_dataset import create_test_data try: @@ -1934,3 +1936,18 @@ def test_open_dataarray_options(self): expected = data.drop('y') with open_dataarray(tmp, drop_variables=['y']) as loaded: self.assertDataArrayIdentical(expected, loaded) + + +@requires_zarr +class TestZarr(TestCase): + + def test_roundtrip(self): + data = create_test_data() + del data.coords['time'] + with create_tmp_file() as tmp: + z = ZarrStore(store=tmp, overwrite=True) + z.store_dataset(data) + + z2 = open_dataset(ZarrStore(store=tmp)) + for k in data: + self.assertTrue((data[k].values == z2[k].values).all())