#!/bin/bash -x
#help#
#**********************************************************************************************************#
#                                                                                                          #
# script to run CPTEC Post-processing on PC Clusters under MPI Scali                                       #
# and Sun Grid Engine without OpenMP                                                                       #
#                                                                                                          #
# assumptions: assume present but NOT at the same directory:                                               #
#              $FEXE/PostGrib (Post-processing Executable file)                                            #
#              $FSCR/POSTIN-GRIB (Post-processing input Namelist file)                                     #
#                                                                                                          #
# usage:runPos cpu_mpi cpu_node task_omp name   TRC LV  LABELI     LABELW     LABELF      NMC START FIRST  #
#       runPos    1       1          1   posbam 126 28  2011120112 2011120712 2011120712  NMC COLD   1     #
# where:                                                                                                   #
# cpu_mpi : integer, the desired number of mpi processes                                                   #
# cpu_node: integer, the desired number of mpi processes per shared memory node                            #
# task_omp: numero de processos openmp por processos mpi                                                   # 
# name    : Name of the Proccess (job)                                                                     #
# TRC     : Spectral resolution(Number of wave)                                                            #
# LV      : Vertical resolution(Number of layer in atmosphere)                                             #
# LABELI  : initial data YYYYMMDDHH                                                                        #
# LABELW  : restart data YYYYMMDDHH (if COLD ---> LABELW=LABELF)                                           #
# LABELF  : Final   data YYYYMMDDHH                                                                        #
# NMC     : prefix of files   NMC                                                                          #
# START   : type of pos-processing (COLD WARM)                                                             #
# FIRST   : ---                                                                                            #
#**********************************************************************************************************#
#help#
#
#       Help:
#
if [ "${1}" = "help" -o -z "${1}" ]
then
  cat < ${0} | sed -n '/^#help#/,/^#help#/p'
  exit 1
else
  TRC=`echo ${5} | awk '{print $1/1}'`   
fi
if [ -z "${6}" ]
then
  echo "LV is not set" 
  exit 2
else
  LV=`echo ${6} | awk '{print $1/1}'`    
fi

if [ -z "${7}" ]
then
  echo "LABELI is not set" 
  exit 3
else
  export LABELI=${7}  
fi

if [ -z "${8}" ]
then
  echo "LABELW is not set" 
  exit 3
else
  export LABELW=${8}  
fi

if [ -z "${9}" ]
then
  echo "LABELF is not set" 
  exit 3
else
  export LABELF=${9}  
fi

if [ -z "${10}" ]
then
  echo "PREFIC is not set" 
  exit 3
else
  export PREFIC=${10}  
fi

if [ -z "${11}" ]
then
  echo "START is not set" 
  exit 3
else
  export START=${11}  
fi
if [ -z "${12}" ]
then 
  echo "FIRST is not set" 
  exit 3
else 
  export FIRST=${12}
fi

if [ "$#" == 13 ]
then 
  export hold=${13}    
  export FIRST=${13}
  export hold=""
else 
  export hold=""
fi
echo $hold

#
# SETTING THE APPROPRIATED ENVIRONMENT
#
CASE=`echo ${TRC} ${LV} |awk '{ printf("TQ%4.4dL%3.3d\n",$1,$2)  }' `
PATHA=`pwd`
export FILEENV=`find ${PATHA} -name EnvironmentalVariablesMCGA -print`
export PATHENV=`dirname ${FILEENV}`
export PATHBASE=`cd ${PATHENV};cd ../;pwd`
. ${FILEENV} ${CASE} ${PREFIC}
cd ${HOME_suite}/run
RUNTM=`date +'%Y%m%d%T'`
yi=`awk 'BEGIN {print substr("'${LABELI}'",1,4)/1}'` ; export yi
mi=`awk 'BEGIN {print substr("'${LABELI}'",5,2)/1}'` ; export mi
di=`awk 'BEGIN {print substr("'${LABELI}'",7,2)/1}'` ; export di
hi=`awk 'BEGIN {print substr("'${LABELI}'",9,2)/1}'` ; export hi

yf=`awk 'BEGIN {print substr("'${LABELF}'",1,4)/1}'` ; export yf
mf=`awk 'BEGIN {print substr("'${LABELF}'",5,2)/1}'` ; export mf
df=`awk 'BEGIN {print substr("'${LABELF}'",7,2)/1}'` ; export df
hf=`awk 'BEGIN {print substr("'${LABELF}'",9,2)/1}'` ; export hf
set +x
declare -a md=( 0 31 28 31 30 31 30 31 31 30 31 30 31 )
let ihour=$hi/1
let houryear=0
let iyear=$yi/1
while [ ${iyear} -le ${yf} ];do

if [[ ${iyear} -eq ${yf}  ]];then
lmon=${mf}
else
lmon=12
fi

let imon=$mi/1
while [ ${imon} -le ${lmon} ];do

if [ ${iyear} -eq ${yf}  ] ; then
   bi=`awk 'BEGIN {print '${iyear}'%4.0 }'` 
   if [ ${imon} -lt ${mf} ]; then
      if [ ${bi} -eq 0  ] && [ ${imon} -eq 2 ]; then
         lday=29
      else
         lday=${md[${imon}]} 
      fi
   else
      lday=${df}
   fi
else
   bi=`awk 'BEGIN {print '${iyear}'%4.0 }'` 
   if [ ${bi} -eq 0  ] && [ ${imon} -eq 2 ]; then
     lday=29
   else
     lday=${md[${imon}]} 
   fi
fi



let iday=$di/1
while [ ${iday} -le ${lday} ];do
##############################################################
      if [ ${iyear} -eq  ${yf} ] && [ ${imon} -eq ${mf} ] && [ ${iday} -le ${df} ]; then
        let lhour=${hf} 
      else
        lhour=23
      fi
while [ ${ihour} -le ${lhour} ];do

let houryear=${houryear}+1

let ihour=${ihour}+1
done
let ihour=0

##############################################################



##############################################################
let iday=${iday}+1
done

let imon=${imon}+1
done

let iyear=${iyear}+1
done

set -x
#En Internet hay mucha mas informacion disponible sobre los dias julianos. Un buen lugar para comenzar es el U.S. Naval Observatory. 
#Si dicho servidor no es encuentra disponible cuando usted lea esto, introduzca julian day en su buscador favorito.
#DJ=`expr ( $di - 32075 + 1461*( $yi + 4800 + ( $mi - 14 ) / 12 ) / 4 + 367*( $mi - 2 - ( $mi - 14 ) / 12 * 12 ) / 12 - 3*( ( $yi + 4900 + ( $mi - 14 ) / 12 ) / 100 ) / 4)`
if  [ ${houryear} -le  0 ];then
NEXTDATE=`${caldate} ${LABELI} + 6h yyyymmddhh`
CURRDATE=`${caldate} ${LABELI} + 0h yyyymmddhh`
PREVDATE=`${caldate} ${LABELI} - 6h yyyymmddhh`
else
NEXTDATE=${LABELF}
CURRDATE=${LABELI}
PREVDATE=${LABELW}
fi
echo ${PREVDATE}

# script arguments and directory
DIRRESOL=`echo ${TRC} ${LV} |awk '{ printf("TQ%4.4dL%3.3d\n",$1,$2)  }' `
EXECFILEPATH=${DK_suite}/pos/exec; EXECFILEPATH2=${DK_suite}/pos/exec_${PREFIC}${LABELI}${LABELF};mkdir -p ${EXECFILEPATH2}/setout
SCRIPTFILEPATH=${EXECFILEPATH2}/postg.${PREFIC}.${DIRRESOL}.${MAQUI}
NAMELISTFILEPATH=${HOME_suite}/run
OUTPUTFILEPATH=${HOME_suite}/run/setout/postg${DIRRESOL}.${MAQUI}.${RUNTM}.out
export cpu_mpi=${1};  if [[ -z "${1}"  ]]; then cpu_mpi=24 ; fi
export cpu_node=${2}; if [[ -z "${2}"  ]]; then cpu_node=24; fi
export ThreadsperMPITASK=$3; if [[ -z "${3}"  ]]; then cpu_node=1; fi
export RES=$4

# total cpus and nodes

num=$(($cpu_mpi+$cpu_node-1))
fra=$(($num/$cpu_node))
cpu_tot=$(($fra*$cpu_node))
echo fila=mpi-npn${cpu_node} total cpus=${cpu_tot}
export PBS_SERVER=${pbs_server1}
optserver=`printf "$PBS_SERVER \n" | cut -c1-3`
if [[ (${optserver} = "aux") ]]; then
export MPPBS="#"
else
export MPPBS="#PBS -l mppwidth=${cpu_mpi}"
fi

host=`hostname`

export aspa="'"  
export GPOS=GPOS
export trunc=$TRC
export lev=$LV
if [ ${START} = "COLD" ] ;then
export nffrs=1        
export nfbeg=1
else
export nffrs=1        
export nfbeg=1
fi
if [ -z "${PREFXO}" ] ;then
export PREFX="NMC" 
else
export PREFX="${PREFXO}" 
fi 
export nfend=60000                                                                    # number of forecasted files to be post-processed
export datain=${DK_suite2}/model/dataout/${DIRRESOL}/${LABELI}-${PREVDATE}    # main dataout directory
export datalib=${DK_suite}/pos/datain                  # main dataout directory
export dataout=${DK_suite2}/pos/dataout/${DIRRESOL}/${LABELI}; mkdir -p ${dataout}     # main dataout directory
export res=-0.5              # if res >  0 Define output resolution (deg)
export ENS=".TRUE."         # ensemble products => TRUE, only for AVN, 0?P, 0?N PREFX
export RegIntIn=".TRUE."    # flag to interpolate outputs on regular grid (.TRUE.)
                             # .FALSE. to get outputs on Gaussian grid
export req='p'  #req   ='p',          ! flag to select requested field file (p, s, c, e g or nothing)
                 #       !   p - use file rfd.pnt
                 #       !   s - use file rfd.sfc
                 #       !   c - use file rfd.clm
                 #       !   e - use file rfd.eta
                 #       !   g - use file rfd.ens
                 #       !     - use file rfd

cat ${NAMELISTFILEPATH}/POSTIN-GRIB | awk '{  
      if (substr($1,1,5) == "trunc")
       {
     	"echo $trunc" | getline trunc
	"echo $aspa" | getline aspa    
         printf("  trunc    =%sTQ%4.4d%s,  !TRC   : three-digit triangular truncation\n",aspa,trunc,aspa)
       }
      else if (substr($1,1,3) == "lev")
       {
     	"echo $lev" | getline lev      
	"echo $aspa" | getline aspa    
         printf("  lev     =%sL%3.3d%s,       ! vertical layers = LXXX\n",aspa,lev,aspa)
       }
      else if (substr($1,1,6) == "labeli")
       {
        "echo $aspa" | getline aspa
     	"echo $LABELI" | getline LABELI       
         printf("  labeli=%s%s%s , ! initial forecasting label (yyyymmddhh)\n",aspa,LABELI,aspa)
       }
      else if (substr($1,1,6) == "labelf")
       { 
        "echo $aspa" | getline aspa
     	"echo $LABELF" | getline LABELF
         printf("  labelf=%s%s%s , ! final forecasting label (yyyymmddhh)\n",aspa,LABELF,aspa)
       }
      else if (substr($1,1,5) == "prefx")
       { 
        "echo $aspa" | getline aspa
     	"echo $PREFX" | getline PREFX
         printf("  prefx =%s%s%s,        ! preffix for input and output files\n",aspa,PREFX,aspa)
       }
      else if (substr($1,1,3) == "req")
       { 
        "echo $aspa" | getline aspa
     	"echo $req" | getline req
         printf(" req   =%s%s%s,          ! flag to select requested field file (p, s, c, e or nothing)\n",aspa,req,aspa)
       }
      else if (substr($1,1,5) == "nfend")
       { 
     	"echo $nfend" | getline nfend
         printf("  nfend=%d,         ! number of forecasted files to be post-processed\n",nfend)
       }
      else if (substr($1,1,5) == "nffrs")
       { 
     	"echo $nffrs" | getline nffrs
         printf("  nffrs=%d,            ! value to indicated if model use or not initialization\n",nffrs)
       }
      else if (substr($1,1,5) == "nfbeg")
       { 
     	"echo $nfbeg" | getline nfbeg
         printf("  nfbeg=%d,           ! number of the first forecasted file to be post-processed\n",nfbeg)
       }
      else if (substr($1,1,3) == "res")
       { 
     	"echo $res" | getline res
         printf("  res=%8.5f,            ! if res >  0 Define output resolution (deg)\n",res)
       }
      else if (substr($1,1,3) == "ENS")
       { 
     	"echo $ENS" | getline ENS
         printf("  ENS=%s,            ! ensemble products => TRUE, only for AVN, 0?P, 0?N prefx\n",ENS)
       }
      else if (substr($1,1,8) == "RegIntIn")
       { 
     	"echo $RegIntIn" | getline RegIntIn
         printf("  RegIntIn=%s,            ! flag to interpolate outputs on regular grid (.TRUE.)\n",RegIntIn)
       }
      else if (substr($1,1,6) == "datain")
       { 
        "echo $aspa" | getline aspa
     	"echo $datain" | getline datain
         printf("  datain=%s%s%s,! main dataout directory\n",aspa,datain,aspa)
       }
      else if (substr($1,1,7) == "datalib")
       { 
        "echo $aspa" | getline aspa
     	"echo $datalib" | getline datalib
         printf("  datalib=%s%s%s,! main dataout directory\n",aspa,datalib,aspa)
       }
      else if (substr($1,1,7) == "dataout")
       { 
        "echo $aspa" | getline aspa
     	"echo $dataout" | getline dataout
         printf("  dataout=%s%s%s,! main dataout directory\n",aspa,dataout,aspa)
       }
      else
       {
     	 print $0
       }
     }'    > ${EXECFILEPATH2}/POSTIN-GRIB

 
# script invoked by mpirun

cat <<EOF1>${SCRIPTFILEPATH}
#!/bin/bash
#BSUB -cwd ${EXECFILEPATH2}
#BSUB -q par-multi
#BSUB -n ${cpu_mpi}
#BSUB -W 0:30
#BSUB -o ${EXECFILEPATH2}/Out.MPI1%J
#BSUB -e ${EXECFILEPATH2}/Out.MPI1%J

#module load openmpi/gcc/4.4.7/1.6.5

cd ${EXECFILEPATH2}
cp ${EXECFILEPATH}/PostGrib ${EXECFILEPATH2}/PostGrib
${EXECFILEPATH2}/PostGrib


EOF1
#
#   Change mode to be executable
#
chmod +x ${SCRIPTFILEPATH}
cd ${EXECFILEPATH}

${SCRIPTFILEPATH}
