Changeset 37049 for trunk/Ohana/src/opihi/cmd.data/read_vectors.c
- Timestamp:
- Jul 17, 2014, 10:21:10 AM (12 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/opihi/cmd.data/read_vectors.c (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/cmd.data/read_vectors.c
r36375 r37049 3 3 FILE *f = (FILE *) NULL; 4 4 char filename[2048]; 5 6 void read_vectors_cleanup (); 5 7 6 8 int datafile (int argc, char **argv) { … … 23 25 // vector types 24 26 enum {COLTYPE_NONE, COLTYPE_FLT, COLTYPE_INT, COLTYPE_TIME, COLTYPE_CHAR}; 25 static int FITS_TRANSPOSE; 27 28 static int Nvec = 0; 29 static Vector **vec = NULL; 30 static char **listname = NULL; 31 static int *col = NULL; 32 static int *coltype = NULL; 33 static char *buffer = NULL; 26 34 27 35 int read_vectors (int argc, char **argv) { … … 29 37 int TimeFormat; 30 38 time_t TimeReference; 31 int i, j, Nskip, Narg, Nvec, *col,IsCSV, VERBOSE;32 int Nbytes, Nstart, NELEM, Nelem, nread , *coltype;39 int i, j, Nskip, Narg, IsCSV, VERBOSE; 40 int Nbytes, Nstart, NELEM, Nelem, nread; 33 41 char *colstr, *c0, *c1, *extname; 34 Vector **vec; 35 36 char *buffer = NULL; 37 38 FITS_TRANSPOSE = FALSE; 39 if ((Narg = get_argument (argc, argv, "-transpose"))) { 40 remove_argument (Narg, &argc, argv); 41 FITS_TRANSPOSE = TRUE; 42 } 42 char varname[1024]; // used as a buffer for the names of string fields 43 43 44 44 /* auto-sense table type */ … … 88 88 89 89 Nvec = (argc - 1) / 2; 90 ALLOCATE (listname, char *, Nvec); 90 91 ALLOCATE (vec, Vector *, Nvec); 91 92 ALLOCATE (col, int, Nvec); 92 93 ALLOCATE (coltype, int, Nvec); 94 for (i = 0; i < Nvec; i++) { 95 listname[i] = NULL; 96 vec[i] = NULL; 97 } 93 98 94 99 for (i = 0; i < Nvec; i++) { … … 115 120 } 116 121 117 if ((vec[i] = SelectVector (argv[2*i + 1], ANYVECTOR, TRUE)) == NULL) { 118 gprint (GP_ERR, "USAGE: read name N name N ...\n"); 119 free (vec); 120 free (col); 121 return (FALSE); 122 if (coltype[i] == COLTYPE_CHAR) { 123 listname[i] = strcreate (argv[2*i + 1]); 124 } else { 125 if ((vec[i] = SelectVector (argv[2*i + 1], ANYVECTOR, TRUE)) == NULL) { 126 gprint (GP_ERR, "USAGE: read name N name N ...\n"); 127 read_vectors_cleanup(); 128 return (FALSE); 129 } 122 130 } 123 131 … … 144 152 bad_colname: 145 153 gprint (GP_ERR, "USAGE: read name N name N ...\n"); 146 free (vec); 147 free (col); 154 read_vectors_cleanup(); 148 155 return (FALSE); 149 156 } … … 153 160 NELEM = 1000; 154 161 for (i = 0; i < Nvec; i++) { 155 if ((coltype[i] == COLTYPE_INT) || (coltype[i] == COLTYPE_CHAR)) { 156 ResetVector (vec[i], OPIHI_INT, NELEM); 157 } else { 158 // note that COLTYPE_TIME is a type of float 159 ResetVector (vec[i], OPIHI_FLT, NELEM); 162 switch (coltype[i]) { 163 case COLTYPE_INT: 164 ResetVector (vec[i], OPIHI_INT, NELEM); 165 break; 166 case COLTYPE_FLT: 167 case COLTYPE_TIME: 168 // note that COLTYPE_TIME is a type of float 169 ResetVector (vec[i], OPIHI_FLT, NELEM); 170 break; 171 case COLTYPE_CHAR: 172 default: 173 break; 160 174 } 161 175 } … … 166 180 if (scan_line_maxlen (f, buffer, 0x10000) == EOF) { 167 181 gprint (GP_ERR, "problem reading file %s\n", filename); 168 free (vec); 169 free (col); 182 read_vectors_cleanup(); 170 183 return FALSE; 171 184 } … … 213 226 for (i = 0; i < Nvec; i++) { 214 227 int ivalue; 215 char cvalue;216 228 double dvalue; 217 229 time_t tvalue; … … 224 236 break; 225 237 case COLTYPE_CHAR: 226 readStatus = IsCSV ? charparse_csv (&cvalue, col[i], c0) : charparse (&cvalue, col[i], c0); 227 vec[i][0].elements.Int[Nelem] = readStatus ? cvalue : 0; 228 break; 238 { 239 // I need to get an isolated word in 'value' with the string value of this field 240 char *ptr = IsCSV ? ptrparse_csv (col[i], c0) : ptrparse (col[i], c0); 241 char *value = NULL; 242 if (IsCSV) { 243 char *end = parse_nextword_csv (ptr); 244 if (end) { 245 value = end ? strncreate (ptr, end - ptr) : strcreate (ptr); 246 } 247 } else { 248 value = thisword(ptr); 249 } 250 set_list_varname (varname, listname[i], Nelem, FALSE); 251 set_str_variable (varname, value); 252 free (value); 253 break; 254 } 229 255 case COLTYPE_FLT: 230 256 readStatus = IsCSV ? dparse_csv (&dvalue, col[i], c0) : dparse (&dvalue, col[i], c0); … … 256 282 NELEM += 1000; 257 283 for (i = 0; i < Nvec; i++) { 258 if (coltype[i] == COLTYPE_INT) { 259 REALLOCATE (vec[i][0].elements.Int, opihi_int, NELEM); 260 } else { 261 REALLOCATE (vec[i][0].elements.Flt, opihi_flt, NELEM); 284 switch (coltype[i]) { 285 case COLTYPE_INT: 286 ResetVector (vec[i], OPIHI_INT, NELEM); 287 break; 288 case COLTYPE_FLT: 289 case COLTYPE_TIME: 290 ResetVector (vec[i], OPIHI_FLT, NELEM); 291 break; 292 case COLTYPE_CHAR: 293 default: 294 break; 262 295 } 263 296 } … … 266 299 } 267 300 } 301 // set the final vector / list length 268 302 for (i = 0; i < Nvec; i++) { 269 if (coltype[i] == COLTYPE_INT) { 270 REALLOCATE (vec[i][0].elements.Int, opihi_int, MAX (Nelem,1)); 271 } else { 272 REALLOCATE (vec[i][0].elements.Flt, opihi_flt, MAX (Nelem,1)); 273 } 274 vec[i][0].Nelements = Nelem; 275 } 276 277 free (vec); 278 free (col); 279 if (buffer) free (buffer); 303 switch (coltype[i]) { 304 case COLTYPE_INT: 305 ResetVector (vec[i], OPIHI_INT, Nelem); 306 break; 307 case COLTYPE_FLT: 308 case COLTYPE_TIME: 309 ResetVector (vec[i], OPIHI_FLT, Nelem); 310 break; 311 case COLTYPE_CHAR: 312 sprintf (varname, "%s:n", listname[i]); 313 set_int_variable (varname, Nelem); 314 break; 315 default: 316 break; 317 } 318 } 319 read_vectors_cleanup(); 280 320 return (TRUE); 281 282 321 } 283 322 284 # define ESCAPE( MSG) {\285 gprint (GP_ERR, "%s\n", MSG); \323 # define ESCAPE(...) { \ 324 gprint (GP_ERR, __VA_ARGS__); \ 286 325 if (CCDKeyword != NULL) free (CCDKeyword); \ 287 326 gfits_free_table (&table); \ … … 292 331 293 332 off_t Nbytes; 294 int i, j, k,N, Nextend, Ny, Binary, vecType, padIfShort;333 int i, j, N, Nextend, Ny, Binary, vecType, padIfShort; 295 334 char type[16], ID[80], *CCDKeyword; 296 335 FTable table; 297 336 Header header; 298 337 Vector **vec; 338 int FITS_TRANSPOSE; 299 339 300 340 table.buffer = NULL; 301 341 header.buffer = NULL; 342 343 FITS_TRANSPOSE = FALSE; 344 if ((N = get_argument (argc, argv, "-transpose"))) { 345 remove_argument (N, &argc, argv); 346 FITS_TRANSPOSE = TRUE; 347 } 302 348 303 349 CCDKeyword = NULL; … … 328 374 // } 329 375 330 if (argc < 2) ESCAPE ("USAGE: read -fits extension [-extnum] [-keyword key] name name ... ");331 332 if (f == NULL) ESCAPE ("file not found ");376 if (argc < 2) ESCAPE ("USAGE: read -fits extension [-extnum] [-keyword key] name name ...\n"); 377 378 if (f == NULL) ESCAPE ("file not found\n"); 333 379 fseeko (f, 0LL, SEEK_SET); 334 380 table.header = &header; … … 338 384 // first extension is PHU, cannot be a table. 339 385 // Nextend counts from 0 for first extension 340 if (!gfits_load_header (f, &header)) ESCAPE ("error reading primary header for file ");386 if (!gfits_load_header (f, &header)) ESCAPE ("error reading primary header for file\n"); 341 387 Nbytes = gfits_data_size (&header); 342 388 fseeko (f, Nbytes, SEEK_CUR); … … 344 390 345 391 for (i = 0; i < Nextend; i++) { 346 if (!gfits_load_header (f, &header)) ESCAPE ("extension not found");392 if (!gfits_load_header (f, &header)) ESCAPE ("extension %d not found\n", i); 347 393 Nbytes = gfits_data_size (&header); 348 394 /* skip the prior data buffers */ … … 350 396 gfits_free_header (&header); 351 397 } 352 if (!gfits_load_header (f, &header)) ESCAPE ("error reading header for extension ");353 if (!gfits_fread_ftable_data (f, &table, padIfShort)) ESCAPE ("error reading table for extension ");398 if (!gfits_load_header (f, &header)) ESCAPE ("error reading header for extension %d\n", Nextend); 399 if (!gfits_fread_ftable_data (f, &table, padIfShort)) ESCAPE ("error reading table for extension %d\n", Nextend); 354 400 355 401 } else { … … 382 428 continue; 383 429 } 384 if (!gfits_fread_ftable_data (f, &table, padIfShort)) ESCAPE ("error reading table for extension ");430 if (!gfits_fread_ftable_data (f, &table, padIfShort)) ESCAPE ("error reading table for extension\n"); 385 431 break; 386 432 } … … 391 437 gfits_scan (&header, "XTENSION", "%s", 1, type); 392 438 if (strcmp (type, "BINTABLE") && strcmp (type, "TABLE")) { 393 ESCAPE ("specified extension is not a table\n");439 ESCAPE ("specified extension %s is not a table\n", type); 394 440 } 395 441 Binary = !strcmp (type, "BINTABLE"); … … 418 464 419 465 if (!FITS_TRANSPOSE) { 466 // read string column into a list rather than a vector 467 if (!strcmp (type, "char")) { 468 char *fieldName = argv[i]; 469 char *Ptr = data; 470 char varname[1024]; // used as a buffer for the names of string fields 471 for (j = 0; j < Ny; j++) { 472 set_list_varname (varname, fieldName, j, FALSE); 473 char *value = strncreate (&Ptr[j*Nval], Nval); 474 // replace instances of $ with _ 475 char *p = strchr (value, '$'); 476 while (p) { 477 *p = '_'; 478 p = strchr (p, '$'); 479 } 480 set_str_variable (varname, value); 481 free (value); 482 } 483 sprintf (varname, "%s:n", fieldName); 484 set_int_variable (varname, Ny); 485 continue; 486 } 487 420 488 // define the multifield vector names (Nval vectors x Ny elements) 421 489 ALLOCATE (vec, Vector *, Nval); … … 429 497 } 430 498 431 if (!strcmp (type, "char")) { 432 char *Ptr = data; 433 for (j = 0; j < Ny; j++) { 434 for (k = 0; k < Nval; k++, Ptr++) { 435 vec[k][0].elements.Int[j] = *Ptr; 436 } 437 } 438 } 439 if (!strcmp (type, "short")) { 440 short *Ptr = data; 441 for (j = 0; j < Ny; j++) { 442 for (k = 0; k < Nval; k++, Ptr++) { 443 vec[k][0].elements.Int[j] = *Ptr; 444 } 445 } 446 } 447 if (!strcmp (type, "int")) { 448 int *Ptr = data; 449 for (j = 0; j < Ny; j++) { 450 for (k = 0; k < Nval; k++, Ptr++) { 451 vec[k][0].elements.Int[j] = *Ptr; 452 } 453 } 454 } 455 if (!strcmp (type, "int64_t")) { 456 int64_t *Ptr = data; 457 for (j = 0; j < Ny; j++) { 458 for (k = 0; k < Nval; k++, Ptr++) { 459 vec[k][0].elements.Int[j] = *Ptr; 460 } 461 } 462 } 463 if (!strcmp (type, "float")) { 464 float *Ptr = data; 465 for (j = 0; j < Ny; j++) { 466 for (k = 0; k < Nval; k++, Ptr++) { 467 vec[k][0].elements.Flt[j] = *Ptr; 468 } 469 } 470 } 471 if (!strcmp (type, "double")) { 472 double *Ptr = data; 473 for (j = 0; j < Ny; j++) { 474 for (k = 0; k < Nval; k++, Ptr++) { 475 vec[k][0].elements.Flt[j] = *Ptr; 476 } 477 } 478 } 499 if (!VectorAssignData (vec, type, data, Ny, Nval)) ESCAPE ("bad column type %s", type); 500 479 501 } else { 480 502 // define the multifield vector names (Ny vectors x Nval elements) … … 489 511 } 490 512 491 if (!strcmp (type, "char")) { 492 char *Ptr = data; 493 for (j = 0; j < Ny; j++) { 494 for (k = 0; k < Nval; k++, Ptr++) { 495 vec[j][0].elements.Int[k] = *Ptr; 496 } 497 } 498 } 499 if (!strcmp (type, "short")) { 500 short *Ptr = data; 501 for (j = 0; j < Ny; j++) { 502 for (k = 0; k < Nval; k++, Ptr++) { 503 vec[j][0].elements.Int[k] = *Ptr; 504 } 505 } 506 } 507 if (!strcmp (type, "int")) { 508 int *Ptr = data; 509 for (j = 0; j < Ny; j++) { 510 for (k = 0; k < Nval; k++, Ptr++) { 511 vec[j][0].elements.Int[k] = *Ptr; 512 } 513 } 514 } 515 if (!strcmp (type, "int64_t")) { 516 int64_t *Ptr = data; 517 for (j = 0; j < Ny; j++) { 518 for (k = 0; k < Nval; k++, Ptr++) { 519 vec[j][0].elements.Int[k] = *Ptr; 520 } 521 } 522 } 523 if (!strcmp (type, "float")) { 524 float *Ptr = data; 525 for (j = 0; j < Ny; j++) { 526 for (k = 0; k < Nval; k++, Ptr++) { 527 vec[j][0].elements.Flt[k] = *Ptr; 528 } 529 } 530 } 531 if (!strcmp (type, "double")) { 532 double *Ptr = data; 533 for (j = 0; j < Ny; j++) { 534 for (k = 0; k < Nval; k++, Ptr++) { 535 vec[j][0].elements.Flt[k] = *Ptr; 536 } 537 } 538 } 513 if (!VectorAssignDataTranspose (vec, type, data, Ny, Nval)) ESCAPE ("bad column type %s", type); 539 514 } 540 515 free (data); … … 546 521 return (TRUE); 547 522 } 523 524 void read_vectors_cleanup () { 525 526 int i; 527 528 if (col) free (col); 529 if (coltype) free (coltype); 530 if (buffer) free (buffer); 531 if (listname) { 532 for (i = 0; i < Nvec; i++) { 533 if (listname[i]) free (listname[i]); 534 } 535 free (listname); 536 } 537 if (vec) free (vec); 538 }
Note:
See TracChangeset
for help on using the changeset viewer.
