Loading...
Searching...
No Matches
g2_create.c
Go to the documentation of this file.
#include "grib2_int.h"
#include <stdio.h>
#define MAPSEC1LEN 13
#define LENSEC0 16
g2int
g2_create(unsigned char *cgrib, g2int *listsec0, g2int *listsec1)
{
g2int zero = 0, one = 1;
/* The mapsec1 array tells us how many bytes are used in the GRIB
* message by each element of the listsec1 array. For example the
* first two elements of listsec1 are identifcation of originating
* section and sub-section - these are each 2-byte entries in the
* GRIB section 1 table, the IDENTIFICATION SECTION. (See
* https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_sect1.shtml). */
g2int mapsec1[MAPSEC1LEN] = {2, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1};
g2int i, lensec1, iofst, ibeg, nbits, len;
/* Only GRIB Edition 2 is acceptable. */
if (listsec0[1] != 2)
{
printf("g2_create: can only code GRIB edition 2.");
return G2_CREATE_GRIB_VERSION;
}
/* Pack Section 0 - Indicator Section (except for total length of
* GRIB message). */
cgrib[0] = 0x47; /* 'G' */
cgrib[1] = 0x52; /* 'R' */
cgrib[2] = 0x49; /* 'I' */
cgrib[3] = 0x42; /* 'B' */
sbit(cgrib, &zero, 32, 16); /* reserved for future use */
sbit(cgrib, listsec0 + 0, 48, 8); /* Discipline */
sbit(cgrib, listsec0 + 1, 56, 8); /* GRIB edition number */
/* Pack Section 1 - Identification Section. */
ibeg = LENSEC0 * 8; /* Calculate offset for beginning of section 1. */
iofst = ibeg + 32; /* Leave space for length of section. */
sbit(cgrib, &one, iofst, 8); /* Store section number (1). */
iofst = iofst + 8;
/* Pack up each input value in array listsec1 into the the
* appropriate number of octets, which are specified in
* corresponding entries in array mapsec1. */
for (i = 0; i < MAPSEC1LEN; i++)
{
nbits = mapsec1[i] * 8;
sbit(cgrib, listsec1 + i, iofst, nbits);
iofst = iofst + nbits;
}
/* Calculate length of section 1 and store it in octets 1-4 of
* section 1. */
lensec1 = (iofst - ibeg) / 8;
sbit(cgrib, &lensec1, ibeg, 32);
/* Put current byte total of message into Section 0. */
sbit(cgrib, &zero, 64, 32);
len = LENSEC0 + lensec1;
sbit(cgrib, &len, 96, 32);
return (len);
}
g2_create
g2int g2_create(unsigned char *cgrib, g2int *listsec0, g2int *listsec1)
Initialize a new GRIB2 message and pack GRIB2 Section 0 (Indicator Section) and Section 1 (Identifica...
Definition g2_create.c:68
LENSEC0
#define LENSEC0
Length of GRIB Section 0.
Definition g2_create.c:11
MAPSEC1LEN
#define MAPSEC1LEN
Length of Map Section 1.
Definition g2_create.c:10
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_CREATE_GRIB_VERSION
#define G2_CREATE_GRIB_VERSION
Wrong GRIB version for g2_create(), must be 2.
Definition grib2.h:432
g2int
int64_t g2int
Long integer type.
Definition grib2.h:32
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.