echo $0
cd "$(dirname "$0")"
echo $(date)
# Get current date and time in YYMMDD_HHMM format
currentdt=$(date +%y%m%d_%H%M)


. ./config.cfg  ##include configuration file

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

### $1 = Producto
### $2 = Canal Goes
### $3 = DATASET del netcdf
### $4 = nombre de la imagen final
### bash /cloudclusters/projects/goes_gdal/goes_netcdf.sh ABI-L2-CMIPF 02 CMI goes16_02

$python_path goes_down.py $1 $2

##ver netcdf "NUEVO"
cmp -s down/ultimo_$1_$2.nc down/ultimo_$1_$2_old.nc
resultado=$?
echo "Resultado $resultado"
if test $resultado -eq 0
    then
        echo " The NTCDF files match....exit"
        exit
else
        echo "The NETCDF file is different"
        cp down/ultimo_$1_$2.nc down/ultimo_$1_$2_old.nc
fi

rm $1_$2_$3*.tif

if [ $2 = "02" ] || [ $2 = "01" ] ; then
    echo "-scale grayscale"
    $gdal_path/gdal_translate -scale NETCDF:down/ultimo_$1_$2.nc:$3 $1_$2_$3.tif
else
    echo "-------------------unscale--------------"
    echo "COMANDO: $gdal_path/gdal_translate -unscale NETCDF:$(dirname "$0")/down/ultimo_$1_$2.nc:$3 $1_$2_$3.tif"
    $gdal_path/gdal_translate -unscale NETCDF:$(dirname "$0")/down/ultimo_$1_$2.nc:$3 $1_$2_$3.tif
fi

$gdal_path/gdalwarp -t_srs EPSG:4326 $1_$2_$3.tif $1_$2_$3_4326.tif -overwrite
$gdal_path/gdal_translate -projwin -120 35 -50 0 $1_$2_$3_4326.tif $1_$2_$3_4326_crop.tif

$gdal_path/gdalwarp -t_srs EPSG:3857 -r cubicspline $1_$2_$3_4326_crop.tif $1_$2_$3_3857.tif -overwrite

if [ $2 = "02" ] || [ $2 = "01" ]; then
    $gdal_path/gdal_translate -ot Byte $1_$2_$3_3857.tif $web_path/$4.jpg
    /usr/bin/convert -quality 75% -scale 75% -brightness-contrast 5x10 -transparent black -fuzz 25% $web_path/$4.jpg $web_path/$4.gif
    ## muy grande /usr/bin/convert -quality 75% -scale 50% -brightness-contrast 10x10 -transparent black -fuzz 20% $web_path/$4.jpg $web_path/$4.png
    echo "Generado: $web_path/$4.jpg y gif"
    imgtype="gif"
else
   if [ $2 = "RR" ]; then
        $gdal_path/gdalwarp $1_$2_$3_3857.tif -r cubicspline -tr 1500 1500 $1_$2_$3_3857r.tif -overwrite
        $gdal_path/gdaldem color-relief $1_$2_$3_3857r.tif -nearest_color_entry ./colors$2$3.txt $web_path/$4.png -alpha
    else 
        $gdal_path/gdaldem color-relief $1_$2_$3_3857.tif -nearest_color_entry ./colors$2$3.txt $web_path/$4.png -alpha
    fi
    /usr/bin/convert -scale 70% $web_path/$4.png $web_path/$4.png
    imgtype="png"
fi

## guardar el geotiff en web
cp $1_$2_$3_4326_crop.tif $web_path/$4.tif

##guardar en Google Storage
echo "Copying files to bucket..."
gsutil -m cp -r "$web_path/$4.png" "$gcbucket_folder/$4.png"
gsutil -m cp -r "$web_path/$4.png" "$gcbucket_folder/ts/$4_$currentdt.png"

gsutil -m cp -r "$web_path/$4.tif" "$gcbucket_folder/$4.tif"
gsutil -m cp -r "$web_path/$4.tif" "$gcbucket_folder/ts/$4_$currentdt.tif"

## para hacer sandwich
## composite -dissolve 30 /cloudclusters/default_site/data/goes/goes16_13_5.png /cloudclusters/default_site/data/goes/goes16_02_5.gif -alpha Set -resize 1662x888 -gravity center /cloudclusters/default_site/data/test/test_sw.png


############################ rayos y tiempo severo por el momento no tenemos rayos heatmap enfriamiento #############################
if [ $2 = "13" ] ; then
    echo "Generado heatmap de rayos"
    rm down/OR_GLM* ##borrando los anteriores
    $gdal_path/python goes_glm.py $web_path 600 ##eran 900 por 15m ahora 600
    $gdal_path/gdal_rasterize -burn 1.0 -tr 0.05 0.05 -a_nodata 0.0 -ot Byte -of GTiff -add $web_path/rayos.geojson $web_path/rayos.tif
    $gdal_path/gdal_translate -projwin -120 35 -50 0 $web_path/rayos.tif $web_path/rayos_crop.tif
    $gdal_path/gdalwarp -t_srs EPSG:3857 $web_path/rayos_crop.tif rayos_3857.tif -overwrite
    ## 220817 no resample $gdal_path/gdalwarp -r cubicspline -tr 1000 1000 rayos_3857.tif rayos_3857r.tif -overwrite
    ## $gdal_path/gdalwarp -t_srs EPSG:3857 $web_path/rayos_crop.tif rayos_3857.tif -overwrite
    $gdal_path/gdaldem color-relief rayos_3857.tif ./colorsGLM.txt -nearest_color_entry $web_path/rayos.png -alpha

    ##guardar en Google Storage
    echo "Copying files to bucket..."
    gsutil -m cp -r "$web_path/rayos.png" "$gcbucket_folder/rayos.png"
    gsutil -m cp -r "$web_path/rayos.png" "$gcbucket_folder/ts/rayos_$currentdt.png"
fi


echo $(date)

##23.06.05 calculo overshooting tops al final cuando ya se generaron
if [ $2 = "14" ]; then
    echo "Calcular overshooting tops -> $(date)"
    ### calculo de lluvia horaria para alertar

    # Get the time coverage end values from the TIFF files
    time_coverage_end_14=$($gdal_path/gdalinfo $web_path/goes16_14.tif | grep -i NC_GLOBAL#time_coverage_end=)
    time_coverage_end_13=$($gdal_path/gdalinfo $web_path/goes16_13.tif | grep -i NC_GLOBAL#time_coverage_end=)

    # Extract the timestamps from the time coverage end values
    timestamp_14=$(echo $time_coverage_end_14 | cut -d'=' -f2)
    timestamp_13=$(echo $time_coverage_end_13 | cut -d'=' -f2)

    # Convert the timestamps to Unix timestamps
    unix_timestamp_14=$(date -d "$timestamp_14" +%s)
    unix_timestamp_13=$(date -d "$timestamp_13" +%s)

    # Calculate the time difference in seconds (absolute value)
    time_difference=$((unix_timestamp_14 - unix_timestamp_13))
    time_difference=${time_difference#-}   ###valor absoluto

    # Check if the time difference is less than 2 minutes (120 seconds)
    if ((time_difference < 120)); then
        echo "The timestamps are less than 2 minutes apart. 14: $timestamp_14 13: $timestamp_13"
        $gdal_path/gdal_calc.py -A $web_path/goes16_14.tif -B $web_path/goes16_13.tif --calc="(A<221)*(A-B)" --outfile $web_path/$4_ost.tif --overwrite
        
        gsutil -m cp -r "$web_path/$4_ost.tif" "$gcbucket_folder/$4_ost.tif"
        gsutil -m cp -r "$web_path/$4_ost.tif" "$gcbucket_folder/ts/$4_ost_$currentdt.tif"

    fi
    
    
fi
echo $(date)

