Skip to content

Commit 4d0732b

Browse files
author
Emma Ai
committed
fix the nodata issue when default to nan with float dtype
1 parent 980ded2 commit 4d0732b

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
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"] = np.nan
5057
return xx
5158

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

odc/stats/plugins/lc_level3.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,22 @@ def measurements(self) -> Tuple[str, ...]:
2121
_measurements = ["level3_class"]
2222
return _measurements
2323

24+
def fuser(self, xx: xr.Dataset) -> xr.Dataset:
25+
return xx
26+
2427
def reduce(self, xx: xr.Dataset) -> xr.Dataset:
2528

2629
# Cultivated pipeline applies a mask which feeds only terrestrial veg (110) to the model
27-
# Just exclude no data (255) and apply the cultivated results
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+
2834
res = expr_eval(
29-
"where(a<nodata, a, b)",
35+
"where(((a==a)&(nodata!=nodata))|((a<nodata)&(nodata==nodata)), a, b)",
3036
{"a": xx.cultivated_class.data, "b": xx.classes_l3_l4.data},
3137
name="mask_cultivated",
3238
dtype="float32",
33-
**{"nodata": NODATA},
39+
**{"nodata": xx.cultivated_class.attrs.get("nodata")},
3440
)
3541

3642
# Mask urban results with bare sfc (210)
@@ -42,10 +48,22 @@ def reduce(self, xx: xr.Dataset) -> xr.Dataset:
4248
"b": xx.urban_classes.data,
4349
},
4450
name="mark_urban",
45-
dtype="uint8",
51+
dtype="float32",
4652
**{"_u": 210},
4753
)
4854

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+
4967
attrs = xx.attrs.copy()
5068
attrs["nodata"] = NODATA
5169
dims = xx.classes_l3_l4.dims[1:]

odc/stats/plugins/lc_veg_class_a1.py

Lines changed: 8 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))|((a>0)&(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,15 +200,15 @@ 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)|((b>=_n)&(_n==_n))|(b!=b), nodata, e)",
203204
{
204205
"a": si5,
205206
"b": xx.veg_frequency.data,
206207
"e": l3_mask,
207208
},
208209
name="mark_nodata",
209210
dtype="uint8",
210-
**{"nodata": NODATA},
211+
**{"_n": xx.veg_frequency.attrs["nodata"], "nodata": NODATA},
211212
)
212213

213214
# Now add the water frequency

0 commit comments

Comments
 (0)