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