From fd6f79922a5686114ecc9e31fe9d65bcd655809b Mon Sep 17 00:00:00 2001 From: Toktam Ebadi Date: Tue, 19 Nov 2024 03:38:37 +0000 Subject: [PATCH] Ensured nodata is replaced with 255 in all level4 plugins --- odc/stats/plugins/l34_utils/l4_bare_gradation.py | 12 ++++++++++-- odc/stats/plugins/l34_utils/l4_veg_cover.py | 4 ++-- odc/stats/plugins/l34_utils/l4_water.py | 2 +- .../plugins/l34_utils/l4_water_persistence.py | 14 +++++--------- odc/stats/plugins/l34_utils/lc_intertidal_mask.py | 2 +- odc/stats/plugins/l34_utils/lc_level3.py | 4 ++-- .../plugins/l34_utils/lc_water_seasonality.py | 7 +++---- odc/stats/plugins/lc_level34.py | 9 ++++----- tests/test_lc_l34.py | 2 +- tests/test_lc_l4_ctv.py | 10 +++++----- tests/test_lc_l4_natural_surface.py | 4 ++-- tests/test_lc_l4_nav.py | 10 +++++----- tests/test_lc_l4_ntv.py | 10 +++++----- tests/test_lc_l4_water.py | 2 +- tests/test_lc_level3.py | 2 +- 15 files changed, 48 insertions(+), 46 deletions(-) diff --git a/odc/stats/plugins/l34_utils/l4_bare_gradation.py b/odc/stats/plugins/l34_utils/l4_bare_gradation.py index 86e80173..3acbcf9c 100644 --- a/odc/stats/plugins/l34_utils/l4_bare_gradation.py +++ b/odc/stats/plugins/l34_utils/l4_bare_gradation.py @@ -7,15 +7,23 @@ def bare_gradation(xx: xr.Dataset, bare_threshold, veg_cover): + bs_pc_50 = expr_eval( + "where((a!=a), nodata, a)", + {"a": xx.bs_pc_50.data}, + name="mark_veg", + dtype="float32", + **{"nodata": NODATA}, + ) + # Map any data > 100 ---> 100 bs_pc_50 = expr_eval( "where((a>100)&(a!=nodata), 100, a)", - {"a": xx.bs_pc_50.data}, + {"a": bs_pc_50}, name="mark_veg", dtype="uint8", **{"nodata": NODATA}, ) - + # 60% <= data --> 15 bs_mask = expr_eval( "where((a>=m)&(a!=nodata), 15, a)", diff --git a/odc/stats/plugins/l34_utils/l4_veg_cover.py b/odc/stats/plugins/l34_utils/l4_veg_cover.py index 7f194444..904db0f1 100644 --- a/odc/stats/plugins/l34_utils/l4_veg_cover.py +++ b/odc/stats/plugins/l34_utils/l4_veg_cover.py @@ -15,7 +15,7 @@ def canopyco_veg_con(xx: xr.Dataset, veg_threshold): dtype="float32", **{"nodata": NODATA}, ) - + # Map any data > 100 ---> 100 pv_pc_50 = expr_eval( "where((a>100) & (a!=nodata), 100, a)", @@ -27,7 +27,7 @@ def canopyco_veg_con(xx: xr.Dataset, veg_threshold): **{"nodata": NODATA}, ) - # ## data < 1 ---> 0 + # data < 1 ---> 0 veg_mask = expr_eval( "where(a 1 diff --git a/odc/stats/plugins/l34_utils/lc_intertidal_mask.py b/odc/stats/plugins/l34_utils/lc_intertidal_mask.py index 9b9cd8da..ba44033b 100644 --- a/odc/stats/plugins/l34_utils/lc_intertidal_mask.py +++ b/odc/stats/plugins/l34_utils/lc_intertidal_mask.py @@ -9,7 +9,7 @@ def intertidal_mask(xx: xr.Dataset): res = expr_eval( "where(a==_u, 1, 0)", - {"a": xx.classes_l3_l4.data}, + {"a": xx.level_3_4.data}, name="mask_intertidal", dtype="uint8", **{"_u": 223}, diff --git a/odc/stats/plugins/l34_utils/lc_level3.py b/odc/stats/plugins/l34_utils/lc_level3.py index 146a64fc..f8071a50 100644 --- a/odc/stats/plugins/l34_utils/lc_level3.py +++ b/odc/stats/plugins/l34_utils/lc_level3.py @@ -14,7 +14,7 @@ def lc_level3(xx: xr.Dataset): res = expr_eval( "where((a!=a)|(a>=nodata), b, a)", - {"a": xx.cultivated.data, "b": xx.classes_l3_l4.data}, + {"a": xx.cultivated.data, "b": xx.level_3_4.data}, name="mask_cultivated", dtype="float32", **{"nodata": xx.cultivated.attrs.get("nodata")}, @@ -46,7 +46,7 @@ def lc_level3(xx: xr.Dataset): # Add intertidal as water res = expr_eval( "where((a==223)|(a==221), 220, b)", - {"a": xx.classes_l3_l4.data, "b": res}, + {"a": xx.level_3_4.data, "b": res}, name="mark_urban", dtype="uint8", ) diff --git a/odc/stats/plugins/l34_utils/lc_water_seasonality.py b/odc/stats/plugins/l34_utils/lc_water_seasonality.py index d5fca52d..1c3ed734 100644 --- a/odc/stats/plugins/l34_utils/lc_water_seasonality.py +++ b/odc/stats/plugins/l34_utils/lc_water_seasonality.py @@ -2,21 +2,20 @@ from odc.stats._algebra import expr_eval -WATER_FREQ_NODATA = -999 NODATA = 255 def water_seasonality(xx: xr.Dataset, water_seasonality_threshold): # >= 3 months ----> 1 Semi-permanent or permanent # < 3 months ----> 2 Temporary or seasonal - + # Apply nodata water_frequency = expr_eval( - "where((a!=a)|(a==watersea_nodata), nodata, a)", + "where((a!=a), nodata, a)", {"a": xx.water_frequency.data}, name="mark_water_season", dtype="float32", - **{"nodata": NODATA, "watersea_nodata": WATER_FREQ_NODATA}, + **{"nodata": NODATA}, ) water_season_mask = expr_eval( diff --git a/odc/stats/plugins/lc_level34.py b/odc/stats/plugins/lc_level34.py index 5fe958a3..15ba9d6f 100644 --- a/odc/stats/plugins/lc_level34.py +++ b/odc/stats/plugins/lc_level34.py @@ -64,16 +64,16 @@ def reduce(self, xx: xr.Dataset) -> xr.Dataset: water_persistence = l4_water_persistence.water_persistence( xx, self.watper_threshold ) - + # print("### Water Persistence", np.unique(water_persistence.compute())) water_seasonality = lc_water_seasonality.water_seasonality( xx, self.water_seasonality_threshold ) - + # print("### Water Seasonality", np.unique(water_persistence.compute())) intertidal_mask = lc_intertidal_mask.intertidal_mask(xx) # #TODO WATER (99-104) l4 = l4_water.water_classification(xx, intertidal_mask, water_persistence) - + # print("### Water classification", np.unique(water_persistence.compute())) # Generate Level3 classes level3 = lc_level3.lc_level3(xx) @@ -93,7 +93,6 @@ def reduce(self, xx: xr.Dataset) -> xr.Dataset: bare_gradation = l4_bare_gradation.bare_gradation( xx, self.bare_threshold, veg_cover ) - l4 = l4_natural_aquatic.natural_auquatic_veg(l4, veg_cover, water_seasonality) level4 = l4_surface.lc_l4_surface(l4, level3, bare_gradation) @@ -103,7 +102,7 @@ def reduce(self, xx: xr.Dataset) -> xr.Dataset: attrs = xx.attrs.copy() attrs["nodata"] = NODATA - dims = xx.classes_l3_l4.dims[1:] + dims = xx.level_3_4.dims[1:] data_vars = { k: xr.DataArray(v, dims=dims, attrs=attrs) for k, v in zip(self.measurements, [level3.squeeze(), level4.squeeze()]) diff --git a/tests/test_lc_l34.py b/tests/test_lc_l34.py index 4fba2fb5..cd353417 100644 --- a/tests/test_lc_l34.py +++ b/tests/test_lc_l34.py @@ -106,7 +106,7 @@ def image_groups(): } data_vars = { - "classes_l3_l4": xr.DataArray( + "level_3_4": xr.DataArray( da.from_array(l34, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, diff --git a/tests/test_lc_l4_ctv.py b/tests/test_lc_l4_ctv.py index 6e160220..7375e7e4 100644 --- a/tests/test_lc_l4_ctv.py +++ b/tests/test_lc_l4_ctv.py @@ -27,7 +27,7 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50): } data_vars = { - "classes_l3_l4": xr.DataArray( + "level_3_4": xr.DataArray( da.from_array(l34, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, @@ -135,7 +135,7 @@ def test_ctv_classes_woody(): veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) l4_ctv = l4_cultivated.lc_l4_cultivated( - xx.classes_l3_l4, level3, lifeform, veg_cover + xx.level_3_4, level3, lifeform, veg_cover ) assert (l4_ctv.compute() == expected_cultivated_classes).all() @@ -217,7 +217,7 @@ def test_ctv_classes_herbaceous(): veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) l4_ctv = l4_cultivated.lc_l4_cultivated( - xx.classes_l3_l4, level3, lifeform, veg_cover + xx.level_3_4, level3, lifeform, veg_cover ) assert (l4_ctv.compute() == expected_cultivated_classes).all() @@ -298,7 +298,7 @@ def test_ctv_classes_woody_herbaceous(): veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) l4_ctv = l4_cultivated.lc_l4_cultivated( - xx.classes_l3_l4, level3, lifeform, veg_cover + xx.level_3_4, level3, lifeform, veg_cover ) assert (l4_ctv.compute() == expected_cultivated_classes).all() @@ -380,6 +380,6 @@ def test_ctv_classes_no_vegcover(): veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) l4_ctv = l4_cultivated.lc_l4_cultivated( - xx.classes_l3_l4, level3, lifeform, veg_cover + xx.level_3_4, level3, lifeform, veg_cover ) assert (l4_ctv.compute() == expected_cultivated_classes).all() diff --git a/tests/test_lc_l4_natural_surface.py b/tests/test_lc_l4_natural_surface.py index 97322542..29b80d7b 100644 --- a/tests/test_lc_l4_natural_surface.py +++ b/tests/test_lc_l4_natural_surface.py @@ -36,7 +36,7 @@ def image_groups(l34, urban, woody, bs_pc_50, pv_pc_50, cultivated, water_freque } data_vars = { - "classes_l3_l4": xr.DataArray( + "level_3_4": xr.DataArray( da.from_array(l34, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, @@ -180,7 +180,7 @@ def test_ns(): # Apply cultivated to match the code in Level4 processing l4_ctv = l4_cultivated.lc_l4_cultivated( - xx.classes_l3_l4, level3, lifeform, veg_cover + xx.level_3_4, level3, lifeform, veg_cover ) l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover) diff --git a/tests/test_lc_l4_nav.py b/tests/test_lc_l4_nav.py index 26af5ccb..7e6f0bc2 100644 --- a/tests/test_lc_l4_nav.py +++ b/tests/test_lc_l4_nav.py @@ -34,7 +34,7 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50, water_frequency): } data_vars = { - "classes_l3_l4": xr.DataArray( + "level_3_4": xr.DataArray( da.from_array(l34, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, @@ -154,7 +154,7 @@ def test_ntv_classes_woody_herbaceous(): # Apply cultivated to match the code in Level4 processing l4_ctv = l4_cultivated.lc_l4_cultivated( - xx.classes_l3_l4, level3, lifeform, veg_cover + xx.level_3_4, level3, lifeform, veg_cover ) l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover) @@ -256,7 +256,7 @@ def test_ntv_herbaceous_seasonal_water_veg_cover(): # Apply cultivated to match the code in Level4 processing l4_ctv = l4_cultivated.lc_l4_cultivated( - xx.classes_l3_l4, level3, lifeform, veg_cover + xx.level_3_4, level3, lifeform, veg_cover ) l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover) @@ -358,7 +358,7 @@ def test_ntv_woody_seasonal_water_veg_cover(): # Apply cultivated to match the code in Level4 processing l4_ctv = l4_cultivated.lc_l4_cultivated( - xx.classes_l3_l4, level3, lifeform, veg_cover + xx.level_3_4, level3, lifeform, veg_cover ) l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover) @@ -455,7 +455,7 @@ def test_ntv_herbaceous_permanent_water_veg_cover(): # Apply cultivated to match the code in Level4 processing l4_ctv = l4_cultivated.lc_l4_cultivated( - xx.classes_l3_l4, level3, lifeform, veg_cover + xx.level_3_4, level3, lifeform, veg_cover ) l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover) diff --git a/tests/test_lc_l4_ntv.py b/tests/test_lc_l4_ntv.py index e4761908..e63ec600 100644 --- a/tests/test_lc_l4_ntv.py +++ b/tests/test_lc_l4_ntv.py @@ -31,7 +31,7 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50): } data_vars = { - "classes_l3_l4": xr.DataArray( + "level_3_4": xr.DataArray( da.from_array(l34, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, @@ -137,7 +137,7 @@ def test_ntv_classes_herbaceous(): lifeform = lc_lifeform.lifeform(xx) veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) l4_ntv = l4_natural_veg.lc_l4_natural_veg( - xx.classes_l3_l4, level3, lifeform, veg_cover + xx.level_3_4, level3, lifeform, veg_cover ) assert (l4_ntv.compute() == expected_natural_terrestrial_veg_classes).all() @@ -218,7 +218,7 @@ def test_ntv_classes_woody(): veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) l4_ntv = l4_natural_veg.lc_l4_natural_veg( - xx.classes_l3_l4, level3, lifeform, veg_cover + xx.level_3_4, level3, lifeform, veg_cover ) assert (l4_ntv.compute() == expected_natural_terrestrial_veg_classes).all() @@ -298,7 +298,7 @@ def test_ntv_classes_no_veg(): lifeform = lc_lifeform.lifeform(xx) veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) l4_ntv = l4_natural_veg.lc_l4_natural_veg( - xx.classes_l3_l4, level3, lifeform, veg_cover + xx.level_3_4, level3, lifeform, veg_cover ) assert (l4_ntv.compute() == expected_natural_terrestrial_veg_classes).all() @@ -378,6 +378,6 @@ def test_ntv_classes_no_lifeform(): lifeform = lc_lifeform.lifeform(xx) veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) l4_ntv = l4_natural_veg.lc_l4_natural_veg( - xx.classes_l3_l4, level3, lifeform, veg_cover + xx.level_3_4, level3, lifeform, veg_cover ) assert (l4_ntv.compute() == expected_natural_terrestrial_veg_classes).all() diff --git a/tests/test_lc_l4_water.py b/tests/test_lc_l4_water.py index 62fdaf45..9029fb85 100644 --- a/tests/test_lc_l4_water.py +++ b/tests/test_lc_l4_water.py @@ -32,7 +32,7 @@ def image_groups(l34, urban, cultivated, woody, bs_pc_50, pv_pc_50, water_freque } data_vars = { - "classes_l3_l4": xr.DataArray( + "level_3_4": xr.DataArray( da.from_array(l34, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, diff --git a/tests/test_lc_level3.py b/tests/test_lc_level3.py index 1906c758..df1957ff 100644 --- a/tests/test_lc_level3.py +++ b/tests/test_lc_level3.py @@ -64,7 +64,7 @@ def image_groups(): } data_vars = { - "classes_l3_l4": xr.DataArray( + "level_3_4": xr.DataArray( da.from_array(l34, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255},