Changeset 38329
- Timestamp:
- May 28, 2015, 8:27:00 PM (11 years ago)
- Location:
- branches/eam_branches/ohana.20150429/src/libfits
- Files:
-
- 5 added
- 18 edited
-
Makefile (modified) (2 diffs)
-
extern/gzip.c (modified) (3 diffs)
-
include/gfitsio.h (modified) (6 diffs)
-
matrix/F_compress_M.c (modified) (7 diffs)
-
matrix/F_compress_data.c (modified) (2 diffs)
-
matrix/F_compress_utils.c (modified) (1 diff)
-
matrix/F_matrix.c (modified) (1 diff)
-
matrix/F_uncompress_M.c (modified) (1 diff)
-
matrix/F_uncompress_data.c (modified) (3 diffs)
-
table/F_compress_T.c (added)
-
table/F_copy_T.c (modified) (2 diffs)
-
table/F_create_T.c (modified) (3 diffs)
-
table/F_define_column.c (modified) (1 diff)
-
table/F_read_T.c (modified) (5 diffs)
-
table/F_set_column.c (modified) (6 diffs)
-
table/F_table_format.c (modified) (3 diffs)
-
table/F_table_row.c (modified) (3 diffs)
-
table/F_table_varlength.c (modified) (9 diffs)
-
table/F_uncompress_T.c (added)
-
table/F_write_T.c (modified) (1 diff)
-
test (added)
-
test/compress.sh (added)
-
test/test.zlib.c (added)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ohana.20150429/src/libfits/Makefile
r37807 r38329 37 37 38 38 MATRIX_OBJ = \ 39 $(MATR)/F_add_M_value.$(ARCH).o $(MATR)/F_add_M.$(ARCH).o \ 40 $(MATR)/F_create_M.$(ARCH).o $(MATR)/F_divide_M.$(ARCH).o \ 41 $(MATR)/F_free_M.$(ARCH).o $(MATR)/F_copy_M.$(ARCH).o \ 42 $(MATR)/F_write_M.$(ARCH).o $(MATR)/F_get_M_value.$(ARCH).o \ 43 $(MATR)/F_multiply_M.$(ARCH).o $(MATR)/F_read_M.$(ARCH).o \ 44 $(MATR)/F_insert_M.$(ARCH).o $(MATR)/F_set_M_value.$(ARCH).o \ 45 $(MATR)/F_convert_format.$(ARCH).o $(MATR)/F_read_segment.$(ARCH).o \ 46 $(MATR)/F_read_portion.$(ARCH).o $(MATR)/F_load_M.$(ARCH).o \ 47 $(MATR)/F_matrix.$(ARCH).o $(MATR)/F_compress_M.$(ARCH).o \ 48 $(MATR)/F_uncompress_data.$(ARCH).o 39 $(MATR)/F_add_M_value.$(ARCH).o $(MATR)/F_add_M.$(ARCH).o \ 40 $(MATR)/F_create_M.$(ARCH).o $(MATR)/F_divide_M.$(ARCH).o \ 41 $(MATR)/F_free_M.$(ARCH).o $(MATR)/F_copy_M.$(ARCH).o \ 42 $(MATR)/F_write_M.$(ARCH).o $(MATR)/F_get_M_value.$(ARCH).o \ 43 $(MATR)/F_multiply_M.$(ARCH).o $(MATR)/F_read_M.$(ARCH).o \ 44 $(MATR)/F_insert_M.$(ARCH).o $(MATR)/F_set_M_value.$(ARCH).o \ 45 $(MATR)/F_convert_format.$(ARCH).o $(MATR)/F_read_segment.$(ARCH).o \ 46 $(MATR)/F_read_portion.$(ARCH).o $(MATR)/F_load_M.$(ARCH).o \ 47 $(MATR)/F_matrix.$(ARCH).o $(MATR)/F_compress_utils.$(ARCH).o \ 48 $(MATR)/F_compress_M.$(ARCH).o $(MATR)/F_compress_data.$(ARCH).o \ 49 $(MATR)/F_uncompress_M.$(ARCH).o $(MATR)/F_uncompress_data.$(ARCH).o 49 50 50 51 TABLE_OBJ = \ … … 55 56 $(TABL)/F_set_column.$(ARCH).o $(TABL)/F_get_column.$(ARCH).o \ 56 57 $(TABL)/F_table_row.$(ARCH).o $(TABL)/F_free_T.$(ARCH).o \ 57 $(TABL)/F_table_varlength.$(ARCH).o $(TABL)/F_copy_T.$(ARCH).o 58 $(TABL)/F_table_varlength.$(ARCH).o $(TABL)/F_copy_T.$(ARCH).o \ 59 $(TABL)/F_compress_T.$(ARCH).o $(TABL)/F_uncompress_T.$(ARCH).o 58 60 59 61 EXTERN_OBJ = \ -
branches/eam_branches/ohana.20150429/src/libfits/extern/gzip.c
r38322 r38329 131 131 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 132 132 133 stream.zalloc = (alloc_func)0; 134 stream.zfree = (free_func)0; 133 stream.zalloc = Z_NULL; 134 stream.zfree = Z_NULL; 135 stream.opaque = Z_NULL; 135 136 136 // MAX_WBITS = 15137 err = inflateInit 2(&stream, -15);137 // err = inflateInit2(&stream, -15); 138 err = inflateInit(&stream); 138 139 if (err != Z_OK) return err; 139 140 … … 156 157 z_stream stream; 157 158 int err; 158 159 159 160 stream.next_in = (Bytef*)source; 160 161 stream.avail_in = (uInt)sourceLen; … … 167 168 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 168 169 169 stream.zalloc = (alloc_func)0; 170 stream.zfree = (free_func)0; 170 stream.zalloc = Z_NULL; 171 stream.zfree = Z_NULL; 172 stream.opaque = Z_NULL; 171 173 172 err = deflateInit(&stream); 174 // the '5' is the compression level: make this a user argument 175 err = deflateInit(&stream, 5); 173 176 if (err != Z_OK) return err; 174 177 -
branches/eam_branches/ohana.20150429/src/libfits/include/gfitsio.h
r38062 r38329 73 73 off_t datasize; // size of the buffer (including block padding at the end) 74 74 off_t validsize; // size of the valid portion of the table (< validsize if file is short) 75 off_t heap_start; // byte offset to start of HEAP 75 76 } FTable; 76 77 … … 85 86 } VTable; 86 87 87 typedef struct { 88 int maxlen; // max size of all table rows 89 int nbytes; // number of bytes per column element 90 int Nstart; // byte offset of this column relative to first column 91 off_t heap_start; // byte offset to start of HEAP 92 char format; // data format character (one of: XLABIJEDCM) 93 char mode; // access data pointer type (one of P or Q) 88 // description / metadata needed for a single varlength column. A varlength column has a 89 // 2-element array giving the (length,offset) of data in the heap. Below, 'real data' are 90 // the values in the heap; 'metadata' are the values in the main table. 91 typedef struct { 92 int column; // field number (of the metadata) 93 char format; // format character for real data (one of: XLABIJKEDCM) 94 char mode; // format character for metadata (one of P [float] or Q [double]) 95 int maxlen; // max size of all table rows for this column 96 int nbytes; // number of bytes per real data column element 97 int offset; // byte offset of the metadata column relative to first column 94 98 } VarLengthColumn; 99 100 typedef struct { 101 char ttype[16]; // TTYPE field of original table 102 char ttype_cmt[80]; // comment associated with TTYPE 103 char tunit[16]; // TUNIT field of original table 104 char tunit_cmt[80]; // comment associated with TUNIT 105 char tformat[16]; // TFORM field of original table 106 char tformat_cmt[80]; // comment associated with TFORM 107 char zctype[80]; // compression type for this field 108 VarLengthColumn zdef; // description of the output variable length column 109 110 char datatype[80]; // named data type associated with ttype (eg, 18J -> int, 18D -> double) 111 int Nvalues; // number of values per row (eg 18J -> 18 int values) 112 int pixsize; // number of bytes per value of this row (eg, 18J -> 4 bytes) 113 int rowsize; // number of bytes for a full row (Nvalues * pixsize) 114 int offset; // byte offset of the real data column relative to first column 115 } TableField; 95 116 96 117 # ifndef PROTO … … 133 154 off_t gfits_data_min_size PROTO((Header *header)); 134 155 off_t gfits_data_pad_size PROTO((off_t rawsize)); 156 off_t gfits_heap_start PROTO((Header *header)); 135 157 int gfits_extended_to_primary PROTO((Header *header, int simple, char *comment)); 136 158 int gfits_primary_to_extended PROTO((Header *header, char *exttype, char *comment)); … … 166 188 void gfits_set_matrix_value PROTO((Matrix *matrix, off_t x, off_t y, double value)); 167 189 int gfits_write_matrix PROTO((char *filename, Matrix *matrix)); 190 168 191 int gfits_uncompress_image PROTO((Header *header, Matrix *matrix, FTable *ftable)); 169 192 int gfits_uncompress_data PROTO((char *zdata, int Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *outdata, int *Nout, int out_pixsize)); 170 193 int gfits_distribute_data PROTO((Matrix *matrix, int bitpix, char *data, int Ndata, int *otile, int oblank, int *ztile, int zblank, float zscale, float zzero)); 194 195 int gfits_compress_data PROTO((char *zdata, int *Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *rawdata, int Nraw, int raw_pixsize)); 196 int gfits_compress_image PROTO((Header *header, Matrix *matrix, FTable *ftable, int *Ztile, char *zcmptype)); 197 int gfits_collect_data PROTO((Matrix *matrix, char *raw, int Nraw, int raw_pixsize, int *otile, int oblank, int *ztile, int zblank, float zscale, float zzero)); 198 int gfits_copy_keywords_compress PROTO((Header *srchead, Header *tgthead)); 199 200 off_t gfits_tile_size PROTO((Matrix *matrix, int *otile, int *ztile)); 201 int gfits_imtile_maxsize PROTO((Matrix *matrix, int *ztile)); 202 int gfits_imtile_count PROTO((Matrix *matrix, int *ztile, int *ntile)); 203 int gfits_imtile_start PROTO((Matrix *matrix, int *otile)); 204 int gfits_imtile_next PROTO((Matrix *matrix, int *ztile, int *ntile, int *otile)); 205 171 206 int gfits_byteswap_zdata PROTO((char *zdata, int Nzdata, int bitpix)); 172 207 int gfits_extension_is_compressed PROTO((Header *header)); 173 off_t gfits_tile_size PROTO((Matrix *matrix, int *otile, int *ztile));174 208 int gfits_uncompressed_data_pixsize PROTO((char *cmptype, int out_bitpix, char **optname, char **optvalue, int Noptions)); 175 209 int gfits_vartable_heap_pixsize PROTO((char format)); 176 210 211 int gfits_varlength_column_add_data PROTO((FTable *table, char *data, off_t Ndata, int row, VarLengthColumn *column)); 212 void *gfits_varlength_column_pointer PROTO((FTable *ftable, VarLengthColumn *column, off_t row, off_t *length)); 213 int gfits_varlength_column_define PROTO((FTable *ftable, VarLengthColumn *def, int column)); 214 int gfits_varlength_column_finish PROTO((FTable *ftable, VarLengthColumn *def)); 215 int gfits_byteswap_varlength_column PROTO((FTable *ftable, int column)); 216 177 217 /******************************* Table functions *************/ 178 218 … … 182 222 char *gfits_table_print PROTO((FTable *ftable,...)); 183 223 int gfits_add_rows PROTO((FTable *ftable, char *data, off_t Nrow, off_t Nbytes)); 224 int gfits_set_table_rows PROTO((Header *header, FTable *table, off_t Nrows)); 184 225 int gfits_bintable_format PROTO((char *format, char *type, int *Nval, int *Nbytes)); 185 226 int gfits_create_table PROTO((Header *header, FTable *ftable)); … … 226 267 int gfits_write_Theader PROTO((char *filename, Header *header)); 227 268 228 int gfits_varlength_column_define PROTO((FTable *ftable, VarLengthColumn *def, int column)); 229 void *gfits_varlength_column_pointer PROTO((FTable *ftable, VarLengthColumn *column, off_t row, off_t *length)); 269 int gfits_compress_table PROTO((FTable *srctable, FTable *tgttable, int ztilelen, char *zcmptype)); 230 270 231 271 #endif /* FITSIO */ -
branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_M.c
r38326 r38329 3 3 # include <zlib.h> 4 4 5 # define ESCAPE { \6 if (ztile != NULL) free (ztile); \7 if (optname != NULL) { \8 for (j = 0; j < Noptions; j++) { \9 free (optname[j]); \10 free (optvalue[j]); \11 } \12 free (optname); \13 free (optvalue); \14 } \15 if (out != NULL) free (out); \16 if (otile != NULL) free (otile); \17 if (ntile != NULL) free (ntile); \18 return (FALSE); }19 20 # define MOD_KEYWORD(ZNAME,NAME,TYPE,OUT,VALUE) { \21 gfits_modify (theader, ZNAME, TYPE, 1, OUT); \22 gfits_delete (theader, NAME, 1); }23 24 5 // the user needs to specify the various compression options: 25 // ztile[i] 26 // zcmptype 6 // ztile[i] (may be NULL) 7 // zcmptype 27 8 28 9 // steps to convert an image to a compressed table: … … 35 16 // * update header keywords 36 17 37 // XXX do not try to support compression of an image with an associated table (not valid) 38 39 int gfits_compress_image (Header *header, Matrix *matrix, FTable *ftable, int *ztile, char *zcmptype) { 40 41 // off_t Nzdata, Nzrows, zcol; 42 // int i, j, status, zimage, Nout, max_tile_size; 43 // char cmptype[80]; 44 // char zaxis[10], naxis[10], key[10], word[81], exttype[81], checksum[81], datasum[81]; 45 // int Noptions, NOPTIONS, zblank, oblank; 46 // VarLengthColumn zdef; 47 // float zscale, zzero; 48 49 // int zdata_pixsize, odata_pixsize; 50 // int *otile = NULL; 51 // int *ntile = NULL; 52 // char **optname = NULL; 53 // char **optvalue = NULL; 54 // char *out = NULL; 55 // char *zdata = NULL; 18 // we do not try to support compression of an image with an associated table (not valid) 19 20 # define ESCAPE(A) { fprintf (stderr, "error in %s @ line %d\n", __func__, __LINE__); goto escape; } 21 22 int gfits_compress_image (Header *header, Matrix *matrix, FTable *ftable, int *Ztile, char *zcmptype) { 23 24 int i; 25 26 // pointers to track the tile data 27 int *otile = NULL; 28 int *ntile = NULL; 29 char *raw = NULL; 30 char *zdata = NULL; 31 32 int *ztile = Ztile; 33 34 Header *theader = ftable->header; 56 35 57 36 // determine the number of tiles (from ztile[] and image size) … … 67 46 68 47 // creates an empty (Naxes = 2, Naxis[i] = 0) table header with XTENSION = BINTABLE, EXTNAME = COMPRESSED_IMAGE 69 gfits_create_table_header (&theader, "BINTABLE", "COMPRESSED_IMAGE");48 if (!gfits_create_table_header (theader, "BINTABLE", "COMPRESSED_IMAGE")) ESCAPE(A); 70 49 71 50 // by using "P" format, we are limited to 32bit pointers (2GB heap) 72 51 // XXX how is the "B" format used? 73 gfits_define_bintable_column (&theader, "1PB(0)", "COMPRESSED_DATA", "compressed image data", "none", 1.0, 0.0); 74 75 // XXX I need to copy the bulk of the header from the matrix header -> ftable.header 76 gfits_create_table (&theader, ftable); 77 // copy original header to output header 78 // XXX gfits_copy_header (header, theader); -- I need to copy only the desired keywords. 52 if (!gfits_define_bintable_column (theader, "1PB(0)", "COMPRESSED_DATA", "compressed image data", "none", 1.0, 0.0)) ESCAPE (A); 53 if (!gfits_delete (theader, "TUNIT1", 1)) ESCAPE(A); 54 55 // allocates the default data array (ftable->buffer) 56 if (!gfits_create_table (theader, ftable)) ESCAPE (A); 57 58 // copy original header to output header (XXX this needs to be finished) 59 if (!gfits_copy_keywords_compress (header, theader)) ESCAPE (A); 79 60 80 // this allocates the data array61 // this allocates the pointer data array for the full set of tiles (NOT the heap) 81 62 char *tmpbuffer = NULL; 82 63 ALLOCATE_ZERO (tmpbuffer, char, Ntile); 83 gfits_set_bintable_column (&theader, &ftable, "COMPRESSED_DATA", tmpbuffer, Ntile); 64 if (!gfits_set_bintable_column (theader, ftable, "COMPRESSED_DATA", tmpbuffer, Ntile)) ESCAPE (A); 65 free (tmpbuffer); 84 66 85 67 // add ZIMAGE from output header 86 gfits_modify_alt (theader, "ZIMAGE", "%t", 1, TRUE); 87 88 // translate image-specific keywords to Z versions (updates the Z version, removes the old version) 89 // XXX use the header or the structure as authoratative? 90 MOVE_KEYWORD ("ZBITPIX", "BITPIX", "%d"); 91 MOVE_KEYWORD ("ZNAXIS", "NAXIS", "%d"); 68 if (!gfits_modify_alt (theader, "ZIMAGE", "%t", 1, TRUE)) ESCAPE (A); 69 70 // define compression-specific keywords, update header as needed. 71 if (!gfits_modify (theader, "ZCMPTYPE", "%s", 1, zcmptype)) ESCAPE(A); 72 73 if (!gfits_modify_alt (theader, "ZSIMPLE", "%t", 1, TRUE)) ESCAPE (A); 74 // gfits_modify (header, "ZTENSION", "%s", 1, exttype); 75 76 // supply the Z* header values from the source image header 77 if (!gfits_modify (theader, "ZBITPIX", "%d", 1, header[0].bitpix)) ESCAPE (A); 78 79 if (!gfits_modify (theader, "ZNAXIS", "%d", 1, header[0].Naxes)) ESCAPE (A); 80 81 char keyword[11]; 82 for (i = 0; i < header[0].Naxes; i++) { 83 snprintf (keyword, 10, "ZNAXIS%d", i + 1); 84 if (!gfits_modify (theader, keyword, OFF_T_FMT, 1, header[0].Naxis[i])) ESCAPE (A); 85 } 92 86 93 87 for (i = 0; i < header->Naxes; i++) { 94 snprintf (zaxis, 10, "ZNAXIS%d", i + 1); 95 snprintf (naxis, 10, "NAXIS%d", i + 1); 96 MOD_KEYWORD (zaxis, naxis, OFF_T_FMT, header->Naxis[i]); 97 } 98 99 for (i = 0; i < header->Naxes; i++) { 100 snprintf (key, 10, "ZTILE%d", i + 1); 101 if (!gfits_modify (theader, key, "%d", 1, ztile[i])) ESCAPE; 102 } 103 104 // define compression-specific keywords, update header as needed. 105 if (!gfits_modify (header, "ZCMPTYPE", "%s", 1, zcmptype)) ESCAPE; 88 snprintf (keyword, 10, "ZTILE%d", i + 1); 89 if (!gfits_modify (theader, keyword, "%d", 1, ztile[i])) ESCAPE(A); 90 } 91 92 if (!gfits_modify (theader, "BSCALE", "%lf", 1, header[0].bscale)) ESCAPE (A); 93 if (!gfits_modify (theader, "BZERO", "%lf", 1, header[0].bzero)) ESCAPE (A); 94 95 // other keywords to define: 96 // ZEXTEND 97 // ZBLOCKED 98 // ZHECKSUM 99 // ZDATASUM 100 // PCOUNT -> ZPCOUNT 101 // GCOUNT -> ZGCOUNT 102 103 float zscale = 1.0; 104 // gfits_modify (header, "ZSCALE", "%f", 1, zscale); 105 106 float zzero = 0.0; 107 // gfits_modify (header, "ZZERO", "%f", 1, zzero); 108 109 int zblank = 32767; 110 // gfits_modify (header, "ZBLANK", "%d", 1, zblank); 111 112 int oblank = 32767; 113 // gfits_modify (header, "BLANK", "%d", 1, oblank); 114 115 # if (0) 116 // need to set ZNAMEnn and ZVALnn if they are defined and used 106 117 107 118 // search for algorithm-specific keywords. these are used to control compression options … … 114 125 if (!gfits_modify (header, key, "%s", 1, optvalue[i])) break; 115 126 } 116 117 # if (0)118 // XXX get ZMASKCMP119 int zsimple, have_zsimple;120 int have_ztension;121 122 // NOTE target of %t must be int length123 // XXX how do I specify this??124 // have_zsimple = gfits_scan_alt (header, "ZSIMPLE", "%t", 1, &zsimple);125 // have_ztension = gfits_scan (header, "ZTENSION", "%s", 1, exttype);126 127 // this is a very bogus case: we cannot have both keywords128 if (have_zsimple && have_ztension) ESCAPE;129 130 // if neither are present, we have an image that is not really following the standard:131 // assume it is a PHU132 if (!have_zsimple && !have_ztension) {133 header->simple = TRUE;134 gfits_extended_to_primary (header, header->simple, "Image data");135 136 MOD_KEYWORD_ALT ("ZEXTEND", "EXTEND", "%t", &header->extend, header->extend);137 MOD_KEYWORD_ALT ("ZBLOCKED", "BLOCKED", "%t", &header->extend, header->extend);138 }139 140 // have_zsimple : image comes from a PHU141 if (have_zsimple) {142 header->simple = zsimple;143 gfits_delete (header, "ZSIMPLE", 1);144 gfits_extended_to_primary (header, header->simple, "Image data");145 146 MOD_KEYWORD_ALT ("ZEXTEND", "EXTEND", "%t", &header->extend, header->extend);147 MOD_KEYWORD_ALT ("ZBLOCKED", "BLOCKED", "%t", &header->extend, header->extend);148 }149 150 // have_ztension : image comes from an extension151 if (have_ztension) {152 gfits_delete (header, "ZTENSION", 1);153 gfits_modify_extended (header, exttype, "Image extension");154 155 // we may have an uncompressed PCOUNT / GCOUNT value, otherwise set to 0,1156 if (gfits_scan (header, "ZPCOUNT", "%d", 1, &header->pcount)) {157 gfits_delete (header, "ZPCOUNT", 1);158 gfits_modify (header, "PCOUNT", "%d", 1, header->pcount);159 } else {160 header->pcount = 0;161 gfits_modify (header, "PCOUNT", "%d", 1, header->pcount);162 }163 if (gfits_scan (header, "ZGCOUNT", "%d", 1, &header->gcount)) {164 gfits_delete (header, "ZGCOUNT", 1);165 gfits_modify (header, "GCOUNT", "%d", 1, header->gcount);166 } else {167 header->pcount = 1;168 gfits_modify (header, "GCOUNT", "%d", 1, header->gcount);169 }170 } else {171 header->pcount = 0;172 header->gcount = 1;173 gfits_modify (header, "PCOUNT", "%d", 1, header->pcount);174 gfits_modify (header, "GCOUNT", "%d", 1, header->gcount);175 }176 177 MOD_KEYWORD ("ZHECKSUM", "CHECKSUM", "%s", checksum, checksum);178 MOD_KEYWORD ("ZDATASUM", "DATASUM", "%s", datasum, datasum);179 180 zscale = 1;181 gfits_scan (header, "ZSCALE", "%f", 1, &zscale);182 183 zblank = 32767;184 gfits_scan (header, "ZBLANK", "%d", 1, &zblank);185 186 oblank = 32767;187 gfits_scan (header, "BLANK", "%d", 1, &oblank);188 189 zzero = 0;190 gfits_scan (header, "ZZERO", "%f", 1, &zzero);191 127 # endif 192 128 193 // XXX how to define zdef?194 129 VarLengthColumn zdef; 195 if (!gfits_varlength_column_define (ftable, &zdef, zcol)) ESCAPE; 196 197 max_tile_size = gfits_imtile_maxsize (matrix, ztile); 198 199 // size of an element in the vartable heap section 200 zdata_pixsize = gfits_vartable_heap_pixsize (zdef.format); 201 202 // size of a pixel in the final image (not needed) 203 // raw_pixsize = gfits_uncompressed_data_pixsize (cmptype, header[0].bitpix, optname, optvalue, Noptions) 130 if (!gfits_varlength_column_define (ftable, &zdef, 1)) ESCAPE(A); 131 132 // allocate the working buffers to the max tile size 133 int max_tile_size = gfits_imtile_maxsize (matrix, ztile); 204 134 205 135 // size of a pixel in the raw image tile: 206 raw_pixsize = abs(header[0].bitpix) / 8; 136 int raw_pixsize = abs(header[0].bitpix) / 8; 137 138 // size of a pixel in the raw image tile: 139 int tile_pixsize = gfits_uncompressed_data_pixsize (zcmptype, header[0].bitpix, NULL, NULL, 0); 207 140 208 141 ALLOCATE (raw, char, raw_pixsize*max_tile_size); 209 ALLOCATE (zdata, char, raw_pixsize*max_tile_size); //142 ALLOCATE (zdata, char, raw_pixsize*max_tile_size); 210 143 211 144 // init the otile[] counters 212 gfits_imfile_start (matrix, otile);145 if (!gfits_imtile_start (matrix, otile)) ESCAPE (A); 213 146 214 147 // compress the data : copy into a tile, compress the tile, then add to the output table … … 217 150 218 151 // size of the current tile 219 Nraw = gfits_tile_size (matrix, otile, ztile);152 int Nraw = gfits_tile_size (matrix, otile, ztile); 220 153 221 154 // copy the raw pixels from their native matrix locations to the temporary output buffer 222 if (!gfits_collect_data (matrix, raw, Nraw, raw_pixsize, otile, oblank, ztile, zblank, zscale, zzero)) return (FALSE);155 if (!gfits_collect_data (matrix, raw, Nraw, raw_pixsize, otile, oblank, ztile, zblank, zscale, zzero)) ESCAPE(A); 223 156 224 157 // XXX the tile must not be > 2GB 225 226 Nzdata = raw_pixsize*max_tile_size; // available space, replaced with actual output size on compression 227 if (!gfits_compress_data (zdata, &Nzdata, zcmptype, optname, optvalue, Noptions, raw, Nraw, raw_pixsize)) return (FALSE); 228 229 if (!gfits_varlength_column_add_data (ftable, zdata, Nzdata, i, &zdef)) return FALSE; 158 159 // optname, optvalue = NULL, Noptions = 0 160 161 int Nzdata = raw_pixsize*max_tile_size; // available space, replaced with actual output size on compression 162 if (!gfits_compress_data (zdata, &Nzdata, zcmptype, NULL, NULL, 0, raw, Nraw, tile_pixsize)) ESCAPE(A); 163 164 if (!gfits_varlength_column_add_data (ftable, zdata, Nzdata, i, &zdef)) ESCAPE(A); 230 165 231 166 // XXX need to activate 232 if (FALSE) gfits_byteswap_zdata (zdata, Nzdata, zdata_pixsize) return (FALSE);167 // if (FALSE) gfits_byteswap_zdata (zdata, Nzdata, zdata_pixsize) ESCAPE(A); 233 168 234 169 // update the otile[] counters, carrying to the next dimension if needed 235 gfits_imfile_next (matrix, ztile, ntile, otile); 236 } 170 gfits_imtile_next (matrix, ztile, ntile, otile); 171 } 172 173 if (!gfits_varlength_column_finish (ftable, &zdef)) ESCAPE(A); 174 175 free (raw); 176 free (zdata); 177 178 free (otile); 179 free (ntile); 180 181 if (!Ztile) free (ztile); 237 182 return (TRUE); 183 184 escape: 185 FREE (raw); 186 FREE (zdata); 187 188 FREE (otile); 189 FREE (ntile); 190 191 if (!Ztile) FREE (ztile); 192 return FALSE; 238 193 } 239 194 240 // this takes place in three major steps: 241 // 1) rawdata -> tiledata (copy a tile chunk to the tiledata buffer) 242 // 2) compress tiledata -> zdata 243 // 3) add zdata to table data heap 244 245 // heapdata.pixsize : zdef.format 246 // zdata.pixsize : depends on compression 247 // odata.pixsize : depends on compression 248 // idata.pixsize : header.bitpix 249 195 int gfits_copy_keywords_compress (Header *srchead, Header *tgthead) { 196 197 int i; 198 199 for (i = 0; i < srchead->datasize; i += FT_LINE_LENGTH) { 200 } 201 return TRUE; 202 } 250 203 251 204 // raw_pixsize is the bytes / pixel for the input matrix … … 298 251 } } } 299 252 253 // we need to set up switches for all of the possible combinations: 254 // this macro is used at the inner switch to run the actual loop 255 // note we copy Optr to Iptr 256 # define SCALE_AND_DIST_FLOAT(TYPE, SIZE) { \ 257 TYPE *Optr = (TYPE *) &matrix->buffer[SIZE*(offset + start)]; \ 258 for (j = 0; j < Ztile[0]; j++, Iptr++, Optr++) { \ 259 if (!isfinite(*Optr)) { \ 260 *Iptr = zblank; \ 261 } else { \ 262 *Iptr = (*Optr - zzero) / zscale; \ 263 } } } 264 300 265 // this macro sets up the outer switch and calls above macro with all output bitpix options 301 266 # define SETUP_INSIZE(TYPE, SIZE) { \ … … 312 277 break; \ 313 278 case -32: \ 314 SCALE_AND_DIST (float, 4); \279 SCALE_AND_DIST_FLOAT (float, 4); \ 315 280 break; \ 316 281 case -64: \ 317 SCALE_AND_DIST (double, 8); \282 SCALE_AND_DIST_FLOAT (double, 8); \ 318 283 break; \ 319 284 default: \ -
branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_data.c
r38322 r38329 3 3 # include <zlib.h> 4 4 5 int gfits_compress_data (char *zdata, int *Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *rawdata, int Nraw, int raw_pixsize) { 5 /* functions defined in ricecomp.c */ 6 int fits_rcomp(int a[], int nx, unsigned char *c, int clen, int nblock); 7 int fits_rdecomp (unsigned char *c, int clen, unsigned int array[], int nx, int nblock); 8 int fits_rdecomp_short (unsigned char *c, int clen, unsigned short array[], int nx, int nblock); 9 int fits_rdecomp_byte (unsigned char *c, int clen, unsigned char array[], int nx, int nblock); 10 11 /* functions defined in fits_hcompress.c */ 12 # define LONGLONG long long 13 int fits_hcompress(int *a, int ny, int nx, int scale, char *output, long *nbytes, int *status); 14 int fits_hcompress64(LONGLONG *a, int ny, int nx, int scale, char *output, long *nbytes, int *status); 15 16 /* functions defined in fits_hdeccompress.c */ 17 int fits_hdecompress(unsigned char *input, int smooth, int *a, int *ny, int *nx, int *scale, int *status); 18 int fits_hdecompress64(unsigned char *input, int smooth, LONGLONG *a, int *ny, int *nx, int *scale, int *status); 19 20 /* functions defined in pliocomp.c */ 21 int pl_p2li (int *pxsrc, int xs, short *lldst, int npix); 22 int pl_l2pi (short *ll_src, int xs, int *px_dst, int npix); 23 24 /* functions defined in gzip.c */ 25 int gfits_gz_stripheader (unsigned char *data, int *ndata); 26 int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen); 27 int gfits_compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen); 28 29 static int pass = 0; 30 31 int gfits_compress_data (char *zdata, int *Nzdata, char *cmptype, 32 char **optname, char **optvalue, int Nopt, 33 char *rawdata, int Nrawpix, int rawpix_size) { 6 34 7 35 int status; 8 static int Ninsum = 0;9 static int Noutsum = 0;10 36 11 37 if (!strcasecmp(cmptype, "GZIP_1")) { 12 unsigned long tNraw = Nraw * out_pixsize;38 unsigned long Nbytes = Nrawpix * rawpix_size; 13 39 14 40 // the data must be byteswapped before compression begins 15 if (!gfits_byteswap_zdata (rawdata, Nraw, raw_pixsize)) return (FALSE);41 // if (!gfits_byteswap_zdata (rawdata, Nrawpix, rawpix_size)) return (FALSE); 16 42 17 43 // Nzdata is the size of the allocated buffer before the function is called, returns the actual size 18 // tNraw is the number of bytes in the raw data buffer 19 status = gfits_compress ((Bytef *) zdata, &Nzdata, (Bytef *) rawdata, tNraw); 44 // Nbytes is the number of bytes in the raw data buffer 45 uLongf destLen = *Nzdata; 46 status = gfits_compress ((Bytef *) zdata, &destLen, (Bytef *) rawdata, Nbytes); 47 *Nzdata = destLen; 20 48 if (status != Z_OK) { 21 49 fprintf (stderr, "error in compress (GZIP)\n"); … … 23 51 } 24 52 53 // test of deflate (vs I/O) 54 { 55 char *srcbuf, *tgtbuf; 56 ALLOCATE (srcbuf, char, destLen); 57 memcpy (srcbuf, zdata, destLen); 58 59 ALLOCATE (tgtbuf, char, Nbytes); 60 61 uLongf Ndeflate = Nbytes; 62 status = gfits_uncompress ((Bytef *) tgtbuf, &Ndeflate, (Bytef *) zdata, destLen); 63 if (status != Z_OK) { 64 fprintf (stderr, "error in uncompress (GZIP) : %d vs %d\n", status, Z_OK); 65 return (FALSE); 66 } 67 68 myAssert (Ndeflate == Nbytes, "invalid output size?"); 69 70 int i; 71 for (i = 0; i < Ndeflate; i++) { 72 myAssert (rawdata[i] == tgtbuf[i], "data mismatch?"); 73 } 74 75 } 76 77 if (pass == 0) { 78 int i; 79 for (i = 0; i < 32; i++) { 80 fprintf (stderr, "0x%02hhx ", zdata[i]); 81 } 82 fprintf (stderr, "\n"); 83 pass = 1; 84 } 25 85 return (TRUE); 26 86 } -
branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_utils.c
r38326 r38329 49 49 otile[i] = 0; 50 50 } 51 return TRUE; 51 52 } 52 53 -
branches/eam_branches/ohana.20150429/src/libfits/matrix/F_matrix.c
r35164 r38329 22 22 size = FT_RECORD_SIZE * Nrec; 23 23 } 24 25 return (size); 26 } 27 28 off_t gfits_heap_start (Header *header) { 29 30 int i; 31 off_t size; 32 33 if (header[0].Naxes == 0) return (0); 34 35 size = abs(header[0].bitpix / 8); 36 37 for (i = 0; i < header[0].Naxes; i++) 38 size *= header[0].Naxis[i]; 24 39 25 40 return (size); -
branches/eam_branches/ohana.20150429/src/libfits/matrix/F_uncompress_M.c
r38322 r38329 4 4 5 5 # define ESCAPE { \ 6 fprintf (stderr, "error in %s @ line %d\n", __func__, __LINE__); \ 6 7 if (ztile != NULL) free (ztile); \ 7 8 if (optname != NULL) { \ -
branches/eam_branches/ohana.20150429/src/libfits/matrix/F_uncompress_data.c
r23815 r38329 26 26 int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen); 27 27 28 static int pass = 0; 29 28 30 int gfits_uncompress_data (char *zdata, int Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *outdata, int *Nout, int out_pixsize) { 29 31 … … 36 38 37 39 // for GZIP data, I need to check for and remove the header on the first block: 38 gfits_gz_stripheader ((unsigned char *)zdata, &Nzdata); 40 // XXX maybe not anymore 41 // gfits_gz_stripheader ((unsigned char *)zdata, &Nzdata); 42 43 if (pass == 0) { 44 int i; 45 for (i = 0; i < 32; i++) { 46 fprintf (stderr, "0x%02hhx ", zdata[i]); 47 } 48 fprintf (stderr, "\n"); 49 pass = 1; 50 } 39 51 40 52 // uncompress does not require us to know the expected number of pixel; it tells us the number 53 // XXX shouldn't we validate the result : we think we know the size 41 54 status = gfits_uncompress ((Bytef *) outdata, &tNout, (Bytef *) zdata, Nzdata); 42 55 if (status != Z_OK) { … … 47 60 48 61 // the resulting uncompressed data is byteswapped 49 if (!gfits_byteswap_zdata (outdata, *Nout, out_pixsize)) return (FALSE);62 // if (!gfits_byteswap_zdata (outdata, *Nout, out_pixsize)) return (FALSE); 50 63 51 64 return (TRUE); -
branches/eam_branches/ohana.20150429/src/libfits/table/F_copy_T.c
r34088 r38329 6 6 7 7 /* find buffer size */ 8 out[0].validsize = in[0].validsize; 9 out[0].datasize = in[0].datasize; 8 out[0].validsize = in[0].validsize; 9 out[0].datasize = in[0].datasize; 10 out[0].heap_start = in[0].heap_start; 10 11 11 12 ALLOCATE (out[0].buffer, char, out[0].datasize); … … 28 29 29 30 /* find buffer size */ 30 out[0].datasize = in[0].datasize; 31 out[0].pad = in[0].pad; 32 out[0].Nrow = in[0].Nrow; 31 out[0].datasize = in[0].datasize; 32 // out[0].heap_start = in[0].heap_start; 33 out[0].pad = in[0].pad; 34 out[0].Nrow = in[0].Nrow; 33 35 34 36 off_t Nrows = out[0].Nrow; -
branches/eam_branches/ohana.20150429/src/libfits/table/F_create_T.c
r35756 r38329 4 4 /*********************** fits init table *******************************/ 5 5 int gfits_init_table (FTable *table) { 6 table[0].header = NULL; 7 table[0].buffer = NULL; 8 table[0].datasize = 0; 9 table[0].validsize = 0; 6 table[0].header = NULL; 7 table[0].buffer = NULL; 8 table[0].datasize = 0; 9 table[0].heap_start = 0; 10 table[0].validsize = 0; 10 11 return (TRUE); 11 12 … … 42 43 } 43 44 table[0].datasize = Nbytes; 45 table[0].heap_start = gfits_heap_start (header); 44 46 return (TRUE); 45 47 48 } 49 50 /*********************** fits create table *******************************/ 51 int gfits_set_table_rows (Header *header, FTable *table, off_t Nrows) { 52 53 header[0].Naxis[1] = Nrows; 54 gfits_modify (header, "NAXIS2", OFF_T_FMT, 1, Nrows); 55 56 off_t Nbytes = gfits_data_size (header); 57 REALLOCATE (table[0].buffer, char, Nbytes); 58 bzero (table[0].buffer, Nbytes); 59 table[0].datasize = Nbytes; 60 table[0].heap_start = gfits_heap_start (header); 61 return (TRUE); 46 62 } 47 63 … … 60 76 Ny = table[0].header[0].Naxis[0]; 61 77 table[0].datasize = gfits_data_size (header); 78 // table[0].heap_start = gfits_heap_start (header); 62 79 table[0].pad = table[0].datasize - Nx*Ny; 63 80 -
branches/eam_branches/ohana.20150429/src/libfits/table/F_define_column.c
r35756 r38329 66 66 sprintf (field, "TTYPE%d", Nfields); 67 67 gfits_modify (header, field, "%s", 1, label); 68 gfits_modify_alt (header, field, "%C", 1, comment); 69 sprintf (field, "TUNIT%d", Nfields); 70 gfits_modify (header, field, "%s", 1, unit); 68 if (comment) { 69 gfits_modify_alt (header, field, "%C", 1, comment); 70 } 71 if (unit) { 72 sprintf (field, "TUNIT%d", Nfields); 73 gfits_modify (header, field, "%s", 1, unit); 74 } 71 75 sprintf (field, "TFORM%d", Nfields); 72 76 gfits_modify (header, field, "%s", 1, format); -
branches/eam_branches/ohana.20150429/src/libfits/table/F_read_T.c
r37807 r38329 45 45 if (Nread < gfits_data_min_size (table[0].header)) { 46 46 fprintf (stderr, "error: fits read error in %s, read "OFF_T_FMT", need "OFF_T_FMT"\n", __func__, Nread, gfits_data_min_size (table[0].header)); 47 if (padIfShort) { 48 memset (&table[0].buffer[Nread], 0, Nbytes - Nread); 49 fprintf (stderr, "warning: file missing data, padding with zeros: USE AT YOUR OWN RISK!\n"); 50 table[0].validsize = Nread; 51 table[0].datasize = Nbytes; 52 return (TRUE); 53 } else { 54 gfits_free_table (table); 47 if (!padIfShort) { 48 gfits_free_table (table); 55 49 return (FALSE); 56 50 } 57 } 58 fprintf (stderr, "warning: file missing pad\n"); 51 memset (&table[0].buffer[Nread], 0, Nbytes - Nread); 52 fprintf (stderr, "warning: file missing data, padding with zeros: USE AT YOUR OWN RISK!\n"); 53 } else { 54 fprintf (stderr, "warning: file missing pad\n"); 55 } 59 56 } 60 57 table[0].validsize = Nread; 61 58 table[0].datasize = Nbytes; 59 table[0].heap_start = gfits_heap_start (table->header); 62 60 return (TRUE); 63 61 } … … 93 91 } 94 92 fprintf (stderr, "error: fits read error in %s, read "OFF_T_FMT", need "OFF_T_FMT"\n", __func__, Nread, Nbytes); 95 if (padIfShort) { 96 memset (&table[0].buffer[Nread], 0, Nbytes - Nread); 97 fprintf (stderr, "warning: file missing data, padding with zeros: USE AT YOUR OWN RISK!\n"); 98 table[0].validsize = Nread; 99 table[0].datasize = Nbytes; 100 } else { 93 if (!padIfShort) { 101 94 free (table[0].buffer); 102 95 return (FALSE); 103 96 } 97 memset (&table[0].buffer[Nread], 0, Nbytes - Nread); 98 fprintf (stderr, "warning: file missing data, padding with zeros: USE AT YOUR OWN RISK!\n"); 104 99 } 105 100 … … 107 102 table[0].header[0].Naxis[1] = Nrows; 108 103 gfits_modify (table[0].header, "NAXIS2", OFF_T_FMT, 1, Nrows); 109 table[0].datasize = gfits_data_size (table[0].header); 104 table[0].validsize = Nread; 105 table[0].datasize = Nbytes; 106 table[0].heap_start = gfits_heap_start (table->header); 110 107 111 108 return (TRUE); … … 122 119 Ny = table[0].header[0].Naxis[1]; 123 120 table[0].datasize = gfits_data_size (table[0].header); 121 // table[0].heap_start = gfits_heap_start (table[0].header); 124 122 table[0].pad = table[0].datasize - Nx*Ny; 125 123 … … 206 204 for (i = 0; i < Nrow; i++) table[0].row[i] = row[i]; 207 205 table[0].datasize = gfits_data_size (table[0].header); 206 // table[0].heap_start = gfits_heap_start (table[0].header); 208 207 table[0].pad = table[0].datasize - Nx*Ny; 209 208 return (TRUE); -
branches/eam_branches/ohana.20150429/src/libfits/table/F_set_column.c
r38325 r38329 18 18 int gfits_set_bintable_column (Header *header, FTable *table, char *label, void *data, off_t Nrow) { 19 19 20 off_t Nx, Ny , nbytes;20 off_t Nx, Ny; 21 21 int i, N, Nfields; 22 22 int Nval, Nbytes, Nstart, Nv, Nb; … … 55 55 gfits_scan (header, "NAXIS1", OFF_T_FMT, 1, &Nx); 56 56 gfits_scan (header, "NAXIS2", OFF_T_FMT, 1, &Ny); 57 58 // if no rows have yet been assigned, we need to allocate the full data buffer 57 59 if (Ny == 0) { 58 60 Ny = Nrow; 59 header[0].Naxis[1] = Ny; 60 gfits_modify (header, "NAXIS2", OFF_T_FMT, 1, Ny); 61 62 nbytes = gfits_data_size (header); 63 REALLOCATE (table[0].buffer, char, MAX (nbytes, 1)); 64 bzero (table[0].buffer, nbytes); 65 table[0].datasize = nbytes; 61 gfits_set_table_rows (header, table, Ny); 66 62 } 67 63 if (Ny != Nrow) return (FALSE); … … 181 177 int gfits_set_bintable_column_reformat (Header *header, FTable *table, char *label, char *intype, void *data, off_t Nrow) { 182 178 183 off_t Nx, Ny , nbytes;179 off_t Nx, Ny; 184 180 int i, N, Nfields; 185 181 int Nval, NbytesOut, Nstart, Nv, Nb; … … 220 216 if (Ny == 0) { 221 217 Ny = Nrow; 222 header[0].Naxis[1] = Ny; 223 gfits_modify (header, "NAXIS2", OFF_T_FMT, 1, Ny); 224 225 nbytes = gfits_data_size (header); 226 REALLOCATE (table[0].buffer, char, MAX (nbytes, 1)); 227 bzero (table[0].buffer, nbytes); 228 table[0].datasize = nbytes; 218 gfits_set_table_rows (header, table, Ny); 229 219 } 230 220 if (Ny != Nrow) return (FALSE); … … 701 691 int gfits_set_table_column (Header *header, FTable *table, char *label, void *data, off_t Nrow) { 702 692 703 off_t Nx, Ny , nbytes;693 off_t Nx, Ny; 704 694 int i, N, Nfields, Nval, Nbytes, Nstart, Nv, Nb; 705 695 char tlabel[80], field[80], format[80], cformat[80], type[16], tmp[16]; … … 731 721 if (Ny == 0) { 732 722 Ny = Nrow; 733 header[0].Naxis[1] = Ny; 734 gfits_modify (header, "NAXIS2", OFF_T_FMT, 1, Ny); 735 736 nbytes = gfits_data_size (header); 737 REALLOCATE (table[0].buffer, char, MAX (nbytes, 1)); 738 bzero (table[0].buffer, nbytes); 739 table[0].datasize = nbytes; 723 gfits_set_table_rows (header, table, Ny); 740 724 } 741 725 if (Ny != Nrow) return (FALSE); -
branches/eam_branches/ohana.20150429/src/libfits/table/F_table_format.c
r38326 r38329 8 8 int gfits_bintable_format (char *format, char *type, int *Nval, int *Nbytes) { 9 9 10 char *Lchar, *Fchar; 11 int Nv; 12 13 if (format == (char *) NULL) return (FALSE); 14 if (format[0] == 0) return (FALSE); 15 Lchar = &format[strlen(format) - 1]; 16 17 Nv = strtol (format, &Fchar, 10); 18 if (Fchar != Lchar) return (FALSE); 10 char *Fchar; 11 12 if (format == (char *) NULL) return (FALSE); // format must be defined 13 if (format[0] == 0) return (FALSE); // format must not be empty string 14 // char *Lchar = &format[strlen(format) - 1]; // last-char pointer 15 16 int Nv = strtol (format, &Fchar, 10); // Fchar points at end of leading number 17 // if (Fchar != Lchar) return (FALSE); -- this test is invalid for eg 1PB(nn) 19 18 if ((Nv == 0) && (Fchar == format)) Nv = 1; 20 19 … … 51 50 { *Nbytes = 8; strcpy (type, "double"); *Nval = Nv; } 52 51 break; 52 case 'C': 53 { *Nbytes = 8; strcpy (type, "float"); *Nval = 2*Nv; } 54 break; 55 case 'M': 56 { *Nbytes = 16; strcpy (type, "double"); *Nval = 2*Nv; } 57 break; 53 58 case 'P': 54 59 { *Nbytes = 4; strcpy (type, "var"); *Nval = 2*Nv; } … … 56 61 case 'Q': 57 62 { *Nbytes = 8; strcpy (type, "var64"); *Nval = 2*Nv; } 58 break;59 case 'C':60 { *Nbytes = 8; strcpy (type, "float"); *Nval = 2*Nv; }61 break;62 case 'M':63 { *Nbytes = 16; strcpy (type, "double"); *Nval = 2*Nv; }64 63 break; 65 64 default: -
branches/eam_branches/ohana.20150429/src/libfits/table/F_table_row.c
r28241 r38329 28 28 REALLOCATE (table[0].buffer, char, MAX (nbytes, 1)); 29 29 table[0].datasize = nbytes; 30 table[0].heap_start = gfits_heap_start (header); 30 31 31 32 memcpy (&table[0].buffer[Nstart], data, Nbytes*Nrow); … … 67 68 68 69 table[0].datasize = gfits_data_size (table[0].header); 70 // table[0].heap_start = gfits_heap_start (header); 69 71 table[0].pad = table[0].datasize - Nx*Ny; 70 72 … … 102 104 REALLOCATE (table[0].buffer, char, MAX (nbytes, 1)); 103 105 table[0].datasize = nbytes; 106 table[0].heap_start = gfits_heap_start (header); 104 107 return (TRUE); 105 108 } -
branches/eam_branches/ohana.20150429/src/libfits/table/F_table_varlength.c
r38326 r38329 15 15 snprintf (field, 80, "TFORM%d", column); 16 16 if (!gfits_scan (ftable->header, field, "%s", 1, format)) return (FALSE); 17 18 def->column = column; 17 19 18 20 // find and remove the max field length element … … 49 51 if (*p1 == 'I') { def->nbytes = 2; def->format = *p1; } 50 52 if (*p1 == 'J') { def->nbytes = 4; def->format = *p1; } 53 if (*p1 == 'K') { def->nbytes = 8; def->format = *p1; } 51 54 if (*p1 == 'E') { def->nbytes = 4; def->format = *p1; } 52 55 if (*p1 == 'D') { def->nbytes = 8; def->format = *p1; } … … 55 58 if (!def->nbytes) { return (FALSE); } 56 59 57 /* scan columns to find column offset */58 def-> Nstart = 0;60 /* scan columns to find column offset for metadata column*/ 61 def->offset = 0; 59 62 for (i = 1; i < column; i++) { 60 63 sprintf (field, "TFORM%d", i); 61 64 gfits_scan (ftable->header, field, "%s", 1, format); 62 65 gfits_bintable_format (format, tmpline, &Nv, &Nb); 63 def-> Nstart += Nv*Nb;66 def->offset += Nv*Nb; 64 67 } 65 68 66 69 // heap_start must be long long so file may be very large 67 if (!gfits_scan (ftable->header, "THEAP", OFF_T_FMT, 1, &def->heap_start)) { 68 def->heap_start = ftable->header->Naxis[0]*ftable->header->Naxis[1]; 69 } 70 70 // confirm that ftable->heap_start is correctly set? 71 72 return TRUE; 73 } 74 75 # define SWAP_WORD \ 76 tmp = pchar[0]; pchar[0] = pchar[3]; pchar[3] = tmp; \ 77 tmp = pchar[1]; pchar[1] = pchar[2]; pchar[2] = tmp; 78 79 # define SWAP_DBLE \ 80 tmp = pchar[0]; pchar[0] = pchar[7]; pchar[7] = tmp; \ 81 tmp = pchar[1]; pchar[1] = pchar[6]; pchar[6] = tmp; \ 82 tmp = pchar[2]; pchar[2] = pchar[5]; pchar[5] = tmp; \ 83 tmp = pchar[3]; pchar[3] = pchar[4]; pchar[4] = tmp; 84 85 int gfits_byteswap_varlength_column (FTable *ftable, int column) { 86 87 # ifdef BYTE_SWAP 88 VarLengthColumn zdef; 89 if (!gfits_varlength_column_define (ftable, &zdef, column)) return FALSE; 90 91 char *bufstart = &ftable->buffer[zdef.offset]; 92 93 int i; 94 char *pchar, tmp; 95 96 off_t Nx = ftable->header->Naxis[0]; 97 98 for (i = 0; i < ftable->header->Naxis[1]; i++) { 99 if (zdef.mode == 'P') { 100 pchar = &bufstart[i*Nx]; 101 SWAP_WORD; 102 pchar = &bufstart[i*Nx + 4]; 103 SWAP_WORD; 104 } 105 if (zdef.mode == 'Q') { 106 char *pchar, tmp; 107 pchar = &bufstart[i*Nx]; 108 SWAP_DBLE; 109 pchar = &bufstart[i*Nx + 8]; 110 SWAP_DBLE; 111 } 112 } 113 # endif 71 114 return TRUE; 72 115 } … … 85 128 Nx = ftable->header->Naxis[0]; 86 129 87 # define SWAP_WORD \88 tmp = pchar[0]; pchar[0] = pchar[3]; pchar[3] = tmp; \89 tmp = pchar[1]; pchar[1] = pchar[2]; pchar[2] = tmp;90 91 130 # ifdef BYTE_SWAP 92 131 char *pchar, tmp; 93 pchar = &ftable->buffer[row*Nx + column-> Nstart];132 pchar = &ftable->buffer[row*Nx + column->offset]; 94 133 SWAP_WORD; 95 pchar = &ftable->buffer[row*Nx + column-> Nstart + 4];134 pchar = &ftable->buffer[row*Nx + column->offset + 4]; 96 135 SWAP_WORD; 97 136 # endif … … 99 138 if (column->mode == 'P') { 100 139 int *ptr; 101 ptr = (int *) &ftable->buffer[row*Nx + column-> Nstart];140 ptr = (int *) &ftable->buffer[row*Nx + column->offset]; 102 141 *length = ptr[0]; 103 142 offset = ptr[1]; … … 105 144 if (column->mode == 'Q') { 106 145 off_t *ptr; 107 ptr = (off_t *) &ftable->buffer[row*Nx + column-> Nstart];146 ptr = (off_t *) &ftable->buffer[row*Nx + column->offset]; 108 147 *length = ptr[0]; 109 148 offset = ptr[1]; 110 149 } 111 150 112 result = (void *) (ftable->buffer + column->heap_start + offset);151 result = (void *) (ftable->buffer + ftable->heap_start + offset); 113 152 return result; 114 153 } … … 121 160 // swap before writing out 122 161 123 // Here are operations we need to do:124 125 162 int gfits_varlength_column_add_data (FTable *table, char *data, off_t Ndata, int row, VarLengthColumn *column) { 126 163 127 164 // find the current starting point for new data (end of current buffer main data + current heap 128 165 off_t heap_offset = gfits_data_min_size (table->header); 129 char *heap_ptr = ftable->buffer + heap_offset;130 166 131 167 // extend the buffer size to add the new data … … 133 169 REALLOCATE (table->buffer, char, Nbytes); 134 170 171 char *heap_ptr = table->buffer + heap_offset; 172 135 173 // add the new data to the table buffer 136 174 memcpy (heap_ptr, data, Ndata); … … 142 180 // find the cell for the specified row & column 143 181 // the values in the main table for this row and varlength column: 144 Nx = ftable->header->Naxis[0];182 int Nx = table->header->Naxis[0]; 145 183 146 184 // set the cell values 147 185 if (column->mode == 'P') { 148 186 int *ptr; 149 ptr = (int *) & ftable->buffer[row*Nx + column->Nstart];187 ptr = (int *) &table->buffer[row*Nx + column->offset]; 150 188 ptr[0] = Ndata; 151 ptr[1] = heap_offset ;189 ptr[1] = heap_offset - table->heap_start; 152 190 } 153 191 if (column->mode == 'Q') { 154 192 off_t *ptr; 155 ptr = (off_t *) & ftable->buffer[row*Nx + column->Nstart];193 ptr = (off_t *) &table->buffer[row*Nx + column->offset]; 156 194 ptr[0] = Ndata; 157 ptr[1] = heap_offset ;195 ptr[1] = heap_offset - table->heap_start; 158 196 } 159 197 160 198 // update maxlen 199 // fprintf (stderr, "row: %d, Ndata: %d, maxlen: %d\n", (int) row, (int) Ndata, (int) column->maxlen); 161 200 column->maxlen = MAX (column->maxlen, Ndata); 162 201 163 202 off_t pcount; 164 gfits_scan ( ftable->header, "PCOUNT", OFF_T_FMT, 1, &pcount);203 gfits_scan (table->header, "PCOUNT", OFF_T_FMT, 1, &pcount); 165 204 166 205 pcount += Ndata; 167 gfits_modify (header, "PCOUNT", OFF_T_FMT, 1, pcount); 168 169 return TRUE; 170 } 206 gfits_modify (table->header, "PCOUNT", OFF_T_FMT, 1, pcount); 207 table->header->pcount = pcount; 208 table->datasize = gfits_data_size (table->header); 209 210 return TRUE; 211 } 212 213 // update the header TFORM value for this entry 214 int gfits_varlength_column_finish (FTable *ftable, VarLengthColumn *def) { 215 216 char field[81]; 217 char format[81]; 218 219 // grab the value of TFORMn for this column 220 snprintf (field, 80, "TFORM%d", def->column); 221 if (!gfits_scan (ftable->header, field, "%s", 1, format)) return FALSE; 222 223 // find and update the max field length element 224 char *p1 = strchr (format, '('); 225 if (!p1) return (FALSE); // not a valid varlength column -- missing (e_max) 226 227 p1 ++; 228 *p1 = 0; 229 230 snprintf (p1, 80 - (p1 - format), "%d)", def->maxlen); 231 if (!gfits_modify (ftable->header, field, "%s", 1, format)) return FALSE; 232 233 return TRUE; 234 } -
branches/eam_branches/ohana.20150429/src/libfits/table/F_write_T.c
r37807 r38329 84 84 Nx = ftable[0].header[0].Naxis[0]; // final output table size on disk 85 85 ftable[0].datasize = gfits_data_size (ftable[0].header); 86 ftable[0].heap_start = gfits_heap_start (ftable[0].header); 86 87 87 88 Nskip = start * Nx;
Note:
See TracChangeset
for help on using the changeset viewer.
