! bn_regrid_to_user
! 8/99 *sh*
! 5/01 *sh* - changes to accomodate truncated levitus_climatology.cdf
!           - additions to test use of expressions in SET/SHOW/DEFINE grid

! test various regridding combos that involve user-defined variables
! for the target grids

use coads_climatology
use levitus_climatology

! define test objects
let a = sst[d=coads_climatology] + 1
let b = sst + 1		! data set unspecified
DEFINE AXIS/Y=20s:20n:.5/units=degrees yax
DEFINE GRID/like=sst[d=coads_climatology]/y=yax gg

! reference data
list/x=158e:161e/y=6s:4s temp[d=2,k=1]

! basic regrid (implicit grid -- sst and temp geometries are mismatched)
list/x=160e/y=5s temp[d=2,k=1,g=a[d=1]]
show grid

! per axis regrid
! 5/01 note: changed from y=0 yo y=5s to accomodate Y-truncated
! levitus_climatology.cdf
list/x=160e/y=5s temp[d=2,k=1,gx=a[d=1],gy=yax]
list/x=160e/y=5s temp[d=2,k=1,gy=yax,gx=a[d=1]]
show grid

! combined full grid and per-axis
list/x=160e/y=5s temp[d=2,k=1,g=gg,gx=a[d=1]]
list/x=160e/y=5s temp[d=2,k=1,gx=a[d=1],g=gg]
show grid

! pseudo-variable access
list/i=1:4 i[g=a]		! a is previously loaded
let c = a
list/i=1:4 i[g=c]		! c is not previously loaded
let d = c
list/i=1:4 x[gx=d]

! DEFINE GRID based on a user-defined variable
let e = d
DEFINE GRID/LIKE=e/x=temp[d=levitus_climatology] gg1
show grid gg1
let f = sst[y=20s:20n:.1,d=coads_climatology]
DEFINE GRID/LIKE=f/x=temp[d=levitus_climatology] gg2
cancel variable f
show grid gg2
show grid/dynamic

! demonstrate proper management of dynamic axes
DEFINE GRID/LIKE=gg2 gg3
show grid/dynamic
DEFINE GRID/like=temp[d=levitus_climatology] gg2
show grid gg2
show grid/dynamic

! SET GRID using a user-defined variable
let g = sst[x=0:30:.1,d=coads_climatology]
set grid g
show grid

! The SET GRID (default) definition should be protected -- not altered
load/d=coads_climatology sst[y=-10:10:.1,l=1,x=160e]
show grid
show grid/dynamic

! deliberate errors
set mode ignore
load/x=160e/y=5s temp[d=2,k=1,g=no_exist]	! non-existent target variable
let v2 = temp[d=2,k=1,gx=a[d=1],gy=yax]
load/x=160e/y=5s v2
cancel variable a
load/x=160e/y=5s v2		! non-existent target grid
set mode/last ignore

! 12/99
! bug fix: g=abstract now works
! new multiple axis syntax: GXY=gg
define axis/x=1:6:1 x1
define axis/y=1:4:1 y1
define axis/z=1:3:1 z1
define axis/x=1:6:2 x2
define axis/y=1:4:2 y2
define axis/z=1:3:2 z2
let a1 = x[gx=x1]+y[gy=y1]+z[gz=z1]
let a2 = x[gx=x2]+y[gy=y2]+z[gz=z2]
list a1
list a1[gx=a2]
list a1[gxy=a2]
list a1[gzxy=a2]
list a1[gxz=a2]

! 6/01
! test use of expressions in dynamic grid commands
SHOW GRID temp[d=2,gx=sst[d=1],y=10s:2s:.2]             ! implicit Y axis
SHOW GRID "temp[d=2,gx=sst[d=1],y=10s:2s:.2] + 5"
SHOW GRID FFTA(sst[d=1,gx=temp[d=2],y=10s:2s:.2,l=1:5]) ! imposed T axis
SHOW GRID SORTI(temp[d=2,gx=sst[d=1],y=10s:2s:.2])      ! abstract X axis
SHOW GRID "temp[d=2,gx=sst[d=1],y=10s:2s:.2] + 5",FFTA(sst[d=1,gx=temp[d=2],y=10s:2s:.2,l=1:5]),SORTI(temp[d=2,gx=sst[d=1],y=10s:2s:.2])  

load/x=160e/y=5s temp[d=2,gx=sst[d=1],y=10s:2s:.2] + 5
SHOW GRID
SET GRID  FFTA(sst[d=1,gx=temp[d=2],y=10s:2s:.2,l=1:5]) + 5
SHOW GRID
list/j=1:5 j
DEFINE GRID/LIKE="temp[d=2,gx=sst[d=1],y=10s:2s:.2] + 5" mygrid
SHOW GRID MYGRID
SHOW GRID/DYNAMIC

