! bn_iin_regrid.jnl
! @IIN as a regridding transform See Ticket 2346
! 5/10/18 changes to tests, regridding to axis w/ different
!         box size

define axis/edge/x=0:10:1 aline
let a = mod(.5+x[gx=aline],2)

! DEFINE AXIS/LIKE=  qualifier
! box_edges function
define axis/x/like=`a,return=xaxis` aline_edges = box_edges(a)

! @IIN regridding
show axis/x aline_edges

let c = a[gx=aline_edges@iin]
list a
list c

define axis/edge/x=0:10:1 aline
let a = mod(.5+x[gx=aline],2)

! Different delta

define axis/x=0.5:10.5:0.5 alinenew
list a[gx=alinenew@iin]


! result out of range, returns missing
define axis/x=100:111:1 alinenew
list a[gx=alinenew@iin]

! partially out of range 
define axis/x=2:12:1 alinenew
list a[gx=alinenew@iin]



! The axes must have convertible units
! Calendar time axes
define axis/edge/t=0:10:1/units=days/t0=1-jan-1990 taxis
let a = mod(.5+L[gt=taxis],2)

define axis/t/like=taxis taxis_edges = box_edges(a)

let c = a[gt=taxis_edges@iin]
list c

!
define axis/edge/t=0:240:12/units=hours/t0=1-jan-1990 thour
let c = a[gt=thour@iin]
list c


! Intentional errors
set mode ignore
define axis/edge/z=0:10:1/units=km aline
let a = mod(.5+z[gz=aline],2)

define axis/z/units=mile aline_edges = box_edges(a)

let c = a[gz=aline_edges@iin]
list c

! Ferret general regridding handling detects this incompatibility
! between calendar and non-caleandar axis
define axis/t=1:20:1/units=days/t0=1-jan-1990 calendar_t
let a = t[gt=calendar_t]

define axis/t=1:17:2 new_t

let c = a[gt=new_t@iin]
list c

! longitude and undefined units

set mode ignore
define axis/edge/x=0:10:1/units=degrees_east aline
let a = mod(.5+x[gx=aline],2)

define axis/x aline_edges = box_edges(a)

let c = a[gx=aline_edges@iin]
list c


define axis/edge/z=0:10:1/units=km aline
let a = mod(.5+z[gz=aline],2)

define axis/z/units=nothing aline_edges = box_edges(a)

let c = a[gz=aline_edges@iin]
list c
can mode ignore

can mem
can var/all
! plots in different directions

! In X

set win/asp=0.5
set v left
define axis/edge/x=0:10:1 aline
let a = mod(.5+x[gx=aline],2)
plot/step=con/vl=-1:6:1/hl=-1:11:1/grat=dash/col=green/thic=3 a
let a_iin = a[x=@iin]
plot/ov/line/sym=2/si=.1/col=2/thic=2 a_iin

define axis/x/like=`a,return=xaxis` aline_edges = box_edges(a)

let a_iin_edges = a_iin[gx=aline_edges@asn]
let a_int = IF i[gx=aline_edges] EQ 1 THEN 0 ELSE a_iin_edges[x=@shf:-1]
plot/ov/line/sym=28/si=.1/col=purple/thic=2 a_int


! @IIN regridding

show axis/x aline_edges
let c = a[gx=aline_edges@iin]
plot/ov/line/sym=19/si=.1/col=light/thic=1 c

set v right
let b = 1+0*a
plot/step=con/vl=-1:12:1/hl=-1:11:1/grat=dash/col=green/thic=3 b
let b_iin = b[x=@iin]
plot/ov/line/sym=2/si=.1/col=2/thic=2 b_iin
let b_iin_edges = b_iin[gx=aline_edges@asn]
let b_int = IF i[gx=aline_edges] EQ 1 THEN 0 ELSE b_iin_edges[x=@shf:-1]
plot/ov/line/sym=28/si=.1/col=purple/thic=2 b_int

! @IIN regridding
let d = b[gx=aline_edges@iin]
plot/ov/line/sym=19/si=.1/col=light/thic=1 d

list d
define axis/z=1:2:1 otheraxis
let b2d = b + z[gz=otheraxis]
list b2d[gx=aline_edges@iin]

can view; can var/all
! In Y

set win/asp=0.5
set v left
define axis/edge/y=0:10:1 aline


let a = mod(.5+y[gy=aline],2)
plot/step=con/vl=-1:6:1/hl=-1:11:1/grat=dash/col=green/thic=3 a
let a_iin = a[y=@iin]
plot/ov/line/sym=2/si=.1/col=2/thic=2 a_iin

define axis/y/like=`a,return=yaxis` aline_edges = box_edges(a)

let a_iin_edges = a_iin[gy=aline_edges@asn]
let a_int = IF j[gy=aline_edges] EQ 1 THEN 0 ELSE a_iin_edges[y=@shf:-1]
plot/ov/line/sym=28/si=.1/col=purple/thic=2 a_int


! @IIN regridding
let c = a[gy=aline_edges@iin]
plot/ov/line/sym=19/si=.1/col=light/thic=1 c


set v right
let b = 1+0*a
plot/step=con/vl=-1:12:1/hl=-1:11:1/grat=dash/col=green/thic=3 b
let b_iin = b[y=@iin]
plot/ov/line/sym=2/si=.1/col=2/thic=2 b_iin
let b_iin_edges = b_iin[gy=aline_edges@asn]
let b_int = IF j[gy=aline_edges] EQ 1 THEN 0 ELSE b_iin_edges[y=@shf:-1]
plot/ov/line/sym=28/si=.1/col=purple/thic=2 b_int

! @IIN regridding
let d = b[gy=aline_edges@iin]
plot/ov/line/sym=19/si=.1/col=light/thic=1 d

list d
define axis/z=1:2:1 otheraxis
let b2d = b + z[gz=otheraxis]
list b2d[gy=aline_edges@iin]


can view; can var/all
! in Z

set win/asp=0.5
set v left
define axis/edge/z=0:10:1 aline
let a = mod(.5+z[gz=aline],2)
plot/step=con/hl=-1:6:1/vl=-1:11:1/grat=dash/col=green/thic=3 a
let a_iin = a[z=@iin]
plot/ov/line/sym=2/si=.1/col=2/thic=2 a_iin
let zlo = zboxlo[gz=a]
define axis/z aline_edges = zcat(zlo[k=1], zboxhi[gz=a])

let a_iin_edges = a_iin[gz=aline_edges@asn]
let a_int = IF k[gz=aline_edges] EQ 1 THEN 0 ELSE a_iin_edges[z=@shf:-1]
plot/ov/line/sym=28/si=.1/col=purple/thic=2 a_int


! @IIN regridding
let c = a[gz=aline_edges@iin]
plot/ov/line/sym=19/si=.1/col=light/thic=1 c

set v right
let b = 1+0*a
plot/step=con/hl=-1:12:1/vl=-1:11:1/grat=dash/col=green/thic=3 b
let b_iin = b[z=@iin]
plot/ov/line/sym=2/si=.1/col=2/thic=2 b_iin
let b_iin_edges = b_iin[gz=aline_edges@asn]
let b_int = IF k[gz=aline_edges] EQ 1 THEN 0 ELSE b_iin_edges[z=@shf:-1]
plot/ov/line/sym=28/si=.1/col=purple/thic=2 b_int

! @IIN regridding
let d = b[gz=aline_edges@iin]
plot/ov/line/sym=19/si=.1/col=light/thic=1 d

list d
define axis/e=1:2:1 otheraxis
let b2d = b + _e[ge=otheraxis]
list b2d[gz=aline_edges@iin]


can view; can var/all
! In T

set win/asp=0.5
set v left
define axis/edge/t=0:10:1 aline
let a = mod(.5+t[gt=aline],2)
plot/step=con/vl=-1:6:1/hl=-1:11:1/grat=dash/col=green/thic=3 a
let a_iin = a[t=@iin]
plot/ov/line/sym=2/si=.1/col=2/thic=2 a_iin
let tlo = tboxlo[gt=a]
define axis/t aline_edges = tcat(tlo[l=1], tboxhi[gt=a])

let a_iin_edges = a_iin[gt=aline_edges@asn]
let a_int = IF l[gt=aline_edges] EQ 1 THEN 0 ELSE a_iin_edges[t=@shf:-1]
plot/ov/line/sym=28/si=.1/col=purple/thic=2 a_int


! @IIN regridding
let c = a[gt=aline_edges@iin]
plot/ov/line/sym=19/si=.1/col=light/thic=1 c

set v right
let b = 1+0*a
plot/step=con/vl=-1:12:1/hl=-1:11:1/grat=dash/col=green/thic=3 b
let b_iin = b[t=@iin]
plot/ov/line/sym=2/si=.1/col=2/thic=2 b_iin
let b_iin_edges = b_iin[gt=aline_edges@asn]
let b_int = IF l[gt=aline_edges] EQ 1 THEN 0 ELSE b_iin_edges[t=@shf:-1]
plot/ov/line/sym=28/si=.1/col=purple/thic=2 b_int

! @IIN regridding
let d = b[gt=aline_edges@iin]
plot/ov/line/sym=19/si=.1/col=light/thic=1 d

list d
define axis/z=1:2:1 otheraxis
let b2d = b + z[gz=otheraxis]
list b2d[gt=aline_edges@iin]


can view; can var/all
! in E

set win/asp=0.5
set v left
define axis/edge/e=0:10:1 aline
let a = mod(.5+_e[ge=aline],2)
plot/step=con/vl=-1:6:1/hl=-1:11:1/grat=dash/col=green/thic=3 a
let a_iin = a[e=@iin]
plot/ov/line/sym=2/si=.1/col=2/thic=2 a_iin
let elo = eboxlo[ge=a]
define axis/e aline_edges = ecat(elo[m=1], eboxhi[ge=a])

let a_iin_edges = a_iin[ge=aline_edges@asn]
let a_int = IF _m[ge=aline_edges] EQ 1 THEN 0 ELSE a_iin_edges[e=@shf:-1]
plot/ov/line/sym=28/si=.1/col=purple/thic=2 a_int


! @IIN regridding
let c = a[ge=aline_edges@iin]
plot/ov/line/sym=19/si=.1/col=light/thic=1 c

set v right
let b = 1+0*a
plot/step=con/hl=-1:12:1/vl=-1:11:1/grat=dash/col=green/thic=3 b
let b_iin = b[e=@iin]
plot/ov/line/sym=2/si=.1/col=2/thic=2 b_iin
let b_iin_edges = b_iin[ge=aline_edges@asn]
let b_int = IF _m[ge=aline_edges] EQ 1 THEN 0 ELSE b_iin_edges[e=@shf:-1]
plot/ov/line/sym=28/si=.1/col=purple/thic=2 b_int

! @IIN regridding
let d = b[ge=aline_edges@iin]
plot/ov/line/sym=19/si=.1/col=light/thic=1 d

list d
define axis/z=1:2:1 otheraxis
let b2d = b + z[gz=otheraxis]
list b2d[ge=aline_edges@iin]


can view; can var/all
! In F

set win/asp=0.5
set v left
define axis/edge/f=0:10:1 aline
let a = mod(.5+_f[gf=aline],2)
plot/step=con/vl=-1:6:1/hl=-1:11:1/grat=dash/col=green/thic=3 a
let a_iin = a[f=@iin]
plot/ov/line/sym=2/si=.1/col=2/thic=2 a_iin
let flo = fboxlo[gf=a]
define axis/f aline_edges = fcat(flo[n=1], fboxhi[gf=a])

let a_iin_edges = a_iin[gf=aline_edges@asn]
let a_int = IF _n[gf=aline_edges] EQ 1 THEN 0 ELSE a_iin_edges[f=@shf:-1]
plot/ov/line/sym=28/si=.1/col=purple/thic=2 a_int


! @IIN regridding
let c = a[gf=aline_edges@iin]
plot/ov/line/sym=19/si=.1/col=light/thic=1 c

set v right
let b = 1+0*a
plot/step=con/vl=-1:12:1/hl=-1:11:1/grat=dash/col=green/thic=3 b
let b_iin = b[f=@iin]
plot/ov/line/sym=2/si=.1/col=2/thic=2 b_iin
let b_iin_edges = b_iin[gf=aline_edges@asn]
let b_int = IF _n[gf=aline_edges] EQ 1 THEN 0 ELSE b_iin_edges[f=@shf:-1]
plot/ov/line/sym=28/si=.1/col=purple/thic=2 b_int

! @IIN regridding
let d = b[gf=aline_edges@iin]
plot/ov/line/sym=19/si=.1/col=light/thic=1 d

list d
define axis/z=1:2:1 otheraxis
let b2d = b + z[gz=otheraxis]
list b2d[gf=aline_edges@iin]

! More examples: scale for world coordinates

! longitudes

 
define axis/edge/x=0:10:1/units=degrees_east aline
let a = mod(.5+x[gx=aline],2)
define axis/x/like=`a,return=xaxis` aline_edges = box_edges(a)
let a_iin_regrid =  a[gx=aline_edges@iin]
let a_iin = a[x=@iin]
 
list a_iin
list a_iin_regrid

use coads_climatology

define axis/x/like=`sst,return=xaxis` aline_edges = box_edges(sst[y=0,l=1,x=151:181])


let sst_iin_regrid = sst[gx=aline_edges@iin]

! With cosine(lat) correction
list/l=1 sst[x=151:181@iin,y=0], sst[x=151:181@iin,y=50]
list/l=1 sst_iin_regrid[y=0], sst_iin_regrid[y=50]

! latitudes

define axis/edge/y=10:80:10/units=degrees_north aline
let a = y[gy=aline]/10
define axis/y/like=`a,return=yaxis` aline_edges = box_edges(a)
let a_iin_regrid = a[gy=aline_edges@iin]
let a_iin = a[y=@iin]

list a_iin
list a_iin_regrid

! Tests of regrid-to-self regridding  
! @iin regridding to the source axis

define axis/edge/x=0:10:1 aline
let a = i[gx=aline]

! edges axis
define axis/x/like=`a,return=xaxis` aline_edges = box_edges(a)

let c = a[gx=aline_edges@iin]
let d = a[gx=aline@iin]

! to edges axis
list c

! to source axis. Pushes result of a @IIN transform up half a cell
list a, d, a[x=@iin]


def ax/edge/t=1-jan-0001:1-jan-0004:365/unit=day/cal=noleap tax
let one = 1 + 0*t[gt=tax]
list one, one[t=@iin], one[gt=tax@iin]

! Gridding to an identical second axis, columns 3 and 4 match:
def ax/edge/t=1-jan-0001:1-jan-0004:365/unit=day/cal=noleap tax2
list one, one[t=@iin], one[gt=tax@iin],  one[gt=tax2@iin]

! Restore default aspect ratio
set window/aspect=0.75
