1 #ifndef GDECODE_H
2 #define GDECODE_H
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include "common/fortint.h"
8 #include "common/fortreal.h"
9 #include "gdecodeStruct.h"
10 #include "sencode1.h"
11 
12 #ifdef FORTRAN_NO_UNDERSCORE
13 #define IGGLAT  igglat
14 #define GBYTE   gbyte
15 #define GDECODE gdecode
16 #define IGLNGTH iglngth
17 #define IGTABLE igtable
18 #define IGCENTR igcentr
19 #define IGPARAM igparam
20 #define IGLEVEL iglevel
21 #define IGLEVL1 iglevl1
22 #define IGLEVL2 iglevl2
23 #define IGDATE  igdate
24 #define IGTIME  igtime
25 #define IGSTEP  igstep
26 #define IGSTEP1 igstep1
27 #define IGSTEP2 igstep2
28 #define IGDEFIN igdefin
29 #define IGCLASS igclass
30 #define IGTYPE  igtype
31 #define IGSTREM igstrem
32 #define IGEXPVR igexpvr
33 #define IGNUMPV ignumpv
34 #define GPV     gpv
35 #define GPL     gpl
36 #define IGREPRS igreprs
37 #define IGNI    igni
38 #define IGNJ    ignj
39 #define RGNWLAT rgnwlat
40 #define RGNWLON rgnwlon
41 #define IGRESCO igresco
42 #define RGSELAT rgselat
43 #define RGSELON rgselon
44 #define RGDI    rgdi
45 #define RGDJ    rgdj
46 #define IGGAUSS iggauss
47 #define IGSCANM igscanm
48 #define IGJ     igj
49 #define IGK     igk
50 #define IGM     igm
51 #define IGREPMO igrepmo
52 #define RGIP    rgip
53 #define IGTJ    igtj
54 #define IGTK    igtk
55 #define IGTM    igtm
56 #define RGLATRP rglatrp
57 #define RGLONRP rglonrp
58 #define RGROTAT rgrotat
59 #define RGLATSP rglatsp
60 #define RGLONSP rglonsp
61 #define RGSFACT rgsfact
62 #define RGREFVL rgrefvl
63 #define IGBTSPV igbtspv
64 #define RGDSCAL rgdscal
65 #define IGUNUSD igunusd
66 #define IGNVALU ignvalu
67 #define GVALUES gvalues
68 #define GVINIT  gvinit
69 #define GVECTOR gvector
70 #define GVEND   gvend
71 #define IGLEVTY iglevty
72 #define RGLEVTY rglevty
73 #define IGNUMAV ignumav
74 #define RGNUMAV rgnumav
75 #define IGNUMMS ignumms
76 #define RGNUMMS rgnumms
77 #define IGSUBID igsubid
78 #define RGSUBID rgsubid
79 #else
80 #define IGGLAT  igglat_
81 #define GBYTE   gbyte_
82 #define GDECODE gdecode_
83 #define IGLNGTH iglngth_
84 #define IGTABLE igtable_
85 #define IGCENTR igcentr_
86 #define IGPARAM igparam_
87 #define IGLEVEL iglevel
88 #define IGLEVL1 iglevl1
89 #define IGLEVL2 iglevl2
90 #define IGDATE  igdate_
91 #define IGTIME  igtime_
92 #define IGSTEP  igstep_
93 #define IGSTEP1 igstep1_
94 #define IGSTEP2 igstep2_
95 #define IGDEFIN igdefin_
96 #define IGCLASS igclass_
97 #define IGTYPE  igtype_
98 #define IGSTREM igstrem_
99 #define IGEXPVR igexpvr_
100 #define IGNUMPV ignumpv_
101 #define GPV     gpv_
102 #define GPL     gpl_
103 #define IGREPRS igreprs_
104 #define IGNI    igni_
105 #define IGNJ    ignj_
106 #define RGNWLAT rgnwlat_
107 #define RGNWLON rgnwlon_
108 #define IGRESCO igresco_
109 #define RGSELAT rgselat_
110 #define RGSELON rgselon_
111 #define RGDI    rgdi_
112 #define RGDJ    rgdj_
113 #define IGGAUSS iggauss_
114 #define IGSCANM igscanm_
115 #define IGJ     igj_
116 #define IGK     igk_
117 #define IGM     igm_
118 #define IGREPMO igrepmo_
119 #define RGIP    rgip_
120 #define IGTJ    igtj_
121 #define IGTK    igtk_
122 #define IGTM    igtm_
123 #define RGLATRP rglatrp_
124 #define RGLONRP rglonrp_
125 #define RGROTAT rgrotat_
126 #define RGLATSP rglatsp_
127 #define RGLONSP rglonsp_
128 #define RGSFACT rgsfact_
129 #define RGREFVL rgrefvl_
130 #define IGBTSPV igbtspv_
131 #define RGDSCAL rgdscal_
132 #define IGUNUSD igunusd_
133 #define IGNVALU ignvalu_
134 #define GVALUES gvalues_
135 #define GVINIT  gvinit_
136 #define GVECTOR gvector_
137 #define GVEND   gvend_
138 #define IGLEVTY iglevty_
139 #define RGLEVTY rglevty_
140 #define IGNUMAV ignumav_
141 #define RGNUMAV rgnumav_
142 #define IGNUMMS ignumms_
143 #define RGNUMMS rgnumms_
144 #define IGSUBID igsubid_
145 #define RGSUBID rgsubid_
146 #endif
147 
148 #define MOVE1BYTE(p,n)  ( *(p)     = ((*(n)>> 0) & 0xFF) )
149 #define MOVE2BYTES(p,n) ( *(p)     = ((*(n)>> 8) & 0xFF) ) , \
150                         ( *((p)+1) = ((*(n)>> 0) & 0xFF) )
151 #define MOVE3BYTES(p,n) ( *(p)     = ((*(n)>>16) & 0xFF) ) , \
152                         ( *((p)+1) = ((*(n)>> 8) & 0xFF) ) , \
153                         ( *((p)+2) = ((*(n)>> 0) & 0xFF) )
154 #define MOVE4BYTES(p,n) ( *(p)     = ((*(n)>>24) & 0xFF) ) , \
155                         ( *((p)+1) = ((*(n)>>16) & 0xFF) ) , \
156                         ( *((p)+2) = ((*(n)>> 8) & 0xFF) ) , \
157                         ( *((p)+3) = ((*(n)>> 0) & 0xFF) )
158 
159 #define MOD(a,b) ((a) - ((a)/(b))*(b))
160 #define MULTIPLE(a,b) ( MOD(a,b) == 0 )
161 
162 #define ONEBYTEINT(a)   (fortint) ( *(a) )
163 #define TWOBYTEINT(a)   (fortint) ( (*(a))<<8 | (*((a)+1))<<0 )
164 #define THREEBYTEINT(a) (fortint) (TWOBYTEINT((a))<<8 | (*((a)+2))<<0 )
165 #define FOURBYTEINT(a)  (fortint) (THREEBYTEINT((a))<<8 | (*((a)+3))<<0 )
166 /*
167 // Section 0
168 */
169 #define g0_length(a)  THREEBYTEINT((a->g0)->totalGRIBlength)
170 #define g0_edition(a) ONEBYTEINT((a->g0)->editionNumber)
171 /*
172 // Section 1
173 */
174 #define g1_length(a)      THREEBYTEINT((a->g1)->sectionLength)
175 #define g1_table(a)       ONEBYTEINT((a->g1)->tableVersionNumber)
176 #define g1_centre(a)      ONEBYTEINT((a->g1)->originatingCentre)
177 #define g1_process(a)     ONEBYTEINT((a->g1)->generatingProcessId)
178 #define g1_grid(a)        ONEBYTEINT((a->g1)->gridDefinition)
179 #define g1_flag(a)        ONEBYTEINT((a->g1)->section2and3PresentFlag)
180 #define g1_parameter(a)   ONEBYTEINT((a->g1)->parameter)
181 #define g1_typeOfLevel(a) ONEBYTEINT((a->g1)->typeOfLevel)
182 #define g1_level1(a)      ONEBYTEINT((a->g1)->level1)
183 #define g1_level2(a)      ONEBYTEINT((a->g1)->level2)
184 #define g1_year(a)        ONEBYTEINT((a->g1)->year)
185 #define g1_month(a)       ONEBYTEINT((a->g1)->month)
186 #define g1_day(a)         ONEBYTEINT((a->g1)->day)
187 #define g1_hour(a)        ONEBYTEINT((a->g1)->hour)
188 #define g1_minute(a)      ONEBYTEINT((a->g1)->minute)
189 #define g1_timeunit(a)    ONEBYTEINT((a->g1)->unitOfTimeRange)
190 #define g1_P1(a)          ONEBYTEINT((a->g1)->P1)
191 #define g1_P2(a)          ONEBYTEINT((a->g1)->P2)
192 #define g1_timerange(a)   ONEBYTEINT((a->g1)->timeRangeIndicator)
193 #define g1_number(a)      TWOBYTEINT((a->g1)->numberInAverage)
194 #define g1_missing(a)     ONEBYTEINT((a->g1)->numberMissing)
195 #define g1_century(a)     ONEBYTEINT((a->g1)->century)
196 #define g1_subcentre(a)   ONEBYTEINT((a->g1)->subCentreId)
197 #define g1_scale(a)       TWOBYTEINT((a->g1)->unitsDecimalScaleFactor)
198 #define g1_definition(a)  ONEBYTEINT((a->g1)->local.mars.definition)
199 #define g1_class(a)       ONEBYTEINT((a->g1)->local.mars.ecmwfClass)
200 #define g1_type(a)        ONEBYTEINT((a->g1)->local.mars.type)
201 #define g1_stream(a)      TWOBYTEINT((a->g1)->local.mars.stream)
202 #define g1_expver(a)      FOURBYTEINT((a->g1)->local.mars.experimentVersionNumber)
203 
204 #define centreUsingECMWFLocalDefinition(a) \
205             ((g1_subcentre(a)==98)&&(g1_length(a)>=40))
206 
207 #define ecmwfLocalDefinitionPresent(a) ((g1_centre(a)==98)&&(g1_length(a)>=40))
208 
209 #define ecmwfLocalDefinition1Present(a) \
210                     ( ecmwfLocalDefinitionPresent(a) && (g1_definition(a)==1) )
211 #define ecmwfLocalDefinition2Present(a) \
212                     ( ecmwfLocalDefinitionPresent(a) && (g1_definition(a)==2) )
213 
214 #define g1_1_number(a) ONEBYTEINT((a->g1)->local.contents.def1.number)
215 #define g1_1_total(a)  ONEBYTEINT((a->g1)->local.contents.def1.total)
216 
217 #define g1_2_number(a)  ONEBYTEINT((a->g1)->local.contents.def2.number)
218 #define g1_2_total(a)   ONEBYTEINT((a->g1)->local.contents.def2.total)
219 #define g1_2_method(a)  ONEBYTEINT((a->g1)->local.contents.def2.method)
220 #define g1_2_start(a)   TWOBYTEINT((a->g1)->local.contents.def2.startTimestep)
221 #define g1_2_end(a)     TWOBYTEINT((a->g1)->local.contents.def2.endTimestep)
222 #define g1_2_control(a) ONEBYTEINT((a->g1)->local.contents.def2.controlForecastCluster)
223 #define g1_2_operational(a)   ONEBYTEINT((a->g1)->local.contents.def2.operationalForecastCluster)
224 #define g1_2_count(a)   ONEBYTEINT((a->g1)->local.contents.def2.numberInCluster)
225 #define g1_2_domainN(a) THREEBYTEINT((a->g1)->local.contents.def2.domainNorthLatitude)
226 #define g1_2_domainW(a) THREEBYTEINT((a->g1)->local.contents.def2.domainWestLongitude)
227 #define g1_2_domainS(a) THREEBYTEINT((a->g1)->local.contents.def2.domainSouthLatitude)
228 #define g1_2_domainE(a) THREEBYTEINT((a->g1)->local.contents.def2.domainEastLongitude)
229 /*
230 // Section 2
231 */
232 #define g2_length(a)           THREEBYTEINT((a->g2)->sectionLength)
233 #define g2_NV(a)               ONEBYTEINT((a->g2)->NV)
234 #define g2_PV_PL(a)            ONEBYTEINT((a->g2)->PV_PL)
235 #define g2_datatype(a)         ONEBYTEINT((a->g2)->dataRepresentationType)
236 #define g2_ni(a)               TWOBYTEINT((a->g2)->grid.latlon.numberOfPointsAlongParallel)
237 #define g2_nj(a)               TWOBYTEINT((a->g2)->grid.latlon.numberOfPointsAlongMeridian)
238 #define g2_firstLat(a)         THREEBYTEINT((a->g2)->grid.latlon.latitudeOfFirstPoint)
239 #define g2_firstLon(a)         THREEBYTEINT((a->g2)->grid.latlon.longitudeOfFirstPoint)
240 #define g2_lastLat(a)          THREEBYTEINT((a->g2)->grid.latlon.latitudeOfLastPoint)
241 #define g2_lastLon(a)          THREEBYTEINT((a->g2)->grid.latlon.longitudeOfLastPoint)
242 #define g2_resAndComp(a)       ONEBYTEINT((a->g2)->grid.latlon.resolutionAndComponentsFlag)
243 #define g2_scan(a)             ONEBYTEINT((a->g2)->grid.latlon.scanningMode)
244 #define g2_di(a)               TWOBYTEINT((a->g2)->grid.latlon.iDirectionIncrement)
245 #define g2_dj(a)               TWOBYTEINT((a->g2)->grid.latlon.jDirectionIncrement)
246 #define g2_gaussNumber(a)      TWOBYTEINT((a->g2)->grid.gaussian.numberOfParallelsBetweenPoleAndEquator)
247 #define g2_latSouthPole(a)     THREEBYTEINT((a->g2)->grid.latlon.latitudeOfSouthPole)
248 #define g2_lonSouthPole(a)     THREEBYTEINT((a->g2)->grid.latlon.longitudeOfSouthPole)
249 #define g2_rotOrStretch(a)     FOURBYTEINT((a->g2)->grid.latlon.angleOfRotationOrStretchingFactor)
250 #define g2_latStretching(a)    THREEBYTEINT((a->g2)->grid.latlon.latitudeOfPoleOfStretching)
251 #define g2_lonStretching(a)    THREEBYTEINT((a->g2)->grid.latlon.longitudeOfPoleOfStretching)
252 #define g2_stretchingFactor(a) FOURBYTEINT((a->g2)->grid.latlon.stretchingFactor)
253 #define g2_J(a)                TWOBYTEINT((a->g2)->grid.spectral.J)
254 #define g2_K(a)                TWOBYTEINT((a->g2)->grid.spectral.K)
255 #define g2_M(a)                TWOBYTEINT((a->g2)->grid.spectral.M)
256 #define g2_repmode(a)          ONEBYTEINT((a->g2)->grid.spectral.representationMode)
257 
258 #define basicSpectralField(a)               (g2_datatype(a)==50)
259 #define rotatedSpectralField(a)             (g2_datatype(a)==60)
260 #define stretchedSpectralField(a)           (g2_datatype(a)==70)
261 #define rotatedAndStretchedSpectralField(a) (g2_datatype(a)==80)
262 #define anySpectralField(a) (basicSpectralField(a)||\
263                              rotatedSpectralField(a)||\
264                              stretchedSpectralField(a)||\
265                              rotatedAndStretchedSpectralField(a))
266 
267 #define basicGaussianGrid(a)               (g2_datatype(a)== 4)
268 #define rotatedGaussianGrid(a)             (g2_datatype(a)==14)
269 #define stretchedGaussianGrid(a)           (g2_datatype(a)==24)
270 #define rotatedAndStretchedGaussianGrid(a) (g2_datatype(a)==34)
271 #define anyGaussianGrid(a) (basicGaussianGrid(a)||\
272                             rotatedGaussianGrid(a)||\
273                             stretchedGaussianGrid(a)||\
274                             rotatedAndStretchedGaussianGrid(a))
275 
276 #define basicLatLonGrid(a)               (g2_datatype(a)== 0)
277 #define rotatedLatLonGrid(a)             (g2_datatype(a)==10)
278 #define stretchedLatLonGrid(a)           (g2_datatype(a)==20)
279 #define rotatedAndStretchedLatLonGrid(a) (g2_datatype(a)==30)
280 #define anyLatLonGrid(a) (basicLatLonGrid(a)||\
281                           rotatedLatLonGrid(a)||\
282                           stretchedLatLonGrid(a)||\
283                           rotatedAndStretchedLatLonGrid(a))
284 
285 #define generalLatLonGrid(a) (anyGaussianGrid(a)||anyLatLonGrid(a))
286 #define generalRotatedGrid(a) (rotatedLatLonGrid(a)||\
287                                rotatedAndStretchedLatLonGrid(a)||\
288                                rotatedGaussianGrid(a)||\
289                                rotatedAndStretchedGaussianGrid(a)||\
290                                rotatedSpectralField(a)||\
291                                rotatedAndStretchedSpectralField(a))
292 #define generalStretchedGrid(a) (stretchedLatLonGrid(a)||\
293                                  rotatedAndStretchedLatLonGrid(a)||\
294                                  stretchedGaussianGrid(a)||\
295                                  rotatedAndStretchedSpectralField(a)||\
296                                  stretchedSpectralField(a)||\
297                                  rotatedAndStretchedGaussianGrid(a))
298 #define generalStretchedAndRotatedGrid(a) (rotatedAndStretchedLatLonGrid(a)||\
299                                            rotatedAndStretchedGaussianGrid(a))
300 #define directionIncrementsGiven(a) ((g2_resAndComp(a)&0x80)&&(g2_di(a)!=0xffff))
301 /*
302 // Section 3
303 */
304 #define g3_length(a)             THREEBYTEINT((a->g3)->sectionLength)
305 #define g3_table(a)              ONEEBYTEINT((a->g3)->tableReference)
306 #define primaryBitmapPresent(a)  ( g1_flag(a) & 0x40 )
307 /*
308 // Section 4
309 */
310 #define g4_length(a)        THREEBYTEINT((a->g4)->sectionLength)
311 #define g4_flag(a)          ONEBYTEINT((a->g4)->flag)
312 #define g4_scale(a)         TWOBYTEINT((a->g4)->scaleFactor)
313 #define g4_reference(a)     FOURBYTEINT((a->g4)->referenceValue)
314 #define g4_bits(a)          ONEBYTEINT((a->g4)->numberOfBitsPerValue)
315 #define g4_n(a)             TWOBYTEINT((a->g4)->data.complexSpectral.N)
316 #define g4_ip(a)            TWOBYTEINT((a->g4)->data.complexSpectral.IP)
317 #define g4_j(a)             ONEBYTEINT((a->g4)->data.complexSpectral.J)
318 #define g4_k(a)             ONEBYTEINT((a->g4)->data.complexSpectral.K)
319 #define g4_m(a)             ONEBYTEINT((a->g4)->data.complexSpectral.M)
320 #define g4_n1(a)            TWOBYTEINT((a->g4)->data.complexGrid.n1)
321 #define g4_extendedFlags(a) ONEBYTEINT((a->g4)->data.complexGrid.extendedFlags)
322 #define g4_n2(a)            TWOBYTEINT((a->g4)->data.complexGrid.n2)
323 #define g4_p1(a)            TWOBYTEINT((a->g4)->data.complexGrid.p1)
324 #define g4_p2(a)            TWOBYTEINT((a->g4)->data.complexGrid.p2)
325 
326 #define gridPoint(a)                      ( (g4_flag(a)&0x80) == 0 )
327 #define simplePacking(a)                  ( (g4_flag(a)&0x40) == 0 )
328 #define floatingPoint(a)                  ( (g4_flag(a)&0x20) == 0 )
329 #define noAdditionalFlags(a)              ( (g4_flag(a)&0x10) == 0 )
330 #define matrixAtGridPoint(a)              (g4_extendedFlags(a)&0x40)
331 #define secondaryBitmapPresent(a)         (g4_extendedFlags(a)&0x20)
332 #define differentWidths(a)                (g4_extendedFlags(a)&0x10)
333 #define generalExtendedSecondOrder(a)     (g4_extendedFlags(a)&0x08)
334 #define boustrophedonicOrdering(a)        (g4_extendedFlags(a)&0x04)
335 #define firstOrderSpatialDifferencing(a)  ( (g4_extendedFlags(a)&0x03) == 1 )
336 #define secondOrderSpatialDifferencing(a) ( (g4_extendedFlags(a)&0x03) == 2 )
337 #define thirdOrderSpatialDifferencing(a)  ( (g4_extendedFlags(a)&0x03) == 3 )
338 
339 #define anyComplexPackedSpectralField(a) \
340                                       (anySpectralField(a) && !simplePacking(a))
341 
342 #ifdef __cplusplus
343 extern "C" {
344 #endif
345 /*
346 // Function prototypes
347 */
348 void GBYTE(void*,void*,fortint*,fortint*);
349 fortint IGGLAT(fortint*,fortdouble*,fortint*,fortint*);
350 double pow(double,double);
351 
352 fortint findSectionOffsets(unsigned char*,fortint*,fortint*,fortint*,fortint*,fortint*,fortint*);
353 fortint prepareGrib(gribProduct**,unsigned char*);
354 fortint convertGRIBFloatToIEEE(unsigned char*);
355 fortdouble realValue(unsigned char*);
356 fortdouble referenceValue(gribProduct*);
357 fortdouble RGREFVL(gribProduct**);
358 fortint IGBTSPV(gribProduct**);
359 fortdouble RGDSCAL(gribProduct**);
360 fortint IGUNUSD(gribProduct**);
361 fortint GDECODE(gribProduct**,unsigned char*);
362 fortint IGLNGTH(gribProduct**);
363 fortint IGDATE(gribProduct**);
364 fortint IGTIME(gribProduct**);
365 fortint GSTEP(gribProduct**);
366 fortint IGTABLE(gribProduct**);
367 fortint IGCENTR(gribProduct**);
368 fortint IGPARAM(gribProduct**);
369 fortint IGLEVEL(gribProduct**);
370 fortint IGLEVL1(gribProduct**);
371 fortint IGLEVL2(gribProduct**);
372 fortint IGDEFIN(gribProduct**);
373 fortint IGCLASS(gribProduct**);
374 fortint IGTYPE(gribProduct**);
375 fortint IGSTREM(gribProduct**);
376 fortint IGEXPVR(gribProduct**);
377 fortint IGREPRS(gribProduct**);
378 fortint IGNUMPV(gribProduct**);
379 fortint GPV(gribProduct**,fortdouble*,fortint*);
380 fortint GPL(gribProduct**,fortint*,fortint*);
381 fortint IGNI(gribProduct**);
382 fortint IGNJ(gribProduct**);
383 fortdouble RGNWLAT(gribProduct**);
384 fortdouble RGNWLON(gribProduct**);
385 fortdouble RGSELAT(gribProduct**);
386 fortdouble RGSELON(gribProduct**);
387 fortint IGRESCO(gribProduct**);
388 fortdouble RGDI(gribProduct**);
389 fortdouble RGDJ(gribProduct**);
390 fortint IGGAUSS(gribProduct**);
391 fortint IGSCANM(gribProduct**);
392 fortint IGJ(gribProduct**);
393 fortint IGK(gribProduct**);
394 fortint IGM(gribProduct**);
395 fortint IGREPMO(gribProduct**);
396 fortdouble RGIP(gribProduct**);
397 fortint IGTJ(gribProduct**);
398 fortint IGTK(gribProduct**);
399 fortint IGTM(gribProduct**);
400 fortdouble RGLATRP(gribProduct**);
401 fortdouble RGLONRP(gribProduct**);
402 fortdouble RGROTAT(gribProduct**);
403 fortdouble RGLATSP(gribProduct**);
404 fortdouble RGLONSP(gribProduct**);
405 fortdouble RGSFACT(gribProduct**);
406 fortint numberOfValuesInSection4(gribProduct*);
407 fortint g4_offset(gribProduct*);
408 fortint IGNVALU(gribProduct**);
409 fortint GVALUES( gribProduct**,fortdouble*,fortint*,fortint*,fortdouble*);
410 void GVEND(gribProduct**);
411 fortint setupIrregularLongitudeIncrements(gribProduct**,fortint);
412 fortint GVINIT(gribProduct**,fortdouble*);
413 fortint GVECTOR( gribProduct**,fortdouble*,fortdouble*,fortdouble*);
414 fortint getSingleMapBit(unsigned char*,fortint);
415 void * allocateMemory(size_t);
416 void freeMemory(void*);
417 
418 #ifdef __cplusplus
419 }
420 #endif
421 
422 #endif /* End of GDECODE_H */
423