Skip to content

Commit 1cc0e77

Browse files
committed
Resolved a conflict
2 parents 40ab398 + d799a99 commit 1cc0e77

File tree

3 files changed

+95
-7
lines changed

3 files changed

+95
-7
lines changed

odc/stats/plugins/_base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Mapping, Optional, Sequence, Tuple
33

44
import xarray as xr
5+
import numpy as np
56
from datacube.model import Dataset
67
from datacube.utils.geometry import GeoBox
78
from odc.algo import to_rgba
@@ -47,6 +48,12 @@ def measurements(self) -> Tuple[str, ...]:
4748
pass
4849

4950
def native_transform(self, xx: xr.Dataset) -> xr.Dataset:
51+
for var in xx.data_vars:
52+
if (
53+
xx[var].attrs.get("nodata") is None
54+
and np.dtype(xx[var].dtype).kind == "f"
55+
):
56+
xx[var].attrs["nodata"] = xx[var].dtype.type(None)
5057
return xx
5158

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

odc/stats/plugins/lc_level3.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
Land Cover Level3 classification
3+
"""
4+
5+
from typing import Tuple
6+
import xarray as xr
7+
from odc.stats._algebra import expr_eval
8+
from ._registry import StatsPluginInterface, register
9+
10+
NODATA = 255
11+
12+
13+
class StatsLccsLevel3(StatsPluginInterface):
14+
NAME = "ga_ls_lccs_level3"
15+
SHORT_NAME = NAME
16+
VERSION = "0.0.1"
17+
PRODUCT_FAMILY = "lccs"
18+
19+
@property
20+
def measurements(self) -> Tuple[str, ...]:
21+
_measurements = ["level3_class"]
22+
return _measurements
23+
24+
def fuser(self, xx: xr.Dataset) -> xr.Dataset:
25+
return xx
26+
27+
def reduce(self, xx: xr.Dataset) -> xr.Dataset:
28+
29+
# Cultivated pipeline applies a mask which feeds only terrestrial veg (110) to the model
30+
# Just exclude no data (255 or nan) and apply the cultivated results
31+
# 255: load with product definition; nan: load without
32+
# hence accormmodate both
33+
34+
res = expr_eval(
35+
"where((a!=a)|(a>=nodata), b, a)",
36+
{"a": xx.cultivated_class.data, "b": xx.classes_l3_l4.data},
37+
name="mask_cultivated",
38+
dtype="float32",
39+
**{"nodata": xx.cultivated_class.attrs.get("nodata")},
40+
)
41+
42+
# Mask urban results with bare sfc (210)
43+
44+
res = expr_eval(
45+
"where(a==_u, b, a)",
46+
{
47+
"a": res,
48+
"b": xx.urban_classes.data,
49+
},
50+
name="mark_urban",
51+
dtype="float32",
52+
**{"_u": 210},
53+
)
54+
55+
# Mark nodata to 255 in case any nan
56+
57+
res = expr_eval(
58+
"where(a==a, a, nodata)",
59+
{
60+
"a": res,
61+
},
62+
name="mark_nodata",
63+
dtype="uint8",
64+
**{"nodata": NODATA},
65+
)
66+
67+
attrs = xx.attrs.copy()
68+
attrs["nodata"] = NODATA
69+
dims = xx.classes_l3_l4.dims[1:]
70+
71+
data_vars = {
72+
"level3_class": xr.DataArray(res.squeeze(), dims=dims, attrs=attrs)
73+
}
74+
75+
coords = dict((dim, xx.coords[dim]) for dim in dims)
76+
level3 = xr.Dataset(data_vars=data_vars, coords=coords, attrs=attrs)
77+
78+
return level3
79+
80+
81+
register("lccs_level3", StatsLccsLevel3)

odc/stats/plugins/lc_veg_class_a1.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ def measurements(self) -> Tuple[str, ...]:
9494
_measurements = ["classes_l3_l4", "water_seasonality"]
9595
return _measurements
9696

97-
def native_transform(self, xx):
98-
return xx
99-
10097
def fuser(self, xx):
10198
return xx
10299

@@ -175,12 +172,16 @@ def l3_class(self, xx: xr.Dataset):
175172
)
176173
elif b == "canopy_cover_class":
177174
# aquatic_veg: (mangroves > 0) & (mangroves != nodata)
175+
# mangroves.nodata = 255 or nan
178176
l3_mask = expr_eval(
179-
"where((a>0)&(a<nodata), m, b)",
177+
"where((a>0)&((a<nodata)|(nodata!=nodata)), m, b)",
180178
{"a": xx[b].data, "b": l3_mask},
181179
name="mark_mangroves",
182180
dtype="uint8",
183-
**{"nodata": NODATA, "m": self.output_classes["aquatic_veg"]},
181+
**{
182+
"nodata": xx[b].attrs["nodata"],
183+
"m": self.output_classes["aquatic_veg"],
184+
},
184185
)
185186

186187
# all unmarked values (0) is terretrial veg
@@ -199,10 +200,9 @@ def l3_class(self, xx: xr.Dataset):
199200

200201
# Mask nans with NODATA
201202
l3_mask = expr_eval(
202-
"where((a!=a)|(b>=nodata), nodata, e)",
203+
"where((a!=a), nodata, e)",
203204
{
204205
"a": si5,
205-
"b": xx.veg_frequency.data,
206206
"e": l3_mask,
207207
},
208208
name="mark_nodata",

0 commit comments

Comments
 (0)