#!/bin/bash

#####################################################
# General AROME-to-chimere interface calling script #
# One must provide   arguments to the script (see   #
# just below). This script extracts data from an    #
# AROME 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-AROME"
fnoARGS="preparo.nml"

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

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

#chimere_tmp                 # Temporary directory
#sd0                         # Starting date of CHIMERE simulation
#nhours                      # Number of hours of CHIMERE simulation
#mdom                        # Name of the AROME domain (coordinates files are COORD_<mdom>
#domains                     # absolute pathname of the chimere DOMAINS file
#chimere_dom                 # name of chimere domain
#fncoo                       # absolute pathname of chimere domain coordinates file

#nzonal                      # length of the X dimension of CHIMERE domain
#nmerid                      # length of the Y dimension of CHIMERE domain

#meteo_file                  # Meteo GRIB files directory absolute address
#meteo_file_2d               # useless for AROME
#meteo_file_3d               # useless for AROME
#meteo_pfile                 # Useless for AROME
#simumode                    # "forecast", "mm5-tradi", "wrf-linked"
#meteo_file_out              # Output meteo file
#fnmetinfos                  # Output infos file (in general, METINFOS.nml)
#u1                          # Urban corrections of fluxes, wind, etc... (1=yes, 0=no)
#u2
#u3

#MAKE                        # Required utilities
#AWK
#NCDUMP

#garbagedir                  # where to put make.log files
#interp_meteo                # if meteo and chimere domains differ, we interpolate
#imakecompil                 # set it to 0 if you have a pre-compiled source tree
#arome_root                  # radix of AROME filenames
#lsm_suffix                  # suffix of AROME LSM filename
#oro_suffix                  # suffix of AROME OROgraphy filename




[ -d $chimere_tmp ]||{ echo "interf-AROME: Non existent directory $chimere_tmp. Exiting"; exit 1; } 
echo "interf-AROME: 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; }


# Interpolation ?
if [ ${interp_meteo} == yes ]; then
    ( cd src && ln -sf preparo-interp.F90 preparo.F90 )
else
    ( cd src && ln -sf preparo-nointerp.F90 preparo.F90 )
fi


# Compilation
if [ ${imakecompil} == 1 ]; then
    if [ ${interp_meteo} == yes ]; then
        ( cd src && ln -sf preparo-interp.F90 preparo.F90 )
    else
        ( ce src && ln -sf preparo-nointerp.F90 preparo.F90 )
    fi
    ( cd src && ${MAKE} -f Makefile-AROME clean >/dev/null 2>&1 )
    ( cd src && ${MAKE} -f Makefile-AROME  > ${garbagedir}/make.$$.log 2>&1 )
    if [ $? -ne 0 ] ; then
        echo
        echo "Compilation of meteo interface aborted"
        echo "Check file ${garbagedir}/make.$$.log"
        echo
        exit 1
    fi
    ln -s src/preparo.e .
else
    if [ ${interp_meteo} == yes ]; then
        ln -sf src/preparo-interp.e preparo.e
    else
        ln -sf src/preparo-nointerp.e preparo.e
    fi
fi


# Extraction of all data from meteo file

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

# linking analysis file to get regular names for all meteo files
#ln -sf ${meteo_file}/${arome_root}_A_000000.grb ${meteo_file}/${arome_root}_P_00.grb

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

# sigma coordinates file
fnsigma=$chimere_tmp/meteo/sigma_coeff_AROME

# Build args namelist for preparo.e
echo "&args"                                          >  ${fnoARGS}
echo "ids         = ${sd0}"                           >> ${fnoARGS}
echo "nhours      = ${nhours}"                        >> ${fnoARGS}
echo "domain      = '"${chimere_dom}"'"               >> ${fnoARGS}
echo "fn_coo      = '"$fncoo"'"                       >> ${fnoARGS}
echo "fn_sigma    = '"$fnsigma"'"                     >> ${fnoARGS}

echo "nzonal      = ${nzonal}"                        >> ${fnoARGS}
echo "nmerid      = ${nmerid}"                        >> ${fnoARGS}
echo "arodir      = '"${meteo_file}"'"                >> ${fnoARGS}
echo "fn_out      = '"${meteo_file_out}"'"            >> ${fnoARGS}
echo "arome_root  = '"${arome_root}"'"                >> ${fnoARGS}
echo "lsm_suffix  = '"${lsm_suffix}"'"                >> ${fnoARGS}
echo "oro_suffix  = '"${oro_suffix}"'"                >> ${fnoARGS}
echo "/"                                              >> ${fnoARGS}

echo "check namelist:"
cat ${fnoARGS}

./preparo.e

$NCDUMP -h ${meteo_file_out} 2>/dev/null >/dev/null || \
    { echo "preparo.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-aro.nml.sed > ${fnmetinfos}

