IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 39460


Ignore:
Timestamp:
Mar 14, 2016, 1:34:58 PM (10 years ago)
Author:
eugene
Message:

add a function to do a byteswap by column on a table (needed for compressed I/O tests)

Location:
trunk/Ohana/src/libfits
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libfits/include/gfitsio.h

    r39457 r39460  
    247247int     gfits_fwrite_vtable            PROTO((FILE *f, VTable *table));
    248248int     gfits_fwrite_ftable_range      PROTO((FILE *f, FTable *table, off_t start, off_t Nrows, off_t Ndisk, off_t Ntotal));
     249int     gfits_byteswap_bintable_column PROTO((FTable *ftable, int column));
    249250int     gfits_get_bintable_column      PROTO((Header *header, FTable *table, char *label, void **data));
    250251int     gfits_get_bintable_column_raw  PROTO((Header *header, FTable *table, char *label, void **data, char nativeOrder));
  • trunk/Ohana/src/libfits/table/F_get_column.c

    r38553 r39460  
    139139  *Nrow = Ny;
    140140  return (array);
     141}
     142
     143// do a column byteswap in-situ (needed because uncompress returns an unswapped binary table
     144int gfits_byteswap_bintable_column (FTable *ftable, int column) {
     145
     146  off_t Nx, Ny;
     147  int i, Nval, Nbytes, Nv, Nb;
     148  char field[256], format[256], tmpline[64], type[64];
     149  char *Pin, *Pout, *array;
     150
     151  Header *header = ftable->header;
     152
     153  /* interpret format */
     154  snprintf (field, 256, "TFORM%d", column);
     155  if (!gfits_scan (header, field, "%s", 1, format)) return FALSE;
     156
     157  if (!gfits_bintable_format (format, type, &Nval, &Nbytes)) return FALSE;
     158 
     159  /* convert data in-situ with correct type, byte swap and Bzero/Bscale */
     160  if (!strcmp (type, "char")) return TRUE;
     161  if (!strcmp (type, "byte")) return TRUE;
     162
     163  /* check existing table dimensions */
     164  gfits_scan (header, "NAXIS1",  OFF_T_FMT, 1,  &Nx);
     165  gfits_scan (header, "NAXIS2",  OFF_T_FMT, 1,  &Ny);
     166
     167  /* scan prior columns to find insert point */
     168  int Nstart = 0;
     169  for (i = 1; i < column; i++) {
     170    snprintf (field, 256, "TFORM%d", i);
     171    gfits_scan (header, field, "%s", 1, format);
     172    gfits_bintable_format (format, tmpline, &Nv, &Nb);
     173    Nstart += Nv*Nb;
     174  }
     175
     176  /* extract bytes from table into array */
     177  ALLOCATE (array, char, Nbytes*Nval*Ny);
     178  Pin  = ftable[0].buffer + Nstart;
     179  Pout = array;
     180  for (i = 0; i < Ny; i++, Pin += Nx, Pout += Nval*Nbytes) {
     181    memcpy (Pout, Pin, Nval*Nbytes);
     182  }
     183
     184  // NOTE: we have already copied the data to 'array', so the blocks below
     185  // only need to swap if this is a direct copy
     186  Pin  = array;
     187  Pout = array;
     188
     189  if (!strcmp (type, "short")) {
     190    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
     191# ifdef BYTE_SWAP
     192      SWAP_BYTE;
     193# endif
     194    } 
     195  }
     196  if (!strcmp (type, "int")) {
     197    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
     198# ifdef BYTE_SWAP
     199      SWAP_WORD;
     200# endif
     201    }
     202  }
     203  if (!strcmp (type, "int64_t")) {
     204    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
     205# ifdef BYTE_SWAP
     206      SWAP_DBLE;
     207# endif
     208    }
     209  }
     210  if (!strcmp (type, "float")) {
     211    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
     212# ifdef BYTE_SWAP
     213      SWAP_WORD;
     214# endif
     215    }
     216  }
     217  if (!strcmp (type, "double")) {
     218    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
     219# ifdef BYTE_SWAP
     220      SWAP_DBLE;
     221# endif
     222    }
     223  }
     224
     225  // save the bytes back into the table buffer
     226  Pout = ftable[0].buffer + Nstart;
     227  Pin  = array;
     228  for (i = 0; i < Ny; i++, Pout += Nx, Pin += Nval*Nbytes) {
     229    memcpy (Pout, Pin, Nval*Nbytes);
     230  }
     231
     232  return TRUE;
    141233}
    142234
Note: See TracChangeset for help on using the changeset viewer.