# 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.
#

position offsetSection2;
section_length[4] section2Length ;

meta section2Pointer section_pointer(offsetSection2,section2Length,2);
unsigned[1] numberOfSection = 2 :read_only;

alias tiggeSuiteID = zero;

# This is a workaround for TIGGE: allow creation of an 'empty' section 2
# so we can create bit-identical grib 2 files for backward compatibility
transient addEmptySection2 = 0;

if ( addEmptySection2 == 0 ) {
    if ( grib2LocalSectionPresent==1 or ( section2Length>5 or new() ) ) {
        alias section2Used=one;

        if(productionStatusOfProcessedData == 4 || productionStatusOfProcessedData == 5) {
            # This is TIGGE-LAM because of the productionStatusOfProcessedData and the non-empty section 2
            codetable[2] tiggeLocalVersion 'grib2/tiggeLocalVersion.table' = 1 : dump;
            template  tiggeSection "grib2/local.tigge.[tiggeLocalVersion:l].def";
        }

        codetable[2] grib2LocalSectionNumber 'grib2/grib2LocalSectionNumber.[centreForLocal:l].table' = 1 : dump;
    
        if (grib2LocalSectionNumber!=0) {
            template_nofail local "grib2/local.[centreForLocal:l].def";
        } else {
            constant deleteLocalDefinition=1;
        }
        position offsetAfterCentreLocalSection;
    }
}

section_padding section2Padding : read_only;



