Skip to content

Commit

Permalink
Save progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiannucci committed Oct 10, 2024
1 parent 80fedcd commit 1f69a0b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
13 changes: 10 additions & 3 deletions kerchunk/netCDF3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import reduce
from packaging.version import Version
from operator import mul

import numpy as np
Expand Down Expand Up @@ -167,7 +168,13 @@ def translate(self):
import zarr

out = self.out
z = zarr.open(out, mode="w", zarr_format=2)
if Version(zarr.__version__) < Version("3.0.0.a0"):
store = zarr.storage.KVStore(out)
z = zarr.group(store=store, overwrite=True)
else:
store = zarr.storage.MemoryStore(mode="a", store_dict=out)
z = zarr.open(store, mode="w", zarr_format=2)

for dim, var in self.variables.items():
if dim in self.chunks:
shape = self.chunks[dim][-1]
Expand Down Expand Up @@ -197,7 +204,7 @@ def translate(self):
dtype=var.data.dtype,
fill_value=fill,
chunks=shape,
compression=None,
compressor=None,
)
part = ".".join(["0"] * len(shape)) or "0"
k = f"{dim}/{part}"
Expand Down Expand Up @@ -251,7 +258,7 @@ def translate(self):
dtype=base,
fill_value=fill,
chunks=(1,) + dtype.shape,
compression=None,
compressor=None,
)
arr.attrs.update(
{
Expand Down
2 changes: 1 addition & 1 deletion kerchunk/tests/test_hdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def list_dir(store, path):
def create_store(test_dict: dict, remote_options: Any = None):
if Version(zarr.__version__) < Version("3.0.0.a0"):
return fsspec.get_mapper(
"reference://", fo=test_dict, remote_protocol="s3", remote_options=so
"reference://", fo=test_dict, remote_protocol="s3", remote_options=remote_options
)
else:
fs = fsspec.implementations.reference.ReferenceFileSystem(fo=test_dict, remote_options=remote_options)
Expand Down
20 changes: 18 additions & 2 deletions kerchunk/tests/test_netcdf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from typing import Any


import fsspec
Expand All @@ -7,6 +8,8 @@
import pytest
from kerchunk import netCDF3

import zarr

xr = pytest.importorskip("xarray")


Expand All @@ -24,16 +27,29 @@
)


def create_store(test_dict: dict, remote_options: Any = None):
if Version(zarr.__version__) < Version("3.0.0.a0"):
return fsspec.get_mapper(
"reference://", fo=test_dict, remote_protocol="s3", remote_options=remote_options
)
else:
fs = fsspec.implementations.reference.ReferenceFileSystem(fo=test_dict, remote_options=remote_options)
return zarr.storage.RemoteStore(fs, mode="r")


def test_one(m):
m.pipe("data.nc3", bdata)
h = netCDF3.netcdf_recording_file("memory://data.nc3")
out = h.translate()

store = create_store(out, remote_options={"remote_protocol": "memory"})

ds = xr.open_dataset(
"reference://",
store,
engine="zarr",
backend_kwargs={
"consolidated": False,
"storage_options": {"fo": out, "remote_protocol": "memory"},
"zarr_format": "2",
},
)
assert (ds.data == data).all()
Expand Down

0 comments on commit 1f69a0b

Please sign in to comment.