/
test_integration_merge.py
187 lines (175 loc) · 6.26 KB
/
test_integration_merge.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import pytest
from numpy import nansum
from numpy.testing import assert_almost_equal
from rioxarray import open_rasterio
from rioxarray.merge import merge_arrays, merge_datasets
from test.conftest import TEST_INPUT_DATA_DIR
def test_merge_arrays():
dem_test = os.path.join(TEST_INPUT_DATA_DIR, "MODIS_ARRAY.nc")
with open_rasterio(dem_test) as rds:
rds.attrs = {
"_FillValue": rds.rio.nodata,
"grid_mapping": "spatial_ref",
"crs": rds.attrs["crs"],
}
arrays = [
rds.isel(x=slice(100), y=slice(100)),
rds.isel(x=slice(100, 200), y=slice(100, 200)),
rds.isel(x=slice(100), y=slice(100, 200)),
rds.isel(x=slice(100, 200), y=slice(100)),
]
merged = merge_arrays(arrays)
assert_almost_equal(
merged.rio.bounds(),
(-7274009.649486291, 5003545.682141737, -7227446.721475236, 5050108.61015275),
)
assert_almost_equal(
tuple(merged.rio.transform()),
(
231.6563582639536,
0.0,
-7274009.649486291,
0.0,
-231.65635826374404,
5050108.61015275,
0.0,
0.0,
1.0,
),
)
assert merged.rio._cached_transform() == merged.rio.transform()
assert merged.rio.shape == (201, 201)
assert merged.coords["band"].values == [1]
assert sorted(merged.coords) == ["band", "spatial_ref", "x", "y"]
assert merged.rio.crs == rds.rio.crs
assert merged.attrs == rds.attrs
assert_almost_equal(merged.sum(), 11368261)
def test_merge_arrays__res():
dem_test = os.path.join(TEST_INPUT_DATA_DIR, "MODIS_ARRAY.nc")
with open_rasterio(dem_test, masked=True) as rds:
rds.attrs = {
"_FillValue": rds.rio.nodata,
"grid_mapping": "spatial_ref",
"crs": rds.attrs["crs"],
}
arrays = [
rds.isel(x=slice(100), y=slice(100)),
rds.isel(x=slice(100, 200), y=slice(100, 200)),
rds.isel(x=slice(100), y=slice(100, 200)),
rds.isel(x=slice(100, 200), y=slice(100)),
]
merged = merge_arrays(arrays, res=(300, 300))
assert_almost_equal(
merged.rio.bounds(),
(-7274009.649486291, 5003608.61015275, -7227509.649486291, 5050108.61015275),
)
assert_almost_equal(
tuple(merged.rio.transform()),
(300.0, 0.0, -7274009.649486291, 0.0, -300.0, 5050108.61015275, 0.0, 0.0, 1.0),
)
assert merged.rio._cached_transform() == merged.rio.transform()
assert merged.rio.shape == (155, 155)
assert merged.coords["band"].values == [1]
assert sorted(merged.coords) == ["band", "spatial_ref", "x", "y"]
assert merged.rio.crs == rds.rio.crs
assert merged.attrs == rds.attrs
assert_almost_equal(nansum(merged), 13556564)
@pytest.mark.xfail(os.name == "nt", reason="On windows the merged data is different.")
def test_merge_datasets():
dem_test = os.path.join(
TEST_INPUT_DATA_DIR, "MOD09GA.A2008296.h14v17.006.2015181011753.hdf"
)
with open_rasterio(dem_test, group="MODIS_Grid_500m_2D") as rds:
datasets = [
rds.isel(x=slice(600), y=slice(600)),
rds.isel(x=slice(600, None), y=slice(600, None)),
rds.isel(x=slice(600), y=slice(600, None)),
rds.isel(x=slice(600, None), y=slice(600)),
]
merged = merge_datasets(datasets)
data_vars = sorted(merged.data_vars)
assert data_vars == [
"QC_500m_1",
"iobs_res_1",
"num_observations_500m",
"obscov_500m_1",
"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",
]
data_var = data_vars[0]
assert_almost_equal(
merged[data_var].rio.bounds(),
(-4447802.078667, -10008017.989716524, -3335388.246283474, -8895604.157333),
)
assert_almost_equal(
tuple(merged[data_var].rio.transform()),
(
463.3127165279158,
0.0,
-4447802.078667,
0.0,
-463.3127165279151,
-8895604.157333,
0.0,
0.0,
1.0,
),
)
assert merged.rio.shape == (2401, 2401)
assert merged.coords["band"].values == [1]
assert sorted(merged.coords) == ["band", "spatial_ref", "x", "y"]
assert merged.rio.crs == rds.rio.crs
base_attrs = dict(rds.attrs)
base_attrs["grid_mapping"] = "spatial_ref"
assert merged.attrs == base_attrs
assert_almost_equal(merged[data_var].sum(), 4543446965182987)
@pytest.mark.xfail(os.name == "nt", reason="On windows the merged data is different.")
def test_merge_datasets__res():
dem_test = os.path.join(
TEST_INPUT_DATA_DIR, "MOD09GA.A2008296.h14v17.006.2015181011753.hdf"
)
with open_rasterio(dem_test, group="MODIS_Grid_500m_2D") as rds:
datasets = [
rds.isel(x=slice(1200), y=slice(1200)),
rds.isel(x=slice(1200, None), y=slice(1200, None)),
rds.isel(x=slice(1200), y=slice(1200, None)),
rds.isel(x=slice(1200, None), y=slice(1200)),
]
merged = merge_datasets(datasets, res=1000)
data_vars = sorted(merged.data_vars)
assert data_vars == [
"QC_500m_1",
"iobs_res_1",
"num_observations_500m",
"obscov_500m_1",
"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",
]
data_var = data_vars[0]
assert_almost_equal(
merged[data_var].rio.bounds(),
(-4447802.078667, -10007604.157333, -3335802.078667, -8895604.157333),
)
assert_almost_equal(
tuple(merged[data_var].rio.transform()),
(1000.0, 0.0, -4447802.078667, 0.0, -1000.0, -8895604.157333, 0.0, 0.0, 1.0),
)
assert merged.rio.shape == (1112, 1112)
assert merged.coords["band"].values == [1]
assert sorted(merged.coords) == ["band", "spatial_ref", "x", "y"]
assert merged.rio.crs == rds.rio.crs
base_attrs = dict(rds.attrs)
base_attrs["grid_mapping"] = "spatial_ref"
assert merged.attrs == base_attrs
assert_almost_equal(merged[data_var].sum(), 973667940761024)