#!/bin/sh

###################################################
# General MM5-to-chimere interface calling script #
# One must provide   arguments to the script (see #
# just below). This script extracts data from a   #
# MM5 output file (MMOUT format) and constructs   #
# a netCDF meteo file for diagmet.f               #
# It also creates a METINFOS namelist file        #
#  to be read by diagmet.f.                       #
# This interface assumes low-endian architecture, #
# i.e. x86 Intel or AMD, because MM5 bytes        #
# are swapped after reading in the prepexmm5.f90  #
# program. This program must be changed when      #
# running on HP or Sun                            #
###################################################

# *** DO NOT FORGET TO COPY THE COORDINATES FILES COORD_<mm5_domain_name>
# *** AND COORD_<mm5_domain_name>_DOT in the meteo directory
# *** These files can be constructed beforehand using the make_mm5_coord script
# *** in the ../util directory. Edit the script itself for description of its use
# These 2 files must be named WGHTS
# Give below the correct information

# 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-mm5"
fnoARGS="prepexmm5.nml"

[ -s ${fniARGS} ]|| \
    { echo "interf-mm5: 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 MM5 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                  # Input MMOUT file absolute address
#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
#iopt_eros                   # Optional dust erosion
#iopt_resu                   # Optional resuspension

fn_mdom=COORD_${mdom}
fn_mdom_dot=COORD_${mdom}_DOT

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

# Compilation

MAKE=`which gmake 2>/dev/null`
[ $? -ne 0 ]&& \
    { echo "You need gmake to run CHIMERE. Use make at your own risk"; MAKE=`which make`; }
AWK=`which gawk 2>/dev/null`
[ $? -ne 0 ]&& \
    { echo "You need gawk to run CHIMERE. use awk at your own risk"; AWK=`which awk`; }
NCDUMP=`which ncdump 2>/dev/null`
[ $? -ne 0 ]&& { echo "You need ncdump to run CHIMERE ! "; echo "Bye ..."; exit 1; }

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

# Extraction of all data from meteo file

rm -f ${meteo_file_out}
[ -s ${fn_mdom} ] || \
    { echo "*** MM5 COORD FILE  ${fn_mdom} empty" ; exit 1 ;}
[ -s ${fn_mdom_dot} ] || \
    { echo "*** MM5 COORD FILE ${fn_mdom_dot} empty" ; exit 1 ; }
[ -s ${fncoo} ] || { echo "*** CHIMERE COORD FILE ${fncoo} empty" ; exit 1 ; }
[ -s ${meteo_file} ] || { echo "*** METEO FILE ${meteo_file} 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 prepexmm5.e
echo "&args"                                          >  ${fnoARGS}
echo "ids         = ${sd0}"                           >> ${fnoARGS}
echo "nhours      = ${nhours}"                        >> ${fnoARGS}
echo "domain      = '"${chimere_dom}"'"               >> ${fnoARGS}
echo "fn_mdom     = '"${fn_mdom}"'"                   >> ${fnoARGS}
echo "fn_mdom_dot = '"${fn_mdom_dot}"'"               >> ${fnoARGS}
echo "fn_coo      = '"$fncoo"'"                       >> ${fnoARGS}
echo "nzonal      = ${nzonal}"                        >> ${fnoARGS}
echo "nmerid      = ${nmerid}"                        >> ${fnoARGS}
echo "fn_mm5      = '"${meteo_file}"'"                >> ${fnoARGS}
echo "fn_out      = '"${meteo_file_out}"'"            >> ${fnoARGS}
echo "/"                                              >> ${fnoARGS}

./prepexmm5.e

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

# Definition of variables
show=`$AWK 'BEGIN {n=1} $1=="'${chimere_dom}'" {n=$4} END {print n}' ${domains}`

# 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-mm5.nml.sed > ${fnmetinfos}

