Debian Codegraph @

frmts/grib/degrib/g2clib/g2_addgrid.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "grib2.h"
4
5
6 g2int g2_addgrid(unsigned char *cgrib,g2int *igds,g2int *igdstmpl,g2int *ideflist,g2int idefnum)
7 //$$$ SUBPROGRAM DOCUMENTATION BLOCK
8 // . . . .
9 // SUBPROGRAM: g2_addgrid
10 // PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-11-01
11 //
12 // ABSTRACT: This routine packs up a Grid Definition Section (Section 3)
13 // and adds it to a GRIB2 message. It is used with routines "g2_create",
14 // "g2_addlocal", "g2_addfield",
15 // and "g2_gribend" to create a complete GRIB2 message.
16 // g2_create must be called first to initialize a new GRIB2 message.
17 //
18 // PROGRAM HISTORY LOG:
19 // 2002-11-01 Gilbert
20 //
21 // USAGE: int g2_addgrid(unsigned char *cgrib,g2int *igds,g2int *igdstmpl,
22 // g2int *ideflist,g2int idefnum)
23 // INPUT ARGUMENTS:
24 // cgrib - Char array that contains the GRIB2 message to which
25 // section should be added.
26 // igds - Contains information needed for GRIB Grid Definition Section 3
27 // Must be dimensioned >= 5.
28 // igds[0]=Source of grid definition (see Code Table 3.0)
29 // igds[1]=Number of grid points in the defined grid.
30 // igds[2]=Number of octets needed for each
31 // additional grid points definition.
32 // Used to define number of
33 // points in each row ( or column ) for
34 // non-regular grids.
35 // = 0, if using regular grid.
36 // igds[3]=Interpretation of list for optional points
37 // definition. (Code Table 3.11)
38 // igds[4]=Grid Definition Template Number (Code Table 3.1)
39 // igdstmpl - Contains the data values for the specified Grid Definition
40 // Template ( NN=igds[4] ). Each element of this integer
41 // array contains an entry (in the order specified) of Grid
42 // Definition Template 3.NN
43 // ideflist - (Used if igds[2] != 0) This array contains the
44 // number of grid points contained in each row ( or column )
45 // idefnum - (Used if igds[2] != 0) The number of entries
46 // in array ideflist. i.e. number of rows ( or columns )
47 // for which optional grid points are defined.
48 //
49 // OUTPUT ARGUMENTS:
50 // cgrib - Char array to contain the updated GRIB2 message.
51 // Must be allocated large enough to store the entire
52 // GRIB2 message.
53 //
54 // RETURN VALUES:
55 // ierr - Return code.
56 // > 0 = Current size of updated GRIB2 message
57 // -1 = GRIB message was not initialized. Need to call
58 // routine gribcreate first.
59 // -2 = GRIB message already complete. Cannot add new section.
60 // -3 = Sum of Section byte counts doesn't add to total byte count
61 // -4 = Previous Section was not 1, 2 or 7.
62 // -5 = Could not find requested Grid Definition Template.
63 //
64 // REMARKS: Note that the Grid Def Section ( Section 3 ) can only follow
65 // Section 1, 2 or Section 7 in a GRIB2 message.
66 //
67 // ATTRIBUTES:
68 // LANGUAGE: C
69 // MACHINE:
70 //
71 //$$$
72 {
73
74 g2int ierr;
75 const unsigned char G=0x47; // 'G'
76 const unsigned char R=0x52; // 'R'
77 const unsigned char I=0x49; // 'I'
78 const unsigned char B=0x42; // 'B'
79 const unsigned char seven=0x37; // '7'
80
81 const g2int one=1,three=3,miss=65535;
82 g2int lensec3,iofst,ibeg,lencurr,len;
83 g2int i,j,temp,ilen,isecnum,nbits;
84 gtemplate *mapgrid=0;
85
86 ierr=0;
87 //
88 // Check to see if beginning of GRIB message exists
89 //
90 if ( cgrib[0]!=G || cgrib[1]!=R || cgrib[2]!=I || cgrib[3]!=B ) {
91 printf("g2_addgrid: GRIB not found in given message.\n");
92 printf("g2_addgrid: Call to routine gribcreate required to initialize GRIB message.\n");
93 ierr=-1;
94 return(ierr);
95 }
96 //
97 // Get current length of GRIB message
98 //
99 gbit(cgrib,&lencurr,96,32);
100 //
101 // Check to see if GRIB message is already complete
102 //
103 if ( cgrib[lencurr-4]==seven && cgrib[lencurr-3]==seven &&
104 cgrib[lencurr-2]==seven && cgrib[lencurr-1]==seven ) {
105 printf("g2_addgrid: GRIB message already complete. Cannot add new section.\n");
106 ierr=-2;
107 return(ierr);
108 }
109 //
110 // Loop through all current sections of the GRIB message to
111 // find the last section number.
112 //
113 len=16; // length of Section 0
114 for (;;) {
115 // Get section number and length of next section
116 iofst=len*8;
117 gbit(cgrib,&ilen,iofst,32);
118 iofst=iofst+32;
119 gbit(cgrib,&isecnum,iofst,8);
120 len=len+ilen;
121 // Exit loop if last section reached
122 if ( len == lencurr ) break;
123 // If byte count for each section doesn't match current
124 // total length, then there is a problem.
125 if ( len > lencurr ) {
126 printf("g2_addgrid: Section byte counts don''t add to total.\n");
127 printf("g2_addgrid: Sum of section byte counts = %d\n",len);
128 printf("g2_addgrid: Total byte count in Section 0 = %d\n",lencurr);
129 ierr=-3;
130 return(ierr);
131 }
132 }
133 //
134 // Section 3 can only be added after sections 1, 2 and 7.
135 //
136 if ( (isecnum!=1) && (isecnum!=2) && (isecnum!=7) ) {
137 printf("g2_addgrid: Section 3 can only be added after Section 1, 2 or 7.\n");
138 printf("g2_addgrid: Section ',isecnum,' was the last found in given GRIB message.\n");
139 ierr=-4;
140 return(ierr);
141 }
142 //
143 // Add Section 3 - Grid Definition Section
144 //
145 ibeg=lencurr*8; // Calculate offset for beginning of section 3
146 iofst=ibeg+32; // leave space for length of section
147 sbit(cgrib,&three,iofst,8); // Store section number ( 3 )
148 iofst=iofst+8;
149 sbit(cgrib,igds+0,iofst,8); // Store source of Grid def.
150 iofst=iofst+8;
151 sbit(cgrib,igds+1,iofst,32); // Store number of data pts.
152 iofst=iofst+32;
153 sbit(cgrib,igds+2,iofst,8); // Store number of extra octets.
154 iofst=iofst+8;
155 sbit(cgrib,igds+3,iofst,8); // Store interp. of extra octets.
156 iofst=iofst+8;
157 // if Octet 6 is not equal to zero, Grid Definition Template may
158 // not be supplied.
159 if ( igds[0] == 0 )
160 sbit(cgrib,igds+4,iofst,16); // Store Grid Def Template num.
161 else
162 sbit(cgrib,&miss,iofst,16); // Store missing value as Grid Def Template num.
163 iofst=iofst+16;
164 //
165 // Get Grid Definition Template
166 //
167 if (igds[0] == 0) {
168 mapgrid=getgridtemplate(igds[4]);
169 if (mapgrid == 0) { // undefined template
170 ierr=-5;
171 return(ierr);
172 }
173 //
174 // Extend the Grid Definition Template, if necessary.
175 // The number of values in a specific template may vary
176 // depending on data specified in the "static" part of the
177 // template.
178 //
179 if ( mapgrid->needext ) {
180 free(mapgrid);
181 mapgrid=extgridtemplate(igds[4],igdstmpl);
182 }
183 }
184
185 /* Added by GDAL to avoid 'potential null pointer dereference [-Wnull-dereference]' on below mapgrid->maplen */
186 if (mapgrid == 0) { // undefined template
187 return(-5);
188 }
189
190 //
191 // Pack up each input value in array igdstmpl into the
192 // the appropriate number of octets, which are specified in
193 // corresponding entries in array mapgrid.
194 //
195 for (i=0;i<mapgrid->maplen;i++) {
196 nbits=abs(mapgrid->map[i])*8;
197 if ( (mapgrid->map[i] >= 0) || (igdstmpl[i] >= 0) )
198 sbit(cgrib,igdstmpl+i,iofst,nbits);
199 else {
200 sbit(cgrib,&one,iofst,1);
201 temp=abs(igdstmpl[i]);
202 sbit(cgrib,&temp,iofst+1,nbits-1);
203 }
204 iofst=iofst+nbits;
205 }
206 // Pack template extension, if appropriate
207 j=mapgrid->maplen;
208 if ( mapgrid->needext && (mapgrid->extlen > 0) ) {
209 for (i=0;i<mapgrid->extlen;i++) {
210 nbits=abs(mapgrid->ext[i])*8;
211 if ( (mapgrid->ext[i] >= 0) || (igdstmpl[j] >= 0) )
212 sbit(cgrib,igdstmpl+j,iofst,nbits);
213 else {
214 sbit(cgrib,&one,iofst,1);
215 temp=abs(igdstmpl[j]);
216 sbit(cgrib,&temp,iofst+1,nbits-1);
217 }
218 iofst=iofst+nbits;
219 j++;
220 }
221 }
222 free(mapgrid);
223 //
224 // If requested,
225 // Insert optional list of numbers defining number of points
226 // in each row or column. This is used for non regular
227 // grids.
228 //
229 if ( igds[2] != 0 ) {
230 nbits=igds[2]*8;
231 sbits(cgrib,ideflist,iofst,nbits,0,idefnum);
232 iofst=iofst+(nbits*idefnum);
233 }
234 //
235 // Calculate length of section 3 and store it in octets
236 // 1-4 of section 3.
237 //
238 lensec3=(iofst-ibeg)/8;
239 sbit(cgrib,&lensec3,ibeg,32);
240
241 //
242 // Update current byte total of message in Section 0
243 //
244 lencurr+=lensec3;
245 sbit(cgrib,&lencurr,96,32);
246
247 return(lencurr);
248
249 }