Previous IDL Reference Guide: Procedures and Functions Next

TRIGRID

Syntax | Return Value | Arguments | Keywords | Examples | Version History | See Also

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.

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 x0 and ends at x1,then the horizontal spacing is

(x1- x0)/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:

(x1- x0)/(NX-1) and (y1- y0)/(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 [x0, y0, x1, y1] that specifies the data range to be gridded (x0 and y0 are the lower X and Y data limits, and x1 and y1 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, MISSING, and QUINTIC.


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

Table 3-122: Keyword Interrelationships for the TRIGRID function 

Table 3-122: Keyword Interrelationships for the TRIGRID function 
INPUT
EXTRAPOLATE
MISSING
Not in
Triangles
Beyond MIN_VALUE,
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

Examples

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.

PRO TrigridExample  
  
; 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:  
PLOT, x, y, psym=1  
in=' '  
READ,"Press enter",in  
  
; Obtain triangulation:  
TRIANGULATE, x, y, tr, b  
  
; Show the triangles:  
FOR i=0, N_ELEMENTS(tr)/3-1 DO BEGIN & $  
   ; Subscripts of vertices [0,1,2,0]:  
   t = [tr[*,i], tr[0,i]] & $  
   ; Connect triangles:  
   PLOTS, x[t], y[t] & $  
ENDFOR  
  
; Show linear surface:  
SURFACE, TRIGRID(x, y, z, tr)  
in=' '  
READ,"Press enter",in  
  
; Show smooth quintic surface:  
SURFACE, TRIGRID(x, y, z, tr, /QUINTIC)  
in=' '  
READ,"Press enter",in  
  
; Show smooth extrapolated surface:  
SURFACE, TRIGRID(x, y, z, tr, EXTRA = b)  
in=' '  
READ,"Press enter",in  
  
; Output grid size is 12 x 24:  
SURFACE, TRIGRID(X, Y, Z, Tr, NX=12, NY=24)  
in=' '  
READ,"Press enter",in  
  
; 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, [.1, .1], NX=20)  
in=' '  
READ,"Press enter",in  
  
; 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:  
SURFACE, TRIGRID(X, Y, Z, Tr, [0,0], [0,0,5,4],NX=20, NY=40)  
in=' '  
READ,"Press enter",in  
  
WDELETE  
  
END  

Example 2

This example shows how to perform spherical gridding:

PRO SphericalGrid  
; 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  
END  

Example 3

This example demonstrates the use of the INPUT keyword:

PRO TrigridInputKeyword  
  
; 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:  
PLOT, x, y, psym=1  
  
; Obtain triangulation:  
TRIANGULATE, x, y, tr, b  
  
;Show the triangles.  
FOR i=0, N_ELEMENTS(tr)/3-1 DO BEGIN $  
   ; Subscripts of vertices [0,1,2,0]:  
   t = [tr[*,i], tr[0,i]] & $  
   ; Connect triangles:  
   PLOTS, x[t], y[t]  
ENDFOR  
  
; The default size for the return value of trigrid. xtemp should be   
; the same type as Z. xtemp provides temporary space for trigrid:  
xtemp=FLTARR(51,51)  
xtemp = TRIGRID(x, y, z, INPUT = xtemp, tr)  
  
; Show linear surface:  
SURFACE, xtemp, TITLE='Linear surface', CHARSIZE=2  
in=' '  
READ,"Press enter",in  
xtemp = TRIGRID(x, y, z, tr, INPUT = xtemp, /QUINTIC)  
  
; Show smooth quintic surface:  
SURFACE, xtemp, TITLE='Smooth Quintic surface', CHARSIZE=2  
in=' '  
READ,"Press enter",in  
xtemp = TRIGRID(x, y, z, tr, INPUT = xtemp, EXTRA = b)  
  
; Show smooth extrapolated surface:  
SURFACE, xtemp, TITLE='Smooth Extrapolated surface', CHARSIZE=2  
in=' '  
READ,"Press enter",in  
END  

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.

PRO GriddingIrregularIntervals  
  
; Make 100 normal x, y points:  
x = RANDOMN(seed, 100)  
y = RANDOMN(seed, 100)  
PRINT, MIN(x), MAX(x)  
PRINT, MIN(y), MAX(y)  
  
; Make a Gaussian surface:  
z = EXP(-(x^2 + y^2))  
  
; Obtain triangulation:  
TRIANGULATE, x, y, triangles, boundary  
  
; Create random x values.  These values will be used to  
; form the x locations of the resulting grid.  
gridX = RANDOMN(seed, 30)  
; Sort x values. Sorted values are required for the XOUT  
; keyword.  
sortX = UNIQ(gridX, SORT(gridX))  
gridX = gridX[sortX]  
; Output sorted x values to be used with the XOUT  
; keyword.  
PRINT, 'gridX:'  
PRINT, gridX  
  
; Create random y values.  These values will be used to  
; form the y locations of the resulting grid.  
gridY = RANDOMN(seed, 30)  
; Sort y values. Sorted values are required for the YOUT  
; keyword.  
sortY = UNIQ(gridY, SORT(gridY))  
gridY = gridY[sortY]  
; Output sorted y values to be used with the YOUT  
; keyword.  
PRINT, 'gridY:'  
PRINT, gridY  
  
; Derive grid of initial values.  The location of the  
; resulting grid points are the inputs to the XOUT and  
; YOUT keywords.  
grid = TRIGRID(x, y, z, triangles, XOUT = gridX, $  
   YOUT = gridY, EXTRAPOLATE = boundary)  
  
; Display resulting 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  
  
END  

Version History

Pre-4.0
Introduced

See Also

SPH_SCAT, TRIANGULATE

  IDL Online Help (June 16, 2005)