# 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 2/template.5.1 ----------------------------------------------------------------------
# TEMPLATE 5.1, Matrix values at grid point -simple packing

include "grib2/template.5.packing.def";


unsigned[1] matrixBitmapsPresent ;
# same as in edition 1
alias secondaryBitmapPresent=matrixBitmapsPresent;

#  Number of data values encoded in Section 7
unsigned[4] numberOfCodedValues ; 

#  NR - first dimension
# (rows)
unsigned[2] firstDimension ;

alias NR  = firstDimension;
#  NC - second dimension
# (columns)
unsigned[2] secondDimension ;

alias NC =  secondDimension;
#  First dimension coordinate value definition
# (Code Table 5.2)
unsigned[1] firstDimensionCoordinateValueDefinition ;

#  NC1 - number of coefficients or values used to specify first dimension coordinate function
unsigned[1] NC1 : dump ;
alias numberOfCoefficientsOrValuesUsedToSpecifyFirstDimensionCoordinateFunction=NC1;

#  Second dimension coordinate value definition
# (Code Table 5.2)
unsigned[1] secondDimensionCoordinateValueDefinition ;

#  NC2 - number of coefficients or values used to specify second dimension coordinate function
unsigned[1] NC2 : dump ;
alias numberOfCoefficientsOrValuesUsedToSpecifySecondDimensionCoordinateFunction = NC2;

#  First dimension physical significance
# (Code Table 5.3)
unsigned[1] firstDimensionPhysicalSignificance ;

#  Second dimension physical significance
# (Code Table 5.3)
unsigned[1] secondDimensionPhysicalSignificance ;

ieeefloat coefsFirst[NC1];  # TODO: find proper names
ieeefloat coefsSecond[NC2];# TODO: find proper names

alias data.coefsFirst = coefsFirst;
alias data.coefsSecond=coefsSecond;

if(matrixBitmapsPresent == 1)
{

  constant datumSize             = NC*NR;
  constant secondaryBitmapsCount = numberOfValues + 0; #
  constant secondaryBitmapsSize  = secondaryBitmapsCount/8;

  transient numberOfDataMatrices  = numberOfDataPoints/datumSize;

  position offsetBBitmap;
  meta secondaryBitmaps g2bitmap(
          dummy,
          missingValue,
          offsetBSection5,
          section5Length,
          numberOfCodedValues ,
          dummy) : read_only
                    ;

    meta bitmap  data_g2secondary_bitmap(primaryBitmap,
      secondaryBitmaps,
      missingValue,datumSize,numberOfDataPoints) : read_only;

}
