NCEPLIBS-g2c  1.7.0
g2_unpack2.c
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "grib2_int.h"
10 
37 g2int
38 g2_unpack2(unsigned char *cgrib, g2int *iofst, g2int *lencsec2,
39  unsigned char **csec2)
40 {
41  g2int isecnum;
42  g2int lensec, ipos, j;
43 
44  *lencsec2 = 0;
45  *csec2 = NULL;
46 
47  /* Get Length of Section. */
48  gbit(cgrib, &lensec, *iofst, 32);
49  *iofst = *iofst + 32;
50  *lencsec2 = lensec - 5;
51 
52  /* Get Section Number. */
53  gbit(cgrib, &isecnum, *iofst, 8);
54  *iofst = *iofst + 8;
55  ipos = *iofst / 8;
56 
57  if (isecnum != 2)
58  {
59  *lencsec2 = 0;
60  fprintf(stderr, "g2_unpack2: Not Section 2 data.\n");
61  return G2_UNPACK_BAD_SEC;
62  }
63 
64  /* If the length is 0, we are done. */
65  if (*lencsec2 == 0)
66  return G2_NO_ERROR;
67 
68  if (!(*csec2 = malloc(*lencsec2)))
69  {
70  *lencsec2 = 0;
71  return G2_UNPACK_NO_MEM;
72  }
73 
74  for (j = 0; j < *lencsec2; j++)
75  (*csec2)[j] = cgrib[ipos + j];
76 
77  *iofst = *iofst + (*lencsec2 * 8);
78 
79  return G2_NO_ERROR;
80 }
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
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
g2_unpack2
g2int g2_unpack2(unsigned char *cgrib, g2int *iofst, g2int *lencsec2, unsigned char **csec2)
This subroutine unpacks Section 2 (Local Use Section) as defined in GRIB Edition 2.
Definition: g2_unpack2.c:38
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
G2_UNPACK_BAD_SEC
#define G2_UNPACK_BAD_SEC
Bad section number in unpacking function.
Definition: grib2.h:301