Skip to content

Commit 6dbd4ce

Browse files
authored
Enable class version of TIFF kerchunker (#548)
1 parent f282c8a commit 6dbd4ce

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

kerchunk/tiff.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@
1414
import kerchunk.utils
1515

1616

17-
def tiff_to_zarr(urlpath, remote_options=None, target=None, target_options=None):
17+
def tiff_to_zarr(
18+
urlpath,
19+
remote_options=None,
20+
target=None,
21+
target_options=None,
22+
inline_threshold=0,
23+
storage_options=None,
24+
):
1825
"""Wraps TIFFFile's fsspec writer to extract metadata as attributes
1926
2027
Parameters
@@ -27,14 +34,20 @@ def tiff_to_zarr(urlpath, remote_options=None, target=None, target_options=None)
2734
Write JSON to this location. If not given, no file is output
2835
target_options: dict
2936
pass these to fsspec when opening target
37+
inline_threshold: int
38+
Bytes blocks smaller than this will be inlined
39+
storage_options: dict
40+
same as remote_options, for compatibility. If both are given, remote_options wins.
3041
3142
Returns
3243
-------
3344
references dict
3445
"""
3546

36-
with fsspec.open(urlpath, **(remote_options or {})) as of:
47+
with fsspec.open(urlpath, **(remote_options or storage_options or {})) as of:
3748
url, name = urlpath.rsplit("/", 1)
49+
prot = of.fs.protocol
50+
remote_protocol = prot[0] if isinstance(prot, tuple) else prot
3851

3952
with tifffile.TiffFile(of, name=name) as tif:
4053
with tif.series[0].aszarr() as store:
@@ -72,6 +85,10 @@ def tiff_to_zarr(urlpath, remote_options=None, target=None, target_options=None)
7285
# coords = generate_coords(meta, z.shape)
7386
# rasterio.crs.CRS.from_epsg(attrs['ProjectedCSTypeGeoKey']).to_wkt("WKT1_GDAL") ??
7487
pass
88+
if inline_threshold:
89+
out = kerchunk.utils.do_inline(
90+
out, inline_threshold, remote_options, remote_protocol
91+
)
7592
if target is not None:
7693
with fsspec.open(target, **(target_options or {})) as of:
7794
ujson.dump(out, of)

tests/test_tiff.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import glob
2-
import fsspec
32
import os.path
43
import zarr
54
import pytest
@@ -32,6 +31,33 @@ def test_one():
3231
assert z["0"][:].max() == 8
3332

3433

34+
def test_one_class():
35+
from collections import Counter
36+
37+
fn = files[0]
38+
out = kerchunk.tiff.TiffToZarr(fn, inline_threshold=1000).translate()
39+
40+
# test inlineing
41+
c = Counter(type(_) for k, _ in out.items() if ".z" not in k)
42+
assert c[list] == 72
43+
assert c[bytes] == 24
44+
45+
# test zarr output
46+
store = refs_as_store(out)
47+
z = zarr.open(store, zarr_format=2)
48+
assert list(z) == ["0", "1", "2"]
49+
assert z.attrs["multiscales"] == [
50+
{
51+
"datasets": [{"path": "0"}, {"path": "1"}, {"path": "2"}],
52+
"metadata": {},
53+
"name": "",
54+
"version": "0.1",
55+
}
56+
]
57+
assert z["0"].shape == (2048, 2048)
58+
assert z["0"][:].max() == 8
59+
60+
3561
def test_coord():
3662
fn = files[0]
3763
out = kerchunk.tiff.tiff_to_zarr(fn)

0 commit comments

Comments
 (0)