NCEPLIBS-g2c  1.6.4
g2_unpack1.c
Go to the documentation of this file.
1 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include "grib2.h"
12 
54 g2int
55 g2_unpack1(unsigned char *cgrib, g2int *iofst, g2int **ids, g2int *idslen)
56 {
57 
58  g2int i, lensec, nbits, ierr, 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  ierr = 0;
64  *idslen = 13;
65 
66  gbit(cgrib, &lensec, *iofst, 32); /* Get Length of Section */
67  *iofst = *iofst + 32;
68  gbit(cgrib, &isecnum, *iofst, 8); /* Get Section Number */
69  *iofst = *iofst + 8;
70 
71  if (isecnum != 1)
72  {
73  ierr = 2;
74  *idslen = 13;
75  fprintf(stderr, "g2_unpack1: Not Section 1 data.\n");
76  return(ierr);
77  }
78 
79  /* Unpack each value into array ids from the appropriate number of
80  * octets, which are specified in` corresponding entries in array
81  * mapid. */
82  *ids = calloc(*idslen, sizeof(g2int));
83  if (*ids == 0)
84  {
85  ierr = 6;
86  return(ierr);
87  }
88 
89  for (i = 0; i < *idslen; i++)
90  {
91  nbits = mapid[i] * 8;
92  gbit(cgrib, *ids + i, *iofst, nbits);
93  *iofst = *iofst + nbits;
94  }
95 
96  return(ierr); // End of Section 1 processing
97 }
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 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