cd "$(dirname "$0")"

. ./config.cfg
################### LLUVIA +++++++++++++++++++++++++
LIM=24
rm selected.txt

for file in $(ls $web_path/pcpn_*.png);
do
  thisfile=${file:2-15}
  echo $i__$thisfile_${thisfile:0:6}${thisfile:7:2}
  [ ${thisfile:0:6}${thisfile:7:2} -gt $(date +%y%m%d%H) ] && echo $file >> selected.txt;
done
## cat selected.txt
/usr/bin/jq -R -s -c 'split("\n")' < selected.txt > $web_path/next_files.json

##animated gif y pause. por ahora sin animación
##rm $web_path/pcpn_*.gif
# /usr/bin/convert -delay 100 -loop 0  -alpha set -dispose previous `cat selected.txt | head -8` $web_path/pcpn_24h.gif
# /usr/bin/convert $web_path/pcpn_24h.gif \( +clone -set delay 500 \) +swap +delete $web_path/pcpn_24h.gif
# /usr/bin/convert -delay 100 -loop 0  -alpha set -dispose previous `cat selected.txt | head -16` $web_path/pcpn_48h.gif
# /usr/bin/convert $web_path/pcpn_48h.gif \( +clone -set delay 500 \) +swap +delete $web_path/pcpn_48h.gif
# /usr/bin/convert -delay 100 -loop 0  -alpha set -dispose previous `cat selected.txt | head -24` $web_path/pcpn_72h.gif
# /usr/bin/convert $web_path/pcpn_72h.gif \( +clone -set delay 500 \) +swap +delete $web_path/pcpn_72h.gif
# /usr/bin/convert -delay 100 -loop 0  -alpha set -dispose previous `cat selected.txt | head -32` $web_path/pcpn_96h.gif
# /usr/bin/convert $web_path/pcpn_96h.gif \( +clone -set delay 500 \) +swap +delete $web_path/pcpn_96h.gif

######################     crear los acumulados
tifs=""
bands=" ABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGH" ##para asignar bandas cada 8 tif
rm selected_tif.txt
i=1 ## para saber el modulo
for file in $(cat selected.txt);
do
  thisfile=${file:2-15}
  thistif=$i__$thisfile_${thisfile:0:6}_${thisfile:7:2}.tif
  echo $i
  echo $thistif
  newfile="-${bands:i:1} $web_path/pcpn_$thistif"
  tifs+=" ${newfile}"
  if ! ((i % 8)); then
    tifname=pcpn_$(($i*3))
    echo "gdal_calc.py $tifs --outfile=$web_path/a_$tifname.tif --calc='A+B+C+D+E+F+G+H' --overwrite"
    gdal_calc.py $tifs --outfile=$web_path/a_$tifname.tif --calc='A+B+C+D+E+F+G+H' --overwrite
    ### generar imagen 
        gdalwarp -t_srs EPSG:3857 -r cubicspline -tr 2000 2000 $web_path/a_$tifname.tif $web_path/a_$tifname\_3857.tif -overwrite
        gdaldem color-relief $web_path/a_$tifname\_3857.tif -nearest_color_entry ./colors_icon_d.txt $web_path/a_$tifname.png -alpha
        ##label
        label="$(date +'%Y-%m-%d_%H:%Mz')_ICON_acc_$tifname" 
        convert $web_path/a_$tifname.png -gravity South -font arial -pointsize 10 -fill white -annotate +0+50 $label $web_path/a_$tifname.png
    ### fin generar image
    tifs=""
  fi
  #####echo $thistif >> selected_tif.txt;
  ((i=i+1))
done


gdal_calc.py  -A $web_path/a_pcpn_24.tif -B $web_path/a_pcpn_48.tif -C $web_path/a_pcpn_72.tif  --outfile=$web_path/a_pcpn_72a.tif --calc='A+B+C' --overwrite
### generar imagen 
        gdalwarp -t_srs EPSG:3857 -r cubicspline -tr 2000 2000 $web_path/a_pcpn_72a.tif $web_path/a_pcpn_72a\_3857.tif -overwrite
        gdaldem color-relief  $web_path/a_pcpn_72a\_3857.tif -nearest_color_entry ./colors_icon_d.txt  $web_path/a_pcpn_72a.png -alpha
        ##label
        label="$(date +'%Y-%m-%d_%H:%Mz')_ICON_accum_72h" 
        convert $web_path/a_pcpn_72a.png -gravity South -font arial -pointsize 10 -fill white -annotate +0+50 $label $web_path/a_pcpn_72a.png
    ### fin generar image

gdal_calc.py  -A $web_path/a_pcpn_24.tif -B $web_path/a_pcpn_48.tif -C $web_path/a_pcpn_72.tif -D $web_path/a_pcpn_96.tif -E $web_path/a_pcpn_120.tif  --outfile=$web_path/a_pcpn_120a.tif --calc='A+B+C+D+E' --overwrite
### generar imagen 
        gdalwarp -t_srs EPSG:3857 -r cubicspline -tr 2000 2000 $web_path/a_pcpn_120a.tif $web_path/a_pcpn_120a\_3857.tif -overwrite
        gdaldem color-relief  $web_path/a_pcpn_120a\_3857.tif -nearest_color_entry ./colors_icon_d.txt  $web_path/a_pcpn_120a.png -alpha
        ##label
        label="$(date +'%Y-%m-%d_%H:%Mz')_ICON_accum_120h" 
        convert $web_path/a_pcpn_120a.png -gravity South -font arial -pointsize 10 -fill white -annotate +0+50 $label $web_path/a_pcpn_120a.png
    ### fin generar image




################### TEMPERATURA +++++++++++++++++++++++++
LIM=24
rm selected.txt

for file in $(ls $web_path/temp_*.png);
do
  thisfile=${file:2-15}
  echo $i__$thisfile_${thisfile:0:6}${thisfile:7:2}
  [ ${thisfile:0:6}${thisfile:7:2} -gt $(date +%y%m%d%H) ] && echo $file >> selected.txt;
done
## cat selected.txt
/usr/bin/jq -R -s -c 'split("\n")' < selected.txt > $web_path/temp_next_files.json

############# guardar a 00 y 12 los valores de tmax y tmin para comparacion siguiente
# Get the current hour of the day
current_hour=$(date +%H)

# Check if the current hour is 00 or 12
if [[ "$current_hour" == "00" || "$current_hour" == "12" ]]; then
    # Copy the file if the condition is met
    cp $web_path/min_temp_24.tif $web_path/min_temp_00.tif
    cp $web_path/max_temp_24.tif $web_path/max_temp_00.tif
    echo "File copied successfully. save 00/12 max temp"
fi

######################     crear el MAXIMO
printf '%.0s-' {1..50}; printf '\n'


tifs=""
bands=" ABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGH" ##para asignar bandas cada 8 tif
rm selected_tif.txt
i=1 ## para saber el modulo
for file in $(cat selected.txt);
do
  thisfile=${file:2-15}
  thistif=$i__$thisfile_${thisfile:0:6}_${thisfile:7:2}.tif
  newfile="-${bands:i:1} $web_path/temp_$thistif"
  echo newfile=$newfile
  tifs+=" ${newfile}"
  if ! ((i % 8)); then
    tifname=temp_$(($i*3))
  ##  echo "gdal_calc.py $tifs --outfile=$web_path/max_$tifname.tif --calc='maximum(maximum(maximum(A,B),maximum(C,D)),maximum(maximum(E,F),maximum(G,H)))'' --overwrite"
  set -x
    gdal_calc.py $tifs --NoDataValue=-99 --outfile=$web_path/max_$tifname.tif --calc='maximum(maximum(maximum(A,B),maximum(C,D)),maximum(maximum(E,F),maximum(G,H)))' --overwrite
    ### generar imagen 
        gdalwarp -t_srs EPSG:3857 -r cubicspline -tr 2000 2000 $web_path/max_$tifname.tif $web_path/max_$tifname\_3857.tif -overwrite
        gdaldem color-relief $web_path/max_$tifname\_3857.tif -nearest_color_entry ./colors_icon_temp.txt $web_path/max_$tifname.png -alpha
  set +x      
        ##label
        label="$(date +'%Y-%m-%d_%H:%Mz')_ICON_MAX_$tifname" 
        convert $web_path/max_$tifname.png -gravity South -font arial -pointsize 10 -fill white -annotate +0+50 $label $web_path/max_$tifname.png
    ### fin generar image
    tifs=""
  fi
  #####echo $thistif >> selected_tif.txt;
  ((i=i+1))
done
printf '%.0s-' {1..50}; printf '\n'
####### fin max

######################     crear el MINIMO
tifs=""
bands=" ABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGH" ##para asignar bandas cada 8 tif
rm selected_tif.txt
i=1 ## para saber el modulo
for file in $(cat selected.txt);
do
  thisfile=${file:2-15}
  thistif=$i__$thisfile_${thisfile:0:6}_${thisfile:7:2}.tif
  echo $i
  echo $thistif
  newfile="-${bands:i:1} $web_path/temp_$thistif"
  tifs+=" ${newfile}"
  if ! ((i % 8)); then
    tifname=temp_$(($i*3))
    echo "gdal_calc.py $tifs --outfile=$web_path/min_$tifname.tif --calc='minimum(minimum(minimum(A,B),minimum(C,D)),minimum(minimum(E,F),minimum(G,H)))'' --overwrite"
    gdal_calc.py $tifs --NoDataValue=-99 --outfile=$web_path/min_$tifname.tif --calc='minimum(minimum(minimum(A,B),minimum(C,D)),minimum(minimum(E,F),minimum(G,H)))' --overwrite
    ### generar imagen 
        gdalwarp -t_srs EPSG:3857 -r cubicspline -tr 2000 2000 $web_path/min_$tifname.tif $web_path/min_$tifname\_3857.tif -overwrite
        gdaldem color-relief $web_path/min_$tifname\_3857.tif -nearest_color_entry ./colors_icon_temp.txt $web_path/min_$tifname.png -alpha
        ##label
        label="$(date +'%Y-%m-%d_%H:%Mz')_ICON_MIN_$tifname" 
        convert $web_path/min_$tifname.png -gravity South -font arial -pointsize 10 -fill white -annotate +0+50 $label $web_path/min_$tifname.png
    ### fin generar image
    tifs=""
  fi
  #####echo $thistif >> selected_tif.txt;
  ((i=i+1))
done
####### fin min

echo "Borrando tif 3857"
rm $web_path/*3857.tif
