#!/bin/bash

###################################################
# General WRF-to-chimere interface calling script #
# One must provide   arguments to the script (see #
# just below). This script extracts data from a   #
# WRF output file (netCDF format) 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-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 WRF file absolute address
#meteo_pfile                 # Input WRF file from the previous run
#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                # set it to 0 if chimere domain is strictly identical to meteo domain
#imakecompil                 # set it to 0 if you have a pre-compiled source tree
#arome_root                  # not for WRF use
#lsm_suffix                  # not for WRF use


[ -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
if [ ${imakecompil} == 1 ]; then
   ./compile-meteo.sh ${meteo}
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_nwrf     = '"${meteo_nfile}"'"               >> ${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}/     \
    METINFOS-wrf.nml.sed > ${fnmetinfos}

