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

. ./config.cfg

mkdir -p $web_path ##create folder if not exists

curl "http://grbsrv.opengribs.org/getmygribs2.php?osys=osx&ver=1.2.6&model=icon_p25_&la1=0&la2=35&lo1=-120&lo2=-50&intv=3&days=7&cyc=last&par=W;P;R;C;T;H;c;G;8;&wmdl=none&wpar=" -A "XyGrib_mac/1.2.6" > icon.json
jq .message.url icon.json > icon.url
cat icon.url 
sed 's/"//g' icon.url > icon2.url
downurl=$(cat icon2.url)
if [[ $downurl != "http"* ]]; then
  echo "Download error $downurl"
  exit 0
fi

echo "Download url valid: $downurl"
curl $downurl -A "XyGrib_mac/1.2.6" > icon.grib2

##distintas variables
echo 'ref_time'
ref_time=$(gdalinfo icon.grib2 | grep -i grib_ref_time | sort | uniq)
echo $ref_time
ref_time=$(echo $ref_time | cut -d' ' -f 2)
echo "$ref_time->$(date -d @$ref_time)"
#echo 'valid_time'
### valid times not shown :: valid_time=$(gdalinfo icon.grib2 | grep -i grib_valid_time | sort | uniq)
### valid times not shown :: echo $valid_time
valid_time=$(echo $valid_time | cut -d' ' -f 2)
echo $valid_time,$(date -d @$valid_time)
echo "Variables"
echo $(gdalinfo icon.grib2 | grep -i grib_comment | sort | uniq)
##extraer las capas

##lluvia 
j=0
tifs=""
for i in {14..70}
do
    if [ $i != 15 ]; then
        echo "Inicia $(date) ::::::::: Banda: $i pcpn_$j  :::::"
        newfile="$web_path/pcpn_$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/pcpn_$(date +%y%m%d_%H -d @${valid_time:20:10})"
        rm $gen_file.tif
        cp $newfile $gen_file.tif
        rm $newfile
        ##para img png
        echo "-----------------------$gen_file---------------------"
        gdalwarp -t_srs EPSG:3857 -r cubicspline -tr 2000 2000 $gen_file.tif $gen_file\_3857.tif -overwrite
        #gdalwarp $gen_file\_3857.tif -r cubicspline -tr 2000 2000 $gen_file\_3857r.tif -overwrite
        gdaldem color-relief $gen_file\_3857.tif -nearest_color_entry ./colors_icon.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
    fi    
        tifs+=" ${newfile}"
        j=$((j+3))   
done

# agrupaciones
echo "#####################--AGRUPACIONES--------############---------------############---------------------"

bands="ABCDEFGH"
j=1
tifs=""
    for j in {0..4}
    do
    tifs=""
    for i in {0..7}
    do  
        tif=$((($i+1)*3))
        tif2=$(($tif+$j*24))
        newfile="-${bands:i:1} /var/www/nube1.on.gt/icon/pcpn_$tif2.tif"
        tifs+=" ${newfile}"
    done 
 ### temporalmente quitado   gdal_calc.py $tifs --outfile=/var/www/nube1.on.gt/icon/a_pcpn_$tif2.tif --calc='A+B+C+D+E+F+G+H'
done


####### temperatura
source fetch_temp.sh
######## viento
source fetch_wspd.sh
##insertar a la BD
source insert_db_icon.sh


# Check if gsutil is installed
if ! command -v gsutil &> /dev/null; then
  echo "Error: gsutil is not installed."
  exit 1
fi

# Copy files and subdirectories to the bucket
echo "Copying files to bucket..."
gsutil -m cp -r "$web_path/*.*" "$gcbucket_folder"

echo FIN $0 $(date)

# j=0
# for i in {0..55}
# do
#         echo "Banda: $i pcpn_$j"
#         gdal_translate icon.grib2 -B $i -a_srs EPSG:4326 -r bilinear -tr 0.05 0.05 /var/www/nube1.on.gt/icon/pcpn_$j.tif
#         newfile="/var/www/nube1.on.gt/icon/pcpn_$j.tif"
#         tifs+=" ${newfile}"
#         j=$((j+3))
# done

#echo " tifs: $tifs"
### demasiado grande 450mb gdalbuildvrt -separate -o pcpn.vrt /var/www/nube1.on.gt/icon/pcpn_*.tif
### demasiado grande gdal_translate pcpn.vrt /var/www/nube1.on.gt/icon/pcpn.tif
#gdal_translate icon.grib2 -B 24 -a_srs EPSG:4326 -r bilinear -tr 0.05 0.05 /var/www/nube1.on.gt/icon/b24.tif
#gdal_translate icon.grib2 -B 166 -a_srs EPSG:4326 -r bilinear -tr 0.05 0.05 /var/www/nube1.on.gt/icon/temp_12z.tif

