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