OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
smigen_input.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include "smigen_input.h"
6 #include "smiinc.h"
7 #include "palette.h"
8 #include "genutils.h"
9 
10 int get_item(char *arg, instr *input_str);
11 void set_param_string(instr *input_str);
12 int par_file(char *pfile, instr *input_str);
13 int get_def_l2prod(char *def_file, instr *input_str);
14 
15 int input_init(instr *input_str) {
16  input_str->ifile[0] = '\0';
17  input_str->ofile[0] = '\0';
18  input_str->pfile[0] = '\0';
19  input_str->prod [0] = '\0';
20  input_str->parms[0] = '\0';
21 
22  input_str->stype = 0;
23  input_str->meas = 1;
24  input_str->datamin = 0.0;
25  input_str->datamax = 0.0;
26 
27  input_str->lonwest = -180.0;
28  input_str->loneast = +180.0;
29  input_str->latsouth = -90.0;
30  input_str->latnorth = +90.0;
31 
32  strcpy(input_str->pversion, "Unspecified");
33  strcpy(input_str->projection, "RECT");
34  strcpy(input_str->resolution, "9km");
35  strcpy(input_str->palfile, "DEFAULT");
36 
37  input_str->gap_fill = 0;
38  input_str->seam_lon = -180;
39 
40  input_str->proddesc[0] = '\0';
41  input_str->units[0] = '\0';
42  input_str->precision[0] = '\0';
43 
44  input_str->minobs = 0;
45  input_str->deflate = 0;
46  input_str->oformat[0] = '\0';
47  return 0;
48 }
49 
50 /*-----------------------------------------------------------------------------
51  Function: smigen_input
52 
53  Returns: int (status)
54  The return code is a negative value if any error occurs, otherwise,
55  returns 0.
56 
57  Description:
58  Convert the arguments from the command line into a structure input
59  variable.
60 
61  Parameters: (in calling order)
62  Type Name I/O Description
63  ---- ---- --- -----------
64  int argc I number of arguments
65  char **argv I list of arguments
66  instr input O structure variable for inputs
67 
68 ----------------------------------------------------------------------------*/
69 
70 int smigen_input(int argc, char **argv, instr *input) {
71  int i;
72 
73  if (argc == 1) return (-1);
74 
75  /* */
76  /* Set input values to defaults */
77  /* */
78  if (input_init(input) != 0) {
79  printf("-E- %s: Error initializing input structure.\n", __FILE__);
80  return (-1);
81  }
82 
83  /* */
84  /* Loop through command arguments and update input struct */
85  /* */
86  for (i = 1; i < argc; i++)
87  if (get_item(argv[i], input) != 0)
88  return -1;
89 
90 
91 #if 0
92  /* Define default palette */
93  /* ---------------------- */
94  if (strcmp(input->prod, "NDVI") == 0 ||
95  strcmp(input->prod, "EVI") == 0)
96  memcpy(input->palette, ndvi_palette, 768);
97  else if (strcmp(input->prod, "sst") == 0 ||
98  strcmp(input->prod, "sst4") == 0)
99  memcpy(input->palette, sst_palette, 768);
100  else
101  memcpy(input->palette, default_palette, 768);
102 
103 
104  /* */
105  /* Add any input-dependent defaults */
106  /* */
107  if (strcmp(input->palfile, "DEFAULT") != 0) {
108  short *r, *g, *b;
109  if (!(r = (short *) calloc(256, sizeof (short)))) {
110  fprintf(stderr, "smigen: Error allocating space for red.\n");
111  return -1;
112  };
113  if (!(g = (short *) calloc(256, sizeof (short)))) {
114  fprintf(stderr, "smigen: Error allocating space for green.\n");
115  return -1;
116  };
117  if (!(b = (short *) calloc(256, sizeof (short)))) {
118  fprintf(stderr, "smigen: Error allocating space for blue.\n");
119  return -1;
120  };
121  if (getlut_file(input->palfile, r, g, b)) {
122  fprintf(stderr, "Error reading palette file %s\n", input->pfile);
123  return -1;
124  }
125  for (i = 0; i < 256; i++) {
126  input->palette[i * 3] = r[i];
127  input->palette[i * 3 + 1] = g[i];
128  input->palette[i * 3 + 2] = b[i];
129  }
130  free(r);
131  free(g);
132  free(b);
133  }
134 #endif
135  return 0;
136 }
137 
138 void set_param_string(instr *input) {
139 
140  char str_buf[4096];
141  /* */
142  /* Build string of parameters for metadata */
143  /* */
144  sprintf(str_buf, "ifile = %s|", input->ifile);
145  strcat(input->parms, str_buf);
146  sprintf(str_buf, "ofile = %s|", input->ofile);
147  strcat(input->parms, str_buf);
148  if (input->pfile[0] != '\0') {
149  sprintf(str_buf, "pfile = %s|", input->pfile);
150  strcat(input->parms, str_buf);
151  }
152  sprintf(str_buf, "prod = %s|", input->prod);
153  strcat(input->parms, str_buf);
154  sprintf(str_buf, "palfile = %s|", input->palfile);
155  strcat(input->parms, str_buf);
156  sprintf(str_buf, "processing version = %s|", input->pversion);
157  strcat(input->parms, str_buf);
158  sprintf(str_buf, "meas = %d|", input->meas);
159  strcat(input->parms, str_buf);
160  sprintf(str_buf, "stype = %d|", input->stype);
161  strcat(input->parms, str_buf);
162  sprintf(str_buf, "datamin = %f|", input->datamin);
163  strcat(input->parms, str_buf);
164  sprintf(str_buf, "datamax = %f|", input->datamax);
165  strcat(input->parms, str_buf);
166  sprintf(str_buf, "lonwest = %f|", input->lonwest);
167  strcat(input->parms, str_buf);
168  sprintf(str_buf, "loneast = %f|", input->loneast);
169  strcat(input->parms, str_buf);
170  sprintf(str_buf, "latsouth = %f|", input->latsouth);
171  strcat(input->parms, str_buf);
172  sprintf(str_buf, "latnorth = %f|", input->latnorth);
173  strcat(input->parms, str_buf);
174  sprintf(str_buf, "resolution = %s|", input->resolution);
175  strcat(input->parms, str_buf);
176  sprintf(str_buf, "projection = %s|", input->projection);
177  strcat(input->parms, str_buf);
178  sprintf(str_buf, "gap_fill = %d|", input->gap_fill);
179  strcat(input->parms, str_buf);
180  sprintf(str_buf, "seam_lon = %f|", input->seam_lon);
181  strcat(input->parms, str_buf);
182  sprintf(str_buf, "minobs = %d|", input->minobs);
183  strcat(input->parms, str_buf);
184  sprintf(str_buf, "deflate = %d|", input->deflate);
185  strcat(input->parms, str_buf);
186  sprintf(str_buf, "oformat = %s|", input->oformat);
187  strcat(input->parms, str_buf);
188  sprintf(str_buf, "precision = %s|", input->precision);
189  strcat(input->parms, str_buf);
190 }
191 
192 int get_item(char *arg, instr *input) {
193  char *tmp_str;
194  char keyword [20];
195  char parm_str[4096];
196  char tmp_file[FILENAME_MAX];
197  int ilen1, iret;
198  static int lon_limit_used = 0;
199  static int seam_used = 0;
200 
201  if ((tmp_str = strchr(arg, '=')) == NULL) {
202  printf("Invalid argument \"%s\"\n", arg);
203  return -1;
204  }
205 
206  ilen1 = tmp_str - arg;
207  strncpy(keyword, arg, ilen1);
208  keyword[ilen1] = '\0';
209  strcpy(parm_str, tmp_str + 1);
210 
211  /* change keyword to lower case */
212  tmp_str = keyword;
213  while (*tmp_str != '\0') {
214  if (isupper(*tmp_str)) *tmp_str = tolower(*tmp_str);
215  tmp_str++;
216  }
217 
218  if (strcmp(keyword, "par") == 0) {
219  iret = par_file(parm_str, input);
220  return iret;
221 
222  } else if (strcmp(keyword, "ifile") == 0) {
223  parse_file_name(parm_str, tmp_file);
224  strcpy(input->ifile, tmp_file);
225 
226  } else if (strcmp(keyword, "ofile") == 0) {
227  parse_file_name(parm_str, tmp_file);
228  strcpy(input->ofile, tmp_file);
229 
230  } else if (strcmp(keyword, "prod") == 0) {
231  parse_file_name(parm_str, tmp_file);
232  strcpy(input->prod, tmp_file);
233 
234  } else if (strcmp(keyword, "palfile") == 0) {
235  parse_file_name(parm_str, tmp_file);
236  strcpy(input->palfile, tmp_file);
237 
238  } else if (strcmp(keyword, "pversion") == 0) {
239  parse_file_name(parm_str, tmp_file);
240  strcpy(input->pversion, tmp_file);
241 
242  } else if (strcmp(keyword, "stype") == 0) {
243  input->stype = atoi(parm_str);
244 
245  } else if (strcmp(keyword, "meas") == 0) {
246  input->meas = atoi(parm_str);
247  if (input->meas == 4) strcpy(input->prod, "pixels");
248  if (input->meas == 5) strcpy(input->prod, "scenes");
249 
250  } else if (strcmp(keyword, "datamin") == 0) {
251  input->datamin = atof(parm_str);
252 
253  } else if (strcmp(keyword, "datamax") == 0) {
254  input->datamax = atof(parm_str);
255 
256  } else if (strcmp(keyword, "lonwest") == 0) {
257  input->lonwest = atof(parm_str);
258  lon_limit_used = 1;
259 
260  } else if (strcmp(keyword, "loneast") == 0) {
261  input->loneast = atof(parm_str);
262  lon_limit_used = 1;
263 
264  } else if (strcmp(keyword, "latsouth") == 0) {
265  input->latsouth = atof(parm_str);
266 
267  } else if (strcmp(keyword, "latnorth") == 0) {
268  input->latnorth = atof(parm_str);
269 
270  } else if (strcmp(keyword, "resolution") == 0) {
271  parse_file_name(parm_str, tmp_file);
272  strcpy(input->resolution, tmp_file);
273 
274  } else if (strcmp(keyword, "projection") == 0) {
275  parse_file_name(parm_str, tmp_file);
276  strcpy(input->projection, tmp_file);
277 
278  } else if (strcmp(keyword, "gap_fill") == 0) {
279  input->gap_fill = atoi(parm_str);
280 
281  } else if (strcmp(keyword, "seam_lon") == 0) {
282  input->seam_lon = atof(parm_str);
283  seam_used = 1;
284 
285  } else if (strcmp(keyword, "proddesc") == 0) {
286  parse_file_name(parm_str, tmp_file);
287  strcpy(input->proddesc, tmp_file);
288 
289  } else if (strcmp(keyword, "units") == 0) {
290  parse_file_name(parm_str, tmp_file);
291  strcpy(input->units, tmp_file);
292 
293  } else if (strcmp(keyword, "precision") == 0) {
294  parse_file_name(parm_str, tmp_file);
295  strcpy(input->precision, tmp_file);
296 
297  } else if (strcmp(keyword, "minobs") == 0) {
298  input->minobs = atol(parm_str);
299 
300  } else if (strcmp(keyword, "deflate") == 0) {
301  input->deflate = atoi(parm_str);
302 
303  } else if (strcmp(keyword, "oformat") == 0) {
304  const char* tmpStr = getFileFormatName(parm_str);
305  strcpy(input->oformat, tmpStr);
306 
307  } else {
308  goto Invalid_return;
309  }
310 
311  if (lon_limit_used == 1 && seam_used == 1) {
312  printf("LONEAST/LONWEST cannot be used with SEAM_LON.\n");
313  exit(1);
314  }
315 
316  return 0;
317 
318 
319 Invalid_return:
320  printf("Invalid argument \"%s\"\n", arg);
321  return -1;
322 }
323 
324 int par_file(char *pfile, instr *input) {
325  FILE *fp;
326  char arg[2048];
327  int iret;
328 
329  if ((fp = fopen(pfile, "r")) == NULL) {
330  printf("Error on opening the parameter file - %s\n", pfile);
331  return -1;
332  }
333 
334  while ((fgets(arg, 2047, fp)) != NULL) {
335  /* skip the comment or blank line */
336  if (arg[0] == '#' || arg[0] == ';' || arg[0] == ' ' || arg[0] == '\0' ||
337  arg[0] == '\n')
338  continue;
339 
340  arg[strlen(arg) - 1] = '\0'; /* replace the last char new_line to NULL */
341  iret = get_item(arg, input);
342  if (iret != 0) {
343  fclose(fp);
344  return -1;
345  }
346  }
347 
348  fclose(fp);
349  return 0;
350 }
int r
Definition: decode_rs.h:73
void set_param_string(instr *input_str)
Definition: smigen_input.c:138
#define NULL
Definition: decode_rs.h:63
int input_init(instr *input_str)
Definition: smigen_input.c:15
int get_def_l2prod(char *def_file, instr *input_str)
instr * input
const char * getFileFormatName(const char *str)
int par_file(char *pfile, instr *input_str)
Definition: smigen_input.c:324
void parse_file_name(const char *inpath, char *outpath)
data_t b[NROOTS+1]
Definition: decode_rs.h:77
int getlut_file(char *lut_file, short *rlut, short *glut, short *blut)
Definition: getlut_file.c:8
int smigen_input(int argc, char **argv, instr *input)
Definition: smigen_input.c:70
PARAM_TYPE_NONE Default value No parameter is buried in the product name name_prefix is case insensitive string compared to the product name PARAM_TYPE_VIS_WAVE The visible wavelength bands from the sensor are buried in the product name The product name is compared by appending and name_suffix ie aph_412_giop where prod_ix will be set to PARAM_TYPE_IR_WAVE same search method as PARAM_TYPE_VIS_WAVE except only wavelength above are looped through but prod_ix is still based ie aph_2_giop for the second and prod_ix set to PARAM_TYPE_INT name_prefix is compared with the beginning of the product name If name_suffix is not empty the it must match the end of the product name The characters right after the prefix are read as an integer and prod_ix is set to that number strncpy(l2prod->name_prefix, "myprod", UNITLEN)
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")
int get_item(char *arg, instr *input_str)
Definition: smigen_input.c:192