IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 33131


Ignore:
Timestamp:
Jan 22, 2012, 6:29:57 AM (14 years ago)
Author:
eugene
Message:

add option to write vectors to a FITS table, not a text file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20111122/Ohana/src/opihi/cmd.data/write_vectors.c

    r30610 r33131  
    77  FILE *f;
    88  char **fmtlist, *fmttype;
    9   char *p0, *p1, *p2, *format;
     9  char *p0, *p1, *p2, *format, *FITS;
    1010  Vector **vec;
    1111
     
    1818  }
    1919
     20  /* option generate a FITS output table */
     21  FITS = NULL;
     22  if ((N = get_argument (argc, argv, "-fits"))) {
     23    remove_argument (N, &argc, argv);
     24    FITS = strcreate (argv[N]);
     25    remove_argument (N, &argc, argv);
     26  }
     27
    2028  append = FALSE;
    2129  if ((N = get_argument (argc, argv, "-append"))) {
     
    3139  /* open file for outuput */
    3240  if (append) {
    33       f = fopen (argv[1], "a");
     41    f = fopen (argv[1], "a");
    3442  } else {
    35       f = fopen (argv[1], "w");
     43    f = fopen (argv[1], "w");
    3644  }
    3745  if (f == (FILE *) NULL) {
     
    4351  Nvec = (argc - 2);
    4452  if (Nvec < 1) {
    45       gprint (GP_ERR, "USAGE: write (file) vector vector ...\n");
    46       fclose (f);
    47   fflush (f);
    48       return (FALSE);
     53    gprint (GP_ERR, "USAGE: write (file) vector vector ...\n");
     54    fclose (f);
     55    fflush (f);
     56    return (FALSE);
    4957  }
    5058  ALLOCATE (vec, Vector *, Nvec);
     
    5765      free (vec);
    5866      fclose (f);
    59   fflush (f);
     67      fflush (f);
    6068      return (FALSE);   
    6169    }
     
    6977      free (vec);
    7078      fclose (f);
    71   fflush (f);
     79      fflush (f);
    7280      return (FALSE);   
    7381    }
     82  }
     83
     84  if (FITS) {
     85    char *tformat = NULL;
     86    Header header;
     87    Matrix matrix;
     88    Header theader;
     89    FTable ftable;
     90
     91    if (!append) {
     92      gfits_init_header (&header);
     93      header.extend = TRUE;
     94      gfits_create_header (&header);
     95      gfits_create_matrix (&header, &matrix);
     96    }
     97
     98    gfits_create_table_header (&theader, "BINTABLE", FITS);
     99
     100    ALLOCATE (tformat, char, 2*Nvec);
     101    if (format) {
     102      // the bintable format string can only define the byte-width of each field.  valid output columns are currently:
     103      // B (char), I (16 bit short), J (32 bit int), E (32 bit float), D (64 bit double).
     104      // the format string is just the sequence of types, eg: LIIJEED
     105      // validate the format string
     106      char *ptr = format;
     107      for (j = 0; j < Nvec; j++) {
     108        while (*ptr && OHANA_WHITESPACE (*ptr)) ptr++;
     109        if (*ptr == 0) {
     110          gprint (GP_ERR, "error in binary table format %s (insufficient format chars)\n", format);
     111          goto escape;
     112        }
     113        if ((*ptr != 'B') && (*ptr != 'I') && (*ptr != 'J') && (*ptr != 'D') && (*ptr != 'E')) {
     114          gprint (GP_ERR, "error in binary table format %s: invalid character %c\n", format, *ptr);
     115          goto escape;
     116        }
     117        tformat[2*j + 0] = *ptr;
     118        tformat[2*j + 1] = 0; // a bit sleazy : use a 2xN string to store N 1-byte strings
     119        ptr ++;
     120      }
     121      while (*ptr && OHANA_WHITESPACE (*ptr)) ptr++;
     122      if (*ptr) {
     123        gprint (GP_ERR, "error in binary table format %s (extra characters in format)\n", format);
     124        goto escape;
     125      }
     126    } else {
     127      for (j = 0; j < Nvec; j++) {
     128        // if the format is not defined, just use the native byte-widths
     129        tformat[2*j + 0] = (vec[j][0].type == OPIHI_FLT) ? 'D' : 'J';
     130        tformat[2*j + 1] = 0;
     131      }
     132    }
     133
     134    // define the columns of the table.  XXX NOTE: we cannot have duplicate names in
     135    // output table (because the data goes to the named column below).  need to enforce
     136    // this somehow
     137    for (j = 0; j < Nvec; j++) {
     138      gfits_define_bintable_column (&theader, &tformat[2*j], vec[j][0].name, NULL, NULL, 1.0, 0.0);
     139    }
     140    free (tformat);
     141
     142    // generate the output array that carries the data
     143    gfits_create_table (&theader, &ftable);
     144
     145    // add the vectors to the output array
     146    for (j = 0; j < Nvec; j++) {
     147      if (vec[j][0].type == OPIHI_FLT) {
     148        gfits_set_bintable_column_reformat (&theader, &ftable, vec[j][0].name, "double", vec[j][0].elements.Flt, vec[j][0].Nelements);
     149      } else {
     150        gfits_set_bintable_column_reformat (&theader, &ftable, vec[j][0].name, "int", vec[j][0].elements.Int, vec[j][0].Nelements);
     151      }
     152    }
     153
     154    if (!append) {
     155      gfits_fwrite_header  (f, &header);
     156      gfits_fwrite_matrix  (f, &matrix);
     157      gfits_free_header (&header);
     158      gfits_free_matrix (&matrix);
     159    }
     160    gfits_fwrite_Theader (f, &theader);
     161    gfits_fwrite_table  (f, &ftable);
     162
     163    gfits_free_header (&theader);
     164    gfits_free_table (&ftable);
     165
     166    fclose (f);
     167    free (vec);
     168    fflush (f);
     169    return (TRUE);
     170
     171  escape:
     172    if (!append) {
     173      gfits_free_header (&header);
     174      gfits_free_matrix (&matrix);
     175    }
     176    gfits_free_header (&theader);
     177    gfits_free_table (&ftable);
     178
     179    if (tformat) free (tformat);
     180    fclose (f);
     181    free (vec);
     182    fflush (f);
     183    return (FALSE);
    74184  }
    75185
     
    88198    fclose (f);
    89199    free (vec);
    90   fflush (f);
     200    fflush (f);
    91201    return (TRUE);
    92202  }
     
    111221      free (format);
    112222      fclose (f);
    113   fflush (f);
     223      fflush (f);
    114224      return (FALSE);
    115225    }
Note: See TracChangeset for help on using the changeset viewer.