ocssw  1.0
/disk01/web/ocssw/build/src/smigen/smigen_input.c (r8218/r4879)
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 "smigen_input.h"
00006 #include "smiinc.h"
00007 #include "palette.h"
00008 #include "genutils.h"
00009 
00010 int get_item(char *arg, instr *input_str);
00011 int par_file(char *pfile, instr *input_str);
00012 int get_def_l2prod(char *def_file, instr *input_str);
00013 
00014 int input_init(instr *input_str)
00015 {
00016   input_str->ifile[0] = '\0';
00017   input_str->ofile[0] = '\0';
00018   input_str->pfile[0] = '\0';
00019   input_str->prod [0] = '\0';
00020   input_str->parms[0] = '\0';
00021 
00022   input_str->stype    =  0;
00023   input_str->meas     =  1;
00024   input_str->datamin  =  0.0;
00025   input_str->datamax  =  0.0;
00026 
00027   input_str->lonwest  = -180.0;
00028   input_str->loneast  = +180.0;
00029   input_str->latsouth = -90.0;
00030   input_str->latnorth = +90.0;
00031 
00032   strcpy(input_str->pversion,"Unspecified");
00033   strcpy(input_str->projection,"RECT");
00034   strcpy(input_str->resolution,"9km");
00035   strcpy(input_str->palfile, "DEFAULT");
00036 
00037   input_str->gap_fill = 0;
00038   input_str->seam_lon = -180;
00039 
00040   input_str->proddesc[0] = '\0';
00041   input_str->units[0] = '\0';
00042   input_str->precision[0] = '\0';
00043 
00044   input_str->minobs = 0;
00045 
00046   return 0;
00047 }
00048 
00049 
00050 
00051 /*-----------------------------------------------------------------------------
00052     Function:  smigen_input
00053 
00054     Returns:   int (status)
00055         The return code is a negative value if any error occurs, otherwise,
00056         returns 0.
00057 
00058     Description:
00059         Convert the arguments from the command line into a structure input
00060         variable.
00061 
00062     Parameters: (in calling order)
00063         Type      Name         I/O   Description
00064         ----      ----         ---   -----------
00065         int       argc          I   number of arguments
00066         char      **argv        I   list of arguments
00067         instr     input         O   structure variable for inputs
00068 
00069 ----------------------------------------------------------------------------*/
00070 
00071 int smigen_input(int argc, char **argv, instr *input)
00072 {
00073   int  i;
00074   char str_buf[4096];
00075 
00076   if (argc == 1) return(-1);
00077 
00078   /*                                                                  */
00079   /* Set input values to defaults                                     */
00080   /*                                                                  */
00081   if ( input_init( input ) != 0 ) {
00082       printf("-E- %s: Error initializing input structure.\n",__FILE__);
00083       return(-1);
00084   }
00085 
00086   /*                                                                  */
00087   /* Loop through command arguments and update input struct           */
00088   /*                                                                  */
00089   for (i=1; i<argc; i++)
00090     if (get_item(argv[i], input) != 0)
00091       return -1;
00092 
00093 
00094 #if 0
00095   /* Define default palette */
00096   /* ---------------------- */
00097   if (strcmp(input->prod, "NDVI") == 0 || 
00098       strcmp(input->prod, "EVI") == 0)
00099     memcpy(input->palette,ndvi_palette,768);
00100   else if (strcmp(input->prod, "sst") == 0 ||
00101            strcmp(input->prod, "sst4") == 0)
00102     memcpy(input->palette,sst_palette,768);
00103   else
00104     memcpy(input->palette,default_palette,768);
00105 
00106 
00107   /*                                                                  */
00108   /* Add any input-dependent defaults                                 */
00109   /*                                                                  */
00110   if (strcmp(input->palfile,"DEFAULT") != 0) {
00111       short *r, *g, *b;
00112       if (!(r = (short *) calloc(256, sizeof(short)))) {
00113          fprintf(stderr,"smigen: Error allocating space for red.\n");
00114          return -1; 
00115       }; 
00116       if (!(g = (short *) calloc(256, sizeof(short)))) {
00117          fprintf(stderr,"smigen: Error allocating space for green.\n");
00118          return -1; 
00119       }; 
00120       if (!(b = (short *) calloc(256, sizeof(short)))) { 
00121          fprintf(stderr,"smigen: Error allocating space for blue.\n");
00122          return -1; 
00123       }; 
00124       if (getlut_file(input->palfile, r, g, b)) {
00125           fprintf(stderr,"Error reading palette file %s\n",input->pfile);
00126           return -1;
00127       }
00128       for (i=0; i<256; i++) {
00129           input->palette[i*3]   = r[i];
00130           input->palette[i*3+1] = g[i];
00131           input->palette[i*3+2] = b[i];
00132       }
00133       free(r); 
00134       free(g); 
00135       free(b); 
00136   }
00137 #endif
00138 
00139 
00140   /*                                                                  */
00141   /* Build string of parameters for metadata                          */
00142   /*                                                                  */
00143   sprintf(str_buf, "IFILE = %s|",input->ifile);
00144   strcat(input->parms, str_buf);
00145   sprintf(str_buf, "OFILE = %s|",input->ofile);
00146   strcat(input->parms, str_buf);
00147   sprintf(str_buf, "PFILE = %s|",input->pfile);
00148   strcat(input->parms, str_buf);
00149   sprintf(str_buf, "PROD = %s|",input->prod);
00150   strcat(input->parms, str_buf);
00151   sprintf(str_buf, "PALFILE = %s|",input->palfile);
00152   strcat(input->parms, str_buf);
00153   sprintf(str_buf, "PROCESSING VERSION = %s|",input->pversion);
00154   strcat(input->parms, str_buf);
00155   sprintf(str_buf, "MEAS = %d|",input->meas);
00156   strcat(input->parms, str_buf);
00157   sprintf(str_buf, "STYPE = %d|",input->stype);
00158   strcat(input->parms, str_buf);
00159   sprintf(str_buf, "DATAMIN = %f|",input->datamin);
00160   strcat(input->parms, str_buf);
00161   sprintf(str_buf, "DATAMAX = %f|",input->datamax);
00162   strcat(input->parms, str_buf);
00163   sprintf(str_buf, "LONWEST = %f|",input->lonwest);
00164   strcat(input->parms, str_buf);
00165   sprintf(str_buf, "LONEAST = %f|",input->loneast);
00166   strcat(input->parms, str_buf);
00167   sprintf(str_buf, "LATSOUTH = %f|",input->latsouth);
00168   strcat(input->parms, str_buf);
00169   sprintf(str_buf, "LATNORTH = %f|",input->latnorth);
00170   strcat(input->parms, str_buf);
00171   sprintf(str_buf, "RESOLUTION = %s|",input->resolution);
00172   strcat(input->parms, str_buf);
00173   sprintf(str_buf, "PROJECTION = %s|",input->projection);
00174   strcat(input->parms, str_buf);
00175   sprintf(str_buf, "GAP_FILL = %d|",input->gap_fill);
00176   strcat(input->parms, str_buf);
00177   sprintf(str_buf, "SEAM_LON = %f|",input->seam_lon);
00178   strcat(input->parms, str_buf);
00179   sprintf(str_buf, "PRECISION = %s|",input->precision);
00180   strcat(input->parms, str_buf);
00181   sprintf(str_buf, "MINOBS = %d|",input->minobs);
00182   strcat(input->parms, str_buf);
00183   return 0;
00184 }
00185 
00186 
00187 
00188 
00189 int get_item(char *arg, instr *input)
00190 {
00191   char *tmp_str; 
00192   char keyword [20]; 
00193   char parm_str[4096];
00194   char tmp_file[FILENAME_MAX];
00195   int  ilen1, iret;
00196   static int  lon_limit_used = 0;
00197   static int  seam_used = 0;
00198 
00199   if ((tmp_str = strchr(arg, '=')) == NULL) {
00200     printf("Invalid argument \"%s\"\n", arg);
00201     return -1;
00202   }
00203 
00204   ilen1 = tmp_str-arg;
00205   strncpy(keyword, arg, ilen1);
00206   keyword[ilen1] = '\0';
00207   strcpy(parm_str, tmp_str+1);
00208 
00209   /* change keyword to lower case */
00210   tmp_str = keyword;
00211   while (*tmp_str != '\0') {
00212     if (isupper(*tmp_str)) *tmp_str = tolower(*tmp_str);
00213     tmp_str++;
00214   }
00215 
00216   if (strcmp(keyword, "par") == 0) {
00217     iret = par_file(parm_str, input);
00218     return iret;
00219   
00220   } else if (strcmp(keyword, "ifile") == 0) {
00221     parse_file_name(parm_str, tmp_file);
00222     strcpy(input->ifile, tmp_file);
00223 
00224   } else if (strcmp(keyword, "ofile") == 0) {
00225     parse_file_name(parm_str, tmp_file);
00226     strcpy(input->ofile, tmp_file);
00227 
00228   } else if (strcmp(keyword, "pfile") == 0) {
00229     parse_file_name(parm_str, tmp_file);
00230     strcpy(input->pfile, tmp_file);
00231 
00232   } else if (strcmp(keyword, "prod") == 0) {
00233     parse_file_name(parm_str, tmp_file);
00234     strcpy(input->prod, tmp_file);
00235 
00236   } else if (strcmp(keyword, "palfile") == 0) {
00237     parse_file_name(parm_str, tmp_file);
00238     strcpy(input->palfile, tmp_file);
00239 
00240   } else if (strcmp(keyword, "pversion") == 0) {
00241     parse_file_name(parm_str, tmp_file);
00242     strcpy(input->pversion, tmp_file);
00243 
00244   } else if (strcmp(keyword, "stype") == 0) {
00245     input->stype = atoi(parm_str);
00246 
00247   } else if (strcmp(keyword, "meas") == 0) {
00248     input->meas = atoi(parm_str);
00249     if (input->meas == 4) strcpy(input->prod, "pixels");
00250     if (input->meas == 5) strcpy(input->prod, "scenes");
00251 
00252   } else if (strcmp(keyword, "datamin") == 0) {
00253     input->datamin = atof(parm_str);
00254 
00255   } else if (strcmp(keyword, "datamax") == 0) {
00256     input->datamax = atof(parm_str);
00257 
00258   } else if (strcmp(keyword, "lonwest") == 0) {
00259     input->lonwest = atof(parm_str);
00260     lon_limit_used = 1;
00261 
00262   } else if (strcmp(keyword, "loneast") == 0) {
00263     input->loneast = atof(parm_str);
00264     lon_limit_used = 1;
00265 
00266   } else if (strcmp(keyword, "latsouth") == 0) {
00267     input->latsouth = atof(parm_str);
00268 
00269   } else if (strcmp(keyword, "latnorth") == 0) {
00270     input->latnorth = atof(parm_str);
00271 
00272   } else if (strcmp(keyword, "resolution") == 0) {
00273     parse_file_name(parm_str, tmp_file);
00274     strcpy(input->resolution, tmp_file);
00275 
00276   } else if (strcmp(keyword, "projection") == 0) {
00277     parse_file_name(parm_str, tmp_file);
00278     strcpy(input->projection, tmp_file);
00279 
00280   } else if (strcmp(keyword, "gap_fill") == 0) {
00281     input->gap_fill = atoi(parm_str);
00282 
00283   } else if (strcmp(keyword, "seam_lon") == 0) {
00284     input->seam_lon = atof(parm_str);
00285     seam_used = 1;
00286 
00287   } else if (strcmp(keyword, "proddesc") == 0) {
00288     parse_file_name(parm_str, tmp_file);
00289     strcpy(input->proddesc, tmp_file);
00290 
00291   } else if (strcmp(keyword, "units") == 0) {
00292     parse_file_name(parm_str, tmp_file);
00293     strcpy(input->units, tmp_file);
00294 
00295   } else if (strcmp(keyword, "precision") == 0) {
00296     parse_file_name(parm_str, tmp_file);
00297     strcpy(input->precision, tmp_file);
00298 
00299   } else if (strcmp(keyword, "minobs") == 0) {
00300     input->minobs = atol(parm_str);
00301 
00302   } else {
00303     goto Invalid_return;
00304 
00305   }
00306 
00307   if (lon_limit_used == 1 && seam_used == 1) {
00308     printf("LONEAST/LONWEST cannot be used with SEAM_LON.\n");
00309     exit(1);
00310   }
00311 
00312   return 0;
00313 
00314 
00315 Invalid_return:
00316   printf("Invalid argument \"%s\"\n", arg);
00317   return -1;
00318 }
00319 
00320 
00321 int par_file(char *pfile, instr *input)
00322 {
00323   FILE *fp;
00324   char arg[2048];
00325   int  iret;
00326 
00327   if ((fp = fopen(pfile, "r")) == NULL) {
00328     printf("Error on opening the parameter file - %s\n", pfile);
00329     return -1;
00330   }
00331 
00332   while ((fgets(arg, 2047, fp)) != NULL) {
00333     /* skip the comment or blank line */
00334     if (arg[0] == '#' || arg[0] == ';' || arg[0] == ' ' || arg[0] == '\0' ||
00335         arg[0] == '\n')   
00336       continue;
00337 
00338     arg[strlen(arg)-1] = '\0';    /* replace the last char new_line to NULL */
00339     iret = get_item(arg, input);
00340     if (iret != 0) {
00341       fclose(fp);
00342       return -1;
00343     }
00344   }
00345 
00346   fclose(fp);
00347   return 0;
00348 }