Search code examples
pythonnumpyraster

How to replace no data value -3.4028231e+38 to numpy.nan


I have an 2d array constructed from raster image. The raster image has no data value assigned to -3.4028231e+38, I am trying to replace this value with 'nan' but I am unable to find this value when I apply conditional operator on it.

my data is as following:

>>> slice22 = inndvi[0:2,0:2]
>>> slice22
array([[ -3.40282306e+38,  -3.40282306e+38],
       [ -3.40282306e+38,  -3.40282306e+38]], dtype=float32)

when I try to check these value in if statement:

>>> if slice22[0][0] ==-3.40282306e+38:
...     print "yes"
... else:
...     print "no"
... 
no

the output is 'no'

Due to this I am not able to assign 3.40282306e+38 to numpy.nan as following:

slice22[slice22 == 3.40282306e+38] = numpy.nan

One more thing I would like to mention is that my dataset ranges from +2 to -2 in the raster. I have tried to use the range to eliminate the 3.40282306e+38 value, but still i get errors.

>>> slice22 [slice22 < 2 and slice22 >2 ]= np.nan 
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Solution

  • Considering you know your real values is between -2 and 2, you can easily filter out anything outside of this range.

    a[(a < -2) | (a > 2)] = np.nan   #option 1
    a[np.abs(a) > 2] = np.nan   #option 2
    a[np.logical_or(a < -2, a > 2)] = np.nan   #option 3