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

# moved here to allow different bitsPerValue in second order packing
unsigned[1] bitsPerValue : dump ;
alias numberOfBitsContainingEachPackedValue = bitsPerValue;

# For grib1 -> grib2

#constant dataRepresentationTemplateNumber = 51;

constant PUnset = -32767;

 unsigned[2] N     : read_only,dump;
 signed[2] P  = PUnset ;
 
 unsigned[1] JS=0  : dump;
 unsigned[1] KS=0  : dump;
 unsigned[1] MS=0  : dump;

 alias subSetJ=JS  ;
 alias subSetK=KS  ;
 alias subSetM=MS  ;

 constant GRIBEXShBugPresent = 1;
 transient computeLaplacianOperator=0;
 
 meta data.laplacianOperator    scale(P,oneConstant,grib1divider,truncateLaplacian) : dump;
 meta laplacianOperatorIsSet evaluate(P != PUnset &&  !computeLaplacianOperator );

 if (localUsePresent) {
   if (changed(localDefinitionNumber)) {
     transient TS = 0 ;
     meta TScalc spectral_truncation(JS,KS,MS,TS) : read_only,hidden;
     meta Nassigned octect_number(N,4*TScalc) : hidden ;
   }
 }

 position offsetBeforeData;
 meta values data_g1complex_packing(
      section4Length,
      offsetBeforeData,
      offsetSection4,
      unitsFactor,
      unitsBias,
      changingPrecision,
      numberOfCodedValues,
      bitsPerValue,
      referenceValue,
      binaryScaleFactor,
      decimalScaleFactor,

      GRIBEXShBugPresent,
      ieeeFloats,

      laplacianOperatorIsSet,
      laplacianOperator,
      subSetJ,
      subSetK,
      subSetM,
      pentagonalResolutionParameterJ,
      pentagonalResolutionParameterK,
      pentagonalResolutionParameterM,

      halfByte,
      N,packingType,spectral_ieee,precision
 ) : dump ;

 meta data.packedValues data_sh_packed(
      section4Length,
      offsetBeforeData,
      offsetSection4,

      unitsFactor,
      unitsBias,
      changingPrecision,
      numberOfCodedValues,
      bitsPerValue,
      referenceValue,
      binaryScaleFactor,
      decimalScaleFactor,

      GRIBEXShBugPresent,
      ieeeFloats,

      laplacianOperatorIsSet,
      laplacianOperator,
      subSetJ,
      subSetK,
      subSetM,
      
      pentagonalResolutionParameterJ,
      pentagonalResolutionParameterK,
      pentagonalResolutionParameterM
 ) : read_only;

 meta data.unpackedValues data_sh_unpacked(
      section4Length,
      offsetBeforeData,
      offsetSection4,

      unitsFactor,
      unitsBias,
      changingPrecision,
      numberOfCodedValues,
      bitsPerValue,
      referenceValue,
      binaryScaleFactor,
      decimalScaleFactor,

      GRIBEXShBugPresent,
      ieeeFloats,

      laplacianOperatorIsSet,
      laplacianOperator,
      subSetJ,
      subSetK,
      subSetM,
      
      pentagonalResolutionParameterJ,
      pentagonalResolutionParameterK,
      pentagonalResolutionParameterM
 ) : read_only;

meta packingError simple_packing_error(bitsPerValue,binaryScaleFactor,decimalScaleFactor,referenceValue,ibm) : no_copy;
meta unpackedError simple_packing_error(zero,binaryScaleFactor,decimalScaleFactor,referenceValue,ibm) : no_copy;

# nearest sh(values,radius,J,K,M);

meta numberOfCodedValues g1number_of_coded_values_sh_complex(bitsPerValue,offsetBeforeData,offsetAfterData,halfByte,numberOfValues,subSetJ,subSetK,subSetM) : dump;

template statistics "common/statistics_spectral.def";
