OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
l2binmatch_input.cpp
Go to the documentation of this file.
1 #include "l2binmatch_input.h"
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <assert.h>
8 #include <genutils.h>
9 #include <clo.h>
10 #include "version.h"
11 
12 // need a place to store the default product lists
13 static char default_l2prod[L1_PRODSTRLEN];
14 
15 static char default_flaguse[1024];
16 
18 
19  input = (instr *) allocateMemory(sizeof (instr), "input structure");
20 
21  // input->ifile[0] = '\0';
22  // input->ofile[0] = '\0';
23  strcpy(default_flaguse, "ATMFAIL,LAND,HIGLINT,HILT,HISATZEN,STRAYLIGHT,CLDICE,COCCOLITH,LOWLW,CHLFAIL,NAVWARN,ABSAER,MAXAERITER,ATMWARN,HISOLZEN,NAVFAIL");
24  strcpy(input->flaguse, default_flaguse);
25 
26  // input->l2prod [0] = '\0';
27 
28  input->vcal_depth = -1000;
29  input->vcal_min_nbin = 4;
30  input->vcal_min_nscene = 3;
31  input->subsamp = 1;
32  input->band_shift_opt = 0;
33  input->deflate = 0;
34  input->demfile[0] = '\0';
35 
36  l1_input_init();
37 
38  return 0;
39 }
40 
41 //-----------------------------------------------------------------------
42 
45  char tmpStr[2048];
46 
47  sprintf(tmpStr, "l2binmatch %d.%d.%d-%s (%s %s)", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, GITSHA, __DATE__, __TIME__);
48  clo_setVersion(tmpStr);
49 
50  sprintf(tmpStr, "Usage: l2binmatch argument-list\n\n");
51 
52  strcat(tmpStr, " This program matches pixels from a L2 file with bins from a L3 file\n");
53 
54  strcat(tmpStr, " The argument-list is a set of keyword=value pairs. The arguments can\n");
55  strcat(tmpStr, " be specified on the commandline, or put into a parameter file, or the\n");
56  strcat(tmpStr, " two methods can be used together, with commandline over-riding.\n\n");
57  strcat(tmpStr, "The list of valid keywords follows:\n");
58  clo_setHelpStr(tmpStr);
59 
60  clo_addOption(list, "ifile", CLO_TYPE_IFILE, NULL, "input L2 file name");
61  clo_addOption(list, "ofile", CLO_TYPE_OFILE, NULL, "output matchup file");
62  clo_addOption(list, "l2prod", CLO_TYPE_STRING, NULL, "products to be included in ofile");
63  clo_addOption(list, "flaguse", CLO_TYPE_STRING, default_flaguse, "flags to mask");
64 
65  strcpy(tmpStr, "output file format\n");
66  strcat(tmpStr, " netcdf4: output a netCDF version 4 file\n");
67  strcat(tmpStr, " hdf4: output a HDF version 4 file");
68  clo_addOption(list, "oformat", CLO_TYPE_STRING, "netCDF4", tmpStr);
69 
70  clo_addOption(list, "deflate", CLO_TYPE_INT, "0", "deflation level");
71  clo_addOption(list, "tgtfile", CLO_TYPE_IFILE, NULL, "L3 bin target file");
72  clo_addOption(list, "spixl", CLO_TYPE_INT, "1", "start pixel number");
73  clo_addOption(list, "epixl", CLO_TYPE_INT, "-1", "end pixel number (-1=the last pixel)");
74  clo_addOption(list, "dpixl", CLO_TYPE_INT, "1", "pixel sub-sampling interval");
75  clo_addOption(list, "sline", CLO_TYPE_INT, "1", "start line number");
76  clo_addOption(list, "eline", CLO_TYPE_INT, "-1", "end line number (-1=the last line)");
77  clo_addOption(list, "dline", CLO_TYPE_INT, "1", "line sub-sampling interval");
78  clo_addOption(list, "subsamp", CLO_TYPE_INT, "1", "valid pixel sub-sampling");
79 
80  strcpy(tmpStr, "bandshifting option \n");
81  strcat(tmpStr, " 1: apply bio-optical bandshift\n");
82  strcat(tmpStr, " 0: linear interpolation");
83  clo_addOption(list, "band_shift_opt", CLO_TYPE_INT, "0", tmpStr);
84 
85  strcpy(tmpStr, "depth to use to exclude data from target file\n");
86  strcat(tmpStr, " e.g. -1000 excludes depths less than 1000m\n");
87  clo_addOption(list, "vcal_depth", CLO_TYPE_FLOAT, "-1000.0", tmpStr);
88 
89  strcpy(tmpStr, "minimum # of samples in a bin for acceptance");
90  clo_addOption(list, "vcal_min_nbin", CLO_TYPE_INT, "4", tmpStr);
91 
92  strcpy(tmpStr, "minimum # of scenes in a bin for acceptance");
93  clo_addOption(list, "vcal_min_nscene", CLO_TYPE_INT, "3", tmpStr);
94 
95  clo_addOption(list, "demfile", CLO_TYPE_IFILE, "$OCDATAROOT/common/ETOPO1_ocssw.nc", "global elevation netCDF file");
96 
97  return 0;
98 }
99 
100 //-----------------------------------------------------------------------
101 
102 /*
103  Read the command line option and all of the default parameter files.
104 
105  This is the order for loading the options:
106  - load the main program defaults file
107  - load the command line (including specified par files)
108  - re-load the command line disabling file decending so command
109  line arguments will over ride
110 
111  */
112 int l2binmatch_read_options(clo_optionList_t* list, int argc, char* argv[]) {
113  char *dataRoot;
114  char tmpStr[FILENAME_MAX];
115  clo_option_t* option;
116  assert(list);
117 
118  if ((dataRoot = getenv("OCDATAROOT")) == NULL) {
119  fprintf(stderr, "-E- OCDATAROOT environment variable is not defined.\n");
120  return (-1);
121  }
122 
123  // load program defaults
124  sprintf(tmpStr, "%s/common/l2binmatch_defaults.par", dataRoot);
125  if (want_verbose) {
126  fprintf(stderr, "Loading default parameters from %s\n", tmpStr);
127  }
128  clo_readFile(list, tmpStr);
129  // set the default l2prod lists before the command line or par file
130  // is loaded
131  option = clo_findOption(list, "l2prod");
132  if (option && clo_isOptionSet(option))
133  strcpy(default_l2prod, option->valStr);
134 
135  clo_readArgs(list, argc, argv);
136 
137  return 0;
138 }
139 
140 //-----------------------------------------------------------------------------
141 
143  char tmp_file[FILENAME_MAX];
144  const char* tmpStr;
145  char *strVal;
146  clo_option_t *option;
147  int numOptions;
148  int optionId;
149  char keyword[FILENAME_MAX];
150  int count;
151  char **strArray;
152  int i;
153 
154  // first load up the default_l2prod
155  if (default_l2prod[0]) {
156  strcpy(input->def_l2prod[0], default_l2prod);
157  }
158 
159  numOptions = clo_getNumOptions(list);
160  for (optionId = 0; optionId < numOptions; optionId++) {
161  option = clo_getOption(list, optionId);
162  strcpy(keyword, option->key);
163 
164  /* change keyword to lower case */
165  strVal = keyword;
166  while (*strVal != '\0') {
167  *strVal = tolower(*strVal);
168  strVal++;
169  }
170 
171  if (strcmp(keyword, "help") == 0)
172  ;
173  else if (strcmp(keyword, "version") == 0)
174  ;
175  else if (strcmp(keyword, "dump_options") == 0)
176  ;
177  else if (strcmp(keyword, "dump_options_paramfile") == 0)
178  ;
179  else if (strcmp(keyword, "dump_options_xmlfile") == 0)
180  ;
181  else if (strcmp(keyword, "par") == 0)
182  ;
183  else if (strcmp(keyword, "ifile") == 0) {
184  strVal = clo_getOptionString(option);
185  parse_file_name(strVal, tmp_file);
186  strcpy(input->ifile[0], tmp_file);
187 
188  } else if (strcmp(keyword, "ofile") == 0) {
189  if (clo_isOptionSet(option)) {
190  strVal = clo_getOptionString(option);
191  parse_file_name(strVal, tmp_file);
192  strcpy(input->ofile[0], tmp_file);
193  }
194 
195  } else if (strcmp(keyword, "oformat") == 0) {
196  strVal = clo_getOptionString(option);
197  tmpStr = getFileFormatName(strVal);
198  if (tmpStr == NULL) {
199  printf("-E- l2binmatch_load_input: oformat=%s is not a recognized file format\n",
200  strVal);
201  return -1;
202  }
203  strcpy(input->oformat, tmpStr);
204 
205  } else if (strcmp(keyword, "flaguse") == 0) {
206  strArray = clo_getOptionStrings(option, &count);
207  input->flaguse[0] = '\0';
208  for (i = 0; i < count; i++) {
209  if (i != 0)
210  strcat(input->flaguse, ",");
211  strcat(input->flaguse, strArray[i]);
212  }
213 
214  } else if (strcmp(keyword, "l2prod") == 0) {
215  strArray = clo_getOptionStrings(option, &count);
216  input->l2prod[0][0] = '\0';
217  for (i = 0; i < count; i++) {
218  if (i != 0)
219  strcat(input->l2prod[0], " ");
220  strcat(input->l2prod[0], strArray[i]);
221  }
222 
223  } else if (strcmp(keyword, "deflate") == 0) {
224  input->deflate = clo_getOptionInt(option);
225 
226  } else if (strcmp(keyword, "spixl") == 0) {
227  l1_input->spixl = clo_getOptionInt(option);
228 
229  } else if (strcmp(keyword, "epixl") == 0) {
230  l1_input->epixl = clo_getOptionInt(option);
231 
232  } else if (strcmp(keyword, "dpixl") == 0) {
233  l1_input->dpixl = clo_getOptionInt(option);
234 
235  } else if (strcmp(keyword, "sline") == 0) {
236  l1_input->sline = clo_getOptionInt(option);
237 
238  } else if (strcmp(keyword, "eline") == 0) {
239  l1_input->eline = clo_getOptionInt(option);
240 
241  } else if (strcmp(keyword, "dline") == 0) {
242  l1_input->dline = clo_getOptionInt(option);
243 
244  } else if (strcmp(keyword, "subsamp") == 0) {
245  input->subsamp = clo_getOptionInt(option);
246 
247  } else if (strcmp(keyword, "band_shift_opt") == 0) {
248  input->band_shift_opt = clo_getOptionInt(option);
249 
250  } else if (strcmp(keyword, "tgtfile") == 0) {
251  if (clo_isOptionSet(option)) {
252  strVal = clo_getOptionString(option);
253  parse_file_name(strVal, tmp_file);
254  strcpy(input->tgtfile, tmp_file);
255  }
256 
257  } else if (strcmp(keyword, "vcal_depth") == 0) {
258  input->vcal_depth = clo_getOptionFloat(option);
259 
260  } else if (strcmp(keyword, "vcal_min_nbin") == 0) {
261  input->vcal_min_nbin = clo_getOptionInt(option);
262 
263  } else if (strcmp(keyword, "vcal_min_nscene") == 0) {
264  input->vcal_min_nscene = clo_getOptionInt(option);
265 
266  } else if (strcmp(keyword, "demfile") == 0) {
267  strVal = clo_getOptionString(option);
268  parse_file_name(strVal, tmp_file);
269  strcpy(input->demfile, tmp_file);
270 
271  } else {
272  fprintf(stderr, "Invalid argument \"%s\"\n", keyword);
273  return -1;
274  }
275  } // for optionId
276 
277  return 0;
278 }
279 
280 /*-----------------------------------------------------------------------------
281  Function: msmapl2_input
282 
283  Returns: int (status)
284  The return code is a negative value if any error occurs, otherwise,
285  returns 0.
286 
287  Description:
288  Convert the arguments from the command line into a structure input
289  variable.
290 
291  Parameters: (in calling order)
292  Type Name I/O Description
293  ---- ---- --- -----------
294  int argc I number of arguments
295  char **argv I list of arguments
296  instr input O structure variable for inputs
297 
298 ----------------------------------------------------------------------------*/
299 
300 int l2binmatch_input(int argc, char **argv, clo_optionList_t* list) {
301  char str_buf[4096];
302 
303 
304  /* initialize the option list with descriptions and default values */
306 
307  /* */
308  /* Set input values to defaults */
309  /* */
310  if (l2binmatch_input_init() != 0) {
311  fprintf(stderr, "-E- %s: Error initializing input structure.\n", __FILE__);
312  return (-1);
313  }
314 
315  /* read the command line options into list */
316  if (l2binmatch_read_options(list, argc, argv) != 0) {
317  fprintf(stderr, "-E- %s: Error reading program options.\n", __FILE__);
318  return (-1);
319  }
320 
321  /* load options from list into input structure */
322  if (l2binmatch_load_input(list) != 0) {
323  fprintf(stderr, "-E- %s: Error loading options into input structure.\n", __FILE__);
324  return (-1);
325  }
326 
327  /* */
328  /* Build string of parameters for metadata */
329  /* */
330 
331  strcat(l1_input->input_parms, "\n");
332  sprintf(str_buf, "ifile = %s ", input->ifile[0]);
333  strcat(l1_input->input_parms, str_buf);
334  strcat(l1_input->input_parms, "\n");
335 
336  sprintf(str_buf, "ofile = %s", input->ofile[0]);
337  strcat(l1_input->input_parms, str_buf);
338  strcat(l1_input->input_parms, "\n");
339 
340  sprintf(str_buf, "l2prod = %s", input->l2prod[0]);
341  strcat(l1_input->input_parms, str_buf);
342  strcat(l1_input->input_parms, "\n");
343 
344  sprintf(str_buf, "oformat = %s", input->oformat);
345  strcat(l1_input->input_parms, str_buf);
346  strcat(l1_input->input_parms, "\n");
347 
348  sprintf(str_buf, "deflate = %5d", input->deflate);
349  strcat(l1_input->input_parms, str_buf);
350  strcat(l1_input->input_parms, "\n");
351 
352  sprintf(str_buf, "spixl = %5d", l1_input->spixl);
353  strcat(l1_input->input_parms, str_buf);
354  strcat(l1_input->input_parms, "\n");
355 
356  sprintf(str_buf, "epixl = %5d", l1_input->epixl);
357  strcat(l1_input->input_parms, str_buf);
358  strcat(l1_input->input_parms, "\n");
359 
360  sprintf(str_buf, "dpixl = %5d", l1_input->dpixl);
361  strcat(l1_input->input_parms, str_buf);
362  strcat(l1_input->input_parms, "\n");
363 
364  sprintf(str_buf, "sline = %5d", l1_input->sline);
365  strcat(l1_input->input_parms, str_buf);
366  strcat(l1_input->input_parms, "\n");
367 
368  sprintf(str_buf, "eline = %5d", l1_input->eline);
369  strcat(l1_input->input_parms, str_buf);
370  strcat(l1_input->input_parms, "\n");
371 
372  sprintf(str_buf, "dline = %5d", l1_input->dline);
373  strcat(l1_input->input_parms, str_buf);
374  strcat(l1_input->input_parms, "\n");
375 
376  sprintf(str_buf, "subsamp = %5d", input->subsamp);
377  strcat(l1_input->input_parms, str_buf);
378  strcat(l1_input->input_parms, "\n");
379 
380  sprintf(str_buf, "band_shift_opt = %3d", input->band_shift_opt);
381  strcat(l1_input->input_parms, str_buf);
382  strcat(l1_input->input_parms, "\n");
383 
384  sprintf(str_buf, "vcal_depth = %8.4f", input->vcal_depth);
385  strcat(l1_input->input_parms, str_buf);
386  strcat(l1_input->input_parms, "\n");
387 
388  sprintf(str_buf, "vcal_min_nbin = %d", input->vcal_min_nbin);
389  strcat(l1_input->input_parms, str_buf);
390  strcat(l1_input->input_parms, "\n");
391 
392  sprintf(str_buf, "vcal_min_nscene = %d", input->vcal_min_nscene);
393  strcat(l1_input->input_parms, str_buf);
394  strcat(l1_input->input_parms, "\n");
395 
396  sprintf(str_buf, "demfile = %s", input->demfile);
397  strcat(l1_input->input_parms, str_buf);
398  strcat(l1_input->input_parms, "\n");
399 
400  return 0;
401 }
void l1_input_init()
Definition: l1_options.c:11
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
int l2binmatch_read_options(clo_optionList_t *list, int argc, char *argv[])
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
int l2binmatch_input(int argc, char **argv, clo_optionList_t *list)
void * allocateMemory(size_t numBytes, const char *name)
Definition: allocateMemory.c:7
#define VERSION_MINOR
Definition: version.h:2
#define NULL
Definition: decode_rs.h:63
char * key
Definition: clo.h:104
char ** clo_getOptionStrings(clo_option_t *option, int *count)
Definition: clo.c:1226
#define GITSHA
Definition: version.h:4
clo_option_t * clo_findOption(clo_optionList_t *list, const char *key)
Definition: clo.c:967
#define VERSION_PATCH
Definition: version.h:3
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
#define VERSION_MAJOR
Definition: version.h:1
instr * input
void clo_setHelpStr(const char *str)
Definition: clo.c:487
const char * getFileFormatName(const char *str)
char * valStr
Definition: clo.h:108
@ CLO_TYPE_INT
Definition: clo.h:79
char * clo_getOptionString(clo_option_t *option)
Definition: clo.c:1050
int l2binmatch_load_input(clo_optionList_t *list)
l1_input_t * l1_input
Definition: l1_options.c:9
#define L1_PRODSTRLEN
Definition: filehandle.h:19
int want_verbose
@ 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_readFile(clo_optionList_t *list, const char *fileName)
Definition: clo.c:2210
int clo_getNumOptions(clo_optionList_t *list)
Definition: clo.c:1017
void parse_file_name(const char *inpath, char *outpath)
int l2binmatch_input_init()
void clo_setVersion(const char *str)
Definition: clo.c:448
int i
Definition: decode_rs.h:71
@ 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 l2binmatch_init_options(clo_optionList_t *list)
int count
Definition: decode_rs.h:79