#!/bin/ksh
#SBATCH --qos=normal 

        # Specifies that your job will run in the queue (Quality Of 
        # Service) normal, which, if this option is not specified, is 
        # the default. 

#SBATCH --job-name=realtime_sund 

#SBATCH --mail-type=FAIL 
#SBATCH --mail-user=
        
        # Specifies that an email should be sent in case the job fails. 
        # Other options include BEGIN, END, REQUEUE and ALL (any state 
        # change). 

#SBATCH --export=MSJ_DAY,MSJ_MONTH,MSJ_YEAR,MSJ_BASETIME,MSJ_EXPVER

#SBATCH --time=01:00:00 

        # Specifies that your job may run up to HH:MM:SS of wall clock 
        # time. The job will be killed if it exceeds this limit. If 
        # time is not defined, the default limit for the queue (qos) 
        # will be used. 


#-------------------------------------------------------------------- 
# setting environment variables  
#-------------------------------------------------------------------- 
module load ecmwf-toolbox
module load netcdf4
module load cdo
module load lftp
set -xe

#-------------------------------------------------------------------- 
# commands to be executed  
#-------------------------------------------------------------------- 

cd $SCRATCHDIR                  # All the files created in this directory  
                                # will be deleted when the job terminates.  
                                # See also keyword initialdir above.  

#-------------------------------------------------------------------- 
# Set up the DATE and TIME for the metgrams from the MSJ_* variables 
# provided by ECaccess 
#-------------------------------------------------------------------- 

echo $MSJ_DAY
echo $MSJ_MONTH
echo $MSJ_YEAR
echo $MSJ_BASETIME
echo $MSJ_EXPVER

# Define the date used by the MARS request   
if [[ "$MSJ_YEAR" != "" && "$MSJ_MONTH" != "" && "$MSJ_DAY" != "" ]]        
then
     DATE=$MSJ_YEAR$MSJ_MONTH$MSJ_DAY  # SMS date, if defined  
else
     DATE='-1'                  # Use latest available 
fi

if [ "$MSJ_BASETIME" != "" ]           # Define the date used by the MARS request   
then
     TIME=$MSJ_BASETIME                # SMS basetime  
else
     echo "SMS base time not defined!"
     TIME='00'                         # Set to 12 UTC 
fi

if [ "$MSJ_EXPVER" != "" ]             # Define the expver to be used by the MARS request   
then
     EXPVER=$MSJ_EXPVER                # SMS EXPVER 
else
     echo "SMS experiment version not defined!"
     EXPVER='0001'                     # Set to operational "0001" 
fi

export TMPDIR=$SCRATCHDIR

mars <<EOF
retrieve,
    class    = od,
    stream   = oper,
    type     = fc,
    expver   = 1,
    levtype  = sfc,
    grid     = 0.1/0.1,
    step     = [payload removed: 395 characters],
    param    = 164.128,
    date     = $DATE,
    time     = $TIME,
    accuracy = 16,
    target   = "tcc.grib"
EOF

grib_get -p date,time -w step=240 tcc.grib|read date time

echo $date $time

mv tcc.grib ECMWF_tcc_${date}T${time}.grib 

#grib_to_netcdf -D NC_FLOAT tcc_$date-$time.grib -o tcc_$date-$time.nc

lftp -u synop,4svsEFP4K synop.lima-ftp.de -e "set ftp:ssl-allow no"<<eof
cd /default-website/ecgate/test_data
put ECMWF_tcc_${date}T${time}.grib
#put tcc_$date-$time.nc
quit
eof
