| 1 |
import fsspec |
| 2 |
import numpy as np |
| 3 |
import pandas as pd |
| 4 |
import pytest |
| 5 |
import xarray as xr |
| 6 |
import fsspec.implementations.reference as reffs |
| 7 |
import ujson |
| 8 |
|
| 9 |
import kerchunk.combine |
| 10 |
import kerchunk.zarr |
| 11 |
import kerchunk.utils |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture(scope="module") |
| 15 |
def ds(): |
| 16 |
ds = xr.Dataset( |
| 17 |
{ |
| 18 |
"x": xr.DataArray(np.linspace(-np.pi, np.pi, 10), dims=["x"]), |
| 19 |
"y": xr.DataArray(np.linspace(-np.pi / 2, np.pi / 2, 10), dims=["y"]), |
| 20 |
"time": xr.DataArray(pd.date_range("2020", "2021"), dims=["time"]), |
| 21 |
}, |
| 22 |
) |
| 23 |
ds["temp"] = ( |
| 24 |
np.cos(ds.x) |
| 25 |
* np.sin(ds.y) |
| 26 |
* xr.ones_like(ds.time).astype("float") |
| 27 |
* np.random.random(ds.time.shape) |
| 28 |
) |
| 29 |
return ds |
| 30 |
|
| 31 |
|
| 32 |
@pytest.fixture |
| 33 |
def zarr_in_zip(tmpdir, ds): |
| 34 |
def _zip(file): |
| 35 |
import os |
| 36 |
import zipfile |
| 37 |
|
| 38 |
filename = file + os.path.extsep + "zip" |
| 39 |
with zipfile.ZipFile( |
| 40 |
filename, "w", compression=zipfile.ZIP_STORED, allowZip64=True |
| 41 |
) as fh: |
| 42 |
for root, _, filenames in os.walk(file): |
| 43 |
for each_filename in filenames: |
| 44 |
each_filename = os.path.join(root, each_filename) |
| 45 |
fh.write(each_filename, os.path.relpath(each_filename, file)) |
| 46 |
return filename |
| 47 |
|
| 48 |
fn = f"{tmpdir}/test.zarr" |
| 49 |
ds.to_zarr(fn, mode="w", zarr_format=2) |
| 50 |
return _zip(fn) |
| 51 |
|
| 52 |
|
| 53 |
def test_zarr_in_zip(zarr_in_zip, ds): |
| 54 |
out = kerchunk.zarr.ZarrToZarr( |
| 55 |
url="zip://", storage_options={"fo": zarr_in_zip} |
| 56 |
).translate() |
| 57 |
|
| 58 |
ds2 = xr.open_dataset( |
| 59 |
out, |
| 60 |
engine="kerchunk", |
| 61 |
backend_kwargs={ |
| 62 |
"storage_options": { |
| 63 |
"remote_protocol": "zip", |
| 64 |
"remote_options": {"fo": zarr_in_zip}, |
| 65 |
} |
| 66 |
}, |
| 67 |
) |
| 68 |
assert ds.equals(ds2) |
| 69 |
|
| 70 |
# tests inlining of metadata |
| 71 |
fs = fsspec.filesystem( |
| 72 |
"reference", fo=out, remote_protocol="zip", remote_options={"fo": zarr_in_zip} |
| 73 |
) |
| 74 |
assert isinstance(fs.references["temp/.zarray"], (str, bytes)) |
| 75 |
|
| 76 |
|
| 77 |
def test_zarr_combine(tmpdir, ds): |
| 78 |
fn1 = f"{tmpdir}/test1.zarr" |
| 79 |
ds.to_zarr(fn1, zarr_format=2) |
| 80 |
|
| 81 |
one = kerchunk.zarr.ZarrToZarr(fn1, inline_threshold=0).translate() |
| 82 |
fn = f"{tmpdir}/out.parq" |
| 83 |
out = reffs.LazyReferenceMapper.create(fn) |
| 84 |
mzz = kerchunk.combine.MultiZarrToZarr([one], concat_dims=["time"], out=out) |
| 85 |
mzz.translate() |
| 86 |
|
| 87 |
ds2 = xr.open_dataset(fn, engine="kerchunk") |
| 88 |
assert ds.equals(ds2) |
| 89 |
|
| 90 |
|
| 91 |
def test_zarr_json_dump_succeeds(tmpdir, ds): |
| 92 |
fn1 = f"{tmpdir}/test1.zarr" |
| 93 |
ds.to_zarr(fn1, zarr_format=2) |
| 94 |
|
| 95 |
one = kerchunk.zarr.ZarrToZarr( |
| 96 |
fn1, |
| 97 |
inline_threshold=0, |
| 98 |
).translate() |
| 99 |
ujson.dumps(one) |