1# Copyright 2005-2018 ECMWF.
2#
3# This software is licensed under the terms of the Apache Licence Version 2.0
4# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
5#
6# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
7# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
8#
9
10unsigned [2] N2 : dump;
11unsigned [2] codedNumberOfFirstOrderPackedValues : no_copy ;
12unsigned [2] numberOfSecondOrderPackedValues : dump;
13
14# used to extend
15unsigned [1] extraValues=0 : hidden, edition_specific;
16
17meta numberOfGroups evaluate(codedNumberOfFirstOrderPackedValues + 65536 * extraValues);
18
19unsigned[1] groupWidths[numberOfGroups] :dump;
20meta bitsPerValue second_order_bits_per_value(values,binaryScaleFactor,decimalScaleFactor);
21
22position offsetBeforeData;
23
24if(bitmapPresent)   {
25   meta codedValues data_g1second_order_general_packing(
26	#simple_packing args
27     section4Length,
28     offsetBeforeData,
29     offsetSection4,
30     unitsFactor,
31     unitsBias,
32     changingPrecision,
33     numberOfCodedValues,
34     bitsPerValue,
35     referenceValue,
36     binaryScaleFactor,
37     decimalScaleFactor,
38	#g1second_order_row_by_row args
39	 halfByte,
40	 packingType,
41	 grid_ieee,
42	 precision,
43	 widthOfFirstOrderValues,
44	 N1,
45	 N2,
46	 numberOfGroups,
47	 numberOfSecondOrderPackedValues,
48	 extraValues,
49	 Ni,
50	 Nj,
51	 pl,
52	 jPointsAreConsecutive,
53	 bitmap,
54	 groupWidths
55
56   ): read_only;
57   alias data.packedValues = codedValues;
58
59   if (boustrophedonicOrdering)
60   {
61      if (GRIBEX_boustrophedonic)
62      {
63         meta preBitmapValues data_apply_boustrophedonic_bitmap(codedValues,bitmap,missingValue,binaryScaleFactor,numberOfRows,numberOfColumns,numberOfPoints): read_only;
64      }
65      else
66      {
67         meta preBitmapValues data_apply_bitmap(codedValues,bitmap,missingValue,binaryScaleFactor) : read_only;
68      }
69      meta values data_apply_boustrophedonic(preBitmapValues,numberOfRows,numberOfColumns,numberOfPoints,pl) : dump;
70   }
71   else
72   {
73      meta values data_apply_bitmap(codedValues,bitmap,missingValue,binaryScaleFactor) : dump;
74   }
75
76} else {
77	if (boustrophedonicOrdering) {
78	   meta values data_g1second_order_general_packing(
79		#simple_packing args
80		 section4Length,
81		 offsetBeforeData,
82		 offsetSection4,
83		 unitsFactor,
84		 unitsBias,
85		 changingPrecision,
86		 numberOfCodedValues,
87		 bitsPerValue,
88		 referenceValue,
89		 binaryScaleFactor,
90		 decimalScaleFactor,
91		#g1second_order_row_by_row args
92		 halfByte,
93		 packingType,
94		 grid_ieee,
95		 precision,
96		 widthOfFirstOrderValues,
97		 N1,
98		 N2,
99		 numberOfGroups,
100		 numberOfSecondOrderPackedValues,
101		 extraValues,
102		 Ni,
103		 Nj,
104		 pl,
105		 jPointsAreConsecutive,
106		 bitmap,
107		 groupWidths
108
109	   ) : dump;
110		meta values data_apply_boustrophedonic(codedValues,numberOfRows,numberOfColumns,numberOfPoints,pl) : dump;
111   } else {
112	   meta values data_g1second_order_general_packing(
113		#simple_packing args
114		 section4Length,
115		 offsetBeforeData,
116		 offsetSection4,
117		 unitsFactor,
118		 unitsBias,
119		 changingPrecision,
120		 numberOfCodedValues,
121		 bitsPerValue,
122		 referenceValue,
123		 binaryScaleFactor,
124		 decimalScaleFactor,
125		#g1second_order_row_by_row args
126		 halfByte,
127		 packingType,
128		 grid_ieee,
129		 precision,
130		 widthOfFirstOrderValues,
131		 N1,
132		 N2,
133		 numberOfGroups,
134		 numberOfSecondOrderPackedValues,
135		 extraValues,
136		 Ni,
137		 Nj,
138		 pl,
139		 jPointsAreConsecutive,
140		 bitmap,
141		 groupWidths
142
143	   ) : dump;
144   }
145   alias data.packedValues = values;
146}
147
148transient numberOfCodedValues = numberOfSecondOrderPackedValues;
149
150meta packingError simple_packing_error(bitsPerValue,binaryScaleFactor,decimalScaleFactor,referenceValue,ibm) : no_copy;
151
152template statistics "common/statistics_grid.def";
153