OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
pml_iop_config.c
Go to the documentation of this file.
1 #include "pml_iop.h"
2 #include "pml_iop_config.h"
3 
4 /* Module: pml_iop_config.c */
5 /* Authors: Tim Smyth and Gerald Moore */
6 /* Date: 06/03/07 */
7 /* Version: 1.3 */
8 /* History: Modified from the original pml_config.c */
9 /* Description: */
10 /* Functions to read in and to extract information from the configuration file */
11 
12 int tab_size = 0;
14 
15 /* Function: loadconfig */
16 
17 /* Purpose: Reads in the config file and reads in the variables therein */
18 void loadconfig(char *fname) {
19  int cp, t_p, t_f, e_p, e_f;
21 
22  FILE *cfg_file;
23 
24  if ((cfg_file = fopen(fname, "r")) == NULL) {
25  printf("loadconfig: File %s was not found\n", fname);
26  exit(1);
27  }
28 
29  /* Read in the config file one line at a time */
30  while (!feof(cfg_file)) {
31  fgets(line, MAX_LINE, cfg_file);
32  /* Remove terminating carriage return <cr> - replace with NULL*/
33  cp = strlen(line) - 1;
34  if (cp >= 0) {
35  if (line[cp] == '\n') line[cp] = '\0';
36  }
37  /* Ignore white space, comments (#) and blank lines */
38  if (!((strlen(line) == 0) || (line[0] == '#') || (isspace(line[0])))) {
39  /* sort out the token and entry */
40  t_p = 0;
41  t_f = -1;
42  e_p = 0;
43  e_f = -1;
44  for (cp = 0; cp < strlen(line); cp++) {
45  if (isspace(line[cp])&&(t_f < 0)) t_f = 1;
46  if ((t_f > 0) && !(isspace(line[cp]))) e_f = 1;
47  if (t_f < 0) token[t_p++] = line[cp];
48  if (e_f > 0) entry[e_p++] = line[cp];
49  }
50  token[t_p] = 0;
51  entry[e_p] = 0;
52  /* put the entry into the cfg_tab structure */
55  tab_size++;
56  }
57  }
58  fclose(cfg_file);
59 }
60 
61 /* Function: get_cfg_s */
62 
63 /* Purpose: get a string value from the configuration table */
64 char *get_cfg_s(char *tok, char *fname) {
65  int i, found;
66  if (tab_size <= 0) loadconfig(fname);
67  found = -1;
68  for (i = 0; i < tab_size; i++) {
69  if (strcmp(tok, cfg_tab[i].token) == 0) found = i;
70  }
71  if (found < 0) {
72  fprintf(stderr, "Entry %s not found in configuration file\n", tok);
73  return NULL;
74  }
75  return (cfg_tab[found].entry);
76 }
77 
78 /* Function: get_cfg_i */
79 
80 /* Purpose: get an integer value from the configuration table */
81 int get_cfg_i(char *tok, char *fname) {
82  int i, found;
83  if (tab_size <= 0) loadconfig(fname);
84  found = -1;
85  for (i = 0; i < tab_size; i++) {
86  if (strcmp(tok, cfg_tab[i].token) == 0) found = i;
87  }
88  if (found < 0) {
89  fprintf(stderr, "Entry %s not found in configuration file\n", tok);
90  return -9999;
91  }
92  return (atoi(cfg_tab[found].entry));
93 }
94 
95 /* Function: get_cfg_f */
96 
97 /* Purpose: get a float value from the configuration table */
98 float get_cfg_f(char *tok, char *fname) {
99  int i, found;
100  if (tab_size <= 0) loadconfig(fname);
101  found = -1;
102  for (i = 0; i < tab_size; i++) {
103  if (strcmp(tok, cfg_tab[i].token) == 0) found = i;
104  }
105  if (found < 0) {
106  fprintf(stderr, "Entry %s not found in configuration file\n", tok);
107  return -9999.999;
108  }
109  return atof(cfg_tab[found].entry);
110 }
111 
112 /* Function: get_cfg_array */
113 /* Purpose: get a float array from the configuration table file */
114 
115 /* Parses up to a max */
116 float *get_cfg_array(char *tok, char *fname) {
117  int i, found, n_arr;
118  float *tmp_arr;
119  char tmp_entry[MAX_ENTRY], *p;
120 
121  if (tab_size <= 0) loadconfig(fname);
122  found = -1;
123 
124  for (i = 0; i < tab_size; i++) {
125  if (strcmp(tok, cfg_tab[i].token) == 0) found = i;
126  }
127  if (found < 0) {
128  fprintf(stderr, "Entry %s not found in configuration file\n", tok);
129  return NULL;
130  }
131 
132  /* Now need to disentangle the comma separated floats */
133  strcpy(tmp_entry, cfg_tab[found].entry);
134  /* Strtok destroys the string */
135  p = strtok(tmp_entry, ",");
136  n_arr = 0;
137  tmp_arr = calloc(MAX_ARR, sizeof (float));
138  /* Loop over the "array" of numbers to find the number of elements */
139  while (p != NULL) {
140  tmp_arr[n_arr] = (float) atof(p);
141  p = strtok(NULL, ",");
142  n_arr++;
143  }
144  for (i = n_arr; i < MAX_ARR; i++) tmp_arr[i] = 0;
145  return tmp_arr;
146 }
147 
148 
149 
void loadconfig(char *fname)
#define MAX_ARR
#define MAX_ENTRY
#define NULL
Definition: decode_rs.h:63
int get_cfg_i(char *tok, char *fname)
float * get_cfg_array(char *tok, char *fname)
#define MAX_TOK
struct tab_atom cfg_tab[MAX_TAB]
#define isspace(c)
#define MAX_LINE
Definition: pml_iop_config.h:9
int tab_size
char * get_cfg_s(char *tok, char *fname)
#define MAX_TAB
char token[MAX_TOK]
float get_cfg_f(char *tok, char *fname)
char entry[MAX_ENTRY]
int i
Definition: decode_rs.h:71
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
float p[MODELMAX]
Definition: atrem_corl1.h:131