NCEPLIBS-g2c 1.9.0
Loading...
Searching...
No Matches
g2_gribend.c
Go to the documentation of this file.
1
6#include <stdio.h>
7#include "grib2_int.h"
8
39g2int g2_gribend(unsigned char *cgrib)
40{
41 g2int iofst, lencurr, len, ilen, isecnum;
42 g2int lengrib;
43 unsigned char seven = 0x37; /* '7' */
44 int ret;
45
46 /* Check for GRIB header and terminator. Translate the error codes
47 * to the legacy G2 error codes. */
48 if ((ret = g2c_check_msg(cgrib, &lencurr, 1)))
49 {
50 if (ret == G2C_ENOTGRIB)
51 return G2_ADD_MSG_INIT;
52 if (ret == G2C_EMSGCOMPLETE)
54 }
55
56 /* Loop through all current sections of the GRIB message to find
57 * the last section number. */
58 len = 16; /* Length of Section 0. */
59 for (;;)
60 {
61 /* Get number and length of next section. */
62 iofst = len * 8;
63 gbit(cgrib, &ilen, iofst, 32);
64 iofst = iofst + 32;
65 gbit(cgrib, &isecnum, iofst, 8);
66 len = len + ilen;
67
68 /* Exit loop if last section reached. */
69 if (len == lencurr)
70 break;
71
72 /* If byte count for each section doesn't match current
73 * total length, then there is a problem. */
74 if (len > lencurr)
75 {
76 printf("g2_gribend: Section byte counts don''t add to total.\n");
77 printf("g2_gribend: Sum of section byte counts = %d\n", (int)len);
78 printf("g2_gribend: Total byte count in Section 0 = %d\n", (int)lencurr);
79 return G2_BAD_SEC_COUNTS;
80 }
81 }
82
83 /* Can only add End Section (Section 8) after Section 7. */
84 if (isecnum != 7 )
85 {
86 printf("g2_gribend: Section 8 can only be added after Section 7.\n");
87 printf("g2_gribend: Section %ld was the last found in given GRIB message.\n", isecnum);
88 return G2_BAD_SEC;
89 }
90
91 /* Add Section 8 - End Section */
92 cgrib[lencurr] = seven;
93 cgrib[lencurr + 1] = seven;
94 cgrib[lencurr + 2] = seven;
95 cgrib[lencurr + 3] = seven;
96
97 /* Update current byte total of message in Section 0. */
98 lengrib = lencurr + 4;
99 sbit(cgrib, &lengrib, 96, 32);
100
101 /* Return the length of the message. */
102 return lengrib;
103}
g2int g2_gribend(unsigned char *cgrib)
Finalize a GRIB2 message after all grids and fields have been added.
Definition g2_gribend.c:39
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
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
#define G2_BAD_SEC_COUNTS
Sum of Section byte counts doesn't add to total byte count.
Definition grib2.h:475
#define G2_BAD_SEC
Previous Section was unexpected.
Definition grib2.h:463
#define G2_ADD_MSG_COMPLETE
GRIB message already complete.
Definition grib2.h:474
#define G2_ADD_MSG_INIT
GRIB message was not initialized - call g2_create() first.
Definition grib2.h:473
#define G2C_ENOTGRIB
GRIB header not found.
Definition grib2.h:493
#define G2C_EMSGCOMPLETE
GRIB message already complete.
Definition grib2.h:494
int64_t g2int
Long integer type.
Definition grib2.h:32
Header file with internal function prototypes NCEPLIBS-g2c library.
int g2c_check_msg(unsigned char *cgrib, g2int *lencurr, int verbose)
Check for 'GRIB' at the beginning of a GRIB message, and check to see if the message is already termi...
Definition util.c:26