| 1 |
import glob |
| 2 |
import os.path |
| 3 |
import zarr |
| 4 |
import pytest |
| 5 |
import xarray as xr |
| 6 |
|
| 7 |
from kerchunk.utils import refs_as_store |
| 8 |
|
| 9 |
pytest.importorskip("tifffile") |
| 10 |
pytest.importorskip("rioxarray") |
| 11 |
import kerchunk.tiff |
| 12 |
|
| 13 |
files = glob.glob(os.path.join(os.path.dirname(__file__), "lc*tif")) |
| 14 |
|
| 15 |
|
| 16 |
def test_one(): |
| 17 |
fn = files[0] |
| 18 |
out = kerchunk.tiff.tiff_to_zarr(fn) |
| 19 |
store = refs_as_store(out) |
| 20 |
z = zarr.open(store, zarr_format=2) |
| 21 |
assert list(z) == ["0", "1", "2"] |
| 22 |
assert z.attrs["multiscales"] == [ |
| 23 |
{ |
| 24 |
"datasets": [{"path": "0"}, {"path": "1"}, {"path": "2"}], |
| 25 |
"metadata": {}, |
| 26 |
"name": "", |
| 27 |
"version": "0.1", |
| 28 |
} |
| 29 |
] |
| 30 |
assert z["0"].shape == (2048, 2048) |
| 31 |
assert z["0"][:].max() == 8 |
| 32 |
|
| 33 |
|
| 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 |
|
| 61 |
def test_coord(): |
| 62 |
fn = files[0] |
| 63 |
out = kerchunk.tiff.tiff_to_zarr(fn) |
| 64 |
store = refs_as_store(out) |
| 65 |
z = zarr.open(store, zarr_format=2) # highest res is the one xarray picks |
| 66 |
out = kerchunk.tiff.generate_coords(z.attrs, z["0"].shape) |
| 67 |
|
| 68 |
ds = xr.open_dataset(fn) |
| 69 |
assert (ds.x == out["x"]).all() |
| 70 |
assert (ds.y == out["y"]).all() |