OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
l3mapgen_input.cpp
Go to the documentation of this file.
1 #include "l3mapgen.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 l3mapgen_init_options(clo_optionList_t* list, const char* softwareVersion) {
15 
16  clo_setVersion2("l3mapgen", softwareVersion);
17 
18  clo_setHelpStr("Usage: l3mapgen argument-list"
19  "\n"
20  "\n This program takes a product (or products if netCDF output) from an L3 bin"
21  "\n or SMI file, reprojects the data using proj.4 and writes a mapped file in"
22  "\n the requested output format."
23  "\n"
24  "\n Return values"
25  "\n 0 = All Good"
26  "\n 1 = Error"
27  "\n 110 = No valid data to map"
28  "\n"
29  "\n The argument list is a set of keyword=value pairs. Arguments can"
30  "\n be specified on the command line, or put into a parameter file, or the"
31  "\n two methods can be used together, with command line overriding."
32  "\n"
33  "\nThe list of valid keywords follows:"
34  "\n");
35 
36  clo_addOption(list, "suite", CLO_TYPE_STRING, NULL, "suite for default parameters");
37 
38  // files
39  clo_addOption(list, "ifile", CLO_TYPE_IFILE, NULL, "input L3 bin filename"
40  "\n");
41 
42  clo_addOption(list, "ofile", CLO_TYPE_OFILE, "output", "output filename");
43  clo_addOption(list, "oformat", CLO_TYPE_STRING, "netcdf4",
44  "output file format"
45  "\n netcdf4: netCDF4 file, can contain more than one product"
46  "\n hdf4: HDF4 file (old SMI format)"
47  "\n png: PNG image file"
48  "\n ppm: PPM image file"
49  "\n tiff: TIFF file with georeference tags");
50 
51  clo_addOption(list, "ofile_product_tag", CLO_TYPE_STRING, "PRODUCT",
52  "sub-string in ofile name that will be substituted by the product name");
54  "second output filename");
55  clo_addOption(list, "oformat2", CLO_TYPE_STRING, "png",
56  "second output file format"
57  "\n same options as oformat");
58 
59  clo_addOption(list, "deflate", CLO_TYPE_INT, "4", "netCDF4 deflation level"
60  "\n");
61 
62  // data, projection, coverage
64  "comma separated list of products."
65  "\n Each product can have an optional colon and modifier appended."
66  "\n For example, \"product=chlor_a,chlor_a:stdev,Kd_490:nobs\""
67  "\n Available modifiers:"
68  "\n avg average value (default)"
69  "\n stdev standard deviation"
70  "\n var variance"
71  "\n nobs number of observations in the bin"
72  "\n nscenes number of contributing scenes"
73  "\n obs_time average observation time (TAI93)"
74  "\n bin_num bin ID number"
75  "\n");
76 
77  clo_addOption(list, "resolution", CLO_TYPE_STRING, NULL,
78  "size of output pixel (default from input file)"
79  "\n in meters or SMI dimensions"
80  "\n 90km: 432 x 216 image for full globe"
81  "\n 36km: 1080 x 540"
82  "\n 18km: 2160 x 1080"
83  "\n 9km: 4320 x 2160"
84  "\n 4km: 8640 x 4320"
85  "\n 2km: 17280 x 8640"
86  "\n 1km: 34560 x 17280"
87  "\n hkm: 69120 x 34560"
88  "\n qkm: 138240 x 69120"
89  "\n smi: 4096 x 2048"
90  "\n smi4: 8192 x 4096"
91  "\n land: 8640 x 4320"
92  "\n #.#: width of a pixel in meters"
93  "\n #.#km: width of a pixel in kilometers"
94  "\n #.#deg: width of a pixel in degrees");
96  "width of output image in pixels; supercedes resolution parameter.\n");
97 
98  clo_addOption(list, "projection", CLO_TYPE_STRING, "platecarree",
99  "proj.4 projection string or one"
100  "\n of the following predefined projections:"
101  "\n smi: Standard Mapped image, cylindrical projection,"
102  "\n uses central_meridian. NSEW defaults to whole globe."
103  "\n projection=\"+proj=eqc +lon_0=<central_meridian>\""
104  "\n platecarree: Plate Carree image, cylindrical projection,"
105  "\n uses central_meridian."
106  "\n projection=\"+proj=eqc +lon_0=<central_meridian>\""
107  "\n mollweide: Mollweide projection"
108  "\n projection=\"+proj=moll +lon_0=<central_meridian>\""
109  "\n lambert: Lambert conformal conic (2SP) projection"
110  "\n projection=\"+proj=lcc +lon_0=<central_meridian>"
111  "\n +lat_0=<scene center latitude>"
112  "\n +lat_1=<scene south latitude>"
113  "\n +lat_2=<scene north latitude>\""
114  "\n albersconic: Albers Equal Area Conic projection"
115  "\n projection=\"+proj=aea +lon_0=<central_meridian>"
116  "\n +lat_0=<scene center latitude>"
117  "\n +lat_1=<scene south latitude>"
118  "\n +lat_2=<scene north latitude>\""
119  "\n mercator: Mercator cylindrical map projection"
120  "\n projection=\"+proj=merc +lon_0=<central_meridian>\""
121  "\n transmerc: Transverse Mercator cylindrical map projection"
122  "\n projection=\"+proj=tmerc +lon_0=<central_meridian>"
123  "\n +lat_0=<scene center latitude>\""
124  "\n utm: Universal Transverse Mercator cylindrical map projection"
125  "\n projection=\"+proj=utm +zone=<utm_zone> [+south]\""
126  "\n obliquemerc: Oblique Mercator cylindrical map projection"
127  "\n projection=\"+proj=omerc +gamma=0 +lat_0=<lat_0>"
128  "\n +lonc=<central_meridian> +alpha=<azimuth>"
129  "\n +k_0=1 +x_0=0 +y_0=0\""
130  "\n ease2: EASE-Grid 2.0 projection"
131  "\n projection=\"EPSG:6933\""
132  "\n stere: Stereographic projection"
133  "\n projection=\"+proj=stere +lat_0=<lat_0> +lat_ts=<lat_ts>"
134  "\n +lon_0=<central_meridian>"
135  "\n +ellps=WGS84 +datum=WGS84 +units=m\""
136  "\n conus: USA Contiguous Albers Equal Area Conic USGS version"
137  "\n projection=\"+proj=aea +lat_1=29.5 +lat_2=45.5"
138  "\n +lat_0=23.0 +lon_0=-96 +x_0=0 +y_0=0"
139  "\n +ellps=GRS80 +datum=NAD83 +units=m\""
140  "\n alaska: Alaskan Albers Equal Area Conic USGS version"
141  "\n projection=\"EPSG:3338\""
142  "\n gibs: latitudinally dependent projection"
143  "\n Plate Carree between 60S and 60N"
144  "\n else use Polar Sterographic"
145  "\n North Polar: projection=\"EPSG:3413\""
146  "\n South Polar: projection=\"EPSG:3031\""
147  "\n raw: Raw dump of bin file contents."
148  "\n");
149  clo_addOption(list, "write_projtext", CLO_TYPE_BOOL, "no", "write projection information to a text file.");
150  clo_addOption(list, "central_meridian", CLO_TYPE_FLOAT, "-999",
151  "central meridian for projection in deg east."
152  "\n Used only for raw dump and predefined projections as above.");
154  "latitude of true scale for projection in deg north."
155  "\n Used only for predefined projections above as required.");
157  "latitude of origin for projection in deg north."
158  "\n Used only for predefined projections above as required.");
160  "latitude of first standard parallel (south)."
161  "\n Used only for predefined projections above as required.");
163  "latitude of second standard parallel (north)."
164  "\n Used only for predefined projections above as required.");
165  clo_addOption(list, "azimuth", CLO_TYPE_FLOAT, NULL,
166  "projection rotation angle in deg north."
167  "\n Used only for predefined projections above as required.");
168  clo_addOption(list, "utm_zone", CLO_TYPE_STRING, NULL,
169  "UTM zone number."
170  "\n Used only for the UTM projection;"
171  "\n Append 'S' for southern hemisphere zones (e.g. 59S).");
172  clo_addOption(list, "north", CLO_TYPE_FLOAT, "-999",
173  "Northernmost Latitude (default: file north)");
174  clo_addOption(list, "south", CLO_TYPE_FLOAT, "-999",
175  "Southernmost Latitude (default: file south)");
176  clo_addOption(list, "east", CLO_TYPE_FLOAT, "-999",
177  "Easternmost Longitude (default: file east)");
178  clo_addOption(list, "west", CLO_TYPE_FLOAT, "-999",
179  "Westernmost Longitude (default: file west)");
180  clo_addOption(list, "trimNSEW", CLO_TYPE_BOOL, "yes",
181  "should we trim output"
182  "\n to match input NSEW range"
183  "\n");
184 
185  clo_addOption(list, "interp", CLO_TYPE_STRING, "nearest",
186  "interpolation method:"
187  "\n nearest: use the value of the nearest bin for the pixel"
188  "\n bin: bin all of the pixels that intersect the area of the"
189  "\n output pixel"
190  "\n area: bin weighted by area of all the pixels that intersect"
191  "\n the area of the output pixel"
192  "\n");
193 
194  // color table, scaling
195  clo_addOption(list, "apply_pal", CLO_TYPE_BOOL, "yes",
196  "apply color palette:"
197  "\n yes: color image"
198  "\n no: grayscale image");
199  clo_addOption(list, "palfile", CLO_TYPE_IFILE, NULL,
200  "palette filename (default from product.xml)");
201  clo_addOption(list, "use_transparency", CLO_TYPE_BOOL, "no",
202  "make missing data transparent (only valid for color PNG and TIFF)");
203  clo_addOption(list, "datamin", CLO_TYPE_FLOAT, NULL,
204  "minimum value for scaling (default from product.xml)");
205  clo_addOption(list, "datamax", CLO_TYPE_FLOAT, NULL,
206  "maximum value for scaling (default from product.xml)");
207  clo_addOption(list, "scale_type", CLO_TYPE_STRING, NULL,
208  "data scaling type (default from product.xml)"
209  "\n linear: linear scaling"
210  "\n log: logarithmic scaling"
211  "\n arctan: arc tangent scaling"
212  "\n");
213 
214  // other options
215  clo_addOption(list, "quiet", CLO_TYPE_BOOL, "false",
216  "stop the status printing");
217  clo_addOption(list, "pversion", CLO_TYPE_STRING, "Unspecified",
218  "processing version string");
219  clo_addOption(list, "use_quality", CLO_TYPE_BOOL, "yes",
220  "should we do quality factor processing");
221  clo_addOption(list, "quality_product", CLO_TYPE_STRING, NULL,
222  "product to use for quality factor processing");
223  clo_addOption(list, "use_rgb", CLO_TYPE_BOOL, "no",
224  "should we use product_rgb to make a"
225  "\n pseudo-true color image");
226  clo_addOption(list, "product_rgb", CLO_TYPE_STRING,
227  "rhos_670,rhos_555,rhos_412",
228  "\n Three products to use for RGB. Default is sensor-specific.");
229  clo_addOption(list, "fudge", CLO_TYPE_FLOAT, "1.0",
230  "fudge factor used to modify size of L3 pixels");
231  clo_addOption(list, "threshold", CLO_TYPE_FLOAT, "0",
232  "minimum percentage of filled pixels before"
233  "\n an image is generated");
234  clo_addOption(list, "num_cache", CLO_TYPE_INT, "500",
235  "number of rows to cache in memory.");
236  clo_addOption(list, "mask_land", CLO_TYPE_BOOL, "no",
237  "set land pixels to pixel value 254");
238  clo_addOption(list, "rgb_land", CLO_TYPE_STRING, "160,82,45",
239  "RGB value to use for land mask; comma separate string");
241  "$OCDATAROOT/common/landmask_GMT15ARC.nc", "land mask file");
242  clo_addOption(list, "full_latlon", CLO_TYPE_BOOL, "yes",
243  "write full latitude and longitude arrays."
244  "\n");
245 
246  return 0;
247 }
248 
249 int getSensorId(const char* fileName) {
250  idDS dsId;
251  int sensorId = -1;
252 
253  dsId = openDS(fileName);
254  if (dsId.fid == -1) {
255  printf("-E- %s: Input file '%s' does not exist or cannot open. \n",
256  __FILE__, fileName);
257  exit(EXIT_FAILURE);
258  }
259 
260  if (dsId.fftype == DS_NCDF) {
261  char* instrumentStr = readAttrStr(dsId, "instrument");
262  if (instrumentStr) {
263  char* platformStr = readAttrStr(dsId, "platform");
264  if (platformStr) {
265  sensorId = instrumentPlatform2SensorId(instrumentStr,
266  platformStr);
267  }
268  } else {
269  char* sensorStr = readAttrStr(dsId, "Sensor");
270  if (sensorStr) {
271  sensorId = sensorName2SensorId(sensorStr);
272  }
273  }
274  } else {
275  char* sensorNameStr = readAttrStr(dsId, "Sensor Name");
276  if (sensorNameStr) {
277  sensorId = sensorName2SensorId(sensorNameStr);
278  }
279  }
280 
281  if (sensorId == -1) {
282  printf("Did not find a valid sensor ID - using OCRVC as the sensor ID.\n");
283  sensorId = OCRVC;
284  }
285 
286  endDS(dsId);
287  return sensorId;
288 }
289 
290 /*
291  Read the command line option and all of the default parameter files.
292 
293  This is the order for loading the options:
294  - load the main program defaults file
295  - load the command line (including specified par files)
296  - re-load the command line disabling file descending so command
297  line arguments will over ride
298 
299  */
300 int l3mapgen_read_options(clo_optionList_t* list, int argc, char* argv[]) {
301  char *dataRoot;
302  char tmpStr[FILENAME_MAX];
303 
304  assert(list);
305 
306  if ((dataRoot = getenv("OCDATAROOT")) == NULL) {
307  fprintf(stderr, "-E- OCDATAROOT environment variable is not defined. \n");
308  return (-1);
309  }
310 
311  // disable the dump option until we have read all of the files
313 
314  // load program defaults
315  sprintf(tmpStr, "%s/common/l3mapgen_defaults.par", dataRoot);
316  clo_readFile(list, tmpStr);
317 
318  // read all arguments
319  clo_readArgs(list, argc, argv);
320 
321  // get sensor directory
322  int sensorId = getSensorId(clo_getString(list, "ifile"));
323  int subsensorId = sensorId2SubsensorId(sensorId);
324  const char* sensorDir = sensorId2SensorDir(sensorId);
325 
326  // load the sensor specific defaults file
327  sprintf(tmpStr, "%s/%s/l3mapgen_defaults.par", dataRoot, sensorDir);
328  int defaultLoaded = 0;
329  if (access(tmpStr, R_OK) != -1) {
330  if (want_verbose)
331  printf("Loading default parameters from %s\n", tmpStr);
332  clo_readFile(list, tmpStr);
333  defaultLoaded = 1;
334  }
335 
336  if (subsensorId != -1) {
337  sprintf(tmpStr, "%s/%s/%s/l3mapgen_defaults.par", dataRoot, sensorDir,
338  subsensorId2SubsensorDir(subsensorId));
339  if (access(tmpStr, R_OK) != -1) {
340  if (want_verbose)
341  printf("Loading default parameters from %s\n", tmpStr);
342  clo_readFile(list, tmpStr);
343  defaultLoaded = 1;
344  }
345  }
346 
347  if (!defaultLoaded) {
348  printf("-E- Failed to load sensor program defaults for %s \n",
349  sensorId2SensorName(sensorId));
350  exit(EXIT_FAILURE);
351  }
352 
353  // load the suite specific defaults file
354  clo_option_t *option = clo_findOption(list, "suite");
355  if (clo_isOptionSet(option)) {
356  int suiteLoaded = 0;
357  sprintf(tmpStr, "%s/common/l3mapgen_defaults_%s.par", dataRoot,
358  clo_getOptionString(option));
359  if (access(tmpStr, R_OK) != -1) {
360  if (want_verbose)
361  printf("Loading default parameters from %s\n", tmpStr);
362  clo_readFile(list, tmpStr);
363  suiteLoaded = 1;
364  }
365 
366  sprintf(tmpStr, "%s/%s/l3mapgen_defaults_%s.par", dataRoot, sensorDir,
367  clo_getOptionString(option));
368  if (access(tmpStr, R_OK) != -1) {
369  if (want_verbose)
370  printf("Loading default parameters from %s\n", tmpStr);
371  clo_readFile(list, tmpStr);
372  suiteLoaded = 1;
373  }
374 
375  if (subsensorId != -1) {
376  sprintf(tmpStr, "%s/%s/%s/l3mapgen_defaults_%s.par", dataRoot,
377  sensorDir, subsensorId2SubsensorDir(subsensorId),
378  clo_getOptionString(option));
379  if (access(tmpStr, R_OK) != -1) {
380  if (want_verbose)
381  printf("Loading default parameters from %s\n", tmpStr);
382  clo_readFile(list, tmpStr);
383  suiteLoaded = 1;
384  }
385  }
386 
387  if (!suiteLoaded) {
388  printf("-E- Failed to load parameters for suite %s for sensor %s\n", clo_getOptionString(option),
389  sensorId2SensorName(sensorId));
390  exit(EXIT_FAILURE);
391  }
392  }
393  // enable the dump option
395  // make the command line over ride
396  clo_readArgs(list, argc, argv);
397 
398  return 0;
399 }
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:240
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
int instrumentPlatform2SensorId(const char *instrument, const char *platform)
Definition: sensorInfo.c:302
int l3mapgen_init_options(clo_optionList_t *list, const char *softwareVersion)
idDS openDS(const char *filename)
Definition: wrapper.c:616
#define NULL
Definition: decode_rs.h:63
int sensorName2SensorId(const char *name)
Definition: sensorInfo.c:268
clo_option_t * clo_findOption(clo_optionList_t *list, const char *key)
Definition: clo.c:967
@ CLO_TYPE_FLOAT
Definition: clo.h:81
ds_format_t fftype
Definition: dfutils.h:31
int sensorId2SubsensorId(int sensorId)
Definition: sensorInfo.c:322
int clo_isOptionSet(clo_option_t *option)
Definition: clo.c:2257
@ CLO_TYPE_BOOL
Definition: clo.h:78
void clo_setVersion2(const char *programName, const char *versionStr)
Definition: clo.c:464
int l3mapgen_read_options(clo_optionList_t *list, int argc, char *argv[])
void clo_setEnableDumpOptions(int val)
Definition: clo.c:410
char * readAttrStr(idDS ds_id, const char *name)
Definition: wrapper.c:102
int getSensorId(const char *fileName)
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
@ DS_NCDF
Definition: dfutils.h:20
void clo_readFile(clo_optionList_t *list, const char *fileName)
Definition: clo.c:2210
const char * sensorId2SensorName(int sensorId)
Definition: sensorInfo.c:198
int32_t fid
Definition: dfutils.h:29
Definition: dfutils.h:28
const char * subsensorId2SubsensorDir(int subsensorId)
Definition: sensorInfo.c:254
#define OCRVC
Definition: sensorDefs.h:24
int endDS(idDS ds_id)
Definition: wrapper.c:634
@ CLO_TYPE_STRING
Definition: clo.h:83