IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 38329


Ignore:
Timestamp:
May 28, 2015, 8:27:00 PM (11 years ago)
Author:
eugene
Message:

adding code for table compress/uncompress; image compress works (but is not quite compatible with cfitsio for some reason and byteswap is broken)

Location:
branches/eam_branches/ohana.20150429/src/libfits
Files:
5 added
18 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ohana.20150429/src/libfits/Makefile

    r37807 r38329  
    3737
    3838MATRIX_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
    4950
    5051TABLE_OBJ = \
     
    5556$(TABL)/F_set_column.$(ARCH).o                  $(TABL)/F_get_column.$(ARCH).o   \
    5657$(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
    5860
    5961EXTERN_OBJ = \
  • branches/eam_branches/ohana.20150429/src/libfits/extern/gzip.c

    r38322 r38329  
    131131    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
    132132
    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;
    135136
    136     // MAX_WBITS = 15
    137     err = inflateInit2(&stream, -15);
     137    // err = inflateInit2(&stream, -15);
     138    err = inflateInit(&stream);
    138139    if (err != Z_OK) return err;
    139140
     
    156157  z_stream stream;
    157158  int err;
    158 
     159 
    159160  stream.next_in = (Bytef*)source;
    160161  stream.avail_in = (uInt)sourceLen;
     
    167168  if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
    168169
    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;
    171173
    172   err = deflateInit(&stream);
     174  // the '5' is the compression level: make this a user argument
     175  err = deflateInit(&stream, 5);
    173176  if (err != Z_OK) return err;
    174177
  • branches/eam_branches/ohana.20150429/src/libfits/include/gfitsio.h

    r38062 r38329  
    7373  off_t                   datasize; // size of the buffer (including block padding at the end)
    7474  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
    7576} FTable;
    7677
     
    8586} VTable;
    8687
    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.
     91typedef 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
    9498} VarLengthColumn;
     99
     100typedef 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;
    95116
    96117# ifndef PROTO
     
    133154off_t   gfits_data_min_size            PROTO((Header *header));
    134155off_t   gfits_data_pad_size            PROTO((off_t rawsize));
     156off_t   gfits_heap_start               PROTO((Header *header));
    135157int     gfits_extended_to_primary      PROTO((Header *header, int simple, char *comment));
    136158int     gfits_primary_to_extended      PROTO((Header *header, char *exttype, char *comment));
     
    166188void    gfits_set_matrix_value         PROTO((Matrix *matrix, off_t x, off_t y, double value));
    167189int     gfits_write_matrix             PROTO((char *filename, Matrix *matrix));
     190
    168191int     gfits_uncompress_image         PROTO((Header *header, Matrix *matrix, FTable *ftable));
    169192int     gfits_uncompress_data          PROTO((char *zdata, int Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *outdata, int *Nout, int out_pixsize));
    170193int     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
     195int     gfits_compress_data            PROTO((char *zdata, int *Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *rawdata, int Nraw, int raw_pixsize));
     196int     gfits_compress_image           PROTO((Header *header, Matrix *matrix, FTable *ftable, int *Ztile, char *zcmptype));
     197int     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));
     198int     gfits_copy_keywords_compress   PROTO((Header *srchead, Header *tgthead));
     199
     200off_t   gfits_tile_size                PROTO((Matrix *matrix, int *otile, int *ztile));
     201int     gfits_imtile_maxsize           PROTO((Matrix *matrix, int *ztile));
     202int     gfits_imtile_count             PROTO((Matrix *matrix, int *ztile, int *ntile));
     203int     gfits_imtile_start             PROTO((Matrix *matrix, int *otile));
     204int     gfits_imtile_next              PROTO((Matrix *matrix, int *ztile, int *ntile, int *otile));
     205
    171206int     gfits_byteswap_zdata           PROTO((char *zdata, int Nzdata, int bitpix));
    172207int     gfits_extension_is_compressed  PROTO((Header *header));
    173 off_t   gfits_tile_size                PROTO((Matrix *matrix, int *otile, int *ztile));
    174208int     gfits_uncompressed_data_pixsize PROTO((char *cmptype, int out_bitpix, char **optname, char **optvalue, int Noptions));
    175209int     gfits_vartable_heap_pixsize    PROTO((char format));
    176210
     211int     gfits_varlength_column_add_data PROTO((FTable *table, char *data, off_t Ndata, int row, VarLengthColumn *column));
     212void   *gfits_varlength_column_pointer  PROTO((FTable *ftable, VarLengthColumn *column, off_t row, off_t *length));
     213int     gfits_varlength_column_define   PROTO((FTable *ftable, VarLengthColumn *def, int column));
     214int     gfits_varlength_column_finish   PROTO((FTable *ftable, VarLengthColumn *def));
     215int     gfits_byteswap_varlength_column PROTO((FTable *ftable, int column));
     216
    177217/******************************* Table functions *************/
    178218
     
    182222char   *gfits_table_print              PROTO((FTable *ftable,...));
    183223int     gfits_add_rows                 PROTO((FTable *ftable, char *data, off_t Nrow, off_t Nbytes));
     224int     gfits_set_table_rows           PROTO((Header *header, FTable *table, off_t Nrows));
    184225int     gfits_bintable_format          PROTO((char *format, char *type, int *Nval, int *Nbytes));
    185226int     gfits_create_table             PROTO((Header *header, FTable *ftable));
     
    226267int     gfits_write_Theader            PROTO((char *filename, Header *header));
    227268
    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));
     269int     gfits_compress_table           PROTO((FTable *srctable, FTable *tgttable, int ztilelen, char *zcmptype));
    230270
    231271#endif /* FITSIO */
  • branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_M.c

    r38326 r38329  
    33# include <zlib.h>
    44
    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 
    245// the user needs to specify the various compression options:
    25 // ztile[i]
    26 // zcmptype
     6// ztile[i] (may be NULL)
     7// zcmptype 
    278
    289// steps to convert an image to a compressed table:
     
    3516// * update header keywords
    3617
    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
     22int 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;
    5635
    5736  // determine the number of tiles (from ztile[] and image size)
     
    6746
    6847  // 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);
    7049
    7150  // by using "P" format, we are limited to 32bit pointers (2GB heap)
    7251  // 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);
    7960 
    80   // this allocates the data array
     61  // this allocates the pointer data array for the full set of tiles (NOT the heap)
    8162  char *tmpbuffer = NULL;
    8263  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);
    8466
    8567  // 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  }
    9286
    9387  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
    106117
    107118  // search for algorithm-specific keywords. these are used to control compression options
     
    114125    if (!gfits_modify (header, key, "%s", 1, optvalue[i])) break;
    115126  }
    116 
    117 # if (0)
    118   // XXX get ZMASKCMP
    119   int zsimple, have_zsimple;
    120   int have_ztension;
    121 
    122   // NOTE target of %t must be int length
    123   // 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 keywords
    128   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 PHU
    132   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 PHU
    141   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 extension
    151   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,1
    156     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);
    191127# endif
    192128
    193   // XXX how to define zdef?
    194129  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);
    204134
    205135  // 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);
    207140
    208141  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);
    210143
    211144  // init the otile[] counters
    212   gfits_imfile_start (matrix, otile);
     145  if (!gfits_imtile_start (matrix, otile)) ESCAPE (A);
    213146
    214147  // compress the data : copy into a tile, compress the tile, then add to the output table
     
    217150
    218151    // size of the current tile
    219     Nraw = gfits_tile_size (matrix, otile, ztile);
     152    int Nraw = gfits_tile_size (matrix, otile, ztile);
    220153
    221154    // 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);
    223156
    224157    // 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);
    230165
    231166    // 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);
    233168
    234169    // 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);
    237182  return (TRUE);
     183
     184escape:
     185  FREE (raw);
     186  FREE (zdata);
     187
     188  FREE (otile);
     189  FREE (ntile);
     190
     191  if (!Ztile) FREE (ztile);
     192  return FALSE;
    238193}
    239194
    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 
     195int 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}
    250203
    251204// raw_pixsize is the bytes / pixel for the input matrix
     
    298251      } } }
    299252
     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
    300265  // this macro sets up the outer switch and calls above macro with all output bitpix options
    301266# define SETUP_INSIZE(TYPE, SIZE) {                     \
     
    312277        break;                                          \
    313278      case -32:                                         \
    314         SCALE_AND_DIST (float, 4);                      \
     279        SCALE_AND_DIST_FLOAT (float, 4);                        \
    315280        break;                                          \
    316281      case -64:                                         \
    317         SCALE_AND_DIST (double, 8);                     \
     282        SCALE_AND_DIST_FLOAT (double, 8);                       \
    318283        break;                                          \
    319284      default:                                          \
  • branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_data.c

    r38322 r38329  
    33# include <zlib.h>
    44
    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 */
     6int fits_rcomp(int a[], int nx, unsigned char *c, int clen, int nblock);
     7int fits_rdecomp (unsigned char *c, int clen, unsigned int array[], int nx, int nblock);
     8int fits_rdecomp_short (unsigned char *c, int clen, unsigned short array[], int nx, int nblock);
     9int 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
     13int fits_hcompress(int *a, int ny, int nx, int scale, char *output, long *nbytes, int *status);
     14int fits_hcompress64(LONGLONG *a, int ny, int nx, int scale, char *output, long *nbytes, int *status);
     15
     16/* functions defined in fits_hdeccompress.c */
     17int fits_hdecompress(unsigned char *input, int smooth, int *a, int *ny, int *nx, int *scale, int *status);
     18int fits_hdecompress64(unsigned char *input, int smooth, LONGLONG *a, int *ny, int *nx, int *scale, int *status);
     19
     20/* functions defined in pliocomp.c */
     21int pl_p2li (int *pxsrc, int xs, short *lldst, int npix);
     22int pl_l2pi (short *ll_src, int xs, int *px_dst, int npix);
     23
     24/* functions defined in gzip.c */
     25int gfits_gz_stripheader (unsigned char *data, int *ndata);
     26int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
     27int gfits_compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
     28
     29static int pass = 0;
     30
     31int gfits_compress_data (char *zdata, int *Nzdata, char *cmptype,
     32                         char **optname, char **optvalue, int Nopt,
     33                         char *rawdata, int Nrawpix, int rawpix_size) {
    634
    735  int status;
    8   static int Ninsum = 0;
    9   static int Noutsum = 0;
    1036
    1137  if (!strcasecmp(cmptype, "GZIP_1")) {
    12     unsigned long tNraw = Nraw * out_pixsize;
     38    unsigned long Nbytes = Nrawpix * rawpix_size;
    1339
    1440    // 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);
    1642
    1743    // 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;
    2048    if (status != Z_OK) {
    2149      fprintf (stderr, "error in compress (GZIP)\n");
     
    2351    }
    2452
     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    }
    2585    return (TRUE);
    2686  }
  • branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_utils.c

    r38326 r38329  
    4949    otile[i] = 0;
    5050  }
     51  return TRUE;
    5152}
    5253
  • branches/eam_branches/ohana.20150429/src/libfits/matrix/F_matrix.c

    r35164 r38329  
    2222    size = FT_RECORD_SIZE * Nrec;
    2323  }
     24
     25  return (size);
     26}
     27
     28off_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];
    2439
    2540  return (size);
  • branches/eam_branches/ohana.20150429/src/libfits/matrix/F_uncompress_M.c

    r38322 r38329  
    44
    55# define ESCAPE { \
     6  fprintf (stderr, "error in %s @ line %d\n", __func__, __LINE__); \
    67  if (ztile != NULL) free (ztile); \
    78  if (optname != NULL) { \
  • branches/eam_branches/ohana.20150429/src/libfits/matrix/F_uncompress_data.c

    r23815 r38329  
    2626int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
    2727
     28static int pass = 0;
     29
    2830int gfits_uncompress_data (char *zdata, int Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *outdata, int *Nout, int out_pixsize) {
    2931
     
    3638
    3739    // 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    }
    3951
    4052    // 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
    4154    status = gfits_uncompress ((Bytef *) outdata, &tNout, (Bytef *) zdata, Nzdata);
    4255    if (status != Z_OK) {
     
    4760
    4861    // 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);
    5063
    5164    return (TRUE);
  • branches/eam_branches/ohana.20150429/src/libfits/table/F_copy_T.c

    r34088 r38329  
    66
    77  /* 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;
    1011
    1112  ALLOCATE (out[0].buffer, char, out[0].datasize);
     
    2829
    2930  /* 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;
    3335
    3436  off_t Nrows = out[0].Nrow;
  • branches/eam_branches/ohana.20150429/src/libfits/table/F_create_T.c

    r35756 r38329  
    44/*********************** fits init table *******************************/
    55int 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;
    1011  return (TRUE);
    1112
     
    4243  }
    4344  table[0].datasize = Nbytes;
     45  table[0].heap_start = gfits_heap_start (header);
    4446  return (TRUE);
    4547
     48}
     49
     50/*********************** fits create table *******************************/
     51int 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);
    4662}
    4763
     
    6076  Ny = table[0].header[0].Naxis[0];
    6177  table[0].datasize = gfits_data_size (header);
     78  // table[0].heap_start = gfits_heap_start (header);
    6279  table[0].pad = table[0].datasize - Nx*Ny;
    6380 
  • branches/eam_branches/ohana.20150429/src/libfits/table/F_define_column.c

    r35756 r38329  
    6666  sprintf (field, "TTYPE%d", Nfields);
    6767  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  }
    7175  sprintf (field, "TFORM%d", Nfields);
    7276  gfits_modify (header, field, "%s", 1, format);
  • branches/eam_branches/ohana.20150429/src/libfits/table/F_read_T.c

    r37807 r38329  
    4545    if (Nread < gfits_data_min_size (table[0].header)) {
    4646      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);
    5549        return (FALSE);
    5650      }
    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    }
    5956  }
    6057  table[0].validsize = Nread;
    6158  table[0].datasize = Nbytes;
     59  table[0].heap_start = gfits_heap_start (table->header);
    6260  return (TRUE);
    6361}       
     
    9391    }
    9492    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) {
    10194      free (table[0].buffer);
    10295      return (FALSE);
    10396    }
     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");
    10499  }
    105100
     
    107102  table[0].header[0].Naxis[1] = Nrows;
    108103  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);
    110107
    111108  return (TRUE);
     
    122119  Ny = table[0].header[0].Naxis[1];
    123120  table[0].datasize = gfits_data_size (table[0].header);
     121  // table[0].heap_start = gfits_heap_start (table[0].header);
    124122  table[0].pad = table[0].datasize - Nx*Ny;
    125123
     
    206204    for (i = 0; i < Nrow; i++) table[0].row[i] = row[i];
    207205    table[0].datasize = gfits_data_size (table[0].header);
     206    // table[0].heap_start = gfits_heap_start (table[0].header);
    208207    table[0].pad      = table[0].datasize - Nx*Ny;
    209208    return (TRUE);
  • branches/eam_branches/ohana.20150429/src/libfits/table/F_set_column.c

    r38325 r38329  
    1818int gfits_set_bintable_column (Header *header, FTable *table, char *label, void *data, off_t Nrow) {
    1919
    20   off_t Nx, Ny, nbytes;
     20  off_t Nx, Ny;
    2121  int i, N, Nfields;
    2222  int Nval, Nbytes, Nstart, Nv, Nb;
     
    5555  gfits_scan (header, "NAXIS1", OFF_T_FMT, 1,  &Nx);
    5656  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
    5759  if (Ny == 0) {
    5860    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);
    6662  }
    6763  if (Ny != Nrow) return (FALSE);
     
    181177int gfits_set_bintable_column_reformat (Header *header, FTable *table, char *label, char *intype, void *data, off_t Nrow) {
    182178
    183   off_t Nx, Ny, nbytes;
     179  off_t Nx, Ny;
    184180  int i, N, Nfields;
    185181  int Nval, NbytesOut, Nstart, Nv, Nb;
     
    220216  if (Ny == 0) {
    221217    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);
    229219  }
    230220  if (Ny != Nrow) return (FALSE);
     
    701691int gfits_set_table_column (Header *header, FTable *table, char *label, void *data, off_t Nrow) {
    702692
    703   off_t Nx, Ny, nbytes;
     693  off_t Nx, Ny;
    704694  int i, N, Nfields, Nval, Nbytes, Nstart, Nv, Nb;
    705695  char tlabel[80], field[80], format[80], cformat[80], type[16], tmp[16];
     
    731721  if (Ny == 0) {
    732722    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);
    740724  }
    741725  if (Ny != Nrow) return (FALSE);
  • branches/eam_branches/ohana.20150429/src/libfits/table/F_table_format.c

    r38326 r38329  
    88int gfits_bintable_format (char *format, char *type, int *Nval, int *Nbytes) {
    99
    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)
    1918  if ((Nv == 0) && (Fchar == format)) Nv = 1;
    2019
     
    5150    { *Nbytes = 8;  strcpy (type, "double"); *Nval = Nv;               }
    5251    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;
    5358  case  'P':
    5459    { *Nbytes = 4;  strcpy (type, "var");    *Nval = 2*Nv;             }
     
    5661  case  'Q':
    5762    { *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;             }
    6463    break;
    6564  default:
  • branches/eam_branches/ohana.20150429/src/libfits/table/F_table_row.c

    r28241 r38329  
    2828  REALLOCATE (table[0].buffer, char, MAX (nbytes, 1));
    2929  table[0].datasize = nbytes;
     30  table[0].heap_start = gfits_heap_start (header);
    3031 
    3132  memcpy (&table[0].buffer[Nstart], data, Nbytes*Nrow);
     
    6768
    6869  table[0].datasize = gfits_data_size (table[0].header);
     70  // table[0].heap_start = gfits_heap_start (header);
    6971  table[0].pad      = table[0].datasize - Nx*Ny;
    7072
     
    102104  REALLOCATE (table[0].buffer, char, MAX (nbytes, 1));
    103105  table[0].datasize = nbytes;
     106  table[0].heap_start = gfits_heap_start (header);
    104107  return (TRUE);
    105108}
  • branches/eam_branches/ohana.20150429/src/libfits/table/F_table_varlength.c

    r38326 r38329  
    1515  snprintf (field, 80, "TFORM%d", column);
    1616  if (!gfits_scan (ftable->header, field, "%s", 1, format)) return (FALSE);
     17
     18  def->column = column;
    1719
    1820  // find and remove the max field length element
     
    4951  if (*p1 == 'I') { def->nbytes = 2;  def->format = *p1; }
    5052  if (*p1 == 'J') { def->nbytes = 4;  def->format = *p1; }
     53  if (*p1 == 'K') { def->nbytes = 8;  def->format = *p1; }
    5154  if (*p1 == 'E') { def->nbytes = 4;  def->format = *p1; }
    5255  if (*p1 == 'D') { def->nbytes = 8;  def->format = *p1; }
     
    5558  if (!def->nbytes) { return (FALSE); }
    5659 
    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;
    5962  for (i = 1; i < column; i++) {
    6063    sprintf (field, "TFORM%d", i);
    6164    gfits_scan (ftable->header, field, "%s", 1, format);
    6265    gfits_bintable_format (format, tmpline, &Nv, &Nb);
    63     def->Nstart += Nv*Nb;
     66    def->offset += Nv*Nb;
    6467  }
    6568
    6669  // 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
     85int 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
    71114  return TRUE;
    72115}
     
    85128  Nx = ftable->header->Naxis[0];
    86129
    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 
    91130# ifdef BYTE_SWAP
    92131  char *pchar, tmp;
    93   pchar = &ftable->buffer[row*Nx + column->Nstart];
     132  pchar = &ftable->buffer[row*Nx + column->offset];
    94133  SWAP_WORD;
    95   pchar = &ftable->buffer[row*Nx + column->Nstart + 4];
     134  pchar = &ftable->buffer[row*Nx + column->offset + 4];
    96135  SWAP_WORD;
    97136# endif
     
    99138  if (column->mode == 'P') {
    100139    int *ptr;
    101     ptr = (int *) &ftable->buffer[row*Nx + column->Nstart];
     140    ptr = (int *) &ftable->buffer[row*Nx + column->offset];
    102141    *length = ptr[0];
    103142    offset = ptr[1];
     
    105144  if (column->mode == 'Q') {
    106145    off_t *ptr;
    107     ptr = (off_t *) &ftable->buffer[row*Nx + column->Nstart];
     146    ptr = (off_t *) &ftable->buffer[row*Nx + column->offset];
    108147    *length = ptr[0];
    109148    offset = ptr[1];
    110149  }
    111150
    112   result = (void *) (ftable->buffer + column->heap_start + offset);
     151  result = (void *) (ftable->buffer + ftable->heap_start + offset);
    113152  return result;
    114153}
     
    121160// swap before writing out
    122161
    123 // Here are operations we need to do:
    124 
    125162int gfits_varlength_column_add_data (FTable *table, char *data, off_t Ndata, int row, VarLengthColumn *column) {
    126163
    127164  // find the current starting point for new data (end of current buffer main data + current heap
    128165  off_t heap_offset = gfits_data_min_size (table->header);
    129   char *heap_ptr = ftable->buffer + heap_offset;
    130166
    131167  // extend the buffer size to add the new data
     
    133169  REALLOCATE (table->buffer, char, Nbytes);
    134170
     171  char *heap_ptr = table->buffer + heap_offset;
     172
    135173  // add the new data to the table buffer
    136174  memcpy (heap_ptr, data, Ndata);
     
    142180  // find the cell for the specified row & column
    143181  // 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];
    145183
    146184  // set the cell values
    147185  if (column->mode == 'P') {
    148186    int *ptr;
    149     ptr = (int *) &ftable->buffer[row*Nx + column->Nstart];
     187    ptr = (int *) &table->buffer[row*Nx + column->offset];
    150188    ptr[0] = Ndata;
    151     ptr[1] = heap_offset;
     189    ptr[1] = heap_offset - table->heap_start;
    152190  }
    153191  if (column->mode == 'Q') {
    154192    off_t *ptr;
    155     ptr = (off_t *) &ftable->buffer[row*Nx + column->Nstart];
     193    ptr = (off_t *) &table->buffer[row*Nx + column->offset];
    156194    ptr[0] = Ndata;
    157     ptr[1] = heap_offset;
     195    ptr[1] = heap_offset - table->heap_start;
    158196  }
    159197
    160198  // update maxlen
     199  // fprintf (stderr, "row: %d, Ndata: %d, maxlen: %d\n", (int) row, (int) Ndata, (int) column->maxlen);
    161200  column->maxlen = MAX (column->maxlen, Ndata);
    162201
    163202  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);
    165204
    166205  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
     214int 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  
    8484  Nx = ftable[0].header[0].Naxis[0]; // final output table size on disk
    8585  ftable[0].datasize = gfits_data_size (ftable[0].header);
     86  ftable[0].heap_start = gfits_heap_start (ftable[0].header);
    8687
    8788  Nskip = start * Nx;
Note: See TracChangeset for help on using the changeset viewer.