! bn_strides_revs_perms.jnl
! *sh* 6/99

! exercise special netCDF performance and robustness enhancements to allow
! reading with strides and permuting and reversing axes

! * * * * * PERMUTATIONS * * * * *
! How to evaluate the correct ordering:
! The data in bn_strides.cdf is ordered with the 1's digit
! moving fastert, then the 10's, then 100's, etc.

! If (say) /ORDER=ZXY is given we should see the 100's digit
! moving fastest (left to right on each line), the 1's next
! (vertically within each block of output), and the 10's next
! advancing from block to block

canc data/all
cancel mode diagnostic

! first with an XYZT variable
set region/i=3:6/j=2:4/k=1:2/l=1
use bn_strides
list bigvar
canc data/all

use/order=yx bn_strides
list bigvar
canc data/all

use/order=zyx bn_strides
list bigvar
canc data/all

use/order=zxy bn_strides
list bigvar
canc data/all

! now with an XYT variable
cancel region
set region/i=1:4/j=1:3/l=1:2
use bn_strides
show grid xytvar
list xytvar
canc data/all

use/order=yx bn_strides
show grid xytvar
list xytvar
canc data/all

use/order=tyx bn_strides
show grid xytvar
list xytvar
canc data/all

use/order=txy bn_strides
show grid xytvar
list xytvar
canc data/all

! map into Z instead of T
use/order=zxy bn_strides
show grid xytvar
list/k=1:2 xytvar
canc data/all

! ambiguous mapping
use/order=tx bn_strides
show grid xytvar
list xytvar
canc data/all

! final "z" goes beyond the 3D -- no effect
use/order=txyz bn_strides
show grid xytvar
show grid var
canc data/all

! * * * * * STRIDES * * * * *
use bn_strides
set mode diag
cancel memory/all

SHOW DATA

! basic strides
LIST VAR[i=2:4:2]

! cache hit after changes to COMPLETE_MISSING_LIMS and FLESH_OUT_AXIS
LIST VAR[i=2:4:2]

! cache hit through non-file variable
let a = VAR[i=2:4:2]
list a
list a		! from cache ...

! cache hit from full region cache -- bypass use of strides
cancel memory/all
load var
LIST VAR[i=2:4:2]
LIST VAR[i=2:4:2,j=1:3:2]
CANC MEM/ALL

! modulo requests cannot use strides (as this example shows)
set axis/modulo XAX1_4
LIST VAR[I=1:10:3,j=1,k=1,l=1]    ! 1, 4, 7(3), 10(2)
cancel axis/modulo XAX1_4

! strides applied to more than one axis need special attention in IS_REGRID
LIST VAR[i=2:4:2,j=1:3:2]
LIST VAR[i=2:6:2,j=1:5:2]

! deliberate error
set mode ignore; LIST/i=2:4:2 VAR; cancel mode ignore

! averaging causes bypass of strides
LIST VAR[i=2:4:2@ave]

! test sub-region selection on strides axis
let a = bigvar[i=1:40:5]
list/j=1/k=1/l=1 a[i=3:5]
list/k=1/l=1 a[i=3:5,j=1:8:2]

! ****** unequally spaced points on parent axis
! reference data
LIST/order=x/j=1/k=1/l=1 UNEVENVAR
cancel mem/all

! basic strides
LIST/order=x/j=1/k=1/l=1 UNEVENVAR[i=2:4:2]

! cache hit after changes to COMPLETE_MISSING_LIMS and FLESH_OUT_AXIS
LIST/order=x/j=1/k=1/l=1 UNEVENVAR[i=2:4:2]

! cache hit through non-file variable
let a = UNEVENVAR[i=2:4:2]
list/order=x/j=1/k=1/l=1 a
list/order=x/j=1/k=1/l=1 a		! from cache ...

! cache hit from full region cache -- bypass use of strides
cancel memory/all
load/k=1/l=1 UNEVENVAR
LIST/j=1/k=1/l=1 UNEVENVAR[i=2:4:2]
LIST/j=1/k=1/l=1 UNEVENVAR[i=2:4:2,j=1:5:2]
CANC MEM/ALL

! modulo requests cannot use strides (as this example shows)
set axis/modulo XUNEVEN
LIST/order=x UNEVENVAR[I=1:30:7,j=1,k=1,l=1]    ! 1, 8, 15(5), 22(2)
cancel axis/modulo XUNEVEN

! strides applied to more than one axis need special attention in IS_REGRID
LIST/k=1/l=1 UNEVENVAR[i=2:4:2,j=1:3:2]
LIST/k=1/l=1 UNEVENVAR[i=2:6:2,j=1:5:2]

! averaging causes bypass of strides
LIST/j=1/k=1/l=1/precision=9 UNEVENVAR[i=2:8:2@ave]
LIST/j=1/k=1/l=1/precision=9 UNEVENVAR[i=2:8:2]		! for comparison

! test special logic in tm_world_recur for endpoints of strides on irreg axis
LIST/j=1/k=1/l=1/precision=9 UNEVENVAR[i=1:10:2@ave]


! * * * * * MIXED STRIDES and PERMUTATIONS * * * * *

set region/i=3:6/j=2:4/k=1:2/l=1

use/order=yx bn_strides
list bigvar[i=3:9:2]
list bigvar[j=2:6:2]
list bigvar[i=3:9:2, j=2:6:2, k=1:3:2]
canc data/all

use/order=zyx bn_strides
list bigvar[i=3:9:2]
list bigvar[j=2:6:2]
list bigvar[i=3:9:2, j=2:6:2, k=1:3:2]
canc data/all

use/order=zxy bn_strides
list bigvar[i=3:9:2]
list bigvar[j=2:6:2]
list bigvar[i=3:9:2, j=2:6:2, k=1:3:2]
canc data/all

! now with xytvar
cancel region
set region/i=1:4/j=1:3/l=1:2
use bn_strides
show grid xytvar
list xytvar[i=1:7:2]
list xytvar[j=2:6:2]
list xytvar[i=1:7:2,j=2:6:2,l=3:5:2]
canc data/all

use/order=yx bn_strides
show grid xytvar
list xytvar[i=1:7:2]
list xytvar[j=2:6:2]
list xytvar[i=1:7:2,j=2:6:2,l=3:5:2]
canc data/all

use/order=tyx bn_strides
show grid xytvar
list xytvar[i=1:7:2]
list xytvar[j=2:6:2]
list xytvar[i=1:7:2,j=2:6:2,l=3:5:2]
canc data/all

use/order=txy bn_strides
show grid xytvar
list xytvar[i=1:7:2]
list xytvar[j=2:6:2]
list xytvar[i=1:7:2,j=2:6:2,l=3:5:2]
canc data/all

! map into Z instead of T
use/order=zxy bn_strides
show grid xytvar
list/k=1:2 xytvar[i=1:7:2]
list/k=1:2 xytvar[j=2:6:2]
list xytvar[i=1:7:2,j=2:6:2,k=3:5:2]
canc data/all


! * * * * * MIXED STRIDES, PERMUTATIONS and REVERSALS * * * * *
! to evaluate the output remember
!  1) the axis lengths on disk are 9,8,7,6 for X,Y,Z,T, respectively
!  2) the negatives apply to the axis in memory (after permutation)
! Thus, with /ORDER=y-x we expect the 2nd disk axis (the 10's place) to
! be reversed and the values 1:5:4 to be replaced by 9-(1:5:4) => 8:4:4

! Note that the logic of the /ORDER syntax when BOTH permutations and
! reversals are aplied would be more natural if the reversal were applied
! BEFORE the permutation. This should occur in routine TM_AXIS_ORDER.
! However, the logic changes needed were messay compared to the rare
! usage of complex combinations of reversals and permutations, so it was
! left as-is.

cancel region; set region/l=1

! reference output
use/order=yx bn_strides
list midvar[i=1:5:4, j=2:5:3, k=3:5:2]
canc data/all

use/order=-yx bn_strides
list midvar[i=1:5:4, j=2:5:3, k=3:5:2]
canc data/all

use/order=y-x bn_strides
list midvar[i=1:5:4, j=2:5:3, k=3:5:2]
canc data/all

! reference output
use/order=tyx bn_strides
list midvar[i=1:5:4, j=2:5:3, k=3:5:2]
canc data/all

! note the reversal on axes 1,2 and 3 (X,Y,Z in memory -- NOT T)
use/order=-t-y-x bn_strides
list midvar[i=1:5:4, j=2:5:3, k=3:5:2]
canc data/all

! reference output
use/order=txy bn_strides
list midvar[i=1:5:4, j=2:5:3, k=3:5:2]
canc data/all

use/order=-t-x-y bn_strides
list midvar[i=1:5:4, j=2:5:3, k=3:5:2]
canc data/all
