##llamar así:   bash georef.sh https://ocean.weather.gov/UA/Atl_Tropics.gif
##usar así https://climaya.com/appjs/leaflet/imgu.html?url=https://cloud1.on.gt/data/nhc/Atl_Tropics.gif&x1=-106.7853&x2=20.0170&y1=-24.4460&y2=64.2502&lat=22&lon=-80&z=6&pin=0&scroll=1&o=.8

cd "$(dirname "$0")"

. ./config.cfg  ##include configuration file

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

curl https://ocean.weather.gov/UA/Atl_Tropics.gif > Atl_Tropics.gif
filename=Atl_Tropics
extension=gif

$gdal_path/gdal_translate -of GTiff -gcp 227.244 716.627 -90 15 -gcp 837.786 495.726 -45 30 -gcp 139.601 707.526 -96.4391 15.6542 -gcp 634.492 716.572 -60 15 -gcp 558.338 913.795 -65.5897 0.689672 -gcp 689.747 897.658 -55.9193 1.8738 -gcp 227.638 495.581 -90 30 Atl_Tropics.gif "out_$filename.$extension"
$gdal_path/gdalwarp -r near -tps -co COMPRESS=LZW  -t_srs EPSG:4326 "out_$filename.$extension" "$filename.tif" -overwrite
$gdal_path/gdalwarp "$filename.tif" -t_srs EPSG:3857 -r near "w_$filename.tif" -overwrite
$gdal_path/gdal_translate "w_$filename.tif" $web_path/$filename.gif

echo "Transparente"
/usr/bin/convert $web_path/$filename.gif -fill white -fuzz 10% -opaque "#F734FF" $web_path/t_$filename.gif
/usr/bin/convert $web_path/t_$filename.gif -fill white -fuzz 10% -opaque "#299C9C" $web_path/t_$filename.gif
/usr/bin/convert $web_path/t_$filename.gif -fill white -fuzz 10% -opaque "#021A23" $web_path/t_$filename.gif
/usr/bin/convert $web_path/t_$filename.gif -fill white -fuzz 10% -opaque "#312CFF" $web_path/t_$filename.gif
/usr/bin/convert $web_path/t_$filename.gif -fill yellow -fuzz 10% -opaque "#E16F00" $web_path/t_$filename.gif
/usr/bin/convert $web_path/t_$filename.gif -transparent white $web_path/t_$filename.gif

#/usr/bin/convert $web_path/$filename.gif -fill white -fuzz 17% -opaque "#E89557" $web_path/t_$filename.gif
#/usr/bin/convert $web_path/$filename.gif -fill white -fuzz 17% -opaque "#38A2A2" $web_path/t_$filename.gif
#/usr/bin/convert $web_path/t_$filename.gif -fill white -fuzz 17% -opaque magenta $web_path/t_$filename.gif
#/usr/bin/convert $web_path/t_$filename.gif -fill white -fuzz 17% -opaque green $web_path/t_$filename.gif
#/usr/bin/convert $web_path/t_$filename.gif -fill white -fuzz 17% -opaque lime $web_path/t_$filename.gif
#

echo "Descarga noaa servir"
## -> https://climaya.com/appjs/leaflet/imgu.html?url=https://www.servir.net/images/imageviewer/noaa_srf/noaa_srf_last.png&x1=-160&x2=-15&y1=-20&y2=50&lat=22&lon=-85&z=6&pin=0&scroll=1
curl https://www.servir.net/images/imageviewer/noaa_srf/noaa_srf_last.png > $web_path/noaa_surface.png
/usr/bin/convert $web_path/noaa_surface.png -fuzz 30% -transparent "#F8F8F8" $web_path/t_noaa_surface.png

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

# # Delete existing files in the bucket (optional, adjust as needed)
# echo "Deleting existing files from bucket..."
# sutil -m rm -r "$gcbucket_folder/*"

# Copy files and subdirectories to the bucket
echo "Copying files to bucket..."
gsutil -m cp -r "$web_path/t_$filename.gif" "$gcbucket_folder"
gsutil -m cp -r "$web_path/t_noaa_surface.png" "$gcbucket_folder"

set +x
