Loading...
Searching...
No Matches
g2_info.c
Go to the documentation of this file.
#include "grib2_int.h"
#include <stdio.h>
#include <stdlib.h>
g2int
g2_info(unsigned char *cgrib, g2int *listsec0, g2int *listsec1,
g2int *numfields, g2int *numlocal)
{
g2int mapsec1len = 13;
g2int mapsec1[13] = {2, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1};
g2int i, j, istart, iofst, lengrib, lensec0, lensec1;
g2int ipos, isecnum, nbits, lensec;
*numlocal = 0;
*numfields = 0;
LOG((2, "g2_info"));
/* Check for beginning of GRIB message in the first 100 bytes. */
istart = -1;
for (j = 0; j < 100; j++)
{
if (cgrib[j] == 'G' && cgrib[j + 1] == 'R' && cgrib[j + 2] == 'I' &&
cgrib[j + 3] == 'B')
{
istart = j;
break;
}
}
if (istart == -1)
{
printf("g2_info: Beginning characters GRIB not found.");
return G2_INFO_NO_GRIB;
}
LOG((3, "msg found at byte %ld", istart));
/* Unpack Section 0 - Indicator Section. */
iofst = 8 * (istart + 6);
gbit(cgrib, listsec0, iofst, 8); /* Discipline */
iofst = iofst + 8;
gbit(cgrib, &listsec0[1], iofst, 8); /* GRIB edition number */
iofst = iofst + 8;
/* iofst = iofst + 32; */
/* gbit(cgrib, &lengrib, iofst, 32); /\* Length of GRIB message *\/ */
gbit(cgrib, &lengrib, iofst, 64); /* Length of GRIB message */
/* iofst = iofst + 32; */
iofst = iofst + 64;
listsec0[2] = lengrib;
lensec0 = 16;
ipos = istart + lensec0;
LOG((3, "unpacked section 0, lengrib %ld now at byte %ld", lengrib, ipos));
/* Currently handles only GRIB Edition 2. */
if (listsec0[1] != 2)
{
printf("g2_info: can only decode GRIB edition 2.");
return G2_INFO_GRIB_VERSION;
}
/* Unpack Section 1 - Identification Section */
gbit(cgrib, &lensec1, iofst, 32); /* Length of Section 1 */
iofst = iofst + 32;
gbit(cgrib, &isecnum, iofst, 8); /* Section number (1) */
iofst = iofst + 8;
if (isecnum != 1)
{
printf("g2_info: Could not find section 1.");
return G2_INFO_NO_SEC1;
}
/* Unpack each input value in array listsec1 into the
appropriate number of octets, which are specified in
corresponding entries in array mapsec1. */
for (i = 0; i < mapsec1len; i++)
{
nbits = mapsec1[i] * 8;
gbit(cgrib, &listsec1[i], iofst, nbits);
iofst = iofst + nbits;
}
ipos = ipos + lensec1;
LOG((3, "unpacked section 1, now at byte %ld", ipos));
/* Loop through the remaining sections to see if they are
* valid. Also count the number of times Section 2 and Section
* 4 appear. */
for (;;)
{
if (cgrib[ipos] == '7' && cgrib[ipos + 1] == '7' && cgrib[ipos + 2] == '7' &&
cgrib[ipos + 3] == '7')
{
LOG((3, "found 7777 at byte %ld", ipos));
ipos = ipos + 4;
if (ipos != (istart + lengrib))
{
printf("g2_info: '7777' found, but not where expected.\n");
return G2_INFO_WRONG_END;
}
break;
}
iofst = ipos * 8;
gbit(cgrib, &lensec, iofst, 32); /* Get Length of Section */
iofst = iofst + 32;
gbit(cgrib, &isecnum, iofst, 8); /* Get Section number */
LOG((3, "found section number %ld of length %ld", isecnum, lensec));
iofst = iofst + 8;
ipos = ipos + lensec; /* Update beginning of section pointer */
if (ipos > (istart + lengrib))
{
printf("g2_info: '7777' not found at end of GRIB message.\n");
return G2_INFO_BAD_END;
}
if (isecnum >= 2 && isecnum <= 7)
{
/* Increment counter for total number of local sections
* or fields found. */
if (isecnum == 2)
(*numlocal)++;
else if (isecnum == 4)
(*numfields)++;
}
else
{
printf("g2_info: Invalid section number found in GRIB message: %ld\n", isecnum);
return G2_INFO_INVAL_SEC;
}
}
return 0;
}
g2_info
g2int g2_info(unsigned char *cgrib, g2int *listsec0, g2int *listsec1, g2int *numfields, g2int *numlocal)
Search through a GRIB2 message and return the number of gridded fields found in the message and the n...
Definition g2_info.c:70
gbit
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbits)
Get arbitrary size values from a packed bit string, right justifying each value in the unpacked iout ...
Definition gbits.c:20
G2_INFO_NO_SEC1
#define G2_INFO_NO_SEC1
g2_info() can't find section 1.
Definition grib2.h:435
G2_INFO_INVAL_SEC
#define G2_INFO_INVAL_SEC
g2_info() found invalid section number.
Definition grib2.h:438
G2_INFO_GRIB_VERSION
#define G2_INFO_GRIB_VERSION
Wrong GRIB version for g2_info(), must be 2.
Definition grib2.h:434
G2_INFO_BAD_END
#define G2_INFO_BAD_END
g2_info() didn't find "7777" at end of message.
Definition grib2.h:437
g2int
int64_t g2int
Long integer type.
Definition grib2.h:32
G2_INFO_NO_GRIB
#define G2_INFO_NO_GRIB
g2_info() can't find beginning characters "GRIB".
Definition grib2.h:433
G2_INFO_WRONG_END
#define G2_INFO_WRONG_END
g2_info() found "7777" not where expected.
Definition grib2.h:436
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
LOG
#define LOG(e)
Ignore logging to stdout.
Definition grib2_int.h:428