#!/bin/ksh

### Diretorios
Eta15="/programas/meteoro/optempo/worketa/eta/mg/grbctl"
MERGE="/Sazonal1/corrigido/prec_obs"
dirFigura="/programas/meteoro/optempo/worketa/eta/mg/Figuras"

### Scripts e Softwares
dirCaldate="/programas/meteoro/optempo/worketa/util/caldate.3.0"

for dataRun in `find ${Eta15}/* -prune -type d -name 2014111[3-7]12 -printf '%f\n'`
do
 vetData[${#vetData[@]}]=${dataRun}
 vetM1[${#vetData[@]}]=`caldate.3.0 ${dataRun} +   0d  "yyyymmmld"`
 vetM2[${#vetData[@]}]=`caldate.3.0 ${dataRun} +  30d  "yyyymmmld"`
 vetM3[${#vetData[@]}]=`caldate.3.0 ${dataRun} +  60d  "yyyymmmld"`
 vetM4[${#vetData[@]}]=`caldate.3.0 ${dataRun} +  90d  "yyyymmmld"`
 vetM5[${#vetData[@]}]=`caldate.3.0 ${dataRun} + 120d  "yyyymmmld"`
done

cat <<EOF> Grafico_Prec_Acumul.gs

_lat='-25 -12'
_lon='-52 -39'

datas="${vetData[@]}"
datasM1="${vetM1[@]}"
datasM2="${vetM2[@]}"
datasM3="${vetM3[@]}"
datasM4="${vetM4[@]}"
datasM5="${vetM5[@]}"

_i=2
while (_i<=5)

'set display color white'
'c'

_Cont=1
while (_Cont <= ${#vetData[@]})

_data=subwrd(datas,_Cont)

'!mkdir -p ${dirFigura}'

_dd=substr(_data,7,2)
_hh=substr(_data,9,2)

M1=subwrd(datasM1,_Cont)
M2=subwrd(datasM2,_Cont)
M3=subwrd(datasM3,_Cont)
M4=subwrd(datasM4,_Cont)
M5=subwrd(datasM5,_Cont)

say ' '

_ldm.1=substr(M1,8,2)
_mes.1=substr(M1,5,3)
_ano.1=substr(M1,1,4)
say _ano.1%' '%_mes.1%' '%_ldm.1

_ldm.2=substr(M2,8,2)
_mes.2=substr(M2,5,3)
_ano.2=substr(M2,1,4)
say _ano.2%' '%_mes.2%' '%_ldm.2

_ldm.3=substr(M3,8,2)
_mes.3=substr(M3,5,3)
_ano.3=substr(M3,1,4)
say _ano.3%' '%_mes.3%' '%_ldm.3

_ldm.4=substr(M4,8,2)
_mes.4=substr(M4,5,3)
_ano.4=substr(M4,1,4)
say _ano.4%' '%_mes.4%' '%_ldm.4

_ldm.5=substr(M5,8,2)
_mes.5=substr(M5,5,3)
_ano.5=substr(M5,1,4)
say _ano.5%' '%_mes.5%' '%_ldm.5

say ''
say ''

***Dados Eta 15km
'open ${Eta15}/'%_data%'/Eta_15km'%_data%'_Diario.ctl'
'open Mascara_Bacia_3_Marias_15km.ctl'

'q files'
say result

say ''

rc = geraFigura(Prec,1,_i,mm)

'close 2'
'close 1'

_Cont=_Cont+1
endwhile

'draw ylab Precipitation (mm)'
'draw xlab Dias'

'draw title Eta 15km - CI: '%_mes.1%'/'%_ano.1%', M+'%_i-1%' ('%_mes._i%')'

'gxprint ${dirFigura}/Grafico_Unico_'%_mes._i%'_Prec_Acumulada.png x850 y700'

'reinit'


_i=_i+1
endwhile

'quit'

function geraFigura(var,z,_i,unidade)

* * Eta 15km 

'set parea 0.5 10.1 0.7 7.8'
'set mpdset brmap_hires'
'set grads off'
'set grid off'
'set xlopts 1 4 0.14'
'set ylopts 1 4 0.14'
'set clab on'
'set clopts -1 -1 0.12'
'set cthick 8'
'set ccolor 11'
'set cmark '%_Cont
'set ylpos 0 r'
'set vrange 0 400'

'set x 1'
'set y 1'
'set z 1'

'set xaxis 1 '%_ldm._i%' 1'

say 'set time 01'%_mes._i%_ano._i%' '%_ldm._i%_mes._i%_ano._i
'set time 01'%_mes._i%_ano._i%' '%_ldm._i%_mes._i%_ano._i

'q dims'
lin=sublin(result,5)
ti=subwrd(lin,11)

say 'd tloop(sum(aave(maskout('%var%'.1(z=1),lsmk.2(z=1,t=1),lon='subwrd(_lon,1)',lon='subwrd(_lon,2)',lat='subwrd(_lat,1)',lat='subwrd(_lat,2)'),t='%ti%',t+0))'
'define membro'%_Cont%' = tloop(sum(aave(maskout('%var%'.1(z=1),lsmk.2(z=1,t=1)),lon='subwrd(_lon,1)',lon='subwrd(_lon,2)',lat='subwrd(_lat,1)',lat='subwrd(_lat,2)'),t='%ti%',t+0))'
say result
'd membro'%_Cont
say result

say ' '

if (_Cont=5)
'set ccolor 2'
'set cthick 10'
'set cmark '%_Cont+1

'd (membro1+membro2+membro3+membro4+membro5)/5'

if (
'open ${MERGE}/prec_obs.ctl'
'set dfile 3'

say 'set time 01'%_mes._i%_ano._i%' '%_ldm._i%_mes._i%_ano._i
'set time 01'%_mes._i%_ano._i%' '%_ldm._i%_mes._i%_ano._i
say ''

'q dims'
lin=sublin(result,5)
ti=subwrd(lin,11)

'q files'
say result
say ' '

'set cmark '%_Cont+2
'set cthick 12'
'set ccolor 1'

'd tloop(sum(aave(maskout(lterp('%var%'.3(z=1),lsmk.2(t=1,z=1)),lsmk.2(z=1,t=1)),lon='subwrd(_lon,1)',lon='subwrd(_lon,2)',lat='subwrd(_lat,1)',lat='subwrd(_lat,2)'),t='%ti%',t+0))'

say result

'close 3'
endif

return

EOF

chmod 775 Grafico_Prec_Acumul.gs

grads -lc "run Grafico_Prec_Acumul.gs"

exit
