NASA Logo
Ocean Color Science Software

ocssw V2022
l3bin_input.cpp
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 <mfhdf.h>
6 
7 #include "genutils.h"
8 #include "l3bin_input.h"
9 
10 static char mainProgramName[50];
11 
12 static char *l3bin_optionKeys[] = {
13  "help",
14  "version",
15  "verbose",
16  "dump_options",
17  "dump_options_paramfile",
18  "dump_options_xmlfile",
19  "par",
20  "pversion",
21  "ifile",
22  "ofile",
23  "oformat",
24  "merged",
25  "latnorth",
26  "latsouth",
27  "loneast",
28  "lonwest",
29  "sday",
30  "eday",
31  "deflate",
32  "orbit1",
33  "orbit2",
34  "median",
35  "noext",
36  "unit_wgt",
37  "composite_scheme",
38  "composite_prod",
39  "reduce_fac",
40  "resolve",
41  "prod",
42  "doi",
43  NULL
44 };
45 
46 static char *l3binmerge_optionKeys[] = {
47  "help",
48  "version",
49  "verbose",
50  "dump_options",
51  "dump_options_paramfile",
52  "dump_options_xmlfile",
53  "par",
54  "pversion",
55  "eval",
56  "ifile",
57  "ofile",
58  "latnorth",
59  "latsouth",
60  "loneast",
61  "lonwest",
62  "noext",
63  "u",
64  "prod",
65  NULL
66 };
67 
68 int input_init(instr *input_str) {
69  input_str->infile[0] = '\0';
70  input_str->ofile[0] = '\0';
71  input_str->pfile[0] = '\0';
72 
73  strcpy(input_str->out_parm, "DEFAULT");
74  strcpy(input_str->pversion, "Unspecified");
75 
76  input_str->syear = 9999;
77  input_str->sday = 999;
78 
79  input_str->eyear = 9999;
80  input_str->eday = 999;
81 
82  input_str->sorbit = -1;
83  input_str->eorbit = -1;
84 
85  input_str->reduce_fac = 1;
86  input_str->resolve[0] = '\0';
87 
88  input_str->noext = 0;
89 
90  input_str->merged[0] = '\0';
91 
92  input_str->loneast = +180;
93  input_str->lonwest = -180;
94  input_str->latnorth = +90;
95  input_str->latsouth = -90;
96 
97  input_str->verbose = 0;
98  input_str->unit_wgt = 0;
99  input_str->median = 0;
100  input_str->union_bins = 0;
101 
102  input_str->deflate = 0;
103  input_str->oformat[0] = '\0';
104 
105  input_str->composite_prod[0] = '\0';
106  input_str->composite_scheme[0] = '\0';
107  input_str->doi[0] = '\0';
108 
109  return 0;
110 }
111 
112 int l3bin_init_options(clo_optionList_t* list, const char* prog, const char* version) {
113  char tmpStr[2048];
114  clo_option_t* option;
115 
116  // set the min program name
117  strcpy(mainProgramName, prog);
118 
119  if (!strcmp(prog, "l3bin")) {
120  clo_setSelectOptionKeys(l3bin_optionKeys);
121  } else if (!strcmp(prog, "l3binmerge")) {
122  clo_setSelectOptionKeys(l3binmerge_optionKeys);
123  }
124  sprintf(tmpStr, "%s ifile=input-file ofile=output-file prod=prodlist\n\n", prog);
125  strcat(tmpStr, " The input file is a list of L3 binned files.\n");
126  strcat(tmpStr, " The argument-list is a set of keyword=value pairs. The arguments can\n");
127  strcat(tmpStr, " be specified on the commandline, or put into a parameter file, or the\n");
128  strcat(tmpStr, " two methods can be used together, with commandline over-riding.\n\n");
129  strcat(tmpStr, " return value: 0=OK, 1=error, 110=no pixels binned. \n\n");
130  strcat(tmpStr, "The list of valid keywords follows:\n");
131  clo_setHelpStr(tmpStr);
132 
133  // add the parfile alias for backward compatibility
134  clo_addAlias(list, "par", "parfile");
135 
136  strcpy(tmpStr, "input file name with list of L3 files");
137  option = clo_addOption(list, "ifile", CLO_TYPE_IFILE, NULL, tmpStr);
138  clo_addOptionAlias(option, "in");
139  clo_addOptionAlias(option, "infile");
140 
141  option = clo_addOption(list, "ofile", CLO_TYPE_OFILE, "output", "output bin file name");
142  clo_addOptionAlias(option, "out");
143  option = clo_addOption(list, "merged", CLO_TYPE_OFILE, NULL, "merged file name");
144 
145  strcpy(tmpStr, "output file format\n");
146  strcat(tmpStr, " hdf4: output a HDF4 file\n");
147  strcat(tmpStr, " netCDF4: output a netCDF4 file\n");
148  strcat(tmpStr, " hdf5: output a HDF5 file\n");
149  clo_addOption(list, "oformat", CLO_TYPE_STRING, "netCDF4", tmpStr);
150 
151  strcpy(tmpStr, "set to 1 to suppress generation of\n external files");
152  clo_addOption(list, "noext", CLO_TYPE_BOOL, "off", tmpStr);
153 
154  clo_addOption(list, "verbose", CLO_TYPE_BOOL, "off", "Allow more verbose screen messages");
155  clo_addOption(list, "latnorth", CLO_TYPE_FLOAT, "+90", "northern most latitude");
156  clo_addOption(list, "latsouth", CLO_TYPE_FLOAT, "-90", "southern most latitude");
157  clo_addOption(list, "loneast", CLO_TYPE_FLOAT, "+180", "eastern most longitude");
158  clo_addOption(list, "lonwest", CLO_TYPE_FLOAT, "-180", "western most longitude");
159  clo_addOption(list, "reduce_fac", CLO_TYPE_INT, "1", "scale reduction factor (power of 2)");
160  strcpy(tmpStr, "bin resolution, overrides reduce_frac if defined\n");
161  strcat(tmpStr, " H: 0.5km\n");
162  strcat(tmpStr, " Q: 250m\n");
163  strcat(tmpStr, " HQ: 100m\n");
164  strcat(tmpStr, " HH: 50m\n");
165  strcat(tmpStr, " 1: 1.1km\n");
166  strcat(tmpStr, " 2: 2.3km\n");
167  strcat(tmpStr, " 4: 4.6km\n");
168  strcat(tmpStr, " 9: 9.2km\n");
169  strcat(tmpStr, " 18: 18.5km\n");
170  strcat(tmpStr, " 36: 36km\n");
171  strcat(tmpStr, " 1D: 1 degree\n");
172  strcat(tmpStr, " HD: 0.5 degree\n");
173  strcat(tmpStr, " QD: 0.25 degree");
174  option = clo_addOption(list, "resolve", CLO_TYPE_STRING, NULL, tmpStr);
175  clo_addOption(list, "pversion", CLO_TYPE_STRING, "unspecified", "production version");
176  clo_addOption(list, "sday", CLO_TYPE_INT, "1970001", "start datadate (YYYYDDD) ");
177  clo_addOption(list, "eday", CLO_TYPE_INT, "2038018", "end datadate (YYYYDDD)");
178  clo_addOption(list, "deflate", CLO_TYPE_INT, "5", "deflate level");
179 
180  clo_addOption(list, "orbit1", CLO_TYPE_INT, "-1", "sorbit");
181  clo_addOption(list, "orbit2", CLO_TYPE_INT, "-1", "eorbit");
182  clo_addOption(list, "median", CLO_TYPE_INT, "0", "median");
183  clo_addOption(list, "unit_wgt", CLO_TYPE_INT, "0", "unit_wgt");
184  clo_addOption(list, "composite_scheme", CLO_TYPE_STRING, NULL, "composite scheme (min/max)");
185  clo_addOption(list, "composite_prod", CLO_TYPE_STRING, NULL, "composite product fieldname");
186 
187  strcpy(tmpStr, "bin products\n [default=all products in L3 file]\n");
188  option = clo_addOption(list, "prod", CLO_TYPE_STRING, "DEFAULT", tmpStr);
189  clo_addOptionAlias(option, "out_parm");
190 
191  option = clo_addOption(list, "union_bins", CLO_TYPE_BOOL, "off", "Output file contains the union of input bins");
192  clo_addOptionAlias(option, "u");
193 
194  clo_addOption(list, "doi", CLO_TYPE_STRING, NULL, "Digital Object Identifier (DOI) string");
195 
197  return 0;
198 }
199 
201  char *tmp_str;
202  char keyword [50];
203  char tmp_file[FILENAME_MAX];
204  int numOptions, optionId;
205  clo_option_t *option;
206 
207  numOptions = clo_getNumOptions(list);
208  for (optionId = 0; optionId < numOptions; optionId++) {
209  option = clo_getOption(list, optionId);
210 
211  // ignore options of type CLO_TYPE_HELP
212  if (option->dataType == CLO_TYPE_HELP)
213  continue;
214 
215  strcpy(keyword, option->key);
216 
217  /* change keyword to lower case */
218  tmp_str = keyword;
219  while (*tmp_str != '\0') {
220  if (isupper(*tmp_str)) *tmp_str = tolower(*tmp_str);
221  tmp_str++;
222  }
223  if (strcmp(keyword, "help") == 0)
224  ;
225  else if (strcmp(keyword, "version") == 0)
226  ;
227  else if (strncmp(keyword, "dump_options", 12) == 0)
228  ;
229  else if (strncmp(keyword, "par", 3) == 0)
230  ;
231  else if (strcmp(keyword, "ifile") == 0) {
232  if (clo_isOptionSet(option)) {
233  parse_file_name(clo_getOptionString(option), tmp_file);
234  strcpy(input->infile, tmp_file);
235  }
236  } else if (strcmp(keyword, "ofile") == 0) {
237  if (clo_isOptionSet(option)) {
238  parse_file_name(clo_getOptionString(option), tmp_file);
239  strcpy(input->ofile, tmp_file);
240  }
241  } else if (strcmp(keyword, "pfile") == 0) {
242  if (clo_isOptionSet(option)) {
243  parse_file_name(clo_getOptionString(option), tmp_file);
244  strcpy(input->pfile, tmp_file);
245  }
246  } else if (strcmp(keyword, "pversion") == 0) {
247  strcpy(input->pversion, clo_getOptionString(option));
248 
249  } else if (strcmp(keyword, "syear") == 0) {
250  input->syear = clo_getOptionInt(option);
251 
252  } else if (strcmp(keyword, "eyear") == 0) {
253  input->eyear = clo_getOptionInt(option);
254 
255  } else if (strcmp(keyword, "sday") == 0) {
256  input->sday = clo_getOptionInt(option);
257 
258  } else if (strcmp(keyword, "eday") == 0) {
259  input->eday = clo_getOptionInt(option);
260 
261  } else if (strcmp(keyword, "orbit1") == 0) {
262  input->sorbit = clo_getOptionInt(option);
263 
264  } else if (strcmp(keyword, "orbit2") == 0) {
265  input->eorbit = clo_getOptionInt(option);
266 
267  } else if (strcmp(keyword, "prod") == 0) {
268  strcpy(input->out_parm, ":");
269  strcat(input->out_parm, clo_getOptionRawString(option));
270  strcat(input->out_parm, ":");
271 
272  } else if (strcmp(keyword, "reduce_fac") == 0) {
273  input->reduce_fac = clo_getOptionInt(option);
274 
275  } else if (strcmp(keyword, "resolve") == 0) {
276  if (clo_isOptionSet(option)) {
277  strcpy(input->resolve, clo_getOptionString(option));
278  }
279 
280  } else if (strcmp(keyword, "noext") == 0) {
281  input->noext = clo_getOptionBool(option);
282 
283  } else if (strcmp(keyword, "merged") == 0) {
284  if (clo_isOptionSet(option)) {
285  parse_file_name(clo_getOptionString(option), tmp_file);
286  strcpy(input->merged, tmp_file);
287  }
288  } else if (strcmp(keyword, "loneast") == 0) {
289  input->loneast = clo_getOptionFloat(option);
290 
291  } else if (strcmp(keyword, "lonwest") == 0) {
292  input->lonwest = clo_getOptionFloat(option);
293 
294  } else if (strcmp(keyword, "latnorth") == 0) {
295  input->latnorth = clo_getOptionFloat(option);
296 
297  } else if (strcmp(keyword, "latsouth") == 0) {
298  input->latsouth = clo_getOptionFloat(option);
299 
300  } else if (strcmp(keyword, "verbose") == 0) {
301  input->verbose = clo_getOptionBool(option);
302 
303  } else if (strcmp(keyword, "unit_wgt") == 0) {
304  input->unit_wgt = clo_getOptionInt(option);
305 
306  } else if (strcmp(keyword, "union_bins") == 0) {
307  input->union_bins = clo_getOptionBool(option);
308 
309  } else if (strcmp(keyword, "median") == 0) {
310  input->median = clo_getOptionInt(option);
311 
312  } else if (strcmp(keyword, "deflate") == 0) {
313  input->deflate = clo_getOptionInt(option);
314 
315  } else if (strcmp(keyword, "oformat") == 0) {
316  const char* tmpStr = getFileFormatName(clo_getOptionString(option));
317  strcpy(input->oformat, tmpStr);
318 
319  } else if (strcmp(keyword, "composite_prod") == 0) {
320  if (clo_isOptionSet(option)) {
321  parse_file_name(clo_getOptionString(option), tmp_file);
322  strcpy(input->composite_prod, tmp_file);
323  }
324  } else if (strcmp(keyword, "composite_scheme") == 0) {
325  if (clo_isOptionSet(option)) {
326  parse_file_name(clo_getOptionString(option), tmp_file);
327  strcpy(input->composite_scheme, tmp_file);
328  }
329  } else if (strcmp(keyword, "doi") == 0) {
330  if (clo_isOptionSet(option)) {
331  strcpy(input->doi, clo_getOptionString(option));
332  }
333  } else {
334  goto Invalid_return;
335 
336  }
337 
338  }
339  return 0;
340 
341 
342 Invalid_return:
343  printf("Invalid argument \"%s\"\n", keyword);
344  return -1;
345 }
346 
347 /*-----------------------------------------------------------------------------
348  Function: l3bin_input
349 
350  Returns: int (status)
351  The return code is a negative value if any error occurs, otherwise,
352  returns 0.
353 
354  Description:
355  Convert the arguments from the command line into a structure input
356  variable.
357 
358  Parameters: (in calling order)
359  Type Name I/O Description
360  ---- ---- --- -----------
361  int argc I number of arguments
362  char **argv I list of arguments
363  instr input O structure variable for inputs
364 
365 ----------------------------------------------------------------------------*/
366 
367 int l3bin_input(int argc, char **argv, instr *input, const char* prog, const char* version) {
368  char str_buf[4096];
369 
370  /* */
371  /* Set input values to defaults */
372  /* */
373  if (input_init(input) != 0) {
374  printf("-E- %s: Error initializing input structure.\n", __FILE__);
375  return (-1);
376  }
377 
378  /* hold all of the command line options */
380 
381  list = clo_createList();
382 
383  /* initialize the option list with descriptions and default values */
385 
386  if (argc == 1) {
388  exit(1);
389  }
390 
392 
393  // read command line args to get the ifile parameter
394  clo_readArgs(list, argc, argv);
395 
396  if (l3bin_load_input(list, input) != 0) {
397  printf("-E- %s: Error loading options into input structure.\n", __FILE__);
399  return (-1);
400  }
401 
403 
404  readFileList(input->infile);
405 
406  /* */
407  /* Build string of parameters for metadata */
408  /* */
409  sprintf(str_buf, "infile = %s\n", input->infile);
410  strcat(input->parms, str_buf);
411  sprintf(str_buf, "ofile = %s\n", input->ofile);
412  strcat(input->parms, str_buf);
413  sprintf(str_buf, "pfile = %s\n", input->ofile);
414  strcat(input->parms, str_buf);
415  sprintf(str_buf, "oformat = %s\n", input->oformat);
416  strcat(input->parms, str_buf);
417 
418  sprintf(str_buf, "syear = %d\n", input->syear);
419  strcat(input->parms, str_buf);
420  sprintf(str_buf, "eyear = %d\n", input->eyear);
421  strcat(input->parms, str_buf);
422 
423  sprintf(str_buf, "sday = %d\n", input->sday);
424  strcat(input->parms, str_buf);
425  sprintf(str_buf, "eday = %d\n", input->eday);
426  strcat(input->parms, str_buf);
427 
428  sprintf(str_buf, "sorbit = %d\n", input->sorbit);
429  strcat(input->parms, str_buf);
430  sprintf(str_buf, "eorbit = %d\n", input->eorbit);
431  strcat(input->parms, str_buf);
432 
433  sprintf(str_buf, "out_parm = %s\n", input->out_parm);
434  strcat(input->parms, str_buf);
435 
436  sprintf(str_buf, "processing_version = %s\n", input->pversion);
437  strcat(input->parms, str_buf);
438 
439  sprintf(str_buf, "reduce_fac = %d\n", input->reduce_fac);
440  strcat(input->parms, str_buf);
441 
442  sprintf(str_buf, "resolve = %s\n", input->resolve);
443  strcat(input->parms, str_buf);
444 
445  sprintf(str_buf, "merged = %s\n", input->merged);
446  strcat(input->parms, str_buf);
447 
448  sprintf(str_buf, "loneast = %f\n", input->loneast);
449  strcat(input->parms, str_buf);
450 
451  sprintf(str_buf, "lonwest = %f\n", input->lonwest);
452  strcat(input->parms, str_buf);
453 
454  sprintf(str_buf, "latnorth = %f\n", input->latnorth);
455  strcat(input->parms, str_buf);
456 
457  sprintf(str_buf, "latsouth = %f\n", input->latsouth);
458  strcat(input->parms, str_buf);
459 
460  sprintf(str_buf, "verbose = %d\n", input->verbose);
461  strcat(input->parms, str_buf);
462 
463  sprintf(str_buf, "unit_wgt = %d\n", input->unit_wgt);
464  strcat(input->parms, str_buf);
465 
466  sprintf(str_buf, "median = %d\n", input->median);
467  strcat(input->parms, str_buf);
468 
469  sprintf(str_buf, "deflate = %d\n", input->deflate);
470  strcat(input->parms, str_buf);
471 
472  sprintf(str_buf, "composite_prod = %s\n", input->composite_prod);
473  strcat(input->parms, str_buf);
474 
475  sprintf(str_buf, "composite_scheme = %s\n", input->composite_scheme);
476  strcat(input->parms, str_buf);
477 
478  sprintf(str_buf, "doi = %s\n", input->doi);
479  strcat(input->parms, str_buf);
480 
481  return 0;
482 }
clo_option_t * clo_addOption(clo_optionList_t *list, const char *key, enum clo_dataType_t dataType, const char *defaultVal, const char *desc)
Definition: clo.c:684
float clo_getOptionFloat(clo_option_t *option)
Definition: clo.c:1167
int l3bin_init_options(clo_optionList_t *list, const char *prog, const char *version)
void clo_readArgs(clo_optionList_t *list, int argc, char *argv[])
Definition: clo.c:2103
#define NULL
Definition: decode_rs.h:63
char * key
Definition: clo.h:107
void clo_addAlias(clo_optionList_t *list, const char *key, const char *alias)
Definition: clo.c:646
char * clo_getOptionRawString(clo_option_t *option)
Definition: clo.c:1030
int clo_getOptionInt(clo_option_t *option)
Definition: clo.c:1113
@ CLO_TYPE_FLOAT
Definition: clo.h:84
int clo_isOptionSet(clo_option_t *option)
Definition: clo.c:2257
@ CLO_TYPE_BOOL
Definition: clo.h:81
instr * input
int l3bin_load_input(clo_optionList_t *list, instr *input)
void clo_setSelectOptionKeys(char **keys)
Definition: clo.c:514
int l3bin_input(int argc, char **argv, instr *input, const char *prog, const char *version)
void clo_setEnableDumpOptions(int val)
Definition: clo.c:410
clo_optionList_t * clo_createList()
Definition: clo.c:532
list(APPEND LIBS ${NETCDF_LIBRARIES}) find_package(GSL REQUIRED) include_directories($
Definition: CMakeLists.txt:8
void clo_setHelpStr(const char *str)
Definition: clo.c:487
const char * getFileFormatName(const char *str)
@ CLO_TYPE_INT
Definition: clo.h:82
char * clo_getOptionString(clo_option_t *option)
Definition: clo.c:1050
void clo_printUsage(clo_optionList_t *list)
Definition: clo.c:1988
int input_init(instr *input_str)
Definition: l3bin_input.cpp:68
@ CLO_TYPE_IFILE
Definition: clo.h:87
@ CLO_TYPE_OFILE
Definition: clo.h:88
clo_option_t * clo_getOption(clo_optionList_t *list, int i)
Definition: clo.c:908
void clo_addOptionAlias(clo_option_t *option, const char *alias)
Definition: clo.c:632
int clo_getNumOptions(clo_optionList_t *list)
Definition: clo.c:1017
void parse_file_name(const char *inpath, char *outpath)
@ CLO_TYPE_HELP
Definition: clo.h:89
std::vector< std::string > readFileList(std::string fileName)
Definition: readFileList.cpp:9
void clo_setVersion(const char *str)
Definition: clo.c:448
void clo_deleteList(clo_optionList_t *list)
Definition: clo.c:875
enum clo_dataType_t dataType
Definition: clo.h:108
@ CLO_TYPE_STRING
Definition: clo.h:86
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
int clo_getOptionBool(clo_option_t *option)
Definition: clo.c:1087
version
Definition: setup.py:15