## TRIGRID

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. The resulting grid is a two-dimensional array of the same data type as Z , with user-specified bounds and spacing. An input triangulation can be constructed using the procedure TRIANGULATE. Together, the TRIANGULATE procedure and the TRIGRID function constitute IDL's solution to the problem of irregularly-gridded data, including spherical gridding.

### Calling Sequence

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

or, for spherical gridding:

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

### 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.

#### 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.

#### 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.

#### 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 Traingles; 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. The default method is linear interpolation.

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 equal to a named variable that will contain a vector of X values for the output grid.

#### YGRID

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

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

• Keyword Interrelationships for the TRIGRID function

INPUT

EXTRAPOLATE

MISSING

Not in Triangles

Beyond (MIN,MAX)_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

### Example

The first 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.

x = RANDOMN(seed, 50) ; Make 50 normal x, y points.

y = RANDOMN(seed, 50)

z = EXP(-(x^2 + y^2)) ; Make the Gaussian.

PLOT, x, y, psym=1 ; Show points.

TRIANGULATE, x, y, tr, b ; Obtain triangulation.

FOR i=0, N_ELEMENTS(tr)/3-1 DO BEGIN & \$ ; Show the triangles.

t = [tr[*,i], tr[0,i]] & \$ ; Subscripts of vertices [0,1,2,0].

PLOTS, x[t], y[t] & \$ ; Connect triangles.

ENDFOR

SURFACE, TRIGRID(x, y, z, tr) ; Show linear surface.

SURFACE, TRIGRID(x, y, z, tr, /QUINTIC) ; Show smooth quintic surface.

SURFACE, TRIGRID(x, y, z, tr, EXTRA = b) ; Show smooth extrapolated surface.

SURFACE, TRIGRID(X, Y, Z, Tr, NX=12, NY=24)
; Output grid size is 12 x 24.

SURFACE, TRIGRID(X, Y, Z, Tr, \$

[.1, .1], NX=20) ; Output grid size is 20 x 11. The X grid is [0, .1, .2, ..., 19 * .1 = 1.9]. The Y grid goes from 0 to 1.

SURFACE, TRIGRID(X, Y, Z, Tr, \$

[0,0], [0,0,5,4],NX=20, NY=40) ; Output size is 20 x 40. The range of the grid in X and Y is specified by the Limits parameter. Grid spacing in X is [5-0]/(20-1) = 0.263. Grid spacing in Y is (4-0)/(40-1) = 0.128.

The next example shows how to perform spherical gridding:

lon = RANDOMU(seed, 50) * 360. - 180. ; Create some random longitude points.

lat = RANDOMU(seed, 50) * 180. - 90. ; Create some random latitude points.

f = SIN(lon * !DTOR)^2 * \$

COS(lat * !DTOR) ; Make a fake function value to be passed to FVALUE. The system variable !DTOR contains the conversion value for degrees to radians.

TRIANGULATE, lon, lat, tr, \$

SPHERE=s, FVALUE=f, /DEGREES ; Perform a spherical triangulation.

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

[-180.,-90.,178.,90.], /DEGREES) ; Perform a spherical triangulation using the values returned from TRIANGULATE. The result, r, is a 180 by 91 element array.

The next example shows the use of the INPUT keyword:

x = RANDOMN(seed, 50) ; Make 50 normal x, y points.

y = RANDOMN(seed, 50)

z = EXP(-(x^2 + y^2)) ; Make the Gaussian.

PLOT, x, y, psym=1 ; Show points.

TRIANGULATE, x, y, tr, b ; Obtain triangulation.

FOR i=0, N_ELEMENTS(tr)/3-1 DO BEGIN \$ ; Show the triangles.

t = [tr[*,i], tr[0,i]] & \$ ; Subscripts of vertices [0,1,2,0].

PLOTS, x[t], y[t] ; Connect triangles .

ENDFOR

xtemp=fltarr(51,51) ; The default size for the return value of trigrid. xtemp should be the same type as Z. xtemp provides temporary space for trigrid.

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

surface, xtemp ; Show linear surface.

in=' '

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

surface,xtemp; Show smooth quintic surface.

in=' '

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

surface,xtemp ; Show smooth extrapolated surface.

in=' '