updated top-level README and version_decl for V4.4.2 (#1795)
[WRF.git] / external / io_grib1 / gribmap.c
blob0ad686c2fc2329865a7ffe9abffcec62caf55c55
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "gribmap.h"
5 /******************************************************************************
7 * The functions in this file are used for opening/reading, and searching for
8 * information in a grib table.
10 * All functions return 0 for success and 1 for failure, unless otherwise
11 * noted.
12 *****************************************************************************/
14 int findchar(char *line, char thechar);
16 #ifdef TEST
17 int main()
19 Grib1_Tables grib_tables;
20 char filename[300];
21 int parm_id;
22 int ret;
23 int tablenum;
24 int center,subcenter,parmtbl;
26 strcpy(filename,"gribmap.txt");
27 LOAD_GRIB1_TABLES(filename, &grib_tables, &ret);
29 GET_GRIB_PARAM (&grib_tables, "TSK", &center,&subcenter,&parmtbl,
30 &tablenum, &parm_id);
31 fprintf(stderr,"got parm_id: %d center: %d subcenter: %d parmtbl: %d\n",
32 parm_id,center,subcenter,parmtbl);
35 #endif
37 /******************************************************************************
39 * read_gribmap - reads a gribmap file and puts the information into the
40 * grib_table_info structure.
42 ******************************************************************************/
44 int READ_GRIBMAP (char *filename, Grib1_Tables *grib_tables, int *ret)
47 FILE *mapptr;
48 char line[MAX_LINE_CHARS];
49 int dummy;
50 int parmidx;
51 int nxtidx, elemidx, charidx;
52 char elems[6][MAX_LINE_CHARS];
53 int tablenum;
55 /* Open parameter table file */
56 mapptr = fopen(filename, "r");
57 if (mapptr == NULL)
59 fprintf(stderr,"Could not open %s\n",filename);
60 *ret=1;
61 return 1;
64 /* Skip over comments at begining of gribmap file */
65 while (fgets(line,500,mapptr))
67 if (line[0] != '#') break;
70 tablenum = 0;
71 grib_tables->num_tables = 1;
72 grib_tables->grib_table_info =
73 (Grib1_Table_Info *)calloc(1,sizeof(Grib1_Table_Info));
75 if (grib_tables->grib_table_info == NULL)
77 fprintf(stderr,"Could not allocate space for grib_table_info\n");
78 *ret = 1;
79 return 1;
81 grib_tables->grib_table_info[tablenum].num_entries = 0;
83 sscanf(line,"%d:%d:%d:%d",&dummy,
84 &(grib_tables->grib_table_info[tablenum].center),
85 &(grib_tables->grib_table_info[tablenum].subcenter),
86 &(grib_tables->grib_table_info[tablenum].parmtbl));
88 /*
89 * Read each line of parameter table, and store information in the
90 * structure.
92 while (fgets(line,MAX_LINE_CHARS,mapptr) != NULL)
94 /* Split up the elements that are seperated by : */
95 nxtidx = 0;
96 elemidx = 0;
97 while ((charidx = findchar(line + nxtidx,':')) >= 0)
99 strncpy(elems[elemidx],line + nxtidx,charidx);
100 elems[elemidx][charidx] = '\0';
101 elemidx++;
102 nxtidx += (charidx + 1);
105 parmidx = atoi(elems[0]);
108 * Check to see if this line specifies the next grib table. If so,
109 * break out
111 if (parmidx == -1) {
112 grib_tables->num_tables++;
113 tablenum++;
114 grib_tables->grib_table_info =
115 (Grib1_Table_Info *)
116 realloc(grib_tables->grib_table_info,
117 grib_tables->num_tables*sizeof(Grib1_Table_Info));
119 if (grib_tables->grib_table_info == NULL)
121 fprintf(stderr,
122 "Could not re-allocate space for grib_table_info\n");
123 *ret = 1;
124 return 1;
126 grib_tables->grib_table_info[tablenum].num_entries = 0;
127 sscanf(line,"%d:%d:%d:%d",&dummy,
128 &(grib_tables->grib_table_info[tablenum].center),
129 &(grib_tables->grib_table_info[tablenum].subcenter),
130 &(grib_tables->grib_table_info[tablenum].parmtbl));
131 continue;
134 /* Assure that we have not gone beyond 256 entries! */
135 if (grib_tables->grib_table_info[tablenum].num_entries >= 256)
137 fprintf(stderr,
138 "Error: Invalid number of lines in table %d in, \n skipping line: %s \n",
139 tablenum,line);
140 break;
143 /* Grab the last field */
144 strcpy(elems[elemidx],line + nxtidx);
146 /* Split up comma-seperated field of wrf varnames */
147 nxtidx = 0;
148 elemidx = 0;
150 /* Allocate number of elements in wrf_param */
151 grib_tables->grib_table_info[tablenum].wrf_param[parmidx] =
152 (char **)malloc(1*sizeof(char *));
153 if (grib_tables->grib_table_info[tablenum].wrf_param[parmidx] == NULL)
155 fprintf(stderr, "Error allocating space for wrf_param[%d], exiting\n",
156 parmidx);
157 *ret = 1;
158 return 1;
161 while ((charidx = findchar(elems[3]+nxtidx,',')) >= 0)
164 /* Allocate number of elements in wrf_param */
165 grib_tables->grib_table_info[tablenum].wrf_param[parmidx] =
166 (char **)
167 realloc(grib_tables->grib_table_info[tablenum].wrf_param[parmidx],
168 (elemidx+2)*sizeof(char *));
169 if (grib_tables->grib_table_info[tablenum].wrf_param[parmidx]
170 == NULL)
172 perror("");
173 fprintf(stderr,
174 "Error allocating space for wrf_param[%d], exiting\n",
175 parmidx);
176 *ret = 1;
177 return 1;
180 grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx] =
181 (char *)malloc((charidx+2)*sizeof(char));
182 if (grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx]
183 == NULL)
185 perror("");
186 fprintf(stderr,
187 "Error allocating space for wrf_param[%d][%d], exiting\n",
188 parmidx,elemidx);
189 *ret = 1;
190 return 1;
193 strncpy(grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx],
194 elems[3]+nxtidx,charidx);
195 grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx][charidx] = '\0';
196 elemidx++;
197 nxtidx += (charidx + 1);
200 /* Grab the last field */
201 if (strlen(elems[3] + nxtidx) <= 0)
203 /* Case for no specified WRF fields */
204 grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx] =
205 (char *)malloc(1*sizeof(char));
206 if (grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx]
207 == NULL)
209 perror("");
210 fprintf(stderr,
211 "Error allocating space for wrf_param[%d][%d], exiting\n",
212 parmidx,elemidx);
213 *ret = 1;
214 return 1;
216 grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx][0]
217 = '\0';
218 grib_tables->grib_table_info[tablenum].num_wrf_params[parmidx] = 0;
220 else
222 /* Allocate space for last element */
223 grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx] =
224 (char *)malloc((strlen(elems[3] + nxtidx)+1)*sizeof(char));
225 if (grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx]
226 == NULL)
228 perror("");
229 fprintf(stderr,
230 "Error allocating space for wrf_param[%d][%d], exiting\n",
231 parmidx,elemidx);
232 *ret = 1;
233 return 1;
236 strcpy(grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx],
237 elems[3] + nxtidx);
238 grib_tables->grib_table_info[tablenum].num_wrf_params[parmidx] =
239 elemidx + 1;
242 grib_tables->grib_table_info[tablenum].parm_id[parmidx]=atoi(elems[0]);
243 grib_tables->grib_table_info[tablenum].dec_sc_factor[parmidx]=atoi(elems[4]);
245 grib_tables->grib_table_info[tablenum].num_entries++;
248 *ret=0;
249 return 0;
252 /******************************************************************************
254 * get_grib_param - searches through a grib_table_info structure and returns
255 * the index for the input "varname".
257 * returns index number, or, -1 for failure.
258 *****************************************************************************/
260 int GET_GRIB_PARAM (Grib1_Tables *grib_tables, char *varname, int *center,
261 int *subcenter, int *parmtbl, int *tablenum, int *index,
262 int strlen1, int strlen2)
264 int idx;
265 int prm_idx;
266 int tableidx;
267 char varnametmp[200];
269 *index = -1;
271 strncpy(varnametmp,varname,strlen2);
272 varnametmp[strlen2] = '\0';
273 trim(varnametmp);
274 for (tableidx = 0; tableidx < grib_tables->num_tables ;tableidx++)
278 for (idx = 0;
279 idx < grib_tables->grib_table_info[tableidx].num_entries;
280 idx++)
282 for (prm_idx = 0;
283 prm_idx <
284 grib_tables->grib_table_info[tableidx].num_wrf_params[idx];
285 prm_idx++)
287 if (strcmp(varnametmp,
288 grib_tables->grib_table_info[tableidx].wrf_param[idx][prm_idx])
289 == 0)
291 *center =
292 grib_tables->grib_table_info[tableidx].center;
293 *subcenter = grib_tables->grib_table_info[tableidx].subcenter;
294 *parmtbl = grib_tables->grib_table_info[tableidx].parmtbl;
295 *tablenum = tableidx;
296 *index = idx;
297 break;
303 return *index;
306 /******************************************************************************
308 * free_gribmap_ - returns the size (in bytes) of a grib_table_info
309 * structure.
311 *****************************************************************************/
313 int FREE_GRIBMAP(Grib1_Tables *grib_tables)
315 int idx, idx2;
316 int tablenum;
318 for (tablenum = 0; tablenum < grib_tables->num_tables; tablenum++)
320 for (idx = 0; idx < grib_tables->grib_table_info[tablenum].num_entries;
321 idx++)
323 for (idx2 = 0;
324 idx2 < grib_tables->grib_table_info[tablenum].num_wrf_params[idx];
325 idx2++)
327 free(grib_tables->grib_table_info[tablenum].wrf_param[idx][idx2]);
329 if (grib_tables->grib_table_info[tablenum].num_wrf_params[idx] > 0)
331 free(grib_tables->grib_table_info[tablenum].wrf_param[idx]);
335 free(grib_tables->grib_table_info);
336 return 0;
339 /******************************************************************************
341 * Return the character index of the first instance of "thechar" in a string.
343 ******************************************************************************/
345 int findchar(char *line, char thechar)
347 int returnidx, charnum;
349 returnidx = -1;
350 for (charnum = 0; charnum < strlen(line); charnum++)
352 if (line[charnum] == thechar)
354 returnidx = charnum;
355 break;
358 return returnidx;
361 /******************************************************************************
363 * get_grib1_table_info_size - returns the size (in bytes) of a grib_table_info
364 * structure.
366 *****************************************************************************/
368 int GET_GRIB1_TABLE_INFO_SIZE (int *size)
370 *size = sizeof(Grib1_Table_Info);
371 return *size;
374 /******************************************************************************
376 * get_grib1_tables_size - returns the size (in bytes) of a grib_tables
377 * structure.
379 *****************************************************************************/
381 int GET_GRIB1_TABLES_SIZE (int *size)
383 *size = sizeof(Grib1_Tables);
384 return *size;
387 /******************************************************************************
389 * load_grib1_table_info - reads a gribmap file and puts the information into
390 * the grib_table_info structure.
392 ******************************************************************************/
394 int LOAD_GRIB1_TABLES (char filename[],
395 Grib1_Tables *grib_tables, int *ret, int strlen1)
398 char tmpfilename[300];
399 strncpy(tmpfilename,filename,strlen1);
400 tmpfilename[strlen1] = '\0';
402 READ_GRIBMAP(tmpfilename, grib_tables, ret);
404 return *ret;
407 /******************************************************************************
409 * get_grid_info_size_ - returns the size (in bytes) of a grib_tables
410 * structure.
412 *****************************************************************************/
414 int GET_GRID_INFO_SIZE (int *size)
416 *size = sizeof(Grib1_Tables);
417 return *size;
421 /******************************************************************************
423 * copy_grib_tables - allocates and fills a grib_tables structure
425 *****************************************************************************/
427 Grib1_Tables *copy_grib_tables(Grib1_Tables *grib_tables)
429 int tblidx,prmidx,elmidx;
430 int strsiz;
432 Grib1_Tables *tmp;
434 tmp = (Grib1_Tables *)malloc(sizeof(Grib1_Tables));
436 memcpy(tmp,grib_tables,sizeof(Grib1_Tables));
438 /* Now do the grib_table_info elements within grib_tables */
440 tmp->grib_table_info =
441 (Grib1_Table_Info *)
442 malloc(grib_tables->num_tables*sizeof(Grib1_Table_Info));
443 if (tmp->grib_table_info == NULL)
445 fprintf(stderr,
446 "copy_grib_tables: Could not allocate space for grib_table_info. num_tables: %d\n",
447 grib_tables->num_tables);
448 exit(1);
451 memcpy(tmp->grib_table_info,
452 grib_tables->grib_table_info,
453 grib_tables->num_tables*sizeof(Grib1_Table_Info));
456 for (tblidx = 0; tblidx < grib_tables->num_tables; tblidx++)
459 for (prmidx = 0; prmidx < MAX_PARAMS; prmidx++)
461 if (grib_tables->grib_table_info[tblidx].num_wrf_params[prmidx] <= 0)
463 continue;
466 tmp->grib_table_info[tblidx].wrf_param[prmidx] = (char **)
467 malloc(grib_tables->grib_table_info[tblidx].num_wrf_params[prmidx]
468 * sizeof(char *));
470 memcpy(tmp->grib_table_info[tblidx].wrf_param[prmidx],
471 grib_tables->grib_table_info[tblidx].wrf_param[prmidx],
472 grib_tables->grib_table_info[tblidx].num_wrf_params[prmidx]
473 * sizeof(char *));
475 for (elmidx = 0;
476 elmidx < grib_tables->grib_table_info[tblidx].num_wrf_params[prmidx];
477 elmidx++)
480 strsiz =
481 strlen(grib_tables->grib_table_info[tblidx].wrf_param[prmidx][elmidx]) + 1;
482 tmp->grib_table_info[tblidx].wrf_param[prmidx][elmidx] =
483 (char *)
484 malloc(strsiz * sizeof(char));
486 memcpy(tmp->grib_table_info[tblidx].wrf_param[prmidx][elmidx],
487 grib_tables->grib_table_info[tblidx].wrf_param[prmidx][elmidx],
488 strsiz * sizeof(char));
496 return tmp;