Update version info for release v4.6.1 (#2122)
[WRF.git] / external / io_grib1 / gribmap.c
blobbfd0aa30dfb9d021fec3229962199e2a4fff714b
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "gribmap.h"
5 #include "trim.h"
7 /******************************************************************************
9 * The functions in this file are used for opening/reading, and searching for
10 * information in a grib table.
12 * All functions return 0 for success and 1 for failure, unless otherwise
13 * noted.
14 *****************************************************************************/
16 int findchar(char *line, char thechar);
18 #ifdef TEST
19 int main()
21 Grib1_Tables grib_tables;
22 char filename[300];
23 int parm_id;
24 int ret;
25 int tablenum;
26 int center,subcenter,parmtbl;
28 strcpy(filename,"gribmap.txt");
29 LOAD_GRIB1_TABLES(filename, &grib_tables, &ret);
31 GET_GRIB_PARAM (&grib_tables, "TSK", &center,&subcenter,&parmtbl,
32 &tablenum, &parm_id);
33 fprintf(stderr,"got parm_id: %d center: %d subcenter: %d parmtbl: %d\n",
34 parm_id,center,subcenter,parmtbl);
37 #endif
39 /******************************************************************************
41 * read_gribmap - reads a gribmap file and puts the information into the
42 * grib_table_info structure.
44 ******************************************************************************/
46 int READ_GRIBMAP (char *filename, Grib1_Tables *grib_tables, int *ret)
49 FILE *mapptr;
50 char line[MAX_LINE_CHARS];
51 int dummy;
52 int parmidx;
53 int nxtidx, elemidx, charidx;
54 char elems[6][MAX_LINE_CHARS];
55 int tablenum;
57 /* Open parameter table file */
58 mapptr = fopen(filename, "r");
59 if (mapptr == NULL)
61 fprintf(stderr,"Could not open %s\n",filename);
62 *ret=1;
63 return 1;
66 /* Skip over comments at begining of gribmap file */
67 while (fgets(line,500,mapptr))
69 if (line[0] != '#') break;
72 tablenum = 0;
73 grib_tables->num_tables = 1;
74 grib_tables->grib_table_info =
75 (Grib1_Table_Info *)calloc(1,sizeof(Grib1_Table_Info));
77 if (grib_tables->grib_table_info == NULL)
79 fprintf(stderr,"Could not allocate space for grib_table_info\n");
80 *ret = 1;
81 return 1;
83 grib_tables->grib_table_info[tablenum].num_entries = 0;
85 sscanf(line,"%d:%d:%d:%d",&dummy,
86 &(grib_tables->grib_table_info[tablenum].center),
87 &(grib_tables->grib_table_info[tablenum].subcenter),
88 &(grib_tables->grib_table_info[tablenum].parmtbl));
90 /*
91 * Read each line of parameter table, and store information in the
92 * structure.
94 while (fgets(line,MAX_LINE_CHARS,mapptr) != NULL)
96 /* Split up the elements that are seperated by : */
97 nxtidx = 0;
98 elemidx = 0;
99 while ((charidx = findchar(line + nxtidx,':')) >= 0)
101 strncpy(elems[elemidx],line + nxtidx,charidx);
102 elems[elemidx][charidx] = '\0';
103 elemidx++;
104 nxtidx += (charidx + 1);
107 parmidx = atoi(elems[0]);
110 * Check to see if this line specifies the next grib table. If so,
111 * break out
113 if (parmidx == -1) {
114 grib_tables->num_tables++;
115 tablenum++;
116 grib_tables->grib_table_info =
117 (Grib1_Table_Info *)
118 realloc(grib_tables->grib_table_info,
119 grib_tables->num_tables*sizeof(Grib1_Table_Info));
121 if (grib_tables->grib_table_info == NULL)
123 fprintf(stderr,
124 "Could not re-allocate space for grib_table_info\n");
125 *ret = 1;
126 return 1;
128 grib_tables->grib_table_info[tablenum].num_entries = 0;
129 sscanf(line,"%d:%d:%d:%d",&dummy,
130 &(grib_tables->grib_table_info[tablenum].center),
131 &(grib_tables->grib_table_info[tablenum].subcenter),
132 &(grib_tables->grib_table_info[tablenum].parmtbl));
133 continue;
136 /* Assure that we have not gone beyond 256 entries! */
137 if (grib_tables->grib_table_info[tablenum].num_entries >= 256)
139 fprintf(stderr,
140 "Error: Invalid number of lines in table %d in, \n skipping line: %s \n",
141 tablenum,line);
142 break;
145 /* Grab the last field */
146 strcpy(elems[elemidx],line + nxtidx);
148 /* Split up comma-seperated field of wrf varnames */
149 nxtidx = 0;
150 elemidx = 0;
152 /* Allocate number of elements in wrf_param */
153 grib_tables->grib_table_info[tablenum].wrf_param[parmidx] =
154 (char **)malloc(1*sizeof(char *));
155 if (grib_tables->grib_table_info[tablenum].wrf_param[parmidx] == NULL)
157 fprintf(stderr, "Error allocating space for wrf_param[%d], exiting\n",
158 parmidx);
159 *ret = 1;
160 return 1;
163 while ((charidx = findchar(elems[3]+nxtidx,',')) >= 0)
166 /* Allocate number of elements in wrf_param */
167 grib_tables->grib_table_info[tablenum].wrf_param[parmidx] =
168 (char **)
169 realloc(grib_tables->grib_table_info[tablenum].wrf_param[parmidx],
170 (elemidx+2)*sizeof(char *));
171 if (grib_tables->grib_table_info[tablenum].wrf_param[parmidx]
172 == NULL)
174 perror("");
175 fprintf(stderr,
176 "Error allocating space for wrf_param[%d], exiting\n",
177 parmidx);
178 *ret = 1;
179 return 1;
182 grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx] =
183 (char *)malloc((charidx+2)*sizeof(char));
184 if (grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx]
185 == NULL)
187 perror("");
188 fprintf(stderr,
189 "Error allocating space for wrf_param[%d][%d], exiting\n",
190 parmidx,elemidx);
191 *ret = 1;
192 return 1;
195 strncpy(grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx],
196 elems[3]+nxtidx,charidx);
197 grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx][charidx] = '\0';
198 elemidx++;
199 nxtidx += (charidx + 1);
202 /* Grab the last field */
203 if (strlen(elems[3] + nxtidx) <= 0)
205 /* Case for no specified WRF fields */
206 grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx] =
207 (char *)malloc(1*sizeof(char));
208 if (grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx]
209 == NULL)
211 perror("");
212 fprintf(stderr,
213 "Error allocating space for wrf_param[%d][%d], exiting\n",
214 parmidx,elemidx);
215 *ret = 1;
216 return 1;
218 grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx][0]
219 = '\0';
220 grib_tables->grib_table_info[tablenum].num_wrf_params[parmidx] = 0;
222 else
224 /* Allocate space for last element */
225 grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx] =
226 (char *)malloc((strlen(elems[3] + nxtidx)+1)*sizeof(char));
227 if (grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx]
228 == NULL)
230 perror("");
231 fprintf(stderr,
232 "Error allocating space for wrf_param[%d][%d], exiting\n",
233 parmidx,elemidx);
234 *ret = 1;
235 return 1;
238 strcpy(grib_tables->grib_table_info[tablenum].wrf_param[parmidx][elemidx],
239 elems[3] + nxtidx);
240 grib_tables->grib_table_info[tablenum].num_wrf_params[parmidx] =
241 elemidx + 1;
244 grib_tables->grib_table_info[tablenum].parm_id[parmidx]=atoi(elems[0]);
245 grib_tables->grib_table_info[tablenum].dec_sc_factor[parmidx]=atoi(elems[4]);
247 grib_tables->grib_table_info[tablenum].num_entries++;
250 *ret=0;
251 return 0;
254 /******************************************************************************
256 * get_grib_param - searches through a grib_table_info structure and returns
257 * the index for the input "varname".
259 * returns index number, or, -1 for failure.
260 *****************************************************************************/
262 int GET_GRIB_PARAM (Grib1_Tables *grib_tables, char *varname, int *center,
263 int *subcenter, int *parmtbl, int *tablenum, int *index,
264 int strlen1, int strlen2)
266 int idx;
267 int prm_idx;
268 int tableidx;
269 char varnametmp[200];
271 *index = -1;
273 strncpy(varnametmp,varname,strlen2);
274 varnametmp[strlen2] = '\0';
275 trim(varnametmp);
276 for (tableidx = 0; tableidx < grib_tables->num_tables ;tableidx++)
280 for (idx = 0;
281 idx < grib_tables->grib_table_info[tableidx].num_entries;
282 idx++)
284 for (prm_idx = 0;
285 prm_idx <
286 grib_tables->grib_table_info[tableidx].num_wrf_params[idx];
287 prm_idx++)
289 if (strcmp(varnametmp,
290 grib_tables->grib_table_info[tableidx].wrf_param[idx][prm_idx])
291 == 0)
293 *center =
294 grib_tables->grib_table_info[tableidx].center;
295 *subcenter = grib_tables->grib_table_info[tableidx].subcenter;
296 *parmtbl = grib_tables->grib_table_info[tableidx].parmtbl;
297 *tablenum = tableidx;
298 *index = idx;
299 break;
305 return *index;
308 /******************************************************************************
310 * free_gribmap_ - returns the size (in bytes) of a grib_table_info
311 * structure.
313 *****************************************************************************/
315 int FREE_GRIBMAP(Grib1_Tables *grib_tables)
317 int idx, idx2;
318 int tablenum;
320 for (tablenum = 0; tablenum < grib_tables->num_tables; tablenum++)
322 for (idx = 0; idx < grib_tables->grib_table_info[tablenum].num_entries;
323 idx++)
325 for (idx2 = 0;
326 idx2 < grib_tables->grib_table_info[tablenum].num_wrf_params[idx];
327 idx2++)
329 free(grib_tables->grib_table_info[tablenum].wrf_param[idx][idx2]);
331 if (grib_tables->grib_table_info[tablenum].num_wrf_params[idx] > 0)
333 free(grib_tables->grib_table_info[tablenum].wrf_param[idx]);
337 free(grib_tables->grib_table_info);
338 return 0;
341 /******************************************************************************
343 * Return the character index of the first instance of "thechar" in a string.
345 ******************************************************************************/
347 int findchar(char *line, char thechar)
349 int returnidx, charnum;
351 returnidx = -1;
352 for (charnum = 0; charnum < strlen(line); charnum++)
354 if (line[charnum] == thechar)
356 returnidx = charnum;
357 break;
360 return returnidx;
363 /******************************************************************************
365 * get_grib1_table_info_size - returns the size (in bytes) of a grib_table_info
366 * structure.
368 *****************************************************************************/
370 int GET_GRIB1_TABLE_INFO_SIZE (int *size)
372 *size = sizeof(Grib1_Table_Info);
373 return *size;
376 /******************************************************************************
378 * get_grib1_tables_size - returns the size (in bytes) of a grib_tables
379 * structure.
381 *****************************************************************************/
383 int GET_GRIB1_TABLES_SIZE (int *size)
385 *size = sizeof(Grib1_Tables);
386 return *size;
389 /******************************************************************************
391 * load_grib1_table_info - reads a gribmap file and puts the information into
392 * the grib_table_info structure.
394 ******************************************************************************/
396 int LOAD_GRIB1_TABLES (char filename[],
397 Grib1_Tables *grib_tables, int *ret, int strlen1)
400 char tmpfilename[300];
401 strncpy(tmpfilename,filename,strlen1);
402 tmpfilename[strlen1] = '\0';
404 READ_GRIBMAP(tmpfilename, grib_tables, ret);
406 return *ret;
409 /******************************************************************************
411 * get_grid_info_size_ - returns the size (in bytes) of a grib_tables
412 * structure.
414 *****************************************************************************/
416 int GET_GRID_INFO_SIZE (int *size)
418 *size = sizeof(Grib1_Tables);
419 return *size;
423 /******************************************************************************
425 * copy_grib_tables - allocates and fills a grib_tables structure
427 *****************************************************************************/
429 Grib1_Tables *copy_grib_tables(Grib1_Tables *grib_tables)
431 int tblidx,prmidx,elmidx;
432 int strsiz;
434 Grib1_Tables *tmp;
436 tmp = (Grib1_Tables *)malloc(sizeof(Grib1_Tables));
438 memcpy(tmp,grib_tables,sizeof(Grib1_Tables));
440 /* Now do the grib_table_info elements within grib_tables */
442 tmp->grib_table_info =
443 (Grib1_Table_Info *)
444 malloc(grib_tables->num_tables*sizeof(Grib1_Table_Info));
445 if (tmp->grib_table_info == NULL)
447 fprintf(stderr,
448 "copy_grib_tables: Could not allocate space for grib_table_info. num_tables: %d\n",
449 grib_tables->num_tables);
450 exit(1);
453 memcpy(tmp->grib_table_info,
454 grib_tables->grib_table_info,
455 grib_tables->num_tables*sizeof(Grib1_Table_Info));
458 for (tblidx = 0; tblidx < grib_tables->num_tables; tblidx++)
461 for (prmidx = 0; prmidx < MAX_PARAMS; prmidx++)
463 if (grib_tables->grib_table_info[tblidx].num_wrf_params[prmidx] <= 0)
465 continue;
468 tmp->grib_table_info[tblidx].wrf_param[prmidx] = (char **)
469 malloc(grib_tables->grib_table_info[tblidx].num_wrf_params[prmidx]
470 * sizeof(char *));
472 memcpy(tmp->grib_table_info[tblidx].wrf_param[prmidx],
473 grib_tables->grib_table_info[tblidx].wrf_param[prmidx],
474 grib_tables->grib_table_info[tblidx].num_wrf_params[prmidx]
475 * sizeof(char *));
477 for (elmidx = 0;
478 elmidx < grib_tables->grib_table_info[tblidx].num_wrf_params[prmidx];
479 elmidx++)
482 strsiz =
483 strlen(grib_tables->grib_table_info[tblidx].wrf_param[prmidx][elmidx]) + 1;
484 tmp->grib_table_info[tblidx].wrf_param[prmidx][elmidx] =
485 (char *)
486 malloc(strsiz * sizeof(char));
488 memcpy(tmp->grib_table_info[tblidx].wrf_param[prmidx][elmidx],
489 grib_tables->grib_table_info[tblidx].wrf_param[prmidx][elmidx],
490 strsiz * sizeof(char));
498 return tmp;