#!/bin/ksh -f
# VARIAVEIS
cd ${Eta_run}
export F_SETBUF=16384
yy=`date +%y`
mm=`date +%m`
dd=`date +%d`

ymd=$yy$mm$dd
mmddhh=`echo ${Run_Date} |cut -c 5-10`
export  MPIPROGINF=ALL_DETAIL
export MPICOMMINF=ALL
export MPIMULTITASKMIX=OFF
export SERVER_GROUPS=1

#rm EGRD* latlon_* lmbc_*
###################################################################
#
#  head is used to define symbolic links.  On HP's it should be "ftn"
#  Believe most other machines (SGI's and Suns, at least) use "fort."
#
head="fort."
#
#######################

if (($#<3)); then
echo "need three arguments "
echo "first is starting time"
echo "second is ending fore hr"
echo "third is interval"
exit
fi

if (($#==3)); then
echo "if include type (latlon or lmbc) as argument 4"
echo "and a gempak file name as argument 5"
echo "the file will automatically be degribbed using GEMPAKs"
echo "nagrib program"
echo " "
echo " "
echo "if you dont use GEMPAK, ignore this"
echo " "
fi

#--------------------------------------------------------------                 
out_dir=`pwd`
export COMSP=${out_dir}/
echo $COMSP
typeset -Z6 fcsthr 
export fcsthr=$1
export fcsthr2=$1		      
export tmmark=t00s		  

tend=$2
numtimes=$2
let numtimes=${tend}-${fcsthr2}
let numtimes=$numtimes/$3
let numtimes=$numtimes+1
invar="$1 $numtimes $3"

echo starting at forecast time $1 and doing $numtimes at $3 hour intervals
sleep 2
if [ ${postout} = latlon ]; then
 echo $invar "GRIBIT" > namelist_${fcsthr}
else
 echo $invar "NOPACK" > namelist_${fcsthr}
fi

#------------------
#
# QUILT JOB
#
#------------------

checkval=`ls restrt*quilt.${tmmark} | wc -l`


if (($checkval>=1)); then
echo "quilting up"
ln -s -f cnst.file ${head}12
${Eta_exe}/quilt.x < namelist_${fcsthr} >& quilt.log
#
# rm restrt*quilt.${tmmark}
   unset F_FF12
fi

#------------------------
#
# POST JOB
#
#------------------------

rm ${head}*

ln -s -f omg                       ${head}81         
ln -s -f all                       ${head}82

#-----------------------------------------------------------------------        

echo start post
cat namelist_${fcsthr}
cat <<EOF1> ${Eta_run}/RPosSdb${fcsthr}
#!/bin/ksh
#PBS -o ${Eta_run}/EtaPos${fcsthr}.out
#PBS -l mppwidth=24
#PBS -A CPTEC
#PBS -l walltime=1:00:00
#PBS -N ${mmddhh}Sdb${fcsthr}
#PBS -j oe
#PBS -q pesq

cd ${Eta_run}
export  MPIPROGINF=ALL_DETAIL
export MPICOMMINF=ALL
export MPIMULTITASKMIX=OFF
export SERVER_GROUPS=1
export tmmark=t00s		  
export COMSP=${out_dir}/
echo $COMSP

rm -f ${head}81 ${head}82
ln -s -f omg                             ${head}81         
ln -s -f all                             ${head}82
aprun -n 24 ${Eta_exe}/etapost_new.x < ${Eta_run}/namelist_${fcsthr} > ${Eta_run}/post${fcsthr}.log
exit
EOF1

export PBS_SERVER=eslogin13
qsub ${Eta_run}/RPosSdb${fcsthr}
export PBS_SERVER=aux20-eth4

#SUBMIT PROFILE
typeset -Z6 Fct
#if (( ${fcsthr} == 0 )) ; then
if ((${fcsthr}==${Fct}-000168)) ; then
cat <<EOF> ${Eta_run}/submitprofile
#!/bin/ksh
#PBS -N prof${Run_Date}
#PBS -lselect=1:ncpus=1
#PBS -A CPTEC
#PBS -o ${Eta_run}/prof${Run_Date}
#PBS -j oe
#PBS -q pesq.q

cd ${Eta_run}
${Eta_run}/outjob_special_prof
EOF

chmod 755 ${Eta_run}/submitprofile
qsub ${Eta_run}/submitprofile
fi
#END SUBMIT PROFILE

# Test if the LATLON was created
ARQ_LATLON="False"
while [ "${ARQ_LATLON}" != "True" ] ; do

   if [ -s ${Eta_run}/latlondone${fcsthr}.t00s ] ; then
##        tam=`du -s ${Eta_run}/LATLON${fcsthr}.t00s | awk '{print $1}'`
#### O valor da variavel tam varia para cada configuracao do modelo. 
#### Portanto ele deve ser checado e alterado para cada configuracao
#### Incluir posteriormente na suite do modelo o calculo do tamanho 
#### tendo como base o numero de pontos pos-processado
##      if ((${tam} >= 68544)) ; then
#### Now extract a more friendly output grid 

# cp ../../copygb/copygb.x .

      input=EGRD3D${fcsthr}.t00s
      outputlmb=lmbc_${fcsthr}
      outputlat=latlon_${fcsthr}
      gridlat=`cat outjob_input_lat` 
      echo $gridlat

      if [ ${postout} = latlonnopack ]; then
      ${Eta_exe}/reform.x < ${Eta_run}/namelist_${fcsthr} > ${Eta_run}/postout_${fcsthr}
      ${Eta_run}/Finaliza.ksh  ${fcsthr} ${Fct} ${IntFct}
###   Incluir teste se os arquivos .bin e .ctl foram criados corretamente
###   e retornar codigo de erro que será utilizado para definir o status 
###   da variável ARQ_LATLON      
      ARQ_LATLON="True"
      fi
      
     else
      
     sleep 20
 
    fi
done

#rm -f  ${Eta_run}/LATLON${fcsthr}.t00s

exit
