OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
l2mapgen_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 <assert.h>
6 #include <genutils.h>
7 #include <clo.h>
8 #include "l2mapgen.h"
9 #include "l2mapgen_input.h"
10 
11 int input_init(instr *input_str) {
12  input_str->ifile[0] = '\0';
13  input_str->ofile[0] = '\0';
14  input_str->palfile[0] = '\0';
15  input_str->palette_dir[0] = '\0';
16  input_str->product_table[0] = '\0';
17  input_str->flaguse[0] = '\0';
18  input_str->parms[0] = '\0';
19 
20  input_str->prod [0] = '\0';
21  input_str->stype = 0;
22  input_str->datamin = 0.0;
23  input_str->datamax = 0.0;
24 
25  input_str->west = 0.0;
26  input_str->east = 0.0;
27  input_str->south = 0.0;
28  input_str->north = 0.0;
29  input_str->width = 800;
30  input_str->threshold = 5.;
31  input_str->mask = 0;
32  input_str->quality = 2;
33  input_str->apply_pal = 0;
34  input_str->outmode = 1;
35 
36  return 0;
37 }
38 
39 //-----------------------------------------------------------------------
40 
43  char tmpStr[2048];
44 
45  sprintf(tmpStr, "l2mapgen %s (%s %s)", VERSION, __DATE__, __TIME__);
46  clo_setVersion(tmpStr);
47 
48  sprintf(tmpStr, "Usage: l2mapgen argument-list\n\n");
49 
50  strcat(tmpStr, " This program takes a product from a L2 file, maps it using a Plate\n");
51  strcat(tmpStr, " Carree cylindrical projection, and produces a gray scale PGM or\n");
52  strcat(tmpStr, " color PPM file.\n\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 or file with a list of files names");
61  clo_addOption(list, "ofile", CLO_TYPE_OFILE, NULL, "output map filename (NULL=STDOUT)");
62  clo_addOption(list, "prod", CLO_TYPE_STRING, NULL, "product name");
63  clo_addOption(list, "apply_pal", CLO_TYPE_BOOL, "false", "apply color palette, false = grayscale");
64  clo_addOption(list, "palfile", CLO_TYPE_IFILE, "default", "palette filename");
65  clo_addOption(list, "palette_dir", CLO_TYPE_IFILE, "$OCDATAROOT/common/palette", "palette directory");
66  clo_addOption(list, "product_table", CLO_TYPE_IFILE, "$OCDATAROOT/common/smigen_product_table.dat", "product table");
67  clo_addOption(list, "flaguse", CLO_TYPE_STRING, NULL, "flags to be masked");
68  clo_addOption(list, "quality", CLO_TYPE_INT, "2", "minimum allowable quality level for SST. Valid\n only for SST and only if qual_sst or qual_sst4 SDS exist");
69  clo_addOption(list, "mask", CLO_TYPE_BOOL, "no", "apply mask to land, cloud and glint (see below)");
70  clo_addOption(list, "datamin", CLO_TYPE_FLOAT, "0.0", "minimum value for data scaling\n (default see SMI product table)");
71  clo_addOption(list, "datamax", CLO_TYPE_FLOAT, "0.0", "maximum value for data scaling\n (default see SMI product table)");
72  clo_addOption(list, "stype", CLO_TYPE_INT, "0", "scaling type (default see SMI product table)\n 1: LINEAR\n 2: LOG");
73  clo_addOption(list, "east", CLO_TYPE_FLOAT, "0.0", "Map East longitude\n (default=scene(s) Easternmost Longitude)");
74  clo_addOption(list, "west", CLO_TYPE_FLOAT, "0.0", "Map West longitude\n (default=scene(s) Westernmost Longitude)");
75  clo_addOption(list, "north", CLO_TYPE_FLOAT, "0.0", "Map North latitude\n (default=scene(s) Northernmost Longitude)");
76  clo_addOption(list, "south", CLO_TYPE_FLOAT, "0.0", "Map South latitude\n (default=scene(s) Southernmost Longitude)");
77  clo_addOption(list, "width", CLO_TYPE_INT, "800", "width of the output image");
78  clo_addOption(list, "threshold", CLO_TYPE_FLOAT, "5", "minimum percentage of the area of interest\n that must receive valid pixel data before an image is generated");
79  strcpy(tmpStr, "format of the output file\n");
80  strcat(tmpStr, " ppm: PPM or PGM image file (alias 1)\n");
81  strcat(tmpStr, " png: PNG color or grayscale image file (alias 2)\n");
82  strcat(tmpStr, " tiff: TIFF color or grayscale geo tiff image file (alias 3)\n");
83  clo_addOption(list, "outmode", CLO_TYPE_STRING, "ppm", tmpStr);
84 
85  strcpy(tmpStr, "\n If the \"mask\" option is set, the output PGM image will be masked for\n");
86  strcat(tmpStr, " flags defined in the flaguse parameter. The \"no data\" pixel value will\n");
87  strcat(tmpStr, " change from 0 to 255, and pixel values 252, 253, and 254 will represent the\n");
88  strcat(tmpStr, " sunglint, land, and all other (e.g. clouds/ice,hilt,atmfail,navfail,chlfail)\n");
89  strcat(tmpStr, " masks, respectively. NOTE: sunglint is NOT masked by default, but if it is\n");
90  strcat(tmpStr, " added to the flaguse parameter, it will be distinguished in the masking as\n");
91  strcat(tmpStr, " medium gray. If a palette is applied and the mask option is set, the\n");
92  strcat(tmpStr, " palette values will be modified:\n");
93  strcat(tmpStr, " Value R G B\n");
94  strcat(tmpStr, " 252 128 128 128\n");
95  strcat(tmpStr, " 253 160 82 45\n");
96  strcat(tmpStr, " 254 255 255 255\n");
97  strcat(tmpStr, " 255 0 0 0\n\n");
98  strcat(tmpStr, " By default, this program sends its results to standard output as a\n");
99  strcat(tmpStr, " PGM-formatted binary data stream. Save it to a file via \">\" or pipe it\n");
100  strcat(tmpStr, " to your favorite image display program. The output image is rendered in\n");
101  strcat(tmpStr, " a Plate Carree projection.");
102  clo_addOption(list, "help1", CLO_TYPE_HELP, NULL, tmpStr);
103 
104  return 0;
105 }
106 
107 //-----------------------------------------------------------------------
108 
109 /*
110  Read the command line option and all of the default parameter files.
111 
112  This is the order for loading the options:
113  - load the main program defaults file
114  - load the command line (including specified par files)
115  - re-load the command line disabling file decending so command
116  line arguments will over ride
117 
118  */
119 int l2mapgen_read_options(clo_optionList_t* list, int argc, char* argv[]) {
120  char *dataRoot;
121  char tmpStr[FILENAME_MAX];
122 
123  assert(list);
124 
125  if ((dataRoot = getenv("OCDATAROOT")) == NULL) {
126  fprintf(stderr, "-E- OCDATAROOT environment variable is not defined.\n");
127  return (-1);
128  }
129 
130  // load program defaults
131  sprintf(tmpStr, "%s/common/l2mapgen_defaults.par", dataRoot);
132  if (want_verbose) {
133  fprintf(stderr, "Loading default parameters from %s\n", tmpStr);
134  }
135  clo_readFile(list, tmpStr);
136  clo_readArgs(list, argc, argv);
137 
138  return 0;
139 }
140 
141 //-----------------------------------------------------------------------------
142 
144  char tmp_file[FILENAME_MAX];
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  numOptions = clo_getNumOptions(list);
155  for (optionId = 0; optionId < numOptions; optionId++) {
156  option = clo_getOption(list, optionId);
157  strcpy(keyword, option->key);
158 
159  /* change keyword to lower case */
160  strVal = keyword;
161  while (*strVal != '\0') {
162  *strVal = tolower(*strVal);
163  strVal++;
164  }
165 
166  if (strcmp(keyword, "help") == 0)
167  ;
168  else if (strcmp(keyword, "version") == 0)
169  ;
170  else if (strcmp(keyword, "dump_options") == 0)
171  ;
172  else if (strcmp(keyword, "dump_options_paramfile") == 0)
173  ;
174  else if (strcmp(keyword, "dump_options_xmlfile") == 0)
175  ;
176  else if (strcmp(keyword, "help1") == 0)
177  ;
178  else if (strcmp(keyword, "par") == 0)
179  ;
180  else if (strcmp(keyword, "ifile") == 0) {
181  strVal = clo_getOptionString(option);
182  parse_file_name(strVal, tmp_file);
183  strcpy(input->ifile, tmp_file);
184 
185  } else if (strcmp(keyword, "ofile") == 0) {
186  if (clo_isOptionSet(option)) {
187  strVal = clo_getOptionString(option);
188  parse_file_name(strVal, tmp_file);
189  strcpy(input->ofile, tmp_file);
190  }
191 
192  } else if (strcmp(keyword, "palfile") == 0) {
193  strVal = clo_getOptionString(option);
194  parse_file_name(strVal, tmp_file);
195  strcpy(input->palfile, tmp_file);
196 
197  } else if (strcmp(keyword, "palette_dir") == 0) {
198  strVal = clo_getOptionString(option);
199  parse_file_name(strVal, tmp_file);
200  strcpy(input->palette_dir, tmp_file);
201 
202  } else if (strcmp(keyword, "product_table") == 0) {
203  strVal = clo_getOptionString(option);
204  parse_file_name(strVal, tmp_file);
205  strcpy(input->product_table, tmp_file);
206 
207  } else if (strcmp(keyword, "flaguse") == 0) {
208  if (clo_isOptionSet(option)) {
209  strArray = clo_getOptionStrings(option, &count);
210  input->flaguse[0] = '\0';
211  for (i = 0; i < count; i++) {
212  if (strcasecmp("default", strArray[i]) == 0) {
213  if (input->flaguse[0] != '\0')
214  strcat(input->flaguse, ",");
215  strcat(input->flaguse, DEF_FLAG);
216  } else {
217  if (input->flaguse[0] != '\0')
218  strcat(input->flaguse, ",");
219  strcat(input->flaguse, strArray[i]);
220  }
221 
222  } // for count
223  } // if option set
224 
225  } else if (strcmp(keyword, "prod") == 0) {
226  strVal = clo_getOptionString(option);
227  parse_file_name(strVal, tmp_file);
228  strcpy(input->prod, tmp_file);
229 
230  } else if (strcmp(keyword, "stype") == 0) {
231  input->stype = clo_getOptionInt(option);
232 
233  } else if (strcmp(keyword, "width") == 0) {
234  input->width = clo_getOptionInt(option);
235 
236  } else if (strcmp(keyword, "threshold") == 0) {
237  input->threshold = clo_getOptionFloat(option);
238 
239  } else if (strcmp(keyword, "apply_pal") == 0) {
240  input->apply_pal = clo_getOptionBool(option);
241 
242  } else if (strcmp(keyword, "mask") == 0) {
243  input->mask = clo_getOptionBool(option);
244 
245  } else if (strcmp(keyword, "quality") == 0) {
246  input->quality = clo_getOptionInt(option);
247 
248  } else if (strcmp(keyword, "datamin") == 0) {
249  input->datamin = clo_getOptionFloat(option);
250 
251  } else if (strcmp(keyword, "datamax") == 0) {
252  input->datamax = clo_getOptionFloat(option);
253 
254  } else if (strcmp(keyword, "west") == 0) {
255  input->west = clo_getOptionFloat(option);
256 
257  } else if (strcmp(keyword, "east") == 0) {
258  input->east = clo_getOptionFloat(option);
259 
260  } else if (strcmp(keyword, "south") == 0) {
261  input->south = clo_getOptionFloat(option);
262 
263  } else if (strcmp(keyword, "north") == 0) {
264  input->north = clo_getOptionFloat(option);
265 
266  } else if (strcmp(keyword, "outmode") == 0) {
267  char *str = clo_getOptionString(option);
268  if (strcmp("1", str) == 0) {
269  input->outmode = 1;
270  } else if (strcasecmp("ppm", str) == 0) {
271  input->outmode = 1;
272  } else if (strcmp("2", str) == 0) {
273  input->outmode = 2;
274  } else if (strcasecmp("png", str) == 0) {
275  input->outmode = 2;
276  } else if (strcmp("3", str) == 0) {
277  input->outmode = 3;
278  } else if (strcasecmp("tiff", str) == 0) {
279  input->outmode = 3;
280  } else {
281  fprintf(stderr, "Invalid value for outmode \"%s\"\n", str);
282  return -1;
283  }
284 
285  } else {
286  fprintf(stderr, "Invalid argument \"%s\"\n", keyword);
287  return -1;
288  }
289  } // for optionId
290 
291  return 0;
292 }
293 
294 /*-----------------------------------------------------------------------------
295  Function: msmapl2_input
296 
297  Returns: int (status)
298  The return code is a negative value if any error occurs, otherwise,
299  returns 0.
300 
301  Description:
302  Convert the arguments from the command line into a structure input
303  variable.
304 
305  Parameters: (in calling order)
306  Type Name I/O Description
307  ---- ---- --- -----------
308  int argc I number of arguments
309  char **argv I list of arguments
310  instr input O structure variable for inputs
311 
312 ----------------------------------------------------------------------------*/
313 
314 int l2mapgen_input(int argc, char **argv, clo_optionList_t* list, instr* input) {
315  char str_buf[4096];
316 
317 
318  /* initialize the option list with descriptions and default values */
320 
321  /* */
322  /* Set input values to defaults */
323  /* */
324  if (input_init(input) != 0) {
325  fprintf(stderr, "-E- %s: Error initializing input structure.\n", __FILE__);
326  return (-1);
327  }
328 
329  /* read the command line options into list */
330  if (l2mapgen_read_options(list, argc, argv) != 0) {
331  fprintf(stderr, "-E- %s: Error reading program options.\n", __FILE__);
332  return (-1);
333  }
334 
335  /* load options from list into input structure */
336  if (l2mapgen_load_input(list, input) != 0) {
337  fprintf(stderr, "-E- %s: Error loading options into input structure.\n", __FILE__);
338  return (-1);
339  }
340 
341  /* */
342  /* Build string of parameters for metadata */
343  /* */
344  sprintf(str_buf, "IFILE=%s|", input->ifile);
345  strcat(input->parms, str_buf);
346  sprintf(str_buf, "OFILE=%s|", input->ofile);
347  strcat(input->parms, str_buf);
348  sprintf(str_buf, "PALFILE=%s|", input->palfile);
349  strcat(input->parms, str_buf);
350  sprintf(str_buf, "PALETTE_DIR=%s|", input->palette_dir);
351  strcat(input->parms, str_buf);
352  sprintf(str_buf, "PRODUCT_TABLE=%s|", input->product_table);
353  strcat(input->parms, str_buf);
354  sprintf(str_buf, "FLAGUSE=%s|", input->flaguse);
355  strcat(input->parms, str_buf);
356  sprintf(str_buf, "PROD=%s|", input->prod);
357  strcat(input->parms, str_buf);
358  sprintf(str_buf, "STYPE=%d|", input->stype);
359  strcat(input->parms, str_buf);
360  sprintf(str_buf, "DATAMIN=%f|", input->datamin);
361  strcat(input->parms, str_buf);
362  sprintf(str_buf, "DATAMAX=%f|", input->datamax);
363  strcat(input->parms, str_buf);
364  sprintf(str_buf, "WEST=%f|", input->west);
365  strcat(input->parms, str_buf);
366  sprintf(str_buf, "EAST=%f|", input->east);
367  strcat(input->parms, str_buf);
368  sprintf(str_buf, "SOUTH=%f|", input->south);
369  strcat(input->parms, str_buf);
370  sprintf(str_buf, "NORTH=%f|", input->north);
371  strcat(input->parms, str_buf);
372  sprintf(str_buf, "WIDTH=%d|", input->width);
373  strcat(input->parms, str_buf);
374  sprintf(str_buf, "THRESHOLD=%f|", input->threshold);
375  strcat(input->parms, str_buf);
376  sprintf(str_buf, "MASK=%d|", input->mask);
377  strcat(input->parms, str_buf);
378  sprintf(str_buf, "QUALITY=%d|", input->quality);
379  strcat(input->parms, str_buf);
380  sprintf(str_buf, "APPLY_PAL=%d|", input->apply_pal);
381  strcat(input->parms, str_buf);
382 
383  return 0;
384 }
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 input_init(instr *input_str)
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 DEF_FLAG
Definition: l1c_input.h:17
#define NULL
Definition: decode_rs.h:63
char * key
Definition: clo.h:104
int l2mapgen_load_input(clo_optionList_t *list, instr *input)
char ** clo_getOptionStrings(clo_option_t *option, int *count)
Definition: clo.c:1226
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
#define VERSION
void clo_setHelpStr(const char *str)
Definition: clo.c:487
@ CLO_TYPE_INT
Definition: clo.h:79
char * clo_getOptionString(clo_option_t *option)
Definition: clo.c:1050
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 l2mapgen_input(int argc, char **argv, clo_optionList_t *list, instr *input)
@ CLO_TYPE_HELP
Definition: clo.h:86
const char * str
Definition: l1c_msi.cpp:35
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 clo_getOptionBool(clo_option_t *option)
Definition: clo.c:1087
int l2mapgen_read_options(clo_optionList_t *list, int argc, char *argv[])
int l2mapgen_init_options(clo_optionList_t *list)
int count
Definition: decode_rs.h:79