Converting WHERE Output to 2D or 3D Subscripts
QUESTION: How can I convert the one-dimensional index number that is returned by the WHERE function to the equivalent two- or three-dimensional location in my image?
ANSWER: The trick here is to understand how data is stored in IDL. In the 2D case (col, row), the data is stored in row order. (The values in each row are stored contiguously.) In the 3D case (col, row, frame), the values are stored one col-row image after the other.
(Thanks to William Daffer at JPL for providing this simple algorithm to do the conversion. William has a wonderful program to do this called UNPACK_WHERE. You can contact William at firstname.lastname@example.org for more information about it.)
Assume that your image is 2D. To find the row and column subscripts of the 1D indices returned by the WHERE function, do this:
index = WHERE(image EQ test) s = SIZE(image) ncol = s(1) col = index MOD ncol row = index / ncol
If your image is 3D, then to find the row, column, and frame subscripts, do this:
index = WHERE(image EQ test) s = SIZE(image) ncol = s nrow = s col = index mod ncol row = (index / ncol) mod nrow frame = index / (nrow*ncol)
Thanks to Donald Masturzo of Oregon State University for correcting some other nonsense that I had written here previously. :-)
Jack Saba (email@example.com) at NASA Goddard Space Flight Center has offered us a nice program that puts these ideas together. It is called WhereToMulti. It can convert WHERE subscripts into either 2D or 3D subscripts.
Numerous other people on the IDL newsgroup have offered N-dimensional versions of this. Here is one, named L_GETDIM, from Mark Schellens.
RSI offered a routine in IDL 6.0 to do this, called ARRAY_INDICES. This routine is also N-dimensional, although interestingly, it can be slower than the "MOD" method above for 2-3 dimensional arrays with lots of values. You might want to run a speed test to see which is faster in your application.
Copyright © 1997-2008 David W. Fanning Last Updated 23 September 2008