File: test_integration_xarray_plugin.py

package info (click to toggle)
python-rioxarray 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,304 kB
  • sloc: python: 7,893; makefile: 93
file content (72 lines) | stat: -rw-r--r-- 2,191 bytes parent folder | download | duplicates (2)
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
71
72
import os.path

import pytest

from rioxarray.exceptions import RioXarrayError
from test.conftest import TEST_INPUT_DATA_DIR

xarray = pytest.importorskip("xarray", minversion="0.18")


@pytest.mark.parametrize(
    "kwargs", [{}, {"engine": "rasterio"}, {"decode_coords": "all"}]
)
def test_xarray_open_dataset(kwargs):
    cog_file = os.path.join(TEST_INPUT_DATA_DIR, "cog.tif")
    ds = xarray.open_dataset(cog_file, **kwargs)

    assert isinstance(ds, xarray.Dataset)
    assert "band_data" in ds.data_vars
    assert ds.data_vars["band_data"].shape == (1, 500, 500)
    assert "spatial_ref" not in ds.data_vars
    assert "spatial_ref" in ds.coords
    assert "grid_mapping" not in ds.data_vars["band_data"].attrs
    assert "grid_mapping" in ds.data_vars["band_data"].encoding
    assert "preferred_chunks" in ds.data_vars["band_data"].encoding


def test_xarray_open_dataset__drop_variables():
    input_file = os.path.join(
        TEST_INPUT_DATA_DIR, "MOD09GA.A2008296.h14v17.006.2015181011753.hdf"
    )

    with xarray.open_dataset(
        input_file,
        engine="rasterio",
        group="MODIS_Grid_500m_2D",
        drop_variables=[
            "sur_refl_b01_1",
            "sur_refl_b02_1",
            "sur_refl_b03_1",
            "sur_refl_b04_1",
            "sur_refl_b05_1",
            "sur_refl_b06_1",
            "sur_refl_b07_1",
        ],
    ) as rds:
        assert sorted(rds.data_vars) == [
            "QC_500m_1",
            "iobs_res_1",
            "num_observations_500m",
            "obscov_500m_1",
        ]


def test_open_multiple_resolution():
    with pytest.raises(
        RioXarrayError,
        match="Multiple resolution sets found. Use 'variable' or 'group' to filter.",
    ):
        xarray.open_dataset(
            os.path.join(
                TEST_INPUT_DATA_DIR, "MOD09GA.A2008296.h14v17.006.2015181011753.hdf"
            ),
            engine="rasterio",
            drop_variables="QC_500m_1",
        )


def test_xarray_open_dataset__invalid_decode_coords():
    cog_file = os.path.join(TEST_INPUT_DATA_DIR, "cog.tif")
    with pytest.raises(RioXarrayError):
        xarray.open_dataset(cog_file, decode_coords=False)