1 | /* |
---|
2 | file: ld_enc_input.c (Ld_enc_config, Ld_enc_ieeeff, Ld_enc_ffinfo) |
---|
3 | Original version from previous Encoder Library; |
---|
4 | Revisions: |
---|
5 | 10/28/96 by Alice T. Nakajima, SAIC |
---|
6 | replaced hard-coded $CONFIG_PATH[]/input.dat, |
---|
7 | now passes in the name of the file (with absolute path) |
---|
8 | to ld as well as the Usr Input struct; |
---|
9 | */ |
---|
10 | #include <stdio.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include <string.h> |
---|
13 | #include "dprints.h" /* for dprints */ |
---|
14 | #include "gribfuncs.h" /* prototypes */ |
---|
15 | |
---|
16 | /* |
---|
17 | ********************************************************************* |
---|
18 | * A. FUNCTION: ld_enc_config |
---|
19 | * fill struct holding user's input from config_fn that |
---|
20 | * is passed in by user |
---|
21 | * |
---|
22 | * INTERFACE: |
---|
23 | * int ld_enc_config (config_fn, User_Input, errmsg) |
---|
24 | * |
---|
25 | * ARGUMENTS (I=input, O=output, I&O=input and output): |
---|
26 | * (I) char *config_fn; name of file to load from; |
---|
27 | * (O) USER_INPUT *User_Input; filled with data read from file; |
---|
28 | * (O) char *errmsg returned filled if error occurred; |
---|
29 | * |
---|
30 | * RETURN CODE: |
---|
31 | * 0> success, file is read and closed, user_input is filled |
---|
32 | * 1> error opening file; errmsg filled; |
---|
33 | * 2> failed to get all expected arguments; errmsg filled; |
---|
34 | * 3> ferror in file; errmsg filled; |
---|
35 | **********************************************************************/ |
---|
36 | |
---|
37 | #if PROTOTYPE_NEEDED |
---|
38 | int ld_enc_config (char *config_fn, USER_INPUT *User_Input, char *errmsg) |
---|
39 | #else |
---|
40 | int ld_enc_config (config_fn, User_Input, errmsg) |
---|
41 | char *config_fn; USER_INPUT *User_Input; char *errmsg; |
---|
42 | #endif |
---|
43 | { |
---|
44 | /* |
---|
45 | * |
---|
46 | * A.1 DEFAULT to error stat=1; |
---|
47 | */ |
---|
48 | char *func="ld_enc_config"; /* name of Func */ |
---|
49 | char temp[100], dummy[100], line[200], *p1; |
---|
50 | int stat= 1; |
---|
51 | int linenum=1; /* number of lines got from the files */ |
---|
52 | int num_expected= 9; /* expecting 9 args from file */ |
---|
53 | FILE *infile; /* user input file */ |
---|
54 | |
---|
55 | DPRINT1 ("Entering %s\n", func); |
---|
56 | /* |
---|
57 | * |
---|
58 | * A.2 OPEN Encoder Config file for reading |
---|
59 | */ |
---|
60 | infile = fopen(config_fn, "r"); |
---|
61 | if (infile==NULL) { |
---|
62 | DPRINT2 ("%s: Failed to open %s\n", func,config_fn); |
---|
63 | sprintf (errmsg,"%s: Failed to open %s\n", func,config_fn); |
---|
64 | goto BYE; |
---|
65 | } |
---|
66 | |
---|
67 | DPRINT2 ("loading %d args from file:\n'%s'\n", num_expected, config_fn); |
---|
68 | |
---|
69 | /* |
---|
70 | * |
---|
71 | * A.3 WHILE (still more lines AND no error yet) DO |
---|
72 | */ |
---|
73 | while (!feof(infile) && !ferror(infile)) |
---|
74 | { |
---|
75 | /* |
---|
76 | * A.3.1 GET a line from the file, quit loop if failed; |
---|
77 | * !format: value opt_comments |
---|
78 | * A.3.2 IF line is empty OR is a comment, Loop again; |
---|
79 | */ |
---|
80 | if (fgets (line, sizeof(line)-1, infile) == NULL) break; |
---|
81 | for (p1=line; *p1 && (iscntrl(*p1)|| isspace(*p1)); p1++); |
---|
82 | if (p1==NULL || *p1=='#') continue; |
---|
83 | /* |
---|
84 | * A.3.3 EXTRACT first non-space argument |
---|
85 | * IF (fails) QUIT; |
---|
86 | * ELSE convert argument into a number |
---|
87 | */ |
---|
88 | if (sscanf (line, "%s%s", temp, dummy) < 1) { |
---|
89 | DPRINT1 ( "%s: failed to extract 1 arg from line\n", func); |
---|
90 | sprintf (errmsg, "%s: failed to extract arg from line:\n%s", |
---|
91 | func, line); |
---|
92 | break; |
---|
93 | } |
---|
94 | |
---|
95 | DPRINT2(" case %d, data=%s: ", linenum, temp); |
---|
96 | |
---|
97 | /* |
---|
98 | * A.3.4 SWITCH (what line number we're on) |
---|
99 | * ... USER_INPUT info ... |
---|
100 | * line 1: fill User_Input->chCase_id |
---|
101 | * line 2: fill User_Input->usParm_tbl |
---|
102 | * line 3: fill User_Input->usSub_tbl, ->usZero (oct26) |
---|
103 | * line 4: fill User_Input->usCenter_id |
---|
104 | * line 5: fill User_Input->usCenter_sub |
---|
105 | * line 6: fill User_Input->usTrack_num |
---|
106 | * line 7: if 1, set (0x80) bit of User_Input->usGds_bms_id |
---|
107 | * line 8: if 1, set (0x40) bit of User_Input->usGds_bms_id |
---|
108 | * line 9: filll User_Input->usBit_pack_num |
---|
109 | * else : print skip line msg |
---|
110 | * ENDSWITCH |
---|
111 | */ |
---|
112 | |
---|
113 | switch (linenum++) { |
---|
114 | |
---|
115 | /* USER_INPUT info: */ |
---|
116 | case 1: User_Input->chCase_id= temp[0]; |
---|
117 | P_CHAR (User_Input->chCase_id); break; |
---|
118 | case 2: User_Input->usParm_tbl= (unsigned short)atoi(temp); |
---|
119 | P_USHORT (User_Input->usParm_tbl); break; |
---|
120 | case 3: User_Input->usSub_tbl= (unsigned short)atoi(temp); |
---|
121 | P_USHORT (User_Input->usSub_tbl ); break; |
---|
122 | case 4: User_Input->usCenter_id= (unsigned short)atoi(temp); |
---|
123 | P_USHORT (User_Input->usCenter_id ); break; |
---|
124 | case 5: User_Input->usCenter_sub= (unsigned short)atoi(temp); |
---|
125 | P_USHORT (User_Input->usCenter_sub ); break; |
---|
126 | case 6: User_Input->usTrack_num= (unsigned short)atoi(temp); |
---|
127 | P_USHORT (User_Input->usTrack_num ); break; |
---|
128 | case 7: if (atoi(temp)) |
---|
129 | User_Input->usGds_bms_id=0x80; |
---|
130 | P_USHORT (User_Input->usGds_bms_id ); break; |
---|
131 | case 8: if (atoi(temp)) |
---|
132 | User_Input->usGds_bms_id +=(unsigned short)0x40; |
---|
133 | P_USHORT (User_Input->usGds_bms_id ); break; |
---|
134 | case 9: User_Input->usBit_pack_num= (unsigned short)atoi(temp); |
---|
135 | P_USHORT (User_Input->usBit_pack_num ); break; |
---|
136 | default: fprintf(stdout, |
---|
137 | "%s Warning: excess line from Configfile skipped=\n%s\n", |
---|
138 | func, line); |
---|
139 | break; |
---|
140 | } |
---|
141 | /* |
---|
142 | * A.3 ENDWHILE !more to read |
---|
143 | */ |
---|
144 | } /* while */ |
---|
145 | |
---|
146 | /* |
---|
147 | * |
---|
148 | * A.4 IF (got a reading error) THEN |
---|
149 | * RETURN Stat 3 |
---|
150 | * ENDIF |
---|
151 | */ |
---|
152 | if (ferror(infile)) { |
---|
153 | DPRINT1 ( "%s: got ferror(infile)\n", func); |
---|
154 | sprintf(errmsg, "%s: got ferror(infile)\n", func); |
---|
155 | stat=3; |
---|
156 | } |
---|
157 | |
---|
158 | /* |
---|
159 | * |
---|
160 | * A.5 CLOSE the input file |
---|
161 | */ |
---|
162 | if (infile) fclose (infile); |
---|
163 | |
---|
164 | /* |
---|
165 | * |
---|
166 | * A.6 IF (only received less than #required arguments) THEN |
---|
167 | * PRINT warning |
---|
168 | * ELSE |
---|
169 | * CHANGE return status to no errors |
---|
170 | * ENDIF |
---|
171 | */ |
---|
172 | if (linenum-1 < num_expected) |
---|
173 | { |
---|
174 | DPRINT4 ("%s: failed to load %s (%d/%d)\n", |
---|
175 | func, config_fn, linenum-1, num_expected); |
---|
176 | sprintf(errmsg, "%s: failed to load %s (%d/%d)", |
---|
177 | func, config_fn, linenum-1, num_expected); |
---|
178 | stat= 2; |
---|
179 | } |
---|
180 | else stat = 0; |
---|
181 | |
---|
182 | /* |
---|
183 | * |
---|
184 | * A.7 RETURN with stat |
---|
185 | */ |
---|
186 | BYE: |
---|
187 | DPRINT2 ("Leaving %s, stat=%d;\n", func,stat); |
---|
188 | return (stat); |
---|
189 | /* |
---|
190 | * |
---|
191 | * END OF FUNCTION |
---|
192 | * |
---|
193 | * |
---|
194 | */ |
---|
195 | } |
---|
196 | |
---|
197 | /* |
---|
198 | ********************************************************************* |
---|
199 | * B. FUNCTION: ld_enc_ieeeff |
---|
200 | * load user's pre-malloced float array with data from |
---|
201 | * binary flat file passed in by user (ie: FF*); |
---|
202 | * |
---|
203 | * INTERFACE: |
---|
204 | * int ld_enc_ieeeff (ieee_fn, farr, elements, errmsg) |
---|
205 | * |
---|
206 | * ARGUMENTS (I=input, O=output, I&O=input and output): |
---|
207 | * (I) char *ieee_fn name of IEEE Flat file (w/fullpath) to read |
---|
208 | * (O) float *farr pre-malloced array to store data from read from file |
---|
209 | * (I) int elements; number of float elements to read from file; |
---|
210 | * (O) char *errmsg returned filled if error occurred; |
---|
211 | * |
---|
212 | * RETURN CODE: |
---|
213 | * 0> success, file is read and closed, float arr is filled |
---|
214 | * 1> error opening file; errmsg filled; |
---|
215 | * 2> failed to get all expected elements; errmsg filled; |
---|
216 | * 3> incoming float array is null; errmsg filled; |
---|
217 | **********************************************************************/ |
---|
218 | |
---|
219 | #if PROTOTYPE_NEEDED |
---|
220 | int ld_enc_ieeeff ( char *ieee_fn, |
---|
221 | float *farr, |
---|
222 | int elements, |
---|
223 | char *errmsg) |
---|
224 | #else |
---|
225 | int ld_enc_ieeeff ( ieee_fn, farr, elements, errmsg) |
---|
226 | char *ieee_fn; |
---|
227 | float *farr; |
---|
228 | int elements; |
---|
229 | char *errmsg; |
---|
230 | #endif |
---|
231 | { |
---|
232 | /* |
---|
233 | * |
---|
234 | * B.1 DEFAULT no error stat=0 |
---|
235 | */ |
---|
236 | char *func="ld_enc_ieeeff"; |
---|
237 | FILE *infile= 0; |
---|
238 | int stat= 0; |
---|
239 | |
---|
240 | |
---|
241 | DPRINT1 ("Entering %s\n",func); |
---|
242 | /* |
---|
243 | * |
---|
244 | * B.2 IF (float array is null) THEN |
---|
245 | * SET stat =3 |
---|
246 | * RETURN |
---|
247 | * ENDIF |
---|
248 | */ |
---|
249 | if (farr == NULL) { |
---|
250 | DPRINT1 ("%s: unexpected null Float array\n", func); |
---|
251 | sprintf (errmsg,"%s: unexpected null Float array\n", func); |
---|
252 | stat = 3; goto BYE; |
---|
253 | } |
---|
254 | /* |
---|
255 | * |
---|
256 | * B.3 OPEN the IEEE file for reading !was 'temp.dat' |
---|
257 | * IF (failed) RETURN err 1; |
---|
258 | */ |
---|
259 | infile = fopen(ieee_fn, "r"); |
---|
260 | if (infile==NULL) |
---|
261 | { DPRINT2 ("%s: Failed to open %s\n", func,ieee_fn); |
---|
262 | sprintf (errmsg,"%s: Failed to open %s\n", func,ieee_fn); |
---|
263 | stat = 1; goto BYE; |
---|
264 | } |
---|
265 | DPRINT1 ("Read %s\n", ieee_fn); |
---|
266 | |
---|
267 | /* |
---|
268 | * |
---|
269 | * B.4 READ float data from file |
---|
270 | * IF (didn't get all) THEN |
---|
271 | * SET status to 2 |
---|
272 | * RETURN |
---|
273 | * ENDIF |
---|
274 | */ |
---|
275 | if ( fread(farr, sizeof(float), elements, infile) != elements) |
---|
276 | { |
---|
277 | DPRINT3 ( "%s: failed to load %s (expecting %d float elements)\n", |
---|
278 | func, ieee_fn, elements); |
---|
279 | sprintf(errmsg,"%s: failed to load %s (expecting %d float elements)\n", |
---|
280 | func, ieee_fn, elements); |
---|
281 | stat= 2; |
---|
282 | } |
---|
283 | else DPRINT1 ("Number of float elements read = %d\n", elements); |
---|
284 | |
---|
285 | BYE: |
---|
286 | /* |
---|
287 | * |
---|
288 | * B.5 CLOSE the input file |
---|
289 | */ |
---|
290 | if (infile) fclose (infile); |
---|
291 | |
---|
292 | /* |
---|
293 | * |
---|
294 | * B.6 RETURN with stat |
---|
295 | */ |
---|
296 | DPRINT2 ("Leaving %s, stat=%d\n", func,stat); |
---|
297 | return (stat); |
---|
298 | /* |
---|
299 | * |
---|
300 | * END OF FUNCTION |
---|
301 | * |
---|
302 | * |
---|
303 | */ |
---|
304 | } |
---|
305 | |
---|
306 | /* |
---|
307 | ********************************************************************* |
---|
308 | * C. FUNCTION: ld_enc_ffinfo |
---|
309 | * fill DATA_INPUT struct from file whose name is passed in |
---|
310 | * |
---|
311 | * INTERFACE: |
---|
312 | * int ld_enc_ffinfo (ieee_info_fn, Data_Input, errmsg) |
---|
313 | * |
---|
314 | * ARGUMENTS (I=input, O=output, I&O=input and output): |
---|
315 | * (I) char *ieee_info_fn name of config info file to load |
---|
316 | * (O) DATA_INPUT *Data_Input to be filled with data read from config file |
---|
317 | * (O) char *errmsg filled if error occurred; |
---|
318 | * |
---|
319 | * RETURN CODE: |
---|
320 | * 0; success, file is read and closed, struct is filled |
---|
321 | * 1: error opening file; |
---|
322 | * 2: failed to get all expected arguments; |
---|
323 | * 3: Ferror; |
---|
324 | **********************************************************************/ |
---|
325 | |
---|
326 | #if PROTOTYPE_NEEDED |
---|
327 | int ld_enc_ffinfo ( char *ieee_info_fn, |
---|
328 | DATA_INPUT *Data_Input, |
---|
329 | char *errmsg) |
---|
330 | #else |
---|
331 | int ld_enc_ffinfo ( ieee_info_fn, Data_Input, errmsg) |
---|
332 | char *ieee_info_fn; |
---|
333 | DATA_INPUT *Data_Input; |
---|
334 | char *errmsg; |
---|
335 | #endif |
---|
336 | { |
---|
337 | /* |
---|
338 | * |
---|
339 | * C.1 DEFAULT to no error stat= 0; |
---|
340 | */ |
---|
341 | int stat= 0; |
---|
342 | FILE *infile; /* user input file */ |
---|
343 | char *func="ld_enc_ffinfo"; |
---|
344 | char temp[100], dummy[100], line[200], *p1; |
---|
345 | int linenum=1; /* number of lines got from the files */ |
---|
346 | int num_expected= 13; /* expecting 13 args from file */ |
---|
347 | int mdl_indx, i, iTemp; |
---|
348 | |
---|
349 | DPRINT1 ("Entering %s\n",func); |
---|
350 | |
---|
351 | /* |
---|
352 | * |
---|
353 | * C.2 OPEN input file for reading |
---|
354 | */ |
---|
355 | infile = fopen(ieee_info_fn, "r"); |
---|
356 | if (infile==NULL) { |
---|
357 | DPRINT2 ("%s: Failed to open %s\n", func,ieee_info_fn); |
---|
358 | sprintf (errmsg,"%s: Failed to open %s\n", func,ieee_info_fn); |
---|
359 | stat=1; goto BYE; |
---|
360 | } |
---|
361 | |
---|
362 | /* |
---|
363 | * |
---|
364 | * C.3 WHILE (still more lines AND no error yet) DO |
---|
365 | * C.3.1 GET a line from the file, quit loop if failed; |
---|
366 | * !format: value opt_comments |
---|
367 | * C.3.2 IF line is empty OR is a comment, Loop again; |
---|
368 | */ |
---|
369 | while (!feof(infile) && !ferror(infile) ) |
---|
370 | { |
---|
371 | if (fgets (line, sizeof(line)-1, infile) == NULL) break; |
---|
372 | for (p1=line; *p1 && (iscntrl(*p1)|| isspace(*p1)); p1++); |
---|
373 | if (p1==NULL || *p1=='#') continue; |
---|
374 | /* |
---|
375 | * C.3.3 EXTRACT arguments from line |
---|
376 | * IF (fails) QUIT; |
---|
377 | * ELSE convert the 2nd argument into an integer |
---|
378 | */ |
---|
379 | if ((i=sscanf (line, "%s", temp)) != 1) |
---|
380 | { |
---|
381 | DPRINT2( "%s: failed to extract 1 arg reading %s\n", |
---|
382 | func,ieee_info_fn); |
---|
383 | sprintf(errmsg, "%s: failed to extract 1 arg reading %s\n", |
---|
384 | func,ieee_info_fn); |
---|
385 | break; |
---|
386 | } |
---|
387 | else iTemp = atoi(temp); |
---|
388 | DPRINT2(" case %d, value=%d: ", linenum, iTemp); |
---|
389 | |
---|
390 | /* |
---|
391 | * C.3.4 SWITCH (what line number we're on) |
---|
392 | * line 1: fill Data_Input->usProc_id field; |
---|
393 | * line 2: fill Data_Input->usGrid_id field; |
---|
394 | * line 3: fill Data_Input->usParm_id field; |
---|
395 | * line 4: fill Data_Input->usParm_sub_id field; |
---|
396 | * line 5: fill Data_Input->usLevel_id field; |
---|
397 | * line 6: fill Data_Input->nLvl_1 field; |
---|
398 | * line 7: fill Data_Input->nLvl_2 field; |
---|
399 | * line 8: fill Data_Input->nYear field; |
---|
400 | * line 9: fill Data_Input->nMonth field; |
---|
401 | * line 10: fill Data_Input->nDay field; |
---|
402 | * line 11: fill Data_Input->nHour field |
---|
403 | * line 12: fill Data_Input->usFcst_per1 field; |
---|
404 | * line 13: fill Data_Input->nDec_sc_fctr field; |
---|
405 | * ENDSWITCH |
---|
406 | */ |
---|
407 | switch (linenum++) { |
---|
408 | case 1: Data_Input->usProc_id = (unsigned short) iTemp; |
---|
409 | P_USHORT (Data_Input->usProc_id); break; |
---|
410 | case 2: Data_Input->usGrid_id = (unsigned short) iTemp; |
---|
411 | P_USHORT (Data_Input->usGrid_id); break; |
---|
412 | case 3: Data_Input->usParm_id = (unsigned short) iTemp; |
---|
413 | P_USHORT (Data_Input->usParm_id); break; |
---|
414 | case 4: Data_Input->usParm_sub_id = (unsigned short) iTemp; |
---|
415 | P_USHORT (Data_Input->usParm_sub_id); break; |
---|
416 | case 5: Data_Input->usLevel_id = (unsigned short) iTemp; |
---|
417 | P_USHORT (Data_Input->usLevel_id); break; |
---|
418 | case 6: Data_Input->nLvl_1 = iTemp; |
---|
419 | P_INT (Data_Input->nLvl_1); break; |
---|
420 | case 7: Data_Input->nLvl_2 = iTemp; |
---|
421 | P_INT (Data_Input->nLvl_2); break; |
---|
422 | case 8: Data_Input->nYear = iTemp; |
---|
423 | P_INT (Data_Input->nYear); break; |
---|
424 | case 9: Data_Input->nMonth = iTemp; |
---|
425 | P_INT (Data_Input->nMonth); break; |
---|
426 | case 10: Data_Input->nDay = iTemp; |
---|
427 | P_INT (Data_Input->nDay); break; |
---|
428 | case 11: Data_Input->nHour = iTemp; |
---|
429 | P_INT (Data_Input->nHour); break; |
---|
430 | case 12: Data_Input->usFcst_per1 = (unsigned short) iTemp; |
---|
431 | P_USHORT (Data_Input->usFcst_per1); break; |
---|
432 | case 13: Data_Input->nDec_sc_fctr = iTemp; |
---|
433 | P_INT (Data_Input->nDec_sc_fctr); break; |
---|
434 | default: fprintf(stdout, |
---|
435 | "%s Warning: excess line from file skipped=\n%s\n", |
---|
436 | func, line); |
---|
437 | break; |
---|
438 | } |
---|
439 | /* |
---|
440 | * C.3 ENDWHILE !more to read |
---|
441 | */ |
---|
442 | } /* while */ |
---|
443 | |
---|
444 | /* |
---|
445 | * |
---|
446 | * C.4 IF (got a reading error) THEN |
---|
447 | * RETURN Stat 3 |
---|
448 | * ENDIF |
---|
449 | */ |
---|
450 | if (ferror(infile)) { |
---|
451 | DPRINT1 ( "%s: got ferror(infile)\n", func); |
---|
452 | sprintf(errmsg, "%s: got ferror(infile)\n", func); |
---|
453 | fclose(infile); |
---|
454 | stat=3; goto BYE; |
---|
455 | } |
---|
456 | |
---|
457 | /* |
---|
458 | * |
---|
459 | * C.5 CLOSE the input file |
---|
460 | */ |
---|
461 | fclose (infile); |
---|
462 | |
---|
463 | /* |
---|
464 | * |
---|
465 | * C.6 IF (only received less than #required arguments) THEN |
---|
466 | * PRINT warning |
---|
467 | */ |
---|
468 | if (linenum-1 < num_expected) { |
---|
469 | DPRINT4 ( "%s: failed to load %s (%d/%d)\n", |
---|
470 | func, ieee_info_fn, linenum-1, num_expected); |
---|
471 | |
---|
472 | sprintf(errmsg, "%s: failed to load %s (%d/%d)\n", |
---|
473 | func, ieee_info_fn, linenum-1, num_expected); |
---|
474 | stat= 2; |
---|
475 | } |
---|
476 | /* |
---|
477 | * ELSE |
---|
478 | * HARDCODE usFcst_id to 1 (code for Hours) |
---|
479 | * ENDIF |
---|
480 | */ |
---|
481 | else { |
---|
482 | Data_Input->usFcst_id = 1; /* Fcst Time Unit default to Hours */ |
---|
483 | DPRINT0("Hard-Code HOURS -> "); P_USHORT (Data_Input->usFcst_id); |
---|
484 | } |
---|
485 | |
---|
486 | /* |
---|
487 | * |
---|
488 | * C.7 RETURN with stat |
---|
489 | */ |
---|
490 | BYE: |
---|
491 | DPRINT2 ("Leaving %s(), stat=%d;\n", func, stat); |
---|
492 | return (stat); |
---|
493 | /* |
---|
494 | * |
---|
495 | * END OF FUNCTION |
---|
496 | * |
---|
497 | * |
---|
498 | */ |
---|
499 | } |
---|
500 | /* |
---|
501 | ********************************************************************* |
---|
502 | * D. FUNCTION: ld_enc_geomfile |
---|
503 | * fill GEOM_IN struct from file whose name is passed in. |
---|
504 | * |
---|
505 | * INTERFACE: |
---|
506 | * int ld_enc_geomfile (geom_fn, Geom_In, errmsg) |
---|
507 | * |
---|
508 | * ARGUMENTS (I=input, O=output, I&O=input and output): |
---|
509 | * (I) char *geom_fn name of geom info file to load |
---|
510 | * (O) GEOM_IN *Geom_In to hold geom info read from file |
---|
511 | * (O) char *errmsg returned filled if error occurred |
---|
512 | * |
---|
513 | * RETURN CODE: |
---|
514 | * 0> success, file is read and closed, struct is filled |
---|
515 | * 1> error opening file; errmsg filled; |
---|
516 | * 2> failed to get all expected arguments; errmsg filled; |
---|
517 | * 3> Ferror; errmsg filled; |
---|
518 | ********************************************************************** |
---|
519 | */ |
---|
520 | #if PROTOTYPE_NEEDED |
---|
521 | int ld_enc_geomfile (char *geom_fn, GEOM_IN *Geom_In, char *errmsg) |
---|
522 | #else |
---|
523 | int ld_enc_geomfile (geom_fn, Geom_In, errmsg) |
---|
524 | char *geom_fn; |
---|
525 | GEOM_IN *Geom_In; |
---|
526 | char *errmsg; |
---|
527 | #endif |
---|
528 | { |
---|
529 | /* |
---|
530 | * |
---|
531 | * D.1 DEFAULT to no error stat= 0; |
---|
532 | */ |
---|
533 | int i, stat= 0; |
---|
534 | FILE *infile; /* user input file */ |
---|
535 | char *func="ld_enc_geomfile"; |
---|
536 | char temp[100], dummy[100], line[200], *p1; |
---|
537 | int linenum=1; /* number of lines got from the files */ |
---|
538 | int num_expected= 15; /* expecting 15 args from file */ |
---|
539 | |
---|
540 | DPRINT1 ("Entering %s\n",func); |
---|
541 | /* |
---|
542 | * |
---|
543 | * D.2 OPEN input file for reading |
---|
544 | */ |
---|
545 | infile = fopen(geom_fn, "r"); |
---|
546 | if (infile==NULL) { |
---|
547 | DPRINT2 ("%s: Failed to open %s\n", func,geom_fn); |
---|
548 | sprintf (errmsg,"%s: Failed to open %s\n", func,geom_fn); |
---|
549 | stat=1; goto BYE; |
---|
550 | } |
---|
551 | |
---|
552 | /* |
---|
553 | * |
---|
554 | * D.3 WHILE (still more lines AND no error yet) DO |
---|
555 | * D.3.1 GET a line from the file, quit loop if failed; |
---|
556 | * !format: value opt_comments |
---|
557 | * D.3.2 IF line is empty OR is a comment, Loop again; |
---|
558 | */ |
---|
559 | while (!feof(infile) && !ferror(infile) && linenum <= num_expected) |
---|
560 | { |
---|
561 | if (fgets (line, sizeof(line)-1, infile) == NULL) break; |
---|
562 | for (p1=line; *p1 && (iscntrl(*p1)|| isspace(*p1)); p1++); |
---|
563 | if (p1==NULL || *p1=='#') continue; |
---|
564 | /* |
---|
565 | * D.3.3 EXTRACT non-space arguments from line |
---|
566 | * !format: value opt_comments |
---|
567 | * IF (fails) set Stat to error 2 |
---|
568 | * ELSE convert the 2nd argument into an integer |
---|
569 | */ |
---|
570 | if (sscanf (line, "%s%s", temp, dummy) < 1) { |
---|
571 | DPRINT3 ("%s: failed to extract arg from line %d of %s\n", |
---|
572 | func, linenum, geom_fn); |
---|
573 | sprintf(errmsg,"%s: failed to extract arg from line %d of %s\n", |
---|
574 | func, linenum, geom_fn); |
---|
575 | stat=2; |
---|
576 | break; |
---|
577 | } |
---|
578 | |
---|
579 | DPRINT2(" case %2d val=%10s: ", linenum, temp); |
---|
580 | /* |
---|
581 | * D.3.4 SWITCH (what line number we're on) |
---|
582 | * ... GEOM_IN info ... |
---|
583 | * line 1: fill Geom_In->prjn_name |
---|
584 | * line 2: fill Geom_In->nx |
---|
585 | * line 3: fill Geom_In->ny |
---|
586 | * line 4: fill Geom_In->x_int_dis |
---|
587 | * line 5: fill Geom_In->y_int_dis |
---|
588 | * line 6: fill Geom_In->parm_1 |
---|
589 | * line 7: fill Geom_In->parm_2 |
---|
590 | * line 8: fill Geom_In->parm_3 |
---|
591 | * line 9: fill Geom_In->first_lat |
---|
592 | * line 10: fill Geom_In->first_lon |
---|
593 | * line 11: fill Geom_In->last_lat |
---|
594 | * line 12: fill Geom_In->last_lon |
---|
595 | * line 13: fill Geom_In->scan |
---|
596 | * ... More info (previously in file input.dat)... |
---|
597 | * line 14: if 1, set (0x40) bit of Geom_In->usRes_flag |
---|
598 | * line 15: if 1, set (0x08) bit of Geom_In->usRes_flag |
---|
599 | * else : print skip line msg |
---|
600 | * ENDSWITCH |
---|
601 | */ |
---|
602 | switch (linenum++) |
---|
603 | { |
---|
604 | /* ...Geometry info */ |
---|
605 | case 1: strcpy (Geom_In->prjn_name, temp); |
---|
606 | P_STRING (Geom_In->prjn_name); break; |
---|
607 | case 2: Geom_In->nx = (long) atoi(temp); |
---|
608 | P_INT (Geom_In->nx); break; |
---|
609 | case 3: Geom_In->ny = (long) atoi(temp); |
---|
610 | P_INT (Geom_In->ny); break; |
---|
611 | case 4: Geom_In->x_int_dis= (double) atof(temp); |
---|
612 | P_DOUBLE (Geom_In->x_int_dis); break; |
---|
613 | case 5: Geom_In->y_int_dis= (double) atof(temp); |
---|
614 | P_DOUBLE (Geom_In->y_int_dis); break; |
---|
615 | case 6: Geom_In->parm_1= (double) atof(temp); |
---|
616 | P_DOUBLE (Geom_In->parm_1); break; |
---|
617 | case 7: Geom_In->parm_2= (double) atof(temp); |
---|
618 | P_DOUBLE (Geom_In->parm_2); break; |
---|
619 | case 8: Geom_In->parm_3= (double) atof(temp); |
---|
620 | P_DOUBLE (Geom_In->parm_3); break; |
---|
621 | case 9: Geom_In->first_lat= (double) atof(temp); |
---|
622 | P_DOUBLE (Geom_In->first_lat); break; |
---|
623 | case 10: Geom_In->first_lon= (double) atof(temp); |
---|
624 | P_DOUBLE (Geom_In->first_lon); break; |
---|
625 | case 11: Geom_In->last_lat= (double) atof(temp); |
---|
626 | P_DOUBLE (Geom_In->last_lat); break; |
---|
627 | case 12: Geom_In->last_lon= (double) atof(temp); |
---|
628 | P_DOUBLE (Geom_In->last_lon); break; |
---|
629 | case 13: Geom_In->scan= (unsigned short) atoi(temp); |
---|
630 | P_USHORT (Geom_In->scan); break; |
---|
631 | |
---|
632 | /*... Misc. info */ |
---|
633 | case 14: if (atoi(temp)) |
---|
634 | Geom_In->usRes_flag =(unsigned short)0x40; |
---|
635 | P_USHORT (Geom_In->usRes_flag ); break; |
---|
636 | case 15: if (atoi(temp)) |
---|
637 | Geom_In->usRes_flag +=(unsigned short)0x08; |
---|
638 | P_USHORT (Geom_In->usRes_flag ); break; |
---|
639 | default: fprintf(stdout, |
---|
640 | "%s Warning: excess line from file skipped=\n%s\n", |
---|
641 | func, line); |
---|
642 | break; |
---|
643 | } |
---|
644 | /* |
---|
645 | * D.3 ENDWHILE !more to read |
---|
646 | */ |
---|
647 | } /* while */ |
---|
648 | |
---|
649 | /* |
---|
650 | * |
---|
651 | * D.4 IF (got a reading error) THEN |
---|
652 | * RETURN Stat 3 |
---|
653 | * ENDIF |
---|
654 | */ |
---|
655 | if (ferror(infile)) { |
---|
656 | DPRINT1 ( "%s: got ferror(infile)\n", func); |
---|
657 | sprintf(errmsg, "%s: got ferror(infile)\n", func); |
---|
658 | fclose(infile); |
---|
659 | stat=3; goto BYE; |
---|
660 | } |
---|
661 | |
---|
662 | /* |
---|
663 | * |
---|
664 | * D.5 CLOSE the input file |
---|
665 | */ |
---|
666 | fclose (infile); |
---|
667 | |
---|
668 | /* |
---|
669 | * |
---|
670 | * D.6 IF (Status is Good ) THEN |
---|
671 | * IF (received less than #required arguments) PRINT warning |
---|
672 | * ENDIF |
---|
673 | */ |
---|
674 | if (stat == 0 ) { |
---|
675 | if (linenum <= num_expected) { |
---|
676 | DPRINT3 ( "%s: only loaded %d/%d args into Geom_In\n", |
---|
677 | func, linenum-1, num_expected); |
---|
678 | |
---|
679 | sprintf(errmsg, "%s: only loaded %d/%d args into Geom_In\n", |
---|
680 | func, linenum-1, num_expected); |
---|
681 | stat= 2; |
---|
682 | } |
---|
683 | else DPRINT1 ("Got all %d arguments; \n", num_expected); |
---|
684 | } |
---|
685 | |
---|
686 | /* |
---|
687 | * |
---|
688 | * D.7 RETURN with stat |
---|
689 | */ |
---|
690 | BYE: |
---|
691 | DPRINT2 ("Leaving %s, stat=%d;\n", func, stat); |
---|
692 | return (stat); |
---|
693 | /* |
---|
694 | * |
---|
695 | * END OF FUNCTION |
---|
696 | * |
---|
697 | * |
---|
698 | */ |
---|
699 | } |
---|