OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
viirs_sim_input.c
Go to the documentation of this file.
1 #include "viirs_sim_sdr.h"
2 #include <stdio.h>
3 #include <assert.h>
4 #include "clo.h"
5 #include <ctype.h>
6 
7 int viirs_sim_input(int argc, char *argv[], ctl_struc *ctl)
8 /*-----------------------------------------------------------------------------
9  Routine: viirs_sim_input
10 
11  Description: get inputs for viirs_sim_sdr
12 
13  Returns: int 0 if all good
14 
15  Arguments:
16  Type Name I/O Description
17  ---- ---- --- -----------
18  int argc I count of command line args
19  char *[] argv I command line arguments to be processed
20  by the clo library
21 
22  Modification history:
23 
24  W. Robinson, SAIC 21 Sep 2010 Original development
25 
26 ----------------------------------------------------------------------------*/ {
27  int errflg, opt_count, opt_count2, verr;
28  char tmpstr[FILENAME_MAX], filetmp[FILENAME_MAX];
29  int i, nbands, ar_count, orb_geo_use = 0, out_loc_use = 0;
31  float *fArray;
32  char localsuite[FILENAME_MAX];
33  char prog_name[] = "viirs_sim_sdr", sensor_id[] = "viirsn", *dataroot;
34 
35  if ((dataroot = getenv("OCDATAROOT")) == NULL) {
36  printf("%s, %d -E- OCDATAROOT environment variable is not defined.\n",
37  __FILE__, __LINE__);
38  return (-1);
39  }
40  /*
41  * follow the clo.h and start it up
42  */
43  sprintf(tmpstr, "viirs_sim_sdr version 1.0 (%s %s)", __DATE__, __TIME__);
44  clo_setVersion(tmpstr);
45  list = clo_createList();
46 
47  assert(list);
48 
49  /*
50  * fill some control values
51  */
52  ctl->vic_cal_chg = 0;
53  for (i = 0; i < MAX_BND; i++) {
54  ctl->gain[i] = 1.;
55  ctl->offset[i] = 0;
56  }
57  /*
58  * set default arguments and add callback for a par file
59  */
60  strcpy(tmpstr, "Input VIIRS geolocation file (required)");
61  clo_addOption(list, "geofile", CLO_TYPE_IFILE, NULL, tmpstr);
62 
63  strcpy(tmpstr, "path to place output SDR files (required)");
64  clo_addOption(list, "opath", CLO_TYPE_OFILE, NULL, tmpstr);
65 
66  ctl->l2_use = 0;
67  strcpy(tmpstr, "level-2 file containing TOA radiances");
68  clo_addOption(list, "il2file", CLO_TYPE_IFILE, "Unspecified", tmpstr);
69 
70  ctl->rhos_use = 0;
71  strcpy(tmpstr, "Land reflectance data file");
72  clo_addOption(list, "iland_refl_file", CLO_TYPE_IFILE, "Unspecified",
73  tmpstr);
74 
75  strcpy(tmpstr, "reflectance replacement options:\n");
76  strcat(tmpstr,
77  "\t0: fill only where there are no valid TOA values\n");
78  strcat(tmpstr, "\t1: replace all points with reflectance taken to TOA");
79  clo_addOption(list, "refl_fil_opt", CLO_TYPE_INT, "0", tmpstr);
80 
81  strcpy(tmpstr, "output scan format, resultant format is\n");
82  strcat(tmpstr, "\tminimum of input format and oscn_fmt_opt:\n");
83  strcat(tmpstr, "\t0 - make aggregated\n");
84  strcat(tmpstr, "\t1 - make unaggregated\n");
85  strcat(tmpstr, "\t2 - same as input format");
86  clo_addOption(list, "oscn_fmt_opt", CLO_TYPE_INT, "2", tmpstr);
87 
88  strcpy(tmpstr, "M-band create option:\n");
89  strcat(tmpstr, "\t0 - make the Vis, NIR bands (M1 - 7)\n");
90  strcat(tmpstr, "\t1 - make all M bands (M1 - 16)");
91  clo_addOption(list, "band_opt", CLO_TYPE_INT, "0", tmpstr);
92 
93  strcpy(tmpstr, "optical crosstalk option:\n");
94  strcat(tmpstr, "\t0 - no added cross talk\n");
95  strcat(tmpstr, "\t1 - add optical crosstalk (OXT) artifact");
96  clo_addOption(list, "oxt_opt", CLO_TYPE_INT, "0", tmpstr);
97 
98  strcpy(filetmp, "$VIIRS_SIM_DATA/oxt_coeff_default.h5\n");
99  sprintf(tmpstr, "optical crosstalk influence coefficients");
100  clo_addOption(list, "oxt_coef_file", CLO_TYPE_IFILE, filetmp, tmpstr);
101 
102  strcpy(filetmp, "$VIIRS_SIM_DATA/inter_band_default.dat");
103  sprintf(tmpstr, "Inter-band data table\n");
104  clo_addOption(list, "inter_band_file", CLO_TYPE_IFILE, filetmp, tmpstr);
105 
106  strcpy(tmpstr, "metadata output file (not implemented yet)");
107  clo_addOption(list, "metafile", CLO_TYPE_OFILE, "Unspecified", tmpstr);
108 
109  strcpy(tmpstr,
110  "bow tie deletion option (default, on to do bow tie delete)");
111  clo_addOption(list, "bow_tie_opt", CLO_TYPE_BOOL, "on", tmpstr);
112 
113  strcpy(tmpstr, "Calibration gains to remove from TOA radiances");
114  clo_addOption(list, "gain", CLO_TYPE_FLOAT, "1.,1.", tmpstr);
115 
116  strcpy(tmpstr, "Calibration offsets to remove from TOA radiances");
117  clo_addOption(list, "offset", CLO_TYPE_FLOAT, "0.,0.", tmpstr);
118 
119  strcpy(tmpstr, "optional create time in form YYYYMMDD/HHMMSS.FFFFFF\n");
120  strcat(tmpstr, "\tif not specified, the time the program ran");
121  clo_addOption(list, "cre_time", CLO_TYPE_STRING, "Unspecified", tmpstr);
122 
123  strcpy(tmpstr, "SDR file name create option:\n");
124  strcat(tmpstr, "\t0 - make standard VIIRS format\n");
125  strcat(tmpstr, "\t1 - omit the create time part (_cYYYY...)\n");
126  clo_addOption(list, "fname_opt", CLO_TYPE_INT, "0", tmpstr);
127 
128  strcpy(tmpstr, "SDR overwrite option:\n");
129  strcat(tmpstr, "\t0 - do not permit overwrite\n");
130  strcat(tmpstr, "\t1 - allow overwrite to happen\n");
131  clo_addOption(list, "sdr_overwrite", CLO_TYPE_INT, "0", tmpstr);
132 
133  /*
134  * the count calibration (decalibration) controls
135  */
136  ctl->count_cal_opt = 0;
137  strcpy(tmpstr, "count calibration option:\n");
138  strcat(tmpstr, "\t0 - no count calibration done\n");
139  strcat(tmpstr, "\t1 - perform de-cal and cal (mandatory for exect X-talk\n");
140  strcat(tmpstr, "\t2 - (1), but also integerize counts");
141  clo_addOption(list, "count_cal_opt", CLO_TYPE_INT, "0", tmpstr);
142 
143  strcpy(tmpstr, "Count calibration gain file");
144  strcat(tmpstr, "\t(if omitted, an internal TVAC general set is used)");
145  clo_addOption(list, "count_cal_gain_file", CLO_TYPE_IFILE, "Unspecified",
146  tmpstr);
147 
148  strcpy(tmpstr, "Count calibration rvs file leader (pre-lambda)");
149  strcat(tmpstr, "\t(if omitted, unity rvs is used)");
150  clo_addOption(list, "count_cal_rvs_file", CLO_TYPE_IFILE, "Unspecified",
151  tmpstr);
152 
153  strcpy(tmpstr, "Count de-calibration gain file\n");
154  strcat(tmpstr, "\t(if omitted, an internal TVAC general set is used)");
155  clo_addOption(list, "count_decal_gain_file", CLO_TYPE_IFILE, "Unspecified",
156  tmpstr);
157 
158  strcpy(tmpstr, "Count de-calibration rvs file (pre-lambda)\n");
159  strcat(tmpstr, "\t(if omitted, unity rvs is used)");
160  clo_addOption(list, "count_decal_rvs_file", CLO_TYPE_IFILE, "Unspecified",
161  tmpstr);
162 
163  ctl->count_dark_opt = 0;
164  strcpy(tmpstr, "count calibration dark subtract option:\n");
165  strcat(tmpstr,
166  "\t0 - do not perform dark subtract in decal / cal process\n");
167  strcat(tmpstr, "\t1 - do the dark subtract in decal / cal process\n");
168  clo_addOption(list, "count_dark_opt", CLO_TYPE_INT, "0", tmpstr);
169 
170  strcpy(tmpstr, "product suite string for loading\n");
171  strcat(tmpstr, " suite-specific defaults");
172  clo_addOption(list, "suite", CLO_TYPE_STRING, "EMPTY", tmpstr);
173 
174  ctl->ext_opt = 0;
175  strcpy(tmpstr, "Electronic crosstalk processing option:\n");
176  strcat(tmpstr, "\t0 - no crosstalk applied\n");
177  strcat(tmpstr, "\t1 - Apply electronic crosstalk\n");
178  clo_addOption(list, "ext_opt", CLO_TYPE_INT, "0", tmpstr);
179 
180  strcpy(tmpstr, "Electronic crosstalk coefficient file\n");
181  strcat(tmpstr,
182  "\t(if omitted, a trivial crosstalk, zero influence, is applied)\n");
183  clo_addOption(list, "ext_coeff_file", CLO_TYPE_IFILE, "Unspecified",
184  tmpstr);
185 
186  strcpy(tmpstr, "SDR origin designation\n");
187  strcat(tmpstr,
188  "\t(will appear in attributes and file name origin location)\n");
189  clo_addOption(list, "id_origin", CLO_TYPE_STRING, "gsfc", tmpstr);
190 
191  strcpy(tmpstr, "SDR domain designation\n");
192  strcat(tmpstr,
193  "\t(will appear in attributes and file name domain location)\n");
194  clo_addOption(list, "id_domain", CLO_TYPE_STRING, "SCI", tmpstr);
195 
196  /*
197  * for the noise, control if used and have file of noise coeffs
198  */
199  ctl->noise_mode = 0;
200  strcpy(tmpstr, "Noise addition option: 0 to not use 1 to apply\n");
201  clo_addOption(list, "noise_opt", CLO_TYPE_INT, "0", tmpstr);
202 
203  strcpy(tmpstr, "Noise coefficient file name\n");
204  strcat(tmpstr, "\t(if omitted or Unspecified, no noise will be added\n");
205  clo_addOption(list, "noise_coeff", CLO_TYPE_IFILE, "Unspecified", tmpstr);
206  /*
207  * for stray light artifact, control if used and have file of coeffs
208  */
209  ctl->stray_opt = 0;
210  strcpy(tmpstr, "Stray light addition option: 0 to not use 1 to apply\n");
211  clo_addOption(list, "stray_opt", CLO_TYPE_INT, "0", tmpstr);
212 
213  strcpy(tmpstr, "Stray light coefficient file name\n");
214  strcat(tmpstr, "\t(if omitted or Unspecified, no stray light is added\n");
215  clo_addOption(list, "stray_tbl", CLO_TYPE_IFILE, "Unspecified", tmpstr);
216  /*
217  * All code to get data from the default file, arguments, suite file...
218  *
219  *
220  */
221  /*
222  * get the option info and re-get so the command line set takes precedence
223  */
225  opt_count = clo_getNumOptions(list);
226  /*
227  * go through the option heirarchy
228  */
229  clo_readArgs(list, argc, argv);
230 
231  localsuite[0] = '\0';
232  if (clo_isSet(list, "suite")) {
233  strcpy(localsuite, clo_getString(list, "suite"));
234  }
235  sprintf(tmpstr, "%s/%s/%s_defaults.par", dataroot, sensor_id, prog_name);
236  clo_readFile(list, tmpstr);
237 
238  if (localsuite[0] == '\0')
239  strcpy(localsuite, clo_getString(list, "suite"));
240  sprintf(tmpstr, "%s/%s/%s_defaults_%s.par", dataroot, sensor_id, prog_name,
241  localsuite);
242  clo_readFile(list, tmpstr);
243 
245  clo_readArgs(list, argc, argv);
246  /*
247  * end heirarchy, check for any new options
248  */
249  opt_count2 = clo_getNumOptions(list);
250  if (opt_count != opt_count2) {
251  printf("\n\nError: initial # options: %d, final # options: %d\n",
252  opt_count, opt_count2);
253  printf("%s, %d: Error, a mis-spelled or non-existant option has\n",
254  __FILE__, __LINE__);
255  printf("\tbeen entered. The extra options should be at the end of\n");
256  printf("\tthe following option list:\n");
258  exit(1);
259  }
260  /*
261  * End all code to get data from the default file, arguments, suite file...
262  *
263  *
264  */
265  /*
266  * gather the inputs to the ctl struct and check input validity
267  */
268  errflg = 0;
269 
270  if (clo_isSet(list, "geofile")) {
271  orb_geo_use = 1;
272  strcpy(ctl->in_geo_file, clo_getString(list, "geofile"));
273  }
274 
275  if (clo_isSet(list, "opath")) {
276  out_loc_use = 1;
277  strcpy(ctl->out_loc, clo_getString(list, "opath"));
278  }
279 
280  if (clo_isSet(list, "il2file")) ctl->l2_use = 1;
281  strcpy(ctl->l2_file, clo_getString(list, "il2file"));
282 
283  if (clo_isSet(list, "iland_refl_file")) ctl->rhos_use = 1;
284  strcpy(ctl->rhos_file, clo_getString(list, "iland_refl_file"));
285 
286  ctl->rhos_opt = clo_getInt(list, "refl_fil_opt");
287  if ((ctl->rhos_opt < 0) || (ctl->rhos_opt > 1)) {
288  printf("refl_fil_opt must be 0 or 1\n");
289  errflg++;
290  }
291 
292  ctl->out_scn_fmt = clo_getInt(list, "oscn_fmt_opt");
293  if ((ctl->out_scn_fmt < 0) || (ctl->out_scn_fmt > 2)) {
294  printf("out_scn_fmt must be 0 to 2\n");
295  errflg++;
296  }
297 
298  ctl->make_m = clo_getInt(list, "band_opt");
299  if ((ctl->make_m < 0) || (ctl->make_m > 1)) {
300  printf("make_m must be 0 or 1\n");
301  errflg++;
302  }
303  if (ctl->make_m == 0)
304  nbands = 7;
305  else
306  nbands = 16;
307 
308  ctl->oxt_mode = clo_getInt(list, "oxt_opt");
309  if ((ctl->oxt_mode < 0) || (ctl->oxt_mode > 1)) {
310  printf("oxt_mode must be 0 to 1\n");
311  errflg++;
312  }
313 
314  strcpy(ctl->oxt_coef, clo_getString(list, "oxt_coef_file"));
315 
316  strcpy(ctl->inter_band, clo_getString(list, "inter_band_file"));
317 
318  ctl->meta_use = (clo_isSet(list, "metafile")) ? 1 : 0;
319  strcpy(ctl->meta_file, clo_getString(list, "metafile"));
320 
321  ctl->bowtie_opt = (clo_getBool(list, "bow_tie_opt")) ? 1 : 0;
322 
323  ctl->sdr_overwrite = clo_getInt(list, "sdr_overwrite");
324  if ((ctl->sdr_overwrite != 0) && (ctl->sdr_overwrite != 1)) {
325  printf("sdr_overwrite must be 0 or 1\n");
326  errflg++;
327  }
328 
329  ctl->fname_opt = clo_getInt(list, "fname_opt");
330  if ((ctl->fname_opt != 0) && (ctl->fname_opt != 1)) {
331  printf("fname_opt must be 0 or 1\n");
332  errflg++;
333  }
334 
335  if (clo_isSet(list, "gain")) {
336  ctl->vic_cal_chg = 1;
337  fArray = clo_getFloats(list, "gain", &ar_count);
338  if (ar_count != nbands) {
339  printf("%s, %d: Error, number of gain elements must be %d\n",
340  __FILE__, __LINE__, nbands);
341  errflg++;
342  } else {
343  for (i = 0; i < nbands; i++)
344  ctl->gain[i] = fArray[i];
345  }
346  }
347 
348  if (clo_isSet(list, "offset")) {
349  ctl->vic_cal_chg = 1;
350  fArray = clo_getFloats(list, "offset", &ar_count);
351  if (ar_count != nbands) {
352  printf("%s, %d: Error, number of offset elements must be %d\n",
353  __FILE__, __LINE__, nbands);
354  errflg++;
355  } else {
356  for (i = 0; i < nbands; i++)
357  ctl->offset[i] = fArray[i];
358  }
359  }
360 
361  strcpy(ctl->cre_time, clo_getString(list, "cre_time"));
362  if (strcmp(ctl->cre_time, "Unspecified") != 0) {
363  verr = 0;
364  for (i = 0; i < 8; i++)
365  if (!isdigit(ctl->cre_time[i])) verr = 1;
366  if (ctl->cre_time[8] != '/') verr = 1;
367  for (i = 9; i < 15; i++)
368  if (!isdigit(ctl->cre_time[i])) verr = 1;
369  if (ctl->cre_time[15] != '.') verr = 1;
370  for (i = 16; i < 22; i++)
371  if (!isdigit(ctl->cre_time[i])) verr = 1;
372 
373  if (verr == 1) {
374  printf("%s, %d: Error, cre_time has improper format\n",
375  __FILE__, __LINE__);
376  errflg++;
377  }
378  }
379  ctl->count_cal_opt = clo_getInt(list, "count_cal_opt");
380  if ((ctl->count_cal_opt < 0) || (ctl->count_cal_opt > 2)) {
381  printf("count_cal_opt must be 0, 1 or 2\n");
382  errflg++;
383  }
384 
385  strcpy(ctl->count_cal_gain_file, clo_getString(list,
386  "count_cal_gain_file"));
387  strcpy(ctl->count_cal_rvs_file, clo_getString(list,
388  "count_cal_rvs_file"));
389  strcpy(ctl->count_decal_gain_file, clo_getString(list,
390  "count_decal_gain_file"));
391  strcpy(ctl->count_decal_rvs_file, clo_getString(list,
392  "count_decal_rvs_file"));
393 
394  ctl->ext_opt = clo_getInt(list, "ext_opt");
395  if ((ctl->ext_opt < 0) || (ctl->ext_opt > 1)) {
396  printf("ext_opt must be 0 or 1\n");
397  errflg++;
398  }
399 
400  strcpy(ctl->ext_coeff_file, clo_getString(list, "ext_coeff_file"));
401 
402  strcpy(ctl->id_domain, clo_getString(list, "id_domain"));
403  strcpy(ctl->id_origin, clo_getString(list, "id_origin"));
404 
405  ctl->noise_mode = clo_getInt(list, "noise_opt");
406  strcpy(ctl->noise_coef, clo_getString(list, "noise_coeff"));
407  if (strcmp(ctl->noise_coef, "Unspecified") == 0)
408  ctl->noise_mode = 0;
409 
410  ctl->stray_opt = clo_getInt(list, "stray_opt");
411  strcpy(ctl->stray_tbl, clo_getString(list, "stray_tbl"));
412  if (strcmp(ctl->stray_tbl, "Unspecified") == 0)
413  ctl->stray_opt = 0;
414 
415  ctl->any_artifact = 0;
416 
417  if (!errflg) {
418  /*
419  * make sure the minimal inputs were provided
420  */
421  if ((orb_geo_use == 0) || (out_loc_use == 0)) {
422  printf("Both geofile and opath must be set\n");
423  errflg++;
424  }
425  /*
426  * if electronic crosstalk is enabled, make sure the cal / decal is done
427  */
428  if ((ctl->ext_opt != 0) && (ctl->count_cal_opt == 0))
429  ctl->count_cal_opt = 1;
430  /*
431  * if any artifact addition or decal / cal is done, set general switch
432  */
433  if ((ctl->oxt_mode != 0) || (ctl->count_cal_opt > 0) ||
434  (ctl->noise_mode != 0) || (ctl->stray_opt != 0))
435  ctl->any_artifact = 1;
436  }
437  if (errflg) {
439  exit(1);
440  }
441  return (0);
442 }
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
char * clo_getString(clo_optionList_t *list, const char *key)
Definition: clo.c:1357
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
int clo_isSet(clo_optionList_t *list, const char *key)
Definition: clo.c:2270
#define MAX_BND
Definition: viirs_sim_sdr.h:34
@ CLO_TYPE_FLOAT
Definition: clo.h:81
int clo_getInt(clo_optionList_t *list, const char *key)
Definition: clo.c:1393
@ CLO_TYPE_BOOL
Definition: clo.h:78
#define isdigit(c)
void clo_setEnableDumpOptions(int val)
Definition: clo.c:410
clo_optionList_t * clo_createList()
Definition: clo.c:532
@ CLO_TYPE_INT
Definition: clo.h:79
void clo_printUsage(clo_optionList_t *list)
Definition: clo.c:1988
float * clo_getFloats(clo_optionList_t *list, const char *key, int *count)
Definition: clo.c:1538
@ CLO_TYPE_IFILE
Definition: clo.h:84
@ CLO_TYPE_OFILE
Definition: clo.h:85
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 viirs_sim_input(int argc, char *argv[], ctl_struc *ctl)
int32_t nbands
void clo_setVersion(const char *str)
Definition: clo.c:448
int clo_getBool(clo_optionList_t *list, const char *key)
Definition: clo.c:1375
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")