- Timestamp:
- Feb 20, 2012, 6:55:53 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20111122/Ohana/src/opihi/lib.shell/VectorOps.c
r33308 r33309 15 15 16 16 // this function is NOT thread protected : it is only used in startup and/or shutdown 17 void FreeVectorArray (Vector **vec, int Nvec) { 18 19 int i; 20 21 if (!vec) return; 22 for (i = 0; i < Nvec; i++) { 23 if (!vec[i]) continue; 24 if (vec[i]->elements.Int) { 25 free (vec[i]->elements.Int); 26 } 27 free (vec[i]); 28 } 29 free (vec); 30 } 31 32 // this function is NOT thread protected : it is only used in startup and/or shutdown 17 33 void FreeVectors () { 18 19 int i; 20 21 for (i = 0; i < Nvectors; i++) { 22 if (vectors[i][0].elements.Int) { 23 free (vectors[i][0].elements.Int); 24 } 25 free (vectors[i]); 26 } 27 free (vectors); 34 FreeVectorArray (vectors, Nvectors); 28 35 } 29 36 … … 93 100 } 94 101 102 // Assign the given Vector to the internal array of vectors by name 103 int AssignVector (Vector *vec, char *name, int mode, int verbose) { 104 105 int i; 106 107 if (name == NULL) goto error; 108 if (ISNUM(name[0])) goto error; 109 if (IsBuffer(name)) goto error; 110 111 for (i = 0; (i < Nvectors) && (strcmp(vectors[i][0].name, name)); i++); 112 /* is a new vector */ 113 if (i == Nvectors) { 114 if (mode == OLDVECTOR) goto error; 115 pthread_mutex_lock (&mutex); 116 Nvectors ++; 117 REALLOCATE (vectors, Vector *, Nvectors); 118 vectors[i] = vec; 119 pthread_mutex_unlock (&mutex); 120 return TRUE; 121 } 122 /* is an old vector */ 123 if (mode == NEWVECTOR) goto error; 124 vectors[i] = vec; 125 return TRUE; 126 127 error: 128 if (verbose) gprint (GP_ERR, "invalid vector %s\n", name); 129 return FALSE; 130 } 131 95 132 /* delete by pointer */ 96 133 int DeleteVector (Vector *vec) { … … 287 324 return (TRUE); 288 325 } 289 290 // write a set of vectors to a FITS file291 int WriteVectorTableFITS (char *filename, char *extname, Vector **vec, int Nvec, int append, char *format) {292 293 char *tformat = NULL;294 Header header;295 Matrix matrix;296 Header theader;297 FTable ftable;298 299 int j;300 FILE *f = NULL;301 302 /* open file for outuput */303 if (append) {304 f = fopen (filename, "a");305 } else {306 f = fopen (filename, "w");307 }308 if (f == (FILE *) NULL) {309 gprint (GP_ERR, "can't open file for write\n");310 return (FALSE);311 }312 313 if (!append) {314 gfits_init_header (&header);315 header.extend = TRUE;316 gfits_create_header (&header);317 gfits_create_matrix (&header, &matrix);318 }319 320 gfits_create_table_header (&theader, "BINTABLE", extname);321 322 ALLOCATE (tformat, char, 2*Nvec);323 if (format) {324 // the bintable format string can only define the byte-width of each field. valid output columns are currently:325 // B (char), I (16 bit short), J (32 bit int), E (32 bit float), D (64 bit double).326 // the format string is just the sequence of types, eg: LIIJEED327 // validate the format string328 char *ptr = format;329 for (j = 0; j < Nvec; j++) {330 while (*ptr && OHANA_WHITESPACE (*ptr)) ptr++;331 if (*ptr == 0) {332 gprint (GP_ERR, "error in binary table format %s (insufficient format chars)\n", format);333 goto escape;334 }335 if ((*ptr != 'B') && (*ptr != 'I') && (*ptr != 'J') && (*ptr != 'D') && (*ptr != 'E')) {336 gprint (GP_ERR, "error in binary table format %s: invalid character %c\n", format, *ptr);337 goto escape;338 }339 tformat[2*j + 0] = *ptr;340 tformat[2*j + 1] = 0; // a bit sleazy : use a 2xN string to store N 1-byte strings341 ptr ++;342 }343 while (*ptr && OHANA_WHITESPACE (*ptr)) ptr++;344 if (*ptr) {345 gprint (GP_ERR, "error in binary table format %s (extra characters in format)\n", format);346 goto escape;347 }348 } else {349 for (j = 0; j < Nvec; j++) {350 // if the format is not defined, just use the native byte-widths351 tformat[2*j + 0] = (vec[j][0].type == OPIHI_FLT) ? 'D' : 'J';352 tformat[2*j + 1] = 0;353 }354 }355 356 // define the columns of the table. XXX NOTE: we cannot have duplicate names in357 // output table (because the data goes to the named column below). need to enforce358 // this somehow359 for (j = 0; j < Nvec; j++) {360 gfits_define_bintable_column (&theader, &tformat[2*j], vec[j][0].name, NULL, NULL, 1.0, 0.0);361 }362 free (tformat);363 364 // generate the output array that carries the data365 gfits_create_table (&theader, &ftable);366 367 // add the vectors to the output array368 for (j = 0; j < Nvec; j++) {369 if (vec[j][0].type == OPIHI_FLT) {370 gfits_set_bintable_column_reformat (&theader, &ftable, vec[j][0].name, "double", vec[j][0].elements.Flt, vec[j][0].Nelements);371 } else {372 gfits_set_bintable_column_reformat (&theader, &ftable, vec[j][0].name, "int", vec[j][0].elements.Int, vec[j][0].Nelements);373 }374 }375 376 if (!append) {377 gfits_fwrite_header (f, &header);378 gfits_fwrite_matrix (f, &matrix);379 gfits_free_header (&header);380 gfits_free_matrix (&matrix);381 }382 gfits_fwrite_Theader (f, &theader);383 gfits_fwrite_table (f, &ftable);384 385 gfits_free_header (&theader);386 gfits_free_table (&ftable);387 388 fclose (f);389 fflush (f);390 return (TRUE);391 392 escape:393 if (!append) {394 gfits_free_header (&header);395 gfits_free_matrix (&matrix);396 }397 gfits_free_header (&theader);398 gfits_free_table (&ftable);399 400 if (tformat) free (tformat);401 fclose (f);402 fflush (f);403 return (FALSE);404 }
Note:
See TracChangeset
for help on using the changeset viewer.
