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

. ./config.cfg 

filename="noaa20_fire"

##descargar
curl -O https://firms.modaps.eosdis.nasa.gov/data/active_fire/noaa-20-viirs-c2/shapes/zips/J1_VIIRS_C2_Global_24h.zip
##descromprimir
unzip -o -d down J1_VIIRS_C2_Global_24h.zip

##cortar a la región el GeoJson
$gdal_path/ogr2ogr -f "GeoJSON" $web_path/$filename.geojson down/J1_VIIRS_C2_Global_24h.shp -clipdst -120 0 -50 35


#rasterize
$gdal_path/gdal_rasterize -l J1_VIIRS_C2_Global_24h -a FRP -tr 0.02 0.02 -a_nodata 0.0 -ot Byte -of GTiff down/J1_VIIRS_C2_Global_24h.shp $web_path/$filename\_global.tif

#cortar a región
$gdal_path/gdal_translate -projwin -120 35 -50 0 $web_path/$filename\_global.tif $web_path/$filename.tif

#borrar el geotiff global
rm $web_path/$filename\_global.tif

#image
$gdal_path/gdalwarp -t_srs EPSG:3857 -r cubicspline $web_path/$filename.tif $web_path/$filename\_3857.tif -overwrite
$gdal_path/gdaldem color-relief $web_path/$filename\_3857.tif  ./colorsFRP.txt $web_path/$filename.png -alpha

label="Gen. $(date +'%Y-%m-%d_%H:%Mz') $filename" 
convert $web_path/$filename.png -gravity South -font arial -pointsize 10 -fill white -annotate +0+50 "$label" $web_path/$filename.png

######## Google cloud

# 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/*.png" "$gcbucket_folder"
gsutil -m cp -r "$web_path/*.tif" "$gcbucket_folder"
gsutil -m cp -r "$web_path/*.geojson" "$gcbucket_folder"
