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
14 *****************************************************************************/
16 int findchar(char *line
, char thechar
);
21 Grib1_Tables grib_tables
;
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", ¢er
,&subcenter
,&parmtbl
,
33 fprintf(stderr
,"got parm_id: %d center: %d subcenter: %d parmtbl: %d\n",
34 parm_id
,center
,subcenter
,parmtbl
);
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
)
50 char line
[MAX_LINE_CHARS
];
53 int nxtidx
, elemidx
, charidx
;
54 char elems
[6][MAX_LINE_CHARS
];
57 /* Open parameter table file */
58 mapptr
= fopen(filename
, "r");
61 fprintf(stderr
,"Could not open %s\n",filename
);
66 /* Skip over comments at begining of gribmap file */
67 while (fgets(line
,500,mapptr
))
69 if (line
[0] != '#') break;
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");
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
));
91 * Read each line of parameter table, and store information in the
94 while (fgets(line
,MAX_LINE_CHARS
,mapptr
) != NULL
)
96 /* Split up the elements that are seperated by : */
99 while ((charidx
= findchar(line
+ nxtidx
,':')) >= 0)
101 strncpy(elems
[elemidx
],line
+ nxtidx
,charidx
);
102 elems
[elemidx
][charidx
] = '\0';
104 nxtidx
+= (charidx
+ 1);
107 parmidx
= atoi(elems
[0]);
110 * Check to see if this line specifies the next grib table. If so,
114 grib_tables
->num_tables
++;
116 grib_tables
->grib_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
)
124 "Could not re-allocate space for grib_table_info\n");
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
));
136 /* Assure that we have not gone beyond 256 entries! */
137 if (grib_tables
->grib_table_info
[tablenum
].num_entries
>= 256)
140 "Error: Invalid number of lines in table %d in, \n skipping line: %s \n",
145 /* Grab the last field */
146 strcpy(elems
[elemidx
],line
+ nxtidx
);
148 /* Split up comma-seperated field of wrf varnames */
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",
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
] =
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
]
176 "Error allocating space for wrf_param[%d], exiting\n",
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
]
189 "Error allocating space for wrf_param[%d][%d], exiting\n",
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';
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
]
213 "Error allocating space for wrf_param[%d][%d], exiting\n",
218 grib_tables
->grib_table_info
[tablenum
].wrf_param
[parmidx
][elemidx
][0]
220 grib_tables
->grib_table_info
[tablenum
].num_wrf_params
[parmidx
] = 0;
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
]
232 "Error allocating space for wrf_param[%d][%d], exiting\n",
238 strcpy(grib_tables
->grib_table_info
[tablenum
].wrf_param
[parmidx
][elemidx
],
240 grib_tables
->grib_table_info
[tablenum
].num_wrf_params
[parmidx
] =
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
++;
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
)
269 char varnametmp
[200];
273 strncpy(varnametmp
,varname
,strlen2
);
274 varnametmp
[strlen2
] = '\0';
276 for (tableidx
= 0; tableidx
< grib_tables
->num_tables
;tableidx
++)
281 idx
< grib_tables
->grib_table_info
[tableidx
].num_entries
;
286 grib_tables
->grib_table_info
[tableidx
].num_wrf_params
[idx
];
289 if (strcmp(varnametmp
,
290 grib_tables
->grib_table_info
[tableidx
].wrf_param
[idx
][prm_idx
])
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
;
308 /******************************************************************************
310 * free_gribmap_ - returns the size (in bytes) of a grib_table_info
313 *****************************************************************************/
315 int FREE_GRIBMAP(Grib1_Tables
*grib_tables
)
320 for (tablenum
= 0; tablenum
< grib_tables
->num_tables
; tablenum
++)
322 for (idx
= 0; idx
< grib_tables
->grib_table_info
[tablenum
].num_entries
;
326 idx2
< grib_tables
->grib_table_info
[tablenum
].num_wrf_params
[idx
];
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
);
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
;
352 for (charnum
= 0; charnum
< strlen(line
); charnum
++)
354 if (line
[charnum
] == thechar
)
363 /******************************************************************************
365 * get_grib1_table_info_size - returns the size (in bytes) of a grib_table_info
368 *****************************************************************************/
370 int GET_GRIB1_TABLE_INFO_SIZE (int *size
)
372 *size
= sizeof(Grib1_Table_Info
);
376 /******************************************************************************
378 * get_grib1_tables_size - returns the size (in bytes) of a grib_tables
381 *****************************************************************************/
383 int GET_GRIB1_TABLES_SIZE (int *size
)
385 *size
= sizeof(Grib1_Tables
);
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
);
409 /******************************************************************************
411 * get_grid_info_size_ - returns the size (in bytes) of a grib_tables
414 *****************************************************************************/
416 int GET_GRID_INFO_SIZE (int *size
)
418 *size
= sizeof(Grib1_Tables
);
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
;
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
=
444 malloc(grib_tables
->num_tables
*sizeof(Grib1_Table_Info
));
445 if (tmp
->grib_table_info
== NULL
)
448 "copy_grib_tables: Could not allocate space for grib_table_info. num_tables: %d\n",
449 grib_tables
->num_tables
);
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)
468 tmp
->grib_table_info
[tblidx
].wrf_param
[prmidx
] = (char **)
469 malloc(grib_tables
->grib_table_info
[tblidx
].num_wrf_params
[prmidx
]
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
]
478 elmidx
< grib_tables
->grib_table_info
[tblidx
].num_wrf_params
[prmidx
];
483 strlen(grib_tables
->grib_table_info
[tblidx
].wrf_param
[prmidx
][elmidx
]) + 1;
484 tmp
->grib_table_info
[tblidx
].wrf_param
[prmidx
][elmidx
] =
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));