[GRASS-SVN] r66973 - grass-addons/grass7/raster/r.series.filter

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Nov 28 10:55:21 PST 2015


Author: DmitryKolesov
Date: 2015-11-28 10:55:21 -0800 (Sat, 28 Nov 2015)
New Revision: 66973

Modified:
   grass-addons/grass7/raster/r.series.filter/r.series.filter.py
Log:
Fix r.series.filter: handle null values proprly

Modified: grass-addons/grass7/raster/r.series.filter/r.series.filter.py
===================================================================
--- grass-addons/grass7/raster/r.series.filter/r.series.filter.py	2015-11-28 17:59:44 UTC (rev 66972)
+++ grass-addons/grass7/raster/r.series.filter/r.series.filter.py	2015-11-28 18:55:21 UTC (rev 66973)
@@ -126,13 +126,16 @@
 
     return result
 
+def _non_zero(x):
+    return x.nonzero()[0]
+
 def _fill_nulls(arr):
     """Fill no-data values in arr
     Return np.array with filled data
     """
-    nz = lambda z: z.nonzero()[0]
     nans = np.isnan(arr)
-    arr[nans] = np.interp(nz(nans), nz(~nans), arr[~nans])
+    if not all(nans):
+        arr[nans] = np.interp(_non_zero(nans), _non_zero(~nans), arr[~nans])
 
     return arr
 
@@ -146,18 +149,27 @@
 
         reg = Region()
         for i in range(reg.rows):
-            row_data = np.array([r.get_row(i) for r in inputs])
+            # import ipdb; ipdb.set_trace()
+            row_data = np.array([_get_val_or_nan(r, i) for r in inputs])
             filtered_rows = _filter(row_data, winsize, order)
-            # import ipdb; ipdb.set_trace()
             for map_num in range(len(outputs)):
                 map = outputs[map_num]
-                row = filtered_rows[map_num]
+                row = filtered_rows[map_num, :]
                 buf = Buffer(row.shape, map.mtype, row)
                 map.put_row(i, buf)
     finally:
         close_rasters(outputs)
         close_rasters(inputs)
 
+def _get_val_or_nan(raster, row_num):
+    row = raster.get_row(row_num)
+    if raster.mtype != 'CELL':
+        return row
+    nans = (row == CNULL)
+    row = row.astype(np.float64)
+    row[nans.astype(np.bool)] = np.nan
+    return row
+
 def main(options, flags):
     xnames = options['input']
     xnames = xnames.split(',')



More information about the grass-commit mailing list