masked out in read



In [18]:
import rasterio
import numpy as np

In [19]:
path = './dummy.asc'
with open(path) as f:
    for l in f:
        print l,


ncols         4
nrows         5
xllcorner     0.0
yllcorner     0.0
cellsize      1.0
NODATA_value  -9999
-9999 -9999   5  2
-9999    20 100 36
    3     8  35 10
   32    42  50  6
   88    75  27  9

In [50]:
ds = rasterio.open(path)
out = np.ma.zeros((5,4), dtype=ds.dtypes[0])
returned = ds.read_band(1, out=out, masked=True)

In [58]:
out


Out[58]:
masked_array(data =
 [[[-9999 -9999     5     2]
  [-9999    20   100    36]
  [    3     8    35    10]
  [   32    42    50     6]
  [   88    75    27     9]]],
             mask =
 False,
       fill_value = 999999)

In [52]:
returned


Out[52]:
masked_array(data =
 [[-- -- 5 2]
 [-- 20 100 36]
 [3 8 35 10]
 [32 42 50 6]
 [88 75 27 9]],
             mask =
 [[ True  True False False]
 [ True False False False]
 [False False False False]
 [False False False False]
 [False False False False]],
       fill_value = -9999)

In [32]:
a = np.array([1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8])
print np.around(a)
out = np.zeros((8))
print out
result = np.around(a,out=out)
print result
print out


[ 1.  2.  3.  4.  6.  7.  8.  9.]
[ 0.  0.  0.  0.  0.  0.  0.  0.]
[ 1.  2.  3.  4.  6.  7.  8.  9.]
[ 1.  2.  3.  4.  6.  7.  8.  9.]

In [54]:
ma = np.ma.array([1,2,3])

In [57]:
ma.fill_value = -9999
ma


Out[57]:
masked_array(data = [1 2 3],
             mask = False,
       fill_value = -9999)

In [62]:
out.mask = out == ma.fill_value

In [3]:
import numpy as np

a = np.ma.array([1, 2, 3, 4], mask=[True, False, False, True])
b = np.ma.array([1, 2, 3, 4], mask=[False, True, False, True])

# ufunc not in-place
c = np.add(a, b)

# object operator
d = a + b 

# ufunc in_place
np.add(a, b, out=a)

print 'not in-place:\n{}\n{}\n'.format(repr(c), c.data)
print 'object operator:\n{}\n{}\n'.format(repr(d), d.data)
print 'in-place:\n{}\n{}\n'.format(repr(a), a.data)


not in-place:
masked_array(data = [-- -- 6 --],
             mask = [ True  True False  True],
       fill_value = 999999)

[2 4 6 8]

object operator:
masked_array(data = [-- -- 6 --],
             mask = [ True  True False  True],
       fill_value = 999999)

[1 2 6 4]

in-place:
masked_array(data = [-- 4 6 --],
             mask = [ True False False  True],
       fill_value = 999999)

[2 4 6 8]


In [2]:



Out[2]:
masked_array(data = [-- 4 6 --],
             mask = [ True False False  True],
       fill_value = 999999)

In [ ]: