#!/bin/bash
#PBS -S /bin/bash
#PBS -q development
#PBS -j oe
#PBS -l walltime=04:00:00
#PBS -l select=1:ncpus=40:mem=50gb:res_image=ppp_eccc_all_default_ubuntu-14.04-amd64_latest

#----------------------------------------------------------------
# get start_time and stop_time from config file
# they were used to check the continuity of netcdf files in scripts ncat.var
rm -f tmp.cfg
grep s*_time=  ${WRK_DIR}/canesm.cfg > tmp.cfg
. tmp.cfg
rm -f tmp.cfg

# make sure they are 4 digits
export start_time=${start_time%:*}
export stop_time=${stop_time%:*}

# get cimp6 directory structure down to variant number
cd $ncout
dircmip6=${ncout}/$(find CMIP6 -maxdepth 5 -mindepth 5 -type d)

# get cmor table list in the directory
cd $dircmip6
[[ $ctablist ]] || ctablist=$(ls )

# Do not concatenate fixed tables
# Sub-daily tables need to be tested for different time stamps
for ctab in Ofx fx CFsubhr 6hrLev ; do
  ctablist=$(echo ${ctablist} | sed "s/${ctab} / /")
done

# loop over all cmor tables
for ctab in $ctablist; do
  cd $dircmip6/$ctab
  # get variable list in the directory
  [[ $varslist ]] || varslist=$(ls )

  # loop over all variables and do the concatenation
  for var in $varslist ; do
    (${CCRNSRC}/ncconv/bin/ncat.var $runid $ctab $var $ncout)&
  done
  varslist=""
done

wait 

