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
12 *****************************************************************************/
14 int findchar(char *line
, char thechar
);
19 Grib1_Tables grib_tables
;
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", ¢er
,&subcenter
,&parmtbl
,
31 fprintf(stderr
,"got parm_id: %d center: %d subcenter: %d parmtbl: %d\n",
32 parm_id
,center
,subcenter
,parmtbl
);
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
)
48 char line
[MAX_LINE_CHARS
];
51 int nxtidx
, elemidx
, charidx
;
52 char elems
[6][MAX_LINE_CHARS
];
55 /* Open parameter table file */
56 mapptr
= fopen(filename
, "r");
59 fprintf(stderr
,"Could not open %s\n",filename
);
64 /* Skip over comments at begining of gribmap file */
65 while (fgets(line
,500,mapptr
))
67 if (line
[0] != '#') break;
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");
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
));
89 * Read each line of parameter table, and store information in the
92 while (fgets(line
,MAX_LINE_CHARS
,mapptr
) != NULL
)
94 /* Split up the elements that are seperated by : */
97 while ((charidx
= findchar(line
+ nxtidx
,':')) >= 0)
99 strncpy(elems
[elemidx
],line
+ nxtidx
,charidx
);
100 elems
[elemidx
][charidx
] = '\0';
102 nxtidx
+= (charidx
+ 1);
105 parmidx
= atoi(elems
[0]);
108 * Check to see if this line specifies the next grib table. If so,
112 grib_tables
->num_tables
++;
114 grib_tables
->grib_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
)
122 "Could not re-allocate space for grib_table_info\n");
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
));
134 /* Assure that we have not gone beyond 256 entries! */
135 if (grib_tables
->grib_table_info
[tablenum
].num_entries
>= 256)
138 "Error: Invalid number of lines in table %d in, \n skipping line: %s \n",
143 /* Grab the last field */
144 strcpy(elems
[elemidx
],line
+ nxtidx
);
146 /* Split up comma-seperated field of wrf varnames */
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",
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
] =
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
]
174 "Error allocating space for wrf_param[%d], exiting\n",
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
]
187 "Error allocating space for wrf_param[%d][%d], exiting\n",
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';
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
]
211 "Error allocating space for wrf_param[%d][%d], exiting\n",
216 grib_tables
->grib_table_info
[tablenum
].wrf_param
[parmidx
][elemidx
][0]
218 grib_tables
->grib_table_info
[tablenum
].num_wrf_params
[parmidx
] = 0;
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
]
230 "Error allocating space for wrf_param[%d][%d], exiting\n",
236 strcpy(grib_tables
->grib_table_info
[tablenum
].wrf_param
[parmidx
][elemidx
],
238 grib_tables
->grib_table_info
[tablenum
].num_wrf_params
[parmidx
] =
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
++;
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
)
267 char varnametmp
[200];
271 strncpy(varnametmp
,varname
,strlen2
);
272 varnametmp
[strlen2
] = '\0';
274 for (tableidx
= 0; tableidx
< grib_tables
->num_tables
;tableidx
++)
279 idx
< grib_tables
->grib_table_info
[tableidx
].num_entries
;
284 grib_tables
->grib_table_info
[tableidx
].num_wrf_params
[idx
];
287 if (strcmp(varnametmp
,
288 grib_tables
->grib_table_info
[tableidx
].wrf_param
[idx
][prm_idx
])
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
;
306 /******************************************************************************
308 * free_gribmap_ - returns the size (in bytes) of a grib_table_info
311 *****************************************************************************/
313 int FREE_GRIBMAP(Grib1_Tables
*grib_tables
)
318 for (tablenum
= 0; tablenum
< grib_tables
->num_tables
; tablenum
++)
320 for (idx
= 0; idx
< grib_tables
->grib_table_info
[tablenum
].num_entries
;
324 idx2
< grib_tables
->grib_table_info
[tablenum
].num_wrf_params
[idx
];
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
);
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
;
350 for (charnum
= 0; charnum
< strlen(line
); charnum
++)
352 if (line
[charnum
] == thechar
)
361 /******************************************************************************
363 * get_grib1_table_info_size - returns the size (in bytes) of a grib_table_info
366 *****************************************************************************/
368 int GET_GRIB1_TABLE_INFO_SIZE (int *size
)
370 *size
= sizeof(Grib1_Table_Info
);
374 /******************************************************************************
376 * get_grib1_tables_size - returns the size (in bytes) of a grib_tables
379 *****************************************************************************/
381 int GET_GRIB1_TABLES_SIZE (int *size
)
383 *size
= sizeof(Grib1_Tables
);
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
);
407 /******************************************************************************
409 * get_grid_info_size_ - returns the size (in bytes) of a grib_tables
412 *****************************************************************************/
414 int GET_GRID_INFO_SIZE (int *size
)
416 *size
= sizeof(Grib1_Tables
);
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
;
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
=
442 malloc(grib_tables
->num_tables
*sizeof(Grib1_Table_Info
));
443 if (tmp
->grib_table_info
== NULL
)
446 "copy_grib_tables: Could not allocate space for grib_table_info. num_tables: %d\n",
447 grib_tables
->num_tables
);
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)
466 tmp
->grib_table_info
[tblidx
].wrf_param
[prmidx
] = (char **)
467 malloc(grib_tables
->grib_table_info
[tblidx
].num_wrf_params
[prmidx
]
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
]
476 elmidx
< grib_tables
->grib_table_info
[tblidx
].num_wrf_params
[prmidx
];
481 strlen(grib_tables
->grib_table_info
[tblidx
].wrf_param
[prmidx
][elmidx
]) + 1;
482 tmp
->grib_table_info
[tblidx
].wrf_param
[prmidx
][elmidx
] =
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));