#!/bin/bash

###################################################
# General MM5-to-chimere interface calling script 
# >>> script doucmentation at the end of this page 
###################################################

# *** 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="./prepmm5.nml"
locdir=`pwd`

echo "... now in"
echo "  >> "${locdir}

echo "  o meteo input data listed in: "${fniARGS}


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

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

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

echo "  o files for MM5 domain "${fn_mdom}

# check data files
[ -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 ; }


echo "  o using MM5 domain in file " ${fn_mdom}

[ -d $chimere_tmp ]||{ echo "interf-MM5: Non existent directory $chimere_tmp. Exiting"; exit 1; } 
echo "    interf-MM5: using $chimere_tmp/meteo 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-MM5 clean >/dev/null 2>&1
${MAKE} -f Makefile-MM5  > ${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 prepmm5.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}

echo "   o namelist for this run: "
echo "   >>" `pwd`/${fnoARGS}

./prepmm5.e

$NCDUMP -h ${meteo_file_out} 2>/dev/null >/dev/null || \
    { echo "prepmm5.e failed. Exiting ..."; cat ${fnoARGS}; 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-mm5.nml.sed > ${fnmetinfos}
    
#========================================================================
#              INTERF meteo script documentation
#
#   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    
#
#========================================================================
#  script arguments:
# ------------------                        
#chimere_tmp                 # Temporary directory
#sd0                         # Starting date of CHIMERE simulation
#nhours                      # Number of hours of CHIMERE simulation
#metdom                        # Name of the MM5 domain (coordinates files are COORD_<metdom>
#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
