IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 30698


Ignore:
Timestamp:
Feb 19, 2011, 10:23:46 AM (15 years ago)
Author:
eugene
Message:

fix row printing and full table printing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20110213/Ohana/src/tools/src/ftable.c

    r28241 r30698  
    33# include "inttypes.h"
    44
    5 char *print_table_row (char *row, Header *header);
     5int print_table_rows (FTable *table, int start, int Nrows);
    66FILE *load_extension (char *file, int Nextend, char *Extname, Header *header);
    77void print_column (FTable *table, int Column, char *Colname);
     
    1313int main (int argc, char **argv) {
    1414
    15   off_t i, Nx, Ny, Nbytes, Nread, Row;
     15  off_t Nx, Ny, Nbytes, Nread, Row;
    1616  int N, Nextend, Column, ListExtname, Layout;
    17   char *Extname, *Colname, *line, ttype[80];
     17  char *Extname, *Colname, ttype[80];
    1818  FTable table;
    1919  Header header;
     
    104104  /* print a row */
    105105  if (Row) {
    106     line = print_table_row (&table.buffer[Nx*Row], table.header);
    107     fprintf (stdout, "%s\n", line);
    108     free (line);
     106    if (!print_table_rows (&table, Row, 1)) {
     107      fprintf (stderr, "failed to print row\n");
     108    }
    109109    exit (0);
    110110  }
    111111
    112112  /* print complete table */
    113   for (i = 0; i < Ny; i++) {
    114     line = print_table_row (&table.buffer[Nx*i], table.header);
    115     fprintf (stdout, "%s\n", line);
    116     free (line);
     113  if (!print_table_rows (&table, 0, Ny)) {
     114    fprintf (stderr, "failed to print table\n");
    117115  }   
    118116  exit (0);
    119117}
    120118
    121 /* print an ASCII table to a row with single spaces separating value */
    122 char *print_table_row (char *row, Header *header) {
     119# define SWAP_BYTE(BYTE)                                        \
     120  tmp = BYTE[0]; BYTE[0] = BYTE[1]; BYTE[1] = tmp;
     121# define SWAP_WORD(BYTE) \
     122  tmp = BYTE[0]; BYTE[0] = BYTE[3]; BYTE[3] = tmp; \
     123  tmp = BYTE[1]; BYTE[1] = BYTE[2]; BYTE[2] = tmp;
     124# define SWAP_DBLE(BYTE) \
     125  tmp = BYTE[0]; BYTE[0] = BYTE[7]; BYTE[7] = tmp; \
     126  tmp = BYTE[1]; BYTE[1] = BYTE[6]; BYTE[6] = tmp; \
     127  tmp = BYTE[2]; BYTE[2] = BYTE[5]; BYTE[5] = tmp; \
     128  tmp = BYTE[3]; BYTE[3] = BYTE[4]; BYTE[4] = tmp;
     129
     130/* print Nrows of the given table starting at row 'start' */
     131int print_table_rows (FTable *table, int start, int Nrows) {
    123132 
    124   off_t Nx;
    125   int i, j, Nfields, Nbytes, Nvals, Oout, Oin;
    126   char field[16], type[16], format[16], *line;
    127 
    128   gfits_scan (header, "NAXIS1",  OFF_T_FMT, 1,  &Nx);
    129   gfits_scan (header, "TFIELDS", "%d", 1, &Nfields);
     133  off_t Nx, Ny;
     134  int n, i, j, Nfields, *Nbyte, *Nvals, Oout, Oin, Nv, Nb, byte, status;
     135  char field[16], **types, format[16], type[80], *line, *row, tmp;
     136  double *Tzero, *Tscal;
     137
     138  gfits_scan (table->header, "NAXIS1",  OFF_T_FMT, 1,  &Nx);
     139  gfits_scan (table->header, "NAXIS2",  OFF_T_FMT, 1,  &Ny);
     140  gfits_scan (table->header, "TFIELDS", "%d", 1, &Nfields);
     141
     142  if (start <   0) return FALSE;
     143  if (start >= Ny) return FALSE;
     144  if (Nrows <   0) return FALSE;
     145  if (start + Nrows > Ny) return FALSE;
    130146
    131147  /* assume we have one space per byte column */
    132   ALLOCATE (line, char, 2*Nx + 1);
    133 
    134   Oin = Oout = 0;
    135   for (i = 1; i <= Nfields; i++) {
    136     sprintf (field, "TFORM%d", i);
    137     gfits_scan (header, field, "%s", 1, format); /* get field format */
    138     gfits_table_format (format, type, &Nvals, &Nbytes);    /* convert to c-style */
    139     memcpy (&line[Oout], &row[Oin], Nvals*Nbytes);
    140     for (j = 0; j < Nvals*Nbytes; j++) if (line[Oout+j] == 0) line[Oout+j] = ' ';
    141     line[Oout+Nvals*Nbytes] = ' ';
    142     Oout += Nvals*Nbytes + 1;
    143     Oin += Nvals*Nbytes;
    144   }
    145 
    146   return (line);
     148  ALLOCATE (line, char, MAX(2*Nx+1,512));
     149
     150  ALLOCATE (types, char *, Nfields);
     151  ALLOCATE (Nvals, int, Nfields);
     152  ALLOCATE (Nbyte, int, Nfields);
     153  ALLOCATE (Tzero, double, Nfields);
     154  ALLOCATE (Tscal, double, Nfields);
     155
     156  // determine the layout of the columns
     157  for (i = 0; i < Nfields; i++) {
     158    sprintf (field, "TFORM%d", i+1);
     159    gfits_scan (table->header, field, "%s", 1, format); /* get field format */
     160
     161    if (Binary)  {
     162      gfits_bintable_format (format, type, &Nv, &Nb);    /* convert to c-style */
     163
     164      sprintf (field, "TZERO%d", i+1);
     165      status = gfits_scan (table[0].header, field, "%lf", 1, &Tzero[i]);       /* get field format */
     166      if (!status) Tzero[i] = 0.0;
     167     
     168      sprintf (field, "TSCAL%d", i+1);
     169      status = gfits_scan (table[0].header, field, "%lf", 1, &Tscal[i]);       /* get field format */
     170      if (!status) Tscal[i] = 1.0;
     171    } else {
     172      gfits_table_format (format, type, &Nv, &Nb);    /* convert to c-style */
     173    }
     174   
     175    types[i] = strcreate (type);
     176    Nvals[i] = Nv;
     177    Nbyte[i] = Nb;
     178  }
     179
     180  for (n = start; n < start + Nrows; n++) {
     181
     182    row = &table->buffer[Nx*n];
     183
     184    if (Binary) {
     185      byte = 0;  // counter for byte element of this row
     186      for (i = 0; i < Nfields; i++) {
     187        int found = FALSE;
     188        if (!strcmp (types[i], "char")) {
     189          memcpy (line, &row[byte], Nvals[i]*Nbyte[i]);
     190          fprintf (stdout, "%s ", line);
     191          found = TRUE;
     192        } else {
     193          for (j = 0; j < Nvals[i]; j++) {
     194            memcpy (line, &row[byte + Nbyte[i]*j], Nbyte[i]);
     195            if (!strcmp (types[i], "int")) {
     196# ifdef BYTE_SWAP
     197              SWAP_WORD (line);
     198# endif
     199              fprintf (stdout, "%d ", (int)(*(int *)line * Tscal[i] + Tzero[i]));
     200              found = TRUE;
     201            }
     202            if (!strcmp (types[i], "short")) {
     203# ifdef BYTE_SWAP
     204              SWAP_BYTE (line);
     205# endif
     206              fprintf (stdout, "%d ", (int)(*(short *)line * Tscal[i] + Tzero[i]));
     207              found = TRUE;
     208            }
     209            if (!strcmp (types[i], "int64_t")) {
     210# ifdef BYTE_SWAP
     211              SWAP_DBLE (line);
     212# endif
     213              fprintf (stdout, "%" PRId64" ",  (int64_t)(*(int64_t*)line * Tscal[i] + Tzero[i]));
     214              found = TRUE;
     215            }
     216            if (!strcmp (types[i], "float")) {
     217# ifdef BYTE_SWAP
     218              SWAP_WORD (line);
     219# endif
     220              fprintf (stdout, "%e ", (*(float *)line * Tscal[i] + Tzero[i]));
     221              found = TRUE;
     222            }
     223            if (!strcmp (types[i], "double")) {
     224# ifdef BYTE_SWAP
     225              SWAP_DBLE (line);
     226# endif
     227              fprintf (stdout, "%e ", (*(double *)line * Tscal[i] + Tzero[i]));
     228              found = TRUE;
     229            }
     230          }
     231        }
     232        byte += Nvals[i]*Nbyte[i];
     233        if (!found) {
     234          fprintf (stderr, "failed to find format for %d : %s\n", i, types[i]);
     235        }
     236      }
     237    } else {
     238      for (i = 0; i < Nfields; i++) {
     239        memcpy (&line[Oout], &row[Oin], Nvals[i]*Nbyte[i]);
     240        for (j = 0; j < Nvals[i]*Nbyte[i]; j++) if (line[Oout+j] == 0) line[Oout+j] = ' ';
     241        line[Oout+Nvals[i]*Nbyte[i]] = ' ';
     242        Oout += Nvals[i]*Nbyte[i] + 1;
     243        Oin += Nvals[i]*Nbyte[i];
     244      }
     245      fprintf (stdout, "%s ", line);
     246    }
     247    fprintf (stdout, "\n");
     248  }
     249  return (TRUE);
    147250}
    148251
     
    325428    sprintf (field, "TFORM%d", i);
    326429    gfits_scan (header, field, "%s", 1, format);
    327     if (Binary)
     430    if (Binary)  {
     431      gfits_bintable_format (format, type, &Nv, &Nb);
     432    } else {
    328433      gfits_table_format (format, type, &Nv, &Nb);
    329     else
    330       gfits_bintable_format (format, type, &Nv, &Nb);
     434    }
    331435    Nstart += Nv*Nb;
    332436  }
     
    334438  sprintf (field, "TFORM%d", Column);
    335439  gfits_scan (header, field, "%s", 1, format);
    336   if (Binary)
     440  if (Binary)  {
    337441    gfits_bintable_format (format, type, &Nv, &Nb);    /* convert to c-style */
    338   else
     442  } else {
    339443    gfits_table_format (format, type, &Nv, &Nb);    /* convert to c-style */
    340 
     444  }
    341445  ALLOCATE (line, char, Nv*Nb + 1);
    342446
Note: See TracChangeset for help on using the changeset viewer.