 #!/bin/bash -x

 path1="/lustre_xc50/paulo_kubota/operacao/grb2"

for ii in $(  ls -F * | grep : | awk '{ print substr($1,1,10)}'  ); do 

# data=`dirname $ii`
# sed s/"tdef    40 linear"/"tdef    41 linear"/g   $ii > ${path1}/atmp0.txt
# sleep 1
# cp ${path1}/atmp0.txt $ii 
# grep -i tdef $ii 
# sleep 1
size=`du -s ${ii} | awk '{ print $1}'`
tot=`ls ${ii}/*.grb2| wc -l`
toti=`ls ${ii}/*.idx | wc -l`

if  [[ ${size} -gt 338732 ]]; then
echo $ii  " = " ${tot}    'idx=' ${toti};
else
echo $ii  " error"
fi
ixx=1
  for xx in $(ls ${path1}/${ii}/*.grb2); do
     dir_path=`dirname ${xx}`
     file_grb2=`basename ${xx} `
     file_idx=`basename ${xx} | awk '{print substr($1,1,43)}'`
     file_ctl=`basename ${xx} | awk '{print substr($1,1,43)".ctl"}'`
     if [[ ${ixx} -eq 1 ]]; then  
        wgrib2 -var -min -max ${ii}/${file_grb2}
        echo '==========================================>' ${file_grb2}
     fi
     if [[ ${ixx} -eq 41 ]]; then
        wgrib2 -var -min -max ${ii}/${file_grb2}
        echo '==========================================>' ${file_grb2}
     fi
     let ixx=${ixx}+1
  done
echo "---------------------------------------------------------------------------------------"
#/opt/grads/2.1.a1/bin/gribmap  -i  $ii; 
#chmod -R 755  ${path1}/${data}
cat<<EOF>teste_${ii}.gs
'reinit'
'set display color white'
'clear'
'open ${ii}/GPOSSMT${ii}P.fct.TQ0666L064.ctl '
say "${ii}/GPOSSMT${ii}P.fct.TQ0666L064.ctl"
it=2
while(it <=41)
'set lat -5'
'set lon 300'
'set t 'it''
tim=subwrd(result,5)
'd aave(APCPsfc(t='it'),g)'
line=sublin(result,1)
say tim'  'line
it=it+1
endwhile
'quit'
EOF
#grads -lbc "run teste_${ii}.gs"
rm -f teste_${ii}.gs

done
