#!/bin/sh
# (C) Copyright 2005- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
#

. ./include.ctest.sh

# --------------------------------------------
# This is the test for the JIRA issue ECC-806
# Local definitions precedence order
# --------------------------------------------
label="grib_ecc-806_test"
tempGrb=temp.${label}.grb
tempOut=temp.${label}.txt

# This NCEP grib2 file has the keys
#   discipline = 0
#   parameterCategory = 1
#   parameterNumber = 11
# Which is normally matched by 3066 but when overriden by NCEP local definitions
# should become 260056
# This assumes our definitions contain the local kwbc concepts
# See definitions/grib2/localConcepts/kwbc/paramId.def

input=$data_dir/gfs.complex.mvmu.grib2
res=`${tools_dir}/grib_get -p paramId,shortName,units,name $input`
[ "$res" = "3066 sde m Snow depth" ]

# Change the precedence rule
res=`${tools_dir}/grib_get -s preferLocalConcepts=1 -p paramId,shortName,units,name $input`
[ "$res" = "260056 sdwe kg m**-2 Water equivalent of accumulated snow depth (deprecated)" ]

# Test the environment variable too
export ECCODES_GRIB_PREFER_LOCAL_CONCEPTS=1
grib_check_key_equals $input preferLocalConceptsEnvVar,preferLocalConcepts '1 1'
res=`${tools_dir}/grib_get -p paramId,shortName,units,name $input`
[ "$res" = "260056 sdwe kg m**-2 Water equivalent of accumulated snow depth (deprecated)" ]

export ECCODES_GRIB_PREFER_LOCAL_CONCEPTS=0
grib_check_key_equals $input preferLocalConceptsEnvVar,preferLocalConcepts '0 0'
unset ECCODES_GRIB_PREFER_LOCAL_CONCEPTS
grib_check_key_equals $input preferLocalConceptsEnvVar,preferLocalConcepts '0 0'


# Clean up
rm -f $tempGrb $tempOut
