70 static unsigned char G = 0x47;
71 static unsigned char R = 0x52;
72 static unsigned char I = 0x49;
73 static unsigned char B = 0x42;
74 static unsigned char seven = 0x37;
76 static g2int one = 1, three = 3, miss = 65535;
77 g2int lensec3, iofst, ibeg, lencurr, len;
78 g2int i, j, temp, ilen, isecnum, nbits;
82 if (cgrib[0] != G || cgrib[1] != R || cgrib[2] != I || cgrib[3] != B)
84 printf(
"g2_addgrid: GRIB not found in given message.\n");
85 printf(
"g2_addgrid: Call to routine gribcreate required to initialize GRIB messge.\n");
91 gbit(cgrib, &lencurr, 96, 32);
94 if (cgrib[lencurr - 4] == seven && cgrib[lencurr - 3] == seven &&
95 cgrib[lencurr - 2] == seven && cgrib[lencurr - 1] == seven)
97 printf(
"g2_addgrid: GRIB message already complete. Cannot add new section.\n");
109 gbit(cgrib, &ilen, iofst, 32);
111 gbit(cgrib, &isecnum, iofst, 8);
114 if (len == lencurr)
break;
119 printf(
"g2_addgrid: Section byte counts don''t add to total.\n");
120 printf(
"g2_addgrid: Sum of section byte counts = %ld\n", len);
121 printf(
"g2_addgrid: Total byte count in Section 0 = %ld\n", lencurr);
128 if (isecnum != 1 && isecnum != 2 && isecnum != 7)
130 printf(
"g2_addgrid: Section 3 can only be added after Section 1, 2 or 7.\n");
131 printf(
"g2_addgrid: Section ',isecnum,' was the last found in given GRIB message.\n");
139 sbit(cgrib, &three, iofst, 8);
141 sbit(cgrib, igds+0, iofst, 8);
143 sbit(cgrib, igds+1, iofst, 32);
145 sbit(cgrib, igds+2, iofst, 8);
147 sbit(cgrib, igds+3, iofst, 8);
153 sbit(cgrib, igds+4, iofst, 16);
155 sbit(cgrib, &miss, iofst, 16);
180 for (i = 0; i < mapgrid->
maplen; i++)
182 nbits = abs(mapgrid->
map[i]) * 8;
183 if ((mapgrid->
map[i] >= 0) || (igdstmpl[i] >= 0))
184 sbit(cgrib, igdstmpl+i, iofst, nbits);
187 sbit(cgrib, &one, iofst, 1);
188 temp = abs(igdstmpl[i]);
189 sbit(cgrib, &temp, iofst+1, nbits-1);
191 iofst = iofst + nbits;
198 for (i = 0; i < mapgrid->
extlen; i++)
200 nbits = abs(mapgrid->
ext[i]) * 8;
201 if (mapgrid->
ext[i] >= 0 || igdstmpl[j] >= 0)
202 sbit(cgrib, igdstmpl + j, iofst, nbits);
205 sbit(cgrib, &one, iofst, 1);
206 temp = abs(igdstmpl[j]);
207 sbit(cgrib, &temp, iofst + 1, nbits - 1);
209 iofst = iofst + nbits;
221 sbits(cgrib, ideflist, iofst, nbits, 0, idefnum);
222 iofst = iofst + (nbits * idefnum);
226 lensec3 = (iofst - ibeg) / 8;
227 sbit(cgrib, &lensec3, ibeg, 32);
231 sbit(cgrib, &lencurr, 96, 32);
g2int g2_addgrid(unsigned char *cgrib, g2int *igds, g2int *igdstmpl, g2int *ideflist, g2int idefnum)
This routine packs up a Grid Definition Section (Section 3) and adds it to a GRIB2 message.
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbyte)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
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 ...
void sbits(unsigned char *out, g2int *in, g2int iskip, g2int nbyte, g2int nskip, g2int n)
Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each ...
Header file for NCEPLIBS-g2c library.
g2int * ext
Number of octets of each entry in the extension part of the template.
g2int extlen
Number of entries in the template extension.
g2int * map
Number of octets of each entry in the static part of the template.
g2int needext
Indicates whether or not the template needs to be extended.
g2int maplen
Number of entries in the static part of the template.
int64_t g2int
Long integer type.
Struct for GRIB template.
gtemplate * extgridtemplate(g2int number, g2int *list)
This subroutine generates the remaining octet map for a given Grid Definition Template,...
gtemplate * getgridtemplate(g2int number)
This subroutine returns grid template information for a specified Grid Definition Template for [Secti...