Fanning Software Consulting

Interpolating a Regular Grid

QUESTION: I have a 110 by 150 regular array of values. The columns of the grid represent pressure, and range from 70 to 1051 millibars, in steps of 9 millibars. The rows of the grid represent temperature, and range from 180 to 329 degrees, in steps of 1 degree K. How can I interpolate a value in this array? For example, suppose I wanted to extract a value that represented a pressure of 700.6 millibars and a temperature of 234.56 K. How would I do this?

ANSWER: In order to do the interpolation with Interpolate, you need to know the “fractional index” of the point you wish to interpolate. Probably the easiest way to create a fractional index is to create the vectors that correspond to your gridded array. I like to use cgScaleVector to do the scaling, since it is so convenient. So, to create the pressure and temperature vectors, we would type this.

   p = cgScaleVector(Findgen(110), 70, 1051)
   t = cgScaleVector(Findgen(150), 180, 329)

Then, we use Value_Locate to locate the value in the vector closest to the value we want to interpolate and return its index. Once we have that, it is easy to find the fractional index. The code will look something like this.

   ; Find pressure fractional index.
   pval = 700.6
   pstep = p[1] - p[0]
   closeIndex = Value_Locate(p, pval)
   pFracIndex = closeIndex + ((pval - p[closeIndex]) / pstep)

   ; Find temperature fractional index.
   tval = 234.56
   tstep = t[1] - t[0]
   closeIndex = Value_Locate(t, tval)
   tFracIndex = closeIndex + ((tval - t[closeIndex]) / tstep)

Then, all we have to do is interpolate to find the value we are looking for.

   interpValue = Interpolate(griddedArray, pFracIndex, tFracIndex)

Mark Hadfield points out that he has a function, MGH_LOCATE, in his Motley Library that does exactly this. Moreover, there are cousins MGH_LOCATE2 and MGH_LOCATE2A that do the same thing for 2D curvilinear arrays.

Google
 
Web Coyote's Guide to IDL Programming