| 1 | import grass.script as gs |
| 2 | from grass.gunittest.case import TestCase |
| 3 | from grass.gunittest.main import test |
| 4 | |
| 5 | |
| 6 | class TestHSEBAL01(TestCase): |
| 7 | """Regression testsuite for i.eb.hsebal01 module.""" |
| 8 | |
| 9 | netradiation_map = "hsebal_netrad" |
| 10 | soilheat_flux_map = "hsebal_soilheat" |
| 11 | aerodynamic_resistance_map = "hsebal_z0m" |
| 12 | temp_mean_sea_level_map = "hsebal_temp" |
| 13 | vapor_pressure_map = "hsebal_vap" |
| 14 | output_map = "hsebal_output" |
| 15 | temp_rasters = [] |
| 16 | |
| 17 | @classmethod |
| 18 | def setUpClass(cls): |
| 19 | """Set up the test environment and create input rasters.""" |
| 20 | cls.use_temp_region() |
| 21 | cls.runModule("g.region", n=10000, s=0, e=10000, w=0, res=1000) |
| 22 | |
| 23 | rasters = [ |
| 24 | (cls.netradiation_map, "500 + col()"), |
| 25 | (cls.soilheat_flux_map, "50 + col()"), |
| 26 | (cls.aerodynamic_resistance_map, "0.1"), |
| 27 | (cls.temp_mean_sea_level_map, "250 + row()*5 + col()*2"), |
| 28 | (cls.vapor_pressure_map, "2 + row()/10.0"), |
| 29 | ] |
| 30 | for name, expr in rasters: |
| 31 | cls.runModule("r.mapcalc", expression=f"{name} = {expr}", overwrite=True) |
| 32 | |
| 33 | cls.temp_rasters.extend([r[0] for r in rasters]) |
| 34 | |
| 35 | @classmethod |
| 36 | def tearDownClass(cls): |
| 37 | """Clean up the test environment by removing created rasters.""" |
| 38 | cls.runModule("g.remove", flags="f", type="raster", name=cls.temp_rasters) |
| 39 | cls.del_temp_region() |
| 40 | |
| 41 | def _run_hsebal(self, output, friction_velocity=0.32407, flags="", **kwargs): |
| 42 | """Helper to run i.eb.hsebal01 with given parameters.""" |
| 43 | self.assertModule( |
| 44 | "i.eb.hsebal01", |
| 45 | netradiation=self.netradiation_map, |
| 46 | soilheatflux=self.soilheat_flux_map, |
| 47 | aerodynresistance=self.aerodynamic_resistance_map, |
| 48 | temperaturemeansealevel=self.temp_mean_sea_level_map, |
| 49 | vapourpressureactual=self.vapor_pressure_map, |
| 50 | frictionvelocitystar=friction_velocity, |
| 51 | output=output, |
| 52 | flags=flags, |
| 53 | overwrite=True, |
| 54 | **kwargs, |
| 55 | ) |
| 56 | self.temp_rasters.append(output) |
| 57 | self.assertRasterExists(output) |
| 58 | |
| 59 | def _assert_univar(self, raster, expected_stats, precision=1e-6): |
| 60 | """Helper to assert raster statistics.""" |
| 61 | self.assertRasterFitsUnivar( |
| 62 | raster, |
| 63 | expected_stats, |
| 64 | precision=precision, |
| 65 | ) |
| 66 | |
| 67 | def _create_raster(self, expr, output): |
| 68 | """Helper to create a raster.""" |
| 69 | self.runModule( |
| 70 | "r.mapcalc", |
| 71 | expression=f"{output} = {expr}", |
| 72 | overwrite=True, |
| 73 | ) |
| 74 | self.temp_rasters.append(output) |
| 75 | |
| 76 | def test_hsebal01_auto_pixels(self): |
| 77 | """Test i.eb.hsebal01 with automatic wet/dry pixel detection.""" |
| 78 | out = f"{self.output_map}_auto" |
| 79 | self._run_hsebal(output=out, flags="a") |
| 80 | |
| 81 | expected_stats = { |
| 82 | "min": 0.0, |
| 83 | "max": 444.971643, |
| 84 | "mean": 240.955903, |
| 85 | "stddev": 108.761122, |
| 86 | } |
| 87 | self._assert_univar(out, expected_stats) |
| 88 | |
| 89 | def test_hsebal01_manual_pixels_rowcol(self): |
| 90 | """Test i.eb.hsebal01 with manual wet/dry pixel (row/column).""" |
| 91 | out = f"{self.output_map}_manual" |
| 92 | self._run_hsebal( |
| 93 | output=out, |
| 94 | row_wet_pixel=0, |
| 95 | column_wet_pixel=0, |
| 96 | row_dry_pixel=9, |
| 97 | column_dry_pixel=9, |
| 98 | ) |
| 99 | |
| 100 | expected_stats = { |
| 101 | "min": 0.0, |
| 102 | "max": 444.971643, |
| 103 | "mean": 240.955903, |
| 104 | "stddev": 108.761122, |
| 105 | } |
| 106 | self._assert_univar(out, expected_stats) |
| 107 | |
| 108 | def test_hsebal01_manual_pixels_coordinates(self): |
| 109 | """Test i.eb.hsebal01 with manual wet/dry pixel (coordinates).""" |
| 110 | out = f"{self.output_map}_manual_coords" |
| 111 | self._run_hsebal( |
| 112 | output=out, |
| 113 | flags="c", |
| 114 | row_wet_pixel=10000, |
| 115 | column_wet_pixel=0, |
| 116 | row_dry_pixel=1000, |
| 117 | column_dry_pixel=9000, |
| 118 | ) |
| 119 | |
| 120 | expected_stats = { |
| 121 | "min": 0.0, |
| 122 | "max": 444.971643, |
| 123 | "mean": 240.955903, |
| 124 | "stddev": 108.761122, |
| 125 | } |
| 126 | self._assert_univar(out, expected_stats) |
| 127 | |
| 128 | def test_hsebal01_with_different_friction_velocity(self): |
| 129 | """Test the sensitivity of i.eb.hsebal01 to changes in friction velocity.""" |
| 130 | out1 = f"{self.output_map}_diff_friction" |
| 131 | out2 = f"{self.output_map}_auto" |
| 132 | |
| 133 | self._run_hsebal(output=out1, flags="a", friction_velocity=0.5) |
| 134 | self._run_hsebal(output=out2, flags="a", friction_velocity=0.32407) |
| 135 | |
| 136 | stats1 = gs.parse_command("r.univar", map=out1, format="json") |
| 137 | stats2 = gs.parse_command("r.univar", map=out2, format="json") |
| 138 | self.assertNotEqual( |
| 139 | stats1["mean"], |
| 140 | stats2["mean"], |
| 141 | msg="Mean values should differ with different friction velocity star", |
| 142 | ) |
| 143 | |
| 144 | def test_hsebal01_energy_balance_constraint(self): |
| 145 | """Test that sensible heat flux does not greatly exceed the available energy (Rn - G).""" |
| 146 | out = f"{self.output_map}_energy_check" |
| 147 | self._run_hsebal(output=out, flags="a") |
| 148 | |
| 149 | avail = f"{self.output_map}_available_energy" |
| 150 | self._create_raster( |
| 151 | f"{self.netradiation_map} - {self.soilheat_flux_map}", avail |
| 152 | ) |
| 153 | |
| 154 | mask = f"{self.output_map}_violation" |
| 155 | self._create_raster(f"if({out} > {avail}, 1, 0)", mask) |
| 156 | |
| 157 | stats = gs.parse_command("r.univar", map=mask, format="json") |
| 158 | ratio = stats["sum"] / stats["n"] |
| 159 | self.assertLess( |
| 160 | ratio, |
| 161 | 0.1, |
| 162 | msg="Sensible heat flux should not greatly exceed available energy", |
| 163 | ) |
| 164 | |
| 165 | def test_hsebal01_monotonicity_along_temperature(self): |
| 166 | """Test that higher temperatures generally result in higher sensible heat flux.""" |
| 167 | out = f"{self.output_map}_monotonicity" |
| 168 | self._run_hsebal(output=out, flags="a") |
| 169 | |
| 170 | low = f"{out}_low_temp" |
| 171 | high = f"{out}_high_temp" |
| 172 | self._create_raster(f"if(row() < 5, {out}, null())", low) |
| 173 | self._create_raster(f"if(row() >= 5, {out}, null())", high) |
| 174 | |
| 175 | stats_low = gs.parse_command("r.univar", map=low, format="json") |
| 176 | stats_high = gs.parse_command("r.univar", map=high, format="json") |
| 177 | |
| 178 | mean_low = stats_low["mean"] |
| 179 | mean_high = stats_high["mean"] |
| 180 | |
| 181 | self.assertGreater( |
| 182 | mean_high, |
| 183 | mean_low, |
| 184 | msg="Higher temperature area does not have higher mean sensible heat flux", |
| 185 | ) |
| 186 | |
| 187 | |
| 188 | if __name__ == "__main__": |
| 189 | test() |