# Copyright 2005-2007 ECMWF
#
# Licensed under the GNU Lesser General Public License which
# incorporates the terms and conditions of version 3 of the GNU
# General Public License.
# See LICENSE and gpl-3.0.txt for details.

constant ieeeFloats   = 0 : hidden, edition_specific;
transient eps=0;

meta headersOnly headers_only();

#template      section_0 "grib1/section.0.def"   ;

meta gts_header gts_header() : no_copy,hidden,read_only;
meta gts_TTAAii gts_header(20,6) : no_copy,hidden,read_only;
meta gts_CCCC gts_header(27,4) : no_copy,hidden,read_only;
meta gts_ddhh00 gts_header(32,6) : no_copy,hidden,read_only;

ascii[4] identifier  = "GRIB" : read_only,hidden;

constant offsetSection0=0;
constant section0Length=8 ;
meta section0Pointer section_pointer(offsetSection0,section0Length,0);

# Due to a trick done by GRIBEX to support large GRIBs, we need a special treatment
# of the message length and of the section4 lenth, so instead of
# length[3] totalLength                     ;
# we get:



g1_message_length[3] totalLength(section4Length) ;
position startOfHeaders;
unsigned[1] editionNumber = 1 : edition_specific,dump;

template      section_1 "grib1/section.1.def"   ;

alias ls.edition = editionNumber;

# Not flagbit numbers 7 to 0, while wmo is 1 to 8
flagbit gridDescriptionSectionPresent(section1Flags,7) = 1;
meta GDSPresent gds_is_present(gridDescriptionSectionPresent,gridDefinition,bitmapPresent,values): dump ;
#alias GDSPresent = gridDescriptionSectionPresent;

flagbit bitmapPresent(section1Flags,6) :dump;
alias bitmapSectionPresent=bitmapPresent;
alias geography.bitmapPresent=bitmapPresent;

if(gridDescriptionSectionPresent){
   template section_2 "grib1/section.2.def"     ;
} else {
  template predefined_grid "grib1/predefined_grid.def";
}


# Used to mark end of headers. Can be accessed with grib_get_offset()
position   endOfHeadersMaker;

meta lengthOfHeaders evaluate( endOfHeadersMaker-startOfHeaders);
meta md5Headers md5(startOfHeaders,lengthOfHeaders);

if (!headersOnly) {
transient  missingValue   = 9999 : dump;

if(bitmapPresent) {
    template   section3 "grib1/section.3.def"     ;
} else {
   constant   tableReference = 0;
}

template     section_4 "grib1/section.4.def"     ;

template     section_5 "grib1/section.5.def"     ;
}
