#!/bin/ksh

#####################################################################################
#                                                                                   #
# Verifica os arquivos gribs da rodada                                              #
#                                                                                   #
#####################################################################################

data=${1}

dia1=`echo ${data} | cut -c7-8`

cam2=/scratchin/grupos/eta/home/etaclim/sazonal/worketa/eta/227X351X38_oper_kieee/scripts/erros

cam3=/scratchout/grupos/eta/home/etaclim/sazonal/worketa/eta/227X351X38_oper_kieee/${data}Etasa40km

cam=/stornext/online1/eta/etaclim/climatologia_nova/BMJ_ZHAO/${data}

typeset -Z6 tempo

ldm=`echo ${data} | cut -c7-8`
ldm1=`../../../util/caldate.3.0 ${data} + 0d   "ld"`
ldm2=`../../../util/caldate.3.0 ${data} + 30d  "ld"`
ldm3=`../../../util/caldate.3.0 ${data} + 60d  "ld"`
ldm4=`../../../util/caldate.3.0 ${data} + 90d  "ld"`
ldm5=`../../../util/caldate.3.0 ${data} + 120d "ld"`

let diai=${ldm1}-${ldm}
let Ndias=${diai}+${ldm2}+${ldm3}+${ldm4}+${ldm5}
let Nfct=${Ndias}*24
let Fct=${Nfct}

tempo=0
while (( ${tempo} <= ${Fct} )) ; do

ano=`${cam2}/../../../../util/caldate.3.0 ${data} + ${tempo}hr 'yyyy'`
mes=`${cam2}/../../../../util/caldate.3.0 ${data} + ${tempo}hr 'mm'`
dia=`${cam2}/../../../../util/caldate.3.0 ${data} + ${tempo}hr 'dd'`
hora=`${cam2}/../../../../util/caldate.3.0 ${data} + ${tempo}hr 'hh'`

echo $ano$mes$dia$hora

if [ -s ${cam}/etasa40km${data}+${ano}${mes}${dia}${hora}.ctl ] && [ -s ${cam}/etasa40km${data}+${ano}${mes}${dia}${hora}.gmp ]  && [ -s ${cam}/etasa40km${data}+${ano}${mes}${dia}${hora}.grb ] ; then

tam=`du -b ${cam}/etasa40km${data}+${ano}${mes}${dia}${hora}.grb | awk '{print $1}'`
  if ((${tam} < 17582622)) ; then
     echo "A HORA $ano$mes$dia$hora ${tempo} ESTA ERRADA" >> ${cam2}/teste_err_${data}.txt
  fi

else

echo "A HORA2 $ano$mes$dia$hora ${tempo} NAO EXISTE" >> ${cam2}/teste_err2_${data}.txt

fi

if [ ! -s ${cam3}/postout_${tempo} ] ; then

echo "A HORA3 $ano$mes$dia$hora ${tempo} NAO EXISTE" >> ${cam2}/teste_${data}.txt

fi

let tempo=$tempo+6
done

if [ ! -s ${cam2}/teste_${data}.txt ] && [ ! -s ${cam2}/teste_err_${data}.txt ] && [ ! -s ${cam2}/teste_err2_${data}.txt ] ; then

mkdir -p ${cam}/../prof/${data}
cp ${cam3}/HST* ${cam}/../prof/${data}
cp ${cam3}/prof* ${cam}/../prof/${data}

${cam2}/../media_lendo_bin2.csh ${data}

tam2=`du -b ${cam}/../prof/${data}/HST${data}.bin | awk '{print $1}'`
if (( "${dia1}" == "13" )) ; then
tam3=138730020
elif (( "${dia1}" == "14" )) ; then
tam3=137717700
elif (( "${dia1}" == "15" )) ; then
tam3=136705380
elif (( "${dia1}" == "16" )) ; then
tam3=135693060
elif (( "${dia1}" == "17" )) ; then
tam3=134680740
fi

  if ((${tam2} < ${tam3})) ; then
     	echo "A DATA $data ESTA ERRADA" >> ${cam2}/HST_err_${data}.txt
  fi	
fi		
exit
