NCEPLIBS-g2c 1.9.0
Loading...
Searching...
No Matches
g2_create.c
Go to the documentation of this file.
1
7#include <stdio.h>
8#include "grib2_int.h"
9
10#define MAPSEC1LEN 13
11#define LENSEC0 16
68g2_create(unsigned char *cgrib, g2int *listsec0, g2int *listsec1)
69{
70 g2int zero = 0, one = 1;
71
72 /* The mapsec1 array tells us how many bytes are used in the GRIB
73 * message by each element of the listsec1 array. For example the
74 * first two elements of listsec1 are identifcation of originating
75 * section and sub-section - these are each 2-byte entries in the
76 * GRIB section 1 table, the IDENTIFICATION SECTION. (See
77 * https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_sect1.shtml). */
78 g2int mapsec1[MAPSEC1LEN] = {2, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1};
79 g2int i, lensec1, iofst, ibeg, nbits, len;
80
81 /* Only GRIB Edition 2 is acceptable. */
82 if (listsec0[1] != 2)
83 {
84 printf("g2_create: can only code GRIB edition 2.");
86 }
87
88 /* Pack Section 0 - Indicator Section (except for total length of
89 * GRIB message). */
90 cgrib[0] = 0x47; /* 'G' */
91 cgrib[1] = 0x52; /* 'R' */
92 cgrib[2] = 0x49; /* 'I' */
93 cgrib[3] = 0x42; /* 'B' */
94 sbit(cgrib, &zero, 32, 16); /* reserved for future use */
95 sbit(cgrib, listsec0 + 0, 48, 8); /* Discipline */
96 sbit(cgrib, listsec0 + 1, 56, 8); /* GRIB edition number */
97
98 /* Pack Section 1 - Identification Section. */
99 ibeg = LENSEC0 * 8; /* Calculate offset for beginning of section 1. */
100 iofst = ibeg + 32; /* Leave space for length of section. */
101 sbit(cgrib, &one, iofst, 8); /* Store section number (1). */
102 iofst = iofst + 8;
103
104 /* Pack up each input value in array listsec1 into the the
105 * appropriate number of octets, which are specified in
106 * corresponding entries in array mapsec1. */
107 for (i = 0; i < MAPSEC1LEN; i++)
108 {
109 nbits = mapsec1[i] * 8;
110 sbit(cgrib, listsec1 + i, iofst, nbits);
111 iofst = iofst + nbits;
112 }
113
114 /* Calculate length of section 1 and store it in octets 1-4 of
115 * section 1. */
116 lensec1 = (iofst - ibeg) / 8;
117 sbit(cgrib, &lensec1, ibeg, 32);
118
119 /* Put current byte total of message into Section 0. */
120 sbit(cgrib, &zero, 64, 32);
121 len = LENSEC0 + lensec1;
122 sbit(cgrib, &len, 96, 32);
123 return (len);
124}
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
#define LENSEC0
Length of GRIB Section 0.
Definition g2_create.c:11
#define MAPSEC1LEN
Length of Map Section 1.
Definition g2_create.c:10
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_CREATE_GRIB_VERSION
Wrong GRIB version for g2_create(), must be 2.
Definition grib2.h:439
int64_t g2int
Long integer type.
Definition grib2.h:32
Header file with internal function prototypes NCEPLIBS-g2c library.