Skip to content
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

convert filters to jsonify for zarr #225

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/test_zarr_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import pytest
import xarray as xr
from numcodecs import Shuffle, Zlib

from xpublish import SingleDatasetRest

Expand Down Expand Up @@ -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',
[
Expand Down
8 changes: 8 additions & 0 deletions xpublish/utils/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down