#!/bin/ksh

# **************************** LICENSE START ***********************************
#
# Copyright 2012 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 ************************************


# create directory for ecregrid if it does not already exist
lpolydir=/tmp/ecregrid/legendre_polynomials

if [ ! -d $lpolydir ]
then
    echo "creating $lpolydir"
    mkdir -p $lpolydir
    chmod uga+rxw $lpolydir
fi


# -----------------------------------------
# ECMWF extras for setting MARS environment
# Note this script runs the Mars module
# unless 'env_only' is specified on the
# command line.
# -----------------------------------------

#if [ x$WMO_SITE_NR = x098 ]  # only do this at ECMWF
#then
    export MV_MARS_MODE=ecRegrid

    export METAPS=/usr/local/lib/metaps/mars_grib_api
    export MARS_HOME=$METAPS

    #-- for xtra safety --
    unset FDB_SERVER_HOST || true

    export MARS_AUTH_FILE=$MARS_HOME/etc/mars.authentication
    export MARS_IBM_RULES=$MARS_HOME/chk/ibm.chk

    export MARS_LANGUAGE_FILE=$METVIEW_DIR_SHARE/etc/MarsEcRegridDef
    export MARS_TEST_FILE=$METVIEW_DIR_SHARE/etc/MarsEcRegridRules
    ##export MARS_LANGUAGE_FILE=$MARS_HOME/etc/mars.def
    ##export MARS_TEST_FILE=$MARS_HOME/chk/mars.chk

    #-- for Lagrandian coeffiecient files --
    export PPDIR=${PPDIR=`$METAPS/bin/ppdir.sh`}

    #-- for MARS statistics --
    export STAT_PREFIX=metview
    . $METAPS/bin/marsstat.sh

    #-- MARS home directory for user --
    . $METAPS/bin/mars_user_home

    #-- how frequent MARS sends warning e-mails --
    export MARS_MAIL_FREQUENCY=${MARS_MAIL_FREQUENCY:=86400}

    export MARS_DHSBASE_CHECK=$METAPS/chk/dhs.chk

    #-- final solution from Manuel 000615/29 --
    . $METAPS/bin/mars.environment

    #-- Choose interpolation configuration (040913) --
    #-- -- moved inside 'mars.environment' (050308) --
    #-- config_interp=$($METAPS/bin/select_interpolation_mode)
    #-- CONFIG_INTERP=${CONFIG_INTERP:=$config_interp}
#else
#    MARS_LANGUAGE_FILE=$METVIEW_DIR_SHARE/etc/MarsDef
#    MARS_TEST_FILE=$METVIEW_DIR_SHARE/etc/MarsRules
#fi


if [ x$1 != xenv_only ]
then
#    tv8 $METVIEW_BIN/MarsOdb
	$METVIEW_BIN/MarsEcRegrid
fi
