#!/bin/ksh

# This Job has been ported to the new server ecgate
# Starting on 2004-06-29, 4-V Data with step 09 are no longer available
# in the Mars Database. This script has been modified accordingly
#
#@ shell       = /usr/bin/ksh

# NOTE: If calculation on Gaussian grid are required below, it is
#       recommended to send the job to the ECMWF HPC facility
# NOTE: On hpce the class should be ns or np
# NOTE: On ecgate it should be normal

#@ class        = normal
#@ resources    = ConsumableCpus(1) ConsumableMemory(800MB)

#@ job_name     = flex__leo_ecmwf
#@ output       = $(job_name).$(host).$(jobid).out
#@ error        = $(job_name).$(host).$(jobid).out
# @ environment = COPY_ALL

#@ queue

# ON ECGATE:
# start with ecjput ecgate flex_ecmwf on gateway server
# start with llsubmit flex_ecmwf on ecgate

# ON HPCE:
# start with ecjput hpce flex_ecmwf on gateway server
# start with llsubmit flex_ecmwf on hpce


set -x

JOBNAME=flex_ecmwf_${HOST}

env
ulimit -a
export OMP_NUM_THREADS=1

#export SCRATCH=$TEMP
export SCRATCHDIR=${SCRATCH}/${JOBNAME}_$$

mkdir $SCRATCHDIR
cd $SCRATCHDIR

LOG_FILE=$SCRATCHDIR/${JOBNAME}_$$
exec 1>${LOG_FILE}

CONTROLFILE=./CONTROL_ERA

###################################
#BEGIN: modification of config file
###################################

cat <<EOF >CONTROL_ERA
DAY1 20111101
DAY2 20111102
DTIME 3
FORCE_EI 1
M_GRID 20  
M_LEFT -1000
M_LOWER 3000
M_UPPER 6000
M_RIGHT 3000
M_LEVEL 60
M_RESOL 255
M_GAUSS 1
M_ACCURACY 24
M_OMEGA 1
M_OMEGADIFF 1
M_SMOOTH 0
M_ADDPAR
M_FORMAT GRIB2
GATEWAY ecaccess.ecmwf.int
DESTINATION leo@genericSftp
ECSTORAGE 1
ECTRANS 0
ECFSDIR ectmp:/${USER}/econdemand/
MAILOPS ${USER}
MAILFAIL ${USER}
EXEDIR .
SOURCECODE ecgate:flex_extract_ecgate_V4
EOF

#################################
#END: modification of config file
#################################

# no changes below

NRW=0

#ksh scripts for date manipulation
juldate2()
   {
    let jc=$1
    if (( ${#jc} < 8 ))
    then
    print "illegal date!"
    exit 1
    fi
    let y=`echo $jc | cut -c1-4`
    let m1=`echo $jc | cut -c5`
    let m2=`echo $jc | cut -c6`
    m=$m1$m2
    let d1=`echo $jc | cut -c7`
    let d2=`echo $jc | cut -c8`
    d=$d1$d2
    let jd=367*y-7*(y+(m+9)/12)/4+275*m/9+d+1721014
    let jd=jd+15-3*((y+(m-9)/7)/100+1)/4
    print $jd
    }

civildate2()
   {
    let jd=$1
    if (( jd < 1721060 ))
    then
       print "Julian date not in AD."
       exit 1
    fi
    let k=jd+68569
    let n=4*k/146097
    let k=k-\(146097*n+3\)/4
    let y=4000*(k+1)/1461001
    let k=k-1461*y/4+31
    let m=80*k/2447
    let d=k-\(2447*m\)/80
    let k=m/11
    let m=m+2-12*k
    let y=100*(n-49)+y+k
    [ $m -le 9 ] && m=0$m
    [ $d -le 9 ] && d=0$d
    print $y$m$d
    }

date2m1()
    {
    let ymd=$1
    let j1=`juldate2 $ymd`
    let j0=j1-1
    civildate2 $j0
    }

date2p1()
    {
    let ymd=$1
    let j1=`juldate2 $ymd`
    let j0=j1+1
    civildate2 $j0
    }

date2m14()
    {
    let ymd=$1
    let j1=`juldate2 $ymd`
    let j0=j1-14
    civildate2 $j0
    }

myerror()
  {
   
    echo $1
    echo $2
    echo $3
    echo ABORT!

    for MUSER in $MAILFAIL
    do
      mailx -s ERROR:${JOBNAME} ${MUSER} <${LOG_FILE}
    done
  }


#read CONTROL file, process specifications
while read NAME PARA
do

eval "export $NAME='$PARA'"
echo `echo $NAME`=$PARA

done <${CONTROLFILE}

if [ -z "$DAY1" -o -z "$DAY2" ]; then
    myerror 'DAY specification missing !'
    exit 1
fi

#defaults
[ -z "$FORCE_E40" ] && FORCE_E40=0
[ -z "$FORCE_EI" ] && FORCE_EI=1
[ -z "$FORCE_EPS" ] && FORCE_EPS=0
[ -z "$M_EXPVER" ] && M_EXPVER=1
[ -z "$M_STREAM" ] && M_STREAM=OPER
[ -z "$DTIME" ] && DTIME=6
[ -z "$M_NUMBER" ] && M_NUMBER=1
[ -z "$M_GRID" ] && M_GRID=20
[ -z "$M_LOWER" ] && M_LOWER=3000
[ -z "$M_LEFT" ] && M_LEFT=-1000
[ -z "$M_UPPER" ] && M_UPPER=6000
[ -z "$M_RIGHT" ] && M_RIGHT=3000
[ -z "$M_LEVEL" ] && M_LEVEL=91
[ -z "$M_LEVMIN" ] && M_LEVMIN=1
[ -z "$M_LEVMAX" ] && M_LEVMAX=91
[ -z "$M_ADDPAR" ] && M_ADDPAR=''
[ -z "$M_RESOL" ] &&  M_RESOL=799
[ -z "$M_GAUSS" ] &&  M_GAUSS=0
[ -z "$M_SMOOTH" ] &&  M_SMOOTH=0
[ -z "$M_OMEGA" ] &&  M_OMEGA=0
[ -z "$M_OMEGADIFF" ] &&  M_OMEGADIFF=0
[ -z "$M_ACCURACY" ] &&  M_ACCURACY=24
[ -z "$M_FORMAT" ] &&  M_FORMAT=GRIB2
[ -z "$EXEDIR" ] && EXEDIR=.
[ -z "$SOURCECODE" ] && SOURCECODE=ecgate:flex_extract_ecgate_V2
[ -z "$GATEWAY" ] && GATEWAY=''
[ -z "$DESTINATION" ] && DESTINATION=''
[ -z "$ECTRANS" ] && ECTRANS=1
[ -z "$ECSTORAGE" ] && ECSTORAGE=1
[ -z "$ECFSDIR" ] && ECFSDIR=ectmp:
[ -z "$MAILOPS" ] && MAILOPS=${USER}
[ -z "$MAILFAIL" ] && MAILFAIL=${USER}

#dates for switch from ERA40 to OPS data
ERA40START=19570902 #19570901
ERA40END=20001013
VAR12START=20000913 #20000912

#model changes
RES511=20001121

#additional dates for flux retrievals (polynomial interpolation)
DAY1M1=`date2m1 ${DAY1}`
DAY2P1=`date2p1 ${DAY2}`

#julian dates for time loops
JULDAY1=`juldate2 ${DAY1}`
JULDAY2=`juldate2 ${DAY2}`

echo JULDATES $JULDAY1 $JULDAY2

if [ $M_OMEGADIFF -eq 1 ] ; then
  M_OMEGA=1
fi

#check consistency
if [ ${DAY1} -gt ${DAY2} ]; then
    `myerror "ERROR: DAY1 > DAY2: ${DAY1}, ${DAY2}"`
    exit 1
fi

if [ ${DAY1} -lt ${ERA40START} ]; then
    `myerror "ERROR: DAY1 < ERA40START ${DAY1} ${ERA40START}"`
    exit 1
fi

if [ ${FORCE_E40} -eq 1 -a ${FORCE_EI} -eq 1 ]; then
    `myerror "ERROR: Cannot force ERA-Interim and ERA-40 at same time"`
    exit 1
fi

#force ERA40
[ ${FORCE_E40} -eq 1 ] && echo "\n"!!! use of ERA40 date forced !!!"\n"


#force ERA Interim
[ ${FORCE_EI} -eq 1 ] && echo "\n"!!! use of ERA Interim date forced !!!"\n"

#force EPS Control forecasts
[ ${FORCE_EPS} -eq 1 ] && echo "\n"!!! use of EPS forecasts forced !!!"\n"


#determine analysis type
  if [ ${FORCE_EPS} -eq 1 ]; then
  M_DATA=EPS
  M_STREAM=ENFO
  M_CLASS=OPER
  if [ ${M_NUMBER} -eq 1 ] ; then
     echo "\n"!!! High resolution control forecast used !!!"\n"
  else
     echo "\n"!!! Low resolution control forecast used !!!"\n"
  fi
    M_TYPE=CV
    M_TYPE2=CV
    M_TYPE3=CV
    M_LEVELIST=1/TO/${M_LEVEL}
    #temporal resolution
    if [ $DTIME -eq 3 ]; then
    M_TIME=00/12
        M_TIME2=00/12
        M_TIME3=00/12
    elif [ $DTIME -eq 6 ]; then
    M_TIME=00/12
    M_TIME2=-1
    M_TIME3=-1
    else
      `myerror "ERROR: DTIME $DTIME not supported!"`
      exit 1
    fi
    M_STEP=00/06
    M_STEP2=03
    M_STEP3=09
    SUFF=.[step]'00'
    TLIST1='0 1200'
  elif [ ${FORCE_EI} -eq 1 ]; then
  M_DATA=VAR12EI
  M_CLASS=EI
  M_EXPVER=1112
    M_TYPE=AN
    M_TYPE2=FC
    M_TYPE3=FC
    M_LEVELIST=1/TO/${M_LEVEL}
    #temporal resolution
    if [ $DTIME -eq 3 ]; then
    M_TIME=00/06/12/18
        M_TIME2=00/12
        M_TIME3=00/12
    elif [ $DTIME -eq 6 ]; then
    M_TIME=00/06/12/18
    M_TIME2=-1
    M_TIME3=-1
    else
      `myerror "ERROR: DTIME $DTIME not supported!"`
      exit 1
    fi
    M_STEP=00
    M_STEP2=03
    M_STEP3=09
    SUFF=.[step]'00'
    TLIST1='0 600 1200 1800'
  elif [ ${DAY2} -le ${ERA40END} -o ${FORCE_E40} -eq 1 ]; then
    M_DATA=ERA40
    M_CLASS=E4
    M_TYPE=AN
    M_TYPE2=FC
    M_EXPVER=1
    M_LEVEL=60
    M_LEVELIST=1/TO/60
    M_RESOL=159
    M_TIME=00/06/12/18
    M_TIME2=00/06/12/18
    M_STEP=00
    M_STEP2=03
    M_STEP3=03
    TLIST1='0 600 1200 1800'
    SUFF=.[step]'00'
elif [ ${DAY1} -ge ${VAR12START} ]; then
    M_DATA=VAR12
    M_CLASS=OD
    M_TYPE=AN
    M_TYPE2=FC
    M_TYPE3=4V
    M_EXPVER=1
    M_LEVELIST=1/TO/${M_LEVEL}
    #temporal resolution
    if [ $DTIME -eq 3 ]; then
    M_TIME=00/06/12/18
        M_TIME2=00/12
        M_TIME3=09/21
    elif [ $DTIME -eq 6 ]; then
    M_TIME=00/06/12/18
    M_TIME2=-1
    M_TIME3=-1
    else
      `myerror "ERROR: DTIME $DTIME not supported!"`
      exit 1
    fi
    #model changes
    if [ ${DAY1} -lt ${RES511} -a ${DAY2} -ge ${RES511} ]; then
    echo 'WARNING: HORIZ RESOLUTION CHANGED 319 -> 511 on' ${RES511}
    (( NRW = NRW + 1 ))
    fi
    M_STEP=00
    M_STEP2=03
    M_STEP3=00
    SUFF=.[step]'00'
    TLIST1='0 600 1200 1800'
else
    `myerror "spec error:  ${DAY1}, ${DAY2}" 'ERA40      19570902 -- 20001012' '12h 4D-VAR 20000912 onwards'`
fi


echo '\n'
echo Requesting $M_DATA data ...
echo '\n'

#temporal resolution
if [ $DTIME -eq 3 ]; then
     TIMELIST='00 03 06 09 12 15 18 21'
elif [ $DTIME -eq 6 ]; then
    TIMELIST='00 06 12 18'
else
    `myerror "ERROR: DTIME $DTIME not supported!"`
    exit 1
fi


# determine number of gridpoints and whether grid is cyclic
ZYK=`expr \( $M_RIGHT + 36000 \) % 36000 - \( $M_LEFT + 36000 \) % 36000 + $M_GRID`

if [[ $ZYK -ne 0 ]] ; then
  MAXB=$((($M_UPPER-($M_LOWER))/$M_GRID+1))
  MAXL=$((($M_RIGHT-($M_LEFT))/$M_GRID+1))

  if [[ $((($M_RIGHT-($M_LEFT))%$M_GRID)) -ne 0 || $((($M_UPPER-($M_LOWER))%$M_GRID)) -ne 0 ]] ; then
    myerror "ERROR: LAT/LON RANGE MUST BE INTEGER MULTIPLES OF GRID SIZE"     "URLO: $M_UPPER  $M_RIGHT $M_LOWER  $M_LEFT GRID: $M_GRID"     "GRID ASSUMED NON-CYCLIC"
    exit 1
  fi
else
  MAXL=$((($M_RIGHT-($M_LEFT))/$M_GRID+1))
  MAXB=$((($M_UPPER-($M_LOWER))/$M_GRID+1))
  if [[ $((($M_RIGHT-($M_LEFT))%$M_GRID)) -ne 0 || $((($M_UPPER-($M_LOWER))%$M_GRID)) -ne 0 ]] ; then
    myerror "ERROR: LAT/LON RANGE MUST BE INTEGER MULTIPLES OF GRID SIZE"           "URLO: $M_UPPER  $M_RIGHT $M_LOWER  $M_LEFT GRID: $M_GRID"           "GRID ASSUMED CYCLIC"
    exit 1
  fi
fi

if [ $((36000/$M_GRID-$M_RESOL)) -lt 0 ] ; then

   if [ ${M_SMOOTH} -eq 0 ] ; then
    myerror "ERROR: OUTPUT GRID SPACING OF ${M_GRID} HUNDREDTH DEGREE IS TOO COARSE FOR GIVEN SPECTRAL RESOLUTION ${M_RESOL} " "USE M_SMOOTH FOR SMOOTHING OR FINER OUTPUT GRID"
    exit 1
   else
    if [ $((36000/$M_GRID-$M_SMOOTH)) -lt 0 ] ; then
      myerror "ERROR: OUTPUT GRID SPACING OF ${M_GRID} HUNDREDTH DEGREE IS TOO COARSE FOR GIVEN SMOOTHED SPECTRAL RESOLUTION ${M_SMOOTH} " 
    exit 1
    fi
   fi

fi

if [ $M_LEVMAX -gt $M_LEVEL ] ; then
  M_LEVMAX=$M_LEVEL
fi


# convert lat/lon to MARS format (degrees)
if [[ $M_LEFT -lt 0 ]] ; then
  LLLO=$(($M_LEFT / 100)).$((($M_LEFT)*(-1) % 100))
else
  LLLO=$(($M_LEFT / 100)).$(($M_LEFT % 100))
fi
if [[ $M_LOWER -lt 0 ]] ; then
  LLLA=$(($M_LOWER / 100)).$(($M_LOWER % 100))
else
  LLLA=$(($M_LOWER / 100)).$((($M_LOWER)*(-1) % 100))
fi
if [[ $M_RIGHT -lt 0 ]] ; then
  URLO=$(($M_RIGHT / 100)).$((($M_RIGHT)*(-1) % 100))
else
  URLO=$(($M_RIGHT / 100)).$(($M_RIGHT % 100))
fi
if [[ $M_UPPER -lt 0 ]] ; then
  URLA=$(($M_UPPER / 100)).$((($M_UPPER)*(-1) % 100))
else
  URLA=$(($M_UPPER / 100)).$(($M_UPPER % 100))
fi

M_AREA=${URLA}/${LLLO}/${LLLA}/${URLO}

if [ $M_GAUSS -eq 1 ] ; then
# Gaussian grid detected
  D_GRID=$(($M_GRID / 100)).$(($M_GRID % 100))
  G_GRID=OFF
  QG_GRID=OFF
  if [ $M_RESOL -le 799 ] ; then
    QG_GRID=$((($M_RESOL+1)/2))
  fi
else
  D_GRID=$(($M_GRID / 100)).$(($M_GRID % 100))
  G_GRID=$(($M_GRID / 100)).$(($M_GRID % 100))
  G_GRID=${G_GRID}/${G_GRID}
fi

if [ ${M_SMOOTH} -ne 0 ]; then
  OMRES=${M_SMOOTH}
else
  OMRES=${M_RESOL}
fi

#namelist
cat <<EOF >fort.4
&NAMGEN
   MAXL=${MAXL}, MAXB=${MAXB},
   MLEVEL=${M_LEVEL}, LEVOUT=${M_LEVEL}, LEVMIN=${M_LEVMIN}, LEVMAX=${M_LEVMAX},
   MNAUF=${M_RESOL},
   RLO0=${LLLO}, RLO1=${URLO}, RLA0=${LLLA}, RLA1=${URLA},
   MOMEGA=${M_OMEGA}, MOMEGADIFF=${M_OMEGADIFF},MGAUSS=${M_GAUSS},MSMOOTH=${M_SMOOTH}
/
&NAMFX2
   NX=${MAXL}, NY=${MAXB},
   MAXTIME=272,JPOLY=4,
   JHRF=${DTIME},
   RLO0=${LLLO}, RLA0=${LLLA}, DX=${D_GRID}, DY=${D_GRID}
/
EOF

rcp ${SOURCECODE}/source.tar .
tar -xvf source.tar
make -f Makefile.IBM FLXACC2 CONVERT2 CHECK

if [ $? -ne 0 ]; then
    ls
    echo 'ERROR: FLXACC2 and CONVERT2 could not be compiled:' ${INFILE}
    echo ABORT!
    for MUSER in $MAILFAIL
    do
      mailx -s ERROR:${JOBNAME} ${MUSER} <${LOG_FILE}
    done
    exit 1
else
  echo 'compile worked'
fi

#
#MARS requests (field)
#

cat <<EOF > mf_1
RETRIEVE,
TYPE=${M_TYPE},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=U/V,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${G_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME},
STEP=${M_STEP},
TARGET="UV_G.[date].[time]${SUFF}"
EOF


cat <<EOF > mf_2
RETRIEVE,
TYPE=${M_TYPE2},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=U/V,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${G_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME2},
STEP=${M_STEP2},
TARGET="UV_G.[date].[time]${SUFF}"
EOF

if [ ${M_DATA} = "VAR12" ]; then
cat <<EOF > mf_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=U/V,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${G_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=00,
TARGET="UV_G.[date].[time]"
EOF
fi

if [ ${M_DATA} = "VAR12EI" -o ${M_DATA} = "EPS" ]; then
cat <<EOF > mf_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=U/V,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${G_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=${M_STEP3},
TARGET="UV_G.[date].[time].[step]00"
EOF
fi

cat <<EOF > mf_t_1
RETRIEVE,
TYPE=${M_TYPE},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=T,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME},
STEP=${M_STEP},
TARGET="T_G.[date].[time]${SUFF}"
EOF


cat <<EOF > mf_t_2
RETRIEVE,
TYPE=${M_TYPE2},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=T,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME2},
STEP=${M_STEP2},
TARGET="T_G.[date].[time]${SUFF}"
EOF

if [ ${M_DATA} = "VAR12" ]; then
cat <<EOF > mf_t_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=T,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=00,
TARGET="T_G.[date].[time]"
EOF
fi

if [ ${M_DATA} = "VAR12EI" -o  ${M_DATA} = "EPS" ]; then
cat <<EOF > mf_t_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=T,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=${M_STEP3},
TARGET="T_G.[date].[time].[step]00"
EOF
fi

#humidity on Gaussian grid for initialization
#of spectral transformations
if [ ${M_GAUSS} -eq 1 ] ; then
NGRID=$(( ( ${M_RESOL} + 1 ) / 2 ))
cat <<EOF >mf_qg_1
RETRIEVE,
TYPE=${M_TYPE},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=Q,
RESOL=${M_RESOL},
AREA=G,
GAUSSIAN=REDUCED,
GRID=${QG_GRID},
LEVTYPE=ML,
LEVELIST=1,
ACCURACY=${M_ACCURACY}, REPRES=GG,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME},
STEP=00,
TARGET="QG_G.[date].[time]"
EOF

fi

#humidity on lat/lon grid
cat <<EOF >mf_q_1
RETRIEVE,
TYPE=${M_TYPE},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=Q,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=GG,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME},
STEP=${M_STEP},
TARGET="Q_G.[date].[time]${SUFF}"
EOF

cat <<EOF >mf_q_2
RETRIEVE,
TYPE=${M_TYPE2},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=Q,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=GG,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME2},
STEP=${M_STEP2},
TARGET="Q_G.[date].[time]${SUFF}"
EOF

if [ ${M_DATA} = "VAR12" ]; then
cat <<EOF >mf_q_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=Q,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=GG,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=00,
TARGET="Q_G.[date].[time]"
EOF
fi

if [ ${M_DATA} = "VAR12EI" -o  ${M_DATA} = "EPS" ]; then
cat <<EOF >mf_q_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=Q,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=GG,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=${M_STEP3},
TARGET="Q_G.[date].[time].[step]00"
EOF
fi

#divergence
cat <<EOF >mf_d_1
RETRIEVE,
TYPE=${M_TYPE},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=D,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${G_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME},
STEP=${M_STEP},
TARGET="D_G.[date].[time]${SUFF}"
EOF

cat <<EOF >mf_d_2
RETRIEVE,
TYPE=${M_TYPE2},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=D,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${G_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME2},
STEP=${M_STEP2},
TARGET="D_G.[date].[time]${SUFF}"
EOF

if [ ${M_DATA} = "VAR12" ]; then
cat <<EOF >mf_d_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=D,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${G_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=00,
TARGET="D_G.[date].[time]"
EOF
fi

if [ ${M_DATA} = "VAR12EI" -o ${M_DATA} = "EPS"  ]; then
cat <<EOF >mf_d_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=D,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=${G_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=${M_STEP3},
TARGET="D_G.[date].[time].[step]00"
EOF
fi

#pressure vertical velocity (omega)
if [ ${M_OMEGA} -eq 1 ]; then
cat <<EOF >mf_omega_1
RETRIEVE,
TYPE=${M_TYPE},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=W,
RESOL=${OMRES},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME},
STEP=${M_STEP},
TARGET="OMEGA_G.[date].[time]${SUFF}"
EOF

cat <<EOF >mf_omega_2
RETRIEVE,
TYPE=${M_TYPE2},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=W,
RESOL=${OMRES},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME2},
STEP=${M_STEP2},
TARGET="OMEGA_G.[date].[time]${SUFF}"
EOF

if [ ${M_DATA} = "VAR12" ]; then
cat <<EOF >mf_omega_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=W,
RESOL=${OMRES},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=00,
TARGET="OMEGA_G.[date].[time]"
EOF
fi

if [ ${M_DATA} = "VAR12EI" -o  ${M_DATA} = "EPS" ]; then
cat <<EOF >mf_omega_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=W,
RESOL=${OMRES},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=ML,
LEVELIST=${M_LEVELIST},
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=${M_STEP3},
TARGET="OMEGA_G.[date].[time].[step]00"
EOF
fi

fi

# surface pressure
cat <<EOF >mf_lnsp_1
RETRIEVE,
TYPE=${M_TYPE},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=LNSP,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=OFF,
LEVTYPE=ML,
LEVELIST=OFF,
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME},
STEP=${M_STEP},
TARGET="LNSP_G.[date].[time]${SUFF}"
EOF

cat <<EOF >mf_lnsp_2
RETRIEVE,
TYPE=${M_TYPE2},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=LNSP,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=OFF,
LEVTYPE=ML,
LEVELIST=OFF,
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME2},
STEP=${M_STEP2},
TARGET="LNSP_G.[date].[time]${SUFF}"
EOF

if [ ${M_DATA} = "VAR12" ]; then
cat <<EOF >mf_lnsp_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=LNSP,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=OFF,
LEVTYPE=ML,
LEVELIST=OFF,
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=00,
TARGET="LNSP_G.[date].[time]"
EOF
fi

if [ ${M_DATA} = "VAR12EI" -o  ${M_DATA} = "EPS" ]; then
cat <<EOF >mf_lnsp_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=LNSP,
RESOL=${M_RESOL},
AREA=${M_AREA},
GRID=OFF,
LEVTYPE=ML,
LEVELIST=OFF,
ACCURACY=${M_ACCURACY}, REPRES=SH,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=${M_STEP3},
TARGET="LNSP_G.[date].[time].[step]00"
EOF
fi

#surf parameters
cat <<EOF >ms_1
RETRIEVE,
TYPE=${M_TYPE},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=SD/MSL/TCC/10U/10V/2T/2D,
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=SFC,
ACCURACY=${M_ACCURACY}, REPRES=GG,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME},
STEP=${M_STEP},
TARGET="SURF.[date].[time]${SUFF}"
EOF

cat <<EOF >ms_2
RETRIEVE,
TYPE=${M_TYPE2},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=SD/MSL/TCC/10U/10V/2T/2D,
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=SFC,
ACCURACY=${M_ACCURACY}, REPRES=GG,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME2},
STEP=${M_STEP2},
TARGET="SURF.[date].[time]${SUFF}"
EOF

if [ ${M_DATA} = "VAR12" ]; then
cat <<EOF >ms_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=SD/MSL/TCC/10U/10V/2T/2D,
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=SFC,
ACCURACY=${M_ACCURACY}, REPRES=GG,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=00,
TARGET="SURF.[date].[time]"
EOF
fi

if [ ${M_DATA} = "VAR12EI" -o  ${M_DATA} = "EPS" ]; then
cat <<EOF >ms_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=SD/MSL/TCC/10U/10V/2T/2D,
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=SFC,
ACCURACY=${M_ACCURACY}, REPRES=GG,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=${M_STEP3},
TARGET="SURF.[date].[time].[step]00"
EOF
fi

#orography
cat <<EOF >ms_oro_1
RETRIEVE,
TYPE=${M_TYPE},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=129/172/160${M_ADDPAR},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=SFC,
ACCURACY=${M_ACCURACY}, REPRES=GG,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME},
STEP=${M_STEP},
TARGET="OROLSM.[date].[time]${SUFF}"
EOF

cat <<EOF >ms_oro_2
RETRIEVE,
TYPE=${M_TYPE2},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=129/172/160${M_ADDPAR},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=SFC,
ACCURACY=${M_ACCURACY}, REPRES=GG,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME2},
STEP=${M_STEP2},
TARGET="OROLSM.[date].[time]${SUFF}"
EOF

if [ ${M_DATA} = "VAR12" ]; then
cat <<EOF >ms_oro_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=129/172/160${M_ADDPAR},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=SFC,
ACCURACY=${M_ACCURACY}, REPRES=GG,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=00,
TARGET="OROLSM.[date].[time]"
EOF
fi

if [ ${M_DATA} = "VAR12EI" -o  ${M_DATA} = "EPS" ]; then
cat <<EOF >ms_oro_3
RETRIEVE,
TYPE=${M_TYPE3},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=129/172/160${M_ADDPAR},
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=SFC,
ACCURACY=${M_ACCURACY}, REPRES=GG,
DATE=${DAY1}/TO/${DAY2},
TIME=${M_TIME3},
STEP=${M_STEP3},
TARGET="OROLSM.[date].[time].[step]00"
EOF
fi

# Some parameters
# 160?stddev of orography
# 27,28 = low/high vegetation cover
# 173=surface roughness
# are not available for forecasts. 
# The patch below avoids nasty error messages
# Success of patch depends on setting of M_ADDPAR and M_ORO
# 
if [ ${M_TYPE} == 'FC' ] ; then
  sed s,160/27/28/173/,, ms_orox_1 > ms_oro_1
else
  mv ms_orox_1 ms_oro_1
fi
if [ ${M_TYPE2} == 'FC' ] ; then
  sed s,160/27/28/173/,, ms_orox_2 > ms_oro_2
else
  mv ms_orox_2 ms_oro_2
fi
if [ ${M_TYPE3} == 'FC' ] ; then
  sed s,160/27/28/173/,, ms_orox_3 > ms_oro_3
else
  mv ms_orox_3 ms_oro_3
fi

mars mf_1
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mf_t_1
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mf_d_1
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mf_q_1
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
if [ ${M_GAUSS} -eq 1 ] ; then
  mars mf_qg_1
  [ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
fi
mars mf_lnsp_1
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars ms_1
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars ms_oro_1
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
if [ ${M_OMEGA} -eq 1 ] ; then
  mars mf_omega_1
  [ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
fi

#get intermediate fields
if [ $DTIME -eq 3 ]; then

mars mf_2
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mf_t_2
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mf_d_2
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mf_q_2
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mf_lnsp_2
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars ms_2
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars ms_oro_2
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
if [ ${M_OMEGA} -eq 1 ] ; then
  mars mf_omega_2
  [ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
fi

if [ ${M_DATA} = "VAR12" -o ${M_DATA} = "VAR12EI"  -o ${M_DATA} = "EPS" ]; then
mars mf_3
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mf_t_3
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mf_d_3
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mf_q_3
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mf_lnsp_3
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars ms_3
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars ms_oro_3
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
if [ ${M_OMEGA} -eq 1 ] ; then
  mars mf_omega_3
  [ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
fi
fi

fi

#fix filenames (ERA40 FG fields)
if [ ${M_DATA} = "ERA40" -a ${DTIME} -eq 3 ]; then
for TIME in ${TLIST1}
do
    SUFFIX=.${TIME}.300
    for OLDNAME in *${SUFFIX}
    do
    (( NEWSUFFIX = ${TIME} + 300 ))
    NEWNAME=`echo $OLDNAME | sed "s/${SUFFIX}/.${NEWSUFFIX}/"`
    mv ${OLDNAME} ${NEWNAME}
    done
    SUFFIX=.${TIME}.000
    for OLDNAME in *${SUFFIX}
    do
    (( NEWSUFFIX = ${TIME} + 000 ))
    NEWNAME=`echo $OLDNAME | sed "s/${SUFFIX}/.${NEWSUFFIX}/"`
    mv ${OLDNAME} ${NEWNAME}
    done
done
fi

#fix filenames (VAR12 FC fields)

if [ ${M_DATA} = "VAR12" -a ${DTIME} -eq 3 ]; then
for TIME in ${TLIST1}
do
    SUFFIX=.${TIME}.300
    for OLDNAME in *${SUFFIX}
    do
        (( NEWSUFFIX = ${TIME} + 300 ))
        NEWNAME=`echo $OLDNAME | sed "s/${SUFFIX}/.${NEWSUFFIX}/"`
        mv ${OLDNAME} ${NEWNAME}
    done
    SUFFIX=.${TIME}.600
    for OLDNAME in *${SUFFIX}
    do
        (( NEWSUFFIX = ${TIME} + 600 ))
        NEWNAME=`echo $OLDNAME | sed "s/${SUFFIX}/.${NEWSUFFIX}/"`
        mv ${OLDNAME} ${NEWNAME}
    done
    SUFFIX=.${TIME}.900
    for OLDNAME in *${SUFFIX}
    do
        (( NEWSUFFIX = ${TIME} + 900 ))
        NEWNAME=`echo $OLDNAME | sed "s/${SUFFIX}/.${NEWSUFFIX}/"`
        mv ${OLDNAME} ${NEWNAME}
    done
done
fi

#fix filenames (VAR12 FC fields)

if [ ${M_DATA} = "VAR12EI" -a ${DTIME} -eq 3 ]; then
for TIME in ${TLIST1}
do
    SUFFIX=.${TIME}.300
    for OLDNAME in *${SUFFIX}
    do
        (( NEWSUFFIX = ${TIME} + 300 ))
        NEWNAME=`echo $OLDNAME | sed "s/${SUFFIX}/.${NEWSUFFIX}/"`
        mv ${OLDNAME} ${NEWNAME}
    done
    SUFFIX=.${TIME}.600
    for OLDNAME in *${SUFFIX}
    do
        (( NEWSUFFIX = ${TIME} + 600 ))
        NEWNAME=`echo $OLDNAME | sed "s/${SUFFIX}/.${NEWSUFFIX}/"`
        mv ${OLDNAME} ${NEWNAME}
    done
    SUFFIX=.${TIME}.900
    for OLDNAME in *${SUFFIX}
    do
        (( NEWSUFFIX = ${TIME} + 900 ))
        NEWNAME=`echo $OLDNAME | sed "s/${SUFFIX}/.${NEWSUFFIX}/"`
        mv ${OLDNAME} ${NEWNAME}
    done
done
fi


#fix filenames (EPS fields) always need a fix since 12 hourly

if [ ${M_DATA} = "EPS" -o  ${M_DATA} = "VAR12" -o  ${M_DATA} = "VAR12EI" ]; then
for TIME in ${TLIST1}
do
    SUFFIX=.${TIME}.000
    for OLDNAME in *${SUFFIX}
    do
        (( NEWSUFFIX = ${TIME} + 000 ))
        NEWNAME=`echo $OLDNAME | sed "s/${SUFFIX}/.${NEWSUFFIX}/"`
        mv ${OLDNAME} ${NEWNAME}
    done
    SUFFIX=.${TIME}.300
    for OLDNAME in *${SUFFIX}
    do
        (( NEWSUFFIX = ${TIME} + 300 ))
        NEWNAME=`echo $OLDNAME | sed "s/${SUFFIX}/.${NEWSUFFIX}/"`
        mv ${OLDNAME} ${NEWNAME}
    done
    SUFFIX=.${TIME}.600
    for OLDNAME in *${SUFFIX}
    do
        (( NEWSUFFIX = ${TIME} + 600 ))
        NEWNAME=`echo $OLDNAME | sed "s/${SUFFIX}/.${NEWSUFFIX}/"`
        mv ${OLDNAME} ${NEWNAME}
    done
    SUFFIX=.${TIME}.900
    for OLDNAME in *${SUFFIX}
    do
        (( NEWSUFFIX = ${TIME} + 900 ))
        NEWNAME=`echo $OLDNAME | sed "s/${SUFFIX}/.${NEWSUFFIX}/"`
        mv ${OLDNAME} ${NEWNAME}
    done
done
fi
#
#MARS requests (flux)
#

export FAC=16
cat <<EOF >mars_flux_1
RETRIEVE,
TYPE=${M_TYPE2},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=LSP/CP/SSHF/EWSS/NSSS/SSR,
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=SFC,
LEVELIST=OFF,
ACCURACY=${FAC}, REPRES=GG,
DATE=${DAY1M1}/TO/${DAY2P1},
TIME=00/12,
AC=N,
STEP=03,
TARGET="surf_03_ub"
EOF

cat <<EOF >mars_flux_2
RETRIEVE,
TYPE=${M_TYPE2},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=LSP/CP/SSHF/EWSS/NSSS/SSR,
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=SFC,
LEVELIST=OFF,
ACCURACY=${FAC}, REPRES=GG,
DATE=${DAY1M1}/TO/${DAY2P1},
TIME=00/12,
AC=N,
STEP=06,
TARGET="surf_06_ub"
EOF

cat <<EOF >mars_flux_3
RETRIEVE,
TYPE=${M_TYPE2},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=LSP/CP/SSHF/EWSS/NSSS/SSR,
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=SFC,
LEVELIST=OFF,
ACCURACY=${FAC}, REPRES=GG,
DATE=${DAY1M1}/TO/${DAY2P1},
TIME=00/12,
AC=N,
STEP=09,
TARGET="surf_09_ub"
EOF

cat <<EOF >mars_flux_4
RETRIEVE,
TYPE=${M_TYPE2},
CLASS=${M_CLASS},NUMBER=${M_NUMBER},
EXPVER=${M_EXPVER},STREAM=${M_STREAM},
PARAM=LSP/CP/SSHF/EWSS/NSSS/SSR,
AREA=${M_AREA},
GRID=${D_GRID}/${D_GRID},
LEVTYPE=SFC,
LEVELIST=OFF,
ACCURACY=${FAC}, REPRES=GG,
DATE=${DAY1M1}/TO/${DAY2P1},
TIME=00/12,
AC=N,
STEP=12,
TARGET="surf_12_ub"
EOF

mars mars_flux_1
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mars_flux_2
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mars_flux_3
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))
mars mars_flux_4
[ $? -ne 0 ] && echo WARNING: check MARS request && (( NRW = NRW + 1 ))

#
# FLXACC job
#
#input for FLXACC job


${EXEDIR}/FLXACC2

#
# CONVERT job
#

#loop over DATE and TIME for CONVERT/CHECK/ECtrans job

FSTEP=0

IJULDAY=${JULDAY1}
while [ $IJULDAY -le $JULDAY2 ];
do

MDATE=`civildate2 ${IJULDAY}`
MDATEX=`echo ${MDATE} | cut -c3-8`

echo $TIMELIST

for TIME in $TIMELIST
do

\rm fort.1[0-7]
\rm fort.19 fort.25


#take care of funny MARS file nameing
if [ $TIME -eq 00 ]; then
    XTIME=0
elif [ $TIME -lt 10 ]; then
    XTIME=`echo ${TIME} | cut -c2`00
else
    XTIME=${TIME}00
fi

echo $TIME $XTIME

#flux data
mv flux${MDATE}${TIME}      fort.16

#fields
mv UV_G.${MDATE}.${XTIME}  fort.10
mv VD_G.${MDATE}.${XTIME}  fort.30
mv T_G.${MDATE}.${XTIME}  fort.11
mv LNSP_G.${MDATE}.${XTIME} fort.12
mv D_G.${MDATE}.${XTIME}    fort.13
mv SURF.${MDATE}.${XTIME}   fort.14
mv Q_G.${MDATE}.${XTIME}    fort.17
mv QG_G.${MDATE}.${XTIME}    fort.18
#if [ ${M_OMEGA} -eq 1] ; then
  mv OMEGA_G.${MDATE}.${XTIME}  fort.19
#fi
# Topography etc data are only available within main
# analysis dates (0/6/12/18)
mv OROLSM.${MDATE}.0        fort.20

${EXEDIR}/CONVERT2

if [ -s fort.15 ]; then
    mv fort.15 EO179${MDATEX}${TIME}
    cat fort.14 >>  EO179${MDATEX}${TIME}
    cat fort.16 >>  EO179${MDATEX}${TIME}
    cat fort.20 >>  EO179${MDATEX}${TIME}
else
    echo ERROR: ENfile ${MDATEX}${TIME} missing!
    echo ABORT!
    for MUSER in $MAILFAIL
    do
      mailx -s ERROR:${JOBNAME} ${MUSER} <${LOG_FILE}
    done
    exit 1
fi

INFILE=EO179${MDATEX}${TIME}

\rm fort.15
ln -s ${INFILE} fort.15

if [ -s fort.25 ]; then
    mv fort.25 OMEGA${MDATEX}${TIME}
    ln -s  OMEGA${MDATEX}${TIME} fort.25
fi
#check ENxxx file & ECtrans to Vienna


[ -s CHECK.SUCCESS ] && rm CHECK.SUCCESS

${EXEDIR}/CHECK

#check fields
if [ -s CHECK.SUCCESS ]; then
    SUCCESS=1
else
    echo 'ERROR: check on ENfile failed:' ${INFILE}
    echo ABORT!
    for MUSER in $MAILFAIL
    do
      mailx -s ERROR:${JOBNAME} ${MUSER} <${LOG_FILE}
    done
    exit 1
fi

#
# Convert to GRIB2 if needed
#
ls -l ${INFILE}
cat >rule.filter<<EOF
if (shortName is "t" || shortName is "u" || shortName is "v" || shortName is "q"
|| shortName is "w" || shortName is "etadot" || shortName is "msl" || shortName is "sp" || shortName is
"10u" || shortName is "10v" || shortName is "2t" || shortName is "2d") {
  set edition=2;
    set packingType="grid_jpeg";
  write "[file]_2";
} else {
  write "[file]_2";
}
EOF

# Only conversion to GRIB2 is allowed, but no backward conversion
if [ ${M_FORMAT} == GRIB2 ] ; then
 grib_filter rule.filter ${INFILE}
 mv ${INFILE}_2 ${INFILE}
fi
ls -l ${INFILE}

#ECtrans
[ $SUCCESS -eq 1 -a $ECTRANS -eq 1 ] && ectrans -overwrite -gateway ${GATEWAY} -remote ${DESTINATION} -source $INFILE
[ $M_OMEGA -eq 1 -a $SUCCESS -eq 1 -a $ECTRANS -eq 1 ] && ectrans -overwrite -gateway ${GATEWAY} -remote ${DESTINATION} -source OMEGA${MDATEX}${TIME}

#ECFS
[ $SUCCESS -eq 1 -a $ECSTORAGE -eq 1 ] && ecp -o $INFILE $ECFSDIR
[ $SUCCESS -eq 1 -a $ECSTORAGE -eq 1 ] && ecp -o OMEGA${MDATEX}${TIME} $ECFSDIR

#done TIME
done

(( IJULDAY = IJULDAY + 1 ))

#done JULDAY
done

#any warnings ?
[ $NRW -gt 0 ] && echo There were $NRW warnings !

#mail logfile (list MAILOPS)
for MUSER in $MAILOPS
do

mailx -s ${JOBNAME} ${MUSER} <${LOG_FILE}
done

#
# cleanup
#
cd ${SCRATCH}
echo $SCRATCHDIR not removed!
\rm -fR $SCRATCHDIR

exit 0
