Loading...
Searching...
No Matches
g2_gribend.c
Go to the documentation of this file.
#include "grib2_int.h"
#include <stdio.h>
g2int
g2_gribend(unsigned char *cgrib)
{
g2int iofst, lencurr, len, ilen, isecnum;
g2int lengrib;
unsigned char seven = 0x37; /* '7' */
int ret;
/* Check for GRIB header and terminator. Translate the error codes
* to the legacy G2 error codes. */
if ((ret = g2c_check_msg(cgrib, &lencurr, 1, 4)))
{
if (ret == G2C_ENOTGRIB)
return G2_ADD_MSG_INIT;
if (ret == G2C_EMSGCOMPLETE)
return G2_ADD_MSG_COMPLETE;
}
/* Loop through all current sections of the GRIB message to find
* the last section number. */
len = 16; /* Length of Section 0. */
for (;;)
{
/* Get number and length of next section. */
iofst = len * 8;
gbit(cgrib, &ilen, iofst, 32);
iofst = iofst + 32;
gbit(cgrib, &isecnum, iofst, 8);
len = len + ilen;
/* Exit loop if last section reached. */
if (len == lencurr)
break;
/* If byte count for each section doesn't match current
* total length, then there is a problem. */
if (len > lencurr)
{
printf("g2_gribend: Section byte counts don''t add to total.\n");
printf("g2_gribend: Sum of section byte counts = %d\n", (int)len);
printf("g2_gribend: Total byte count in Section 0 = %d\n", (int)lencurr);
return G2_BAD_SEC_COUNTS;
}
}
/* Can only add End Section (Section 8) after Section 7. */
if (isecnum != 7)
{
printf("g2_gribend: Section 8 can only be added after Section 7.\n");
printf("g2_gribend: Section %ld was the last found in given GRIB message.\n", isecnum);
return G2_BAD_SEC;
}
/* Add Section 8 - End Section */
cgrib[lencurr] = seven;
cgrib[lencurr + 1] = seven;
cgrib[lencurr + 2] = seven;
cgrib[lencurr + 3] = seven;
/* Update current byte total of message in Section 0. */
lengrib = lencurr + 4;
sbit(cgrib, &lengrib, 96, 32);
/* Return the length of the message. */
return lengrib;
}
g2_gribend
g2int g2_gribend(unsigned char *cgrib)
Finalize a GRIB2 message after all grids and fields have been added.
Definition g2_gribend.c:40
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
sbit
void sbit(unsigned char *out, g2int *in, g2int iskip, g2int nbits)
Store arbitrary size values into a packed bit string, taking the low order bits from each value in th...
Definition gbits.c:38
G2_BAD_SEC_COUNTS
#define G2_BAD_SEC_COUNTS
Sum of Section byte counts doesn't add to total byte count.
Definition grib2.h:474
G2_BAD_SEC
#define G2_BAD_SEC
Previous Section was unexpected.
Definition grib2.h:462
G2_ADD_MSG_COMPLETE
#define G2_ADD_MSG_COMPLETE
GRIB message already complete.
Definition grib2.h:473
G2_ADD_MSG_INIT
#define G2_ADD_MSG_INIT
GRIB message was not initialized - call g2_create() first.
Definition grib2.h:472
G2C_ENOTGRIB
#define G2C_ENOTGRIB
GRIB header not found.
Definition grib2.h:492
G2C_EMSGCOMPLETE
#define G2C_EMSGCOMPLETE
GRIB message already complete.
Definition grib2.h:493
g2int
int64_t g2int
Long integer type.
Definition grib2.h:32
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
g2c_check_msg
int g2c_check_msg(unsigned char *cgrib, g2int *lencurr, int verbose, int from)
Check for 'GRIB' at the beginning of a GRIB message, and check to see if the message is already termi...
Definition util.c:41