OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
l3bindump_input.c
Go to the documentation of this file.
1 /*
2  * l3bindump_input.c
3  *
4  * Created on: Mar 14, 2014
5  * Author: Sean Bailey
6  */
7 
8 #include "l3bindump.h"
9 #include <string.h>
10 #include <stdlib.h>
11 
14  char tmpStr[2048];
15 
17 
18  char softwareVersion[200];
19  sprintf(softwareVersion, "%d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR,
21 
22  clo_setVersion2("l3bindump", softwareVersion);
23  // clo_addXmlProgramMetadata("progressRegex", "Processing scan .+?\\((\\d+) of (\\d+)\\)");
24 
25  sprintf(tmpStr, "Usage: l3bindump argument-list\n\n");
26  strcat(tmpStr, " The argument-list is a set of keyword=value pairs. The arguments can\n");
27  strcat(tmpStr, " be specified on the command line, or put into a parameter file, or the\n");
28  strcat(tmpStr, " two methods can be used together, with command line overriding.\n\n");
29  strcat(tmpStr, " return value: 0=OK, 1=error, 110=requested bin(s) not found\n");
30  strcat(tmpStr, " file data.\n\n");
31  strcat(tmpStr, " There are 3 use cases:\n");
32  strcat(tmpStr, " \t1) bin_number\n");
33  strcat(tmpStr, " \t2) region defined by lat, lon, and radius (in km)\n");
34  strcat(tmpStr, " \t3) region defined by north, south, west, east\n");
35 
36  strcat(tmpStr, "The list of valid keywords follows:\n");
37  clo_setHelpStr(tmpStr);
38 
39  clo_addOption(list, "ifile", CLO_TYPE_IFILE, NULL, "input L3 bin file name");
40  clo_addOption(list, "ofile", CLO_TYPE_OFILE, NULL, "output file name");
41 
42  strcpy(tmpStr, "output file format\n");
43  strcat(tmpStr, " txt: plain text columnar format\n");
44  strcat(tmpStr, " seabass: SeaBASS format");
45  clo_addOption(list, "oformat", CLO_TYPE_STRING, "txt", tmpStr);
46 
47  clo_addOption(list, "l3bprod", CLO_TYPE_STRING, "Unspecified", "binned product to extract");
48  clo_addOption(list, "bin_number", CLO_TYPE_INT64, "-1", "bin number");
49  clo_addOption(list, "north", CLO_TYPE_FLOAT, "-999", "north boundary");
50  clo_addOption(list, "south", CLO_TYPE_FLOAT, "-999", "south boundary");
51  clo_addOption(list, "east", CLO_TYPE_FLOAT, "-999", "east boundary");
52  clo_addOption(list, "west", CLO_TYPE_FLOAT, "-999", "west boundary");
53  clo_addOption(list, "lat", CLO_TYPE_FLOAT, "-999", "latitude");
54  clo_addOption(list, "lon", CLO_TYPE_FLOAT, "-999", "longitude");
55  clo_addOption(list, "radius", CLO_TYPE_FLOAT, "-999", "radius in km");
56  clo_addOption(list, "verbose", CLO_TYPE_BOOL, "0", "verbose output");
57 
58  return 0;
59 }
60 
62  const char* tmpStr;
63  char tmp_file[FILENAME_MAX];
64  char *strVal;
65  clo_option_t *option;
66  int numOptions;
67  int optionId;
68  char keyword[FILENAME_MAX];
69  int count;
70  char **strArray;
71  int i;
72  numOptions = clo_getNumOptions(list);
73  for (optionId = 0; optionId < numOptions; optionId++) {
74  option = clo_getOption(list, optionId);
75 
76  // ignore options of type CLO_TYPE_HELP
77  if (option->dataType == CLO_TYPE_HELP)
78  continue;
79 
80  strcpy(keyword, option->key);
81 
82  /* change keyword to lower case */
83  strVal = keyword;
84  while (*strVal != '\0') {
85  *strVal = tolower(*strVal);
86  strVal++;
87  }
88 
89  if (strcmp(keyword, "help") == 0)
90  ;
91  else if (strcmp(keyword, "version") == 0)
92  ;
93  else if (strncmp(keyword, "dump_options", 12) == 0)
94  ;
95  else if (strcmp(keyword, "par") == 0)
96  ;
97  else if (strcmp(keyword, "ifile") == 0) {
98  strVal = clo_getOptionString(option);
99  parse_file_name(strVal, tmp_file);
100  strcpy(input->ifile, tmp_file);
101 
102  } else if (strcmp(keyword, "ofile") == 0) {
103  if (clo_isOptionSet(option)) {
104  strVal = clo_getOptionString(option);
105  parse_file_name(strVal, tmp_file);
106  strcpy(input->ofile, tmp_file);
107  }
108  } else if (strcmp(keyword, "oformat") == 0) {
109  strVal = clo_getOptionString(option);
110  tmpStr = getFileFormatName(strVal);
111  if (tmpStr == NULL) {
112  printf("-E- l2gen_load_input: oformat=%s is not a recognized file format\n",
113  strVal);
114  return -1;
115  }
116  strcpy(input->oformat, tmpStr);
117 
118  } else if (strcmp(keyword, "l3bprod") == 0) {
119  strArray = clo_getOptionStrings(option, &count);
120  input->l3bprod[0] = '\0';
121  for (i = 0; i < count; i++) {
122  if (i != 0)
123  strcat(input->l3bprod, " ");
124  strcat(input->l3bprod, strArray[i]);
125  }
126  } else if (strcmp(keyword, "west") == 0) {
127  input->west = clo_getOptionFloat(option);
128  } else if (strcmp(keyword, "east") == 0) {
129  input->east = clo_getOptionFloat(option);
130  } else if (strcmp(keyword, "north") == 0) {
131  input->north = clo_getOptionFloat(option);
132  } else if (strcmp(keyword, "south") == 0) {
133  input->south = clo_getOptionFloat(option);
134  } else if (strcmp(keyword, "lat") == 0) {
135  input->lat = clo_getOptionFloat(option);
136  } else if (strcmp(keyword, "lon") == 0) {
137  input->lon = clo_getOptionFloat(option);
138  } else if (strcmp(keyword, "bin_number") == 0) {
139  input->bin_number = clo_getOptionInt64(option);
140  } else if (strcmp(keyword, "radius") == 0) {
141  input->radius = clo_getOptionFloat(option);
142  } else if (strcmp(keyword, "verbose") == 0) {
143  input->verbose = clo_getOptionBool(option);
144  } else {
145  printf("-E- Invalid argument \"%s\"\n", keyword);
146  clo_dumpOption(option);
147  exit(1);
148  }
149 
150  } // for optionIDs
151 
152  /*
153  * check for valid use cases
154  */
155  if (input->bin_number >= 0 && (input->radius != -999 ||
156  input->lat != -999 || input->lon != -999 ||
157  input->north != -999 || input->south != -999 ||
158  input->west != -999 || input->east != -999)) {
159  printf("-E- Invalid argument set: bin_number cannot be provided if lon/lat/radius or NSWE are provided \n");
160  clo_dumpOption(option);
161  exit(1);
162  }
163  if (input->radius != -999 && (input->lat == -999 || input->lon == -999)) {
164  printf("-E- Invalid argument set: radius reqiures lon/lat to be provided\n");
165  clo_dumpOption(option);
166  exit(1);
167  }
168  if (input->lat != -999 && input->lon == -999) {
169  printf("-E- Invalid argument set: if lat is set, lon must be provided\n");
170  clo_dumpOption(option);
171  exit(1);
172  }
173  if (input->lat == -999 && input->lon != -999) {
174  printf("-E- Invalid argument set: if lon is set, lat must be provided\n");
175  clo_dumpOption(option);
176  exit(1);
177  }
178  if ((input->radius != -999 || input->lat != -999 || input->lon != -999) &&
179  (input->north != -999 || input->south != -999 ||
180  input->west != -999 || input->east != -999)) {
181  printf("-E- Choose either lon/lat/radius or NSWE\n");
182  clo_dumpOption(option);
183  exit(1);
184  }
185 
186  return 0;
187 }
188 
189 
190 //-----------------------------------------------------------------------
191 
192 /*
193  Read the command line option and all of the default parameter files.
194 
195  This is the order for loading the options:
196  - read the command line to get the ifile and suite options
197  - load the main program defaults file
198  - load the command line (including specified par files)
199  - re-load the command line so they take precedence
200 
201  */
202 int l3bindump_read_options(clo_optionList_t* list, int argc, char* argv[]) {
203  char *dataRoot;
204  char tmpStr[FILENAME_MAX];
205  char progName[] = "l3bindump";
206 
207  assert(list);
208 
209  if ((dataRoot = getenv("OCDATAROOT")) == NULL) {
210  printf("-E- OCDATAROOT environment variable is not defined.\n");
211  return (-1);
212  }
213 
214  // disable the dump option until we have read all of the files
216  clo_readArgs(list, argc, argv);
217 
218  // load program defaults
219  sprintf(tmpStr, "%s/common/%s_defaults.par", dataRoot, progName);
220  if (want_verbose)
221  printf("Loading default parameters from %s\n", tmpStr);
222  clo_readFile(list, tmpStr);
223 
224  // re-load the command line and par file
225  if (want_verbose)
226  printf("Loading command line parameters\n\n");
227  // enable the dump option the last time through
229  clo_readArgs(list, argc, argv);
230 
231  return 0;
232 }
233 
234 void l3bindump_input_init(instr *input) {
235  /* */
236  /* Set input values to defaults */
237  /* */
238 
239  input->ifile[0] = '\0';
240  input->ofile [0] = '\0';
241  input->oformat[0] = '\0';
242  input->l3bprod[0] = '\0';
243 
244  input->west = -999;
245  input->east = -999;
246  input->north = -999;
247  input->south = -999;
248  input->lat = -999;
249  input->lon = -999;
250  input->bin_number = -1;
251  input->radius = -999;
252  input->verbose = 0;
253 
254  return;
255 }
256 
257 int l3bindump_usage(char *prog) {
259 
260  list = clo_createList();
263 
264  return 0;
265 }
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 l3bindump_load_input(clo_optionList_t *list, instr *input)
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 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
#define VERSION_PATCH
Definition: version.h:3
@ 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
#define VERSION_MAJOR
Definition: version.h:1
void clo_setVersion2(const char *programName, const char *versionStr)
Definition: clo.c:464
instr * input
void clo_setSelectOptionKeys(char **keys)
Definition: clo.c:514
@ CLO_TYPE_INT64
Definition: clo.h:80
int64_t clo_getOptionInt64(clo_option_t *option)
Definition: clo.c:1140
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)
char * clo_getOptionString(clo_option_t *option)
Definition: clo.c:1050
void clo_printUsage(clo_optionList_t *list)
Definition: clo.c:1988
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
int l3bindump_usage(char *prog)
void parse_file_name(const char *inpath, char *outpath)
@ CLO_TYPE_HELP
Definition: clo.h:86
void clo_dumpOption(clo_option_t *option)
Definition: clo.c:1896
void l3bindump_input_init(instr *input)
enum clo_dataType_t dataType
Definition: clo.h:105
int l3bindump_read_options(clo_optionList_t *list, int argc, char *argv[])
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 l3bindump_init_options(clo_optionList_t *list)
int clo_getOptionBool(clo_option_t *option)
Definition: clo.c:1087
int count
Definition: decode_rs.h:79