! bn_def_axis_like
! testing DEFINE AXIS/LIKE=existing_axis  new_axis
!
! Gets the attributes from the specified variable
! and assigns them to the new variable.  Skips
! missing_flag and _FillVar as does the existing
! command "set att/like=oldvar newvar"
! 2/22/2018  ACM

use ocean_atlas_temp.cdf

! get the attributes, define a new depth axis 

define axis/like=`temp,return=zaxis`/z=1:15:1 myz
sh axis `temp,return=zaxis`
sh axis myz

! ! Get the attributes, but change one of them with /UNITS=
define axis/like=`temp,return=xaxis`/x=20:90:.25/units="Deg E" myx
sh axis `temp,return=xaxis`
sh axis myx

! On the second axis inherit time origin, units, but change the calendar

define axis/t=1-jan-2001:9-dec-2001:`24*60`/t0=1-jan-1970/units=seconds timesec
define axis/like=timesec/t=1-jan-2015:31-dec-2015:`24*3600`/calendar=Julian timeday

sh axis timesec
sh axis timeday

! intentional errors
set mode ignore

! Unknown axis

show axis axis_undefined
define axis/like=axis_undefined/x=1:8:1 myx

! axis must be in the same direction as the one we're inheriting from

define axis/like=`temp,return=xaxis`/y=-10:10:10 myy

!  Still need to specify the direction and coordinates
define axis/like=timesec/units=minutes timemin
define axis/like=timesec/units=minutes/t timemin

set mode/last ignore

! Tests for details on time axes

cancel data/all

use coads_climatology
define axis/t/like=`sst,r=taxis` tax = t[gt=sst]
say `sst,r=t0`, `sst,r=tmod`
say `t[gt=tax],r=t0`, `t[gt=tax],r=tmod` 

use clim_airt_lev.cdf
define axis/t/like=`airt,r=taxis` tax = t[gt=airt]

! These should be the same

say `airt,return=t0`, `airt,return=tmod`, `airt,return=tunits`
say `t[gt=tax],r=t0`, `t[gt=tax],r=tmod`, `t[gt=tax],r=tunits`

! calendar is inherited
show axis month_julian

define axis/t/like=month_julian tax = t[gt=month_julian]
sh axis tax

! Note how the modulo length is shorter than month_julian's.
! But it's ok if we use the edges.

define axis/t/like=month_julian/edges tax = box_edges(t[gt=month_julian])
sh axis tax

! Define and redefine an axis with /LIKE.  Make sure the last definition
! is what's stored

define axis/edge/t=1-jan-0001:1-jan-0004:1/unit=year/cal=noleap tax
define axis/t/like=tax tax2 = t[gt=tax]
show axis tax2

! Keep the calendar from axis tax, change the time origin
define axis/t/like=tax/t0=01-JAN-1970 tax2 = {1,4,5,6,7}
show axis tax2

! Intentional errors. 
! The second command here is an error - tax is an axis not a var.
! Make sure we can recover from that error.

set mode ignore
define axis/edge/t=1-jan-0001:1-jan-0004:1/unit=year/cal=noleap tax
define axis/t/like=tax tax3 = tax

! the axis should be undisturbed
sh axis tax

set mode/last ignore
