# (C) Copyright 2005- ECMWF.

#  Type of first fixed surface
codetable[1] typeOfFirstFixedSurface ('4.5.table',masterDir,localDir) : dump,no_copy,edition_specific,string_type;
meta unitsOfFirstFixedSurface codetable_units(typeOfFirstFixedSurface) : dump;
meta nameOfFirstFixedSurface codetable_title(typeOfFirstFixedSurface) : dump;

#  Scale factor of first fixed surface
signed[1] scaleFactorOfFirstFixedSurface = missing()  : can_be_missing,dump,no_copy,edition_specific;

#  Scaled value of first fixed surface
unsigned[4] scaledValueOfFirstFixedSurface = missing()  : can_be_missing,dump,no_copy,edition_specific;

#  Type of second fixed surface
codetable[1] typeOfSecondFixedSurface ('4.5.table',masterDir,localDir) = 255 : dump,no_copy,edition_specific;
meta unitsOfSecondFixedSurface codetable_units(typeOfSecondFixedSurface) : dump;
meta nameOfSecondFixedSurface codetable_title(typeOfSecondFixedSurface) : dump;

#  Scale factor of second fixed surface
signed[1] scaleFactorOfSecondFixedSurface = missing() : can_be_missing,dump,no_copy,edition_specific;

#  Scaled value of second fixed surface
unsigned[4] scaledValueOfSecondFixedSurface = missing() : can_be_missing,dump,no_copy,edition_specific;

transient pressureUnits="hPa";

concept_nofail vertical.typeOfLevel (unknown,"typeOfLevelConcept.def",conceptsDir2,conceptsDir1);

alias levelType=typeOfFirstFixedSurface;

if (typeOfSecondFixedSurface == 255) {
  # Only one surface
  meta level g2level(typeOfFirstFixedSurface,
    scaleFactorOfFirstFixedSurface,
    scaledValueOfFirstFixedSurface,
    pressureUnits)  :dump;
  transient bottomLevel=level; # Do not use alias (see GRIB-725)
  transient topLevel=level;
} else {
  # Two surfaces
  meta topLevel g2level(typeOfFirstFixedSurface,
    scaleFactorOfFirstFixedSurface,
    scaledValueOfFirstFixedSurface,
    pressureUnits)  :dump;
  meta bottomLevel g2level(typeOfSecondFixedSurface,
    scaleFactorOfSecondFixedSurface,
    scaledValueOfSecondFixedSurface,
    pressureUnits)  :dump;
    alias level=topLevel;  # (see GRIB-725)
    
}
alias ls.level=level;
alias vertical.level=level;
alias vertical.bottomLevel=bottomLevel;
alias vertical.topLevel=topLevel;

alias extraDim=zero;
if (defined(extraDimensionPresent)) {
    if (extraDimensionPresent) {
        alias extraDim=one;
    }
}
if (extraDim) {
    alias mars.levelist = dimension;
    alias mars.levtype = dimensionType;
} else {
    # See GRIB-74 why we store the pressureUnits in a transient
    transient tempPressureUnits=pressureUnits;
    if (!(typeOfLevel is "surface")) {
        if (tempPressureUnits is "Pa") {
            meta marsLevel scale(level,one,hundred) : read_only;
            alias mars.levelist=marsLevel;
        } else {
            alias mars.levelist = level;
        }
    }
    alias mars.levtype  = typeOfFirstFixedSurface;
    # GRIB-372: levelist alias does not pertain to surface parameters
    if (levtype is "sfc") {
        unalias mars.levelist;
    }
}

# See ECC-854
if(typeOfFirstFixedSurface == 151 && typeOfSecondFixedSurface == 151) {
    alias mars.levelist = bottomLevel;
}

alias ls.typeOfLevel=typeOfLevel;
