! bn_tree.jnl

! 11/2013 -- demonstrate SHOW VARIABLE/TREE and RETRUN=STATUS
! 12/14 update to test regridding output using bn_vtree_regrid.jnl
! 11/15 - DEFINE DATA/AGG has become /T instead of /E

CANCEL DATA/ALL
! pure abstract variable -- no dataset
let a = 1
show var/tree a

let b = 1
let c = SIN(b)
show var/tree c

! create dummy datasets so we'll have named file variables to play with
let fv1 = 1
let fvx = x[i=1:3]
let fvz = Z[k=1:3]
SAVE/CLOBBER/QUIET/FILE=my_file_vars.nc fv1, fvx, fvz
SAVE/CLOBBER/QUIET/FILE=other_file_vars.nc fv1
SAVE/CLOBBER/QUIET/FILE=another_file_vars.nc fv1
CAN VAR/ALL
use  my_file_vars, other_file_vars, another_file_vars
set data 1

! ================

! more pure abstract variables
let a = 1
let b = 1
let c = SIN(b)
go bn_vtree.sub c

! file variables
go bn_vtree.sub fv1
show var/tree/d=other_file_vars fv1

! expressions
let a = fv1 + fvx
go bn_vtree.sub a

let a = fv1[d=1] - fv1[d=2]
vtree=file a
vtree=all a
vtree=all /d=1 a
vtree=all /d=2 a
\say "********************************"

! unrecognized variable names
go bn_vtree.sub noexist

! unrecognized dataset
go bn_vtree.sub v[d=noexist]

! unrecognized dataset and variable
go bn_vtree.sub novar[d=nodset]

let b = noexist
go bn_vtree.sub b

! grid-changing functions 
go bn_vtree.sub RESHAPE(fvx,fvz)

! file variable aux var
LET Zpts = Z[Z=0:500:100]
define axis/z/units=meters zax = zpts
LET fv1z = fv1[gz(fvz)=zpts] + fvx
go bn_vtree.sub fv1z

! user-defined aux vars
let depth = z[g=fvz]
LET fv1z = fv1[gz(depth)=zpts] + fvx
go bn_vtree.sub fv1z

let depth = z[g=fvz]+ 0*fvz[k=1]
LET fv1z = fv1[gz(depth)=zpts] + fvx
go bn_vtree.sub fv1z

! unknown aux var
LET fv1z = fv1[gz(noexist)=zpts] + fvx
go bn_vtree.sub fv1z

! unknown variable with known aux var
LET fv1z = noexist[gz(depth)=zpts] + fvx
go bn_vtree.sub fv1z

! unknown variable with unknown aux var
LET fv1z = noexist[gz(unknown_aux)=zpts] + fvx
go bn_vtree.sub fv1z

! aggregate (ensemble) dataset of file variables
ENSEMBLE my_agg = my_file_vars, other_file_vars, another_file_vars
go bn_vtree.sub fv1

! aggregation in which some members are uvars
let/d=other_file_vars   fvx = fv1 + x[gx=fvx[d=1],i=1:3]
let/d=another_file_vars fvx = 2*fv1 + x[gx=fvx[d=1],i=1:3]
ENSEMBLE my_uvar_agg = my_file_vars, other_file_vars, another_file_vars
go bn_vtree.sub fvx

! recursions
! ... a circular parent dependency is a recursion; sibling dependency is not
let p0 = s1 + p1
let p1 = s2 + p2
let p2 = s3 + p3
let p3 = p1 + S3
go bn_vtree.sub p0    ! recursion
let p3 = s1 + S3
go bn_vtree.sub p0    ! ok

! suppressing duplicated siblings (twins)
let fv = fv1 + fv1 + fv1
let uv = fvx + fvx + fvx
show var/tree/d=my_file_vars    fv
show var/tree/d=my_uvar_agg     fv
show var/tree/d=other_file_vars uv
show var/tree/d=my_uvar_agg     uv
\say "***************************"

! suppressing duplicates elsewhere in the family tree ("defined above")
let b = c
let c = 1
let v1 = a  + b + b^2
let v2 = v1 + b + a + a        ! a appears after v1
let v3 = a + v1 + b + a + a    ! a appears before v1
go bn_vtree.sub v2
go bn_vtree.sub v3

! collections of trees handled as a group
! display from large tree size to small, suppressing definitions already displayed
cancel var/all
let a = f
let b = fv1[d=my_file_vars]
let c = 1
let d = b + c
let e = 1
go bn_vtree.sub "a, b, c, noexist, d, e, fvx[d=my_file_vars]"

! reporting other errors in definitions
  set mode ignore

  ! unidentified grid
  let a = i[i=1:5]
  let b = a[g=noexist]
  go bn_vtree.sub b
  list b

  ! syntax error - format of longitude
  let c = a[x=45s]
  go bn_vtree.sub c
  list c

  ! syntax error - extra comma
  let d = a[i=1,,j=2]
  go bn_vtree.sub d
  list d

  ! syntax error - unknown pseudovariable
  let e = a[p=1]
  go bn_vtree.sub e
  list e

  ! out of limit region -- not detectable as error, because grids are never determined
  let f = a[i=10]
  go bn_vtree.sub f
  list f

  set mode/last ignore

! dependency through attribute sharing (not yet implemented)
let a = fvz.long_name[d=1]
show var/tree a
let a = ..history[d=1]
show var/tree a

! *****
! 12/14 addition to test regridding dependencies
go bn_vtree_regrid
