Skip to content
Snippets Groups Projects
Commit 45db43c5 authored by Sebastian Müller's avatar Sebastian Müller 🐈
Browse files

Masking: incorporate 'missing_value' meta data

parent 24e44700
No related branches found
No related tags found
1 merge request!258Grid mask support
Pipeline #187249 passed with stages
in 4 minutes and 32 seconds
......@@ -28,10 +28,15 @@ class Masking(Adapter):
Parameters
----------
nodata : numeric, optional
Value to set at masked positions. Default: np.nan
Value to set at masked positions. Default: None
If None, will be determined from outputs "missing_value"
meta data or, if that is missing, from the inputs meta data.
In- and output "missing_value" can be different but if a
nodata value is given and output has a "missing_value", they
need to match (will not be overwritten).
"""
def __init__(self, nodata=np.nan):
def __init__(self, nodata=None):
super().__init__()
self.nodata = nodata
self._canonical_mask = None
......@@ -57,8 +62,11 @@ class Masking(Adapter):
def _get_info(self, info):
# info coming from output, set grid None to get the input grid
request = info.copy_with(grid=None)
request = info.copy_with(grid=None, missing_value=None)
in_info = self.exchange_info(request)
# get missing value from cf-convention meta data (in/out can differ here)
out_nodata = info.meta.get("missing_value", None)
in_nodata = in_info.meta.get("missing_value", None)
if info.grid is None:
with ErrorLogger(self.logger):
......@@ -81,11 +89,17 @@ class Masking(Adapter):
# create_selection
if self._sub_grid.mask is not None:
self._canonical_mask = self._sub_grid.to_canonical(self._sub_grid.mask)
# check no-data value
if self.nodata is None:
self.nodata = out_nodata if out_nodata is not None else in_nodata
if self.nodata is None:
with ErrorLogger(self.logger):
raise FinamMetaDataError("Couldn't determine no-data value.")
else:
self._canonical_mask = None
# return output info
return in_info.copy_with(grid=info.grid)
return in_info.copy_with(grid=info.grid, missing_value=self.nodata)
def _masks_compatible(self, sup_grid, sub_grid):
if sup_grid.mask is None:
......
......@@ -25,7 +25,8 @@ class TestMasking(unittest.TestCase):
in_grid = EsriGrid(ncols=3, nrows=4, order="F")
out_grid = EsriGrid(ncols=3, nrows=4, mask=mask, order="F")
in_info = Info(time=time, grid=in_grid, units="m")
# missing_value to indicate no-data value in masking adapter
in_info = Info(time=time, grid=in_grid, units="m", missing_value=-9999)
in_data = np.zeros(shape=in_info.grid.data_shape, order=in_info.grid.order)
in_data.data[0, 0] = 1.0
......@@ -46,7 +47,8 @@ class TestMasking(unittest.TestCase):
composition = Composition([source, sink], log_level="DEBUG")
composition.initialize()
source.outputs["Output"] >> Masking(nodata=-9999) >> sink.inputs["Input"]
# no-data value from missing-value (from source)
source.outputs["Output"] >> Masking(nodata=None) >> sink.inputs["Input"]
composition.connect()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment