#!/bin/csh -x
#PBS -N proc_150
#PBS -j oe
#PBS -o postproc.log
#PBS -W umask=027
#PBS -l application=hycom
#PBS -l select=1:ncpus=32:mpiprocs=32
#PBS -l place=scatter:excl
#PBS -l walltime=00:30:00
#PBS -A ONRDC10855122
#PBS -q standard

# directory where your files are
#cd /p/work1/abozec/hycom/HYCOM_CESM/gh08_f05_2000p_15.0/run/OUTPUT
set echo

# directory where your scripts are
setenv H /p/home/abozec/hycom/POST_PROC/GLBb0.08/

setenv KDM 32    # number of layer
setenv E   150   # number of the experiment
setenv yrflag 3  # HYCOM calendar
setenv archfq 1  # frequency of output
setenv Y01 101   # year to do (-1900)
setenv TOOLS /p/home/abozec/HYCOM-tools/  # HYCOM-tools directory

set A = ( "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" )

# month to do
foreach m  ( 3 )
setenv mon `echo ${m} | awk '{printf("%02.0f", $1)}'`

## get the time limits of the run
setenv TS `echo "LIMITS" | awk -f ${H}/get_date.awk y01=${Y01} ab=${A[$m]} | awk '{print $1}'`
setenv TM `echo "LIMITS" | awk -f ${H}/get_date.awk y01=${Y01} ab=${A[$m]} | awk '{print $2}'`
C
echo "TS =" $TS "TM =" $TM

## get dates of the archives for the month
setenv year `echo ${Y01} | awk '{printf("%04.0f", $1+1900)}'`
echo ${year}
setenv archm_dates `echo $yrflag ${archfq} ${TS} ${TM} | ${TOOLS}/bin/hycom_archm_dates `
setenv sum_day `echo ${archm_dates} | awk '{printf("%3.0f", $16)}'`
echo 'number of day in a month:' $sum_day
setenv DDATES `echo $yrflag ${archfq} ${TS} ${TM} | ${TOOLS}/bin/hycom_archm_dates | grep ${year} `
set d1=($DDATES)
echo $d1

## create the input file
\rm blkdat.input
touch blkdat.input

echo " ${KDM}     'kk    ' = number of layers involved (usually 1)"         >> blkdat.input
echo "    0      'meansq' = form meansq, rather than mean (0=F,1=T)"       >> blkdat.input
echo " ${sum_day}   'narchs' = number of archives to read (==0 to end input)" >> blkdat.input
@ i=1
 while (${i} <= $sum_day)
echo "archm.${d1[$i]}.a"                                                    >> blkdat.input
   @ i =$i + 1
end
echo "   0     'narchs' = number of archives to read (==0 to end input)"    >> blkdat.input
echo "${E}_archm.${year}_m${mon}"                                           >> blkdat.input


\rm ${E}_archm.${year}_m${mon}.*
cp ${TOOLS}/MPI/meanstd/src/hycom_mnsq .
aprun -n 32  ./hycom_mnsq
wait
end
