Changeset 33115
- Timestamp:
- Jan 18, 2012, 8:31:56 AM (15 years ago)
- Location:
- branches/eam_branches/ipp-20111122/Ohana/src/opihi/cmd.data
- Files:
-
- 5 added
- 1 edited
-
read_vectors.c (modified) (9 diffs)
-
test/read.sh (added)
-
test/read.t1.dat (added)
-
test/read.t2.dat (added)
-
test/read.t3.dat (added)
-
test/read.t4.dat (added)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20111122/Ohana/src/opihi/cmd.data/read_vectors.c
r32846 r33115 21 21 } 22 22 23 // vector types 24 enum {COLTYPE_NONE, COLTYPE_FLT, COLTYPE_INT, COLTYPE_TIME}; 25 23 26 int read_vectors (int argc, char **argv) { 24 27 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; 27 32 char *colstr, *c0, *c1, *buffer, *extname; 28 double value;29 33 Vector **vec; 30 34 31 35 /* 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]); 35 39 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); 38 42 free (extname); 39 43 return (status); … … 41 45 42 46 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); 47 51 } 48 52 49 53 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); 52 56 IsCSV = TRUE; 53 57 } 54 58 55 59 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); 58 62 VERBOSE = TRUE; 59 63 } … … 65 69 /* read name N name N */ 66 70 71 // do this only optionally? 72 GetTimeFormat (&TimeReference, &TimeFormat); 73 67 74 if (f == (FILE *) NULL) { 68 75 gprint (GP_ERR, "no open file for read\n"); … … 71 78 fseeko (f, 0LL, SEEK_SET); 72 79 73 // if (IsCSV) {74 // status = read_vectors_csv (argc, argv, Nskip, f);75 // return status;76 // }77 78 80 Nvec = (argc - 1) / 2; 79 81 ALLOCATE (vec, Vector *, Nvec); 80 82 ALLOCATE (col, int, Nvec); 83 ALLOCATE (coltype, int, Nvec); 81 84 82 85 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 83 107 if ((vec[i] = SelectVector (argv[2*i + 1], ANYVECTOR, TRUE)) == NULL) { 84 108 gprint (GP_ERR, "USAGE: read name N name N ...\n"); … … 87 111 return (FALSE); 88 112 } 89 // XXX we could allow flags (eg, N.i) to specify INT vs FLT types vectors... 113 90 114 colstr = argv[2*i+2]; 91 115 for (j = 0; j < strlen (colstr); j++) { … … 119 143 NELEM = 1000; 120 144 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 } 122 151 } 123 152 … … 128 157 } 129 158 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) { 134 166 Nbytes = 0x10000 - Nstart; 135 167 bzero (&buffer[Nstart], Nbytes); … … 139 171 break; 140 172 } 141 if (nread == 0) break; 142 nbytes = nread + Nstart;173 if (nread == 0) break; // end of the file 174 // nbytes = nread + Nstart; 143 175 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 148 180 if (c1 == (char *) NULL) { 149 181 Nstart = strlen (c0); 150 182 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) { 157 217 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); 159 219 } else { 160 status = dparse (&value, col[i], c0);220 gprint (GP_ERR, "suspect field: %d in %s\n", col[i], c0); 161 221 } 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) { 188 233 NELEM += 1000; 189 234 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; 193 243 } 194 244 } 195 245 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; 198 252 } 199 253 … … 399 453 return (TRUE); 400 454 } 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 FLT436 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.
