#!/bin/bash

#####################################################
# General ECMWF-to-chimere interface calling script #
# One must provide   arguments to the script (see   #
# just below). This script extracts data from an    #
# ECMWF output directory and constructs             #
# a netCDF meteo file for diagmet.f90               #
# It also creates a METINFOS namelist file          #
#  to be read by diagmet.f90.                       #
#####################################################

# This script can be called from the main CHIMERE script, or can be run in stand-alone
# mode for testing purpose. It does not use CHIMERE environment variables.

# Arguments to this script are located in a file named ${fniARGS}.

fniARGS="metargs-ecm"
fnoARGS="prepecm.nml"

fnlsmoro="${chimere_root}/meteo/LSMORO_GEMS.GRB"

[ -s ${fniARGS} ]|| \
    { echo "interf-ecm: Non existent ${fniARGS} file. Exiting"; exit 1; }
rm -Rf ${fnoARGS}

# Sourcing ${fniARGS} to get arguments
. ${fniARGS}

[ -d $chimere_tmp ]||{ echo "interf-ecm: Non existent directory $chimere_tmp. Exiting"; exit 1; } 
echo "interf-ecm: Using $chimere_tmp as a temporary directory"

${MAKE} --version 2>/dev/null >/dev/null || \
    { echo "You need gmake to run CHIMERE. Bye ..."; exit 1; }
${AWK} --version 2>/dev/null >/dev/null || \
    { echo "You need gawk to run CHIMERE. Bye ..."; exit 1; }
which ${NCDUMP} 2>/dev/null >/dev/null  || \
    { echo "You need ncdump to run CHIMERE. Bye ..."; exit 1; }

# Compilation

${MAKE} -f Makefile-ecm clean >/dev/null 2>&1
${MAKE} -f Makefile-ecm > ${garbagedir}/make.$$.log 2>&1 
if [ $? -ne 0 ] ; then
        echo
        echo "Compilation of meteo inteface aborted"
        echo "Check file ${garbagedir}/make.$$.log"
        echo
        exit 1
fi

# Extraction of all data from meteo file

rm -f ${meteo_file_out}
[ -s ${fncoo} ] || { echo "*** CHIMERE COORD FILE ${fncoo} empty" ; exit 1 ; }


# g95 requires setting a run-time environment variable to read big-endian files
G95_ENDIAN=BIG
export G95_ENDIAN

# Build args namelist for prepecm.e
echo "&args"                                          >  ${fnoARGS}
echo "ids         = ${sd0}"                           >> ${fnoARGS}
echo "nhours      = ${nhours}"                        >> ${fnoARGS}
echo "domain      = '"${chimere_dom}"'"               >> ${fnoARGS}
echo "fn_coo      = '"$fncoo"'"                       >> ${fnoARGS}
echo "nzonal      = ${nzonal}"                        >> ${fnoARGS}
echo "nmerid      = ${nmerid}"                        >> ${fnoARGS}
echo "ecmdir      = '"${meteo_file}"'"                >> ${fnoARGS}
echo "fn_lsmoro   = '"${fnlsmoro}"'"                  >> ${fnoARGS}
echo "fn_out      = '"${meteo_file_out}"'"            >> ${fnoARGS}
echo "/"                                              >> ${fnoARGS}

echo "check namelist:"
cat ${fnoARGS}

curdir=`pwd`

echo "Untar ECMWF data"
cd ${meteo_file}
tar xvf 2d.${di}.tar
tar xvf flx.${di}.tar
tar xvf soil.${di}.tar
tar xvf surf.${di}.tar
tar xvf uvtqz1.${di}.tar
tar xvf uvtqz2.${di}.tar
tar xvf uvtqz3.${di}.tar
tar xvf uvtqz4.${di}.tar
tar xvf uvtqz5.${di}.tar
tar xvf uvtqz6.${di}.tar
tar xvf uvtqz7.${di}.tar
tar xvf uvtqz8.${di}.tar
tar xvf wat1.${di}.tar
tar xvf wat2.${di}.tar
tar xvf wat3.${di}.tar
tar xvf wat4.${di}.tar
wait
\rm *.tar
wait

cd $curdir
pwd


./prepecm.e

$NCDUMP -h ${meteo_file_out} 2>/dev/null >/dev/null || \
    { echo "prepecm.e failed. Exiting ..."; exit 1; }

# Definition of variables
show=`expr ${nzonal} \* ${nmerid}`
show=`expr ${show} / 2`
show=`expr ${show} + ${nzonal}`

# Building the METINFOS file
rm -f ${fnmetinfos}
sed -e s/_START_/${sd0}/      \
    -e s/_SHOW_/${show}/      \
    -e s/_URBOPT1_/${u1}/     \
    -e s/_URBOPT2_/${u2}/     \
    -e s/_URBOPT3_/${u3}/     \
    -e s/_EROS_/${iopt_eros}/ \
    -e s/_RESU_/${iopt_resu}/ \
    METINFOS-ecm.nml.sed > ${fnmetinfos}

