#!/bin/ksh  -x

# VARIAVEIS

Recortes=1
Prec_acum=1
#mem=$3


DIR=/home/ibituruna/Modelo_Eta/worketa/eta/Eta_40km_New
datarun=${DIR}/${Run_Date}Eta_40km_BMJ_FER
binctl=${DIR}/binctl/BMJ_FER
grbctl=${DIR}/grbctl/BMJ_FER
dir_prod=${DIR}/produtos
grads=/usr/local/lib/Grads_2.1/Contents
datai=`${Eta_home}/util/caldate.3.0 ${Run_Date} + 0d 'yyyymmdd'`
hh=`${Eta_home}/util/caldate.3.0 ${Run_Date} + ${Fct}h 'hh'`
hst_name=CNTRL
dir_date=${dir_prod}/recortes/${Run_Date}

size_bin=24778368
size_ctl=3371

mkdir -p ${dir_date}


############################################ FAZ RECORTE PARA O ONS ########################################## 
if  [ $Recortes -eq 1 ] ; then
#   	 "INTEGRANDO - $Run_Date"
 cc=0
 rm -f ${datarun}/submet_rec.ksh 
#  while [ $cc -le 20 ] ; do                            
   if [ -s ${datarun}/submet_rec.ksh ] ; then  
     break                                            
   else                                               
cat << Eof >${datarun}/submet_rec.ksh
#!/bin/ksh -x
cd ${datarun}
typeset -Z3 fct
ti=\$(date +"%s %Y/%m/%d %H:%M:%S") # TEMPO INICIAL
export fcsthr=000000
echo \$fcsthr

par=" -center "cptec" -format "grads_grib" -ftype ctl -table ${DIR}/grb/cptec.table -v -q "
arq[0]="-vars pslm pslc tgsc tgrz ussl uzrs zgeo uvel vvel temp umrl -levs 1000 925 900 850 800 750 700 650 600 550 500 450 400 350 300 250 200 150 100"
arq[1]="-vars pslm  pslc tp2m dp2m u10m v10m prec prcv clsf cssf ghfl tsfc qsfc tgsc tgrz ussl uzrs rnof cbnt ocis oles role albe cape bli agpl tppp fzht fzrh mxwp pcbs pctp zgeo uvel vvel temp umrl omeg umes -levs 1000 925 850 700 600 500 400 300 250 200 150 100 -lat -44.0 -19.0 -lon -69.0 -40.0 "
arq[2]="-vars pslm  pslc tp2m dp2m u10m v10m prec prcv clsf cssf ghfl tsfc qsfc tgsc tgrz ussl uzrs rnof cbnt ocis oles role albe cape bli agpl tppp fzht fzrh mxwp pcbs pctp zgeo uvel vvel temp umrl omeg umes -levs 1000 925 850 700 600 500 400 300 250 200 150 100 -lat -29.0  -1.0 -lon -64.0 -40.0 "
arq[3]="-vars pslm  pslc tp2m dp2m u10m v10m prec prcv clsf cssf ghfl tsfc qsfc tgsc tgrz ussl uzrs rnof cbnt ocis oles role albe cape bli agpl tppp fzht fzrh mxwp pcbs pctp zgeo uvel vvel temp umrl omeg umes -levs 1000 925 850 700 600 500 400 300 250 200 150 100 -lat -32.0 -10.0 -lon -58.0 -32.0 "
arq[4]="-vars pslm pslc tp2m dp2m u10m v10m prec role "
arq[5]="-vars pslm pslc topo tgsc tgrz ussl uzrs prec zgeo uvel vvel temp umrl -levs  1000 925 900 850 800 750 700 650 600 550 500 450 400 350 300 250 200 150 100 50"
arq[6]="-vars pslm pslc tp2m dp2m u10m v10m prec prcv prge neve tsfc qsfc lwnv mdnv hinv cbnt cape cine bli agpl fzht fzrh mxwp zgeo uvel vvel temp umrl omeg umes psat -levs  1000 925 900 850 800 750 700 650 600 550 500 450 400 350 300 250 200 150 100 "
arq[7]="-vars prec "

name=(onsll40ganl sulll40ganl coll40ganl sell40ganl supll40ganl cpteta svll40ganl prec)
nf=264
while ((\${fcsthr}<=\${nf})) ; do
 export fct=\${fcsthr}
# export datafct=\``echo date -d \\"${datai} $hh:00 \\${fcsthr} hours\\" +\\"%Y%m%d%H\\"`\`
 export datafct=\``echo ${Eta_home}/util/caldate.3.0 ${Run_Date} + \\${fcsthr}h "yyyymmddhh"`\`
 export LANG=us
# export datactl=\``echo date -d \\"${datai} $hh:00 \\${fcsthr} hours\\" +\\"%H"Z"%d%b%Y\\"`\`
 export datactl=\``echo ${Eta_home}/util/caldate.3.0 ${Run_Date} + \\${fcsthr}h "hhzddmmmyyyy"`\`
 export fct_m6=\$((\$fcsthr-5))
# export data_m6=\``echo date -d \\"${datai} $hh:00 \\${fct_m6} hours\\" +\\"%H"Z"%d%b%Y\\"`\`
 export data_m6=\``echo ${Eta_home}/util/caldate.3.0 ${Run_Date} + \\${fct_m6}h "hhzddmmmyyyy"`\`
 export arqin_templ=${binctl}/${Run_Date}/${LabRod}${Res}km_${paramet}_${Run_Date}
 
bin=\$(ls -ltr ${binctl}/${Run_Date}/${LabRod}${Res}km_${paramet}_${Run_Date}+\${datafct}.bin| grep $size_bin |wc -l |  awk '{print \$1}')
ctl=\$(ls -ltr ${binctl}/${Run_Date}/${LabRod}${Res}km_${paramet}_${Run_Date}+\${datafct}.ctl| grep $size_ctl |wc -l |  awk '{print \$1}')    
 if [ \$bin -eq 1 ] && [ \$ctl -eq 1 ] ; then 
    j=0
                                                            
    while((\$j<8)) ; do 
      mkdir -p ${dir_date}/\${name[\$j]}
      arq_in=${binctl}/${Run_Date}/${LabRod}${Res}km_${paramet}_${Run_Date}+\${datafct}
      echo \$arq_in                                                          
      arq_out=\${name[\$j]}${hst_name}${Run_Date}+\${datafct}
      
      
      if [  \${name[\$j]} =  prec ] ;then     
         $grads/lats4d.sh "-i \${arqin_templ}.ctl -o ${dir_date}/\${name[\$j]}/\${arq_out} \${par} \${arq[\$j]} -func sum(prec,time=\${data_m6},time=\${datactl}) -time \${datactl}"
	 $grads/gribmap -i  ${dir_date}/\${name[\$j]}/\${arq_out}.ctl
      
      elif [  \${name[\$j]} =  cpteta ] ;then
           mv ${dir_date}/\${name[\$j]}/\${arq_out}.grb ${dir_date}/\${name[\$j]}/\${name[\$j]}${Run_Date}.pgrbf0\${fct}.grb
           cat ${dir_date}/\${name[\$j]}/\${arq_out}.ctl |sed "s@\${arq_out}.grb@\${name[\$j]}${Run_Date}.pgrbf0\${fct}.grb@g"  |sed "s@\${arq_out}.gmp@\${name[\$j]}${Run_Date}.pgrbf0\${fct}.gmp@g" > ${dir_date}/\${name[\$j]}/\${name[\$j]}${Run_Date}.pgrbf0\${fct}.ctl
           mv ${dir_date}/\${name[\$j]}/\${arq_out}.gmp ${dir_date}/\${name[\$j]}/\${name[\$j]}${Run_Date}.pgrbf0\${fct}.gmp
           rm -f ${dir_date}/\${name[\$j]}/\${arq_out}.ctl
      
      else        
      	   $grads/lats4d.sh -i \${arq_in}.ctl -o ${dir_date}/\${name[\$j]}/\${arq_out} \${par} \${arq[\$j]} -time \${datactl}
           $grads/gribmap -i  ${dir_date}/\${name[\$j]}/\${arq_out}.ctl
      
      fi
      
      if [ \${fcsthr} -eq  264 ] ; then
           cat   ${dir_date}/\${name[\$j]}/\${name[\$j]}${hst_name}${Run_Date}+${Run_Date}.ctl  |sed "s@+${Run_Date}.grb@+%y4%m2%d2%h2.grb@g"|sed "s@+${Run_Date}.gmp@.gmp@g"  | sed  "s@tdef 1 linear@tdef   45 linear@g" | sed "s@1hr@06hr@g" | sed "s@title File@options template@g" > ${dir_date}/\${name[\$j]}/\${name[\$j]}${hst_name}${Run_Date}.ctl
           $grads/gribmap -i   ${dir_date}/\${name[\$j]}/\${name[\$j]}${hst_name}${Run_Date}.ctl
      
      fi
   
      j=\$((\$j+1)) 
   done                                       
    fcsthr=\$((\$fcsthr+6))                                                                                 
 else                                                                                                        
     sleep 10                                                                                                
 fi                                                                                                          
done

date +"${datarun}/submet_rec.ksh @ aux 1 @ ini \$ti @ fim %s %Y/%m/%d %H:%M:%S" >> ${dir_prod}/${Run_Date}.txt
exit
Eof
   fi
#   cc=$(($cc+1))
# done
  chmod 755 ${datarun}/submet_rec.ksh
  ${datarun}/submet_rec.ksh
fi

############################################################################################################## 
#  smslabel RECORTES               "INTEGRANDO - $data"
######################################  CALCULA A PRECIPITACAO ACUMULADA EM 24 HORAS   #######################

if [ $Prec_acum -eq 1 ]  ; then
##################################################### 
cc=0
#  smslabel PREC_ACUM              "INTEGRANDO - $data"

   mkdir -p ${dir_date}/prec_acum                                                                                          
   rm -f ${datarun}/submet_prec_acum.ksh                 
                                               
cat << Eot > ${datarun}/submet_prec_acum.ksh
#!/bin/ksh -x
typeset -Z4 bb
cd ${datarun}
datatmp=$datai
ti=\$(date +"%s %Y/%m/%d %H:%M:%S") # TEMPO INICIAL

yr=`echo $Run_Date |cut -c 3-4` 
hr=`echo $Run_Date |cut -c 9-10`
dd=`echo $Run_Date |cut -c7-8`
mm=`echo $Run_Date |cut -c5-6` 
if [ \$hr -eq 00 ] ; then
 bb=36
else
 bb=24
fi

	
j=1

if [ \$hr -eq 00 ] ; then
	
while((\$j<=10)) ; do 
 k=1
 tt=13
 nfim=24
 tinc=1
 
 aux=\``echo date -d \\"\\$datatmp 12:00 24 hours\\" +\\"%d%m%y\\"`\`
 echo ${dir_date}/prec_acum/p\${dd}\${mm}\${yr}a\${aux}.dat   > fctlist_\${j}
 echo ${dir_date}/prec_acum/pp${datai}_\${bb}.bin >> fctlist_\${j}
 echo ${dir_date}/prec_acum/p\${dd}\${mm}\${yr}a\${aux}.dat   
 echo ${dir_date}/prec_acum/pp${datai}_\${bb}.bin 
 datactl=\``echo date -d \\"${datai} \\$hr:00 \\${bb} hours\\" +\\"%H"Z"%d%b%Y\\"`\` 
 while((\$k<=\$nfim)) ; do
   ddate=\``echo date -d \\"\\${datatmp}  00:00 \\${tt} hours\\" +\\"%Y%m%d%H\\"`\`
   ddate2=\``echo date -d \\"\\${datatmp} 00:00 \\${tt} hours\\" +\\"%Y%m%d\\"`\`
   echo ${binctl}/${Run_Date}/${LabRod}${Res}km_${paramet}_${Run_Date}+\${ddate}.bin >> fctlist_\${j}
   echo ${binctl}/${Run_Date}/${LabRod}${Res}km_${paramet}_${Run_Date}+\${ddate}.bin 
   tt=\$((\${tt}+\$tinc)) 
   k=\$((\${k}+1))
 done
 cp fctlist_\${j} fctlist
 ${DIR}/grb/onsprec.exe 
 cat ${DIR}/grb/PREC_ACUM.ctl |sed "s/_PP_/pp${datai}_\${bb}.bin/g"|sed "s/DATACTL/\$datactl/g" >${dir_date}/prec_acum/pp${datai}_\${bb}.ctl
 j=\$((\${j}+1))
 datatmp=\``echo date -d \\"\\${ddate2} 00:00 \$tt hours\\" +\\"%Y%m%d\\"`\`
 bb=\$((\${bb}+24))
done


else
	
while((\$j<=11)) ; do 
 k=1
 tt=13
 nfim=24
 tinc=1
 
 aux=\``echo date -d \\"\\$datatmp 12:00 24 hours\\" +\\"%d%m%y\\"`\`
 echo ${dir_date}/prec_acum/p\${dd}\${mm}\${yr}a\${aux}.dat   > fctlist_\${j}
 echo ${dir_date}/prec_acum/pp${datai}_\${bb}.bin >> fctlist_\${j}
 echo ${dir_date}/prec_acum/p\${dd}\${mm}\${yr}a\${aux}.dat   
 echo ${dir_date}/prec_acum/pp${datai}_\${bb}.bin 
 datactl=\``echo date -d \\"${datai} \\$hr:00 \\${bb} hours\\" +\\"%H"Z"%d%b%Y\\"`\` 
 while((\$k<=\$nfim)) ; do
   ddate=\``echo date -d \\"\\${datatmp}  00:00 \\${tt} hours\\" +\\"%Y%m%d%H\\"`\`
   ddate2=\``echo date -d \\"\\${datatmp} 00:00 \\${tt} hours\\" +\\"%Y%m%d\\"`\`
   echo ${binctl}/${Run_Date}/${LabRod}${Res}km_${paramet}_${Run_Date}+\${ddate}.bin >> fctlist_\${j}
   echo ${binctl}/${Run_Date}/${LabRod}${Res}km_${paramet}_${Run_Date}+\${ddate}.bin 
   tt=\$((\${tt}+\$tinc)) 
   k=\$((\${k}+1))
 done
 cp fctlist_\${j} fctlist
 ${DIR}/grb/onsprec.exe 
 cat ${DIR}/grb/PREC_ACUM.ctl |sed "s/_PP_/pp${datai}_\${bb}.bin/g"|sed "s/DATACTL/\$datactl/g" >${dir_date}/prec_acum/pp${datai}_\${bb}.ctl
 j=\$((\${j}+1))
 datatmp=\``echo date -d \\"\\${ddate2} 00:00 \$tt hours\\" +\\"%Y%m%d\\"`\`
 bb=\$((\${bb}+24))
done

fi	
	
date +"${datarun}/submet_prec_acum.ksh @ aux 1 @ ini \$ti @ fim %s %Y/%m/%d %H:%M:%S" >> ${dir_prod}/${Run_Date}.txt
Eot
chmod 755 ${datarun}/submet_prec_acum.ksh
${datarun}/submet_prec_acum.ksh	

fi
########################################################################################################### 

