OB.DAAC Logo
NASA Logo
Ocean Color Science Software

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