For non-spherical data, given data points defined by the parameters *X, Y,* and *Z* and a triangulation of the planar set of points determined by *X* and *Y*, the TRIGRID function returns a regular grid of interpolated *Z* values. Linear or smooth quintic polynomial interpolation can be selected. Extrapolation for gridpoints outside of the triangulation area is also an option. An input triangulation can be constructed using the procedure TRIANGULATE. TRIGRID uses double precision for all computations.

For spherical data, given the data values *F* and the spherical triangulation, the TRIGRID function returns a regular grid of interpolated *F* values using quadratic polynomial interpolation.

## Examples

See Additional Examples for more information on using TRIGRID.

### Example 1

This example creates and displays a 50 point random normal distribution. The random points are then triangulated, with the triangulation displayed. Next, the interpolated surface is computed and displayed using linear and quintic interpolation. Finally, the smooth extrapolated surface is generated and shown.

; Make 50 normal x, y points:

x = RANDOMN(seed, 50)

y = RANDOMN(seed, 50)

; Make the Gaussian:

z = EXP(-(x^2 + y^2))

; Show points:

IPLOT, x, y, SYM_INDEX=1, LINESTYLE=6, $

VIEW_TITLE='Random Points', VIEW_GRID=[3,2], $

DIMENSIONS=[1000,800]

; Obtain triangulation:

TRIANGULATE, x, y, tr, b

; Show linear surface:

ISURFACE, TRIGRID(x, y, z, tr), VIEW_TITLE='Linear Surface', $

STYLE=1, /VIEW_NEXT

; Show smooth quintic surface:

ISURFACE, TRIGRID(x, y, z, tr, /QUINTIC), $

VIEW_TITLE='Quintic Surface', STYLE=1, /VIEW_NEXT

; Show smooth extrapolated surface:

ISURFACE, TRIGRID(x, y, z, tr, EXTRA = b), $

VIEW_TITLE='Extrapolated Surface', STYLE=1, /VIEW_NEXT

; Set output grid size to 12 x 24:

ISURFACE, TRIGRID(X, Y, Z, Tr, NX=12, NY=24), $

VIEW_TITLE='12x24 Grid', STYLE=1, /VIEW_NEXT

ISETPROPERTY, 'text*', FONT_SIZE=36

## Syntax

*Result* = TRIGRID( *X*, *Y*, *Z*, *Triangles* [, *GS*, *Limits*] )

For spherical gridding:

*Result* = TRIGRID( *F* , *GS*, *Limits*, SPHERE=*S *)

**Keywords:** [, /DEGREES] [, EXTRAPOLATE=*array*] [, INPUT=*variable*] [, MAX_VALUE=*value*] [, MIN_VALUE=*value*] [, MISSING=*value*] [, NX=*value*] [, NY=*value*] [, /QUINTIC] [, SPHERE=*variable*] [, XGRID=*variable*] [, XOUT=*vector*] [, YGRID=*variable*] [, YOUT=*vector*]

## Return Value

If any of *X*, *Y*, or *Z* are double precision, *Result* is double precision; otherwise, *Result* is single precision. If SPHERE is present, *Result* is always double precision.

## Arguments

### X, Y, Z

Input arrays of *X*, *Y*, and *Z* coordinates of data points. Integer, long, double-precision and floating-point values are allowed. In addition, *Z* can be a complex array. All three arrays must have the same number of elements.

### F

When performing a spherical gridding, this argument should be the named variable that contains the rearranged sample values that were returned by TRIANGULATE’s FVALUE keyword.

### Triangles

A longword array of the form output by TRIANGULATE. That is, *Triangles* has the dimensions (3, *number of triangles*) and, for each *i*, Triangles[0,i], Triangles[1,i], and Triangles[2,i] are the indices of the vertices of the *i-*th triangle.

### GS

If present, *GS* should be a two-element vector [*XS, YS*], where *XS* is the horizontal spacing between grid points and *YS* is the vertical spacing. The default is based on the extents of *X* and *Y*. If the grid starts at *X* value *x*_{0} and ends at *x*_{1},then the horizontal spacing is

(*x*_{1}- *x*_{0})/50

The default for *YS* is computed in the same way. Since the default grid spacing divides each axis into 50 intervals and produces 51 samples, TRIGRID returns a grid with dimensions (51, 51).

If the NX or NY keywords are set to specify the output grid dimensions, either or both of the values of *GS* may be set to 0. In this case, the grid spacing is computed as the respective range divided by the dimension minus one:

(*x*_{1}- *x*_{0})/(NX-1) and (*y*_{1}- *y*_{0})/(NY-1)

For spherical gridding, *GS* is assumed to be specified in radians, unless the DEGREES keyword is set.

### Limits

If present, *Limits* should be a four-element vector [*x*_{0}*, y*_{0}*, x*_{1}*, y*_{1}] that specifies the data range to be gridded (*x*_{0} and *y*_{0} are the lower X and Y data limits, and *x*_{1} and *y*_{1} are the upper limits). The default for *Limits* is:

[MIN(X), MIN(Y), MAX(X), MAX(Y)]

If the NX or NY keywords are not specified, the size of the grid produced is specified by the value of *Limits*. If the NX or NY keywords are set to specify the output grid dimensions, a grid of the specified size will be used regardless of the value of *Limits*.

## Keywords

### DEGREES

For a spherical gridding, set this keyword to indicate that the grid spacing (the *GS* argument) is specified in degrees rather than radians.

### EXTRAPOLATE

Set this keyword equal to an array of boundary node indices (as returned by the optional parameter B of the TRIANGULATE procedure) to extrapolate to grid points outside the triangulation. The extrapolation is not smooth, but should give acceptable results in most cases.

Setting this keyword sets the quintic interpolation mode, as if the QUINTIC keyword has been specified.

This keyword is ignored for spherical gridding.

### INPUT

Set this keyword to a named variable (which must be an array of the appropriate size to hold the output from TRIGRID) in which the results of the gridding are returned. This keyword is provided to make it easy and memory-efficient to perform multiple calls to TRIGRID. The interpolates within each triangle overwrite the array and the array is not initialized.

### MAX_VALUE

Set this keyword to a value that represents the maximum *Z* value to be gridded. Data larger than this value are treated as missing data and are not gridded.

This keyword is ignored for spherical gridding.

### MIN_VALUE

Set this keyword to a value that represents the minimum *Z* value to be gridded. Data smaller than this value are treated as missing data and are not gridded.

This keyword is ignored for spherical gridding.

### MISSING

The *Z* value to be used for grid points that lie outside the triangles in *Triangles*. The default is 0. This keyword also applies to data points outside the range specified by MIN_VALUE and MAX_VALUE.

*Note: *Letting MISSING default to 0 does not always produce the same result as explicitly setting it to 0. For example, if you specify INPUT and not EXTRAPOLATE, letting MISSING default to 0 will result in the INPUT values being used for data outside the Triangles; explicitly setting MISSSING to 0 will result in 0 being used for the data outside the Triangles.

### NX

The output grid size in the *x* direction. The default value is 51.

### NY

The output grid size in the *y *direction. The default value is 51.

### QUINTIC

If QUINTIC is set, smooth interpolation is performed using Akima’s quintic polynomials from “A Method of Bivariate Interpolation and Smooth Surface Fitting for Irregularly Distributed Data Points” in *ACM Transactions on Mathematical Software*, 4, 148-159. For non-spherical data, the default method is linear interpolation. For spherical data, the default method is quadratic interpolation, and the QUINTIC keyword is ignored.

Derivatives are estimated by Renka’s global method in “A Triangle-Based C1 Interpolation Method” in *Rocky Mountain Journal of Mathematics*, vol. 14, no. 1, 1984.

QUINTIC is not available for complex data values. Setting the EXTRAPOLATE keyword implies the use of quintic interpolation; it is not necessary to specify both.

### SPHERE

For a spherical gridding, set this keyword to the named variable that contains the results of the spherical triangulation returned by TRIANGULATE’s SPHERE keyword.

### XGRID

Set this keyword to a named variable that will contain a vector of *X* values for the output grid.

### XOUT

Set this keyword to a vector specifying the output grid *X* values. If this keyword is supplied, the *GS* and *Limits* arguments are ignored. Use this keyword to specify irregularly spaced rectangular output grids. If XOUT is specified, YOUT must also be specified. If keyword NX is also supplied then only the first NX points of XOUT will be used.

*Note: *The XOUT keyword cannot be used with the SPHERE keyword.

### YGRID

Set this keyword to a named variable that will contain a vector of *Y* values for the output grid.

### YOUT

Set this keyword to a vector specifying the output grid *Y* values. If this keyword is supplied, the *GS* and *Limits* arguments are ignored. Use this keyword to specify irregularly spaced rectangular output grids. If keyword NY is also supplied then only the first NY points of YOUT will be used.

The following table shows the interrelationships between the keywords EXATRAPOLATE, INPUT, MAX_VALUE, MIN_VALUE, and MISSING.

*Note: *The YOUT keyword cannot be used with the SPHERE keyword.

INPUT |
EXTRAPOLATE |
MISSING |
Not in |
Beyond MIN_VALUE, |

no |
no |
no |
uses 0 |
uses 0 |

no |
no |
yes |
uses MISSING |
uses MISSING |

no |
yes |
no |
EXTRAPOLATEs |
uses 0 |

no |
yes |
yes |
EXTRAPOLATEs |
uses MISSING |

yes |
no |
no |
uses INPUT |
uses INPUT |

yes |
no |
yes |
uses MISSING |
uses MISSING |

yes |
yes |
no |
EXTRAPOLATEs |
uses INPUT |

yes |
yes |
yes |
EXTRAPOLATEs |
uses MISSING |

## Additional Examples

### Example 2

This example shows how to perform spherical gridding:

; Create some random longitude points:

lon = RANDOMU(seed, 50) * 360. - 180.

; Create some random latitude points:

lat = RANDOMU(seed, 50) * 180. - 90.

; Make a fake function value to be passed to FVALUE. The system

; variable !DTOR contains the conversion value for degrees to

; radians.

f = SIN(lon * !DTOR)^2 * COS(lat * !DTOR)

; Perform a spherical triangulation:

TRIANGULATE, lon, lat, tr, $

SPHERE=s, FVALUE=f, /DEGREES

; Perform a spherical triangulation using the values returned from

; TRIANGULATE. The result, r, is a 180 by 91 element array:

r=TRIGRID(f, SPHERE=s, [2.,2.],$

[-180.,-90.,178.,90.], /DEGREES)

; Display the surface

SURFACE, r

### Example 3

This example demonstrates the use of the INPUT keyword:

; Make 50 normal x, y points:

x = RANDOMN(seed, 50)

y = RANDOMN(seed, 50)

; Make the Gaussian:

z = EXP(-(x^2 + y^2))

; Show points:

IPLOT, x, y, SYM_INDEX=1, LINESTYLE=6, $

VIEW_TITLE='Random Points', VIEW_GRID=[2,2], $

DIMENSIONS=[800,600]

; Obtain triangulation:

TRIANGULATE, x, y, tr, b

; xtemp provides temporary space for trigrid and should be

; the same type as Z.

xtemp=FLTARR(51,51)

; Show linear surface:

xtemp = TRIGRID(x, y, z, INPUT = xtemp, tr)

ISURFACE, xtemp, VIEW_TITLE='Linear surface', STYLE=1, /VIEW_NEXT

; Show smooth quintic surface:

xtemp = TRIGRID(x, y, z, tr, INPUT = xtemp, /QUINTIC)

ISURFACE, xtemp, VIEW_TITLE='Smooth Quintic surface', $

STYLE=1, /VIEW_NEXT

; Show smooth extrapolated surface:

xtemp = TRIGRID(x, y, z, tr, INPUT = xtemp, EXTRA = b)

ISURFACE, xtemp, VIEW_TITLE='Smooth Extrapolated surface', $

STYLE=1, /VIEW_NEXT

ISETPROPERTY, 'text*', FONT_SIZE=36

### Example 4

The XOUT and YOUT keywords allow you to obtain an irregular interval from the TRIGRID routine. This example creates an irregularly-gridded dataset of a Gaussian surface. A grid is formed from these points with the TRIANGULATE and TRIGRID routines. The inputs to the XOUT and YOUT keywords are determined at random to produce an irregular interval. These inputs are sorted before setting them to XOUT and YOUT because these keywords require monotonically ascending or descending values. The lines of the resulting surface are spaced at the irregular intervals provided by the settings of the XOUT and YOUT keywords.

; Make 100 normal x, y points:

x = RANDOMN(seed, 100)

y = RANDOMN(seed, 100)

; Make a Gaussian surface:

z = EXP(-(x^2 + y^2))

; Obtain triangulation:

TRIANGULATE, x, y, triangles, boundary

; Create random x and y values for the grid.

gridX = RANDOMN(seed, 30)

gridY = RANDOMN(seed, 30)

; Sorted values are required for the XOUT and YOUT keywords.

sortX = UNIQ(gridX, SORT(gridX))

gridX = gridX[sortX]

sortY = UNIQ(gridY, SORT(gridY))

gridY = gridY[sortY]

; Derive the grid.

grid = TRIGRID(x, y, z, triangles, XOUT = gridX, $

YOUT = gridY, EXTRAPOLATE = boundary)

; Display the grid. The grid lines are not

; at regular intervals because of the randomness of the

; inputs to the XOUT and YOUT keywords.

SURFACE, grid, gridX, gridY, /XSTYLE, /YSTYLE

## Version History

Pre-4.0 |
Introduced |