Changeset 30698
- Timestamp:
- Feb 19, 2011, 10:23:46 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20110213/Ohana/src/tools/src/ftable.c
r28241 r30698 3 3 # include "inttypes.h" 4 4 5 char *print_table_row (char *row, Header *header);5 int print_table_rows (FTable *table, int start, int Nrows); 6 6 FILE *load_extension (char *file, int Nextend, char *Extname, Header *header); 7 7 void print_column (FTable *table, int Column, char *Colname); … … 13 13 int main (int argc, char **argv) { 14 14 15 off_t i,Nx, Ny, Nbytes, Nread, Row;15 off_t Nx, Ny, Nbytes, Nread, Row; 16 16 int N, Nextend, Column, ListExtname, Layout; 17 char *Extname, *Colname, *line,ttype[80];17 char *Extname, *Colname, ttype[80]; 18 18 FTable table; 19 19 Header header; … … 104 104 /* print a row */ 105 105 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 } 109 109 exit (0); 110 110 } 111 111 112 112 /* 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"); 117 115 } 118 116 exit (0); 119 117 } 120 118 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' */ 131 int print_table_rows (FTable *table, int start, int Nrows) { 123 132 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; 130 146 131 147 /* 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); 147 250 } 148 251 … … 325 428 sprintf (field, "TFORM%d", i); 326 429 gfits_scan (header, field, "%s", 1, format); 327 if (Binary) 430 if (Binary) { 431 gfits_bintable_format (format, type, &Nv, &Nb); 432 } else { 328 433 gfits_table_format (format, type, &Nv, &Nb); 329 else 330 gfits_bintable_format (format, type, &Nv, &Nb); 434 } 331 435 Nstart += Nv*Nb; 332 436 } … … 334 438 sprintf (field, "TFORM%d", Column); 335 439 gfits_scan (header, field, "%s", 1, format); 336 if (Binary) 440 if (Binary) { 337 441 gfits_bintable_format (format, type, &Nv, &Nb); /* convert to c-style */ 338 else442 } else { 339 443 gfits_table_format (format, type, &Nv, &Nb); /* convert to c-style */ 340 444 } 341 445 ALLOCATE (line, char, Nv*Nb + 1); 342 446
Note:
See TracChangeset
for help on using the changeset viewer.
