#!/bin/ksh

rodada=${1}
##experimentos="E13 E23 C20 C30 E03"
experimentos=${2}
versao="SZN"

cam_grads=/opt/grads/2.1.a1/bin
Eta_cald=/dados/grpeta/Team/Gustavo/scripts_rodada_sazonal
Eta_binctl=/dados/grpeta/S2S/rodadas_sazonal_xc50/netcdf
bin_Mswep=/dados/grpeta/Team/Priscila/PCAPES/dados/mswep/diario
bin_Era5=/dados/grpeta/S2S/rodadas_sazonal_xc50/ERA5

ano=$(${Eta_cald}/caldate.3.0 ${rodada} + 1mo "yyyy")
mes1=$(${Eta_cald}/caldate.3.0 ${rodada} + 1dias "mmm")
mes2=$(${Eta_cald}/caldate.3.0 ${rodada} + 32dias "mmm")
mes3=$(${Eta_cald}/caldate.3.0 ${rodada} + 62dias "mmm")
mes4=$(${Eta_cald}/caldate.3.0 ${rodada} + 92dias "mmm")
mes5=$(${Eta_cald}/caldate.3.0 ${rodada} + 122dias "mmm")
mes6=$(${Eta_cald}/caldate.3.0 ${rodada} + 152dias "mmm")
mes7=$(${Eta_cald}/caldate.3.0 ${rodada} + 182dias "mmm")

mesa1=$(${Eta_cald}/caldate.3.0 ${rodada} + 1dias "mmm")1
mesa2=$(${Eta_cald}/caldate.3.0 ${rodada} + 32dias "mmm")1
mesa3=$(${Eta_cald}/caldate.3.0 ${rodada} + 62dias "mmm")1
mesa4=$(${Eta_cald}/caldate.3.0 ${rodada} + 92dias "mmm")1
mesa5=$(${Eta_cald}/caldate.3.0 ${rodada} + 122dias "mmm")1
mesa6=$(${Eta_cald}/caldate.3.0 ${rodada} + 152dias "mmm")1
mesa7=$(${Eta_cald}/caldate.3.0 ${rodada} + 182dias "mmm")1

mesb1=$(${Eta_cald}/caldate.3.0 ${rodada} + 1dias "mmm")2
mesb2=$(${Eta_cald}/caldate.3.0 ${rodada} + 32dias "mmm")2
mesb3=$(${Eta_cald}/caldate.3.0 ${rodada} + 62dias "mmm")2
mesb4=$(${Eta_cald}/caldate.3.0 ${rodada} + 92dias "mmm")2
mesb5=$(${Eta_cald}/caldate.3.0 ${rodada} + 122dias "mmm")2
mesb6=$(${Eta_cald}/caldate.3.0 ${rodada} + 152dias "mmm")2
mesb7=$(${Eta_cald}/caldate.3.0 ${rodada} + 182dias "mmm")2

mesc1=$(${Eta_cald}/caldate.3.0 ${rodada} + 1dias "mmm")3
mesc2=$(${Eta_cald}/caldate.3.0 ${rodada} + 32dias "mmm")3
mesc3=$(${Eta_cald}/caldate.3.0 ${rodada} + 62dias "mmm")3
mesc4=$(${Eta_cald}/caldate.3.0 ${rodada} + 92dias "mmm")3
mesc5=$(${Eta_cald}/caldate.3.0 ${rodada} + 122dias "mmm")3
mesc6=$(${Eta_cald}/caldate.3.0 ${rodada} + 152dias "mmm")3
mesc7=$(${Eta_cald}/caldate.3.0 ${rodada} + 182dias "mmm")3

mesd1=$(${Eta_cald}/caldate.3.0 ${rodada} + 1dias "mmm")4
mesd2=$(${Eta_cald}/caldate.3.0 ${rodada} + 32dias "mmm")4
mesd3=$(${Eta_cald}/caldate.3.0 ${rodada} + 62dias "mmm")4
mesd4=$(${Eta_cald}/caldate.3.0 ${rodada} + 92dias "mmm")4
mesd5=$(${Eta_cald}/caldate.3.0 ${rodada} + 122dias "mmm")4
mesd6=$(${Eta_cald}/caldate.3.0 ${rodada} + 152dias "mmm")4
mesd7=$(${Eta_cald}/caldate.3.0 ${rodada} + 182dias "mmm")4

mese1=$(${Eta_cald}/caldate.3.0 ${rodada} + 1dias "mmm")5
mese2=$(${Eta_cald}/caldate.3.0 ${rodada} + 32dias "mmm")5
mese3=$(${Eta_cald}/caldate.3.0 ${rodada} + 62dias "mmm")5
mese4=$(${Eta_cald}/caldate.3.0 ${rodada} + 92dias "mmm")5
mese5=$(${Eta_cald}/caldate.3.0 ${rodada} + 122dias "mmm")5
mese6=$(${Eta_cald}/caldate.3.0 ${rodada} + 152dias "mmm")5
mese7=$(${Eta_cald}/caldate.3.0 ${rodada} + 182dias "mmm")5

if [ $mes1 == "Jan" ];then; fdays="28 31 30 31 30 31"; label="FMAMJJ";fi
if [ $mes1 == "Feb" ];then; fdays="31 30 31 30 31 31"; label="MAMJJA";fi
if [ $mes1 == "Mar" ];then; fdays="30 31 30 31 31 30"; label="AMJJAS";fi
if [ $mes1 == "Apr" ];then; fdays="31 30 31 31 30 31"; label="MJJASO";fi
if [ $mes1 == "May" ];then; fdays="30 31 31 30 31 30"; label="JJASON";fi
if [ $mes1 == "Jun" ];then; fdays="31 31 30 31 30 31"; label="JASOND";fi
if [ $mes1 == "Jul" ];then; fdays="31 30 31 30 31 31"; label="ASONDJ";fi
if [ $mes1 == "Aug" ];then; fdays="30 31 30 31 31 28"; label="SONDJF";fi
if [ $mes1 == "Sep" ];then; fdays="31 30 31 31 28 31"; label="ONDJFM";fi
if [ $mes1 == "Oct" ];then; fdays="30 31 31 28 31 30"; label="NDJFMA";fi
if [ $mes1 == "Nov" ];then; fdays="31 31 28 31 30 31"; label="DJFMAM";fi
if [ $mes1 == "Dec" ];then; fdays="31 28 31 30 31 30"; label="JFMAMJ";fi

anoi="${ano} ${ano} ${ano} ${ano} ${ano} ${ano}"
ano2=$(($ano + 1))

# #  PARA ANOS BISSEXTOS # #
if [ $mes1 == "Jan" ] && [ $ano%4 -eq 0  ];then; fdays="29 31 30 31 30 31"; label="FMAMJJ";fi
if [ $mes1 == "Aug" ] && [ $ano2%4 -eq 0 ];then; fdays="30 31 30 31 31 29"; label="SONDJF";fi
if [ $mes1 == "Sep" ] && [ $ano2%4 -eq 0 ];then; fdays="31 30 31 31 29 31"; label="ONDJFM";fi
if [ $mes1 == "Oct" ] && [ $ano2%4 -eq 0 ];then; fdays="30 31 31 29 31 30"; label="NDJFMA";fi
if [ $mes1 == "Nov" ] && [ $ano2%4 -eq 0 ];then; fdays="31 31 29 31 30 31"; label="DJFMAM";fi
if [ $mes1 == "Dec" ] && [ $ano2%4 -eq 0 ];then; fdays="31 29 31 30 31 30"; label="JFMAMJ";fi

if [ $mes1 == "Aug" ];then; anoi="${ano} ${ano} ${ano} ${ano} ${ano2} ${ano2} ";fi
if [ $mes1 == "Sep" ];then; anoi="${ano} ${ano} ${ano} ${ano2} ${ano2} ${ano2} ";fi
if [ $mes1 == "Oct" ];then; anoi="${ano} ${ano} ${ano2} ${ano2} ${ano2} ${ano2} ";fi
if [ $mes1 == "Nov" ];then; anoi="${ano} ${ano2} ${ano2} ${ano2} ${ano2} ${ano2}";fi
if [ $mes1 == "Dec" ];then; anoi="${ano2} ${ano2} ${ano2} ${ano2} ${ano2} ${ano2}";fi

fmonth=''$mes2' '$mes3' '$mes4' '$mes5' '$mes6' '$mes7''
fmonth1=''$mesa2' '$mesa3' '$mesa4' '$mesa5' '$mesa6' '$mesa7''
fmonth2=''$mesb2' '$mesb3' '$mesb4' '$mesb5' '$mesb6' '$mesb7''
fmonth3=''$mesc2' '$mesc3' '$mesc4' '$mesc5' '$mesc6' '$mesc7''
fmonth4=''$mesd2' '$mesd3' '$mesd4' '$mesd5' '$mesd6' '$mesd7''
fmonth5=''$mese2' '$mese3' '$mese4' '$mese5' '$mese6' '$mese7''

echo ${fdays} ${anoi} ${label} ${fmonth}

cat << EOF > sznp.gs
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*  OBJETIVO : GERAR FIGURAS MENSAIS E QUADRIMESTRAIS CLIMATICAS DE PRECIPITACAO  *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* * ANOS INICIAIS E FINAIS DAS RODADAS

_AnosI = ${ano}
_AnosF = ${ano}

* * CENARIOS-EXPERIMENTOS / VERSOES DAS RODADAS
_Cenarios = '${experimentos}'

* * VERSOES
_Versoes = '${versao}'

* * PERPASSA OS CENARIOS / VERSOES
_Cont = 1
while ( _Cont <= 1 )

_Cenario = subwrd(_Cenarios,_Cont)
_Versao = subwrd(_Versoes,_Cont)

* * INFORMACOES DO CABECALHO
* Ex.: Precipitacao (mm/dia) do Modelo _Modelo _Contorno _Res
*      Media _MediaMensal/Sazonal MES/QUADRIMESTRE (_AnoI-_AnoF)

_Modelo = 'Eta'
_Res = '20km'
_Contorno = 'CFS'
_MediaSazonal = 'Sazonal'
_MediaMensal = 'Mensal'

* * ANOS INICIAL E FINAL DA MEDIA CLIMATOLOGICA
* _AnoIDir = subwrd(_AnosI,_Cont)
* _AnoFDir = subwrd(_AnosF,_Cont)

_AnoI = subwrd(_AnosI,_Cont)
_AnoF = subwrd(_AnosF,_Cont)

* * MEDIAS MENSAIS DAS RODADAS
*** Caminho das rodadadas
_Rodada1.1 = '${Eta_binctl}/${rodada}/'%_Cenario%'/Eta20_'%_Cenario%'_${rodada}_2D.nc'
_Rodada2.1 = '${bin_Mswep}/template_2017_062023.ctl'
_Rodada3.1 = '${bin_Era5}/Prec_lsp_ERA5_2018_2023.nc'
_Rodada4.1 = '${bin_Era5}/Prec_conv_ERA5_2018_2023.nc'

_File1 = _Rodada1._Cont
_File2 = _Rodada2._Cont
_File3 = _Rodada3._Cont
_File4 = _Rodada4._Cont

'reinit'

* * DIRETORIO DAS FIGURAS
_Figuras = '/dados/grpeta/S2S/rodadas_sazonal_xc50/Figuras/'%_Cenario%'/${rodada}'

'! mkdir -p '%_Figuras%'/PREC'

* * PALETA DE CORES
rc = RGB()

* * CONFIGURACAO GERAL DO LAYOUT DA FIGURA
*  Definicao de cores, valores e posicionamento da barra de legenda de cores
*  Posicionamento do texto lateral (se houver)
*  Definicao do tipo de tracado, cor e espessura do mapa

_clevs = '50 100 200 300 400 500 600 700 800 900 1000'
**_clevs = '50 100 200 300 400 500 600 700 800 900 1000 1200'
_ccols = '0 16 17 18 19 20 21 22 23 24 25 26 27'

_cbar = '1 1 0.9 4'
_xcbar = '1.2 9.5 0.3 0.6 -fw 0.15 -fh 0.18 -ft 4 -fo center -dir h -line on -lc 15 -edge box'

_strpos = '8.4 3.5'
_map = '1 1 3'
_parea = '1 9.5 0.42 7.7'
_basemap = 'O 0 15 M'

_ResXY = "1275 1080"

'set lwid 13 0.3'

* * COORDENADAS DO DOMINIO
**_Lon = '-111.841 4.5595'
**_Lat = '-56.8038 25.1962'

_Lon = '-113.633 6.3674'
_Lat = '-57.7538 21.2462'

***_Lon = '-117.031 4.76933'
***_Lat = '-56.1192 29.0808'

* * CALCULO DA MEDIA MENSAL E QUADRIMESTRAL'
rc = Precipitacao()

_Cont = _Cont+1
endwhile

'quit'

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DEFINICAO DA FUNCAO DE CALCULO DA MEDIA MENSAL E QUADRIMESTRAL *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function Precipitacao()

* * ABERTURA DO ARQUIVO DE MEDIA MENSAL
'sdfopen '%_File1
'open '%_File2
'sdfopen '%_File3
'sdfopen '%_File4

say ''
say ''
'q files'
say result
say ''

* Dom?nio da Figura *
* * * * * * * * * * *
'set lat '%_Lat
'set lon '%_Lon

'q dims'
say result
say ''

* * DEFININDO A MEDIA CLIMATOLOGICA MENSAL

meses = '${fmonth}'
mesesa = '${fmonth1}'
mesesb = '${fmonth2}'
mesesc = '${fmonth3}'
mesesd = '${fmonth4}'
mesese = '${fmonth5}'
fmes = '${fdays} '
fano = '${anoi}'

*  PREC   *
* * * * * *

m = 1
while ( m <= 6 )

mes = subwrd(meses,m)
mesa1 = subwrd(mesesa,m)
mesb2 = subwrd(mesesb,m)
mesc3 = subwrd(mesesc,m)
mesd4 = subwrd(mesesd,m)
mese5 = subwrd(mesese,m)

dd=subwrd(fmes,m)
yy=subwrd(fano,m)

'set dfile 1'
************* MODELO ETA *************************************************
say ' '%dd
say mes%' = SUM(PREC,TIME=00z01'%mes''%yy',TIME=21z'%dd''%mes''%yy')*1000'
mes%' =  SUM(PREC,TIME=00z01'%mes''%yy',TIME=21z'%dd''%mes''%yy')*1000'
say result

say ' '%dd
say mesa1%' = SUM(PRCV,TIME=00z01'%mes''%yy',TIME=21z'%dd''%mes''%yy')*1000'
mesa1%' =  SUM(PRCV,TIME=00z01'%mes''%yy',TIME=21z'%dd''%mes''%yy')*1000'
say result

say ' '%dd
say mesb2%' = SUM(PRGE,TIME=00z01'%mes''%yy',TIME=21z'%dd''%mes''%yy')*1000'
mesb2%' =  SUM(PRGE,TIME=00z01'%mes''%yy',TIME=21z'%dd''%mes''%yy')*1000'
say result
**************************************************************************
'set dfile 2'
************* MSWEP ******************************************************
say ' '%dd
say mesc3%' = SUM(PREC.2,TIME=00z01'%mes''%yy',TIME=23z'%dd''%mes''%yy')'
mesc3%' =  SUM(PREC.2,TIME=00z01'%mes''%yy',TIME=23z'%dd''%mes''%yy')'
say result
**************************************************************************
'set dfile 3'
************* ERA5 *******************************************************
say ' '%dd
say mesd4%' = SUM(LSP.3,TIME=00z01'%mes''%yy',TIME=23z'%dd''%mes''%yy')*1000'
mesd4%' =  SUM(LSP.3,TIME=00z01'%mes''%yy',TIME=23z'%dd''%mes''%yy')*1000'
say result

'set dfile 4'
say ' '%dd
say mese5%' = SUM(CP.4,TIME=00z01'%mes''%yy',TIME=23z'%dd''%mes''%yy')*1000'
mese5%' =  SUM(CP.4,TIME=00z01'%mes''%yy',TIME=23z'%dd''%mes''%yy')*1000'
say result
**************************************************************************

m = m+1
endwhile

*** CHAMADA DA FUNCAO DE GERACAO DAS FIGURAS CLIMATOLOGICAS MENSAIS
rc = Mensal()

'close 1'

return

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* FUNCAO DE GERACAO DE FIGURAS CLIMATOLOGICAS DE PRECIPITACAO MENSAIS *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function Mensal()

str1 = '${fmonth}'
str2 = '${fmonth1}'
str3 = '${fmonth2}'
str4 = '${fmonth3}'
str5 = '${fmonth4}'
str6 = '${fmonth5}'

i = 1
while( i <= 6 )

'set display color white'
'c'

varTMP1 = subwrd(str1,i)
varTMP2 = subwrd(str2,i)
varTMP3 = subwrd(str3,i)
varTMP4 = subwrd(str4,i)
varTMP5 = subwrd(str5,i)
varTMP6 = subwrd(str6,i)

'set parea 0.3 3.8  4.3 8.3'
'set ylopts 1 3 0.11'
'set clab off'
'set font 4'
'set map '%_map
'set mpdset brmap_hires'
'set gxout shaded'
'set grads off'
'set grid off'
'set clevs '%_clevs
'set ccols '%_ccols
'set clopts -1 3 0.17'
'set xlint 20'
'set ylint 20'
'd '%varTMP4
'set parea off'

'set parea 3.8 7.3  4.3 8.3'
'set ylopts 0 0 0'
'set clab off'
'set font 4'
'set map '%_map
'set mpdset brmap_hires'
'set gxout shaded'
'set grads off'
'set grid off'
'set clevs '%_clevs
'set ccols '%_ccols
'set clopts -1 3 0.17'
'set xlint 20'
'set ylint 20'
'd '%varTMP6
'set parea off'

'set parea 7.3 10.8  4.3 8.3'
'set ylopts 0 0 0'
'set clab off'
'set font 4'
*'set mproj scaled'
'set map '%_map
'set mpdset brmap_hires'
'set gxout shaded'
'set grads off'
'set grid off'
'set clevs '%_clevs
'set ccols '%_ccols
'set clopts -1 3 0.17'
'set xlint 20'
'set ylint 20'
'd '%varTMP5
'set parea off'


*********************************************************************************
'set parea 0.3 3.8  0.1 4.5'
'set ylopts 1 3 0.11'
'set clab off'
'set font 4'
'set map '%_map
'set mpdset brmap_hires'
'set gxout shaded'
'set grads off'
'set grid off'
'set clevs '%_clevs
'set ccols '%_ccols
'set clopts -1 3 0.17'
'set xlint 20'
'set ylint 20'
'd '%varTMP1
'set parea off'

'set parea 3.8 7.3  0.1 4.5'
'set ylopts 0 0 0'
'set clab off'
'set font 4'
'set map '%_map
'set mpdset brmap_hires'
'set gxout shaded'
'set grads off'
'set grid off'
'set clevs '%_clevs
'set ccols '%_ccols
'set clopts -1 3 0.17'
'set xlint 20'
'set ylint 20'
'd '%varTMP2
'set parea off'

'set parea 7.3 10.8  0.1 4.5'
'set ylopts 0 0 0'
'set clab off'
'set font 4'
'set map '%_map
'set mpdset brmap_hires'
'set gxout shaded'
'set grads off'
'set grid off'
'set clevs '%_clevs
'set ccols '%_ccols
'set clopts -1 3 0.17'
'set xlint 20'
'set ylint 20'
'd '%varTMP3
'set parea off'

'xcbar '%_xcbar

'set string 1 tl 2 0'
'draw string 0.7 8.3 'subwrd(str1,i)''

'set string 1 tl 1 0'
'draw string 1.3 8 PREC_MSWEP'
'draw string 4.8 8 PRCV_ERA5'
'draw string 8.4 8 PRGE_ERA5'

'draw string 1.2 4 PREC_Eta20km'
'draw string 4.7 4 PRCV_Eta20km'
'draw string 8.2 4 PRGE_Eta20km'

'gxprint '%_Figuras%'/PREC/'%_Modelo%'_'%_Contorno%'_'%_Res%'_'%_Cenario%'_${rodada}_PREC2_'subwrd(str1,i)'_HD.png x'subwrd(_ResXY,1)' y'subwrd(_ResXY,2)

i = i+1
endwhile

return

* * * * * * * * * * * * * * * *
* FUNCAO DE GERACAO DE CORES  *
* * * * * * * * * * * * * * * *
function RGB()
'set rgb  16 130   0 220'
'set rgb  17  30  60 255'
'set rgb  18   0 160 255'
'set rgb  19   0 200 200'
'set rgb  20   0 210 140'
'set rgb  21   0 220   0'
'set rgb  22 160 230  50'
'set rgb  23 230 220  50'
'set rgb  24 230 175  45'
'set rgb  25 240 130  40 '
'set rgb  26 250  60  60'
'set rgb  27 240   0 130 '

return

'quit'
EOF

${cam_grads}/grads -blc "run sznp.gs"
rm -f sznp.gs

exit


