Fanning Software Consulting

Resizing Arrays Containing NaNs

Facebook Twitter RSS Google+

QUESTION: I have a 256 by 2000 by 32 floating point array which contains missing data represented by NaNs. I would like to resample this array, using 4 by 4 averaging in the first and second dimension to a 64 by 500 by 32 array. If the array didn't contains NaNs, I could simply use the IDL Rebin function to do this. But, this function doesn't contain a NaN keyword like the Mean function does, for example. Is there a way to do to this in IDL?

ANSWER: This answer is provided by Alain Lechacheux.

Alain gave a one-line answer on the IDL newsgroup, but even I had to study that for quite a few minutes to figure out what was going on. Let me pull it apart for you here, so you can understand what is happening.

What we are trying to do here is use dimensional juggling to rearrange the data in such a way we can use the Mean function with the NaN keyword to solve the problem for us. The first step is separate out the 4 by 4 chunks of data we want to average. We reform the first dimension (256) into a 4 by 64 array, and the second dimension (2000) into a 4 by 500 array.

   IDL> array = Reform(array, 4, 64, 4, 500, 32, /Overwrite)
   IDL> Help, array
        ARRAY    FLOAT = Array[4, 64, 4, 500, 32]

In the next step, we put the two dimensions of length 4 next to each other in the file. This can be done with the Transpose function.

   IDL> array = Transpose(array, [0, 2, 1, 3, 4])
   IDL> Help, array
        ARRAY    FLOAT = Array[4, 4, 64, 500, 32]

Next, we put the first two dimensions together, using the Reform function again.

   IDL> array = Reform(array, 16, 64, 500, 32, /Overwrite)
   IDL> Help, array
        ARRAY    FLOAT = Array[16, 64, 500, 32]

In the final step, we just calculate average value with the Mean function over the first dimension of the array, setting the NaN keyword.

   IDL> resampledArray = Mean(array, Dimension=1, /NaN)
   IDL> Help, resampledArray
        RESAMPLEDARRAY    FLOAT = Array[64, 500, 32]

And, finished!

Version of IDL used to prepare this article: IDL 8.2.3.

Written: 4 December 2013