NCEPLIBS-g2c  1.6.4
g2_info.c
Go to the documentation of this file.
1 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "grib2.h"
13 
66 g2int
67 g2_info(unsigned char *cgrib, g2int *listsec0, g2int *listsec1,
68  g2int *numfields, g2int *numlocal)
69 {
70  g2int ierr, mapsec1len = 13;
71  g2int mapsec1[13] = {2, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1};
72  g2int i, j, istart, iofst, lengrib, lensec0, lensec1;
73  g2int ipos, isecnum, nbits, lensec;
74 
75  ierr = 0;
76  *numlocal = 0;
77  *numfields = 0;
78 
79  /* Check for beginning of GRIB message in the first 100 bytes. */
80  istart = -1;
81  for (j = 0; j < 100; j++)
82  {
83  if (cgrib[j] == 'G' && cgrib[j + 1] == 'R' && cgrib[j + 2] == 'I' &&
84  cgrib[j + 3] == 'B')
85  {
86  istart = j;
87  break;
88  }
89  }
90  if (istart == -1)
91  {
92  printf("g2_info: Beginning characters GRIB not found.");
93  ierr = 1;
94  return(ierr);
95  }
96 
97  /* Unpack Section 0 - Indicator Section. */
98  iofst = 8 * (istart + 6);
99  gbit(cgrib, listsec0, iofst, 8); /* Discipline */
100  iofst = iofst + 8;
101  gbit(cgrib, listsec0 + 1, iofst, 8); /* GRIB edition number */
102  iofst = iofst + 8;
103  iofst = iofst + 32;
104  gbit(cgrib, &lengrib, iofst, 32); /* Length of GRIB message */
105  iofst = iofst + 32;
106  listsec0[2] = lengrib;
107  lensec0 = 16;
108  ipos = istart + lensec0;
109 
110  /* Currently handles only GRIB Edition 2. */
111  if (listsec0[1] != 2)
112  {
113  printf("g2_info: can only decode GRIB edition 2.");
114  ierr = 2;
115  return(ierr);
116  }
117 
118  /* Unpack Section 1 - Identification Section */
119  gbit(cgrib, &lensec1, iofst, 32); /* Length of Section 1 */
120  iofst = iofst + 32;
121  gbit(cgrib, &isecnum, iofst, 8); /* Section number (1) */
122  iofst = iofst + 8;
123  if (isecnum != 1)
124  {
125  printf("g2_info: Could not find section 1.");
126  ierr = 3;
127  return(ierr);
128  }
129 
130  /* Unpack each input value in array listsec1 into the
131  appropriate number of octets, which are specified in
132  corresponding entries in array mapsec1. */
133  for (i = 0;i<mapsec1len;i++)
134  {
135  nbits = mapsec1[i]*8;
136  gbit(cgrib, listsec1 + i, iofst, nbits);
137  iofst = iofst + nbits;
138  }
139  ipos = ipos + lensec1;
140 
141  /* Loop through the remaining sections to see if they are
142  * valid. Also count the number of times Section 2 and Section
143  * 4 appear. */
144  for (;;)
145  {
146  if (cgrib[ipos]=='7' && cgrib[ipos + 1]=='7' && cgrib[ipos + 2]=='7' &&
147  cgrib[ipos + 3]=='7')
148  {
149  ipos = ipos + 4;
150  if (ipos != (istart + lengrib))
151  {
152  printf("g2_info: '7777' found, but not where expected.\n");
153  ierr = 4;
154  return(ierr);
155  }
156  break;
157  }
158 
159  iofst = ipos*8;
160  gbit(cgrib, &lensec, iofst, 32); /* Get Length of Section */
161  iofst = iofst + 32;
162  gbit(cgrib, &isecnum, iofst, 8); /* Get Section number */
163  iofst = iofst + 8;
164  ipos = ipos + lensec; /* Update beginning of section pointer */
165  if (ipos > (istart + lengrib))
166  {
167  printf("g2_info: '7777' not found at end of GRIB message.\n");
168  ierr = 5;
169  return(ierr);
170  }
171  if ( isecnum>=2 && isecnum<=7 )
172  {
173  if (isecnum == 2) /* Local Section 2 */
174  /* increment counter for total number of local sections found */
175  (*numlocal)++;
176 
177  else if (isecnum == 4)
178  /* increment counter for total number of fields found */
179  (*numfields)++;
180  }
181  else
182  {
183  printf("g2_info: Invalid section number found in GRIB message: %ld\n", isecnum);
184  ierr = 6;
185  return(ierr);
186  }
187  }
188 
189  return(0);
190 
191 }
g2int g2_info(unsigned char *cgrib, g2int *listsec0, g2int *listsec1, g2int *numfields, g2int *numlocal)
This subroutine searches through a GRIB2 message and returns the number of gridded fields found in th...
Definition: g2_info.c:67
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