IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 31598


Ignore:
Timestamp:
Jun 2, 2011, 12:43:07 PM (15 years ago)
Author:
eugene
Message:

enable read_vectors to read csv files

Location:
branches/eam_branches/ipp-20110505/Ohana/src
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20110505/Ohana/src/libohana/include/ohana.h

    r31593 r31598  
    235235int     scan_line              PROTO((FILE *f, char *line));
    236236int     dparse                 PROTO((double *X, int NX, char *line));
     237int     dparse_csv             PROTO((double *X, int NX, char *line));
    237238int     fparse                 PROTO((float *X, int NX, char *line));
    238239int     get_argument           PROTO((int argc, char **argv, char *arg));
  • branches/eam_branches/ipp-20110505/Ohana/src/libohana/src/string.c

    r27435 r31598  
    153153}
    154154
     155// advance to the next word, separated by commas:
     156// AA,BB,CC : go from AA to BB to CC to 0
     157// AA,,CC : go from AA to , to CC to 0
     158// ,,, : go from , to , to , to 0
     159
     160char *_parse_nextword_csv (char *string) {
     161
     162  if (string == (char *) NULL) return ((char *) NULL);
     163
     164  for (; (*string != 0) && (*string != ','); string++);
     165  if (*string == ',') string ++;
     166  return (string);
     167}
     168
    155169int dparse (double *X, int NX, char *line) {
    156170
     
    162176  for (i = 0; i < NX - 1; i++)
    163177    word = _parse_nextword (word);
     178
     179  *X = strtod (word, &ptr);
     180  if (ptr == word) return (FALSE);
     181  if (word[0] == '-') return (-1);
     182  return (1);
     183}
     184
     185int dparse_csv (double *X, int NX, char *line) {
     186
     187  int i;
     188  char *word;
     189  char *ptr;
     190
     191  word = line;
     192  for (i = 0; i < NX - 1; i++)
     193    word = _parse_nextword_csv (word);
     194 
     195  if (word[0] == ',') {
     196      *X = NAN;
     197      return 1;
     198  }
    164199
    165200  *X = strtod (word, &ptr);
  • branches/eam_branches/ipp-20110505/Ohana/src/opihi/cmd.data/read_vectors.c

    r29938 r31598  
    22
    33FILE *f = (FILE *) NULL;
    4 char filename[256];
     4char filename[1024];
    55
    66int datafile (int argc, char **argv) {
     
    2323int read_vectors (int argc, char **argv) {
    2424 
    25   int i, j, Nskip, Nvec, *col, done, status;
     25  int i, j, Nskip, Nvec, *col, done, status, IsCSV;
    2626  int Nbytes, nbytes, Nstart, NELEM, N, nread;
    2727  char *colstr, *c0, *c1, *buffer, *extname;
     
    4747  }
    4848
     49  IsCSV = FALSE;
     50  if ((N = get_argument (argc, argv, "-csv"))) {
     51    remove_argument (N, &argc, argv);
     52    IsCSV = TRUE;
     53  }
     54
    4955  if ((argc < 3) || !(argc % 2)) {
    5056    gprint (GP_ERR, "USAGE: read name N name N ...\n");
     
    5864  }
    5965  fseeko (f, 0LL, SEEK_SET);
     66
     67//  if (IsCSV) {
     68//    status = read_vectors_csv (argc, argv, Nskip, f);
     69//    return status;
     70//  }
    6071
    6172  Nvec = (argc - 1) / 2;
     
    122133      if ((*c0 != '#') && (*c0 != '!')) {
    123134        for (i = 0; (i < Nvec) && status; i++) {
    124           status = dparse (&value, col[i], c0);
     135          if (IsCSV) {
     136            status = dparse_csv (&value, col[i], c0);
     137          } else {
     138            status = dparse (&value, col[i], c0);
     139          }
    125140          vec[i][0].elements.Flt[N] = value;
    126141          if (!status) vec[i][0].elements.Flt[N] = NAN;
     
    341356  return (TRUE);
    342357}
     358
     359# if (0)
     360int read_vectors_csv (int argc, char **argv, int Nskip, FILE *f) {
     361 
     362  int i, j, Nvec, *col, done, status;
     363  int Nbytes, nbytes, Nstart, NELEM, N, nread;
     364  char *colstr, *c0, *c1, *buffer, *extname;
     365  double value;
     366  Vector **vec;
     367
     368  Nvec = (argc - 1) / 2;
     369  ALLOCATE (vec, Vector *, Nvec);
     370  ALLOCATE (col, int, Nvec);
     371
     372  for (i = 0; i < Nvec; i++) {
     373    if ((vec[i] = SelectVector (argv[2*i + 1], ANYVECTOR, TRUE)) == NULL) {
     374      gprint (GP_ERR, "USAGE: read name N name N ...\n");
     375      free (vec);
     376      free (col);
     377      return (FALSE);   
     378    }
     379    // XXX we could allow flags (eg, N.i) to specify INT vs FLT types vectors...
     380    colstr = argv[2*i+2];
     381    for (j = 0; j < strlen (colstr); j++) {
     382      if (!isdigit(colstr[j])) {
     383        gprint (GP_ERR, "USAGE: read name N name N ...\n");
     384        free (vec);
     385        free (col);
     386        return (FALSE);   
     387      }
     388    }
     389    col[i] = atof (colstr);
     390  }
     391
     392  // currently, all read vectors are forced to be type FLT
     393  NELEM = 1000;
     394  for (i = 0; i < Nvec; i++) {
     395    ResetVector (vec[i], OPIHI_FLT, NELEM);
     396  }
     397 
     398  ALLOCATE (buffer, char, 0x10001);
     399  bzero (buffer, 0x10001);
     400  for (i = 0; i < Nskip; i++) {
     401    scan_line (f, buffer);
     402  }
     403
     404  Nstart = 0;
     405  N = 0;
     406  done = FALSE;
     407  while (!done) {
     408    Nbytes = 0x10000 - Nstart;
     409    bzero (&buffer[Nstart], Nbytes);
     410    nread = fread (&buffer[Nstart], 1, Nbytes, f);
     411    if (ferror (f)) {
     412      perror ("error reading data file");
     413      break;
     414    }
     415    if (nread == 0) break;
     416    nbytes = nread + Nstart;
     417   
     418    status = TRUE;
     419    c0 = buffer;
     420    while (status) {
     421      c1 = strchr (c0, '\n');
     422      if (c1 == (char *) NULL) {
     423        Nstart = strlen (c0);
     424        memmove (buffer, c0, Nstart);
     425        status = FALSE;
     426      } else {
     427        *c1 = 0;
     428      }     
     429      if ((*c0 != '#') && (*c0 != '!')) {
     430        for (i = 0; (i < Nvec) && status; i++) {
     431          status = dparse_csv (&value, col[i], c0);
     432          vec[i][0].elements.Flt[N] = value;
     433          if (!status) vec[i][0].elements.Flt[N] = NAN;
     434        }
     435        if (status) N++;
     436      }
     437      c0 = c1 + 1;
     438      if (N == NELEM) {
     439        NELEM += 1000;
     440        for (i = 0; i < Nvec; i++) {
     441          REALLOCATE (vec[i][0].elements.Flt, opihi_flt, NELEM);
     442        }
     443      }
     444       
     445    }
     446  }
     447  for (i = 0; i < Nvec; i++) {
     448    REALLOCATE (vec[i][0].elements.Flt, opihi_flt, MAX (N,1));
     449    vec[i][0].Nelements = N;
     450  }
     451 
     452  free (vec);
     453  free (col);
     454  free (buffer);
     455  return (TRUE);
     456
     457}
     458
     459# endif
Note: See TracChangeset for help on using the changeset viewer.