

! Benchmark for sort and sample external Functions
! ACM 5/99
! *sh* 3/00 - indicate that test of illegal indices should produce an error

! V550 *sh* 11/02 - documentation change only, reflecting subspan modulo
! The use of L=5 on a 3 point time axis triggers a valid subspan modulo calculation
! V581 *ACM 3/05 Changes in specifying context for arg 2 of SAMPLEL 
!                see notes in Bugzilla under bug 1187.

use coads_climatology
set region/x=141/y=39/z=0/l=1:6

list sst
list samplel(sst, {1,3,5})

let tsorted_indices = sortl(sst)
let tsorted_sst = samplel(sst, tsorted_indices[x=141,y=39,z=0])

list tsorted_indices, tsorted_sst

! test sortt with data containing bad/missing data.

can region 

use  coads_vwnd.cdf
set region/i=90/j=65
list vwnd

let tsorted_indices = sortl(vwnd)
let tsorted_wnd = samplel(vwnd, tsorted_indices[i=90,j=65])
list tsorted_indices, tsorted_wnd

let len = tsorted_wnd[l=@ngd]
list len

! deliberately sample at invalid points
! Note: in MOST circumstances this would simply result in a result of
! missing values, however, the underlying data set has an "enhanced heading"
! so that these indices appear to be valid
SET MODE ignore_error
let tsorted_indices = sortl(vwnd) + 90
let tsorted_wnd = samplel(vwnd, tsorted_indices[i=90,j=65])
list tsorted_indices, tsorted_wnd
SET MODE/LAST ignore_error

! deliberately sample at invalid points that are outside of the axis range
let tsorted_indices = sortl(vwnd) + 999
let tsorted_wnd = samplel(vwnd, tsorted_indices[i=90,j=65])
stat tsorted_wnd

