NCEPLIBS-g2c  1.6.4
g2_unpack2.c
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "grib2.h"
10 
36 g2int
37 g2_unpack2(unsigned char *cgrib, g2int *iofst, g2int *lencsec2,
38  unsigned char **csec2)
39 {
40  g2int ierr = 0, isecnum;
41  g2int lensec, ipos, j;
42 
43  *lencsec2 = 0;
44  *csec2 = NULL;
45 
46  gbit(cgrib, &lensec, *iofst, 32); /* Get Length of Section */
47  *iofst = *iofst + 32;
48  *lencsec2 = lensec - 5;
49  gbit(cgrib, &isecnum, *iofst, 8); /* Get Section Number */
50  *iofst = *iofst + 8;
51  ipos = (*iofst/8);
52 
53  if ( isecnum != 2 )
54  {
55  ierr = 2;
56  *lencsec2 = 0;
57  fprintf(stderr, "g2_unpack2: Not Section 2 data.\n");
58  return(ierr);
59  }
60 
61  if (*lencsec2 == 0)
62  {
63  ierr = 0;
64  return(ierr);
65  }
66 
67  if (!(*csec2 = malloc(*lencsec2 + 1)))
68  {
69  ierr = 6;
70  *lencsec2 = 0;
71  return(ierr);
72  }
73 
74  /*printf(" SAGIPO %d \n", (int)ipos);*/
75  for (j = 0; j < *lencsec2; j++)
76  *(*csec2 + j) = cgrib[ipos + j];
77 
78  *iofst = *iofst + (*lencsec2 * 8);
79 
80  return(ierr); /* End of Section 2 processing */
81 }
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:37
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.
int64_t g2int
Long integer type.
Definition: grib2.h:20