NCEPLIBS-g2c  1.7.0
g2_unpack5.c
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "grib2_int.h"
10 
41 g2int
42 g2_unpack5(unsigned char *cgrib, g2int *iofst, g2int *ndpts, g2int *idrsnum,
43  g2int **idrstmpl, g2int *mapdrslen)
44 {
45  g2int needext, i, j, nbits, isecnum;
46  g2int lensec, isign, newlen;
47  g2int *lidrstmpl = 0;
48  gtemplate *mapdrs;
49 
50  *idrstmpl = 0; /* NULL*/
51 
52  gbit(cgrib, &lensec, *iofst, 32); /* Get Length of Section */
53  *iofst = *iofst + 32;
54  gbit(cgrib, &isecnum, *iofst, 8); /* Get Section Number */
55  *iofst = *iofst + 8;
56 
57  if (isecnum != 5)
58  {
59  *ndpts = 0;
60  *mapdrslen = 0;
61  return G2_UNPACK_BAD_SEC;
62  }
63 
64  gbit(cgrib, ndpts, *iofst, 32); /* Get num of data points */
65  *iofst = *iofst + 32;
66  gbit(cgrib, idrsnum, *iofst, 16); /* Get Data Rep Template Num. */
67  *iofst = *iofst + 16;
68 
69  /* Gen Data Representation Template */
70  if (!(mapdrs = getdrstemplate(*idrsnum)))
71  {
72  *mapdrslen = 0;
73  return G2_UNPACK5_BAD_DRT;
74  }
75  *mapdrslen = mapdrs->maplen;
76  needext = mapdrs->needext;
77 
78  /* Unpack each value into array ipdstmpl from the appropriate
79  * number of octets, which are specified in corresponding
80  * entries in array mapdrs. */
81  if (*mapdrslen > 0)
82  lidrstmpl = calloc(*mapdrslen, sizeof(g2int));
83  if (!lidrstmpl)
84  {
85  *mapdrslen = 0;
86  *idrstmpl = NULL;
87  if (mapdrs)
88  free(mapdrs);
89  return G2_UNPACK_NO_MEM;
90  }
91  else
92  {
93  *idrstmpl = lidrstmpl;
94  }
95  for (i = 0; i < mapdrs->maplen; i++)
96  {
97  nbits = abs(mapdrs->map[i]) * 8;
98  if (mapdrs->map[i] >= 0)
99  {
100  gbit(cgrib, lidrstmpl + i, *iofst, nbits);
101  }
102  else
103  {
104  gbit(cgrib, &isign, *iofst, 1);
105  gbit(cgrib, lidrstmpl + i, *iofst + 1, nbits - 1);
106  if (isign == 1)
107  lidrstmpl[i] = -1 * lidrstmpl[i];
108  }
109  *iofst = *iofst + nbits;
110  }
111 
112  /* Check to see if the Data Representation Template needs to be
113  * extended. The number of values in a specific gtemplate may
114  * vary depending on data specified in the "static" part of the
115  * gtemplate. */
116  if (needext == 1)
117  {
118  free(mapdrs);
119  mapdrs = extdrstemplate(*idrsnum, lidrstmpl);
120  newlen = mapdrs->maplen + mapdrs->extlen;
121  lidrstmpl = realloc(lidrstmpl, newlen * sizeof(g2int));
122  *idrstmpl = lidrstmpl;
123 
124  /* Unpack the rest of the Data Representation Template */
125  j = 0;
126  for (i = *mapdrslen; i < newlen; i++)
127  {
128  nbits = abs(mapdrs->ext[j]) * 8;
129  if (mapdrs->ext[j] >= 0)
130  {
131  gbit(cgrib, lidrstmpl + i, *iofst, nbits);
132  }
133  else
134  {
135  gbit(cgrib, &isign, *iofst, 1);
136  gbit(cgrib, lidrstmpl + i, *iofst + 1, nbits - 1);
137  if (isign == 1)
138  lidrstmpl[i] = -1 * lidrstmpl[i];
139  }
140  *iofst = *iofst + nbits;
141  j++;
142  }
143  *mapdrslen = newlen;
144  }
145  if (mapdrs->ext)
146  free(mapdrs->ext);
147  if (mapdrs)
148  free(mapdrs);
149 
150  return G2_NO_ERROR;
151 }
g2_unpack5
g2int g2_unpack5(unsigned char *cgrib, g2int *iofst, g2int *ndpts, g2int *idrsnum, g2int **idrstmpl, g2int *mapdrslen)
This subroutine unpacks Section 5 (Data Representation Section) as defined in GRIB Edition 2.
Definition: g2_unpack5.c:42
gtemplate::ext
g2int * ext
Number of octets of each entry in the extension part of the template.
Definition: grib2_int.h:53
G2_UNPACK_NO_MEM
#define G2_UNPACK_NO_MEM
Error allocating memory in unpack function.
Definition: grib2.h:302
G2_NO_ERROR
#define G2_NO_ERROR
Function succeeded.
Definition: grib2.h:275
getdrstemplate
gtemplate * getdrstemplate(g2int number)
This subroutine returns DRS template information for a specified Data Representation Template.
Definition: drstemplates.c:166
gtemplate::needext
g2int needext
Indicates whether or not the template needs to be extended.
Definition: grib2_int.h:46
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
extdrstemplate
gtemplate * extdrstemplate(g2int number, g2int *list)
This subroutine generates the remaining octet map for a given Data Representation Template,...
Definition: drstemplates.c:212
gtemplate::maplen
g2int maplen
Number of entries in the static part of the template.
Definition: grib2_int.h:39
G2_UNPACK5_BAD_DRT
#define G2_UNPACK5_BAD_DRT
In g2_unpack5(), undefined Data Representation Template.
Definition: grib2.h:305
gtemplate::map
g2int * map
Number of octets of each entry in the static part of the template.
Definition: grib2_int.h:43
gbit
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbits)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
Definition: gbits.c:20
g2int
int64_t g2int
Long integer type.
Definition: grib2.h:28
gtemplate::extlen
g2int extlen
Number of entries in the template extension.
Definition: grib2_int.h:49
G2_UNPACK_BAD_SEC
#define G2_UNPACK_BAD_SEC
Bad section number in unpacking function.
Definition: grib2.h:301
gtemplate
Struct for GRIB template.
Definition: grib2_int.h:28