IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 33115


Ignore:
Timestamp:
Jan 18, 2012, 8:31:56 AM (15 years ago)
Author:
eugene
Message:

adding int and time types to read function

Location:
branches/eam_branches/ipp-20111122/Ohana/src/opihi/cmd.data
Files:
5 added
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20111122/Ohana/src/opihi/cmd.data/read_vectors.c

    r32846 r33115  
    2121}
    2222
     23// vector types
     24enum {COLTYPE_NONE, COLTYPE_FLT, COLTYPE_INT, COLTYPE_TIME};
     25
    2326int read_vectors (int argc, char **argv) {
    2427 
    25     int i, j, Nskip, Nvec, *col, done, status, IsCSV, VERBOSE;
    26   int Nbytes, nbytes, Nstart, NELEM, N, nread;
     28  int TimeFormat;
     29  time_t TimeReference;
     30  int i, j, Nskip, Narg, Nvec, *col, IsCSV, VERBOSE;
     31  int Nbytes, Nstart, NELEM, Nelem, nread, *coltype;
    2732  char *colstr, *c0, *c1, *buffer, *extname;
    28   double value;
    2933  Vector **vec;
    3034
    3135  /* auto-sense table type */
    32   if ((N = get_argument (argc, argv, "-fits"))) {
    33     remove_argument (N, &argc, argv);
    34     extname = strcreate (argv[N]);
     36  if ((Narg = get_argument (argc, argv, "-fits"))) {
     37    remove_argument (Narg, &argc, argv);
     38    extname = strcreate (argv[Narg]);
    3539    if (extname == (char *) NULL) return (FALSE);
    36     remove_argument (N, &argc, argv);
    37     status = read_table_vectors (argc, argv, extname);
     40    remove_argument (Narg, &argc, argv);
     41    int status = read_table_vectors (argc, argv, extname);
    3842    free (extname);
    3943    return (status);
     
    4145
    4246  Nskip = 0;
    43   if ((N = get_argument (argc, argv, "-skip"))) {
    44     remove_argument (N, &argc, argv);
    45     Nskip = atof (argv[N]);
    46     remove_argument (N, &argc, argv);
     47  if ((Narg = get_argument (argc, argv, "-skip"))) {
     48    remove_argument (Narg, &argc, argv);
     49    Nskip = atof (argv[Narg]);
     50    remove_argument (Narg, &argc, argv);
    4751  }
    4852
    4953  IsCSV = FALSE;
    50   if ((N = get_argument (argc, argv, "-csv"))) {
    51     remove_argument (N, &argc, argv);
     54  if ((Narg = get_argument (argc, argv, "-csv"))) {
     55    remove_argument (Narg, &argc, argv);
    5256    IsCSV = TRUE;
    5357  }
    5458
    5559  VERBOSE = FALSE;
    56   if ((N = get_argument (argc, argv, "-v"))) {
    57     remove_argument (N, &argc, argv);
     60  if ((Narg = get_argument (argc, argv, "-v"))) {
     61    remove_argument (Narg, &argc, argv);
    5862    VERBOSE = TRUE;
    5963  }
     
    6569  /* read name N name N  */
    6670
     71  // do this only optionally?
     72  GetTimeFormat (&TimeReference, &TimeFormat);
     73
    6774  if (f == (FILE *) NULL) {
    6875    gprint (GP_ERR, "no open file for read\n");
     
    7178  fseeko (f, 0LL, SEEK_SET);
    7279
    73 //  if (IsCSV) {
    74 //    status = read_vectors_csv (argc, argv, Nskip, f);
    75 //    return status;
    76 //  }
    77 
    7880  Nvec = (argc - 1) / 2;
    7981  ALLOCATE (vec, Vector *, Nvec);
    8082  ALLOCATE (col, int, Nvec);
     83  ALLOCATE (coltype, int, Nvec);
    8184
    8285  for (i = 0; i < Nvec; i++) {
     86
     87    // interpret the column names including type flags
     88    // XXX review the grammar before releasing this : is foo:type best, or is something else needed?
     89    // Note the conflict wrt list entries
     90    // the name may be of the form foo:type, where type may be one of : int, float, time
     91   
     92    coltype[i] = COLTYPE_FLT;
     93    char *colname = argv[2*i + 1];
     94    char *ptr = strchr (colname, ':');
     95    if (ptr) {
     96      // split out colname and type
     97      *ptr = 0;
     98      ptr ++;
     99      if (!ptr) goto bad_colname;
     100      coltype[i] = COLTYPE_NONE;
     101      if (!strcasecmp(ptr, "float")) { coltype[i] = COLTYPE_FLT; }
     102      if (!strcasecmp(ptr, "int"))   { coltype[i] = COLTYPE_INT; }
     103      if (!strcasecmp(ptr, "time"))  { coltype[i] = COLTYPE_TIME; }
     104      if (!coltype[i]) goto bad_colname;
     105    }
     106
    83107    if ((vec[i] = SelectVector (argv[2*i + 1], ANYVECTOR, TRUE)) == NULL) {
    84108      gprint (GP_ERR, "USAGE: read name N name N ...\n");
     
    87111      return (FALSE);   
    88112    }
    89     // XXX we could allow flags (eg, N.i) to specify INT vs FLT types vectors...
     113
    90114    colstr = argv[2*i+2];
    91115    for (j = 0; j < strlen (colstr); j++) {
     
    119143  NELEM = 1000;
    120144  for (i = 0; i < Nvec; i++) {
    121     ResetVector (vec[i], OPIHI_FLT, NELEM);
     145    if (coltype[i] == COLTYPE_INT) {
     146      ResetVector (vec[i], OPIHI_INT, NELEM);
     147    } else {
     148      // note that COLTYPE_TIME is a type of float
     149      ResetVector (vec[i], OPIHI_FLT, NELEM);
     150    }
    122151  }
    123152 
     
    128157  }
    129158
    130   Nstart = 0;
    131   N = 0;
    132   done = FALSE;
    133   while (!done) {
     159  // we have a working buffer read from the file. we parse the lines in the working buffer
     160  // until we reach the last chunk without an EOL char.  at that point, we shift the start
     161  // of the last (partial) line to the start of the buffer and re-fill.
     162
     163  Nstart = 0; // location of the last valid byte in the buffer (start filling here)
     164  Nelem = 0; // number of valid rows read (vector elements)
     165  while (TRUE) {
    134166    Nbytes = 0x10000 - Nstart;
    135167    bzero (&buffer[Nstart], Nbytes);
     
    139171      break;
    140172    }
    141     if (nread == 0) break;
    142     nbytes = nread + Nstart;
     173    if (nread == 0) break; // end of the file
     174    // nbytes = nread + Nstart;
    143175   
    144     status = TRUE;
    145     c0 = buffer;
    146     while (status) {
    147       c1 = strchr (c0, '\n');
     176    int bufferStatus = TRUE;
     177    c0 = buffer; // c0 always marks the start of a line
     178    while (bufferStatus) {
     179      c1 = strchr (c0, '\n'); // find the end of this current line
    148180      if (c1 == (char *) NULL) {
    149181        Nstart = strlen (c0);
    150182        memmove (buffer, c0, Nstart);
    151         status = FALSE;
    152       } else {
    153         *c1 = 0;
    154       }     
    155       if ((*c0 != '#') && (*c0 != '!')) {
    156         for (i = 0; (i < Nvec) && status; i++) {
     183        bufferStatus = FALSE;
     184        continue;
     185      }
     186      *c1 = 0; // mark the end of the line
     187
     188      if (*c0 == '#') { c0 = c1 + 1; continue; }
     189      if (*c0 == '!') { c0 = c1 + 1; continue; }
     190
     191      // parse the vectors in this line.  this code is a bit inefficient: each column
     192      // requires a separate pass through the line.
     193
     194      int lineStatus = TRUE;
     195      for (i = 0; i < Nvec; i++) {
     196        int ivalue;
     197        double dvalue;
     198        time_t tvalue;
     199        int readStatus;
     200        // need to make the if cases for coltype[i]
     201        switch (coltype[i]) {
     202          case COLTYPE_INT:
     203            readStatus = IsCSV ? iparse_csv (&ivalue, col[i], c0) : iparse (&ivalue, col[i], c0);
     204            vec[i][0].elements.Int[Nelem] = readStatus ? ivalue : 0;
     205            break;
     206          case COLTYPE_FLT:
     207            readStatus = IsCSV ? dparse_csv (&dvalue, col[i], c0) : dparse (&dvalue, col[i], c0);
     208            vec[i][0].elements.Flt[Nelem] = readStatus ? dvalue : NAN;
     209            break;
     210          case COLTYPE_TIME:
     211            readStatus = IsCSV ? tparse_csv (&tvalue, col[i], c0) : tparse (&tvalue, col[i], c0);
     212            dvalue = TimeValue (tvalue, TimeReference, TimeFormat);
     213            vec[i][0].elements.Flt[Nelem] = readStatus ? dvalue : NAN;
     214            break;
     215        }
     216        if (!readStatus && VERBOSE) {
    157217          if (IsCSV) {
    158             status = dparse_csv (&value, col[i], c0);
     218            gprint (GP_ERR, "suspect field: %d (%s) in %s\n", col[i], argv[2*i+2], c0);
    159219          } else {
    160             status = dparse (&value, col[i], c0);
     220            gprint (GP_ERR, "suspect field: %d in %s\n", col[i], c0);
    161221          }
    162           if (status) {
    163               vec[i][0].elements.Flt[N] = value;
    164           } else {
    165               vec[i][0].elements.Flt[N] = NAN;
    166               if (VERBOSE) {
    167                   if (IsCSV) {
    168                       gprint (GP_ERR, "suspect field: %d (%s) in %s\n", col[i], argv[2*i+2], c0);
    169                   } else {
    170                       gprint (GP_ERR, "suspect field: %d in %s\n", col[i], c0);
    171                   }
    172               }
    173           }
    174         }
    175         if (status) {
    176             N++;
    177         } else {
    178             if (VERBOSE && FALSE) {
    179                 char temp[32];
    180                 strncpy (temp, c0, 32);
    181                 temp[31] = 0;
    182                 gprint (GP_ERR, "skip line %s\n\n", temp);
    183             }
    184         }
    185       }
    186       c0 = c1 + 1;
    187       if (N == NELEM) {
     222        }
     223        lineStatus &= readStatus;
     224      }
     225      if (!lineStatus && VERBOSE) {
     226        char temp[32];
     227        strncpy (temp, c0, 32);
     228        temp[31] = 0;
     229        gprint (GP_ERR, "skip line %s\n\n", temp);
     230      }
     231      Nelem ++;
     232      if (Nelem == NELEM) {
    188233        NELEM += 1000;
    189234        for (i = 0; i < Nvec; i++) {
    190           REALLOCATE (vec[i][0].elements.Flt, opihi_flt, NELEM);
    191         }
    192       }
     235          if (coltype[i] == COLTYPE_INT) {
     236            REALLOCATE (vec[i][0].elements.Int, opihi_int, NELEM);
     237          } else {
     238            REALLOCATE (vec[i][0].elements.Flt, opihi_flt, NELEM);
     239          }
     240        }
     241      }
     242      c0 = c1 + 1;
    193243    }
    194244  }
    195245  for (i = 0; i < Nvec; i++) {
    196     REALLOCATE (vec[i][0].elements.Flt, opihi_flt, MAX (N,1));
    197     vec[i][0].Nelements = N;
     246    if (coltype[i] == COLTYPE_INT) {
     247      REALLOCATE (vec[i][0].elements.Int, opihi_int, MAX (Nelem,1));
     248    } else {
     249      REALLOCATE (vec[i][0].elements.Flt, opihi_flt, MAX (Nelem,1));
     250    }
     251    vec[i][0].Nelements = Nelem;
    198252  }
    199253 
     
    399453  return (TRUE);
    400454}
    401 
    402 # if (0)
    403 int read_vectors_csv (int argc, char **argv, int Nskip, FILE *f) {
    404  
    405   int i, j, Nvec, *col, done, status;
    406   int Nbytes, nbytes, Nstart, NELEM, N, nread;
    407   char *colstr, *c0, *c1, *buffer, *extname;
    408   double value;
    409   Vector **vec;
    410 
    411   Nvec = (argc - 1) / 2;
    412   ALLOCATE (vec, Vector *, Nvec);
    413   ALLOCATE (col, int, Nvec);
    414 
    415   for (i = 0; i < Nvec; i++) {
    416     if ((vec[i] = SelectVector (argv[2*i + 1], ANYVECTOR, TRUE)) == NULL) {
    417       gprint (GP_ERR, "USAGE: read name N name N ...\n");
    418       free (vec);
    419       free (col);
    420       return (FALSE);   
    421     }
    422     // XXX we could allow flags (eg, N.i) to specify INT vs FLT types vectors...
    423     colstr = argv[2*i+2];
    424     for (j = 0; j < strlen (colstr); j++) {
    425       if (!isdigit(colstr[j])) {
    426         gprint (GP_ERR, "USAGE: read name N name N ...\n");
    427         free (vec);
    428         free (col);
    429         return (FALSE);   
    430       }
    431     }
    432     col[i] = atof (colstr);
    433   }
    434 
    435   // currently, all read vectors are forced to be type FLT
    436   NELEM = 1000;
    437   for (i = 0; i < Nvec; i++) {
    438     ResetVector (vec[i], OPIHI_FLT, NELEM);
    439   }
    440  
    441   ALLOCATE (buffer, char, 0x10001);
    442   bzero (buffer, 0x10001);
    443   for (i = 0; i < Nskip; i++) {
    444     scan_line (f, buffer);
    445   }
    446 
    447   Nstart = 0;
    448   N = 0;
    449   done = FALSE;
    450   while (!done) {
    451     Nbytes = 0x10000 - Nstart;
    452     bzero (&buffer[Nstart], Nbytes);
    453     nread = fread (&buffer[Nstart], 1, Nbytes, f);
    454     if (ferror (f)) {
    455       perror ("error reading data file");
    456       break;
    457     }
    458     if (nread == 0) break;
    459     nbytes = nread + Nstart;
    460    
    461     status = TRUE;
    462     c0 = buffer;
    463     while (status) {
    464       c1 = strchr (c0, '\n');
    465       if (c1 == (char *) NULL) {
    466         Nstart = strlen (c0);
    467         memmove (buffer, c0, Nstart);
    468         status = FALSE;
    469       } else {
    470         *c1 = 0;
    471       }     
    472       if ((*c0 != '#') && (*c0 != '!')) {
    473         for (i = 0; (i < Nvec) && status; i++) {
    474           status = dparse_csv (&value, col[i], c0);
    475           vec[i][0].elements.Flt[N] = value;
    476           if (!status) vec[i][0].elements.Flt[N] = NAN;
    477         }
    478         if (status) N++;
    479       }
    480       c0 = c1 + 1;
    481       if (N == NELEM) {
    482         NELEM += 1000;
    483         for (i = 0; i < Nvec; i++) {
    484           REALLOCATE (vec[i][0].elements.Flt, opihi_flt, NELEM);
    485         }
    486       }
    487        
    488     }
    489   }
    490   for (i = 0; i < Nvec; i++) {
    491     REALLOCATE (vec[i][0].elements.Flt, opihi_flt, MAX (N,1));
    492     vec[i][0].Nelements = N;
    493   }
    494  
    495   free (vec);
    496   free (col);
    497   free (buffer);
    498   return (TRUE);
    499 
    500 }
    501 
    502 # endif
Note: See TracChangeset for help on using the changeset viewer.