ocssw  1.0
/disk01/web/ocssw/build/src/l2mapgen/l2mapgen_input.c (r8084/r6739)
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <ctype.h>
00005 #include <assert.h>
00006 #include <palette.h>
00007 #include <genutils.h>
00008 #include <clo.h>
00009 #include "l2mapgen.h"
00010 #include "l2mapgen_input.h"
00011 
00012 // should the par file processing decend into other par files
00013 static int enableFileDecending = 1;
00014 
00015 int input_init(instr *input_str) {
00016     input_str->ifile[0] = '\0';
00017     input_str->ofile[0] = '\0';
00018     input_str->palfile[0] = '\0';
00019     input_str->palette_dir[0] = '\0';
00020     input_str->product_table[0] = '\0';
00021     input_str->flaguse[0] = '\0';
00022     input_str->parms[0] = '\0';
00023 
00024     input_str->prod [0] = '\0';
00025     input_str->stype = 0;
00026     input_str->datamin = 0.0;
00027     input_str->datamax = 0.0;
00028 
00029     input_str->west = 0.0;
00030     input_str->east = 0.0;
00031     input_str->south = 0.0;
00032     input_str->north = 0.0;
00033     input_str->width = 800;
00034     input_str->threshold = 5.;
00035     input_str->mask = 0;
00036     input_str->quality = 2;
00037     input_str->apply_pal = 0;
00038     input_str->outmode = 1;
00039 
00040     return 0;
00041 }
00042 
00043 //-----------------------------------------------------------------------
00044 
00047 void par_option_cb(struct clo_option_t *option) {
00048     if (enableFileDecending)
00049         clo_readFile((clo_optionList_t*) option->cb_data, option->valStr);
00050 }
00051 
00052 //-----------------------------------------------------------------------
00053 
00055 int l2mapgen_init_options(clo_optionList_t* list) {
00056     char tmpStr[2048];
00057     clo_option_t* option;
00058     int i;
00059 
00060     sprintf(tmpStr, "l2mapgen %s (%s %s)", VERSION, __DATE__, __TIME__);
00061     clo_setVersion(tmpStr);
00062 
00063     sprintf(tmpStr, "Usage: l2mapgen argument-list\n\n");
00064 
00065     strcat(tmpStr, "  This program takes a product from a L2 file, maps it using a Plate\n");
00066     strcat(tmpStr, "  Carree cylindrical projection, and produces a gray scale PGM or\n");
00067     strcat(tmpStr, "  color PPM file.\n\n");
00068 
00069     strcat(tmpStr, "  The argument-list is a set of keyword=value pairs. The arguments can\n");
00070     strcat(tmpStr, "  be specified on the commandline, or put into a parameter file, or the\n");
00071     strcat(tmpStr, "  two methods can be used together, with commandline over-riding.\n\n");
00072     strcat(tmpStr, "The list of valid keywords follows:\n");
00073     clo_setHelpStr(tmpStr);
00074 
00075     // add the par option and add the callback function
00076     option = clo_addOption(list, "par", CLO_TYPE_STRING, NULL, "input parameter file");
00077     option->cb_data = (void*) list;
00078     option->cb = par_option_cb;
00079 
00080     clo_addOption(list, "ifile", CLO_TYPE_IFILE, NULL, "input L2 file name or file with a list of files names");
00081     clo_addOption(list, "ofile", CLO_TYPE_OFILE, NULL, "output map filename (NULL=STDOUT)");
00082     clo_addOption(list, "prod", CLO_TYPE_STRING, NULL, "product name");
00083     clo_addOption(list, "apply_pal", CLO_TYPE_BOOL, "false", "apply color palette, false = grayscale");
00084     clo_addOption(list, "palfile", CLO_TYPE_IFILE, "default", "palette filename");
00085     clo_addOption(list, "palette_dir", CLO_TYPE_IFILE, "$OCDATAROOT/common/palette", "palette directory");
00086     clo_addOption(list, "product_table", CLO_TYPE_IFILE, "$OCDATAROOT/common/smigen_product_table.dat", "product table");
00087     clo_addOption(list, "flaguse", CLO_TYPE_STRING, NULL, "flags to be masked");
00088     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");
00089     clo_addOption(list, "mask", CLO_TYPE_BOOL, "no", "apply mask to land, cloud and glint (see below)");
00090     clo_addOption(list, "datamin", CLO_TYPE_FLOAT, "0.0", "minimum value for data scaling\n        (default see SMI product table)");
00091     clo_addOption(list, "datamax", CLO_TYPE_FLOAT, "0.0", "maximum value for data scaling\n        (default see SMI product table)");
00092     clo_addOption(list, "stype", CLO_TYPE_INT, "0", "scaling type (default see SMI product table)\n        1: LINEAR\n        2: LOG");
00093     clo_addOption(list, "east", CLO_TYPE_FLOAT, "0.0", "Map East longitude\n        (default=scene(s) Easternmost Longitude)");
00094     clo_addOption(list, "west", CLO_TYPE_FLOAT, "0.0", "Map West longitude\n        (default=scene(s) Westernmost Longitude)");
00095     clo_addOption(list, "north", CLO_TYPE_FLOAT, "0.0", "Map North latitude\n        (default=scene(s) Northernmost Longitude)");
00096     clo_addOption(list, "south", CLO_TYPE_FLOAT, "0.0", "Map South latitude\n        (default=scene(s) Southernmost Longitude)");
00097     clo_addOption(list, "width", CLO_TYPE_INT, "800", "width of the output image");
00098     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");
00099     strcpy(tmpStr, "format of the output file\n");
00100     strcat(tmpStr, "        ppm: PPM or PGM image file (alias 1)\n");
00101     strcat(tmpStr, "        png: PNG color or grayscale image file (alias 2)\n");
00102     strcat(tmpStr, "        tiff: TIFF color or grayscale geo tiff image file (alias 3)\n");
00103     clo_addOption(list, "outmode", CLO_TYPE_STRING, "ppm", tmpStr);
00104 
00105     strcpy(tmpStr, "\n   If the \"mask\" option is set, the output PGM image will be masked for\n");
00106     strcat(tmpStr, "   flags defined in the flaguse parameter. The \"no data\" pixel value will\n");
00107     strcat(tmpStr, "   change from 0 to 255, and pixel values 252, 253, and 254 will represent the\n");
00108     strcat(tmpStr, "   sunglint, land, and all other (e.g. clouds/ice,hilt,atmfail,navfail,chlfail)\n");
00109     strcat(tmpStr, "   masks, respectively. NOTE: sunglint is NOT masked by default, but if it is\n");
00110     strcat(tmpStr, "   added to the flaguse parameter, it will be distinguished in the masking as\n");
00111     strcat(tmpStr, "   medium gray.  If a palette is applied and the mask option is set, the\n");
00112     strcat(tmpStr, "   palette values will be modified:\n");
00113     strcat(tmpStr, "                  Value   R       G       B\n");
00114     strcat(tmpStr, "                  252     128     128     128\n");
00115     strcat(tmpStr, "                  253     160     82      45\n");
00116     strcat(tmpStr, "                  254     255     255     255\n");
00117     strcat(tmpStr, "                  255     0       0       0\n\n");
00118     strcat(tmpStr, "   By default, this program sends its results to standard output as a\n");
00119     strcat(tmpStr, "   PGM-formatted binary data stream.  Save it to a file via \">\" or pipe it\n");
00120     strcat(tmpStr, "   to your favorite image display program.  The output image is rendered in\n");
00121     strcat(tmpStr, "   a Plate Carree projection.");
00122     clo_addOption(list, "help1", CLO_TYPE_HELP, NULL, tmpStr);
00123 
00124     return 0;
00125 }
00126 
00127 //-----------------------------------------------------------------------
00128 
00129 /* 
00130    Read the command line option and all of the default parameter files.
00131 
00132    This is the order for loading the options:
00133     - load the main program defaults file
00134     - load the command line (including specified par files)
00135     - re-load the command line disabling file decending so command
00136        line arguments will over ride
00137 
00138  */
00139 int l2mapgen_read_options(clo_optionList_t* list, int argc, char* argv[]) {
00140     char *dataRoot;
00141     char tmpStr[FILENAME_MAX];
00142     int i;
00143 
00144     assert(list);
00145 
00146     if ((dataRoot = getenv("OCDATAROOT")) == NULL) {
00147         fprintf(stderr, "-E- OCDATAROOT environment variable is not defined.\n");
00148         return (-1);
00149     }
00150 
00151     // disable the dump option until we have read all of the files
00152     clo_setEnableDumpOptions(0);
00153 
00154     // make sure file decending is turned on
00155     enableFileDecending = 1;
00156 
00157     // load program defaults
00158     sprintf(tmpStr, "%s/common/l2mapgen_defaults.par", dataRoot);
00159     if (want_verbose) {
00160         fprintf(stderr, "Loading default parameters from %s\n", tmpStr);
00161     }
00162     clo_readFile(list, tmpStr);
00163 
00164     // read all arguments including decending par files
00165     clo_readArgs(list, argc, argv);
00166 
00167     // if a file was decended, make sure the command args over-rides
00168     enableFileDecending = 0;
00169 
00170     // enable the dump option
00171     clo_setEnableDumpOptions(1);
00172 
00173     clo_readArgs(list, argc, argv);
00174     enableFileDecending = 1;
00175 
00176     return 0;
00177 }
00178 
00179 //-----------------------------------------------------------------------------
00180 
00181 int l2mapgen_load_input(clo_optionList_t *list, instr *input) {
00182     char tmp_file[FILENAME_MAX];
00183     char *strVal;
00184     clo_option_t *option;
00185     int numOptions;
00186     int optionId;
00187     char keyword[FILENAME_MAX];
00188     int count;
00189     char **strArray;
00190     float *fArray;
00191     int *iArray;
00192     int i, j;
00193     FILE *fp;
00194 
00195     numOptions = clo_getNumOptions(list);
00196     for (optionId = 0; optionId < numOptions; optionId++) {
00197         option = clo_getOption(list, optionId);
00198         strcpy(keyword, option->key);
00199 
00200         /* change keyword to lower case */
00201         strVal = keyword;
00202         while (*strVal != '\0') {
00203             *strVal = tolower(*strVal);
00204             strVal++;
00205         }
00206 
00207         if (strcmp(keyword, "-help") == 0)
00208             ;
00209         else if (strcmp(keyword, "-version") == 0)
00210             ;
00211         else if (strcmp(keyword, "-dump_options") == 0)
00212             ;
00213         else if (strcmp(keyword, "-dump_options_paramfile") == 0)
00214             ;
00215         else if (strcmp(keyword, "-dump_options_xmlfile") == 0)
00216             ;
00217         else if (strcmp(keyword, "help1") == 0)
00218             ;
00219         else if (strcmp(keyword, "par") == 0)
00220             ;
00221         else if (strcmp(keyword, "ifile") == 0) {
00222             strVal = clo_getOptionString(option);
00223             parse_file_name(strVal, tmp_file);
00224             strcpy(input->ifile, tmp_file);
00225 
00226         } else if (strcmp(keyword, "ofile") == 0) {
00227             if (clo_isOptionSet(option)) {
00228                 strVal = clo_getOptionString(option);
00229                 parse_file_name(strVal, tmp_file);
00230                 strcpy(input->ofile, tmp_file);
00231             }
00232 
00233         } else if (strcmp(keyword, "palfile") == 0) {
00234             strVal = clo_getOptionString(option);
00235             parse_file_name(strVal, tmp_file);
00236             strcpy(input->palfile, tmp_file);
00237 
00238         } else if (strcmp(keyword, "palette_dir") == 0) {
00239             strVal = clo_getOptionString(option);
00240             parse_file_name(strVal, tmp_file);
00241             strcpy(input->palette_dir, tmp_file);
00242 
00243         } else if (strcmp(keyword, "product_table") == 0) {
00244             strVal = clo_getOptionString(option);
00245             parse_file_name(strVal, tmp_file);
00246             strcpy(input->product_table, tmp_file);
00247 
00248         } else if (strcmp(keyword, "flaguse") == 0) {
00249             if (clo_isOptionSet(option)) {
00250                 strArray = clo_getOptionStrings(option, &count);
00251                 input->flaguse[0] = '\0';
00252                 for (i = 0; i < count; i++) {
00253                     if (strcasecmp("default", strArray[i]) == 0) {
00254                         if (input->flaguse[0] != '\0')
00255                             strcat(input->flaguse, ",");
00256                         strcat(input->flaguse, DEF_FLAG);
00257                     } else {
00258                         if (input->flaguse[0] != '\0')
00259                             strcat(input->flaguse, ",");
00260                         strcat(input->flaguse, strArray[i]);
00261                     }
00262 
00263                 } // for count
00264             } // if option set
00265 
00266         } else if (strcmp(keyword, "prod") == 0) {
00267             strVal = clo_getOptionString(option);
00268             parse_file_name(strVal, tmp_file);
00269             strcpy(input->prod, tmp_file);
00270 
00271         } else if (strcmp(keyword, "stype") == 0) {
00272             input->stype = clo_getOptionInt(option);
00273 
00274         } else if (strcmp(keyword, "width") == 0) {
00275             input->width = clo_getOptionInt(option);
00276 
00277         } else if (strcmp(keyword, "threshold") == 0) {
00278             input->threshold = clo_getOptionFloat(option);
00279 
00280         } else if (strcmp(keyword, "apply_pal") == 0) {
00281             input->apply_pal = clo_getOptionBool(option);
00282 
00283         } else if (strcmp(keyword, "mask") == 0) {
00284             input->mask = clo_getOptionBool(option);
00285 
00286         } else if (strcmp(keyword, "quality") == 0) {
00287             input->quality = clo_getOptionInt(option);
00288 
00289         } else if (strcmp(keyword, "datamin") == 0) {
00290             input->datamin = clo_getOptionFloat(option);
00291 
00292         } else if (strcmp(keyword, "datamax") == 0) {
00293             input->datamax = clo_getOptionFloat(option);
00294 
00295         } else if (strcmp(keyword, "west") == 0) {
00296             input->west = clo_getOptionFloat(option);
00297 
00298         } else if (strcmp(keyword, "east") == 0) {
00299             input->east = clo_getOptionFloat(option);
00300 
00301         } else if (strcmp(keyword, "south") == 0) {
00302             input->south = clo_getOptionFloat(option);
00303 
00304         } else if (strcmp(keyword, "north") == 0) {
00305             input->north = clo_getOptionFloat(option);
00306 
00307         } else if (strcmp(keyword, "outmode") == 0) {
00308             char *str = clo_getOptionString(option);
00309             if (strcmp("1", str) == 0) {
00310                 input->outmode = 1;
00311             } else if (strcasecmp("ppm", str) == 0) {
00312                 input->outmode = 1;
00313             } else if (strcmp("2", str) == 0) {
00314                 input->outmode = 2;
00315             } else if (strcasecmp("png", str) == 0) {
00316                 input->outmode = 2;
00317             } else if (strcmp("3", str) == 0) {
00318                 input->outmode = 3;
00319             } else if (strcasecmp("tiff", str) == 0) {
00320                 input->outmode = 3;
00321             } else {
00322                 fprintf(stderr, "Invalid value for outmode \"%s\"\n", str);
00323                 return -1;
00324             }
00325 
00326         } else {
00327             fprintf(stderr, "Invalid argument \"%s\"\n", keyword);
00328             return -1;
00329         }
00330     } // for optionId
00331 
00332     return 0;
00333 }
00334 
00335 /*-----------------------------------------------------------------------------
00336     Function:  msmapl2_input
00337 
00338     Returns:   int (status)
00339         The return code is a negative value if any error occurs, otherwise,
00340         returns 0.
00341 
00342     Description:
00343         Convert the arguments from the command line into a structure input
00344         variable.
00345 
00346     Parameters: (in calling order)
00347         Type      Name         I/O   Description
00348         ----      ----         ---   -----------
00349         int       argc          I   number of arguments
00350         char      **argv        I   list of arguments
00351         instr     input         O   structure variable for inputs
00352 
00353 ----------------------------------------------------------------------------*/
00354 
00355 int l2mapgen_input(int argc, char **argv, clo_optionList_t* list, instr* input) {
00356     int i;
00357     char str_buf[4096];
00358 
00359 
00360     /* initialize the option list with descriptions and default values */
00361     l2mapgen_init_options(list);
00362 
00363     /*                                                                  */
00364     /* Set input values to defaults                                     */
00365     /*                                                                  */
00366     if (input_init(input) != 0) {
00367         fprintf(stderr, "-E- %s: Error initializing input structure.\n", __FILE__);
00368         return (-1);
00369     }
00370 
00371     /* read the command line options into list */
00372     if (l2mapgen_read_options(list, argc, argv) != 0) {
00373         fprintf(stderr, "-E- %s: Error reading program options.\n", __FILE__);
00374         return (-1);
00375     }
00376 
00377     /* load options from list into input structure */
00378     if (l2mapgen_load_input(list, input) != 0) {
00379         fprintf(stderr, "-E- %s: Error loading options into input structure.\n", __FILE__);
00380         return (-1);
00381     }
00382 
00383     /*                                                                  */
00384     /* Build string of parameters for metadata                          */
00385     /*                                                                  */
00386     sprintf(str_buf, "IFILE=%s|", input->ifile);
00387     strcat(input->parms, str_buf);
00388     sprintf(str_buf, "OFILE=%s|", input->ofile);
00389     strcat(input->parms, str_buf);
00390     sprintf(str_buf, "PALFILE=%s|", input->palfile);
00391     strcat(input->parms, str_buf);
00392     sprintf(str_buf, "PALETTE_DIR=%s|", input->palette_dir);
00393     strcat(input->parms, str_buf);
00394     sprintf(str_buf, "PRODUCT_TABLE=%s|", input->product_table);
00395     strcat(input->parms, str_buf);
00396     sprintf(str_buf, "FLAGUSE=%s|", input->flaguse);
00397     strcat(input->parms, str_buf);
00398     sprintf(str_buf, "PROD=%s|", input->prod);
00399     strcat(input->parms, str_buf);
00400     sprintf(str_buf, "STYPE=%d|", input->stype);
00401     strcat(input->parms, str_buf);
00402     sprintf(str_buf, "DATAMIN=%f|", input->datamin);
00403     strcat(input->parms, str_buf);
00404     sprintf(str_buf, "DATAMAX=%f|", input->datamax);
00405     strcat(input->parms, str_buf);
00406     sprintf(str_buf, "WEST=%f|", input->west);
00407     strcat(input->parms, str_buf);
00408     sprintf(str_buf, "EAST=%f|", input->east);
00409     strcat(input->parms, str_buf);
00410     sprintf(str_buf, "SOUTH=%f|", input->south);
00411     strcat(input->parms, str_buf);
00412     sprintf(str_buf, "NORTH=%f|", input->north);
00413     strcat(input->parms, str_buf);
00414     sprintf(str_buf, "WIDTH=%d|", input->width);
00415     strcat(input->parms, str_buf);
00416     sprintf(str_buf, "THRESHOLD=%f|", input->threshold);
00417     strcat(input->parms, str_buf);
00418     sprintf(str_buf, "MASK=%d|", input->mask);
00419     strcat(input->parms, str_buf);
00420     sprintf(str_buf, "QUALITY=%d|", input->quality);
00421     strcat(input->parms, str_buf);
00422     sprintf(str_buf, "APPLY_PAL=%d|", input->apply_pal);
00423     strcat(input->parms, str_buf);
00424 
00425     return 0;
00426 }