NASA Logo
Ocean Color Science Software

ocssw V2022
l2extract_input.c
Go to the documentation of this file.
1 #include "l2extract.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <assert.h>
7 #include <genutils.h>
8 #include <clo.h>
9 #include <dfutils.h>
10 #include <sensorInfo.h>
11 #include <unistd.h>
12 
14 int l2extract_init_options(clo_optionList_t* list, const char* softwareVersion) {
15  clo_setVersion2("l2extract", softwareVersion);
16 
18  "Usage: l2extract argument-list"
19  "\n or: l2extract ifile spix epix sline eline pix_sub sc_sub ofile <prodlist>"
20  "\n"
21  "\n This program takes a product (or products if netCDF output) from an L2 file"
22  "\n and does the extraction"
23  "\n"
24  "\n The argument list is a set of keyword=value pairs. Arguments can"
25  "\n be specified on the command line, or put into a parameter file, or the"
26  "\n two methods can be used together, with command line overriding."
27  "\n"
28  "\nThe list of valid keywords follows:"
29  "\n");
30 
31  // files
32  clo_addOption(list, "ifile", CLO_TYPE_IFILE, NULL, "input L2 filename");
33  clo_addOption(list, "ofile", CLO_TYPE_OFILE, "output", "output filename");
34 
35  // data, projection, coverage
37  "comma separated list of products, empty string outputs all\n all products");
38  clo_addOption(list, "spix", CLO_TYPE_INT, "1", "start pixel number (1-based).");
39  clo_addOption(list, "epix", CLO_TYPE_INT, "-1", "end pixel number. -1 = last pixel (1-based).");
40  clo_addOption(list, "sline", CLO_TYPE_INT, "1", "start line (1-based).");
41  clo_addOption(list, "eline", CLO_TYPE_INT, "-1", "end line. -1 = last line (1-based).");
42  // clo_addOption(list, "pix_sub", CLO_TYPE_INT, "1", "pixel subsampling rate.");
43  // clo_addOption(list, "sc_sub", CLO_TYPE_INT, "1", "scan line subsampling rate.");
44  clo_addOption(list, "suite", CLO_TYPE_STRING, NULL, "suite for default parameters");
45  clo_addOption(list, "verbose", CLO_TYPE_BOOL, "false", "more information reporting");
46  clo_addOption(list, "wavelist", CLO_TYPE_STRING, NULL,
47  "comma separated list of 3D wavelengths and/or colon\n"
48  " separated nnn:nnn range of wavelengths, empty string outputs all\n"
49  " 3D wavelengths (i.e. wavelist=353,355,358,360,360:370,450:600,700)");
50  return 0;
51 }
52 
53 int getSensorId(const char* fileName) {
54  idDS dsId;
55  int sensorId = -1;
56 
57  dsId = openDS(fileName);
58  if (dsId.fid == -1) {
59  printf("-E- %s: Input file '%s' does not exist or cannot open. \n", __FILE__, fileName);
60  exit(EXIT_FAILURE);
61  }
62 
63  if (dsId.fftype == DS_NCDF) {
64  char* instrumentStr = readAttrStr(dsId, "instrument");
65  if (instrumentStr) {
66  char* platformStr = readAttrStr(dsId, "platform");
67  if (platformStr) {
68  sensorId = instrumentPlatform2SensorId(instrumentStr, platformStr);
69  free(platformStr);
70  }
71  free(instrumentStr);
72  } else {
73  char* sensorStr = readAttrStr(dsId, "Sensor");
74  if (sensorStr) {
75  sensorId = sensorName2SensorId(sensorStr);
76  free(sensorStr);
77  }
78  }
79  } else {
80  char* sensorNameStr = readAttrStr(dsId, "Sensor Name");
81  if (sensorNameStr) {
82  sensorId = sensorName2SensorId(sensorNameStr);
83  free(sensorNameStr);
84  }
85  }
86 
87  if (sensorId == -1) {
88  printf("Did not find a valid sensor ID - using OCRVC as the sensor ID.\n");
89  sensorId = OCRVC;
90  }
91 
92  endDS(dsId);
93  return sensorId;
94 }
95 
96 /*
97  Read the command line option and all of the default parameter files.
98 
99  This is the order for loading the options:
100  - load the main program defaults file
101  - load the command line (including specified par files)
102  - re-load the command line disabling file descending so command
103  line arguments will over ride
104 
105  */
107  char* dataRoot;
108  char tmpStr[FILENAME_MAX];
109 
110  assert(list);
111 
112  if ((dataRoot = getenv("OCDATAROOT")) == NULL) {
113  fprintf(stderr, "-E- OCDATAROOT environment variable is not defined. \n");
114  return (-1);
115  }
116 
117  // disable the dump option until we have read all of the files
119 
120  // load program defaults
121  sprintf(tmpStr, "%s/common/l2extract_defaults.par", dataRoot);
122  if (access(tmpStr, R_OK) != -1) {
123  if (want_verbose)
124  printf("Loading program default parameters from %s\n", tmpStr);
125  clo_readFile(list, tmpStr);
126  }
127 
128  // read all arguments
129  clo_readArgs(list, argc, argv);
130 
131  // get sensor directory
132  char* ifileStr;
133  if (clo_getPositionNumOptions(list) == 0) {
134  ifileStr = clo_getString(list, "ifile");
135  } else {
136  ifileStr = clo_getPositionString(optionList, 0);
137  }
138 
139  int sensorId = getSensorId(ifileStr);
140  int subsensorId = sensorId2SubsensorId(sensorId);
141  const char* sensorDir = sensorId2SensorDir(sensorId);
142 
143  // load the sensor specific defaults file
144  sprintf(tmpStr, "%s/%s/l2extract_defaults.par", dataRoot, sensorDir);
145  // int defaultLoaded = 0;
146  if (access(tmpStr, R_OK) != -1) {
147  if (want_verbose)
148  printf("Loading default parameters from %s\n", tmpStr);
149  clo_readFile(list, tmpStr);
150  }
151 
152  if (subsensorId != -1) {
153  sprintf(tmpStr, "%s/%s/%s/l2extract_defaults.par", dataRoot, sensorDir,
154  subsensorId2SubsensorDir(subsensorId));
155  if (access(tmpStr, R_OK) != -1) {
156  if (want_verbose)
157  printf("Loading default parameters from %s\n", tmpStr);
158  clo_readFile(list, tmpStr);
159  }
160  }
161 
162  // load the suite specific defaults file
163  clo_option_t* option = clo_findOption(list, "suite");
164  if (clo_isOptionSet(option)) {
165  int suiteLoaded = 0;
166  const char* suiteStr = clo_getOptionString(option);
167 
168  // common suite
169  sprintf(tmpStr, "%s/common/l2extract_defaults_%s.par", dataRoot, suiteStr);
170  if (access(tmpStr, R_OK) != -1) {
171  if (want_verbose)
172  printf("Loading default parameters from %s\n", tmpStr);
173  clo_readFile(list, tmpStr);
174  suiteLoaded = 1;
175  }
176 
177  // sensor suite
178  sprintf(tmpStr, "%s/%s/l2extract_defaults_%s.par", dataRoot, sensorDir, suiteStr);
179  if (access(tmpStr, R_OK) != -1) {
180  if (want_verbose)
181  printf("Loading default parameters from %s\n", tmpStr);
182  clo_readFile(list, tmpStr);
183  suiteLoaded = 1;
184  }
185 
186  // sub-sensor suite
187  if (subsensorId != -1) {
188  sprintf(tmpStr, "%s/%s/%s/l2extract_defaults_%s.par", dataRoot, sensorDir,
189  subsensorId2SubsensorDir(subsensorId), suiteStr);
190  if (access(tmpStr, R_OK) != -1) {
191  if (want_verbose)
192  printf("Loading default parameters from %s\n", tmpStr);
193  clo_readFile(list, tmpStr);
194  suiteLoaded = 1;
195  }
196  }
197 
198  if (!suiteLoaded) {
199  printf("-E- Failed to load parameters for suite %s for sensor %s\n", suiteStr,
200  sensorId2SensorName(sensorId));
201  exit(EXIT_FAILURE);
202  }
203  }
204  // enable the dump option
206  // make the command line over ride
207  clo_readArgs(list, argc, argv);
208 
209  return 0;
210 }
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
const char * sensorId2SensorDir(int sensorId)
Definition: sensorInfo.c:315
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
int instrumentPlatform2SensorId(const char *instrument, const char *platform)
Definition: sensorInfo.c:405
idDS openDS(const char *filename)
Definition: wrapper.c:606
#define NULL
Definition: decode_rs.h:63
int sensorName2SensorId(const char *name)
Definition: sensorInfo.c:371
clo_option_t * clo_findOption(clo_optionList_t *list, const char *key)
Definition: clo.c:967
ds_format_t fftype
Definition: dfutils.h:31
int sensorId2SubsensorId(int sensorId)
Definition: sensorInfo.c:438
int clo_isOptionSet(clo_option_t *option)
Definition: clo.c:2257
@ CLO_TYPE_BOOL
Definition: clo.h:81
void clo_setVersion2(const char *programName, const char *versionStr)
Definition: clo.c:464
void clo_setEnableDumpOptions(int val)
Definition: clo.c:410
char * readAttrStr(idDS ds_id, const char *name)
Definition: wrapper.c:99
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
@ CLO_TYPE_INT
Definition: clo.h:82
char * clo_getOptionString(clo_option_t *option)
Definition: clo.c:1050
int l2extract_read_options(clo_optionList_t *list, int argc, char *argv[])
int getSensorId(const char *fileName)
char * clo_getPositionString(clo_optionList_t *list, int pos)
Definition: clo.c:1723
int l2extract_init_options(clo_optionList_t *list, const char *softwareVersion)
int want_verbose
@ CLO_TYPE_IFILE
Definition: clo.h:87
@ CLO_TYPE_OFILE
Definition: clo.h:88
@ DS_NCDF
Definition: dfutils.h:20
void clo_readFile(clo_optionList_t *list, const char *fileName)
Definition: clo.c:2210
clo_optionList_t * optionList
const char * sensorId2SensorName(int sensorId)
Definition: sensorInfo.c:273
int32_t fid
Definition: dfutils.h:29
Definition: dfutils.h:28
int clo_getPositionNumOptions(clo_optionList_t *list)
Definition: clo.c:1704
const char * subsensorId2SubsensorDir(int subsensorId)
Definition: sensorInfo.c:329
#define OCRVC
Definition: sensorDefs.h:24
int endDS(idDS ds_id)
Definition: wrapper.c:624
@ CLO_TYPE_STRING
Definition: clo.h:86