-
Notifications
You must be signed in to change notification settings - Fork 17
/
readvalue.c
477 lines (384 loc) · 15.4 KB
/
readvalue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* ------- file: -------------------------- readvalues.c ------------
Version: rh2.0
Author: Han Uitenbroek ([email protected])
Last modified: Mon Nov 22 11:20:47 2010 --
-------------------------- ----------RH-- */
/* --- Reads input parameters from file and stores them in InputData
structure input. The input file is read line by line and
keyword--value pairs are extracted, separated by `` = ''. Possible
keywords are defined in the Keyword structure theKeywords. This
structure has the following contents:
struct Keyword {
char keyword; -- Strings for keyword.
char value; -- String for (default) value.
bool set; -- TRUE when keyword has been set
enum keywordtype type; -- One of three types:
KEYWORD_REQUIRED, KEYWORD_DEFAULT,
and KEYWORD_OPTIONAL.
void *pointer, (*setValue)(char *value, void *pointer);
-- pointer to function of type void
accepting pointer to string value
and pointer to actual value.
};
Keywords with KEYWORD_REQUIRED required always have to be set;
a fatal error will occur otherwise. In case of a KEYWORD_DEFAULT
the default (set in the structure initialization for theKeywords,
in the readInput routines of each of the RH codes) is used when
the keyword has not been set. A warning that the default has been
overridden is issued when the keyword is set. In the case of
KEYWORD_OPTIONAL no such warning is issued.
The setValue pointers should point to functions that translate
the string input value to the proper type and store the latter
in the variable pointed to by pointer.
-- -------------- */
#define _GNU_SOURCE
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "rh.h"
#include "atom.h"
#include "atmos.h"
#include "error.h"
#include "inputs.h"
#define COMMENT_CHAR "#"
/* --- Function prototypes -- -------------- */
/* --- Global variables -- -------------- */
extern InputData input;
extern CommandLine commandline;
extern char messageStr[];
/* ------- begin -------------------------- readValues.c ------------ */
void readValues(char *fp_keyword, int Nkeyword, Keyword *theKeywords)
{
const char routineName[] = "readValues";
register int n;
char keyword[MAX_KEYWORD_LENGTH], value[MAX_VALUE_LENGTH],
line[MAX_LINE_SIZE];
bool_t recognized, exit_on_EOF;
int nread;
/* --- Read input data line-by-line -- ------------- */
while (getLineString(&fp_keyword, COMMENT_CHAR, line, exit_on_EOF=FALSE) != EOF) {
if ((nread = sscanf(line, "%s = %s", keyword, value)) != 2) {
sprintf(messageStr, "Missing input value for keyword %s", keyword);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
recognized = FALSE;
for (n = 0; n < Nkeyword; n++) {
if (!strcmp(keyword, theKeywords[n].keyword)) {
recognized = TRUE;
if (theKeywords[n].type == KEYWORD_DEFAULT) {
sprintf(messageStr,
"Overriding default value %s for keyword %s with %s",
theKeywords[n].value, theKeywords[n].keyword, value);
Error(WARNING, routineName, messageStr);
}
strcpy(theKeywords[n].value, value);
theKeywords[n].set = TRUE;
break;
}
}
if (!recognized) {
sprintf(messageStr, "Did not recognize keyword %s with value %s",
keyword, value);
Error(WARNING, routineName, messageStr);
}
}
/* --- Go through all possible keywords and set their values -- --- */
for (n = 0; n < Nkeyword; n++) {
if ((!theKeywords[n].set) &&
(theKeywords[n].type == KEYWORD_REQUIRED)) {
sprintf(messageStr, "Missing input value for required keyword %s",
theKeywords[n].keyword);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
(*theKeywords[n].setValue)(theKeywords[n].value, theKeywords[n].pointer);
}
}
/* ------- end ---------------------------- readValues.c ------------ */
/* ------- end ---------------------------- showValues.c ------------ */
void showValues(int Nkeyword, Keyword *theKeywords)
{
register int n;
char typeStr[MAX_VALUE_LENGTH], *value;
int length;
fprintf(stderr, "\n %-20s = %-30.30s %-18.18s %s\n\n",
"KEYWORD", "VALUE", "(KEYWORD_TYPE)", "SET");
for (n = 0; n < Nkeyword; n++) {
switch (theKeywords[n].type) {
case KEYWORD_REQUIRED: strcpy(typeStr, "(KEYWORD_REQUIRED)"); break;
case KEYWORD_DEFAULT: strcpy(typeStr, "(KEYWORD_DEFAULT)"); break;
case KEYWORD_OPTIONAL: strcpy(typeStr, "(KEYWORD_OPTIONAL)"); break;
}
/* --- Display end rather than begin of keyword value if string is
too long -- -------------- */
if ((length = strlen(theKeywords[n].value)) > 30)
value = theKeywords[n].value + (length - 30);
else
value = theKeywords[n].value;
fprintf(stderr, " %-20s = %-30.30s %-18.18s %s\n",
theKeywords[n].keyword, value, typeStr,
(theKeywords[n].set) ? "X" : "");
}
exit(EXIT_SUCCESS);
}
/* ------- end ---------------------------- showValues.c ------------ */
/* ------- begin -------------------------- setcharValue.c ---------- */
void setcharValue(char *value, void *pointer)
{
strcpy((char *) pointer, value);
}
/* ------- end ---------------------------- setcharValue.c ---------- */
/* ------- begin -------------------------- setintValue.c ----------- */
void setintValue(char *value, void *pointer)
{
int intvalue = atoi(value);
memcpy(pointer, &intvalue, sizeof(int));
}
/* ------- end ---------------------------- setintValue.c ----------- */
/* ------- begin -------------------------- setdoubleValue.c -------- */
void setdoubleValue(char *value, void *pointer)
{
double doublevalue = atof(value);
memcpy(pointer, &doublevalue, sizeof(double));
}
/* ------- end ---------------------------- setdoubleValue.c -------- */
/* ------- begin -------------------------- setboolValue.c ---------- */
void setboolValue(char *value, void *pointer)
{
bool_t boolvalue = (strstr(value, "TRUE")) ? TRUE : FALSE;
memcpy(pointer, &boolvalue, sizeof(bool_t));
}
/* ------- end ---------------------------- setboolValue.c ---------- */
/* ------- begin -------------------------- setAngleSet.c ----------- */
void setAngleSet(char *value, void *pointer)
{
int Nread, Ninclination, Nazimuth;
AngleSet angleSet;
initAngleSet(&angleSet);
if (!strcmp(value, "SET_VERTICAL")) angleSet.set = SET_VERTICAL;
else if (!strcmp(value, "SET_A2")) angleSet.set = SET_A2;
else if (!strcmp(value, "SET_A4")) angleSet.set = SET_A4;
else if (!strcmp(value, "SET_A6")) angleSet.set = SET_A6;
else if (!strcmp(value, "SET_A8")) angleSet.set = SET_A8;
else if (!strcmp(value, "SET_B4")) angleSet.set = SET_B4;
else if (!strcmp(value, "SET_B6")) angleSet.set = SET_B6;
else if (!strcmp(value, "SET_B8")) angleSet.set = SET_B8;
else if (!strcmp(value, "NO_SET")) angleSet.set = NO_SET;
else if (strstr(value, "SET_GL_")) {
if (sscanf(value, "SET_GL_%dX%d", &Ninclination, &Nazimuth) != 2) {
sprintf(messageStr,
"\n Invalid Gauss-Legandre format for keyword ANGLE_SET: %s",
value);
Error(ERROR_LEVEL_2, "setAngleSet", messageStr);
}
angleSet.set = SET_GL;
angleSet.Ninclination = Ninclination;
angleSet.Nazimuth = Nazimuth;
sprintf(messageStr,
"\n Found Gauss-Legendre angleset with "
"Ninclination = %d and Nazimuth = %d",
Ninclination, Nazimuth);
Error(MESSAGE, "setAngleSet", messageStr);
if (Ninclination <= 0 || Ninclination > NMAXINCLINATION) {
sprintf(messageStr,
"\n Invalid value for Ninclination in angleset: %d",
Ninclination);
Error(ERROR_LEVEL_2, "setAngleSet", messageStr);
}
if (Nazimuth <= 0 || Nazimuth > NMAXAZIMUTH) {
sprintf(messageStr,
"\n Invalid value for Nazimuth in angleset: %d",
Nazimuth);
Error(ERROR_LEVEL_2, "setAngleSet", messageStr);
}
} else {
sprintf(messageStr,
"\n Invalid value for keyword ANGLE_SET: %s", value);
Error(ERROR_LEVEL_2, "setAngleSet", messageStr);
}
memcpy(pointer, &angleSet, sizeof(AngleSet));
}
/* ------- begin -------------------------- initAngleSet.c ---------- */
void initAngleSet(AngleSet *angleSet)
{
angleSet->set = NO_SET;
angleSet->Ninclination = 0;
angleSet->Nazimuth = 0;
}
/* ------- end ---------------------------- initAngleSet.c ---------- */
/* ------- begin -------------------------- setStokesMode.c --------- */
void setStokesMode(char *value, void *pointer)
{
const char routineName[] = "setStokesMode";
enum StokesMode StokesMode;
if (!strcmp(value, "NO_STOKES"))
StokesMode = NO_STOKES;
else if (!strcmp(value, "FIELD_FREE"))
StokesMode = FIELD_FREE;
else if (!strcmp(value, "POLARIZATION_FREE"))
StokesMode = POLARIZATION_FREE;
else if (!strcmp(value, "FULL_STOKES"))
StokesMode = FULL_STOKES;
else {
sprintf(messageStr,
"Invalid value for keyword STOKES_MODE: %s", value);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
memcpy(pointer, &StokesMode, sizeof(enum_t));
}
/* ------- end ---------------------------- setStokesMode.c --------- */
/* ------- begin ---------------------- set_S_interpolation.c ------- */
void set_S_interpolation(char *value, void *pointer)
{
const char routineName[] = "set_S_interpolation";
enum S_interpol interpolation;
if (!strcmp(value, "LINEAR"))
interpolation = S_LINEAR;
else if (!strcmp(value, "BEZIER"))
interpolation = BEZIER;
else if (!strcmp(value, "BEZIER3"))
interpolation = BEZIER3;
else if (!strcmp(value, "CUBIC_HERMITE"))
interpolation = CUBIC_HERMITE;
else {
sprintf(messageStr,
"Invalid value for keyword S_INTERPOLATION: %s", value);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
memcpy(pointer, &interpolation, sizeof(enum S_interpol));
}
/* ------- end -----------------------set_S_interpolation.c ------- */
/* ------- begin ---------------------- set_S_interpolation_stokes.c ------- */
void set_S_interpolation_stokes(char *value, void *pointer)
{
const char routineName[] = "set_S_interpolation_stokes";
enum S_interpol_stokes interpolation;
if (!strcmp(value, "DELO_BEZIER3"))
interpolation = DELO_BEZIER3;
else if (!strcmp(value, "DELO_PARABOLIC"))
interpolation = DELO_PARABOLIC;
else {
sprintf(messageStr,
"Invalid value for keyword S_INTERPOLATION_STOKES: %s", value);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
memcpy(pointer, &interpolation, sizeof(enum S_interpol));
}
/* ------- end -----------------------set_S_interpolation_stokes.c ------- */
/* ------- begin -------------------------- setPRDangle.c --------- */
void setPRDangle(char *value, void *pointer)
{
const char routineName[] = "setPRDangle";
enum PRDangle PRD_angle_dep;
if (!strcmp(value, "PRD_ANGLE_INDEP"))
PRD_angle_dep = PRD_ANGLE_INDEP;
else if (!strcmp(value, "PRD_ANGLE_APPROX"))
PRD_angle_dep = PRD_ANGLE_APPROX;
else if (!strcmp(value, "PRD_ANGLE_DEP"))
PRD_angle_dep = PRD_ANGLE_DEP;
else {
sprintf(messageStr,
"Invalid value for keyword PRD_ANGLE_DEP: %s", value);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
memcpy(pointer, &PRD_angle_dep, sizeof(enum_t));
}
/* ------- end ---------------------------- setPRDangle.c --------- */
/* ------- begin -------------------------- setThreadValue.c -------- */
#define N_THREAD_LIMIT 32
void setThreadValue(char *value, void *pointer)
{
const char routineName[] = "setThreadValue";
int Nthreads = atoi(value), return_value;
if (Nthreads > N_THREAD_LIMIT) {
sprintf(messageStr,
"Value of keyword N_THREADS (%d) larger than allowed limit (%d)",
Nthreads, N_THREAD_LIMIT);
Error(ERROR_LEVEL_2, routineName, messageStr);
} else if (Nthreads < 1) {
Nthreads = 1;
}
if (Nthreads > 1) {
/* --- For now we try to use the default thread attributes,
most notably:
- PTHREAD_SCOPE_PROCESS for scope
- PTHREAD_CREATE_JOINABLE for detach state
- PTHREAD_INHERIT_SCHED for scheduling inheritance
- SCHED_OTHER for scheduling policy
--- -------------- */
pthread_attr_init(&input.thread_attr);
if (pthread_attr_setscope(&input.thread_attr, PTHREAD_SCOPE_PROCESS))
Error(WARNING, routineName, "Non-default thread scope");
if (pthread_attr_setdetachstate(&input.thread_attr,
PTHREAD_CREATE_JOINABLE))
Error(WARNING, routineName, "Non-default thread detach state");
if (pthread_attr_setinheritsched(&input.thread_attr,
PTHREAD_INHERIT_SCHED))
Error(WARNING, routineName, "Non-default thread scheduling inheritance");
if (pthread_attr_setschedpolicy(&input.thread_attr, SCHED_OTHER))
Error(WARNING, routineName, "Non-default thread scheduling policy");
/* --- Suggest thread concurrency to the operating system -- ---- */
if ((return_value = pthread_setconcurrency(Nthreads)))
Error(ERROR_LEVEL_2, routineName,
"Failed to set concurrency level for threads.");
else {
sprintf(messageStr, "Setting thread concurrency to %d", Nthreads);
Error(WARNING, routineName, messageStr);
}
}
memcpy(pointer, &Nthreads, sizeof(int));
}
/* ------- end ---------------------------- setThreadValue.c -------- */
/* ------- begin -------------------------- setInterpolate_3D.c ----- */
void setInterpolate_3D(char *value, void *pointer)
{
const char routineName[] = "setInterpolate_3D";
enum order_3D order;
if (!strcmp(value, "LINEAR_3D"))
order = LINEAR_3D;
else if (!strcmp(value, "BICUBIC_3D"))
order = BICUBIC_3D;
else {
sprintf(messageStr,
"\n Invalid value for keyword INTERPOLATE_3D: %s", value);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
memcpy(pointer, &order, sizeof(enum order_3D));
}
/* ------- end ---------------------------- setInterpolate_3D.c ----- */
/* ------- begin -------------------------- setstartValue.c --------- */
void setstartValue(char *value, void *pointer)
{
const char routineName[] = "setStartValue";
enum solution startvalue;
if (!strcmp(value, "NEW_J"))
startvalue = NEW_J;
else if (!strcmp(value, "OLD_J"))
startvalue = OLD_J;
else {
sprintf(messageStr,
"Invalid value for keyword STARTING_J: %s", value);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
memcpy(pointer, &startvalue, sizeof(enum_t));
}
/* ------- end ---------------------------- setstartValue.c --------- */
/* ------- begin -------------------------- setnesolution.c --------- */
void setnesolution(char *value, void *pointer)
{
const char routineName[] = "setnesolution";
enum ne_solution nesolution;
if (!strcmp(value, "NONE"))
nesolution = NONE;
else if (!strcmp(value, "ONCE"))
nesolution = ONCE;
else if (!strcmp(value, "ITERATION")) {
nesolution = ITERATION;
} else {
sprintf(messageStr,
"Invalid value for keyword SOLVE_NE: %s", value);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
memcpy(pointer, &nesolution, sizeof(enum_t));
}
/* ------- end ---------------------------- setnesolution.c --------- */