Skip to content

Commit

Permalink
Cleaned the code and removed redundant checks at level4
Browse files Browse the repository at this point in the history
  • Loading branch information
tebadi committed Nov 22, 2024
1 parent 2ef2f84 commit b8f77e5
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 213 deletions.
18 changes: 5 additions & 13 deletions odc/stats/plugins/l34_utils/l4_bare_gradation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,20 @@

def bare_gradation(xx: xr.Dataset, bare_threshold, veg_cover):

# Address nodata
bs_pc_50 = expr_eval(
"where((a!=a), nodata, a)",
{"a": xx.bs_pc_50.data},
name="mark_veg",
name="mark_bare_gradation_nodata",
dtype="float32",
**{"nodata": NODATA},
)

# Map any data > 100 ---> 100
bs_pc_50 = expr_eval(
"where((a>100)&(a!=nodata), 100, a)",
{"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)",
{"a": bs_pc_50},
name="mark_veg",
name="mark_bare",
dtype="uint8",
**{"m": bare_threshold[1], "nodata": NODATA},
)
Expand All @@ -37,7 +29,7 @@ def bare_gradation(xx: xr.Dataset, bare_threshold, veg_cover):
bs_mask = expr_eval(
"where((a>=m)&(a<n), 12, b)",
{"a": bs_pc_50, "b": bs_mask},
name="mark_veg",
name="mark_very_sparse_veg",
dtype="uint8",
**{"m": bare_threshold[0], "n": bare_threshold[1]},
)
Expand All @@ -46,7 +38,7 @@ def bare_gradation(xx: xr.Dataset, bare_threshold, veg_cover):
bs_mask = expr_eval(
"where(a<m, 10, b)",
{"a": bs_pc_50, "b": bs_mask},
name="mark_veg",
name="mark_sparse_veg",
dtype="uint8",
**{"m": bare_threshold[0]},
)
Expand Down
81 changes: 46 additions & 35 deletions odc/stats/plugins/l34_utils/l4_cultivated.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,95 +3,106 @@
NODATA = 255


def lc_l4_cultivated(l34, level3, lifeform, veg_cover):
def lc_l4_cultivated(l34, level3, woody, veg_cover):

woody = expr_eval(
"where((a!=a), nodata, a)",
{"a": woody.data},
name="mask_woody_nodata",
dtype="float32",
**{"nodata": NODATA},
)

# the 4-8 classes can't happen in LC since cultivated class will not be classified if vegetation doesn't exist.
# skip these classes in level4
l4 = expr_eval(
"where((d==110)&(a==111)&(b==10)&(c==1), 9, d)",
{"a": level3, "b": veg_cover, "c": lifeform, "d": l34},
"where((a==111), 1, d)",
{"a": level3, "d": l34},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==111)&(b==12)&(c==1), 10, d)",
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
"where((a==111)&(b==113), 2, d)",
{"a": level3, "b": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==111)&(b==13)&(c==1), 11, d)",
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
"where((a==111)&(b==114), 3, d)",
{"a": level3, "b": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==111)&(b==15)&(c==1), 12, d)",
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
"where((a==111)&(b==10)&(c==113), 9, d)",
{"a": level3, "b": veg_cover, "c": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==111)&(b==16)&(c==1), 13, d)",
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
"where((a==111)&(b==12)&(c==113), 10, d)",
{"a": level3, "b": veg_cover, "c": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==111)&(b==10)&(c==2), 14, d)",
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
"where((a==111)&(b==13)&(c==113), 11, d)",
{"a": level3, "b": veg_cover, "c": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)
l4 = expr_eval(
"where((d==110)&(a==111)&(b==12)&(c==2), 15, d)",
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
"where((a==111)&(b==15)&(c==113), 12, d)",
{"a": level3, "b": veg_cover, "c": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==111)&(b==13)&(c==2), 16, d)",
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
"where((a==111)&(b==16)&(c==113), 13, d)",
{"a": level3, "b": veg_cover, "c": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==111)&(b==15)&(c==2), 17, d)",
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
"where((a==111)&(b==10)&(c==114), 14, d)",
{"a": level3, "b": veg_cover, "c": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==111)&(b==16)&(c==2), 18, d)",
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
"where((a==111)&(b==12)&(c==114), 15, d)",
{"a": level3, "b": veg_cover, "c": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==111)&(b==1), 2, d)",
{"a": level3, "b": lifeform, "d": l4},
"where((a==111)&(b==13)&(c==114), 16, d)",
{"a": level3, "b": veg_cover, "c": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==111)&(b==2), 3, d)",
{"a": level3, "b": lifeform, "d": l4},
"where((a==111)&(b==15)&(c==114), 17, d)",
{"a": level3, "b": veg_cover, "c": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

# the 4-8 classes can't happen in LC since cultivated class will not be classified if vegetation doesn't exist.
# skip these classes in level4

l4 = expr_eval(
"where((d==110)&(a==111), 1, d)",
{"a": level3, "d": l4},
"where((a==111)&(b==16)&(c==114), 18, d)",
{"a": level3, "b": veg_cover, "c": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)





return l4
98 changes: 55 additions & 43 deletions odc/stats/plugins/l34_utils/l4_natural_veg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,128 +3,140 @@
NODATA = 255


def lc_l4_natural_veg(l4, l3, lifeform, veg_cover):
def lc_l4_natural_veg(l4, l3, woody, veg_cover):

woody = expr_eval(
"where((a!=a), nodata, a)",
{"a": woody.data},
name="mask_woody_nodata",
dtype="float32",
**{"nodata": NODATA},
)

l4 = expr_eval(
"where((a==110)&(b==nodata), nodata, a)",
"where((b==nodata), nodata, a)",
{"a": l4, "b": l3},
name="mark_cultivated",
dtype="uint8",
**{"nodata": NODATA},
)

l4 = expr_eval(
"where((d==110)&(a==112)&(c==10)&(b==1), 27, d)",
{"a": l3, "b": lifeform, "c": veg_cover, "d": l4},
"where((a==112), 19, d)",
{"a": l3, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==112)&(c==12)&(b==1), 28, d)",
{"a": l3, "b": lifeform, "c": veg_cover, "d": l4},
"where((a==112)&(b==113), 20, d)",
{"a": l3, "b": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==112)&(c==13)&(b==1), 29, d)",
{"a": l3, "b": lifeform, "c": veg_cover, "d": l4},
"where((a==112)&(b==114), 21, d)",
{"a": l3, "b": woody, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==112)&(c==15)&(b==1), 30, d)",
{"a": l3, "b": lifeform, "c": veg_cover, "d": l4},
"where((a==112)&(c==10), 22, d)",
{"a": l3, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==112)&(c==16)&(b==1), 31, d)",
{"a": l3, "b": lifeform, "c": veg_cover, "d": l4},
"where((a==112)&(c==12), 23, d)",
{"a": l3, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==112)&(c==10)&(b==2), 32, d)",
{"a": l3, "b": lifeform, "c": veg_cover, "d": l4},
"where((a==112)&(c==13), 24, d)",
{"a": l3, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)
l4 = expr_eval(
"where((d==110)&(a==112)&(c==12)&(b==2), 33, d)",
{"a": l3, "b": lifeform, "c": veg_cover, "d": l4},
"where((a==112)&(c==15), 25, d)",
{"a": l3, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)
l4 = expr_eval(
"where((d==110)&(a==112)&(c==13)&(b==2), 34, d)",
{"a": l3, "b": lifeform, "c": veg_cover, "d": l4},
"where((a==112)&(c==16), 26, d)",
{"a": l3, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==112)&(c==15)&(b==2), 35, d)",
{"a": l3, "b": lifeform, "c": veg_cover, "d": l4},
"where((a==112)&(c==10)&(b==113), 27, d)",
{"a": l3, "b": woody, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==112)&(c==16)&(b==2), 36, d)",
{"a": l3, "b": lifeform, "c": veg_cover, "d": l4},
"where((a==112)&(c==12)&(b==113), 28, d)",
{"a": l3, "b": woody, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==112)&(b==1), 20, d)",
{"a": l3, "b": lifeform, "d": l4},
"where((a==112)&(c==13)&(b==113), 29, d)",
{"a": l3, "b": woody, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==112)&(b==2), 21, d)",
{"a": l3, "b": lifeform, "d": l4},
"where((a==112)&(c==15)&(b==113), 30, d)",
{"a": l3, "b": woody, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==112)&(c==10), 22, d)",
{"a": l3, "c": veg_cover, "d": l4},
"where((a==112)&(c==16)&(b==113), 31, d)",
{"a": l3, "b": woody, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==112)&(c==12), 23, d)",
{"a": l3, "c": veg_cover, "d": l4},
"where((a==112)&(c==10)&(b==114), 32, d)",
{"a": l3, "b": woody, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)
l4 = expr_eval(
"where((d==110)&(a==112)&(c==13), 24, d)",
{"a": l3, "c": veg_cover, "d": l4},
"where((a==112)&(c==12)&(b==114), 33, d)",
{"a": l3, "b": woody, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)
l4 = expr_eval(
"where((d==110)&(a==112)&(c==15), 25, d)",
{"a": l3, "c": veg_cover, "d": l4},
"where((a==112)&(c==13)&(b==114), 34, d)",
{"a": l3, "b": woody, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)
l4 = expr_eval(
"where((d==110)&(a==112)&(c==16), 26, d)",
{"a": l3, "c": veg_cover, "d": l4},
"where((a==112)&(c==15)&(b==114), 35, d)",
{"a": l3, "b": woody, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)

l4 = expr_eval(
"where((d==110)&(a==112), 19, d)",
{"a": l3, "d": l4},
"where((a==112)&(c==16)&(b==114), 36, d)",
{"a": l3, "b": woody, "c": veg_cover, "d": l4},
name="mark_cultivated",
dtype="uint8",
)



return l4
Loading

0 comments on commit b8f77e5

Please sign in to comment.