NASA Logo
Ocean Color Science Software

ocssw V2022
opt.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004-2006,2010,2012 Remik Ziemlinski <first d0t surname att n0aa d0t g0v>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2, or (at your option)
7  any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; see the file COPYING.
16  If not, write to the Free Software Foundation,
17  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19 
20 #include "opt.h"
21 #include <stdbool.h>
22 #define VERSION "2.1.1"
23 
24 bool anyDataSelected(nccmpopts *popts) {
25  return (popts->data || popts->metadata || popts->all);
26 }
27 
28 static struct option const long_options[] ={
29  {"Attribute", required_argument, 0, 'A'},
30  /*{"cartesian_axis", required_argument, 0, 'c'},*/
31  {"data", no_argument, 0, 'd'},
32  {"debug", no_argument, 0, 'D'},
33  {"exclude", required_argument, 0, 'x'},
34  {"force", no_argument, 0, 'f'},
35  {"fortran", no_argument, 0, 'F'},
36  {"global", no_argument, 0, 'g'},
37  {"globalex", required_argument, 0, 'G'},
38  {"header-pad", no_argument, 0, 'P'},
39  {"history", no_argument, 0, 'h'},
40  {"maxdiff ", required_argument, 0, 'C'},
41  {"metadata", no_argument, 0, 'm'},
42  {"missing", no_argument, 0, 'M'},
43  {"nans-are-equal", no_argument, 0, 'N'},
44  {"precision", required_argument, 0, 'p'},
45  {"quiet", no_argument, 0, 'q'},
46  {"report-identical-files", no_argument, 0, 's'},
47  {"tolerance", required_argument, 0, 't'},
48  {"Tolerance", required_argument, 0, 'T'},
49  {"notolerance", no_argument, 0, 'n'},
50  {"variable", required_argument, 0, 'v'},
51  {"extent", required_argument, 0, 'e'},
52  {"all", no_argument, 0, 'a'},
53  {"verbose", no_argument, 0, 'b'},
54  {"help", no_argument, 0, 'H'},
55  {"usage", no_argument, 0, 'H'},
56  {"version", no_argument, 0, 'V'},
57  {"warn", required_argument, 0, 'w'},
58  {0, 0, 0, 0}
59 };
60 
61 void freenccmpopts(nccmpopts* popts) {
62  freestringlist(&popts->excludeattlist, popts->nexcludeatt);
63  freestringlist(&popts->globalexclude, popts->nglobalexclude);
64  freestringlist(&popts->excludelist, popts->nexclude);
65  freestringlist(&popts->variablelist, popts->nvariable);
66  freestringlist(&popts->cmpvarlist, popts->ncmpvarlist);
67  XFREE(popts->file1);
68  XFREE(popts->file2);
69  XFREE(popts->precision);
70 }
71 
72 void initnccmpopts(nccmpopts* popts) {
73  if (popts == NULL) return;
74 
75  popts->data = 0;
76  popts->debug = 0;
77  popts->force = 0;
78  popts->fortran = 0;
79  popts->global = 0;
80  popts->history = 0;
81  popts->metadata = 0;
82  popts->missing = 0;
83  popts->nanequal = 0;
84  popts->precision = NULL;
85  popts->quiet = 0;
86  popts->report_identical = 0;
87  popts->verbose = 0;
88  popts->help = 0;
89  popts->tolerance = 0;
90  popts->abstolerance = 0;
91  popts->notolerance = 0;
92  popts->version = 0;
93  popts->maxdiff = 0;
94  popts->diffcount = 0;
95  popts->nexclude = 0;
96  popts->nvariable = 0;
97  popts->exclude = 0;
98  popts->variable = 0;
99  popts->nexcludeatt = 0;
100  popts->excludeattlist = NULL;
101  popts->excludelist = NULL;
102  popts->variablelist = NULL;
103  popts->file1 = NULL;
104  popts->file2 = NULL;
105  popts->nglobalexclude = 0;
106  popts->globalexclude = NULL;
107  popts->cmpvarlist = NULL;
108  popts->ncmpvarlist = 0;
109  popts->tprintf = NULL;
110  popts->all = 0;
111  popts->extent = 0;
112  popts->extentcount = 0;
113 
114  memset(popts->warn, 0, NCCMP_W_NUMTAGS);
115 }
116 
117 void printusage() {
118  fprintf(stderr, USAGE);
119 }
120 
121 void printversion() {
122  fprintf(stderr, "nccmp %s\n", VERSION);
123  fprintf(stderr, "Built with NetCDF %s\n", nc_inq_libvers());
124  fprintf(stderr, "Copyright (C) 2004-2007,2009,2010,2012,2013 Remik Ziemlinski\n \
125 \n\
126 This program comes with NO WARRANTY, to the extent permitted by law.\n\
127 You may redistribute copies of this program\n\
128 under the terms of the GNU General Public License.\n");
129 }
130 
131 int getnccmpopts(int argc, char** argv, nccmpopts* popts) {
132  int c;
133  char** warningtags = NULL;
134  int nwarningtags, i;
135  char** valid_warnings = NULL;
136  char tmp[256];
137 
138  /* Create searchable list of defined warning tags.
139  'nwarningtags' is used as a dummy arg here. */
140  newstringlist(&valid_warnings, &nwarningtags, NCCMP_W_NUMTAGS);
141  strcpy(tmp, NCCMP_W_TAGS); /* Necessary evil to prevent segfault. */
142  getstringlist(tmp, &valid_warnings, &nwarningtags);
143  /* printf("%s:%d\n", __FILE__, __LINE__); */
144 
145  if (popts == NULL) return EXIT_FATAL;
146 
147  if (newstringlist(&popts->globalexclude, &popts->nglobalexclude, NC_MAX_VARS) != EXIT_SUCCESS) {
148  printf("ERROR: Failed to allocate memory for global attribute exclusion list.\n");
149  exit(EXIT_FATAL);
150  }
151 
152  if (newstringlist(&popts->excludeattlist, &popts->nexcludeatt, NC_MAX_VARS) != EXIT_SUCCESS) {
153  printf("ERROR: Failed to allocate memory for attribute exclusion list.\n");
154  exit(EXIT_FATAL);
155  }
156 
157  if (newstringlist(&popts->excludelist, &popts->nexclude, NC_MAX_VARS) != EXIT_SUCCESS) {
158  printf("ERROR: Failed to allocate memory for variable exclusion list.\n");
159  exit(EXIT_FATAL);
160  }
161 
162  if (newstringlist(&popts->variablelist, &popts->nvariable, NC_MAX_VARS) != EXIT_SUCCESS) {
163  printf("ERROR: Failed to allocate memory for variable list.\n");
164  exit(EXIT_FATAL);
165  }
166 
167  while ((c = getopt_long(argc, argv, "A:C:dDx:fFghmNnMp:qst:T:v:bHVG:w:ae:", long_options, 0))
168  != -1
169  )
170 
171  switch (c) {
172  case 'a':
173  popts->all = 1;
174  popts->metadata = 1;
175  popts->global = 1;
176  popts->data = 1;
177  popts->force = 1;
178  popts->maxdiff = 10;
179  break;
180  case 'e':
181  popts->extent = atoi(optarg);
182  break;
183  case 'd':
184  popts->data = 1;
185  break;
186 
187  case 'D':
188  popts->debug = 1;
189  break;
190 
191  case 'x':
192  popts->exclude = 1;
193  getstringlist(optarg, &popts->excludelist, &popts->nexclude);
194  break;
195 
196  case 'f':
197  popts->force = 1;
198  break;
199 
200  case 'F':
201  popts->fortran = 1;
202  break;
203 
204  case 'g':
205  popts->global = 1;
206  popts->metadata = 1;
207  break;
208 
209  case 'h':
210  popts->history = 1;
211  break;
212 
213  case 'm':
214  popts->metadata = 1;
215  break;
216 
217  case 'M':
218  popts->missing = 1;
219  break;
220 
221  case 'N':
222  popts->nanequal = 1;
223  break;
224 
225  case 'p':
226  popts->precision = XMALLOC(char, strlen(optarg) + 1);
227  strcpy(popts->precision, optarg);
228  break;
229  case 'q':
230  popts->quiet = 1;
231  break;
232  case 's':
233  popts->report_identical = 1;
234  break;
235  case 'n':
236  popts->notolerance = 1;
237  break;
238  case 'C':
239  popts->maxdiff = (long) strtod(optarg, NULL);
240  break;
241  case 't':
242  popts->tolerance = strtod(optarg, NULL);
243  popts->abstolerance = 1;
244  if ((errno == ERANGE) || (errno == EDOM)) {
245  fprintf(stderr, "ERROR: Specified tolerance cannot be used.\n");
246  return EXIT_FATAL;
247  }
248  break;
249  case 'T':
250  popts->tolerance = strtod(optarg, NULL);
251  popts->abstolerance = 0;
252  if ((errno == ERANGE) || (errno == EDOM)) {
253  fprintf(stderr, "ERROR: Specified tolerance cannot be used.\n");
254  return EXIT_FATAL;
255  }
256  break;
257  case 'v':
258  getstringlist(optarg, &popts->variablelist, &popts->nvariable);
259  popts->variable = 1;
260  break;
261  case 'b':
262  popts->verbose = 1;
263  break;
264 
265  case 'V':
266  popts->version = 1;
267  break;
268 
269  case ':':
270  fprintf(stderr, "Error, -%c without argument.\n\n", optopt);
271  popts->help = 1;
272  break;
273 
274  case '?':
275  fprintf(stderr, "Error, Unknown argument %c.\n\n", optopt);
276  popts->help = 1;
277  break;
278 
279  case 'H':
280  popts->help = 1;
281  break;
282 
283  case 'A':
284  getstringlist(optarg, &popts->excludeattlist, &popts->nexcludeatt);
285  break;
286 
287  case 'G':
288  getstringlist(optarg, &popts->globalexclude, &popts->nglobalexclude);
289  break;
290 
291  case 'w':
292  newstringlist(&warningtags, &nwarningtags, NCCMP_W_NUMTAGS);
293  getstringlist(optarg, &warningtags, &nwarningtags);
294 
295  for (i = 0; i < nwarningtags; ++i) {
296  if (instringlist(valid_warnings, warningtags[i], NCCMP_W_NUMTAGS) != 1) {
297  /* Warn user of any invalid warning tags they supplied. */
298  fprintf(stderr, "WARNING: Warning tag \"%s\" is unsupported and will be ignored.\n", warningtags[i]);
299  }
300  }
301 
302  char *sall = (char*) "all", *sfmt = (char*) "format", *seos = (char*) "eos";
303 
304  /* Look for defined tags that are supported. */
305  if (instringlist(warningtags, sall, nwarningtags))
306  popts->warn[NCCMP_W_ALL] = 1;
307  else if (instringlist(warningtags, sfmt, nwarningtags))
308  popts->warn[NCCMP_W_FORMAT] = 1;
309  else if (instringlist(warningtags, seos, nwarningtags))
310  popts->warn[NCCMP_W_EOS] = 1;
311 
312  break;
313  }
314  if (popts->help) {
315  printusage();
316  printversion();
317  return EXIT_FATAL;
318  }
319  if (popts->version) {
320  printversion();
321  return EXIT_FATAL;
322  }
323  if (optind == argc) {
324  fprintf(stderr, "Error, missing operand after `%s'.\n\n", argv[argc - 1]);
325  printusage();
326  return EXIT_FATAL;
327  }
328  if (!anyDataSelected(popts)) {
329  fprintf(stderr, "Error, must supply at least one of these options: -d, -m, -a.\n\n");
330  printusage();
331  return EXIT_FATAL;
332  }
333  if (popts->variable && popts->exclude) {
334  fprintf(stderr, "Error, cannot combine -x and -v options.\n\n");
335  printusage();
336  return EXIT_FATAL;
337  }
338  if (popts->precision == NULL) {
339  popts->precision = XMALLOC(char, strlen("%g") + 1);
340  strcpy(popts->precision, "%g");
341  }
342 
343  /* get filename arguments */
344  argc -= optind;
345  argv += optind;
346  if ((argc < 2) ||
347  (argv[0] == NULL) ||
348  (argv[1] == NULL)
349  ) {
350  fprintf(stderr, "Error, 2 file arguments required.\n\n");
351  printusage();
352  popts->file1 = NULL;
353  popts->file2 = NULL;
354  return EXIT_FATAL;
355  } else {
356  /* store filenames */
357  popts->file1 = XMALLOC(char, strlen(argv[0]) + 1);
358  popts->file2 = XMALLOC(char, strlen(argv[1]) + 1);
359  strcpy(popts->file1, argv[0]);
360  strcpy(popts->file2, argv[1]);
361  }
362  /* allocate space to store tprintf based on size of precision option
363  * otherwise set it large enough for a simple format string
364  */
365  if (popts->precision) {
366  popts->tprintf = XMALLOC(char, strlen(popts->precision) + 1);
367  } else {
368  popts->tprintf = XMALLOC(char, 3);
369  }
370  if (popts->history && !popts->global) {
371  fprintf(stderr, "Error, -g required for -h option.\n\n");
372  printusage();
373  return EXIT_FATAL;
374  }
375  if (!popts->metadata && (popts->history || popts->global)) {
376  fprintf(stderr, "Error, -m required for -g and -h options.\n\n");
377  printusage();
378  return EXIT_FATAL;
379  }
380 
381  freestringlist(&warningtags, nwarningtags);
382  freestringlist(&valid_warnings, NCCMP_W_NUMTAGS);
383  /*printf("%s:%d\n", __FILE__, __LINE__);*/
384 
385  return EXIT_SUCCESS;
386 }
#define EXIT_SUCCESS
Definition: GEO_basic.h:72
#define NCCMP_W_NUMTAGS
Definition: opt.h:100
#define VERSION
Definition: opt.c:22
void initnccmpopts(nccmpopts *popts)
Definition: opt.c:72
#define NULL
Definition: decode_rs.h:63
void printversion()
Definition: opt.c:121
int newstringlist(char ***list, int *n, int size)
Definition: strlist.c:22
void freenccmpopts(nccmpopts *popts)
Definition: opt.c:61
#define NCCMP_W_ALL
Definition: opt.h:101
void freestringlist(char ***list, int nitems)
Definition: strlist.c:88
#define NCCMP_W_EOS
Definition: opt.h:103
character(len=1000) if
Definition: names.f90:13
int instringlist(char **list, char *str, int nitems)
Definition: strlist.c:107
#define NCCMP_W_FORMAT
Definition: opt.h:102
data_t tmp
Definition: decode_rs.h:74
int getnccmpopts(int argc, char **argv, nccmpopts *popts)
Definition: opt.c:131
bool anyDataSelected(nccmpopts *popts)
Definition: opt.c:24
#define USAGE
Definition: bindepths.c:20
void getstringlist(char *optarg, char ***list, int *nitems)
Definition: strlist.c:53
int errno
#define XFREE(stale)
Definition: xmalloc.h:42
void printusage()
Definition: opt.c:117
#define XMALLOC(type, num)
Definition: xmalloc.h:36
#define NCCMP_W_TAGS
Definition: opt.h:99
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE rather than treating them as resolving MODxl01780 Changed to use spacecraft ancillary data to recognise when the mirror encoder data is being set by side A or side B and to change calculations accordingly This removes the need for seperate LUTs for Side A and Side B data it makes the new LUTs incompatible with older versions of the and vice versa Also resolves MODxl01685 A more robust GRing algorithm is being which will create a non default GRing anytime there s even a single geolocated pixel in a granule Removed obsolete messages from seed as required for compatibility with version of the SDP toolkit Corrected test output file names to end in per delivery and then split off a new MYD_PR03 pcf file for Aqua Added AssociatedPlatformInstrumentSensor to the inventory metadata in MOD01 mcf and MOD03 mcf Created new versions named MYD01 mcf and MYD03 where AssociatedPlatformShortName is rather than Terra The program itself has been changed to read the Satellite Instrument validate it against the input L1A and LUT and to use it determine the correct files to retrieve the ephemeris and attitude data from Changed to produce a LocalGranuleID starting with MYD03 if run on Aqua data Added the Scan Type file attribute to the Geolocation copied from the L1A and attitude_angels to radians rather than degrees The accumulation of Cumulated gflags was moved from GEO_validate_earth_location c to GEO_locate_one_scan c
Definition: HISTORY.txt:464
#define EXIT_FATAL
Definition: common.h:85
int i
Definition: decode_rs.h:71
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")