\cancel mode verify
!Description: Demonstration of interpolating scattered data to grids  4/99

! External Functions introduced in Ferret V5.0
! Two interpolation techniques are available for gridding

! Note that both techniques are applicable ONLY for output
! grids which are REGULAR, that is to say, have EQUALLY-SPACED
! coordinates.

set grid/save

if ($program_name"0|PyFerret>1|*>0") THEN set text/font=arial


! Define viewports
DEFINE VIEW/ylim=.5,1/text=.5 vupper
DEFINE VIEW/xlim=0.,.5/ylim=.3,.63/text=.2 vul
DEFINE VIEW/xlim=.5,1./ylim=.3,.63/text=.2 vur
DEFINE VIEW/xlim=0.,.5/ylim=0.0,.33/text=.2 vll
DEFINE VIEW/xlim=.5,1./ylim=0.0,.33/text=.2 vlr

! Define a 2-dimensional function and sample it
DEFINE AXIS/x=0:10:0.05 x10
DEFINE AXIS/y=0:10:0.05 y10
DEFINE GRID/x=x10/y=y10 g10x10
SET GRID g10x10

LET/QUIET WAVE = SIN(KX*XPTS + KY*YPTS - PHASE) / 3
LET/QUIET PHASE = 0
LET/QUIET KAPPA = 0.4
LET/QUIET KX = 0.4
LET/QUIET KY = 0.7

LET/QUIET FCN1 = SIN(R)/(R+1)
LET/QUIET R = ((XPTS-X0)^2+ 5*(YPTS-Y0)^2)^0.5
LET/QUIET X0 = 3
LET/QUIET Y0 = 8

LET/QUIET sample_function = fcn1 + wave

say ...
say For simplicity this demo will work with abstract mathematical functions
say rather than with "real" data.  We will create an abstract function in
say the XY plane and use it as a model of "reality".  Our goal is to
say sample this "reality" at scattered (X,Y) points and then attempt to
say recreate the original field through interpolation and objectibe analysis
say techniques. 
say ...
say From here on the lines you see will be exactly the lines executed in this
say demonstration script.  To focus attention on the issues of regridding
say and avoid the clutter of graphics layout commands you will notice that
say graphics are performed by a script called "draw_it".
say ...
MESSAGE
set mode verify

! **********************************************************
! To display "reality" we will first let our sample points (xpts,ypts) be
! simply the X and y points of a grid.  Then we will change (xpts,ypts) to
! be 200 sampling locations and mark them with symbols on the plot.
! We will draw this output in the "upper" viewport. 

! Shaded plot of the points on the (X,Y) grid
LET/QUIET xpts = x; LET/QUIET ypts = y
set view vupper
fill/nolab/lev=20 fcn1+wave
annotate/norm/xpos=0.5/ypos=1.2/halign=0  "A field with 200 sample points marked"


! Store the color-levels used here; use the same levels in all the 
! plots of gridded fields below.
define symbol same_levels = /lev=(($lev_min),($lev_max),($lev_del))

! points randomly sampled in (X,Y)
LET/QUIET xpts = 10*randu(i); LET/QUIET ypts = 10*randu(i+2)
set region/i=1:200
plot/vs/over/symbols xpts,ypts
cancel region
MESSAGE

! **********************************************************
! Now we will interpolate those 200 (X,Y,value) triples back onto a regular
! grid in the (X,Y) plane.  The output grid will be from 1 to 10 by .5 along
! both the X and y axes.  Defaults will be used for all other interpolation
! controls; CAY = 5.0, NRNG = 5
! Under the "SHADEd" (raster-style) plot we will display the very same result
! as a filled contour plot.

DEFINE AXIS/x=1:10:.5 xax5
DEFINE AXIS/y=1:10:.5 yax5
LET/QUIET sgrid = scat2gridlaplace_xy (xpts, ypts, sample_function, x[gx=xax5], y[gy=yax5], 5., 5)

set v vul
shade($same_levels)/nolab sgrid
annotate/norm/xpos=0.5/ypos=-0.3/halign=0 "Laplace gridding to 20x20 grid"

set v vll
fill($same_levels)/nolab sgrid
annotate/norm/xpos=0.5/ypos=-0.3/halign=0 "20x20 grid, fill contoured"

MESSAGE

! **********************************************************
! Now we perform the identical analysis but we will instead interpolate onto
! a higher resolution grid.  The gaps in the output are because there are
! points on this output grid that are unacceptably far from any sample points
! using the default interpolation parameters.

DEFINE AXIS/x=1:10:.2 xax2
DEFINE AXIS/y=1:10:.2 yax2
LET/QUIET sgrid = scat2gridlaplace_xy (xpts, ypts, sample_function, x[gx=xax2], y[gy=yax2], 5., 5)
set v vur
shade($same_levels)/nolab sgrid
annotate/norm/xpos=0.5/ypos=-0.3/halign=0 "Laplace gridding to 50x50 grid"

set v vlr
fill($same_levels)/nolab sgrid
annotate/norm/xpos=0.5/ypos=-0.3/halign=0 "50x50 grid, fill contoured"

MESSAGE

! Now do the same plots with the Gaussian gridding function 
! scat2gridgauss_xy which uses a different technique to interpolate the 
! scattered points to the grid. This results in a somewhat smoother plot. 
! however with these parameters, the result does not capture the extreme 
! values. Using the same color scale on all plots makes this clear. 
! The interpolation parameters are  XSCALE, YSCALE, XCUTOFF, YCUTOFF;
! where the algorithm uses scattered points which are within 
!         cutoff* the scale width from the gridbox center.  We use 
! XSCALE = YSCALE = XCUTOFF = YCUTOFF = 2.

set window/new
! Shaded plot of the points on the (X,Y) grid
LET/QUIET xpts = x; LET/QUIET ypts = y
set view vupper
fill/nolab/lev=20 fcn1+wave
annotate/norm/xpos=0.5/ypos=1.2/halign=0  "A field with 200 sample points marked"

! points randomly sampled in (X,Y)
LET/QUIET xpts = 10*randu(i); LET/QUIET ypts = 10*randu(i+2)
set region/i=1:200
plot/vs/over/symbols xpts,ypts
cancel region

DEFINE AXIS/x=1:10:.5 xax5
DEFINE AXIS/y=1:10:.5 yax5
LET/QUIET sgrid = scat2gridgauss_xy (xpts, ypts, sample_function, x[gx=xax5], y[gy=yax5], 2.,2.,2.,2)

set v vul
shade($same_levels)/nolab sgrid
annotate/norm/xpos=0.5/ypos=-0.3/halign=0 "Gauss gridding to 20x20 grid"

set v vll
fill($same_levels)/nolab sgrid
annotate/norm/xpos=0.5/ypos=-0.3/halign=0 "20x20 grid, fill contoured"

! Finer grid
DEFINE AXIS/x=1:10:.2 xax2
DEFINE AXIS/y=1:10:.2 yax2
LET/QUIET sgrid = scat2gridgauss_xy (xpts, ypts, sample_function, x[gx=xax2], y[gy=yax2], 1.,1.,2.,2)
set view vur
shade($same_levels)/nolab sgrid
annotate/norm/xpos=0.5/ypos=-0.3/halign=0 "Gauss gridding to 50x50 grid"
set view vlr
fill($same_levels)/nolab sgrid
annotate/norm/xpos=0.5/ypos=-0.3/halign=0 "50x50 grid, fill contoured"


MESSAGE
set grid/restore
exit

