#!/bin/bash

umask 022

export PATH=/cray_home/local/python/python_3.7/bin:${PATH}
export PYTHONPATH=/cray_home/local/python/python_3.7/bin
export GDAL_DATA=/cray_home/local/python/python_3.7/share/gdal

export GRADSB=${MHOME}/produtos/WEB/opengrads-2.2.1.oga.1/Contents
export GADDIR=/cray_home/local/grads/grads-2.1.0.oga.1/bin/Resources/SupportData
export GADLIB=/cray_home/local/grads/grads-2.1.0.oga.1/bin/Linux/Versions/2.1.0.oga.1/x86_64/libs
export GASCRP="${MHOME}/produtos/WEB/opengrads-2.2.1.oga.1/Contents/Resources/Scripts ${MHOME}/produtos/WEB/scripts/grads"
export PATH=${GRADSB}/ImageMagick-7.0.8-45/utilities/:${GRADSB}:$PATH

module switch PrgEnv-cray PrgEnv-gnu

export LD_LIBRARY_PATH=/usr/lib/:${LD_LIBRARY_PATH}
export LIBPATH=/usr/lib/
export MAGICK_CONFIGURE_PATH=${GRADSB}/ImageMagick-7.0.8-45/config

i=0
for arq in $(ls /lustre_xc50/ioper/data/BRAMS/ams_15km/2022090800G/model/POSPROCESS/profile*ctl /lustre_xc50/ioper/data/BRAMS/ams_15km/2022090900/model/POSPROCESS/profile*ctl ); do
cat <<EOF> aot_anim.gs
'open ${arq}'
'set display color white'
'c'

***************************************************************
***************** COLOR SETTINGS ******************************

'set rgb  22    0    0   74'
'set rgb  30    0    0  122'
'set rgb  38    0    0  170'
'set rgb  60   78   91  255'
'set rgb  74  162  189  255'
'set rgb  44    0    0  206'
'set rgb  77    0  255  255'

'set rgb  31    0   96    0'
'set rgb  36    0  126    0'
'set rgb  45    0  174    0'
'set rgb  50   18  204   21'
'set rgb  61   78  244   91'

'set rgb  35  134    0    0'
'set rgb  40  180    0    0'
'set rgb  47  206    0    0'
'set rgb  52  226   40   15'
'set rgb  57  246   80   30'
'set rgb  62  255  120   45'
'set rgb  67  255  160   60'
'set rgb  72  255  200   75'
'set rgb  80  255  255   99'

'set rgb  23  170  170  170'

'set rgb 99 8 8 8'
'set rgb 98 13 13 13'
'set rgb 97 23 23 23'
'set rgb 96 105 105 105'
'set rgb 95 188 188 188'
'set rgb 94 57 57 57'
'set rgb 93 103 103 103'
'set rgb 92 128 128 128'
'set rgb 91 177 177 177'
'set rgb 90 238 238 238'
'set rgb 89 203 203 203'
'set rgb 88 177 177 177'
'set rgb 87 152 152 152'
'set rgb 86 124 124 124'
'set rgb 85 93 93 93'
'set rgb 84 54 54 54'
'set rgb 83 40 40 40'

clev_aot550 = '0.02 0.05 0.1 0.15 0.2 0.25 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.25 1.5 2'
*clev_aot550 = '0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.25 1.5 1.75 2 2.5 3'
ccol_aot550 = ' 22 22 30 44 60 74 31 45 50 61 80 72 67 62 57 52 40 35'
*ccol_aot550 = '77'

'set grads off'

'q time'
say result
fct=subwrd(result, 3)

'set lat -45 15'
'set lon -85 -25'

'set map 0'
'set mpdset brmap_hires'

*'set mproj scaled'
*'set parea 0.0 8.5 0.0 11.0'

'set gxout shaded'
'set clevs 'clev_aot550 
'set ccols 'ccol_aot550

'd aot550'
'xcbar 0.47  8  0.5 0.7 '
'draw title BRAMS 15km CPTEC/INPE\Espessura Otica do Aerossol (550m)\'fct'(Inicializacao: 00Z08SEP2022)'
'printim aot_550_${i}.png'
'quit'

EOF

grads -bpc aot_anim.gs
let i=$i+1
sleep 01

done

magick -limit memory 1 -limit map 1 $(ls -ltr aot_550_*.png | awk '{print $9}') ./final_aot_550_animation.gif
exit 0
