#!/bin/bash
#
# Get GFS analysis files 
#

for i in "$@"
do
case $i in
#    -d=*|--date=*)
#    date="${i#*=}"
#    ;;
#    -r=*|--date=*)
#    run="${i#*=}"
#    ;;
    -wl=*)
    leftlon="${i#*=}"
    ;;
    -el=*)
    rightlon="${i#*=}"
    ;;
    -nl=*)
    toplat="${i#*=}"
    ;;
    -sl=*)
    bottomlat="${i#*=}"
    ;;
#    -st=*)
#    StartTime="${i#*=}"
#    ;;
#    -et=*)
#    EndTime="${i#*=}"
#    ;;
    -tmpdir=*)
    tmpdiri="${i#*=}"
    ;;
    -outdir=*)
    outdiri="${i#*=}"
    ;;
esac
done

echo "argument list:"
echo " "
#echo date=${date}
#echo run=${run}
#echo StartTime=${StartTime}
#echo EndTime=${EndTime}
echo leftlon=${leftlon}
echo rightlon=${rightlon}
echo toplat=${toplat}
echo bottomlat=${bottomlat}

TEMPDATAPATH=$tmpdiri
echo "~~~~~~~~~~~~~~~~~"
echo "temp folder:"
echo "$TEMPDATAPATH"
echo "~~~~~~~~~~~~~~~~~"

OUTDATAPATH=$outdiri
echo "~~~~~~~~~~~~~~~~~"
echo "output folder:"
echo "$OUTDATAPATH"
echo "~~~~~~~~~~~~~~~~~"


cd $OUTDATAPATH/../data

cdo -ntime temp.nc > timestep.txt

timestep=`cat timestep.txt`

cdo -seltimestep,$timestep temp.nc lasttemp.nc
cdo -s showdate lasttemp.nc > list.txt

tiempos=`cat list.txt`

datum1=`date -d $tiempos +%s`
datum2=`date +%s`

dif=$(( $datum2 - $datum1))
#echo $dif

days=$(( (($dif / (60 * 60 * 24)) - 1) ))
#echo $days

if [ $days -gt 1 ] 
then

cd $OUTDATAPATH
rm *

cd $TEMPDATAPATH
rm *

for i in $(seq ${days} -1 2)
do

ym=`date --date=${i}" days ago" +%Y%m`
d=`date --date=${i}" days ago" +%d`
y=`date --date=${i}" days ago" +%Y`
m=`date --date=${i}" days ago" +%m`

set URL="https://thredds.ucar.edu/thredds/ncss/grib/NCEP/GFS/Global_0p5deg_ana/GFS_Global_0p5deg_ana_" 

for hracb in 00 06 12 18
do

echo "start download...."
#curl "${URL}${ym}${d}_${hracb}00.grib2?var=Geopotential_height_isobaric&north=15&west=0.0000&east=359.5000&south=-70&disableProjSubset=on&horizStride=1&time_start=${y}-${m}-${d}T${hracb}%3A00%3A00Z&time_end=${y}-${m}-${d}T${hracb}%3A00%3A00Z&timeStride=1&vertCoord=50000&accept=netcdf" -o gfs.${y}${m}${d}_${hracb}.nc

curl "${URL}${ym}${d}_${hracb}00.grib2?var=Geopotential_height_isobaric&north=${toplat}&west=${leftlon}&east=${rightlon}&south=${bottomlat}&horizStride=1&time_start=${y}-${m}-${d}T${hracb}%3A00%3A00Z&time_end=${y}-${m}-${d}T${hracb}%3A00%3A00Z&timeStride=1&vertCoord=50000&accept=netcdf" -o gfs.${y}${m}${d}_${hracb}.nc

done
done

find . -name "gfs*" -type 'f' -size -160k -delete

cdo mergetime gfs* recover_analysis.nc

rm gfs*

mv recover_analysis.nc $OUTDATAPATH

cd $OUTDATAPATH

cdo chname,Geopotential_height_isobaric,gh recover_analysis.nc aux.nc
rm recover_analysis.nc
cdo invertlat aux.nc recover_analysis.nc
rm aux.nc

mv recover_analysis.nc $OUTDATAPATH/../data

cd $OUTDATAPATH/../data
mv temp.nc aux.nc
cdo mergetime aux.nc recover_analysis.nc temp.nc
mv aux.nc $OUTDATAPATH/../data/backup
echo "The file temp.nc was updated"
echo "recoverSETI.csh finished"
echo " "

else

echo "The file temp.nc is already updated"
echo "recoverSETI.sh finished"
echo " "

fi

