! bn_true_monthly.jnl
! examples defining axes with /DEFINE AXIS/MONTHLY
!
! When a true monthly axis is found in a dataset, store it as a TRUEMONTH axis
! When writing a TRUEMONTH axis, write the correct coordinates using the original
! units if it came from a dataset, or using units of days if it's user-defined.

! See known error conditions below


! For comparison, define regularly-spaced monthly axis of the sort we've always defined

define axis/t=1-jan-2010:31-jul-2011:1/t0=1-jan-2000/units=month/edges t_reg_month
sh axis/t t_reg_month

! true-monthly axes, t0 at start of axis
define axis/t=1-jan-2010:31-jul-2011:1/t0=1-jan-2010/monthly taxis
sh axis/t taxis

! t0 before start of axis
define axis/t=1-jan-2010:31-jul-2011:1/t0=1-jan-2000/monthly taxis
sh axis/t taxis

! default time origin is start of month of first month
define axis/t=1-jul-2010:1-jan-2012:1/monthly taxis
sh axis/t taxis

! Cell edges at mid-month
define axis/t=15-mar-2010:15-dec-2011:1/t0=1-jan-2000/monthly taxis
sh axis/t taxis

! other calendars JULIAN
define axis/cal=julian/t=1-jan-2011:31-jul-2012:1/t0=1-jan-2000/monthly taxis
sh axis/t taxis

! default time origin
define axis/cal=julian/t=15-mar-2010:15-dec-2011:1/monthly taxis
sh axis/t taxis

! other calendars NOLEAP
define axis/cal=noleap/t=1-jan-2011:31-jul-2012:1/t0=1-jan-2000/monthly taxis
sh axis/t taxis

! not at first of month
define axis/cal=julian/t=15-mar-2010:15-dec-2011:1/t0=1-jan-2000/monthly taxis
sh axis/t taxis

! other calendars 360_day
define axis/cal=360_day/t=1-jan-2010:30-jul-2011:1/t0=1-jan-2000/monthly taxis
sh axis/t taxis

! Writing data, including a leap year

define axis/t=3-mar-2011:3-jan-2013:1/monthly/t0=1-jan-2010 taxis
def var tt = t[gt=taxis]
save/clobber/file=a.nc tt

can var/all
use a
sh dat/att
sh grid tt
sh axis/t taxis

can dat/all

define axis/t=3-mar-2011:3-jan-2013:1/monthly/t0=1-jan-2010/calendar=julian taxis
def var tt = t[gt=taxis]
save/clobber/file=a.nc tt

can var/all
use a
sh dat/att
sh grid tt
sh axis/t taxis

can dat/all

define axis/t=3-mar-2011:3-jan-2013:1/monthly/t0=1-jan-2010/calendar=360_day taxis
def var tt = t[gt=taxis]
save/clobber/file=a.nc tt

can var/all
use a
sh dat/att
sh grid tt
sh axis/t taxis

! Without date range spec,
def axis/monthly/t0=1-jan-0000/t=1:24:1 taxis
sh axis/t taxis

! With T0 later than year 0
def axis/monthly/t0=1-jan-2000/t=1:24:1 taxis
sh axis/t taxis

! Default T0, non-calendar t def
define axis/monthly/t=1:36:1 taxis
sh axis/t taxis

! modulo 
define axis/t=1-jan-2010:31-dec-2010:1/t0=1-jan-2000/monthly/modulo taxis

let var = {1,2,3,4,5,6,7,8,9,10,11,12}
let tvar = reshape(var,t[gt=taxis])
list/L=1:23 tvar
! *sh* 3/17:  a possible alternative syntax:
!  let var = TSEQUENCE({1,2,3,4,5,6,7,8,9,10,11,12})
!  let tvar = var[gt=taxis@asn]
! Since this is a fairly common need -- creating a line along some axis
! should we offer a new function AXSEQUENCE(array, axis)  ?

! Striding on a monthly axis

def axis/monthly/t0=1-jan-2000/t=1-jan-2000:31-dec-2004:1/unit=monthly taxis
!sh axis/t/l=1:14 taxis
let tt = t[gt=taxis]
list tt[l=1:6:2]

list/prec=6 tt[l=1:60:12]

! From a file
save/file=a.nc/clobber tt
can dat/all; can var/all
use a
list/prec=6 tt[l=1:60:12]

! DEFINE AXIS/MONTHLY warnings and error conditions:

! Not an error - /EDGES is ignored, as it's the default
define axis/t=1-jan-2010:31-jul-2011:1/t0=1-jan-2000/monthly/edges taxis
sh axis taxis

! Not an error - /UNITS=month is the default
define axis/t=1-jan-2010:31-jul-2011:1/t0=1-jan-2000/monthly/units=months taxis
sh axis taxis

set mode ignore
! not all months have day 30, cannot start at day 30
define axis/t=30-mar-2010:30-dec-2011:1/monthly taxis

! Units of other than months is an error
define axis/t=1-jan-2010:31-jul-2011:30/t0=1-jan-2000/monthly/units=days taxis

! /NPOINTS, /BOUNDS, /FROMDATA are not allowed. Errors in start:end:del
define axis/t=1-jan-2010:31-dec-2012:1/t0=1-jan-2000/monthly/npoints=13 taxis

! missing delta-t
define axis/t=1-jan-2010:31-dec-2012/t0=1-jan-2000/monthly taxis

! delta-t must be 1
define axis/t=1-jan-2010:31-dec-2012:2/t0=1-jan-2000/monthly taxis

! irregular or from-data
define axis/t/t0=1-jan-2000/monthly taxis = {123,124,126}

define axis/from/t/t0=1-jan-2001/monthly/edges taxis = {5,6,6,6,7}

! bounds
def axis/t/t0=1-jan-2000/monthly/bounds tax={1.2, 2, 3.2, 5, 7, 8.4, 9}, {0.5,1.5, 1.5,2.5, 2.5,3.5, 3.5,6.5, 6.5,7.5, 7.5,8.5, 8.5,9.5}

! Must be T direction (do we need /F=?)
define axis/x=1:5:1/monthly aaxis

cancel mode ignore
