#!/bin/ksh 
#
#
# VARIAVEIS

cd ${Eta_run}

DIR_OUT=${Eta_binctl}
mkdir -p ${DIR_OUT}
mkdir -p ${Eta_grbctl}

NARQ=${LabRod}${Res}km_${paramet}_${Run_Date}
NARQ2=${NARQ}

typeset -Z6 Hfct

export Hfct=$1
export Fct=$2
export IntFct=$3

INTHR2=`echo ${IntPhisAcum} | cut -d. -f1`

Arqout=`${Eta_home1}/util/caldate.3.0 ${Run_Date} + ${Hfct}hr ${NARQ}'+yyyymmddhh'`
mv ${Eta_run}/latlon_${Hfct} ${DIR_OUT}/${Arqout}.bin
TIMEOUT=`${Eta_home1}/util/caldate.3.0 ${Run_Date} + 0hr 'HHZDDMMMYYYY'`
TIMEOUT2=`${Eta_home1}/util/caldate.3.0 ${Run_Date} + ${Hfct}hr 'hhZddmmmyyyy'`
TIMEOUT3=`${Eta_home1}/util/caldate.3.0 ${Run_Date} + ${Hfct}hr 'yyyymmddhh'`

cat ${Eta_ucl}/CTLTEMPLATE |sed "s/NARQ/${Arqout}.bin/g"  \
|sed "s/NTIME/1/g" \
|sed "s/LLTIME/${TIMEOUT}/g"|sed "s/INTHR/${INTHR2}hr/g" > ${DIR_OUT}/${Arqout}.ctl
chmod 755 ${DIR_OUT}/${Arqout}.*

let NTIME=${Fct}/${INTHR2}
let NTIME=${NTIME}+1
TIMEOUT=`${Eta_home1}/util/caldate.3.0 ${Run_Date} + 0hr 'HHZDDMMMYYYY'`
cat ${Eta_ucl}/CTLTEMPLATE |sed "s/NARQ/${NARQ}+%y4%m2%d2%h2.bin/g"  \
|sed "s/NTIME/${NTIME}/g" \
|sed "s/LLTIME/${TIMEOUT}/g"|sed "s/INTHR/${INTHR2}hr/g" > ${DIR_OUT}/${NARQ}.ctl

${Eta_home1}/util/grads/2.0.a9/bin/lats4d.sh -i ${Eta_binctl}/${NARQ}.ctl -o \
${Eta_grbctl}/${NARQ2}+${TIMEOUT3} -ftype ctl -format grads_grib \
-time ${TIMEOUT2} -table ${Eta_run}/cptec.table


${Eta_home1}/util/grads/2.0.a9/bin/gribmap -i ${Eta_grbctl}/${NARQ2}+${TIMEOUT3}.ctl 

typeset -Z6 Fct
if [ "${Hfct}" == "${Fct}" ] ; then

  if [ -s ${Eta_grbctl}/${NARQ2}+${Run_Date}.ctl ] ; then

    cat ${Eta_grbctl}/${NARQ2}+${Run_Date}.ctl | sed -e "s:dset ^${LabRod}${Res}km_${paramet}_${Run_Date}+${Run_Date}.grb:dset ^${LabRod}${Res}km_${paramet}_${Run_Date}+%y4%m2%d2%h2.grb:g" | \
    sed -e "s:index ^${LabRod}${Res}km_${paramet}_${Run_Date}+${Run_Date}.gmp:index ^${LabRod}${Res}km_${paramet}_${Run_Date}.idx:g" | \
    sed -e "s:tdef 1 linear:tdef ${NTIME} linear:g" | sed "/grb/{p;s/.*/options template/;}" > ${Eta_grbctl}/${NARQ2}.ctl

    ${Eta_home1}/util/grads/2.0.a9/bin/gribmap -i ${Eta_grbctl}/${NARQ2}.ctl
    chmod 755 ${Eta_grbctl}/*
#    ${Eta_home1}/eta/${Conf}/scripts_figuras/Gera_Figuras_Total.ksh ${Run_Date} ${paramet}
  fi
fi		

