Locating NaN and Inf values in an array
Anonym
(This week's post is brief because I'm out learning a lot at the 2013 NOAA Satellite Conference!) The ubiquitous WHERE function can be used to quickly locate values in an array. However, you can't directly search for the location of IEEE NaN (not a number) and Inf (infinity) values; to do so, you need the FINITE function. Here's an example. Start with a simple array:
IDL> a = findgen(5, 2)
IDL> print, a
0.000000 1.00000 2.00000 3.00000 4.00000
5.00000 6.00000 7.00000 8.00000 9.00000
and add NaN values at up to three random locations:
IDL> i = floor(randomu(seed, 3)*n_elements(a))
IDL> a[i] = !values.f_nan
IDL> print, a
0.000000 1.00000 2.00000 3.00000 NaN
NaN 6.00000 7.00000 NaN 9.00000
OK, so where are the NaNs located? By eye, I can see where they are (indices 4, 5 and 8), but to identify them programmatically, use WHERE with FINITE and "~", the logical not operator:
IDL> i_nan = where(~finite(a), /null)
IDL> print, i_nan
4 5 8
Check!