#! /bin/ksh -x

dia=`date +%d`
#dia=15
mes=`date +%m`
#mes=08
ano=`date +%Y`
#ano=2021
hora=00

Run_Date=${ano}${mes}${dia}${hora}

RECURSOS="/lustre_xc50/gustavo_sueiro/Tempo/worketa/util"
DIRCALDATE="${RECURSOS}/caldate.3.0"

##tam_Rodada=`du -b /lustre_xc50/gustavo_sueiro/Tempo/worketa/eta/rjsp_01km_oper/grbctl/BMJ_FER/${Run_Date} | awk '{print $1}'`

tam_Rodada=`du -b /lustre_xc50/ioper/data/Eta/rjsp_01km/${Run_Date} | awk '{print $1}'`
##pesq="/lustre_xc50/gustavo_sueiro/Tempo/worketa/eta/rjsp_01km_oper/Pagina/Figuras/"
dir_script="/lustre_xc50/gustavo_sueiro/Tempo/worketa/eta/rjsp_01km_oper/scripts/Figuras_Pagina"

pesq=${dir_script}

while [[  ! -d  "/lustre_xc50/ioper/data/Eta/rjsp_01km/${Run_Date}"  ]]
do
echo "Dados Incompletos"
sleep 200
done

LASTBIN=`${DIRCALDATE} ${Run_Date} + 3d 'yyyymmddhh'`
while [[ ! -f "/lustre_xc50/ioper/data/Eta/rjsp_01km/${Run_Date}/GRBCTL/eta_01km_${Run_Date}+${LASTBIN}.gmp" ]]
do
echo "Dados Incompletos"
sleep 200
done


echo "Dados Completos"  
 
EDATE=$Run_Date
LabRod=eta_01km_
Res=1
Fct=72 #numero de horas da previsao (3 dias)
DIR=`pwd`
DIR_TEMP=${dir_script}/scripts_temporarios

DIRCTL=/lustre_xc50/ioper/data/Eta/rjsp_01km/${Run_Date}/GRBCTL
#DIROUT=${pesq}/${Run_Date}
DIROUT=${pesq}/${Run_Date}/Oper
DIR_EXEC=${dir_script}

mkdir -p ${DIR_TEMP}
mkdir -p ${DIROUT}

cd ${DIR_EXEC}
 typeset -Z3 FHR
 FHR=000
 TT=1
 ARQTMP=`echo "${LabRod}${Run_Date}"`
 ARQTMPL=${DIRCTL}/${ARQTMP}

 dtle=`${DIRCALDATE} ${Run_Date} + ${FHR}hr 'dd/mm/yyyy, hh'` #Muda de acordo com o num de horas. Ex: 17/01/2011, 12
 ####Variavel que armazena a data incial da previsao, esta sera usada para as preciptacoes acumuladas em 3 e 7 dias#####################
 dt_inicial=`${DIRCALDATE} ${Run_Date} + 000d 'dd/mm/yyyy, hh'` #Muda de acordo com o num de horas. Ex: 17/01/2011, 12#
 #######################################################################################################################################
 DATE=`${DIRCALDATE} ${Run_Date} + 000h 'dd/mm/yyyy hh'`

while (($FHR<=$Fct)) ; do
 dtle=`${DIRCALDATE} ${EDATE} + ${FHR}h 'dd/mm/yyyy, hh'`
 rec_dtle=`${DIRCALDATE} ${EDATE} + ${FHR}h 'yyyymmdd'`
 dtle2=`${DIRCALDATE} ${rec_dtle} - 1d 'dd/mm/yyyy'`
ARQTMPL=${DIRCTL}/${LabRod}${Run_Date}
 
 dtle3_1=`${DIRCALDATE} ${EDATE} + ${FHR}h 'yyyymmddhh'`
 dtle3=`${DIRCALDATE} ${dtle3_1} - 1d 'dd/mm/yyyy, hh'`

cat ${DIR_EXEC}/templ_4x4_zoom.gs|sed "s:TT:${TT}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:DATE:${DATE}:g"|sed "s:FHR:${FHR}:g" | sed "s:SAIDA:${DIROUT}:g" > ${DIR_TEMP}/precip1.gs
   /lustre_xc50/gustavo_sueiro/Tempo/worketa/eta/rjsp_01km_oper/scripts/Figuras_Pagina/2.0.a9/bin/grads -blc  'run '${DIR_TEMP}'/precip1.gs'

### CALCULO DOMINIO TOTAL ACUMULADO DE 24HRS (DAS 0 E DAS 12), 3 e 7 dias
case $FHR in
  024) cat ${DIR_EXEC}/templ_prec_acumul.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/24/g"|sed "s/TII/2/g"|sed "s/TFI/25/g"|sed "s:DT_ANT:${dtle3}:g"|sed "s:DTLE:${dtle}:g" | sed "s:TIT:${dtle2}:g"|sed "s:SAIDA:${DIROUT}:g"|sed '2,3d' > ${DIR_TEMP}/precip1.gs
   /lustre_xc50/gustavo_sueiro/Tempo/worketa/eta/rjsp_01km_oper/scripts/Figuras_Pagina/2.0.a9/bin/grads -blc  'run '${DIR_TEMP}'/precip1.gs';;
  036) cat ${DIR_EXEC}/templ_prec_acumul.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/36/g"|sed "s/TII/14/g"|sed "s/TFI/37/g"|sed "s:DT_ANT:${dtle3}:g"|sed "s:DTLE:${dtle}:g"| sed "s:TIT:${dtle2}:g"|sed "s:SAIDA:${DIROUT}:g" |sed '2,3d' > ${DIR_TEMP}/precip2.gs
   /lustre_xc50/gustavo_sueiro/Tempo/worketa/eta/rjsp_01km_oper/scripts/Figuras_Pagina/2.0.a9/bin/grads -blc 'run '${DIR_TEMP}'/precip2.gs';;
  048) cat ${DIR_EXEC}/templ_prec_acumul.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/48/g"|sed "s/TII/26/g"|sed "s/TFI/49/g"|sed "s:DT_ANT:${dtle3}:g"|sed "s:DTLE:${dtle}:g" | sed "s:TIT:${dtle2}:g"|sed "s:SAIDA:${DIROUT}:g" |sed '2,3d'> ${DIR_TEMP}/precip3.gs
   /lustre_xc50/gustavo_sueiro/Tempo/worketa/eta/rjsp_01km_oper/scripts/Figuras_Pagina/2.0.a9/bin/grads -blc 'run '${DIR_TEMP}'/precip3.gs';;
  060) cat ${DIR_EXEC}/templ_prec_acumul.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/60/g"|sed "s/TII/38/g"|sed "s/TFI/61/g"|sed "s:DT_ANT:${dtle3}:g"|sed "s:DTLE:${dtle}:g"|sed "s:TIT:${dtle2}:g"|sed "s:SAIDA:${DIROUT}:g" |sed '2,3d'> ${DIR_TEMP}/precip4.gs
   /lustre_xc50/gustavo_sueiro/Tempo/worketa/eta/rjsp_01km_oper/scripts/Figuras_Pagina/2.0.a9/bin/grads -blc 'run '${DIR_TEMP}'/precip4.gs';;
  072) cat ${DIR_EXEC}/templ_prec_acumul.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/72/g"|sed "s/TII/50/g"|sed "s/TFI/73/g"|sed "s:DT_ANT:${dtle3}:g"|sed "s:DTLE:${dtle}:g" | sed  "s:TIT:${dtle2}:g"|sed "s:SAIDA:${DIROUT}:g" |sed '2,3d'> ${DIR_TEMP}/precip5.gs
   /lustre_xc50/gustavo_sueiro/Tempo/worketa/eta/rjsp_01km_oper/scripts/Figuras_Pagina/2.0.a9/bin/grads -blc 'run '${DIR_TEMP}'/precip5.gs'
  cat ${DIR_EXEC}/templ_precip_3d_zoom.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/72/g"|sed "s/TII/2/g"|sed "s/TFI/73/g"|sed "s:DT_INICIAL:${dt_inicial}:g"|sed "s:DTLE:${dtle}:g"| sed "s:TIT:${dtle2}:g"|sed "s:SAIDA:${DIROUT}:g" |sed '4,5d'| sed "s:set map 1 1 6:set map 1 1 3:g" >  ${DIR_TEMP}/precip14.gs
  /lustre_xc50/gustavo_sueiro/Tempo/worketa/eta/rjsp_01km_oper/scripts/Figuras_Pagina/2.0.a9/bin/grads -blc 'run '${DIR_TEMP}'/precip14.gs';;
  084) cat ${DIR_EXEC}/templ_prec_acumul.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/84/g"|sed "s/TII/62/g"|sed "s/TFI/85/g"|sed "s:DT_ANT:${dtle3}:g"|sed "s:DTLE:${dtle}:g" | sed "s:TIT:${dtle2}:g"|sed "s:SAIDA:${DIROUT}:g" |sed '2,3d'> ${DIR_TEMP}/precip6.gs
   /lustre_xc50/gustavo_sueiro/Tempo/worketa/eta/rjsp_01km_oper/scripts/Figuras_Pagina/2.0.a9/bin/grads -blc 'run '${DIR_TEMP}'/precip6.gs';;
  096) cat ${DIR_EXEC}/templ_prec_acumul.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/96/g"|sed "s/TII/74/g"|sed "s/TFI/97/g"|sed "s:DT_ANT:${dtle3}:g"|sed "s:DTLE:${dtle}:g"| sed "s:TIT:${dtle2}:g"|sed "s:SAIDA:${DIROUT}:g" |sed '2,3d'> ${DIR_TEMP}/precip7.gs
   /cray_home/jorge_gomes/grads/2.1.a1/bin/grads -blc 'run '${DIR_TEMP}'/precip7.gs';;
  108) cat ${DIR_EXEC}/templ_prec_acumul.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/108/g"|sed "s/TII/86/g"|sed "s/TFI/109/g"|sed "s:DT_ANT:${dtle3}:g"|sed "s:DTLE:${dtle}:g"| sed "s:TIT:${dtle2}:g"|sed "s:SAIDA:${DIROUT}:g" |sed '2,3d'> ${DIR_TEMP}/precip8.gs
   /opt/grads/2.0.a9/bin/grads -blc 'run '${DIR_TEMP}'/precip8.gs';;
  120) cat ${DIR_EXEC}/templ_prec_acumul.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/120/g"|sed "s/TII/98/g"|sed "s/TFI/121/g"|sed "s:DT_ANT:${dtle3}:g"|sed "s:DTLE:${dtle}:g"| sed "s:TIT:${dtle2}:g"|sed "s:SAIDA:${DIROUT}:g" |sed '2,3d'> ${DIR_TEMP}/precip9.gs
   /opt/grads/2.0.a9/bin/grads -blc 'run '${DIR_TEMP}'/precip9.gs';;
  132) cat ${DIR_EXEC}/templ_prec_acumul.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/132/g"|sed "s/TII/110/g"|sed "s/TFI/133/g"|sed "s:DT_ANT:${dtle3}:g"|sed "s:DTLE:${dtle}:g"| sed "s:TIT:${dtle2}:g"| sed "s:SAIDA:${DIROUT}:g" |sed '2,3d'> ${DIR_TEMP}/precip10.gs
   /opt/grads/2.0.a9/bin/grads -blc 'run '${DIR_TEMP}'/precip10.gs';;
  144) cat ${DIR_EXEC}/templ_prec_acumul.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/144/g"|sed "s/TII/122/g"|sed "s/TFI/145/g"|sed "s:DT_ANT:${dtle3}:g"|sed "s:DTLE:${dtle}:g"| sed "s:TIT:${dtle2}:g"|sed "s:SAIDA:${DIROUT}:g" |sed '2,3d'> ${DIR_TEMP}/precip11.gs
   /opt/grads/2.0.a9/bin/grads -blc 'run '${DIR_TEMP}'/precip11.gs';;
  156) cat ${DIR_EXEC}/templ_prec_acumul.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/156/g"|sed "s/TII/134/g"|sed "s/TFI/157/g"|sed "s:DT_ANT:${dtle3}:g"|sed "s:DTLE:${dtle}:g"| sed "s:TIT:${dtle2}:g"|sed "s:SAIDA:${DIROUT}:g" |sed '2,3d'> ${DIR_TEMP}/precip12.gs
   /opt/grads/2.0.a9/bin/grads -blc 'run '${DIR_TEMP}'/precip12.gs';;
  168) cat ${DIR_EXEC}/templ_prec_acumul.gs|sed "s:ARQCTL:${ARQTMPL}:g"|sed "s:RECURSOS:${RECURSOS}:g"|sed "s/DATE/$EDATE/g"|sed "s/HP/168/g"|sed "s/TII/146/g"|sed "s/TFI/169/g"|sed "s:DT_ANT:${dtle3}:g"|sed "s:DTLE:${dtle}:g"| sed "s:TIT:${dtle2}:g"|sed "s:SAIDA:${DIROUT}:g" |sed '2,3d'> ${DIR_TEMP}/precip13.gs
   /opt/grads/2.0.a9/bin/grads -blc 'run '${DIR_TEMP}'/precip13.gs';;
  esac

 echo $FHR
let TT=${TT}+1
let FHR=${FHR}+1
done

rm -rf ${DIR_TEMP}
data_Remove=`${DIRCALDATE} ${Run_Date} - 2d 'yyyymmddhh'`
rm -rf ${pesq}/${data_Remove}

chmod -R 755 ${DIROUT}/../

exit

