#!/bin/bash  -x
export LANG=us
D_SAT=/oper/goes/goes16/level2_noaa/GPM_MERGIR_1
D_MERGE=/lustre_xc50/ioper/users/rozante/MERGE_HOUR_V07B/BKP
cdo=/stornext/home/io/bin/cdo-1.8.2/bin/cdo

datai=$1
dataf=$1

while [ $datai -le $dataf ] ; do
 data1=$(date -u -d "${datai:0:8} ${datai:8:2}:00 1 hours  " "+%Y%m%d%H")
 if [ -s $D_SAT/${datai:0:4}/merg_${datai}_4km-pixel.nc4 ] && [ -s $D_SAT/${data1:0:4}/merg_${data1}_4km-pixel.nc4 ] && [ -s $D_MERGE/${data1:0:4}/${data1:4:2}/${data1:6:2}/MERGE_CPTEC_${data1}.grib2  ] ; then
   mkdir ${data1:0:6}
   TIME=$(date -u -d "${datai:0:8} ${datai:8:2}:00 1 hours  " +"%H"Z"%d%b%Y")
   grads -blc "run recorta_goes_BRA.gs $D_SAT/${datai:0:4}/merg_${datai}_4km-pixel.nc4 $D_SAT/${data1:0:4}/merg_${data1}_4km-pixel.nc4  $D_MERGE/${data1:0:4}/${data1:4:2}/${data1:6:2}/MERGE_CPTEC_${data1}.ctl ${data1:0:6}/TB_${data1}"

   $cdo merge ${data1:0:6}/TB_${data1}_1.nc ${data1:0:6}/TB_${data1}_2.nc ${data1:0:6}/TB_${data1}_3.nc  ${data1:0:6}/TB_${data1}_4.nc  ${data1:0:6}/MATRIZ_${data1}.nc
   rm -f  ${data1:0:6}/TB_${data1}_1.nc ${data1:0:6}/TB_${data1}_2.nc ${data1:0:6}/TB_${data1}_3.nc ${data1:0:6}/TB_${data1}_4.nc
 else
   echo "FALTA ARQUIVOS $D_SAT/${datai:0:4}/merg_${datai}_4km-pixel.nc4 ou  $D_SAT/${data1:0:4}/merg_${data1}_4km-pixel.nc4 ou $D_MERGE/${datai:0:4}/${datai:4:2}/${datai:6:2}/MERGE_CPTEC_${datai}.grib2"  >> ARQUIVOS_FALTANTES.TXT
 fi
 datai=$(date -u -d "${datai:0:8} ${datai:8:2}:00 1 hours  " "+%Y%m%d%H")
done
