Skip to content

Commit

Permalink
Update the band names and fix the nodata in a few LC modules. (#169)
Browse files Browse the repository at this point in the history
* Changed the band names to match the config files. Also updated vegetation , lifeform and water seasonality to correctly replace nodata

* Fixed a typo in the name of the expression in the water sesonality

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update the path to the urban model on s3

* Differentiate mangroves and other aquatic veg (#170)

* differentiate mangroves and other aquatic veg

* fix level1 test

---------

Co-authored-by: Emma Ai <[email protected]>

* Fixed a few errors after rewriting the logic on aquatic vegetation

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Emma Ai <[email protected]>
Co-authored-by: Emma Ai <[email protected]>
  • Loading branch information
4 people authored Nov 18, 2024
1 parent 9ecc309 commit 52c37a0
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 710 deletions.
379 changes: 130 additions & 249 deletions odc/stats/plugins/l34_utils/l4_natural_aquatic.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions odc/stats/plugins/l34_utils/l4_veg_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def canopyco_veg_con(xx: xr.Dataset, veg_threshold):

# Mask NODATA
pv_pc_50 = expr_eval(
"where(a!=nodata, a, nodata)",
"where(a==a, a, nodata)",
{"a": xx.pv_pc_50.data},
name="mark_nodata",
dtype="uint8",
dtype="float32",
**{"nodata": NODATA},
)

Expand Down
4 changes: 2 additions & 2 deletions odc/stats/plugins/l34_utils/lc_level3.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def lc_level3(xx: xr.Dataset):

res = expr_eval(
"where((a!=a)|(a>=nodata), b, a)",
{"a": xx.cultivated_class.data, "b": xx.classes_l3_l4.data},
{"a": xx.cultivated.data, "b": xx.classes_l3_l4.data},
name="mask_cultivated",
dtype="float32",
**{"nodata": xx.cultivated_class.attrs.get("nodata")},
**{"nodata": xx.cultivated.attrs.get("nodata")},
)

# Mask urban results with bare sfc (210)
Expand Down
13 changes: 12 additions & 1 deletion odc/stats/plugins/l34_utils/lc_lifeform.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
from odc.stats._algebra import expr_eval
import xarray as xr

NODATA = 255


def lifeform(xx: xr.Dataset):

# 113 ----> 1 woody
# 114 ----> 2 herbaceous

lifeform_mask = expr_eval(
"where((a!=a)|(a>=nodata), nodata, a)",
{"a": xx.woody.data},
name="mark_lifeform",
dtype="float32",
**{"nodata": NODATA},
)

lifeform_mask = expr_eval(
"where(a==113, 1, a)",
{"a": xx.woody_cover.data},
{"a": lifeform_mask},
name="mark_lifeform",
dtype="uint8",
)
Expand Down
6 changes: 3 additions & 3 deletions odc/stats/plugins/l34_utils/lc_water_seasonality.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def water_seasonality(xx: xr.Dataset, water_seasonality_threshold):

# Apply nodata
water_frequency = expr_eval(
"where((a==watersea_nodata), nodata, a)",
"where((a!=a)|(a==watersea_nodata), nodata, a)",
{"a": xx.water_frequency.data},
name="mark_water_season",
dtype="uint8",
**{"watersea_nodata": WATER_FREQ_NODATA, "nodata": NODATA},
dtype="float32",
**{"nodata": NODATA, "watersea_nodata": WATER_FREQ_NODATA},
)

water_season_mask = expr_eval(
Expand Down
4 changes: 1 addition & 3 deletions odc/stats/plugins/lc_level34.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ def reduce(self, xx: xr.Dataset) -> xr.Dataset:
xx, self.bare_threshold, veg_cover
)

l4 = l4_natural_aquatic.natural_auquatic_veg(
l4, lifeform, veg_cover, water_seasonality
)
l4 = l4_natural_aquatic.natural_auquatic_veg(l4, veg_cover, water_seasonality)

level4 = l4_surface.lc_l4_surface(l4, level3, bare_gradation)

Expand Down
4 changes: 2 additions & 2 deletions odc/stats/plugins/lc_veg_class_a1.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def l3_class(self, xx: xr.Dataset):
{"a": data, "b": l3_mask},
name="intertidal_veg",
dtype="uint8",
**{"m": self.output_classes["aquatic_veg"]},
**{"m": self.output_classes["aquatic_veg_herb"]},
)
elif b == "canopy_cover_class":
# aquatic_veg: (mangroves > 0) & (mangroves != nodata)
Expand All @@ -161,7 +161,7 @@ def l3_class(self, xx: xr.Dataset):
dtype="uint8",
**{
"nodata": xx[b].attrs["nodata"],
"m": self.output_classes["aquatic_veg"],
"m": self.output_classes["aquatic_veg_wood"],
},
)

Expand Down
11 changes: 7 additions & 4 deletions tests/test_landcover_plugin_a1.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ def dataset():
def test_l3_classes(dataset):
stats_l3 = StatsVegClassL1(
output_classes={
"aquatic_veg": 124,
"aquatic_veg_wood": 124,
"aquatic_veg_herb": 125,
"terrestrial_veg": 110,
"water": 221,
"intertidal": 223,
Expand All @@ -141,7 +142,7 @@ def test_l3_classes(dataset):
expected_res = np.array(
[
[
[223, 221, 210, 124],
[223, 221, 210, 125],
[223, 223, 223, 210],
[223, 221, 223, 223],
[221, 223, 223, 223],
Expand All @@ -157,7 +158,8 @@ def test_l3_classes(dataset):
def test_l4_water_seasonality(dataset):
stats_l3 = StatsVegClassL1(
output_classes={
"aquatic_veg": 124,
"aquatic_veg_wood": 124,
"aquatic_veg_herb": 125,
"terrestrial_veg": 110,
"water": 221,
"intertidal": 223,
Expand Down Expand Up @@ -203,7 +205,8 @@ def test_l4_water_seasonality(dataset):
def test_reduce(dataset):
stats_l3 = StatsVegClassL1(
output_classes={
"aquatic_veg": 124,
"aquatic_veg_wood": 124,
"aquatic_veg_herb": 125,
"terrestrial_veg": 110,
"water": 221,
"intertidal": 223,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_lc_l34.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ def image_groups():
dims=("spec", "y", "x"),
attrs={"nodata": 255},
),
"cultivated_class": xr.DataArray(
"cultivated": xr.DataArray(
da.from_array(cultivated, chunks=(1, -1, -1)),
dims=("spec", "y", "x"),
attrs={"nodata": 255},
),
"woody_cover": xr.DataArray(
"woody": xr.DataArray(
da.from_array(woody, chunks=(1, -1, -1)),
dims=("spec", "y", "x"),
attrs={"nodata": 255},
Expand Down
5 changes: 3 additions & 2 deletions tests/test_lc_l4_ctv.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50):
dims=("spec", "y", "x"),
attrs={"nodata": 255},
),
"cultivated_class": xr.DataArray(
"cultivated": xr.DataArray(
da.from_array(cultivated, chunks=(1, -1, -1)),
dims=("spec", "y", "x"),
attrs={"nodata": 255},
),
"woody_cover": xr.DataArray(
"woody": xr.DataArray(
da.from_array(woody, chunks=(1, -1, -1)),
dims=("spec", "y", "x"),
attrs={"nodata": 255},
Expand Down Expand Up @@ -300,6 +300,7 @@ def test_ctv_classes_woody_herbaceous():
l4_ctv = l4_cultivated.lc_l4_cultivated(
xx.classes_l3_l4, level3, lifeform, veg_cover
)

assert (l4_ctv.compute() == expected_cultivated_classes).all()


Expand Down
6 changes: 3 additions & 3 deletions tests/test_lc_l4_natural_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def image_groups(l34, urban, woody, bs_pc_50, pv_pc_50, cultivated, water_freque
dims=("spec", "y", "x"),
attrs={"nodata": 255},
),
"cultivated_class": xr.DataArray(
"cultivated": xr.DataArray(
da.from_array(cultivated, chunks=(1, -1, -1)),
dims=("spec", "y", "x"),
attrs={"nodata": 255},
),
"woody_cover": xr.DataArray(
"woody": xr.DataArray(
da.from_array(woody, chunks=(1, -1, -1)),
dims=("spec", "y", "x"),
attrs={"nodata": 255},
Expand Down Expand Up @@ -188,7 +188,7 @@ def test_ns():
xx, stats_l4.water_seasonality_threshold
)
l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg(
l4_ctv_ntv, lifeform, veg_cover, water_seasonality
l4_ctv_ntv, veg_cover, water_seasonality
)

# Bare gradation
Expand Down
Loading

0 comments on commit 52c37a0

Please sign in to comment.