echo $0
cd "$(dirname "$0")"
echo INICIO $0 $(date)

. ./config.cfg

### selection of temperature surface

#gdalinfo icon.grib2 | grep -i TMP -B 5 -A 1 | grep -i "2\[m\]" -B 1 -A 5 | grep -o 'Band [0-9]*' | cut -c 6- > temp_bands.txt

IFS=$'\n' read -d '' -r -a array <<< $(gdalinfo icon.grib2 | grep -i TMP -B 5 -A 1 | grep -i "2\[m\]" -B 1 -A 5 | grep -o 'Band [0-9]*' | cut -c 6-)

cd "$(dirname "$0")"

##temperatura

j=0
tifs=""

for index in "${!array[@]}"
do
        i=${array[index]}
        varname="temp"
        echo "Inicia $(date) ::::::::: Banda: $i temp_$j  :::::"
        newfile="$web_path/temp_$j.tif"
        gdal_translate icon.grib2 -B $i -a_srs EPSG:4326 -r bilinear -tr 0.05 0.05 $newfile
        valid_time=$(gdalinfo $newfile | grep -i grib_valid_time)
        gen_file="$web_path/temp_$(date +%y%m%d_%H -d @${valid_time:20:10})"
        rm $gen_file.tif
        cp $newfile $gen_file\_full.tif
        rm $newfile
        ##para img png
        echo "-----------------------$gen_file---------------------"
        gdalwarp -cutline ./land.geojson -dstnodata -99 -of GTiff $gen_file\_full.tif $gen_file.tif -overwrite
        gdalwarp -t_srs EPSG:3857 -r cubicspline -tr 2000 2000 $gen_file.tif $gen_file\_3857.tif -overwrite
        gdaldem color-relief $gen_file\_3857.tif -nearest_color_entry ./colors_icon_temp.txt $gen_file.png -alpha
        ##label
        label="ICON_${gen_file:0-14}"
        convert $gen_file.png -gravity South -pointsize 10 -fill white -annotate +0+50 $label $gen_file.png
        ### fin imagen
        ### borrar los 3857
        rm $gen_file\_3857.*
        rm $gen_file\_full.tif ##dejar solo el cropped
       ### tifs+=" ${newfile}"
        j=$((j+3))  
done
echo $tifs