#!/bin/ksh

data_run=2024061300
ti=1 # Numero do tempo inicial no CTL, geralmente t=1 (no Grads -> set t 1)  
tf=121 # Numero de tempo final no CTL (no Grads -> set t last)

t=$ti

while [ $t -le $tf ]; do

echo ${t}

t2=$((t - 1))
data_prev=$(date -u "+%Y%m%d%H" -d "${data_run:0:8} 00:00 $t2 hour")
echo $data_prev

rm plot.gs
cat <<EOF >> plot.gs
'c'
'reinit'
'set grads off'
'set display color white'
'sdfopen ww3icon_brcoast_$data_run.nc'
'set t '$t
'set lat -26 -21'
'set lon 314 320'
'set gxout shaded'
'set mpdset hires'
'define hsx=hs*cos(dir*3.1416/180)'
'define hsy=hs*sin(dir*3.1416/180)'
*'color -levs 0.5 1 1.5 2 2.5 3 4 5 6 -kind darkgreen->cyan->blue->red'
'color -levs 0.5 1 1.5 2 2.5 3.5 4 4.5 5 -kind rainbow'
'd hs'
*'set ccolor 0'
'set arrscl 0.1 1'
'set cthick 1'
'd skip(hsx,4);hsy'
'draw string 1.9 7.8 Rodada 2024061300Z / Prev+${t2}h ${data_prev}Z Hs(m) e Direcao da onda [WW3-ICON/CHM 4km]'
'cbar'
'printim Onda_2024061300_prev${data_prev}_WW3ICON4km_CHM.png'
'quit'
EOF

grads -blcx "run plot.gs"
t=$((t + 1))
done
