#!/bin/bash

###################################################
# 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.f90.                     #
# This interface assumes low-endian architecture, #
# i.e. x86 Intel or AMD, because MM5 bytes        #
# are swapped after reading in the prepmm5.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-WRF"
fnoARGS="prepwrf.nml"

[ -s ${fniARGS} ]|| \
    { echo "interf-WRF: 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 WRF 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

#MAKE                        # Required utilities
#AWK
#NCDUMP

#garbagedir                  # where to put make.log files

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

# MAKE,AWK and NCDUMP are set by the calling script. We check it again
${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-WRF clean >/dev/null 2>&1
${MAKE} -f Makefile-WRF  > ${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 ; }
[ -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 prepwrf.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 "fn_wrf      = '"${meteo_file}"'"                >> ${fnoARGS}
echo "fn_out      = '"${meteo_file_out}"'"            >> ${fnoARGS}
echo "/"                                              >> ${fnoARGS}

./prepwrf.e

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

