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