From ca3d7435bf2a87deeabb9367cc6b68e8c81145f1 Mon Sep 17 00:00:00 2001 From: Kristen Thyng Date: Thu, 17 Aug 2023 10:48:40 -0700 Subject: [PATCH] convert filters to jsonify for zarr --- tests/test_zarr_compat.py | 32 ++++++++++++++++++++++++++++++++ xpublish/utils/zarr.py | 8 ++++++++ 2 files changed, 40 insertions(+) diff --git a/tests/test_zarr_compat.py b/tests/test_zarr_compat.py index 994191b8..b149d597 100644 --- a/tests/test_zarr_compat.py +++ b/tests/test_zarr_compat.py @@ -3,6 +3,7 @@ import numpy as np import pytest import xarray as xr +from numcodecs import Shuffle, Zlib from xpublish import SingleDatasetRest @@ -39,6 +40,37 @@ def test_zmetadata_identical(start, end, freq, nlats, nlons, var_const, calendar assert actual == expected +@pytest.mark.parametrize( + 'start, end, freq, nlats, nlons, var_const, calendar, use_cftime', + [ + ('2018-01-01', '2021-01-01', 'MS', 180, 360, True, 'standard', False), + ('2018-01-01', '2021-01-01', 'D', 180, 360, False, 'noleap', True), + ('2018-01-01', '2021-01-01', '6H', 180, 360, True, 'gregorian', False), + ('2018-01-01', '2050-01-01', 'A', 180, 360, None, '360_day', True), + ], +) +def test_compressor_filters(start, end, freq, nlats, nlons, var_const, calendar, use_cftime): + ds = create_dataset( + start=start, + end=end, + freq=freq, + nlats=nlats, + nlons=nlons, + var_const=var_const, + use_cftime=use_cftime, + calendar=calendar, + ) + + ds = ds.chunk(ds.dims) + zarr_dict = {} + ds['time'].encoding['filters'] = [Shuffle(elementsize=8), Zlib(level=8)] + ds.to_zarr(zarr_dict, consolidated=True) + mapper = TestMapper(SingleDatasetRest(ds).app) + actual = json.loads(mapper['.zmetadata'].decode()) + expected = json.loads(zarr_dict['.zmetadata'].decode()) + assert actual == expected + + @pytest.mark.parametrize( 'start, end, freq, nlats, nlons, var_const, calendar, use_cftime', [ diff --git a/xpublish/utils/zarr.py b/xpublish/utils/zarr.py index b4dd2b74..c419e1cd 100644 --- a/xpublish/utils/zarr.py +++ b/xpublish/utils/zarr.py @@ -174,6 +174,14 @@ def jsonify_zmetadata( ].get_config() zjson['metadata'][f'{key}/{array_meta_key}']['compressor'] = compressor_config + # convert list of filters to dict + filters = zjson['metadata'][f'{key}/{array_meta_key}']['filters'] + if filters is not None: + filters_configs = [] + for Filter in zjson['metadata'][f'{key}/{array_meta_key}']['filters']: + filters_configs.append(Filter.get_config()) + zjson['metadata'][f'{key}/{array_meta_key}']['filters'] = filters_configs + return zjson