#! /bin/ksh -x

#dia=`date +%d`
dia=15
#mes=`date +%m`
mes=01
#ano=`date +%Y`
ano=2022
hora=00

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

echo $Run_Date

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/DiegoChagas/GERA_FIGURAS/"

pesq=${dir_script}

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

DIRCTL=/lustre_xc50/gustavo_sueiro/Tempo_io/out/Eta1km_1.4.4_90m/binctl/${Run_Date}/${EXP}/2D
DIRCTL3D=/lustre_xc50/gustavo_sueiro/Tempo_io/out/Eta1km_1.4.4_90m/binctl/${Run_Date}/${EXP}/3D
#DIROUT=${pesq}/${Run_Date}
DIROUT=${pesq}/FIGS/${EXP}/${Run_Date}
DIR_EXEC=${dir_script}

mkdir -p ${DIR_TEMP}
mkdir -p ${DIROUT}/{PSLM,TP2M,ST2,ZGEO,TEMP,CLSF,CSSF,PREC,PRCV,PRGE,OCIS,OLIS,ROLE,TMIN,TMAX,V10M,V100,V10MOROG,TSFC}

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}_2D
ARQTMPL3D=${DIRCTL3D}/${LabRod}${Run_Date}_3D
 
 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:ARQC3D:${ARQTMPL3D}: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'

 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}/../

set -A vars PSLM TP2M ST2 ZGEO TEMP CLSF CSSF PREC PRCV PRGE OCIS OLIS ROLE TMIN TMAX V10M V100 V10MOROG TSFC
for i in ${vars[@]}
do
        cd ${DIROUT}/$i
        i2=$(echo "$i" | tr '[:upper:]' '[:lower:]')
        montage $(ls ${i2}_*.png | sort)  -crop +30+0 -crop -30-0 +repage -resize 350x350 -tile 6x -geometry +0+2 ${i}_all.png
done


exit

