1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include "grib_lookup.h" |
---|
5 | #include "dprints.h" /* for dprints */ |
---|
6 | #include "gribfuncs.h" /* prototypes */ |
---|
7 | |
---|
8 | CTR_DEFN db_ctr_tbl[NCTRS]; /* GLOBVARS */ |
---|
9 | |
---|
10 | /* |
---|
11 | *************************************************************************** |
---|
12 | * A. FUNCTION: ld_grib_origctrs |
---|
13 | * Load Originating Centers information from named file into |
---|
14 | * an array of structures of type CTR_DEFN. |
---|
15 | * |
---|
16 | * INTERFACE: |
---|
17 | * int ld_grib_origctrs (orig_ctr_fn, pathnm, errmsg) |
---|
18 | * |
---|
19 | * ARGUMENTS (I=input, O=output, I&O=input and output): |
---|
20 | * (I) char *orig_ctr_fn; name of file to load |
---|
21 | * (I) char *pathnm; path where input file resides |
---|
22 | * (O) char *errmsg; returned filled if error occurred |
---|
23 | * |
---|
24 | * RETURN CODE: |
---|
25 | * 0: no errors; db_ctrs_tbl array filled; |
---|
26 | * -1: bad, errmsg is filled; |
---|
27 | *************************************************************************** |
---|
28 | */ |
---|
29 | #if PROTOTYPE_NEEDED |
---|
30 | int ld_grib_origctrs ( char *orig_ctr_fn, char *pathnm, char *errmsg) |
---|
31 | #else |
---|
32 | int ld_grib_origctrs ( orig_ctr_fn, pathnm, errmsg) |
---|
33 | char *orig_ctr_fn; char *pathnm; char *errmsg; |
---|
34 | #endif |
---|
35 | { |
---|
36 | /* |
---|
37 | * |
---|
38 | * A.0 DEFAULT to bad Status; |
---|
39 | */ |
---|
40 | char *func="ld_grib_origctrs", *ptr; |
---|
41 | char strGribCode[200],Line[200], fn[200], mybuff[200]; |
---|
42 | int ftp_already=0, usGribCode, cnt=0, stat= -1; |
---|
43 | FILE *fLook; |
---|
44 | |
---|
45 | /* |
---|
46 | * |
---|
47 | * A.1 PREPARE name and path of orig ctr file |
---|
48 | * CLEAR out the lookup arrays |
---|
49 | */ |
---|
50 | DPRINT1 ("Entering %s\n", func); |
---|
51 | sprintf (fn, "%s/%s", pathnm, orig_ctr_fn); |
---|
52 | DPRINT1 ("Try to load= %s\n", fn); |
---|
53 | memset ((void*)db_ctr_tbl, '\0', NCTRS * sizeof(CTR_DEFN)); |
---|
54 | |
---|
55 | /* |
---|
56 | * |
---|
57 | * A.2 IF (unable to open OrigCtr file for reading) THEN |
---|
58 | * RETURN with error status; |
---|
59 | * ENDIF |
---|
60 | */ |
---|
61 | if ( (fLook= fopen(fn,"r")) == NULL) |
---|
62 | { |
---|
63 | sprintf(errmsg,"%s: failed to load '%s';\n", func, orig_ctr_fn); |
---|
64 | goto BYE; /* return with error status */ |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | /* Now, read: *** ORIG_CTRS **** |
---|
69 | Sample: |
---|
70 | |
---|
71 | GRIB Table 0 - Originating Center Definitions (Octet 5 of PDS) |
---|
72 | Code Figure Model Name |
---|
73 | =========== ========== |
---|
74 | 007 US Weather Service - National Meteorological Center (NMC) |
---|
75 | 057 US Air Force - Air Force Global Weather Central |
---|
76 | 058 Fleet Numerical Meteorology and Oceanography Center (FNMOC) |
---|
77 | 059 NOAA Forecast Systems Laboratory (FSL) |
---|
78 | 097 European Space Agency (ESA) |
---|
79 | 098 European Centre for Medium Range Weather Forecasts (ECMWF) |
---|
80 | 128 Naval Research Laboratory (NRL) Monterey, CA |
---|
81 | 129 Center for Air/Sea Technology (CAST) |
---|
82 | */ |
---|
83 | |
---|
84 | /* |
---|
85 | * |
---|
86 | * A.3 SKIP over the comments lines |
---|
87 | */ |
---|
88 | /* Read until last of Header line */ |
---|
89 | for (Line[0]='\0'; ! strstr(Line,"====") ; ) |
---|
90 | { |
---|
91 | fgets(Line, sizeof(Line), fLook); |
---|
92 | if (feof(fLook) || ferror(fLook)) |
---|
93 | { sprintf(errmsg, |
---|
94 | "%s: got EOF/ERROR skipping over Header lines in %s\n", func,fn); |
---|
95 | goto BYE; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /* |
---|
100 | * A.4 WHILE (not end of file yet) |
---|
101 | */ |
---|
102 | cnt=0; |
---|
103 | while (!feof(fLook) && !ferror(fLook)) |
---|
104 | { |
---|
105 | /* |
---|
106 | * READ a line !stop if fails |
---|
107 | * SKIP line if it doesn't have 2 args or ctr_id out of range |
---|
108 | * STORE center info into db_ctr_tbl array, cell #usGribCode; |
---|
109 | */ |
---|
110 | if (fgets(Line, sizeof(Line), fLook) == NULL) break; |
---|
111 | |
---|
112 | /* skip additional comments, |
---|
113 | replace tabs with spaces, newlines with null |
---|
114 | */ |
---|
115 | if (Line[0]=='#') continue; |
---|
116 | while (ptr=strchr(Line,'\t')) *ptr=' '; |
---|
117 | if (ptr=strchr(Line,'\n')) *ptr='\0'; |
---|
118 | |
---|
119 | if (sscanf(Line,"%s%s",strGribCode, mybuff) != 2) continue; |
---|
120 | |
---|
121 | /* Make sure Ctr_Id field has a Number */ |
---|
122 | if (strspn (strGribCode, "0123456789") != strlen(strGribCode)) { |
---|
123 | sprintf(errmsg,"%s: Invalid Ctr_id '%s', LINE=\n%s\n", |
---|
124 | func,strGribCode, Line); |
---|
125 | goto BYE; } |
---|
126 | |
---|
127 | usGribCode = (unsigned short) atoi(strGribCode); |
---|
128 | if (usGribCode<0 || usGribCode>= NOCTR) continue; |
---|
129 | |
---|
130 | /* copy over to Neon Tbl, descr has more than 1 words */ |
---|
131 | strncpy(db_ctr_tbl[usGribCode].ctr_dsc, |
---|
132 | strstr(Line, mybuff), |
---|
133 | sizeof(db_ctr_tbl[usGribCode].ctr_dsc) -1); |
---|
134 | ++cnt; |
---|
135 | DPRINT2 ("(+) ctr_id=%d, descr=%s\n", usGribCode, |
---|
136 | db_ctr_tbl[usGribCode].ctr_dsc); |
---|
137 | /* |
---|
138 | * ENDWHILE !read entries |
---|
139 | */ |
---|
140 | } |
---|
141 | |
---|
142 | /* |
---|
143 | * |
---|
144 | * A.5 SET Status to no errors |
---|
145 | */ |
---|
146 | DPRINT1 ("File 'orig_ctrs' has %d entries\n", cnt); |
---|
147 | stat = 0; |
---|
148 | |
---|
149 | BYE: |
---|
150 | /* |
---|
151 | * |
---|
152 | * A.6 CLOSE "orig_ctrs" if file is opened |
---|
153 | * |
---|
154 | * A.7 RETURN with status |
---|
155 | */ |
---|
156 | if (fLook) close(fLook); |
---|
157 | DPRINT2 ("Exiting %s, Stat=%d\n", func, stat); |
---|
158 | return (stat); |
---|
159 | /* |
---|
160 | * |
---|
161 | * END OF FUNCTION |
---|
162 | * |
---|
163 | */ |
---|
164 | } |
---|