Skip to content

Commit

Permalink
Applied formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tebadi committed Oct 29, 2024
1 parent e8e7725 commit 3c7944b
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 170 deletions.
33 changes: 12 additions & 21 deletions odc/stats/plugins/l34_utils/l4_water.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,45 @@
NODATA = 255


def water_classification(l4, level3, intertidal_mask, water_persistence):
def water_classification(xx, intertidal_mask, water_persistence):

l4 = expr_eval(
"where((c==221)&(a==220)&(b==1), 101, c)",
{"a": level3, "b": water_persistence, "c": l4},
"where(((a==223)|(a==221))&(b==1), 101, a)",
{"a": xx.classes_l3_l4.data, "b": water_persistence},
name="mark_water",
dtype="uint8",
)

l4 = expr_eval(
"where((c==221)&(a==220)&(b==7), 102, c)",
{"a": level3, "b": water_persistence, "c": l4},
"where(((a==223)|(a==221))&(b==7), 102, a)",
{"a": l4, "b": water_persistence},
name="mark_water",
dtype="uint8",
)

l4 = expr_eval(
"where((c==221)&(a==220)&(b==8), 103, c)",
{"a": level3, "b": water_persistence, "c": l4},
"where(((a==223)|(a==221))&(b==8), 103, a)",
{"a": l4, "b": water_persistence},
name="mark_water",
dtype="uint8",
)

l4 = expr_eval(
"where((c==221)&(a==220)&(b==9), 104, c)",
{"a": level3, "b": water_persistence, "c": l4},
"where(((a==223)|(a==221))&(b==9), 104, a)",
{"a": l4, "b": water_persistence},
name="mark_water",
dtype="uint8",
)

# L34 water: (water_freq >= 0.2)
l4 = expr_eval(
"where((a==221)&(b==220)&(c==nodata), 99, a)",
{"a": l4, "b": level3, "c": water_persistence},
"where(((a==223)|(a==221))&(b==1), 100, a)",
{"a": l4, "b": intertidal_mask},
name="mark_water",
dtype="uint8",
**{"nodata": NODATA},
)

l4 = expr_eval(
"where((a==220)&(b==1), 100, c)",
{"a": level3, "b": intertidal_mask, "c": l4},
name="mark_water",
dtype="uint8",
)

l4 = expr_eval(
"where((a==221), 98, a)", {"a": l4}, name="mark_water", dtype="uint8"
"where((a==223)|(a==221), 98, a)", {"a": l4}, name="mark_water", dtype="uint8"
)

return l4
9 changes: 2 additions & 7 deletions odc/stats/plugins/l34_utils/l4_water_persistence.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import xarray as xr

from odc.stats._algebra import expr_eval
from . import utils

NODATA = 255
WATRE_FREQ_NODATA = -999
WATER_FREQ_NODATA = -999


def water_persistence(xx: xr.Dataset, watper_threshold):
Expand All @@ -15,11 +14,7 @@ def water_persistence(xx: xr.Dataset, watper_threshold):
{"a": xx.water_frequency.data},
name="mark_water",
dtype="uint8",
**{
"m": watper_threshold[3],
"nodata": NODATA,
"water_freq_nodata": WATRE_FREQ_NODATA,
},
**{"m": watper_threshold[3], "nodata": NODATA, "water_freq_nodata": WATER_FREQ_NODATA},
)

# 10 <= water_frequency < 1 --> 1
Expand Down
18 changes: 18 additions & 0 deletions odc/stats/plugins/l34_utils/lc_intertidal_mask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import xarray as xr

from odc.stats._algebra import expr_eval

NODATA = 255


def intertidal_mask(xx: xr.Dataset):

res = expr_eval(
"where(a==_u, 1, 0)",
{"a": xx.classes_l3_l4.data},
name="mask_intertidal",
dtype="uint8",
**{"_u": 223},
)

return res
25 changes: 13 additions & 12 deletions odc/stats/plugins/l34_utils/lc_level3.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@ def lc_level3(xx: xr.Dataset):
**{"_u": 210},
)

intertidal_mask = expr_eval(
"where(a==_u, 1, 0)",
{
"a": res,
"b": xx.urban_classes.data,
},
# Add intertidal as water
res = expr_eval(
"where((a==223)|(a==221), 220, b)",
{"a": xx.classes_l3_l4.data, "b": res},
name="mark_urban",
dtype="uint8",
**{"_u": 223},
)
# Add intertidal as water

# Mark nodata to 255 in case any nan
res = expr_eval(
"where((a==223)|(a==221)|(c==1), 220, b)",
{"a": xx.classes_l3_l4.data, "b": res, "c": intertidal_mask},
name="mark_urban",
"where(a==a, a, nodata)",
{
"a": res,
},
name="mark_nodata",
dtype="uint8",
**{"nodata": NODATA},
)

return intertidal_mask, res
return res
81 changes: 0 additions & 81 deletions odc/stats/plugins/lc_level3.py

This file was deleted.

38 changes: 19 additions & 19 deletions odc/stats/plugins/lc_level34.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
l4_bare_gradation,
l4_water,
lc_lifeform,
lc_intertidal_mask,
)


Expand Down Expand Up @@ -67,7 +68,22 @@ def fuser(self, xx):

def reduce(self, xx: xr.Dataset) -> xr.Dataset:

intertidal_mask, level3 = lc_level3.lc_level3(xx)
# Water persistence
water_persistence = l4_water_persistence.water_persistence(
xx, self.watper_threshold
)

water_seasonality = lc_water_seasonality.water_seasonality(
xx, self.water_seasonality_threshold
)

intertidal_mask = lc_intertidal_mask.intertidal_mask(xx)

# #TODO WATER (99-104)
l4 = l4_water.water_classification(xx, intertidal_mask, water_persistence)

# Generate Level3 classes
level3 = lc_level3.lc_level3(xx)

# Vegetation cover
veg_cover = l4_veg_cover.canopyco_veg_con(xx, self.veg_threshold)
Expand All @@ -76,9 +92,7 @@ def reduce(self, xx: xr.Dataset) -> xr.Dataset:
lifeform = lc_lifeform.lifeform(xx)

# Apply cultivated Level-4 classes (1-18)
l4 = l4_cultivated.lc_l4_cultivated(
xx.classes_l3_l4, level3, lifeform, veg_cover
)
l4 = l4_cultivated.lc_l4_cultivated(l4, level3, lifeform, veg_cover)

# Apply terrestrial vegetation classes [19-36]
l4 = l4_natural_veg.lc_l4_natural_veg(l4, level3, lifeform, veg_cover)
Expand All @@ -88,25 +102,11 @@ def reduce(self, xx: xr.Dataset) -> xr.Dataset:
xx, self.bare_threshold, veg_cover
)

# Water persistence
water_persistence = l4_water_persistence.water_persistence(
xx, self.watper_threshold
)

water_seasonality = lc_water_seasonality.water_seasonality(
xx, self.water_seasonality_threshold
)

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

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

# #TODO WATER (99-104)
level4 = l4_water.water_classification(
l4, level3, intertidal_mask, water_persistence
)
level4 = l4_surface.lc_l4_surface(l4, level3, bare_gradation)

level3 = level3.astype(np.uint8)
level4 = level4.astype(np.uint8)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_lc_l4_ctv.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def test_ctv_classes_woody():
xx = image_groups(l34, urban, cultivated, woody, pv_pc_50)

stats_l4 = StatsLccsLevel4()
intertidal_mask, level3 = lc_level3.lc_level3(xx)
level3 = lc_level3.lc_level3(xx)
lifeform = lc_lifeform.lifeform(xx)
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)

Expand Down Expand Up @@ -209,7 +209,7 @@ def test_ctv_classes_herbaceous():
xx = image_groups(l34, urban, cultivated, woody, pv_pc_50)

stats_l4 = StatsLccsLevel4()
intertidal_mask, level3 = lc_level3.lc_level3(xx)
level3 = lc_level3.lc_level3(xx)
lifeform = lc_lifeform.lifeform(xx)
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)

Expand Down Expand Up @@ -290,7 +290,7 @@ def test_ctv_classes_woody_herbaceous():
xx = image_groups(l34, urban, cultivated, woody, pv_pc_50)

stats_l4 = StatsLccsLevel4()
intertidal_mask, level3 = lc_level3.lc_level3(xx)
level3 = lc_level3.lc_level3(xx)
lifeform = lc_lifeform.lifeform(xx)
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)

Expand Down Expand Up @@ -371,7 +371,7 @@ def test_ctv_classes_no_vegcover():
xx = image_groups(l34, urban, cultivated, woody, pv_pc_50)

stats_l4 = StatsLccsLevel4()
intertidal_mask, level3 = lc_level3.lc_level3(xx)
level3 = lc_level3.lc_level3(xx)
lifeform = lc_lifeform.lifeform(xx)
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_lc_l4_natural_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def test_ns():
)

stats_l4 = StatsLccsLevel4()
intertidal_mask, level3 = lc_level3.lc_level3(xx)
level3 = lc_level3.lc_level3(xx)
lifeform = lc_lifeform.lifeform(xx)
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)

Expand Down
Loading

0 comments on commit 3c7944b

Please sign in to comment.