! bn500_gc_functions.jnl

! 10/97 - first version - test GC functions released in V4.9 
! 3/98 - added single test of a dynamic axis passed to GC fcn
! Note: OFFSET VALUES NOT YET TESTED

! 2/99 "UNRAVEL" renamed to XSEQUENCE
! 1/00 changes to RESHAPE tests ==> "K=1" removed

CANCEL REGION
CANCEL VIEWPORTS

! XSEQUENCE function
LET v = X[x=1:4:1] + Y[Y=.1:.4:.1]
LIST v
LIST SIN(v)	! preserves source grid
LIST XSEQUENCE(v)	! replaces source grid
STAT v, XSEQUENCE(v)

! check regions specified on source and on destination
LIST/I=3:5 XSEQUENCE(v)	! destination region

LIST v[I=2:3,J=2:3]
LIST XSEQUENCE( v[I=2:3,J=2:3] )
LIST/I=2:3 XSEQUENCE( v[I=2:3,J=2:3] )

! check grid interactions where XSEQUENCE is inside of expression
LET a = XSEQUENCE( v[I=2:3,J=2:3] ) + Z[Z=.01:.02:.01]
LIST a

! RESHAPE function
DEFINE AXIS/X=1:4:1 x4
DEFINE AXIS/Y=.1:.4:.1 y4
DEFINE AXIS/Z=.01:.04:.01 z4
DEFINE AXIS/T=.001:.004:.001 T4
DEFINE GRID/X=x4/Y=y4/Z=z4/T=t4 g4d
LET v4d = X[x=1:3:1] + Y[Y=.1:.3:.1] + Z[Z=.01:.03:.01] + T[T=.001:.003:.001]
LIST v4d
LET V3d =  X[x=1:3:1] + Y[Y=.1:.3:.1] + Z[Z=.01:.09:.01]	! same size
LIST v3d
LIST RESHAPE(v4d, v3d)
! if source and dest grid share an axis then the region is passed to the arg
LIST RESHAPE(v4d[i=2:3,j=2:3], v3d)		! I,J preserved
LIST/k=2:3 RESHAPE(v4d[i=2:3,j=2:3,k=2:3], v3d)	! K,L not preserved
LIST/I=2:3/J=2:3  RESHAPE(v4d, v3d)
LIST/I=2:3/J=2:3/K=2:3  RESHAPE(v4d, v3d)	! K refers to result, only
LIST/I=2:3/J=2:3  RESHAPE(v4d[k=1:3], v3d[k=2:5])	! output to a subregion in K
LIST/I=2:3/J=2:3/K=4:5  RESHAPE(v4d[k=1:3], v3d[k=2:5])	! "conflicting" K specs

! ZAXREPLACE function
! definitions taken from sigma_coordinate_demo.jnl 9/96 *sh*
! 1) CREATE AN ARTIFICIAL SIGMA COORDINATE MODEL OUTPUT
define axis/x=-50:50:10/unit=km xchannel
define axis/y=-30:30:10/unit=km yrise
define axis/z=1:10:2/unit=layer/depth zlayer
define axis/T=1:20:1/unit=hours time
define grid/x=xchannel/y=yrise/z=zlayer/t=time gsigma

! bathymetry: a channel with a rise along the axis of the channel
let pi = 3.14159
let nominal_depth = 100
let cross_channel_size = nominal_depth * (1 + COS(X[g=gsigma]/60*pi))
let xchannel = -1 * cross_channel_size
let rise_shape = (1 + COS(Y[g=gsigma]/40*pi))/6
let bathymetry = xchannel + rise_shape*CROSS_CHANNEL_SIZE
set variable/title="Channel Bathymetry"/unit=meters bathymetry

! sigma layer thickness: varies in X, Y, Z, and T in this example
let time_evolve = 0 + L[g=gsigma]/100
let h0 = EXP(time_evolve*K[g=gsigma])
let h_normalized = h0/h0[k=1:10@sum]
let h = h_normalized * (-1 * bathymetry)
set variable/title="layer thickness"/unit=meters h

! fictitious flow field:
let flow_profile = LOG((11-K[g=gsigma]))
let time_ramp = 1 + L[g=gsigma]/20 + 0.2*SIN((L[g=gsigma]-1)/2)
let flow = time_ramp * flow_profile * cross_channel_size / (1-rise_shape)
set variable/title="non-physical flow field" flow

! ----------------------
! "DEPTH" --  THE VERTICAL INTEGRAL OF LAYER THICKNESS
let depth = h[k=@rsum]-h/2
set variable/title="DEPTH function"/unit=meters depth

! Desired result axes - depth in meters
define axis/depth/z=0:200:50/units=meters z50m
define axis/depth/z=0:200:20/units=meters z20m
define axis/depth/z=0:200:2/units=meters z2m

LIST/x=0/l=1 flow
LIST/x=0/l=1 depth
set view ul
shade/x=0/l=1/lev=30 flow
!LIST/x=0/l=1 ZAXREPLACE(flow,depth,z[gz=z50m])
!LIST/x=0/l=1 ZAXREPLACE(flow,depth,z[gz=z20m])
LIST/x=0/l=1 ZAXREPLACE(flow[z=0:200],depth[z=0:200],z[gz=z50m])
LIST/x=0/l=1 ZAXREPLACE(flow[z=0:200],depth[z=0:200],z[gz=z20m])

set view ur
shade/x=0/l=1/lev ZAXREPLACE(flow[z=0:200],depth[z=0:200],z[gz=z20m])

! how do the layers get distributed?
!LET Kflow  = ZAXREPLACE(flow,depth,z[gz=z20m])
!LET Kdepth = ZAXREPLACE(k[g=gsigma],depth,z[gz=z20m])
LET Kflow  = ZAXREPLACE(flow[z=0:200],depth[z=0:200],z[gz=z20m])
LET Kdepth = ZAXREPLACE(k[z=0:200,g=gsigma],depth[z=0:200],z[gz=z20m])
LIST/x=0/l=1 Kdepth
LIST/x=0/l=1 Kdepth[k=@max]

! mark deepest layer with a zero
LET K0 = Kdepth - Kdepth[K=@max]
LET Kkernel = K0[K=@WEQ] * Kflow

! compare flow at bottom
LET Z10_flow = flow[Z=10]
LIST/x=0/l=1 Z10_flow		! bottom layer in layered version

LET bottom_flow = Kkernel[z=0:200@sum]
!message LIST/x=0/l=1 Kkernel[z=0:200]	! crash!!!
!message LIST/x=0/l=1 bottom_flow	! deepest flow in depth version
!message

! error using 20 meter depth resolution
LIST/x=0/l=1 Z10_flow - bottom_flow

! reduced error using 2 meter depth resolution
set view ll
shade/x=0/l=1/lev ZAXREPLACE(flow[z=0:200],depth[z=0:200],z[gz=z2m])
LET Kflow  = ZAXREPLACE(flow[z=0:200],depth[z=0:200],z[gz=z2m])
LET Kdepth = ZAXREPLACE(k[g=gsigma,z=0:200],depth[z=0:200],z[gz=z2m])
LIST/x=0/l=1 Z10_flow - bottom_flow

! dynamic axis passes to GC function -- 1 meter resolution (3/98)
set view lr
shade/x=0/l=1/ylim=0:200:-20/lev ZAXREPLACE(flow[z=0:200],depth[z=0:200],z[z=0:200:.5])
