! bn_aggregate_e.jnl
! using the DEFINE DATA/AGGREGATE/E command
! Datasets share sst, but only ens1 has airt. 

! v6.95+ 11/15 *sh* -- /T is now the default orientation
! v6.98 12/15 *sh -- modified so that the sequence number of each
!   dataset in the aggregation is not also its Ferret dataset number
!   Allowing them to be the same was hiding bugs

set mode diag

use ens1, ens2, ens3, ens4
define data/agg/E fourfiles = ens1, ens3, ens2, ens4

! The grid of SST is known.
show data fourfiles

list/i=3 sst[T=@ave]

show memory/all  ! show memory-resident variables before canceling dataset
cancel data fourfiles
show memory/all  ! show memory-resident variables after  canceling dataset

! 12/15 now do again adding COADS to de-align toe dset #s from the sequence #s
cancel data/all
use coads_climatology
use ens1, ens2, ens3, ens4
define data/agg/E/hide fourfiles = ens1, ens3, ens2, ens4
! confirm that it isn't COADS that got hidden
show data
cancel data fourfiles
show data/brief/all/hidden

! Create airt in the other datasets by LET/D definitions.
use ens1, ens2, ens3, ens4
let/d=ens2 airt = sst + 1
let/d=ens3 airt = sst + 1
let/d=ens4 airt = sst + 1

ENSEMBLE fourfiles = ens1, ens3, ens2, ens4
show data fourfiles

list/i=3 airt[T=@ave]

! create an ensemble where one member is missing
ENSEMBLE fourfiles_gap = ens1, -, ens3, ens4
show data/files fourfiles_gap
list/i=3 airt[T=@ave]

cancel mode diag
cancel data/all
cancel var/all

! Subsets of coads_climatology and monthly_navy_winds.
! Define an ensemble after making LET/D definitions so that
! variables have the same name and grid.
use coads_uw
use navy_uw

! Define the ensemble dataset

! intentional errors: 
set mode ignore

! Use dataset thats not open.
ensemble windy = 1,2,3

! No variables on comparable grids.
ensemble windy = 1,2

set mode/last ignore

! Rename the varibles in dataset 2, then define UWND and VWND as
! variables on the grid of dset 1.
set dat 2
set var/name=uin uwnd
set var/name=vin vwnd

! Define uwnd and vwnd in dataset 2 to have the grid of 
! those variables in dataset 1

let/d=2/units="`uin,return=units`"/title="`uin,return=title`" \
 uwnd = uin[d=2,gxy=uwnd[d=1],gt=uwnd[d=1]@mod]
let/d=2/units="`vin,return=units`"/title="`vin,return=title`" \
 vwnd = vin[d=2,gxy=vwnd[d=1],gt=vwnd[d=1]@mod]

show data

! Define the ensemble dataset
ensemble windy = 1,2

! The grid of the aggregate variables with LET/D is known.
show data windy

! Use variable from the new dataset
list/l=3/y=30/x=-59:-51 uwnd[d=1], uwnd[d=2], uwnd[d=3,e=1], uwnd[d=3,e=2], uwnd[d=3,e=@ave]

! Use the other order. As long as we have a file variable, we're ok
show data/brief    ! see what is *not* hidden
cancel data 3
show data/brief    ! see what is *not* hidden

! Define the ensemble dataset
ensemble windy = 2,1

! The grid of the aggregate variables with LET/D is known.
show data windy

! Use variable from the new dataset
list/l=3/y=30/x=-59:-51 uwnd[d=1], uwnd[d=2], uwnd[d=3,e=1], uwnd[d=3,e=2], uwnd[d=3,e=@ave]

cancel data/all
cancel variable/all

! DEFINE DATA/HIDE
! SHOW DATA/HIDDEN
! Alias ENSEMBLE for DEFINE DATA/AGG/E

! If define the ensemble with /HIDE then SHOW DAT will skip the
! member datasets and show only the ensemble.
! show dat/hidden forces showing all

show command define
show command show

use coads_climatology ! to offset indices so they arent equal to Ferret dset #s

use ens1, ens2, ens3, ens4
ensemble/HIDE/title="ensemble with hidden members" fourfiles = ens1, ens2, ens3, ens4

! should just show the ensemble set.  Members are hidden.
show data/brief

! If ask for a member set by name or number, do show it
show data/brief 2
show data/brief ens3

! should show all members plus ensemble
show data/brief/hidden

! Since the members were hidden, they get deleted with the aggregation
cancel data fourfiles
show data/brief
cancel data/all

! Cancel a member, then the ensemble gets canceled.
! The hidden members get canceled along with the parent (1/16)
use coads_climatology ! to offset indices so they arent equal to Ferret dset #s
use ens1, ens2, ens3, ens4
ensemble/HIDE/title="uvars in dset 2"  my_ens = 2,3
show data/brief
cancel data  ens2
show data/brief

! Cancel the ensemble, then hidden members get canceled too

use ens1, ens2, ens3, ens4
ensemble/HIDE/title="uvars in dset 2"  my_ens = 2,3,4,5

! members hidden, showing only ensemble.
show data/brief
cancel data my_ens

! Cancel ensemble dataset: hidden members deleted
show data/brief

! Define several ensembles with /HIDE
ensemble/HIDE/title="ensemble with hidden members" fourfiles = ens1, ens2, ens3, ens4
ensemble/HIDE/title="ensemble with hidden members" some = ens1, ens2
ensemble/HIDE/title="ensemble with hidden members" more = ens2, ens3, ens4

! Shows all the ensembles
show data/brief/files

! Cancel a member of all the ensembles.  All the ensembles get deleted.
cancel data ens2
show data/brief

