updated top-level README and version_decl for V4.4.2 (#1795)
[WRF.git] / external / io_grib1 / WGRIB / gribtable.c
blob561465b0473dcc63f5ee502abfa04d900791ff12
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stddef.h>
4 #include <string.h>
5 #include "cnames.h"
7 #define START -1
9 static int user_center = 0, user_subcenter = 0, user_ptable = 0;
10 static enum {filled, not_found, not_checked, no_file, init} status = init;
12 struct ParmTable parm_table_user[256];
15 * sets up user parameter table
18 int setup_user_table(int center, int subcenter, int ptable) {
20 int i, j, c0, c1, c2;
21 static FILE *input;
22 static int file_open = 0;
23 char *filename, line[300];
25 if (status == init) {
26 for (i = 0; i < 256; i++) {
27 parm_table_user[i].name = parm_table_user[i].comment = NULL;
29 status = not_checked;
32 if (status == no_file) return 0;
34 if ((user_center == -1 || center == user_center) &&
35 (user_subcenter == -1 || subcenter == user_subcenter) &&
36 (user_ptable == -1 || ptable == user_ptable)) {
38 if (status == filled) return 1;
39 if (status == not_found) return 0;
42 /* open gribtab file if not open */
44 if (!file_open) {
45 filename = getenv("GRIBTAB");
46 if (filename == NULL) filename = getenv("gribtab");
47 if (filename == NULL) filename = "gribtab";
49 if ((input = fopen(filename,"r")) == NULL) {
50 status = no_file;
51 return 0;
53 file_open = 1;
55 else {
56 rewind(input);
59 user_center = center;
60 user_subcenter = subcenter;
61 user_ptable = ptable;
63 /* scan for center & subcenter and ptable */
64 for (;;) {
65 if (fgets(line, 299, input) == NULL) {
66 status = not_found;
67 return 0;
69 if (atoi(line) != START) continue;
70 i = sscanf(line,"%d:%d:%d:%d", &j, &center, &subcenter, &ptable);
71 if (i != 4) {
72 fprintf(stderr,"illegal gribtab center/subcenter/ptable line: %s\n", line);
73 continue;
75 if ((center == -1 || center == user_center) &&
76 (subcenter == -1 || subcenter == user_subcenter) &&
77 (ptable == -1 || ptable == user_ptable)) break;
80 user_center = center;
81 user_subcenter = subcenter;
82 user_ptable = ptable;
84 /* free any used memory */
85 if (parm_table_user[i].name != NULL) {
86 for (i = 0; i < 256; i++) {
87 free(parm_table_user[i].name);
88 free(parm_table_user[i].comment);
92 /* read definitions */
94 for (;;) {
95 if (fgets(line, 299, input) == NULL) break;
96 if ((i = atoi(line)) == START) break;
97 line[299] = 0;
99 /* find the colons and end-of-line */
100 for (c0 = 0; line[c0] != ':' && line[c0] != 0; c0++) ;
101 /* skip blank lines */
102 if (line[c0] == 0) continue;
104 for (c1 = c0 + 1; line[c1] != ':' && line[c1] != 0; c1++) ;
105 c2 = strlen(line);
106 if (line[c2-1] == '\n') line[--c2] = '\0';
107 if (c2 <= c1) {
108 fprintf(stderr,"illegal gribtab line:%s\n", line);
109 continue;
111 line[c0] = 0;
112 line[c1] = 0;
114 parm_table_user[i].name = (char *) malloc(c1 - c0);
115 parm_table_user[i].comment = (char *) malloc(c2 - c1);
116 strcpy(parm_table_user[i].name, line+c0+1);
117 strcpy(parm_table_user[i].comment, line+c1+1);
120 /* now to fill in undefined blanks */
121 for (i = 0; i < 255; i++) {
122 if (parm_table_user[i].name == NULL) {
123 parm_table_user[i].name = (char *) malloc(7);
124 sprintf(parm_table_user[i].name, "var%d", i);
125 parm_table_user[i].comment = (char *) malloc(strlen("undefined")+1);
126 strcpy(parm_table_user[i].comment, "undefined");
129 status = filled;
130 return 1;