| 1 |
import grass.script as gs |
| 2 |
from grass.gunittest.case import TestCase |
| 3 |
from grass.gunittest.main import test |
| 4 |
|
| 5 |
|
| 6 |
class TestIEbSoilHeatFlux(TestCase): |
| 7 |
"""Regression test suite for i.eb.soilheatflux.""" |
| 8 |
|
| 9 |
albedo = "test_albedo" |
| 10 |
ndvi = "test_ndvi" |
| 11 |
temperature = "test_temperature" |
| 12 |
netradiation = "test_netradiation" |
| 13 |
localutctime = "test_localutctime" |
| 14 |
output = "test_soilheatflux" |
| 15 |
tmp_rasters = [] |
| 16 |
|
| 17 |
@classmethod |
| 18 |
def setUpClass(cls): |
| 19 |
"""Initialize a temporary region and input rasters.""" |
| 20 |
cls.use_temp_region() |
| 21 |
cls.runModule("g.region", e=10, w=0, s=0, n=10, rows=10, cols=10) |
| 22 |
|
| 23 |
cls._create_raster(cls.albedo, "0.1 + 0.3 * (row() + col()) / 20.0") |
| 24 |
cls._create_raster(cls.ndvi, "-0.2 + 0.8 * (row() + col()) / 20.0") |
| 25 |
cls._create_raster(cls.temperature, "300.0 + 10.0 * sin((row() + col()) * 0.1)") |
| 26 |
cls._create_raster( |
| 27 |
cls.netradiation, "400.0 + 20.0 * sin((row() + col()) * 0.1)" |
| 28 |
) |
| 29 |
cls._create_raster(cls.localutctime, "12.0 + 1.0 * cos((row() + col()) * 0.2)") |
| 30 |
|
| 31 |
cls.tmp_rasters.extend( |
| 32 |
[cls.albedo, cls.ndvi, cls.temperature, cls.netradiation, cls.localutctime] |
| 33 |
) |
| 34 |
|
| 35 |
@classmethod |
| 36 |
def tearDownClass(cls): |
| 37 |
"""Remove temporary raster maps and region after tests.""" |
| 38 |
cls.tmp_rasters.append(cls.output) |
| 39 |
cls.runModule("g.remove", flags="f", type="raster", name=cls.tmp_rasters) |
| 40 |
cls.del_temp_region() |
| 41 |
|
| 42 |
@classmethod |
| 43 |
def _create_raster(cls, name, expr): |
| 44 |
"""Helper method to create a raster map.""" |
| 45 |
cls.runModule("r.mapcalc", expression=f"{name} = float({expr})", overwrite=True) |
| 46 |
|
| 47 |
def _run_soilheatflux(self, ndvi=None, output=None, time=None, flags=""): |
| 48 |
"""Run the i.eb.soilheatflux module with provided parameters.""" |
| 49 |
return self.assertModule( |
| 50 |
"i.eb.soilheatflux", |
| 51 |
flags=flags, |
| 52 |
albedo=self.albedo, |
| 53 |
ndvi=ndvi or self.ndvi, |
| 54 |
temperature=self.temperature, |
| 55 |
netradiation=self.netradiation, |
| 56 |
localutctime=time or self.localutctime, |
| 57 |
output=output or self.output, |
| 58 |
overwrite=True, |
| 59 |
) |
| 60 |
|
| 61 |
def test_output_statistics(self): |
| 62 |
"""Verify statistics of the output map against reference values.""" |
| 63 |
self._run_soilheatflux() |
| 64 |
|
| 65 |
reference = { |
| 66 |
"min": 48.935794, |
| 67 |
"max": 64.674187, |
| 68 |
"mean": 59.542719, |
| 69 |
"stddev": 4.095612, |
| 70 |
} |
| 71 |
self.assertRasterFitsUnivar(self.output, reference, precision=1e-6) |
| 72 |
|
| 73 |
def test_heatflux_decreases_with_high_ndvi(self): |
| 74 |
"""Validate that soil heat flux is lower for higher NDVI (dense vegetation).""" |
| 75 |
self._create_raster("ndvi_low", "0.2") |
| 76 |
self.tmp_rasters.append("ndvi_low") |
| 77 |
|
| 78 |
self._run_soilheatflux(ndvi="ndvi_low", output="g0_low") |
| 79 |
self.tmp_rasters.append("g0_low") |
| 80 |
g0_low = gs.parse_command("r.univar", map="g0_low", format="json") |
| 81 |
|
| 82 |
self._create_raster("ndvi_high", "0.8") |
| 83 |
self.tmp_rasters.append("ndvi_high") |
| 84 |
|
| 85 |
self._run_soilheatflux(ndvi="ndvi_high", output="g0_high") |
| 86 |
self.tmp_rasters.append("g0_high") |
| 87 |
g0_high = gs.parse_command("r.univar", map="g0_high", format="json") |
| 88 |
|
| 89 |
self.assertGreater( |
| 90 |
g0_low["mean"], |
| 91 |
g0_high["mean"], |
| 92 |
"Expected lower soil heat flux for higher NDVI.", |
| 93 |
) |
| 94 |
|
| 95 |
def test_heatflux_increases_with_time(self): |
| 96 |
"""Check that soil heat flux increases with local overpass time.""" |
| 97 |
self._create_raster("time_early", "8.0") |
| 98 |
self.tmp_rasters.append("time_early") |
| 99 |
|
| 100 |
self._run_soilheatflux(time="time_early", output="g0_early") |
| 101 |
self.tmp_rasters.append("g0_early") |
| 102 |
g0_early = gs.parse_command("r.univar", map="g0_early", format="json") |
| 103 |
|
| 104 |
self._create_raster("time_noon", "12.0") |
| 105 |
self.tmp_rasters.append("time_noon") |
| 106 |
|
| 107 |
self._run_soilheatflux(time="time_noon", output="g0_noon") |
| 108 |
self.tmp_rasters.append("g0_noon") |
| 109 |
g0_noon = gs.parse_command("r.univar", map="g0_noon", format="json") |
| 110 |
|
| 111 |
self.assertGreater( |
| 112 |
g0_noon["mean"], |
| 113 |
g0_early["mean"], |
| 114 |
"Soil heat flux should be higher at noon.", |
| 115 |
) |
| 116 |
|
| 117 |
def test_roerink_correction_flag(self): |
| 118 |
"""Test that the -r flag applies the expected correction.""" |
| 119 |
self._run_soilheatflux(output="g0_base") |
| 120 |
self.tmp_rasters.append("g0_base") |
| 121 |
|
| 122 |
self._run_soilheatflux(output="g0_roerink", flags="r") |
| 123 |
self.tmp_rasters.append("g0_roerink") |
| 124 |
|
| 125 |
self.runModule( |
| 126 |
"r.mapcalc", |
| 127 |
expression="g0_expected = 1.43 * g0_base - 0.0845", |
| 128 |
overwrite=True, |
| 129 |
) |
| 130 |
self.runModule( |
| 131 |
"r.mapcalc", |
| 132 |
expression="g0_diff = abs(g0_expected - g0_roerink)", |
| 133 |
overwrite=True, |
| 134 |
) |
| 135 |
self.tmp_rasters.extend(["g0_expected", "g0_diff"]) |
| 136 |
|
| 137 |
stats = gs.parse_command("r.univar", map="g0_diff", format="json") |
| 138 |
self.assertLess( |
| 139 |
stats["max"], |
| 140 |
1e-6, |
| 141 |
"Roerink correction did not match expected transformation.", |
| 142 |
) |
| 143 |
|
| 144 |
|
| 145 |
if __name__ == "__main__": |
| 146 |
test() |