! bn_vtree_regrid.jnl
! exercise 12/14 enhancements to display regrid target on a separate line 
! updated 1/26/15 to exercise multiple regrid target outputs
! updated 2/23/15 to report target of regrid even when aux regridding occurs
! 5/2017 note that the fix for #2533 causes some duplicate  UNKNOWN GRID lines 

! case 1: target grid known,   regridded variable known
let myvar = 1           ! var to be regridded is known
let gvar = x[x=1:5]    ! target regridding var is known
let a = myvar[g=gvar] + unvar + kvar
let kvar = 1  ! known var
! unvar is always unknown
sho var/tree a

! case 2: target grid known,   regridded variable unknown
canc var/all
! myvar unknown
let gvar = x[x=1:5]    ! target regridding var is known
let a = myvar[g=gvar] + unvar + kvar
let kvar = 1  ! known var
! unvar is always unknown
sho var/tree a

! case 3: target grid unknown, regridded variable known
canc var/all
let myvar = 1           ! var to be regridded is known
! gvar unknown
let a = myvar[g=gvar] + unvar + kvar
let kvar = 1  ! known var
! unvar is always unknown
sho var/tree a

! case 4: target grid unknown, regridded variable unknown
canc var/all
! myvar unknown
! gvar  unknown
let a = myvar[g=gvar] + unvar + kvar
let kvar = 1  ! known var
! unvar is always unknown
sho var/tree a

! **************************************
! same tests but now with a dataset named with the target grid
use coads_climatology
use levitus_climatology   ! default to d=2

! case 1: target grid known,   regridded variable known
canc var/all
let myvar = 1           ! var to be regridded is known
let a = myvar[g=sst[d=1]] + unvar + kvar
let kvar = 1  ! known var
! unvar is always unknown
sho var/tree=all a

! case 2: target grid known,   regridded variable unknown
canc var/all
! myvar unknown
let a = myvar[g=sst[d=1]] + unvar + kvar
let kvar = 1  ! known var
! unvar is always unknown
sho var/tree=all a

! case 3a: target grid unknown, regridded variable known
canc var/all
let myvar = 1           ! var to be regridded is known
! gvar unknown
let a = myvar[g=gvar[d=1]] + unvar + kvar
let kvar = 1  ! known var
! unvar is always unknown
sho var/tree=all a

! case 3b: target grid unknown cuz of dataset, regridded variable known
canc var/all
let myvar = 1           ! var to be regridded is known
! gvar unknown
let a = myvar[g=sst[d=bad_dset]] + unvar + kvar
let kvar = 1  ! known var
! unvar is always unknown
sho var/tree=all a

! case 4: target grid unknown, regridded variable unknown
canc var/all
! myvar unknown
! gvar  unknown
let a = myvar[g=gvar[d=1]] + unvar + kvar
let kvar = 1  ! known var
! unvar is always unknown
sho var/tree=all a

! *******************
! file variable listing, only
canc var/all
let myvar = 1  
let fvar = airt[d=1]
let kvar = 1  ! known var
* /TREE=FILE with know regrid target
let a = myvar[g=sst[d=1]] + fvar + kvar
sho var/tree=file a

* /TREE=FILE with unknow regrid target
let a = myvar[g=unknown] + fvar + kvar
sho var/tree=file a

! ********************
! regridding to multiple targets

define axis/x=140e:160e:1 x1
define axis/y=20s:20n:1   y1
LET myvar = X[gx=x1] + Y[gy=y1]

define axis/y=5s:5n:.5 yp5

go bn_vtree.sub myvar[g=temp]

! second target *is* a named variable
go bn_vtree.sub myvar[g=temp,gy=sst[d=1]]
go bn_vtree.sub myvar[gx=temp,gy=sst[d=1]]
go bn_vtree.sub myvar[gy=sst[d=1],gx=temp]  ! reverse order, only

! second target is not a named variable
go bn_vtree.sub myvar[g=temp,gy=yp5]
go bn_vtree.sub myvar[gx=temp,gy=yp5]
go bn_vtree.sub myvar[g=temp,y=5s:5n:.5]
go bn_vtree.sub myvar[g=sst[d=1],y=5s:5n:.5]

! multiple targets, including unknown variables
go bn_vtree.sub myvar[g=temp,gy=mybad]
go bn_vtree.sub myvar[g=mybad,gy=temp]
go bn_vtree.sub myvar[gy=temp,g=mybad[d=1]]
go bn_vtree.sub myvar[g=temp,gx=mybad[d=1],gy=mybad2]! distinct unknown targets
go bn_vtree.sub myvar[gx=temp,gy=temp,g=mybad[d=1]]  ! dup known targets
go bn_vtree.sub myvar[g=temp,gx=mybad,gy=mybad]      ! dup unknown suppressed
go bn_vtree.sub myvar[g=temp,gx=mybad[d=2],gy=mybad] ! dup unknown missed
go bn_vtree.sub myvar[g=temp,gx=mybad,gy='MYBAD']    ! dup unknown missed
go bn_vtree.sub myvar[g=myvar,gy=mybad]
go bn_vtree.sub myvar[gy=yp5,g=mybad]
go bn_vtree.sub myvar[g=mybad,y=5s:5n:.5]

! 2/15 additions -- auxiliary variable regridding
USE levitus_climatology
LET myzvar = Z[Z=100:500:100]
DEFINE AXIS/z/units=meters zax = myzvar
SET REGION/i=101:102/j=41:42

LET/D=levitus_climatology depth = z[g=temp]+ 0*temp[k=1]

! valid aux var and target variable
LET tempz = temp[gz(depth)=myzvar]
vtree=all tempz

! valid aux var.  Target is axis name
LET tempz1 = temp[gz(depth)=zax]
vtree=all tempz1

! valid aux var.  Target is unknown
LET tempz2 = temp[gz(depth)=unknown_target]
vtree=all tempz2

! --- unknown aux var, valid target ---
! When an unknown auxiliary variable error occurs it suppresses 
! the reporting of both the variable being regridded and the regrid target
! The underlying issue is that GET_VAR_CONTEXT (called from is_dependencies.F) 
! can return only a single error status
! If it contains ferr_unknown_auxvar, then the success or failure of other
! parts of the context processing are unknown.
! One could kludge the code to find a way around this, but the violence
! required is not justified in order to fix this small loss in error reporting.
LET tempz3 = temp[gz(unknown_aux)=myzvar]
vtree=all tempz3

! a syntax error leads to a similar behavior
LET tempz3a = temp[gz(unknown_aux)=myzvar,SYNTAX_ERROR]
vtree=all tempz3a

! similar behavior here:  the unknown_target does not get reported
LET tempz4 = temp[gz(unknown_aux)=unknown_target]
vtree=all tempz4


