| 1 |
"""Test t.rast.series |
| 2 |
|
| 3 |
(C) 2015 by the GRASS Development Team |
| 4 |
This program is free software under the GNU General Public |
| 5 |
License (>=v2). Read the file COPYING that comes with GRASS |
| 6 |
for details. |
| 7 |
|
| 8 |
:authors: Soeren Gebbert |
| 9 |
""" |
| 10 |
|
| 11 |
import os |
| 12 |
|
| 13 |
import grass.temporal as tgis |
| 14 |
from grass.gunittest.case import TestCase |
| 15 |
from grass.gunittest.gmodules import SimpleModule |
| 16 |
from grass.gunittest.utils import xfail_windows |
| 17 |
|
| 18 |
|
| 19 |
class TestSnapAbsoluteSTRDS(TestCase): |
| 20 |
@classmethod |
| 21 |
def setUpClass(cls): |
| 22 |
"""Initiate the temporal GIS and set the region""" |
| 23 |
os.putenv("GRASS_OVERWRITE", "1") |
| 24 |
tgis.init() |
| 25 |
cls.use_temp_region() |
| 26 |
cls.runModule("g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10) |
| 27 |
cls.runModule("r.mapcalc", expression="a1 = 100", overwrite=True) |
| 28 |
cls.runModule("r.mapcalc", expression="a2 = 200", overwrite=True) |
| 29 |
cls.runModule("r.mapcalc", expression="a3 = 300", overwrite=True) |
| 30 |
cls.runModule("r.mapcalc", expression="a4 = 400", overwrite=True) |
| 31 |
|
| 32 |
cls.runModule( |
| 33 |
"t.create", |
| 34 |
type="strds", |
| 35 |
temporaltype="absolute", |
| 36 |
output="A", |
| 37 |
title="A test", |
| 38 |
description="A test", |
| 39 |
overwrite=True, |
| 40 |
) |
| 41 |
|
| 42 |
cls.runModule( |
| 43 |
"t.register", |
| 44 |
type="raster", |
| 45 |
input="A", |
| 46 |
maps="a1,a2,a3,a4", |
| 47 |
start="2001-01-01", |
| 48 |
increment="1 month", |
| 49 |
flags="i", |
| 50 |
overwrite=True, |
| 51 |
) |
| 52 |
|
| 53 |
@classmethod |
| 54 |
def tearDownClass(cls): |
| 55 |
"""Remove the temporary region""" |
| 56 |
cls.del_temp_region() |
| 57 |
cls.runModule("t.remove", flags="df", type="strds", inputs="A") |
| 58 |
cls.runModule( |
| 59 |
"t.unregister", |
| 60 |
type="raster", |
| 61 |
maps="series_average,series_maximum,series_minimum,series_minimum_2", |
| 62 |
) |
| 63 |
cls.runModule( |
| 64 |
"g.remove", |
| 65 |
flags="f", |
| 66 |
type="raster", |
| 67 |
name=( |
| 68 |
"series_average,series_maximum" |
| 69 |
",series_minimum,series_minimum_2,series_quantile" |
| 70 |
), |
| 71 |
) |
| 72 |
|
| 73 |
def test_time_stamp(self): |
| 74 |
self.assertModule( |
| 75 |
"t.rast.series", |
| 76 |
input="A", |
| 77 |
method="average", |
| 78 |
output="series_time_stamp", |
| 79 |
where="start_time > '2001-02-01'", |
| 80 |
) |
| 81 |
|
| 82 |
tinfo_string = """start_time='2001-02-01 00:00:00' |
| 83 |
end_time='2001-05-01 00:00:00'""" |
| 84 |
|
| 85 |
info = SimpleModule( |
| 86 |
"t.info", flags="g", type="raster", input="series_time_stamp" |
| 87 |
) |
| 88 |
self.assertModuleKeyValue( |
| 89 |
module=info, reference=tinfo_string, precision=2, sep="=" |
| 90 |
) |
| 91 |
|
| 92 |
def test_average(self): |
| 93 |
self.assertModule( |
| 94 |
"t.rast.series", input="A", method="average", output="series_average" |
| 95 |
) |
| 96 |
|
| 97 |
self.assertRasterMinMax( |
| 98 |
map="series_average", refmin=250, refmax=250, msg="Average must be 250" |
| 99 |
) |
| 100 |
|
| 101 |
def test_maximum(self): |
| 102 |
self.assertModule( |
| 103 |
"t.rast.series", input="A", method="maximum", output="series_maximum" |
| 104 |
) |
| 105 |
|
| 106 |
self.assertRasterMinMax( |
| 107 |
map="series_maximum", refmin=400, refmax=400, msg="Maximum must be 400" |
| 108 |
) |
| 109 |
|
| 110 |
def test_minimum(self): |
| 111 |
self.assertModule( |
| 112 |
"t.rast.series", input="A", method="minimum", output="series_minimum" |
| 113 |
) |
| 114 |
|
| 115 |
self.assertRasterMinMax( |
| 116 |
map="series_minimum", refmin=100, refmax=100, msg="Minimum must be 100" |
| 117 |
) |
| 118 |
|
| 119 |
def test_multi_stats(self): |
| 120 |
self.assertModule( |
| 121 |
"t.rast.series", |
| 122 |
input="A", |
| 123 |
method=["average", "maximum", "minimum"], |
| 124 |
output=["series_average", "series_maximum", "series_minimum"], |
| 125 |
overwrite=True, |
| 126 |
) |
| 127 |
|
| 128 |
self.assertRasterMinMax( |
| 129 |
map="series_average", refmin=250, refmax=250, msg="Average must be 250" |
| 130 |
) |
| 131 |
|
| 132 |
self.assertRasterMinMax( |
| 133 |
map="series_maximum", refmin=400, refmax=400, msg="Maximum must be 400" |
| 134 |
) |
| 135 |
|
| 136 |
self.assertRasterMinMax( |
| 137 |
map="series_minimum", refmin=100, refmax=100, msg="Minimum must be 100" |
| 138 |
) |
| 139 |
|
| 140 |
def test_minimum_where(self): |
| 141 |
self.assertModule( |
| 142 |
"t.rast.series", |
| 143 |
input="A", |
| 144 |
method="minimum", |
| 145 |
output="series_minimum_2", |
| 146 |
where="start_time >= '2001-03-01'", |
| 147 |
) |
| 148 |
|
| 149 |
self.assertRasterMinMax( |
| 150 |
map="series_minimum_2", refmin=300, refmax=300, msg="Minimum must be 300" |
| 151 |
) |
| 152 |
|
| 153 |
@xfail_windows |
| 154 |
def test_quantile(self): |
| 155 |
self.assertModule( |
| 156 |
"t.rast.series", |
| 157 |
input="A", |
| 158 |
method="quantile", |
| 159 |
quantile=0.5, |
| 160 |
output="series_quantile", |
| 161 |
) |
| 162 |
|
| 163 |
self.assertRasterMinMax( |
| 164 |
map="series_quantile", refmin=300, refmax=300, msg="Minimum must be 300" |
| 165 |
) |
| 166 |
|
| 167 |
|
| 168 |
class TestSnapRelativeSTRDS(TestCase): |
| 169 |
@classmethod |
| 170 |
def setUpClass(cls): |
| 171 |
"""Initiate the temporal GIS and set the region""" |
| 172 |
os.putenv("GRASS_OVERWRITE", "1") |
| 173 |
tgis.init() |
| 174 |
cls.use_temp_region() |
| 175 |
cls.runModule("g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10) |
| 176 |
cls.runModule("r.mapcalc", expression="a1 = 100", overwrite=True) |
| 177 |
cls.runModule("r.mapcalc", expression="a2 = 200", overwrite=True) |
| 178 |
cls.runModule("r.mapcalc", expression="a3 = 300", overwrite=True) |
| 179 |
cls.runModule("r.mapcalc", expression="a4 = 400", overwrite=True) |
| 180 |
|
| 181 |
cls.runModule( |
| 182 |
"t.create", |
| 183 |
type="strds", |
| 184 |
temporaltype="relative", |
| 185 |
output="A", |
| 186 |
title="A test", |
| 187 |
description="A test", |
| 188 |
overwrite=True, |
| 189 |
) |
| 190 |
|
| 191 |
cls.runModule( |
| 192 |
"t.register", |
| 193 |
type="raster", |
| 194 |
input="A", |
| 195 |
maps="a1,a2,a3,a4", |
| 196 |
start="0", |
| 197 |
increment="14", |
| 198 |
unit="days", |
| 199 |
overwrite=True, |
| 200 |
) |
| 201 |
|
| 202 |
@classmethod |
| 203 |
def tearDownClass(cls): |
| 204 |
"""Remove the temporary region""" |
| 205 |
cls.del_temp_region() |
| 206 |
cls.runModule("t.remove", flags="df", type="strds", inputs="A") |
| 207 |
cls.runModule( |
| 208 |
"t.unregister", |
| 209 |
type="raster", |
| 210 |
maps="series_average,series_maximum,series_minimum,series_minimum_2", |
| 211 |
) |
| 212 |
cls.runModule( |
| 213 |
"g.remove", |
| 214 |
flags="f", |
| 215 |
type="raster", |
| 216 |
name="series_average,series_maximum,series_minimum,series_minimum_2", |
| 217 |
) |
| 218 |
|
| 219 |
def test_average(self): |
| 220 |
self.assertModule( |
| 221 |
"t.rast.series", input="A", method="average", output="series_average" |
| 222 |
) |
| 223 |
|
| 224 |
self.assertRasterMinMax( |
| 225 |
map="series_average", refmin=250, refmax=250, msg="Average must be 250" |
| 226 |
) |
| 227 |
|
| 228 |
def test_maximum(self): |
| 229 |
self.assertModule( |
| 230 |
"t.rast.series", input="A", method="maximum", output="series_maximum" |
| 231 |
) |
| 232 |
|
| 233 |
self.assertRasterMinMax( |
| 234 |
map="series_maximum", refmin=400, refmax=400, msg="Maximum must be 400" |
| 235 |
) |
| 236 |
|
| 237 |
def test_minimum(self): |
| 238 |
self.assertModule( |
| 239 |
"t.rast.series", input="A", method="minimum", output="series_minimum" |
| 240 |
) |
| 241 |
|
| 242 |
self.assertRasterMinMax( |
| 243 |
map="series_minimum", refmin=100, refmax=100, msg="Minimum must be 100" |
| 244 |
) |
| 245 |
|
| 246 |
def test_minimum_where(self): |
| 247 |
self.assertModule( |
| 248 |
"t.rast.series", |
| 249 |
input="A", |
| 250 |
method="minimum", |
| 251 |
output="series_minimum_2", |
| 252 |
where="start_time >= 28", |
| 253 |
) |
| 254 |
|
| 255 |
self.assertRasterMinMax( |
| 256 |
map="series_minimum_2", refmin=300, refmax=300, msg="Minimum must be 300" |
| 257 |
) |
| 258 |
|
| 259 |
|
| 260 |
if __name__ == "__main__": |
| 261 |
from grass.gunittest.main import test |
| 262 |
|
| 263 |
test() |