NCEPLIBS-g2c  1.8.0
g2_unpack1.c
Go to the documentation of this file.
1 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include "grib2_int.h"
12 
54 g2int
55 g2_unpack1(unsigned char *cgrib, g2int *iofst, g2int **ids, g2int *idslen)
56 {
57 
58  g2int i, lensec, nbits, isecnum;
59  /* The map holds the number of bytes used by each value in section
60  * 1. */
61  g2int mapid[13] = {2, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1};
62 
63  *idslen = 13;
64 
65  gbit(cgrib, &lensec, *iofst, 32); /* Get Length of Section */
66  *iofst = *iofst + 32;
67  gbit(cgrib, &isecnum, *iofst, 8); /* Get Section Number */
68  *iofst = *iofst + 8;
69 
70  if (isecnum != 1)
71  {
72  *idslen = 13;
73  fprintf(stderr, "g2_unpack1: Not Section 1 data.\n");
74  return G2_UNPACK_BAD_SEC;
75  }
76 
77  /* Unpack each value into array ids from the appropriate number of
78  * octets, which are specified in` corresponding entries in array
79  * mapid. */
80  if (!(*ids = calloc(*idslen, sizeof(g2int))))
81  return G2_UNPACK_NO_MEM;
82 
83  for (i = 0; i < *idslen; i++)
84  {
85  nbits = mapid[i] * 8;
86  gbit(cgrib, *ids + i, *iofst, nbits);
87  *iofst = *iofst + nbits;
88  }
89 
90  return G2_NO_ERROR;
91 }
g2int g2_unpack1(unsigned char *cgrib, g2int *iofst, g2int **ids, g2int *idslen)
This subroutine unpacks Section 1 - Identification Section as defined in GRIB Edition 2.
Definition: g2_unpack1.c:55
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
#define G2_UNPACK_BAD_SEC
Bad section number in unpacking function.
Definition: grib2.h:464
#define G2_UNPACK_NO_MEM
Error allocating memory in unpack function.
Definition: grib2.h:465
#define G2_NO_ERROR
Function succeeded.
Definition: grib2.h:438
int64_t g2int
Long integer type.
Definition: grib2.h:33
Header file with internal function prototypes NCEPLIBS-g2c library.