IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 18, 2012, 8:44:13 PM (14 years ago)
Author:
eugene
Message:

some support functions for dvo_client; mextract example nearly complete

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20111122/Ohana/src/opihi/lib.shell/VectorOps.c

    r31160 r33308  
    288288}
    289289 
     290// write a set of vectors to a FITS file
     291int 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: LIIJEED
     327    // validate the format string
     328    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 strings
     341      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-widths
     351      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 in
     357  // output table (because the data goes to the named column below).  need to enforce
     358  // this somehow
     359  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 data
     365  gfits_create_table (&theader, &ftable);
     366
     367  // add the vectors to the output array
     368  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.