#!/bin/ksh -f
# VARIAVEIS
cd ${Eta_run}
export F_SETBUF=16384

mmddhh=`echo ${Run_Date} |cut -c 5-10`

#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
			      
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
#
#------------------

if [ -s restrt${fcsthr}.quilt.${tmmark} ] ; then
echo "quilting up"
ln -s -f cnst.file ${head}12
${Eta_exe}/quilt.x < namelist_${fcsthr} > quilt.log
#
# rm restrt*quilt.${tmmark}
fi

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

echo start post
cat namelist_${fcsthr}
cat <<EOF1> ${Eta_run}/RPosSdb${fcsthr}
#!/bin/ksh

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
${Eta_exe}/etapost_new.x < ${Eta_run}/namelist_${fcsthr} > ${Eta_run}/post${fcsthr}.log
exit
EOF1

chmod 755 ${Eta_run}/RPosSdb${fcsthr}
${Eta_run}/RPosSdb${fcsthr}

#SUBMIT PROFILE
#typeset -Z6 Fct
#if (( ${fcsthr}==${Fct} )) ; then

#cd ${Eta_run}
#${Eta_run}/outjob_special_prof > ${Eta_run}/profile.out

#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

#### Now extract a more friendly output grid 

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

     gridlat=`cat outjob_input_lat`
     echo $gridlat

     input=EGRD3D${fcsthr}.t00s
     outputlmb=lmbc_${fcsthr}
     outputlat=latlon_${fcsthr}
 
     if [ ${postout} = latlonnopack ]; then
      ${Eta_exe}/reform.x < namelist_${fcsthr} > 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 10  
#  fi
 else
   sleep 20
 fi 
done

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

exit                                                                            
