
cd "$(dirname "$0")"

. ./config.cfg

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

rm $web_path/active_hurr.geojson
$gdal_path/ogr2ogr -f GEOJSON $web_path/active_hurr.geojson "https://services9.arcgis.com/RHVPKKiFTONKtxq3/ArcGIS/rest/services/Active_Hurricanes_v1/FeatureServer/0/query?where=objectid+%3D+objectid&outfields=*&f=json" -nln capa
$gdal_path/ogr2ogr -f GEOJSON $web_path/active_hurr.geojson "https://services9.arcgis.com/RHVPKKiFTONKtxq3/ArcGIS/rest/services/Active_Hurricanes_v1/FeatureServer/1/query?where=objectid+%3D+objectid&outfields=*&f=json" -nln capa -append -update
$gdal_path/ogr2ogr -f GEOJSON $web_path/active_hurr.geojson "https://services9.arcgis.com/RHVPKKiFTONKtxq3/ArcGIS/rest/services/Active_Hurricanes_v1/FeatureServer/2/query?where=objectid+%3D+objectid&outfields=*&f=json" -nln capa -append -update
$gdal_path/ogr2ogr -f GEOJSON $web_path/active_hurr.geojson "https://services9.arcgis.com/RHVPKKiFTONKtxq3/ArcGIS/rest/services/Active_Hurricanes_v1/FeatureServer/3/query?where=objectid+%3D+objectid&outfields=*&f=json" -nln capa -append -update
$gdal_path/ogr2ogr -f GEOJSON $web_path/active_hurr.geojson "https://services9.arcgis.com/RHVPKKiFTONKtxq3/ArcGIS/rest/services/Active_Hurricanes_v1/FeatureServer/4/query?where=objectid+%3D+objectid&outfields=*&f=json" -nln capa -append -update

##tropical weather outlook
curl https://www.nhc.noaa.gov/xgtwo/gtwo_shapefiles.zip > two.zip
$gdal_path/ogr2ogr -f GEOJSON $web_path/two.geojson /vsizip/two.zip -nln two


## $gdal_path/gdal_translate -of GTiff -gcp 227.533 716.614 -90 15 -gcp 1651.61 716.413 15 15 -gcp 837.495 495.398 -45 30 -gcp 1448.57 494.863 0 30 -gcp 227.339 495.583 -90 30 -gcp 1244.48 495.482 -15 30 "/vsicurl/https://ocean.weather.gov/UA/Atl_Tropics.gif" "Atl_Tropics.gif"
## $gdal_path/gdalwarp -r near -tps -co COMPRESS=LZW  -t_srs EPSG:4326 "Atl_Tropics.gif" "Atl_Tropics.tif" -overwrite
## $gdal_path/gdal_translate -projwin -118 35 -50 0 Atl_Tropics.tif Atl_Tropics_crop.tif
## $gdal_path/gdalwarp "Atl_Tropics_crop.tif" -t_srs EPSG:3857 -r bilinear "$web_path/Atl_Tropics.tif" -overwrite
## $gdal_path/gdal_translate "Atl_Tropics_crop.tif" "$web_path/Atl_Tropics.gif"

## unified
curl https://ocean.weather.gov/gis/UA_SFC_ANAL.png > UA_SFC_ANALdown.png
/usr/bin/convert ./UA_SFC_ANALdown.png -crop 625x345+750+297 ./UA_SFC_ANAL_crop.png
$gdal_path/gdal_translate -of GTiff -gcp 1.61571 185.006 -125 22.4 -gcp 623.458 290.459 -38.15 7.62 -gcp 595.594 2.51577 -42 48 -gcp 44.4665 7.328 -119 47.3 "UA_SFC_ANAL_crop.png" "UA_SFC_ANAL_georef.png"
$gdal_path/gdalwarp -r near -tps -co COMPRESS=LZW -t_srs EPSG:4326 -tr 0.01 0.01 "UA_SFC_ANAL_georef.png" "UA_SFC_ANAL.tif" -overwrite
$gdal_path/gdalwarp "UA_SFC_ANAL.tif" -t_srs EPSG:3857 -r bilinear "UA_SFC_ANAL_3857.tif" -overwrite
$gdal_path/gdal_translate "UA_SFC_ANAL_3857.tif" "$web_path/UA_SFC_ANAL.png"
$gdal_path/gdal_translate "UA_SFC_ANAL_3857.tif" "$web_path/UA_SFC_ANAL.gif"

cp UA_SFC_ANAL_crop.png $web_path/UA_SFC_ANAL_crop.png
cp UA_SFC_ANALdown.png $web_path/UA_SFC_ANALdown.png

## marine
echo "Marine ATL"
curl https://www.nhc.noaa.gov/marine/forecast/images/ATLCFULL0.gif > ATLCFULL0.gif
curl https://www.nhc.noaa.gov/marine/forecast/images/ATLCFULL24.gif > ATLCFULL24.gif
curl https://www.nhc.noaa.gov/marine/forecast/images/ATLCFULL48.gif > ATLCFULL48.gif
curl https://www.nhc.noaa.gov/marine/forecast/images/ATLCFULL72.gif > ATLCFULL72.gif

$gdal_path/gdal_translate -of GTiff -gcp 111.583 259.766 -90 20 -gcp 602.512 123.466 -50 30 -gcp 725.441 386.549 -40 10 -gcp 234.505 123.514 -80 30 -gcp 222.188 421.559 -80.8883 7.221 -gcp 584.178 460.471 -51.6459 4.39828 -gcp 82.6653 328.881 -92.2269 14.5292 -gcp 356.611 259.734 -70 20 -gcp 479.475 386.618 -60 10 -gcp 356.433 386.607 -70 10 -gcp 111.817 124.039 -90 30 -gcp 111.887 386.863 -90 10 ATLCFULL0.gif ATLCFULL0_georef.gif
$gdal_path/gdalwarp -r near -tps -co COMPRESS=LZW -t_srs EPSG:4326 -tr 0.01 0.01 ATLCFULL0_georef.gif ATLCFULL0_georef.tif -overwrite
$gdal_path/gdalwarp ATLCFULL0_georef.tif -t_srs EPSG:3857 -r bilinear $web_path/ATLCFULL00.png -overwrite

$gdal_path/gdal_translate -of GTiff -gcp 111.583 259.766 -90 20 -gcp 602.512 123.466 -50 30 -gcp 725.441 386.549 -40 10 -gcp 234.505 123.514 -80 30 -gcp 222.188 421.559 -80.8883 7.221 -gcp 584.178 460.471 -51.6459 4.39828 -gcp 82.6653 328.881 -92.2269 14.5292 -gcp 356.611 259.734 -70 20 -gcp 479.475 386.618 -60 10 -gcp 356.433 386.607 -70 10 -gcp 111.817 124.039 -90 30 -gcp 111.887 386.863 -90 10 ATLCFULL24.gif ATLCFULL24_georef.gif
$gdal_path/gdalwarp -r near -tps -co COMPRESS=LZW -t_srs EPSG:4326 -tr 0.01 0.01 ATLCFULL24_georef.gif ATLCFULL24_georef.tif -overwrite
$gdal_path/gdalwarp ATLCFULL24_georef.tif -t_srs EPSG:3857 -r bilinear $web_path/ATLCFULL24.png -overwrite

$gdal_path/gdal_translate -of GTiff -gcp 111.583 259.766 -90 20 -gcp 602.512 123.466 -50 30 -gcp 725.441 386.549 -40 10 -gcp 234.505 123.514 -80 30 -gcp 222.188 421.559 -80.8883 7.221 -gcp 584.178 460.471 -51.6459 4.39828 -gcp 82.6653 328.881 -92.2269 14.5292 -gcp 356.611 259.734 -70 20 -gcp 479.475 386.618 -60 10 -gcp 356.433 386.607 -70 10 -gcp 111.817 124.039 -90 30 -gcp 111.887 386.863 -90 10 ATLCFULL48.gif ATLCFULL48_georef.gif
$gdal_path/gdalwarp -r near -tps -co COMPRESS=LZW -t_srs EPSG:4326 -tr 0.01 0.01 ATLCFULL48_georef.gif ATLCFULL48_georef.tif -overwrite
$gdal_path/gdalwarp ATLCFULL48_georef.tif -t_srs EPSG:3857 -r bilinear $web_path/ATLCFULL48.png -overwrite

$gdal_path/gdal_translate -of GTiff -gcp 111.583 259.766 -90 20 -gcp 602.512 123.466 -50 30 -gcp 725.441 386.549 -40 10 -gcp 234.505 123.514 -80 30 -gcp 222.188 421.559 -80.8883 7.221 -gcp 584.178 460.471 -51.6459 4.39828 -gcp 82.6653 328.881 -92.2269 14.5292 -gcp 356.611 259.734 -70 20 -gcp 479.475 386.618 -60 10 -gcp 356.433 386.607 -70 10 -gcp 111.817 124.039 -90 30 -gcp 111.887 386.863 -90 10 ATLCFULL72.gif ATLCFULL72_georef.gif
$gdal_path/gdalwarp -r near -tps -co COMPRESS=LZW -t_srs EPSG:4326 -tr 0.01 0.01 ATLCFULL72_georef.gif ATLCFULL72_georef.tif -overwrite
$gdal_path/gdalwarp ATLCFULL72_georef.tif -t_srs EPSG:3857 -r bilinear $web_path/ATLCFULL72.png -overwrite


echo "Marine PACFull"
curl https://www.nhc.noaa.gov/marine/forecast/images/PACFULL0.gif > PACFULL0.gif
curl https://www.nhc.noaa.gov/marine/forecast/images/PACFULL24.gif > PACFULL24.gif
curl https://www.nhc.noaa.gov/marine/forecast/images/PACFULL48.gif > PACFULL48.gif
curl https://www.nhc.noaa.gov/marine/forecast/images/PACFULL72.gif > PACFULL72.gif

$gdal_path/gdal_translate -of GTiff -gcp 258.449 82.8898 -120 30 -gcp 258.468 218.789 -120 20 -gcp 382.572 218.857 -110 20 -gcp 628.446 218.561 -90 20 -gcp 628.698 469.501 -90 0 -gcp 751.505 218.566 -80 20 -gcp 751.685 82.4168 -80 30 -gcp 628.388 346.542 -90 10 -gcp 135.489 469.484 -130 0 -gcp 294.582 45.6044 -117.112 32.536 -gcp 467.197 53.4515 -103.065 32.0004 -gcp 790.138 371.679 -76.749 7.91179 PACFULL0.gif PACFULL0_georef.gif
$gdal_path/gdalwarp -r near -tps -co COMPRESS=LZW -t_srs EPSG:4326 -tr 0.01 0.01 PACFULL0_georef.gif PACFULL0_georef.tif -overwrite
$gdal_path/gdalwarp PACFULL0_georef.tif -t_srs EPSG:3857 -r bilinear $web_path/PACFULL00.png -overwrite

$gdal_path/gdal_translate -of GTiff -gcp 258.449 82.8898 -120 30 -gcp 258.468 218.789 -120 20 -gcp 382.572 218.857 -110 20 -gcp 628.446 218.561 -90 20 -gcp 628.698 469.501 -90 0 -gcp 751.505 218.566 -80 20 -gcp 751.685 82.4168 -80 30 -gcp 628.388 346.542 -90 10 -gcp 135.489 469.484 -130 0 -gcp 294.582 45.6044 -117.112 32.536 -gcp 467.197 53.4515 -103.065 32.0004 -gcp 790.138 371.679 -76.749 7.91179 PACFULL24.gif PACFULL24_georef.gif
$gdal_path/gdalwarp -r near -tps -co COMPRESS=LZW -t_srs EPSG:4326 -tr 0.01 0.01 PACFULL24_georef.gif PACFULL24_georef.tif -overwrite
$gdal_path/gdalwarp PACFULL24_georef.tif -t_srs EPSG:3857 -r bilinear $web_path/PACFULL24.png -overwrite

$gdal_path/gdal_translate -of GTiff -gcp 258.449 82.8898 -120 30 -gcp 258.468 218.789 -120 20 -gcp 382.572 218.857 -110 20 -gcp 628.446 218.561 -90 20 -gcp 628.698 469.501 -90 0 -gcp 751.505 218.566 -80 20 -gcp 751.685 82.4168 -80 30 -gcp 628.388 346.542 -90 10 -gcp 135.489 469.484 -130 0 -gcp 294.582 45.6044 -117.112 32.536 -gcp 467.197 53.4515 -103.065 32.0004 -gcp 790.138 371.679 -76.749 7.91179 PACFULL48.gif PACFULL48_georef.gif
$gdal_path/gdalwarp -r near -tps -co COMPRESS=LZW -t_srs EPSG:4326 -tr 0.01 0.01 PACFULL48_georef.gif PACFULL48_georef.tif -overwrite
$gdal_path/gdalwarp PACFULL48_georef.tif -t_srs EPSG:3857 -r bilinear $web_path/PACFULL48.png -overwrite

$gdal_path/gdal_translate -of GTiff -gcp 258.449 82.8898 -120 30 -gcp 258.468 218.789 -120 20 -gcp 382.572 218.857 -110 20 -gcp 628.446 218.561 -90 20 -gcp 628.698 469.501 -90 0 -gcp 751.505 218.566 -80 20 -gcp 751.685 82.4168 -80 30 -gcp 628.388 346.542 -90 10 -gcp 135.489 469.484 -130 0 -gcp 294.582 45.6044 -117.112 32.536 -gcp 467.197 53.4515 -103.065 32.0004 -gcp 790.138 371.679 -76.749 7.91179 PACFULL72.gif PACFULL72_georef.gif
$gdal_path/gdalwarp -r near -tps -co COMPRESS=LZW -t_srs EPSG:4326 -tr 0.01 0.01 PACFULL72_georef.gif PACFULL72_georef.tif -overwrite
$gdal_path/gdalwarp PACFULL72_georef.tif -t_srs EPSG:3857 -r bilinear $web_path/PACFULL72.png -overwrite

# 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/*.*" "$gcbucket_folder"

set +x