Skip to content

Commit

Permalink
Merge pull request #104 from noaa-ocs-modeling/bugfix/np_rio_mask
Browse files Browse the repository at this point in the history
Consider the data mask when creating raster from numpy input
  • Loading branch information
SorooshMani-NOAA authored Aug 23, 2023
2 parents 58fc223 + 2b7e84e commit d75d9af
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ocsmesh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,8 @@ def raster_from_numpy(
crs=crs,
transform=transform,
) as dst:
if isinstance(data, np.ma.MaskedArray):
dst.nodata = data.fill_value
dst.write(data, 1)


Expand Down
33 changes: 33 additions & 0 deletions tests/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,39 @@ def test_diff_extent_x_n_y(self):
# TODO: Test when x and y extent are different
pass


def test_data_masking(self):
fill_value = 12
in_rast_xy = np.mgrid[0:1:0.2, 0:1:0.2]
in_rast_z_nomask = np.random.random(in_rast_xy[0].shape)
in_rast_z_mask = np.ma.MaskedArray(
in_rast_z_nomask,
mask=np.random.random(size=in_rast_z_nomask.shape) < 0.5,
fill_value=fill_value
)

with tempfile.NamedTemporaryFile(suffix='.tiff') as tf:
utils.raster_from_numpy(
tf.name,
data=in_rast_z_nomask,
mgrid=in_rast_xy,
crs=4326
)

rast = Raster(tf.name)
self.assertEqual(rast.src.nodata, None)

with tempfile.NamedTemporaryFile(suffix='.tiff') as tf:
utils.raster_from_numpy(
tf.name,
data=in_rast_z_mask,
mgrid=in_rast_xy,
crs=4326
)

rast = Raster(tf.name)
self.assertEqual(rast.src.nodata, fill_value)


if __name__ == '__main__':
unittest.main()

0 comments on commit d75d9af

Please sign in to comment.