PyNGL Home > Functions > Interpolation


Linearly interpolates to fill in missing values.

Available in version 1.3.0 or later.


y = Ngl.linmsg(x, end_pts_msg=None, max_msg=None, fill_value=1.e20)



An array of any dimensionality. Missing values should be indicated by fill_value. If fill_value is not set, then the missing value default appropriate to the type of x will be assumed. The fastest varying dimension (the rightmost dimension) is the dimension upon which the linear interpolation will be performed.


A scalar that specifies how missing beginning and end points will be returned. If this value is greater than or equal to 0, then the beginning and end points will be returned as missing (default option). If this value is less than 0, then they will be set to the nearest non-missing value.


Specifies the maximum number of consecutive missing values to be interpolated. If max_msg is not set, then the routine will try to interpolate as many values as it can.


The missing value, if any.

Return value


A multi-dimensional array of the same size as x.


This function uses piecewise linear interpolation to fill in any missing values. End points which are missing on input may be returned as missing or set to the nearest non-missing value.


Example 1

This example returns the interpolated values to the original one-dimensional array. The end points will be left as missing.

x = [ 1190.,1455.,1550.,-999.,1745.,1770., \
      1900.,-999.,-999.,-999.,2335.,2490., \
      2720.,2710.,2530.,2900.,2760.,-999. ]

x = Ngl.linmsg(x)   # missing end point(s) unaltered

After interpolation, x contains:
  [ 1190.,1455.  ,1550. ,1647.5 ,1745.,1770., \
    1900.,2008.75,2117.5,2226.25,2335.,2490., \
    2720.,2710.  ,2530. ,2900.  ,2760.,-999.]

Example 2

This example returns the interpolated values to a new 1D array. The missing value has been specified by the user. The end points with missing values will be set to the nearest non-missing value.

ymsg = 1.e10
y = [ 1115.,ymsg ,1515.,1794.,ymsg ,1710., \
      1830.,1920.,1970.,2300.,2280.,2520., \
      2630.,ymsg ,ymsg ,2800.,ymsg ,ymsg  /)

# end_pts_msg = -1 --> missing end point(s) set to nearest non-missing value

ynew = Ngl.linmsg(y,-1,fill_value=ymsg) 

After interpolation ynew contains:

   [1115.,1315.,1515.,1794.,1752.,1710., \
    1830.,1920.,1970.,2300.,2280.,2520., \
    2630.,2686.67 ,2743.33 ,2800.,2800.,2800.]

Example 3

Assume x is a 4-dimensional array with dimensions "time", "lev", "lat", "lon", and sizes ntim x klev nlat x mlon. Assume that at random times all data are missing. Linearly interpolate in time to fill in the values. Use a temporary array for the interpolated values, then copy the interpolated values back to the original array.

end_pts_msg is set to 0 so that missing values at the beginning and end will be returned as missing. max_msg is set to 5 meaning that the routine will not interpolate segments of the series where more than 5 consecutive values are encountered:

end_pts_msg = 0
max_msg     = 5 

y = Ngl.linmsg(x, end_pts_msg,max_msg,fill_value)