#!/bin/ksh +x
#**********************#
#                      #
#     Get_ENER_UVZ     #
#                      #
#**********************#
#
if [ "${1}" = "-h" ]
then
  echo " "
  echo " Reads zonal wind, meridional wind, temperature, specific humidity and"
  echo " geopotential height of the CasoInp with KmaxInp pressure levels and"
  echo " interpolate/extrapolate Vertically to KmaxOut pressure levels of CasoOut"
  echo " into a file with zonal wind, meridional wind and geopotential height."
  echo " If there is extrapolation the global mean of those variables the CasoClm is used."
  echo " "
  echo " Usage:"
  echo " ./Run_GEUVZ CasoInp KmaxInp [Epoch] [CaseStudy] [Mean] [DelFileInp] [MmaxOut] [GrdInp] [CasoClm]"
  echo " "
  echo " Default:"
  echo " CasoInp=MONAN ; KmaxInp=22 ; Epoch=1 ; CaseStudy=ZCAS ; Mean=no"
  echo " DelFileInp=.FALSE. ; MmaxOut=126 ; GrdInp=GrdReg CasoClm=ERA_5"
  echo " "
  echo " CasoInp : five letters input case identifier"
  echo " KmaxInp : three digits number of modes or levels"
  echo " Epoch : number idenfying the datei and datef"
  echo " CaseStudy : Name of the Case Study"
  echo " Mean : yes if the input is a time mean or no otherwise"
  echo " DelFileInp : .FALSE. to not delele input file or .TRUE. otherwise"
  echo " MmaxOut : Rhomb+1 (even), Rhomb is the zonal wave truncation (Rhomb odd number)"
  echo "           is the total number of zonal waves plus the zonal mean (+1)"
  echo " GrdInp : Input Regular Grid: GrdReg or Gaussian Grid: GrdGau"
  echo " CasoClm : five letters climatology case identifier"
  echo " "
  exit
fi
#
dircfg='../../../Config/'
#
. ${dircfg}Config_Dir
. ${dircfg}Reference_Case
#
if [ -z "${1}" ]
then
  CasoI='MONAN'
else
  CasoI=${1}
fi
if [ -z "${2}" ]
then
  KmxI=22
else
  KmxI=${2}
fi
if [ -z "${3}" ]
then
  epoca=1
else
  epoca=${3}
fi
if [ -z "${4}" ]
then
  csst='ZCAS'
else
  csst=${4}
fi
if [ -z "${5}" ]
then
  mn=''
else
  mn=${5}
fi
if [ -z "${6}" ]
then
  DFI=.FALSE.
else
  DFI=${6}
fi
if [ -z "${7}" ]
then
 Mmxo=126
else
 Mmxo=${7}
fi
if [ -z "${8}" ]
then
 Grd='GrdReg'
else
 Grd=${8}
fi
if [ -z "${9}" ]
then
 Clm='ERA_5'
else
 Clm=${9}
fi
#
if [ "${mn}" != "yes" ]
then
  mn='no'
fi
if [ "${mn}" = "yes" ]
then
  fileg=${dircfg}'Dates_'${csst}'_mean.dgs'
else
  fileg=${dircfg}'Dates_'${csst}'.dgs'
fi
echo " "
echo " fileg = ${fileg}"
set -A vars `cat ${fileg}`
let nd=vars[0]
if [ ${epoca} -lt 1 ]
then
  echo " "
  echo " Wrong epoca : ${epoca}"
  echo " Must be >= 1 and <= ${nd}"
  echo " "
  exit 1
fi
if [ ${epoca} -gt ${nd} ]
then
  echo " "
  echo " Wrong epoca : ${epoca}"
  echo " Must be >= 1 and <= ${nd}"
  echo " "
  exit 2
fi
echo " "
ne=1
na=1
while [ ne -le nd ]
do
  let epoch=vars[na]
  let datei=vars[na+1]
  let datef=vars[na+2]
  if [ "${epoch}" -eq "${epoca}" ]
  then
    echo " epoch = ${epoch}  datei = ${datei}  datef = ${datef}"
    break
  fi
  let na=na+3
  let ne=ne+1
done
echo " epoca = ${epoca}  datei = ${datei}  datef = ${datef}"
echo " "
#
rm -f ./NameList
#
set -x
#
cat << eot > ./NameList
 &Intpnml
  CasoInp='${CasoI}',
  KmaxInp=${KmxI},
  CasoOut='${csvm}',
  KmaxOut=${Kmax},
  Datei='${datei}',
  Datef='${datef}',
  DelFileInp=${DFI}
  MmaxOut=${Mmxo},
  GrdInp='${Grd}',
  CasoClm='${Clm}', 
  DirData='${dirdprcfg}'
 /
eot
#
typeset -Z3 kmxi
kmxi=${KmxI}
typeset -Z4 Mmxo1
let Mmxo1=${Mmxo}-1
#
ext=RB${Mmxo1}L${kmxi}_${CasoI}_${datei}_${datef}.out
out=../output/Get_ENER_UVZ_${ext}
#
rm -f ${out}
time ../bin/Get_ENER_UVZ.bin > ${out}
#
rm -f ./NameList
#
