- Timestamp:
- Feb 26, 2016, 8:31:22 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ohana.20160226/src/libfits/table/F_compress_T.c
r39358 r39409 23 23 # define ESCAPE { fprintf (stderr, "error in %s @ line %d\n", __func__, __LINE__); goto escape; } 24 24 25 int gfits_collect_table_data (FTable *table, TableField *field, char *raw, int row_start,int Nrows);26 int gfits_collect_table_gzp2 (FTable *table, TableField *field, char *raw, int row_start,int Nrows);25 int gfits_collect_table_data (FTable *table, TableField *field, char *raw, unsigned long int row_start, unsigned long int Nrows); 26 int gfits_collect_table_gzp2 (FTable *table, TableField *field, char *raw, unsigned long int row_start, unsigned long int Nrows); 27 27 28 28 static float timeSum1 = 0.0; … … 51 51 } 52 52 53 int gfits_compress_table (FTable *srctable, FTable *tgttable, int ztilelen, char *zcmptype) { 54 55 int i, j; 53 int gfits_compress_table (FTable *srctable, FTable *tgttable, unsigned long int ztilelen, char *zcmptype) { 56 54 57 55 char keyword[81]; … … 69 67 gettimeofday (&startTimer, (void *) NULL); 70 68 71 int Ntile, ztilelast;69 unsigned long int Ntile, ztilelast; 72 70 if (!ztilelen) ztilelen = srcheader->Naxis[1]; 73 71 … … 104 102 105 103 ALLOCATE_ZERO (fields, TableField, Nfields); 106 for (i = 0; i < Nfields; i++) { 107 snprintf (keyword, 80, "TTYPE%d", i+1); 108 if (!gfits_scan (srcheader, keyword, "%s", 1, fields[i].ttype)) ESCAPE; 109 if (!gfits_scan_alt (srcheader, keyword, "%C", 1, fields[i].ttype_cmt)) ESCAPE; 104 105 int field; 106 for (field = 0; field < Nfields; field++) { 107 snprintf (keyword, 80, "TTYPE%d", field+1); 108 if (!gfits_scan (srcheader, keyword, "%s", 1, fields[field].ttype)) ESCAPE; 109 if (!gfits_scan_alt (srcheader, keyword, "%C", 1, fields[field].ttype_cmt)) ESCAPE; 110 110 111 111 // TUNIT is not mandatory 112 snprintf (keyword, 80, "TUNIT%d", i+1);113 if (!gfits_scan (srcheader, keyword, "%s", 1, fields[ i].tunit)) {114 fields[ i].tunit[0] = 0;112 snprintf (keyword, 80, "TUNIT%d", field+1); 113 if (!gfits_scan (srcheader, keyword, "%s", 1, fields[field].tunit)) { 114 fields[field].tunit[0] = 0; 115 115 } else { 116 if (!gfits_scan_alt (srcheader, keyword, "%C", 1, fields[ i].tunit_cmt)) ESCAPE;116 if (!gfits_scan_alt (srcheader, keyword, "%C", 1, fields[field].tunit_cmt)) ESCAPE; 117 117 } 118 118 119 snprintf (keyword, 80, "TFORM%d", i+1);120 if (!gfits_scan (srcheader, keyword, "%s", 1, fields[ i].tformat)) ESCAPE;121 if (!gfits_scan_alt (srcheader, keyword, "%C", 1, fields[ i].tformat_cmt)) ESCAPE;119 snprintf (keyword, 80, "TFORM%d", field+1); 120 if (!gfits_scan (srcheader, keyword, "%s", 1, fields[field].tformat)) ESCAPE; 121 if (!gfits_scan_alt (srcheader, keyword, "%C", 1, fields[field].tformat_cmt)) ESCAPE; 122 122 123 123 // for now we set all fields to the requested type. since I do not yet know the column data types I cannot yet assign automatic cmptypes 124 strcpy (fields[ i].zctype, zcmptype);124 strcpy (fields[field].zctype, zcmptype); 125 125 126 126 // by using "P" format, we are limited to 32bit pointers (2GB heap) 127 if (!gfits_define_bintable_column (tgtheader, "1QB(0)", fields[ i].ttype, fields[i].ttype_cmt, fields[i].tunit, 1.0, 0.0)) ESCAPE;127 if (!gfits_define_bintable_column (tgtheader, "1QB(0)", fields[field].ttype, fields[field].ttype_cmt, fields[field].tunit, 1.0, 0.0)) ESCAPE; 128 128 } 129 129 … … 137 137 char *tmpbuffer = NULL; 138 138 ALLOCATE_ZERO (tmpbuffer, char, 16*Ntile); // need space for Ntile entries, each of width 16 bytes (2 long) 139 for ( i = 0; i < Nfields; i++) {140 if (!gfits_set_bintable_column (tgtheader, tgttable, fields[ i].ttype, tmpbuffer, Ntile)) ESCAPE;139 for (field = 0; field < Nfields; field++) { 140 if (!gfits_set_bintable_column (tgtheader, tgttable, fields[field].ttype, tmpbuffer, Ntile)) ESCAPE; 141 141 } 142 142 free (tmpbuffer); … … 146 146 147 147 // define compression-specific keywords, update header as needed. 148 if (!gfits_modify (tgtheader, "ZTILELEN", "% d", 1, ztilelen)) ESCAPE;148 if (!gfits_modify (tgtheader, "ZTILELEN", "%lud", 1, ztilelen)) ESCAPE; 149 149 150 150 if (!gfits_modify (tgtheader, "ZNAXIS1", OFF_T_FMT, 1, srcheader->Naxis[0])) ESCAPE; … … 163 163 int offset = 0; // bytes from first column of first field to the current field (accumulate to set) 164 164 165 for ( i = 0; i < Nfields; i++) {166 snprintf (keyword, 81, "ZFORM%d", i+1);167 if (!gfits_modify (tgtheader, keyword, "%s", 1, fields[ i].tformat)) ESCAPE;168 if (!gfits_varlength_column_define (tgttable, &fields[ i].zdef, i+1)) ESCAPE;169 if (!gfits_bintable_format (fields[ i].tformat, fields[i].datatype, &fields[i].Nvalues, &fields[i].pixsize)) ESCAPE; //170 fields[ i].rowsize = fields[i].Nvalues*fields[i].pixsize;171 max_width = MAX(max_width, fields[ i].rowsize);172 173 if (!strcasecmp (fields[ i].zctype, "AUTO")) {174 if (!strcmp (fields[ i].datatype, "short") ||175 !strcmp (fields[ i].datatype, "float") ||176 !strcmp (fields[ i].datatype, "double")||177 !strcmp (fields[ i].datatype, "int64_t")) {178 strcpy (fields[ i].zctype, "GZIP_2");165 for (field = 0; field < Nfields; field++) { 166 snprintf (keyword, 81, "ZFORM%d", field+1); 167 if (!gfits_modify (tgtheader, keyword, "%s", 1, fields[field].tformat)) ESCAPE; 168 if (!gfits_varlength_column_define (tgttable, &fields[field].zdef, field+1)) ESCAPE; 169 if (!gfits_bintable_format (fields[field].tformat, fields[field].datatype, &fields[field].Nvalues, &fields[field].pixsize)) ESCAPE; // 170 fields[field].rowsize = fields[field].Nvalues*fields[field].pixsize; 171 max_width = MAX(max_width, fields[field].rowsize); 172 173 if (!strcasecmp (fields[field].zctype, "AUTO")) { 174 if (!strcmp (fields[field].datatype, "short") || 175 !strcmp (fields[field].datatype, "float") || 176 !strcmp (fields[field].datatype, "double")|| 177 !strcmp (fields[field].datatype, "int64_t")) { 178 strcpy (fields[field].zctype, "GZIP_2"); 179 179 goto got_cmptype; 180 180 } 181 if (!strcmp (fields[ i].datatype, "int")) {182 strcpy (fields[ i].zctype, "RICE_1");181 if (!strcmp (fields[field].datatype, "int")) { 182 strcpy (fields[field].zctype, "RICE_1"); 183 183 goto got_cmptype; 184 184 } 185 if (!strcmp (fields[ i].datatype, "byte") ||186 !strcmp (fields[ i].datatype, "char")) {187 strcpy (fields[ i].zctype, "GZIP_1");185 if (!strcmp (fields[field].datatype, "byte") || 186 !strcmp (fields[field].datatype, "char")) { 187 strcpy (fields[field].zctype, "GZIP_1"); 188 188 goto got_cmptype; 189 189 } … … 193 193 194 194 // OVERRIDE: RICE can only be used on integer fields 195 if (!strcasecmp (fields[ i].zctype, "RICE_1") || !strcasecmp (fields[i].zctype, "RICE_ONE")) {196 if (!strcmp (fields[ i].datatype, "float") || !strcmp (fields[i].datatype, "double") || !strcmp (fields[i].datatype, "int64_t")) {197 strcpy (fields[ i].zctype, "GZIP_2");195 if (!strcasecmp (fields[field].zctype, "RICE_1") || !strcasecmp (fields[field].zctype, "RICE_ONE")) { 196 if (!strcmp (fields[field].datatype, "float") || !strcmp (fields[field].datatype, "double") || !strcmp (fields[field].datatype, "int64_t")) { 197 strcpy (fields[field].zctype, "GZIP_2"); 198 198 } 199 199 } 200 200 201 201 // OVERRIDE: GZIP_2 invalid for B (use GZIP_1) 202 if (!strcasecmp (fields[ i].zctype, "GZIP_2") && !strcmp (fields[i].datatype, "byte")) {203 strcpy (fields[ i].zctype, "GZIP_1");202 if (!strcasecmp (fields[field].zctype, "GZIP_2") && !strcmp (fields[field].datatype, "byte")) { 203 strcpy (fields[field].zctype, "GZIP_1"); 204 204 } 205 205 206 206 // compression type per column (XXX for now we are just using zcmptype, as set above) 207 snprintf (keyword, 81, "ZCTYP%d", i+1);208 if (!gfits_modify (tgtheader, keyword, "%s", 1, fields[ i].zctype)) ESCAPE;209 210 fields[ i].offset = offset;211 offset += fields[ i].rowsize;207 snprintf (keyword, 81, "ZCTYP%d", field+1); 208 if (!gfits_modify (tgtheader, keyword, "%s", 1, fields[field].zctype)) ESCAPE; 209 210 fields[field].offset = offset; 211 offset += fields[field].rowsize; 212 212 } 213 213 214 214 // allocate the intermediate storage buffers 215 int Nzdata_alloc = 2*max_width*ztilelen + 100;215 unsigned long int Nzdata_alloc = 2*max_width*ztilelen + 100; 216 216 ALLOCATE (raw, char, max_width*ztilelen); 217 217 ALLOCATE (zdata, char, Nzdata_alloc); … … 229 229 // compress the data : copy into a tile, compress the tile, then add to the output table 230 230 // each tile -> 1 row of the output table 231 for (i = 0; i < Ntile; i++) { 232 233 for (j = 0; j < Nfields; j++) { 234 235 gettimeofday (&startTimer, (void *) NULL); 236 237 int Nrows = (i == Ntile - 1) ? ztilelast : ztilelen; 238 int row_start = i*ztilelen; 231 unsigned long int tile; 232 for (tile = 0; tile < Ntile; tile++) { 233 234 for (field = 0; field < Nfields; field++) { 235 236 gettimeofday (&startTimer, (void *) NULL); 237 238 unsigned long int Nrows = (tile == Ntile - 1) ? ztilelast : ztilelen; 239 unsigned long int row_start = tile*ztilelen; 239 240 // ^- first row for this tile & field 240 241 // NOTE: assumes each tile has the same length, ex the last (true for now) 241 242 242 243 // copy the raw pixels from their native matrix locations to the temporary output buffer 243 if (!strcasecmp(fields[ j].zctype, "GZIP_2") || !strcasecmp(fields[j].zctype, "NONE_2")) {244 if (!gfits_collect_table_gzp2 (srctable, &fields[ j], raw, row_start, Nrows)) ESCAPE;244 if (!strcasecmp(fields[field].zctype, "GZIP_2") || !strcasecmp(fields[field].zctype, "NONE_2")) { 245 if (!gfits_collect_table_gzp2 (srctable, &fields[field], raw, row_start, Nrows)) ESCAPE; 245 246 } else { 246 if (!gfits_collect_table_data (srctable, &fields[ j], raw, row_start, Nrows)) ESCAPE;247 if (!gfits_collect_table_data (srctable, &fields[field], raw, row_start, Nrows)) ESCAPE; 247 248 } 248 249 … … 256 257 257 258 if (VERBOSE_DUMP) { 258 int k;259 unsigned long int k; 259 260 fprintf (stderr, "c1: "); 260 for (k = 0; k < Nrows*fields[ j].Nvalues*fields[j].pixsize; k++) {261 for (k = 0; k < Nrows*fields[field].Nvalues*fields[field].pixsize; k++) { 261 262 fprintf (stderr, "%02hhx", raw[k]); 262 263 if (k % 2) fprintf (stderr, " "); … … 269 270 // optname, optvalue = NULL, Noptions = 0 270 271 271 int Nraw = Nrows*fields[j].Nvalues; // number of pixels272 if (!strcasecmp(fields[ j].zctype, "GZIP_1")) {273 if (!gfits_byteswap_zdata (raw, Nraw * fields[ j].pixsize, fields[j].pixsize)) ESCAPE;272 unsigned long int Nraw = Nrows*fields[field].Nvalues; // number of pixels 273 if (!strcasecmp(fields[field].zctype, "GZIP_1")) { 274 if (!gfits_byteswap_zdata (raw, Nraw * fields[field].pixsize, fields[field].pixsize)) ESCAPE; 274 275 } 275 276 … … 281 282 282 283 if (VERBOSE_DUMP) { 283 int k;284 unsigned long int k; 284 285 fprintf (stderr, "c2: "); 285 for (k = 0; k < Nrows*fields[ j].Nvalues*fields[j].pixsize; k++) {286 for (k = 0; k < Nrows*fields[field].Nvalues*fields[field].pixsize; k++) { 286 287 fprintf (stderr, "%02hhx", raw[k]); 287 288 if (k % 2) fprintf (stderr, " "); … … 291 292 } 292 293 293 int Nzdata = Nzdata_alloc; // available space, replaced with actual output size on compression294 if (!gfits_compress_data (zdata, &Nzdata, fields[ j].zctype, NULL, NULL, 0, raw, Nraw, fields[j].pixsize, 0, 0)) ESCAPE;294 unsigned long int Nzdata = Nzdata_alloc; // available space, replaced with actual output size on compression 295 if (!gfits_compress_data (zdata, &Nzdata, fields[field].zctype, NULL, NULL, 0, raw, Nraw, fields[field].pixsize, 0, 0)) ESCAPE; 295 296 296 297 // XXX TIMER 2c … … 301 302 302 303 if (VERBOSE_DUMP) { 303 int k;304 unsigned long int k; 304 305 fprintf (stderr, "c3: "); 305 306 for (k = 0; k < Nzdata; k++) { … … 311 312 } 312 313 313 if (strcasecmp(fields[ j].zctype, "NONE_2") && // NONE and NONE_1 not swapped?314 strcasecmp(fields[ j].zctype, "GZIP_1") &&315 strcasecmp(fields[ j].zctype, "GZIP_2") &&316 strcasecmp(fields[ j].zctype, "RICE_1") &&317 strcasecmp(fields[ j].zctype, "RICE_ONE")) {318 if (!gfits_byteswap_zdata (zdata, Nzdata, fields[ j].pixsize)) ESCAPE;314 if (strcasecmp(fields[field].zctype, "NONE_2") && // NONE and NONE_1 not swapped? 315 strcasecmp(fields[field].zctype, "GZIP_1") && 316 strcasecmp(fields[field].zctype, "GZIP_2") && 317 strcasecmp(fields[field].zctype, "RICE_1") && 318 strcasecmp(fields[field].zctype, "RICE_ONE")) { 319 if (!gfits_byteswap_zdata (zdata, Nzdata, fields[field].pixsize)) ESCAPE; 319 320 } 320 321 … … 326 327 327 328 if (VERBOSE_DUMP) { 328 int k;329 unsigned long int k; 329 330 fprintf (stderr, "c4: "); 330 331 for (k = 0; k < Nzdata; k++) { … … 336 337 } 337 338 338 if (!gfits_varlength_column_add_data (tgttable, zdata, Nzdata, i, &fields[j].zdef)) ESCAPE;339 if (!gfits_varlength_column_add_data (tgttable, zdata, Nzdata, field, &fields[field].zdef)) ESCAPE; 339 340 // XXX TIMER 2e 340 341 gettimeofday (&stopTimer, (void *) NULL); … … 347 348 if (OHANA_MEMCHECK) ohana_memcheck (TRUE); 348 349 349 for ( i = 0; i < Nfields; i++) {350 if (!gfits_varlength_column_finish (tgttable, &fields[ i].zdef)) ESCAPE;350 for (field = 0; field < Nfields; field++) { 351 if (!gfits_varlength_column_finish (tgttable, &fields[field].zdef)) ESCAPE; 351 352 } 352 353 … … 375 376 // raw_pixsize is the bytes / pixel for the input matrix 376 377 // place the raw image bytes for the current tile into the tile buffer 377 int gfits_collect_table_data (FTable *table, TableField *field, char *raw, int row_start,int Nrows) {378 379 off_t i;378 int gfits_collect_table_data (FTable *table, TableField *field, char *raw, unsigned long int row_start, unsigned long int Nrows) { 379 380 unsigned long int i; 380 381 381 382 // we are copying NN rows into the column which starts at XX and has MM bytes per row … … 399 400 } 400 401 401 int gfits_collect_table_data_alt (FTable *table, TableField *field, char *raw, int row_start,int Nrows) {402 403 off_t i;402 int gfits_collect_table_data_alt (FTable *table, TableField *field, char *raw, unsigned long int row_start, unsigned long int Nrows) { 403 404 unsigned long int i; 404 405 405 406 // we are copying NN rows of the column which starts at XX and has MM bytes per row … … 420 421 } 421 422 422 int gfits_collect_table_gzp2 (FTable *table, TableField *field, char *raw, int row_start,int Nrows) {423 424 off_t i,j, k;423 int gfits_collect_table_gzp2 (FTable *table, TableField *field, char *raw, unsigned long int row_start, unsigned long int Nrows) { 424 425 off_t j, k; 425 426 426 427 // we are copying NN rows into the column which starts at XX and has MM bytes per row … … 439 440 char *tblptr_start = &table->buffer[Nx*row_start + offset + k]; 440 441 # endif 442 unsigned long int i; 441 443 for (i = 0; i < Nrows; i++, tblptr_start += Nx) { 442 444 char *tblptr = tblptr_start; … … 449 451 } 450 452 451 int gfits_collect_table_gzp2_alt (FTable *table, TableField *field, char *raw, int row_start,int Nrows) {452 453 off_t i,j, k;453 int gfits_collect_table_gzp2_alt (FTable *table, TableField *field, char *raw, unsigned long int row_start, unsigned long int Nrows) { 454 455 off_t j, k; 454 456 455 457 // we are copying NN rows into the column which starts at XX and has MM bytes per row … … 458 460 459 461 for (k = 0; k < field->pixsize; k++) { 462 unsigned long int i; 460 463 for (i = 0; i < Nrows; i++) { 461 int row = row_start + i;464 unsigned long int row = row_start + i; 462 465 char *rawptr = &raw[i*field->Nvalues + k*Nrows*field->Nvalues]; 463 466 # ifdef BYTE_SWAP … … 468 471 for (j = 0; j < field->Nvalues; j++, tblptr += field->pixsize, rawptr++) { 469 472 *rawptr = *tblptr; 470 myAssert (rawptr - raw < Nrows*field->Nvalues*field->pixsize, "oops"); 473 myAssert (rawptr >= raw, "oops"); 474 myAssert ((unsigned long int)(rawptr - raw) < Nrows*field->Nvalues*field->pixsize, "oops"); 471 475 myAssert (tblptr - table->buffer < table->header->Naxis[0]*table->header->Naxis[1], "oops"); 472 476 }
Note:
See TracChangeset
for help on using the changeset viewer.
