#!/bin/bash

# **************************** LICENSE START ***********************************
#
# Copyright 2013 ECMWF and INPE. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************


# ---------------------------------------------------------
# Script to run SCM from within Metview
# ---------------------------------------------------------

set -x

print_err()
{
	echo ${text_ERR}  $* >> "${f_LOG}"
} 

text_ERR="Script `basename $0` FAILED> "

#Get args
if [ $# -ne 9 ] ; then
    echo "Invalid number of arguments specified for script $0 ! (" $# " instead of 9)"
    exit 1
fi

d_WORK=$1
f_EXE=$2
NLEV=$3
f_INPUT=$4
f_NAMELIST=$5
f_VTABLE=$6
d_RCLIM=$7
f_LOG=$8
f_RES=$9

if [ "$f_EXE" = "_UNDEF_" ] ; then
	exe_SCM=${MV_SCM_EXE}
else
	exe_SCM=${f_EXE}
fi

#-------------------------------
# Go to working directory 
#-------------------------------

if [ ! -d "$d_WORK" ] ; then   
   print_err "No working directory found: " $d_WORK
   exit 1
fi

cd $d_WORK

#-------------------------------
# Checks
#-------------------------------

if [ x"$exe_SCM" = "x" ] ; then   
   print_err "No SCM executable is defined. Please define it via env variable MV_SCM_EXE."
   exit 1
fi

if [ ! -f "$exe_SCM" ] ; then   
   print_err "No SCM executable found: " $exe_SCM
   exit 1
fi

if [ ! -x "$exe_SCM" ] ; then   
   print_err "SCM executable cannot be run! Permission is missing. " $exe_SCM 
   exit 1
fi

if [ ! -f "$f_INPUT" ] ; then   
   print_err "No SCM input profile file is found: " $f_INPUT
   exit 1
fi

if [ ! -r "$f_INPUT" ] ; then   
   print_err "SCM input profile file cannot be read! Permission is missing. " $f_INPUT
   exit 1
fi

if [ ! -f "$f_NAMELIST" ] ; then   
   print_err  "No SCM namelist file is found: " $f_NAMELIST
   exit 1
fi

if [ ! -r "$f_NAMELIST" ] ; then   
   print_err "SCM namelist file cannot be read! Permission is missing. " $f_NAMELIST
   exit 1
fi

if [ "$f_VTABLE" = "_UNDEF_" ] ; then
	SCM_VTABLE_FILES_PATH="${SCM_VTABLE_FILES_PATH:=/home/graphics/cgx/model_files/scm}"
	f_VTABLE="${SCM_VTABLE_FILES_PATH}/vtable_L${NLEV}"
fi

if [  ! -f "$f_VTABLE" ] ; then   
  	print_err "No SCM vtable file is found: " $f_VTABLE
   	exit 1
fi

if [ ! -r "$f_VTABLE" ] ; then   
   	print_err "SCM vtable file cannot be read! Permission is missing. " $f_VTABLE
   	exit 1
fi

if [ "$d_RCLIM" = "_UNDEF_" ] ; then
	SCM_RCLIM_FILES_PATH="${SCM_RCLIM_FILES_PATH:=/home/graphics/cgx/model_files/scm}"
	d_RCLIM=${SCM_RCLIM_FILES_PATH}
fi

if [  ! -d "$d_RCLIM" ] ; then   
  	print_err "No directory of SCM climatology data is found: " $d_RCLIM
   	exit 1
fi

if [ ! -r "$d_RCLIM" ] ; then   
   	print_err "Directory of SCM climatology data cannot be read! Permission is missing. " $d_RCLIM
   	exit 1
fi

#-------------------------------
# Prepare input profile
#-------------------------------

ln -sf "${f_INPUT}" scm_in.nc

#-------------------------------
# Prepare namelist
#-------------------------------

ln -sf "${f_NAMELIST}" namelist
ln -fs "${f_VTABLE}"  vtable
cpp -P -Dnew_NFLEVG=${NLEV} -Dnew_NCEXTR=${NLEV} namelist > fort.4

#-------------------------------
# Prepare climatology data
#-------------------------------

for cf in `ls "$d_RCLIM"` ; do
	f_cf="${d_RCLIM}"/$cf
	if [ -r "${f_cf}" -a x`echo $cf | cut -c 1-8` != x"vtable_L" ] ; then
		ln -sf  "${f_cf}" .
	fi
done  

#-------------------------------
#Run SCM
#-------------------------------

# temporary fix (April 2015) until the classroom machines are upgraded
if [ ${OS_VERSION} = "opensuse113" ] ; then
	LD_LIBRARY_PATH="/home/rd/openifs/software/grib_api/1.11.0/grib_api-gcc-4.8.1-opt/lib:${LD_LIBRARY_PATH}"
fi

$exe_SCM > "${f_LOG}" 2>&1 
outCode=$?

#-----------------------------------
#  Check log
#-----------------------------------

if [ -f "${f_LOG}" ] ; then
  if [ `grep -c -i WARNING "$f_LOG"` -ne 0 ] ; then
	outCode=255 
  elif [ `grep -c -i ERROR "$f_LOG"` -ne 0 ] ; then
	outCode=1
  elif [ $outCode -ne 0 ] ; then
	outCode=$outCode
  fi  
fi

#-----------------------------------
#  Check results
#-----------------------------------

if [ ! -f progvar.nc ] ; then
   print_err "SCM ouput file \"progvar.nc\" was not generated!"
   exit 1	 
fi

if [ ! -f diagvar.nc ] ; then
   print_err "SCM ouput file \"diagvar.nc\" was not generated!"
   exit 1	 
fi

if [ ! -f diagvar2.nc ] ; then
   print_err "SCM ouput file \"diagvar2.nc\" was not generated!"
   exit 1	 
fi

cp -f progvar.nc "${f_RES}"

exit $outCode
