2

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()
Gurminder Bharani
  • 501
  • 1
  • 8
  • 22

2 Answers2

2

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
M4rtini
  • 13,186
  • 4
  • 35
  • 42
1

Is it crucial for it to have to equal -3.40282306e+38 for your algorithm to work? If nor try an inequality instead of equals. Equlaity works on floating point numbers if they are perfect integers such as 0,for numbers like youre using an inequality maybe the answer. Wont something like

(-3.40282306e+38-smallnumber) fix your problem?-You have to set small number to somthing in the context of your problem which I dont know what it is.