File: test_tiff.py

package info (click to toggle)
kerchunk 0.2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 135,172 kB
  • sloc: python: 6,477; makefile: 39
file content (70 lines) | stat: -rw-r--r-- 1,817 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import glob
import os.path
import zarr
import pytest
import xarray as xr

from kerchunk.utils import refs_as_store

pytest.importorskip("tifffile")
pytest.importorskip("rioxarray")
import kerchunk.tiff

files = glob.glob(os.path.join(os.path.dirname(__file__), "lc*tif"))


def test_one():
    fn = files[0]
    out = kerchunk.tiff.tiff_to_zarr(fn)
    store = refs_as_store(out)
    z = zarr.open(store, zarr_format=2)
    assert list(z) == ["0", "1", "2"]
    assert z.attrs["multiscales"] == [
        {
            "datasets": [{"path": "0"}, {"path": "1"}, {"path": "2"}],
            "metadata": {},
            "name": "",
            "version": "0.1",
        }
    ]
    assert z["0"].shape == (2048, 2048)
    assert z["0"][:].max() == 8


def test_one_class():
    from collections import Counter

    fn = files[0]
    out = kerchunk.tiff.TiffToZarr(fn, inline_threshold=1000).translate()

    # test inlineing
    c = Counter(type(_) for k, _ in out.items() if ".z" not in k)
    assert c[list] == 72
    assert c[bytes] == 24

    # test zarr output
    store = refs_as_store(out)
    z = zarr.open(store, zarr_format=2)
    assert list(z) == ["0", "1", "2"]
    assert z.attrs["multiscales"] == [
        {
            "datasets": [{"path": "0"}, {"path": "1"}, {"path": "2"}],
            "metadata": {},
            "name": "",
            "version": "0.1",
        }
    ]
    assert z["0"].shape == (2048, 2048)
    assert z["0"][:].max() == 8


def test_coord():
    fn = files[0]
    out = kerchunk.tiff.tiff_to_zarr(fn)
    store = refs_as_store(out)
    z = zarr.open(store, zarr_format=2)  # highest res is the one xarray picks
    out = kerchunk.tiff.generate_coords(z.attrs, z["0"].shape)

    ds = xr.open_dataset(fn)
    assert (ds.x == out["x"]).all()
    assert (ds.y == out["y"]).all()