# Copyright 2005-2018 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.
#

# START 1/local.98.5 ----------------------------------------------------------------------
# LOCAL 98 5
#
# localDefinitionTemplate_005
# ---------------------------
#
# Description            Octet    Code    Ksec1    Count
# -----------            -----    ----    -----    -----
#localDefinitionNumber       41  I1  37  -
#class                       42  I1  38  -
#type                        43  I1  39  -
#stream                      44  I2  40  -
#experimentVersionNumber     46  A4  41  -
#number                      50  I1  42  -
#total                       51  I1  43  -
#decimalScaleFactor          52  S1  44  -
#thresholdIndicator          53  I1  45  -
#lowerThreshold              54  S2  46  -
#upperThreshold              56  S2  47  -
#spareSetToZero              58  PAD n/a 1
#

constant GRIBEXSection1Problem = 58 - section1Length ;

constant probPoint=5 : hidden;
constant probContinous=9 : hidden;

# 1 to 2 conversion
_if (timeRangeIndicator==3 || timeRangeIndicator==4
    || timeRangeIndicator==5) {
  alias productDefinitionTemplateNumber=probContinous;
} else {
  alias productDefinitionTemplateNumber=probPoint;
}


template mars_labeling "grib1/mars_labeling.def";

unsigned[1] forecastProbabilityNumber : dump ;

unsigned[1] totalNumberOfForecastProbabilities  : dump;

signed[1] localDecimalScaleFactor : dump ;

unsigned[1] thresholdIndicator : dump ;

signed[2] lowerThreshold : dump ;

signed[2] upperThreshold  : dump;

# 1 to 2 conversion
_if (thresholdIndicator == 1) {
#  Probability of event above lower limit
  transient probabilityType=3;
  transient scaleFactorOfLowerLimit=localDecimalScaleFactor;
  transient scaledValueOfLowerLimit=lowerThreshold;
  transient scaleFactorOfUpperLimit=missing();
  transient scaledValueOfUpperLimit=missing();

}
_if (thresholdIndicator == 2) {
#  Probability of event below upper limit
  transient probabilityType=4;
  transient scaleFactorOfLowerLimit= missing();
  transient scaledValueOfLowerLimit=missing();
  transient scaleFactorOfUpperLimit=localDecimalScaleFactor;
  transient scaledValueOfUpperLimit=upperThreshold;
}
_if (thresholdIndicator == 3) {
# Probability of event between lower and upper limits.
# The range includes the lower limit but not the upper limit
  transient probabilityType=2;
  transient scaleFactorOfLowerLimit=localDecimalScaleFactor;
  transient scaledValueOfLowerLimit=lowerThreshold;
  transient scaleFactorOfUpperLimit=localDecimalScaleFactor;
  transient scaledValueOfUpperLimit=upperThreshold;
}


# spareSetToZero
pad padding_loc5_1(1);
alias number = forecastProbabilityNumber;
alias totalNumber=totalNumberOfForecastProbabilities;

# END   1/local.98.5 ----------------------------------------------------------------------
