#! /usr/bin/ksh

### Diretorios
Eta15="/Sazonal1/CEMIG"
dirFigura="/Sazonal1/Figuras/Eta_15km"

### Scripts e Softwares
dirCaldate="/Sazonal1/bin/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> Plota_Prec_Acumul_Mensal.gs

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

_map='15 1 6'
_cbarn='1 0 4.25 0.35'
_clevs='10 50 100 150 200 250 300 350 400 450 500'
_ccols='34 17 18 19 20 21 22 23 24 25 26 27'

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

i=2
while (i<=5)

'set rgb 17 130 200 255'
'set rgb 18 255 150  50'
'set rgb 40  30  35 244'
'set rgb 46  84  98 255'
'set rgb 50 120 140 255'
'set rgb 55 162 189 255'
'set rgb 59 198 231 255'

* TONS de AZUL
'set rgb 17 235 240 255 255'
'set rgb 18 214 224 255 255'
'set rgb 19 194 209 255 255'
'set rgb 20 173 194 255 255'
'set rgb 21 153 178 255 255'
'set rgb 22 133 163 255 255'
'set rgb 23 112 148 255 255'
'set rgb 24  92 133 255 255'
'set rgb 25  71 117 255 255'
'set rgb 26  51 102 255 255'
'set rgb 27  46  92 230 255'
'set rgb 28  41  82 204 255'
'set rgb 29  36  71 178 255'
'set rgb 30  31  61 153 255'
'set rgb 31  26  51 128 255'
'set rgb 32  20  41 102 255'
'set rgb 33  15  31  76 255'
'set rgb 34 255 255 255 255'

'set rgb 35   0   0  85 255'
'set rgb 36 102  20   0 255'
'set rgb 37   0 127   0 255'

'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%'/etasa15km'%_data%'.ctl'

'q files'
say result

say ''

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

'close 1'

_Cont=_Cont+1
endwhile

'reinit'

i=i+1
endwhile

'quit'

function geraFigura(var,z,i,unidade)

* * Eta 15km 
'set parea 0.5 8.3 0.9 10.3'
'set mproj scaled'
'set mpdset brmap_hires'
'set map '%_map
'set grads off'
'set grid off'
'set xlopts 1 4 0.14'
'set ylopts 1 4 0.14'
'set clab off'
'set clopts -1 -1 0.12'
'set gxout contour'
'set clevs '%_clevs

'set t 1'

say 'define membro'%_Cont%' = sum('%var%'.1(z=1),time=00z01'%_mes.i%_ano.i%',time=18z'%_ldm.i%_mes.i%_ano.i')'
'define membro'%_Cont%' = sum('%var%'.1(z=1),time=00z01'%_mes.i%_ano.i%',time=18z'%_ldm.i%_mes.i%_ano.i')*1000'

say result
say ' '

if (_Cont=5)

'define media=(membro1+membro2+membro3+membro4+membro5)/5'

'set gxout shaded'
'set clevs '%_clevs
'set ccols '%_ccols

'd media'
'cbarn '%_cbarn

'set gxout contour'
'set cthick 1'
'set ccolor 1'
'set clevs '%_clevs
'd media'

* * Sao Francisco
'set line 35 1 12'
*'set shpopts 7'
'draw shp Bacias/Bacia_4'

* * Tocantins
'set line 36 1 12'
*'set shpopts 8'
'draw shp Bacias/Bacia_2'

* * Rio Grande
'set line 37 1 12'
*'set shpopts 3'
'draw shp Bacias/Sub_bacias_Parana 39'

* * Tres Marias
'set line 2 1 12'
*'set shpopts 2'
'draw shp Bacias/Sub_bacias 73'

* * Parnaiba
'set line 14 1 12'
*'set shpopts 14'
'draw shp Bacias/Sub_bacias_Parana 26'

'draw title Precipitacao Acumuldada (mm)\Eta 15km - CI: '%_mes.1%'/'%_ano.1%', M+'%i-1%' ('%_mes.i%')'

'gxprint ${dirFigura}/Prec_Acumul_Mensal_'%_mes.i%'_Media_5_Membros.png x700 y850'

'c'

'set lat 'subwrd(_lat,1)' 'subwrd(_lat,2)
'set lon 'subwrd(_lon,1)' 'subwrd(_lon,2)

'set parea 0.5 8.3 0.9 10.3'
'set mproj scaled'
'set mpdset brmap_hires'
'set map '%_map
'set grads off'
'set grid off'
'set xlopts 1 4 0.14'
'set ylopts 1 4 0.14'
'set clab off'
'set clopts -1 -1 0.12'

'set t 1'

'set gxout shaded'
'set clevs '%_clevs
'set ccols '%_ccols

'd media'
'cbarn '%_cbarn

'set gxout contour'
'set cthick 1'
'set ccolor 1'
'set clevs '%_clevs
'd media'

* * Tres Marias
'set line 2 1 12'
*'set shpopts 2'
'draw shp Bacias/Sub_bacias 73'

'draw title Precipitacao Acumuldada (mm)\Eta 15km - CI: '%_mes.1%'/'%_ano.1%', M+'%i-1%' ('%_mes.i%')'

'gxprint ${dirFigura}/Prec_Acumul_Mensal_'%_mes.i%'_Media_5_Membros_Zoom.png x700 y850'

endif

return

EOF

chmod 775 Plota_Prec_Acumul_Mensal.gs

grads -pc "run Plota_Prec_Acumul_Mensal.gs"

exit
