IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 12, 2015, 6:18:23 PM (11 years ago)
Author:
eugene
Message:

merge changes from EAM dev branch ohana.20150429

Location:
trunk/Ohana
Files:
20 edited
7 copied

Legend:

Unmodified
Added
Removed
  • trunk/Ohana

  • trunk/Ohana/src/libfits/Makefile

    r38255 r38441  
    11default: install
    22help:
    3         @echo "make options: install libfits man clean dist"
     3        @echo "make options: install libfits man clean dist test test.verbose test.clean"
    44
    55include ../../Makefile.System
     
    1313INC     =       $(HOME)/include
    1414MAN     =       $(HOME)/doc
     15TESTDIR =       $(HOME)/test
     16TESTBIN =       $(HOME)/test
    1517include ../../Makefile.Common
    1618
     
    1921FULL_CPPFLAGS = $(BASE_CPPFLAGS) -I$(EXT)
    2022FULL_LDFLAGS  = $(BASE_LDFLAGS) -lohana
     23
     24TEST_CFLAGS   = $(BASE_CFLAGS)
     25TEST_CPPFLAGS = $(BASE_CPPFLAGS)
     26TEST_LDFLAGS  = $(BASE_LDFLAGS) -lFITS -lohana -ltap_ohana
     27
     28
     29TESTXTRA = tcomptiming
     30TESTPROG = zlib ricetest imagecomp tablecomp
     31$(TESTPROG) : % : $(TESTBIN)/%
     32$(TESTXTRA) : % : $(TESTBIN)/%
     33test: $(TESTPROG)
     34        for i in $(TESTPROG); do $(TESTBIN)/$$i 2>&1 | $(TESTHARNESS); done
     35test.verbose: $(TESTPROG)
     36        for i in $(TESTPROG); do $(TESTBIN)/$$i 2>&1 | $(TESTHARNESS) -v; done
    2137
    2238install: $(DESTLIB)/libFITS.a $(DESTLIB)/libFITS.$(DLLTYPE) $(DESTMAN)/fits.1
     
    3753
    3854MATRIX_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             $(MATR)/F_swap_raw.$(ARCH).o
     55$(MATR)/F_add_M_value.$(ARCH).o                 $(MATR)/F_add_M.$(ARCH).o          \
     56$(MATR)/F_create_M.$(ARCH).o                    $(MATR)/F_divide_M.$(ARCH).o       \
     57$(MATR)/F_free_M.$(ARCH).o                      $(MATR)/F_copy_M.$(ARCH).o         \
     58$(MATR)/F_write_M.$(ARCH).o                     $(MATR)/F_get_M_value.$(ARCH).o    \
     59$(MATR)/F_multiply_M.$(ARCH).o                  $(MATR)/F_read_M.$(ARCH).o         \
     60$(MATR)/F_insert_M.$(ARCH).o                    $(MATR)/F_set_M_value.$(ARCH).o    \
     61$(MATR)/F_convert_format.$(ARCH).o              $(MATR)/F_read_segment.$(ARCH).o   \
     62$(MATR)/F_read_portion.$(ARCH).o                $(MATR)/F_load_M.$(ARCH).o         \
     63$(MATR)/F_matrix.$(ARCH).o                      $(MATR)/F_compress_utils.$(ARCH).o \
     64$(MATR)/F_compress_M.$(ARCH).o                  $(MATR)/F_compress_data.$(ARCH).o  \
     65$(MATR)/F_uncompress_M.$(ARCH).o                $(MATR)/F_uncompress_data.$(ARCH).o \
     66$(MATR)/F_swap_raw.$(ARCH).o
    4967
    5068TABLE_OBJ = \
     
    5573$(TABL)/F_set_column.$(ARCH).o                  $(TABL)/F_get_column.$(ARCH).o   \
    5674$(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
     75$(TABL)/F_table_varlength.$(ARCH).o             $(TABL)/F_copy_T.$(ARCH).o       \
     76$(TABL)/F_compress_T.$(ARCH).o                  $(TABL)/F_uncompress_T.$(ARCH).o
    5877
    5978EXTERN_OBJ = \
  • trunk/Ohana/src/libfits/doc/notes-compress.txt

    r15487 r38441  
     1
     220150525 :
     3
     4uncompression steps:
     5
     6 * read in a table: gfits_fread_ftable_data (f, &ftable, FALSE); (no byte swap)
     7 * uncompress the image: gfits_uncompress_image (&buf[0].header, &buf[0].matrix, &ftable);
     8   * get ZIMAGE from heaer & delete
     9   * get ZCMPTYPE
     10   * get ZBITPIX, ZNAXIS, ZNAXISnn
     11   * get ZTILEn (if missing ZTILE1 = Nx, others 1)
     12   * get ZNAME, ZVAL
     13   * ZSIMPLE, ZTENSION, ZEXTEND, ZBLOCKED, ZPCOUNT, ZGCOUNT, etc
     14   * ZSCALE, ZBLANK, OBLANK, ZZERO
     15
     16   * data is stored in fits table COMPRESSED_DATA (1PB, 1PI, 1PJ)
     17
     18------------------------------------------------
    119
    220TFORMn : rPt(e_max)
  • trunk/Ohana/src/libfits/extern/gzip.c

    r23816 r38441  
    22# include <gfitsio.h>
    33# include <zlib.h>
     4# define EXTRA_VERBOSE 0
    45
    56/** the two functions in this file re-implement the uncompress function of zlib. 
     
    1011
    1112/* zlib.h -- interface of the 'zlib' general purpose compression library
    12   version 1.2.3, July 18th, 2005
    13 
    14   Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
    15 
    16   This software is provided 'as-is', without any express or implied
    17   warranty.  In no event will the authors be held liable for any damages
    18   arising from the use of this software.
    19 
    20   Permission is granted to anyone to use this software for any purpose,
    21   including commercial applications, and to alter it and redistribute it
    22   freely, subject to the following restrictions:
    23 
    24   1. The origin of this software must not be misrepresented; you must not
    25      claim that you wrote the original software. If you use this software
    26      in a product, an acknowledgment in the product documentation would be
    27      appreciated but is not required.
    28   2. Altered source versions must be plainly marked as such, and must not be
    29      misrepresented as being the original software.
    30   3. This notice may not be removed or altered from any source distribution.
    31 
    32   Jean-loup Gailly        Mark Adler
    33   jloup@gzip.org          madler@alumni.caltech.edu
    34 
    35 
    36   The data format used by the zlib library is described by RFCs (Request for
    37   Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
    38   (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
     13   version 1.2.3, July 18th, 2005
     14
     15   Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
     16
     17   This software is provided 'as-is', without any express or implied
     18   warranty.  In no event will the authors be held liable for any damages
     19   arising from the use of this software.
     20
     21   Permission is granted to anyone to use this software for any purpose,
     22   including commercial applications, and to alter it and redistribute it
     23   freely, subject to the following restrictions:
     24
     25   1. The origin of this software must not be misrepresented; you must not
     26   claim that you wrote the original software. If you use this software
     27   in a product, an acknowledgment in the product documentation would be
     28   appreciated but is not required.
     29   2. Altered source versions must be plainly marked as such, and must not be
     30   misrepresented as being the original software.
     31   3. This notice may not be removed or altered from any source distribution.
     32
     33   Jean-loup Gailly        Mark Adler
     34   jloup@gzip.org          madler@alumni.caltech.edu
     35
     36
     37   The data format used by the zlib library is described by RFCs (Request for
     38   Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
     39   (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
    3940*/
    4041
    4142/*
    42      The 'zlib' compression library provides in-memory compression and
     43  The 'zlib' compression library provides in-memory compression and
    4344  decompression functions, including integrity checks of the uncompressed
    4445  data.  This version of the library supports only one compression method
     
    4647  stream interface.
    4748
    48      Compression can be done in a single step if the buffers are large
     49  Compression can be done in a single step if the buffers are large
    4950  enough (for example if an input file is mmap'ed), or can be done by
    5051  repeated calls of the compression function.  In the latter case, the
     
    5253  (providing more output space) before each call.
    5354
    54      The compressed data format used by default by the in-memory functions is
     55  The compressed data format used by default by the in-memory functions is
    5556  the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
    5657  around a deflate stream, which is itself documented in RFC 1951.
    5758
    58      The library also supports reading and writing files in gzip (.gz) format
     59  The library also supports reading and writing files in gzip (.gz) format
    5960  with an interface similar to that of stdio using the functions that start
    6061  with "gz".  The gzip format is different from the zlib format.  gzip is a
    6162  gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
    6263
    63      This library can optionally read and write gzip streams in memory as well.
    64 
    65      The zlib format was designed to be compact and fast for use in memory
     64  This library can optionally read and write gzip streams in memory as well.
     65
     66  The zlib format was designed to be compact and fast for use in memory
    6667  and on communications channels.  The gzip format was designed for single-
    6768  file compression on file systems, has a larger header than zlib to maintain
    6869  directory information, and uses a different, slower check method than zlib.
    6970
    70      The library does not install any signal handler. The decoder checks
     71  The library does not install any signal handler. The decoder checks
    7172  the consistency of the compressed data, so the library should never
    7273  crash even in case of corrupted input.
     
    116117}
    117118
    118 // XXX ??? is cfitsio writing data which is incompatible with zlib ???
    119 int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
    120 {
    121     z_stream stream;
    122     int err;
    123 
    124     stream.next_in = (Bytef*)source;
    125     stream.avail_in = (uInt)sourceLen;
    126     /* Check for source > 64K on 16-bit machine: */
    127     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
    128 
    129     stream.next_out = dest;
    130     stream.avail_out = (uInt)*destLen;
    131     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
    132 
    133     stream.zalloc = (alloc_func)0;
    134     stream.zfree = (free_func)0;
    135 
    136     // MAX_WBITS = 15
    137     err = inflateInit2(&stream, -15);
    138     if (err != Z_OK) return err;
    139 
    140     err = inflate(&stream, Z_FINISH);
    141     if (err != Z_STREAM_END) {
    142         inflateEnd(&stream);
    143         if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
    144             return Z_DATA_ERROR;
    145         return err;
    146     }
    147     assert (stream.total_out <= *destLen);
    148     *destLen = stream.total_out;
    149 
    150     err = inflateEnd(&stream);
    151     return err;
     119# define ESCAPE(RET) { fprintf (stderr, "gzip error in %s @ %s:%d\n", __func__, __FILE__, __LINE__); return (RET); }
     120
     121int gfits_compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen) {
     122
     123  z_stream stream;
     124  int i, err;
     125 
     126  stream.next_in = (Bytef*)source;
     127  stream.avail_in = (uInt)sourceLen;
     128
     129  /* Check for source > 64K on 16-bit machine: */
     130  if ((uLong)stream.avail_in != sourceLen) ESCAPE (Z_BUF_ERROR);
     131
     132  stream.next_out = dest;
     133  stream.avail_out = (uInt)*destLen; // allocated space
     134  if ((uLong)stream.avail_out != *destLen) ESCAPE (Z_BUF_ERROR);
     135
     136  stream.zalloc = Z_NULL;
     137  stream.zfree  = Z_NULL;
     138  stream.opaque = Z_NULL;
     139
     140  if (EXTRA_VERBOSE) {
     141    fprintf (stderr, "inp cmp: ");
     142    for (i = 0; i < sourceLen; i++) {
     143      fprintf (stderr, "0x%02hhx ", source[i]);
     144    }
     145    fprintf (stderr, "\n");
     146  }
     147
     148  // the '1' is the compression level: make this a user argument
     149  // NOTE: cfitsio uses a fixed value of 1
     150  // I am using deflateInit2 because cfitsio expects gzip, not just zlib
     151  // windowBits = 31 = (15 + 16) = (2^15 window bits) + (create a gzip stream)
     152  err = deflateInit2(&stream, 1, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
     153  if (0) fprintf (stderr, "inp buffers cmp 0: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out);
     154
     155  if (err != Z_OK) ESCAPE(err);
     156
     157  // XXX this is written to do the compression in a single pass.  it could be re-done to have
     158  // the compression occur in a series of steps
     159  err = deflate(&stream, Z_FINISH);
     160  if (err != Z_STREAM_END) {
     161    err = deflateEnd(&stream);
     162    ESCAPE (Z_BUF_ERROR);
     163  }
     164
     165  if (EXTRA_VERBOSE) {
     166    fprintf (stderr, "out cmp: ");
     167    for (i = 0; i < stream.total_out; i++) {
     168      fprintf (stderr, "0x%02hhx ", dest[i]);
     169    }
     170    fprintf (stderr, "\n");
     171  }
     172
     173  assert (stream.total_out <= *destLen);
     174  *destLen = stream.total_out;
     175 
     176  err = deflateEnd(&stream);
     177  return err;
    152178}
     179
     180// NOTE: CFITSIO uses gzip format, not just zlib
     181int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen) {
     182
     183  z_stream stream;
     184  int i, err;
     185 
     186  stream.next_in = (Bytef*)source;
     187  stream.avail_in = (uInt)sourceLen;
     188
     189  /* Check for source > 64K on 16-bit machine: */
     190  if ((uLong)stream.avail_in != sourceLen) ESCAPE (Z_BUF_ERROR);
     191 
     192  stream.next_out = dest;
     193  stream.avail_out = (uInt)*destLen;
     194  if ((uLong)stream.avail_out != *destLen) ESCAPE (Z_BUF_ERROR);
     195 
     196  stream.zalloc = Z_NULL;
     197  stream.zfree  = Z_NULL;
     198  stream.opaque = Z_NULL;
     199 
     200  if (EXTRA_VERBOSE) {
     201    fprintf (stderr, "inp unc: ");
     202    for (i = 0; i < sourceLen; i++) {
     203      fprintf (stderr, "0x%02hhx ", source[i]);
     204    }
     205    fprintf (stderr, "\n");
     206  }
     207 
     208  // windowBits = 47 = (15 + 32) = (2^15 window bits) + (test for either gzip or zlib
     209  // streams).  NOTE: modern versions of zlib (version > 1.2.3.4) allow for windowBits = 0
     210  // (or 16 or 32) to use the compressed stream windowBits value.  IPP cluster still has
     211  // zlib version = 1.2.3, so we cannot use this feature.
     212  err = inflateInit2(&stream, 47);
     213  if (err != Z_OK) ESCAPE (err);
     214 
     215  err = inflate(&stream, Z_FINISH);
     216  if (err != Z_STREAM_END) {
     217    inflateEnd(&stream);
     218    if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) ESCAPE (Z_DATA_ERROR);
     219    ESCAPE(err);
     220  }
     221 
     222  if (EXTRA_VERBOSE) {
     223    fprintf (stderr, "out unc: ");
     224    for (i = 0; i < stream.total_out; i++) {
     225      fprintf (stderr, "0x%02hhx ", dest[i]);
     226    }
     227    fprintf (stderr, "\n");
     228  }
     229
     230  assert (stream.total_out <= *destLen);
     231  *destLen = stream.total_out;
     232
     233  err = inflateEnd(&stream);
     234  return err;
     235}
     236
  • trunk/Ohana/src/libfits/header/F_copy_H.c

    r27435 r38441  
    3030  return (TRUE);
    3131}       
     32
     33int gfits_copy_header_ptr (Header *in, Header *out) {
     34
     35  int i;
     36
     37  if (!in)  return FALSE;
     38  if (!out) return FALSE;
     39
     40  out[0].simple = in[0].simple;
     41  out[0].unsign = in[0].unsign;
     42  out[0].extend = in[0].extend;
     43  out[0].bitpix = in[0].bitpix;
     44
     45  out[0].Naxes  = in[0].Naxes;
     46  for (i = 0; i < FT_MAX_NAXES; i++)
     47    out[0].Naxis[i] = in[0].Naxis[i];
     48
     49  out[0].datasize = in[0].datasize;
     50
     51  out[0].pcount = in[0].pcount;
     52  out[0].gcount = in[0].gcount;
     53  out[0].bzero  = in[0].bzero;
     54  out[0].bscale = in[0].bscale;
     55
     56  out[0].buffer = in[0].buffer;
     57
     58  return (TRUE);
     59}       
  • trunk/Ohana/src/libfits/header/F_modify.c

    r34577 r38441  
    4848    strncpy (comment, qe, p + 80 - qe);
    4949  }
    50   gfits_pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */
     50  gfits_pad_ending (comment, 0x20, 82);  /* comment must contain spaces to the end */
    5151
    5252  /* write the numeric modes */
     
    134134    strncpy (comment, qe, p + 80 - qe);
    135135  }
    136   gfits_pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */
     136  gfits_pad_ending (comment, 0x20, 82);  /* comment must contain spaces to the end */
    137137
    138138  /* write the boolean mode */
     
    166166    if (!ptr) goto invalid;
    167167    strncpy (comment, ptr, 80);
    168     gfits_pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */
     168    gfits_pad_ending (comment, 0x20, 82);  /* comment must contain spaces to the end */
    169169    snprintf (string, 81, "%-8s= %s / %-s", field, data, comment);
    170170    /* this will keep the original line, but truncate the comment */
     
    296296 */
    297297
     298// XXX this is an absurd patch on a gcc bug: memset has a problem is value != 0 and N is constant
     299void myMemset (char *ptr, int value, size_t N) {
     300
     301  char *p = ptr;
     302 
     303  size_t i = 0;
     304  for (i = 0; i < N; i++, p++) {
     305    *p = value;
     306  }
     307}
     308
    298309/* fill 'line' with Nbyte space from first NULL to last byte with value */
    299310void gfits_pad_ending (char *line, char value, int Nbyte) {
    300  
    301   char *p;
    302   int N;
    303311
    304312  line[Nbyte-1] = 0;
    305313 
    306   p = line + strlen (line);
    307   N = MAX (Nbyte - strlen (line) - 1, 0);
    308   memset (p, value, N);
     314  char *p = line + strlen (line);
     315  size_t N = MAX (Nbyte - strlen (line) - 1, 0);
     316  myMemset (p, value, N);
    309317}
    310318
  • trunk/Ohana/src/libfits/include/gfitsio.h

    r38255 r38441  
    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
     
    106127char   *gfits_keyword_end              PROTO((char *line));
    107128int     gfits_copy_header              PROTO((Header *in, Header *out));
     129int     gfits_copy_header_ptr          PROTO((Header *in, Header *out));
    108130int     gfits_create_header            PROTO((Header *header));
    109131int     gfits_delete                   PROTO((Header *header, char *field, int N));
     
    117139int     gfits_modify                   PROTO((Header *header, char *field, char *mode, int N,...)) OHANA_FORMAT(printf, 3, 5);
    118140int     gfits_print                    PROTO((Header *header, char *field, char *mode, int N,...)) OHANA_FORMAT(printf, 3, 5);
    119 int     gfits_modify_alt               PROTO((Header *header, char *field, char *mode, int N,...));
    120 int     gfits_print_alt                PROTO((Header *header, char *field, char *mode, int N,...));
     141int     gfits_modify_alt               PROTO((Header *header, char *field, char *mode, int N,...)); // do not use a FORMAT: non-standard fmt chars
     142int     gfits_print_alt                PROTO((Header *header, char *field, char *mode, int N,...)); // do not use a FORMAT: non-standard fmt chars
    121143int     gfits_find_Xheader             PROTO((FILE *f, Header *header, char *extname));
    122144int     gfits_read_Xheader             PROTO((char *filename, Header *header, int N));
     
    124146int     gfits_save_header              PROTO((FILE *f, Header *header));
    125147int     gfits_scan                     PROTO((Header *header, char *field, char *mode, int N,...)) OHANA_FORMAT(scanf, 3, 5);
    126 int     gfits_scan_alt                 PROTO((Header *header, char *field, char *mode, int N,...));
     148int     gfits_scan_alt                 PROTO((Header *header, char *field, char *mode, int N,...)); // do not use a FORMAT: non-standard fmt chars
    127149int     gfits_set_unsign_mode          PROTO((int mode));
    128150int     gfits_stripwhite               PROTO((char *string));
     
    133155off_t   gfits_data_min_size            PROTO((Header *header));
    134156off_t   gfits_data_pad_size            PROTO((off_t rawsize));
     157off_t   gfits_heap_start               PROTO((Header *header));
    135158int     gfits_extended_to_primary      PROTO((Header *header, int simple, char *comment));
    136159int     gfits_primary_to_extended      PROTO((Header *header, char *exttype, char *comment));
     
    166189void    gfits_set_matrix_value         PROTO((Matrix *matrix, off_t x, off_t y, double value));
    167190int     gfits_write_matrix             PROTO((char *filename, Matrix *matrix));
     191
    168192int     gfits_uncompress_image         PROTO((Header *header, Matrix *matrix, FTable *ftable));
    169 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 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));
    171 int     gfits_byteswap_zdata           PROTO((char *zdata, int Nzdata, int bitpix));
     193int     gfits_uncompress_data          PROTO((char *zdata, int Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *outdata, int *Nout, int Nout_alloc, int out_pixsize));
     194int     gfits_distribute_data          PROTO((Matrix *matrix, char *data, int Ndata, int bitpix, int *otile, int oblank, int *ztile, int zblank, float zscale, float zzero));
     195
     196int     gfits_compress_data            PROTO((char *zdata, int *Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *rawdata, int Nraw, int raw_pixsize, int Nx, int Ny));
     197int     gfits_compress_image           PROTO((Header *header, Matrix *matrix, FTable *ftable, int *Ztile, char *zcmptype));
     198int     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));
     199
     200int     gfits_copy_keywords_compress   PROTO((Header *srchead, Header *tgthead));
    172201int     gfits_swap_raw                 PROTO((Matrix *matrix));
    173 int     gfits_extension_is_compressed  PROTO((Header *header));
     202
    174203off_t   gfits_tile_size                PROTO((Matrix *matrix, int *otile, int *ztile));
     204int     gfits_imtile_maxsize           PROTO((Matrix *matrix, int *ztile));
     205int     gfits_imtile_count             PROTO((Matrix *matrix, int *ztile, int *ntile));
     206int     gfits_imtile_start             PROTO((Matrix *matrix, int *otile));
     207int     gfits_imtile_next              PROTO((Matrix *matrix, int *ztile, int *ntile, int *otile));
     208
     209int     gfits_extension_is_compressed_image  PROTO((Header *header));
     210int     gfits_extension_is_compressed_table  PROTO((Header *header));
     211int     gfits_byteswap_zdata            PROTO((char *zdata, int Nzdata, int pixsize));
     212int     gfits_compressed_data_pixsize   PROTO((char *cmptype, int out_bitpix, char **optname, char **optvalue, int Noptions));
    175213int     gfits_uncompressed_data_pixsize PROTO((char *cmptype, int out_bitpix, char **optname, char **optvalue, int Noptions));
    176 int     gfits_vartable_heap_pixsize    PROTO((char format));
     214int     gfits_uncompressed_data_bitpix  PROTO((char *cmptype, int out_bitpix, char **optname, char **optvalue, int Noptions));
     215int     gfits_vartable_heap_pixsize     PROTO((char format));
     216int     gfits_cmptype_valid             PROTO((char *cmptype));
     217
     218int     gfits_varlength_column_add_data PROTO((FTable *table, char *data, off_t Ndata, int row, VarLengthColumn *column));
     219void   *gfits_varlength_column_pointer  PROTO((FTable *ftable, VarLengthColumn *column, off_t row, off_t *length));
     220int     gfits_varlength_column_define   PROTO((FTable *ftable, VarLengthColumn *def, int column));
     221int     gfits_varlength_column_finish   PROTO((FTable *ftable, VarLengthColumn *def));
     222int     gfits_byteswap_varlength_column PROTO((FTable *ftable, int column));
    177223
    178224/******************************* Table functions *************/
     
    183229char   *gfits_table_print              PROTO((FTable *ftable,...));
    184230int     gfits_add_rows                 PROTO((FTable *ftable, char *data, off_t Nrow, off_t Nbytes));
     231int     gfits_set_table_rows           PROTO((Header *header, FTable *table, off_t Nrows));
    185232int     gfits_bintable_format          PROTO((char *format, char *type, int *Nval, int *Nbytes));
    186233int     gfits_create_table             PROTO((Header *header, FTable *ftable));
     
    199246int     gfits_fwrite_ftable_range      PROTO((FILE *f, FTable *table, off_t start, off_t Nrows, off_t Ndisk, off_t Ntotal));
    200247int     gfits_get_bintable_column      PROTO((Header *header, FTable *table, char *label, void **data));
     248int     gfits_get_bintable_column_raw  PROTO((Header *header, FTable *table, char *label, void **data, char nativeOrder));
    201249int     gfits_get_bintable_column_type PROTO((Header *header, char *label, char *type, int *Nval));
     250void   *gfits_get_bintable_column_data PROTO((Header *header, FTable *table, char *label, char *type, off_t *Nrow, int *Ncol));
    202251int     gfits_get_bintable_column_type_by_N  PROTO((Header *header, int N, char *type, int *Nval));
    203 void   *gfits_get_bintable_column_data PROTO((Header *header, FTable *table, char *label, char *type, off_t *Nrow, int *Ncol));
     252void   *gfits_get_bintable_column_data_raw   PROTO((Header *header, FTable *table, char *label, char *type, off_t *Nrow, int *Ncol, char nativeOrder));
    204253int     gfits_get_table_column         PROTO((Header *header, FTable *table, char *label, void **data));
    205254int     gfits_get_table_column_type    PROTO((Header *header, char *label, char *type));
     
    207256int     gfits_read_table               PROTO((char *filename, FTable *ftable));
    208257int     gfits_set_bintable_column      PROTO((Header *header, FTable *table, char *label, void *data, off_t Nrow));
    209 int     gfits_set_bintable_column_reformat PROTO((Header *header, FTable *table, char *label, char *intype, void *data, off_t Nrow));
     258int     gfits_set_bintable_column_reformat PROTO((Header *header, FTable *table, char *label, char *intype, void *data, off_t Nrow, char nativeOrder));
    210259int     gfits_set_table_column         PROTO((Header *header, FTable *table, char *label, void *data, off_t Nrow));
    211260int     gfits_table_column             PROTO((FTable *ftable, char *field, char *mode,...)) OHANA_FORMAT(printf, 3, 4);
     
    219268int     gfits_copy_ftable              PROTO((FTable *in, FTable *out));
    220269int     gfits_copy_vtable              PROTO((VTable *in, VTable *out));
     270int     gfits_copy_ftable_ptr          PROTO((FTable *in, FTable *out));
    221271
    222272int     gfits_create_Theader           PROTO((Header *header, char *type));
     
    227277int     gfits_write_Theader            PROTO((char *filename, Header *header));
    228278
    229 int     gfits_varlength_column_define  PROTO((FTable *ftable, VarLengthColumn *def, int column));
    230 void   *gfits_varlength_column_pointer PROTO((FTable *ftable, VarLengthColumn *column, off_t row, off_t *length));
     279int     gfits_compress_table           PROTO((FTable *srctable, FTable *tgttable, int ztilelen, char *zcmptype));
     280int     gfits_uncompress_table         PROTO((FTable *srctable, FTable *tgttable));
     281
     282// XXX EAM for testing
     283int gfits_dump_raw_table (FTable *table, char *message);
     284int gfits_dump_cmp_table (FTable *table, char *message);
     285
    231286
    232287#endif /* FITSIO */
  • trunk/Ohana/src/libfits/matrix/F_matrix.c

    r35164 r38441  
    22# include <gfitsio.h>
    33
    4 off_t gfits_data_size (Header *header) {
    5  
    6   int i;
    7   off_t Nrec, size;
    8 
    9   if (header[0].Naxes == 0) return (0);
    10 
    11   size = abs(header[0].bitpix / 8);
    12 
    13   for (i = 0; i < header[0].Naxes; i++)
    14     size *= header[0].Naxis[i];
    15 
    16   // XXX do I multiply this times gcount?
    17   size += header[0].pcount;
    18 
    19   /* round up to next complete block */
    20   if (size % FT_RECORD_SIZE) {
    21     Nrec = 1 + (int) (size / FT_RECORD_SIZE);
    22     size = FT_RECORD_SIZE * Nrec;
    23   }
    24 
    25   return (size);
    26 }
    27 
    28 off_t gfits_data_min_size (Header *header) {
     4off_t gfits_heap_start (Header *header) {
    295 
    306  int i;
     
    3814    size *= header[0].Naxis[i];
    3915
    40   // XXX do I multiply this times gcount?
     16  return (size);
     17}
     18
     19off_t gfits_data_min_size (Header *header) {
     20 
     21  off_t size = gfits_heap_start (header);
    4122  size += header[0].pcount;
     23
     24  // XXX what do I do with gcount?
    4225
    4326  return (size);
     
    4629off_t gfits_data_pad_size (off_t rawsize) {
    4730 
    48   off_t Nrec, size;
    49 
    50   size = rawsize;
     31  off_t size = rawsize;
    5132
    5233  /* round up to next complete block */
    5334  if (rawsize % FT_RECORD_SIZE) {
    54     Nrec = 1 + (int) (rawsize / FT_RECORD_SIZE);
     35    off_t Nrec = 1 + (int) (rawsize / FT_RECORD_SIZE);
    5536    size = FT_RECORD_SIZE * Nrec;
    5637  }
     
    5839  return (size);
    5940}
     41
     42off_t gfits_data_size (Header *header) {
     43  off_t rawsize = gfits_data_min_size(header);
     44  off_t size = gfits_data_pad_size (rawsize);
     45
     46  return (size);
     47}
     48
  • trunk/Ohana/src/libfits/matrix/F_uncompress_data.c

    r23815 r38441  
    2626int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
    2727
    28 int gfits_uncompress_data (char *zdata, int Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *outdata, int *Nout, int out_pixsize) {
     28# define ESCAPE(RET) { fprintf (stderr, "gzip error in %s @ %s:%d\n", __func__, __FILE__, __LINE__); return (RET); }
     29
     30int gfits_uncompress_data (char *zdata, int Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *outdata, int *Nout, int Nout_alloc, int out_pixsize) {
    2931
    3032  int status;
    31   static int Ninsum = 0;
    32   static int Noutsum = 0;
    3333
    34   if (!strcasecmp(cmptype, "GZIP_1")) {
    35     unsigned long tNout = *Nout * out_pixsize;
     34  // do not actually uncompress : this is used for testing
     35  if (!strcasecmp(cmptype, "NONE") || !strcasecmp(cmptype, "NONE_1") || !strcasecmp(cmptype, "NONE_2")) {
     36    memcpy (outdata, zdata, Nzdata);
     37    myAssert (*Nout == Nzdata, "invalid size");
     38    *Nout = Nzdata / out_pixsize;
     39    return (TRUE);
     40  }
     41
     42  if (!strcasecmp(cmptype, "GZIP_1") || !strcasecmp(cmptype, "GZIP_2")) {
     43    // unsigned long tNout = *Nout * out_pixsize;
     44    // input value of *Nout is number of BYTES
     45    unsigned long tNout = Nout_alloc;
    3646
    3747    // for GZIP data, I need to check for and remove the header on the first block:
    38     gfits_gz_stripheader ((unsigned char *)zdata, &Nzdata);
     48    // XXX maybe not anymore
     49    // gfits_gz_stripheader ((unsigned char *)zdata, &Nzdata);
    3950
    4051    // uncompress does not require us to know the expected number of pixel; it tells us the number
     52    // XXX shouldn't we validate the result : we think we know the size
    4153    status = gfits_uncompress ((Bytef *) outdata, &tNout, (Bytef *) zdata, Nzdata);
    42     if (status != Z_OK) {
    43       fprintf (stderr, "error in uncompress (GZIP)\n");
    44       return (FALSE);
    45     }
     54    if (status != Z_OK) ESCAPE(FALSE);
     55    myAssert (*Nout == tNout, "uncompressed size mismatch");
    4656    *Nout = tNout / out_pixsize;
     57    // output value of *Nout is number of PIXELS
    4758
    4859    // the resulting uncompressed data is byteswapped
    49     if (!gfits_byteswap_zdata (outdata, *Nout, out_pixsize)) return (FALSE);
     60    // if (!gfits_byteswap_zdata (outdata, *Nout, out_pixsize)) return (FALSE);
    5061
    5162    return (TRUE);
    5263  }
    5364
    54   if (!strcasecmp(cmptype, "RICE_1")) {
     65  if (!strcasecmp(cmptype, "RICE_1") || !strcasecmp(cmptype, "RICE_ONE")) {
    5566    int i, blocksize;
    5667    // look for the BLOCKSIZE
     
    6677    }
    6778
    68     int Npix;
    69     // Npix = *Nout * (out_pixsize / 4.0);
    70     Npix = *Nout;
    71 
    72     Ninsum += Nzdata;
    73     Noutsum += *Nout;
    74     // fprintf (stderr, "%d comp bytes; %d uncomp 'pixels', totals: %d %d\n", Nzdata, Npix, Ninsum, Noutsum);
     79    int status = FALSE;
     80    int Npix = *Nout / out_pixsize;
     81    int k;
    7582
    7683    switch (out_pixsize) {
    7784      case 4:
    7885        // rice decompression from the CFITSIO source tree : we need to tell it the expected number of pixels
    79         // is also REQUIRES 4byte output, which is fairly stupid.
    80         if (fits_rdecomp ((unsigned char *) zdata, Nzdata, (unsigned int *) outdata, Npix, blocksize)) {
    81           fprintf (stderr, "error in rice decompression\n");
    82           return (FALSE);
     86        status = fits_rdecomp ((unsigned char *) zdata, Nzdata, (unsigned int *) outdata, Npix, blocksize);
     87        break;
     88      case 2:
     89        if (0) {
     90          fprintf (stderr, "Nout: %d, Nrawpix: %d\n", Nzdata, Npix);
     91          fprintf (stderr, "cmp out: ");
     92          for (k = 0; k < Nzdata; k++) { fprintf (stderr, "0x%02hhx ", zdata[k]); }
     93          fprintf (stderr, "\n");
    8394        }
    84         return (TRUE);
    85 
    86       case 2:
    87         if (fits_rdecomp_short ((unsigned char *) zdata, Nzdata, (unsigned short *) outdata, Npix, blocksize)) {
    88           fprintf (stderr, "error in rice decompression\n");
    89           return (FALSE);
    90         }
    91         return (TRUE);
    92 
     95        status = fits_rdecomp_short ((unsigned char *) zdata, Nzdata, (unsigned short *) outdata, Npix, blocksize);
     96        break;
    9397      case 1:
    94         if (fits_rdecomp_byte ((unsigned char *) zdata, Nzdata, (unsigned char *) outdata, Npix, blocksize)) {
    95           fprintf (stderr, "error in rice decompression\n");
    96           return (FALSE);
    97         }
    98         return (TRUE);
    99        
     98        status = fits_rdecomp_byte ((unsigned char *) zdata, Nzdata, (unsigned char *) outdata, Npix, blocksize);
     99        break;
    100100      default:
    101101        fprintf (stderr, "invalid output pixel size %d\n", out_pixsize);
    102         return (FALSE);
     102        status = FALSE;
     103        break;
    103104    }
    104    
     105    if (status) {
     106      *Nout = 0;
     107      return FALSE;
     108    }
     109    *Nout = Npix;
     110    return TRUE;
    105111  }
    106112 
  • trunk/Ohana/src/libfits/table/F_copy_T.c

    r34088 r38441  
    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);
    1213  memcpy (out[0].buffer, in[0].buffer, out[0].datasize);
     14  return (TRUE);
     15}       
     16
     17/*********************** copy ftable pointers (does not copy memory) ***********************************/
     18int gfits_copy_ftable_ptr (FTable *in, FTable *out) {
     19
     20  if (!in)  return FALSE;
     21  if (!out) return FALSE;
     22
     23  /* find buffer size */
     24  out[0].validsize  = in[0].validsize;
     25  out[0].datasize   = in[0].datasize;
     26  out[0].heap_start = in[0].heap_start;
     27  out[0].header     = in[0].header;
     28  out[0].buffer     = in[0].buffer;
    1329  return (TRUE);
    1430}       
     
    2844
    2945  /* find buffer size */
    30   out[0].datasize = in[0].datasize;
    31   out[0].pad      = in[0].pad;
    32   out[0].Nrow     = in[0].Nrow;
     46  out[0].datasize   = in[0].datasize;
     47  // out[0].heap_start = in[0].heap_start;
     48  out[0].pad        = in[0].pad;
     49  out[0].Nrow       = in[0].Nrow;
    3350
    3451  off_t Nrows = out[0].Nrow;
  • trunk/Ohana/src/libfits/table/F_create_T.c

    r35756 r38441  
    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 
  • trunk/Ohana/src/libfits/table/F_define_column.c

    r35756 r38441  
    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);
  • trunk/Ohana/src/libfits/table/F_get_column.c

    r37039 r38441  
    1717
    1818void *gfits_get_bintable_column_data (Header *header, FTable *table, char *label, char *type, off_t *Nrow, int *Ncol) {
     19  void *data = gfits_get_bintable_column_data_raw (header, table, label, type, Nrow, Ncol, FALSE);
     20  return data;
     21}
     22
     23void *gfits_get_bintable_column_data_raw (Header *header, FTable *table, char *label, char *type, off_t *Nrow, int *Ncol, char nativeOrder) {
    1924
    2025  off_t Nx, Ny;
     
    7176  }
    7277
    73   /* convert data in-situ with correct type, byte swap and Bzero/Bscale */
     78  // NOTE: we have already copied the data to 'array', so the blocks below
     79  // only need to swap if this is a direct copy
    7480  Pin  = array;
    7581  Pout = array;
     82  int directCopy = (Bzero == 0.0) && (Bscale == 1.0);
     83
     84  /* convert data in-situ with correct type, byte swap and Bzero/Bscale */
    7685  if (!strcmp (type, "char")) {
    7786    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
    78       *(char *)Pout = *(char *)Pin*Bscale + Bzero;
     87      if (!directCopy) { *(char *)Pout = *(char *)Pin*Bscale + Bzero; }
    7988    }
    8089  }
    8190  if (!strcmp (type, "byte")) {
    8291    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
    83       *(char *)Pout = *(char *)Pin*Bscale + Bzero;
     92      if (!directCopy) { *(char *)Pout = *(char *)Pin*Bscale + Bzero; }
    8493    }
    8594  }
     
    8796    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
    8897# ifdef BYTE_SWAP
    89       SWAP_BYTE;
    90 # endif
    91       *(short *)Pout = *(short *)Pin*Bscale + Bzero;
     98      if (!nativeOrder) { SWAP_BYTE; }
     99# endif
     100      if (!directCopy) { *(short *)Pout = *(short *)Pin*Bscale + Bzero; }
    92101    } 
    93102  }
     
    95104    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
    96105# ifdef BYTE_SWAP
    97       SWAP_WORD;
    98 # endif
    99       *(int *)Pout = *(int *)Pin*Bscale + Bzero;
     106      if (!nativeOrder) { SWAP_WORD; }
     107# endif
     108      if (!directCopy) { *(int *)Pout = *(int *)Pin*Bscale + Bzero; }
    100109    }
    101110  }
    102111  if (!strcmp (type, "int64_t")) {
    103     if ((Bzero == 0.0) && (Bscale == 1.0)) {
    104       for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
    105 # ifdef BYTE_SWAP
    106         SWAP_DBLE;
    107 # endif
    108         *(int64_t *)Pout = *(int64_t *)Pin;
    109       }
    110     } else {
    111       for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
    112 # ifdef BYTE_SWAP
    113         SWAP_DBLE;
    114 # endif
    115         *(int64_t *)Pout = *(int64_t *)Pin*Bscale + Bzero;
    116       }
     112    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
     113# ifdef BYTE_SWAP
     114      if (!nativeOrder) { SWAP_DBLE; }
     115# endif
     116      if (!directCopy) { *(int64_t *)Pout = *(int64_t *)Pin*Bscale + Bzero; }
    117117    }
    118118  }
     
    120120    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
    121121# ifdef BYTE_SWAP
    122       SWAP_WORD;
    123 # endif
    124       *(float *)Pout = *(float *)Pin*Bscale + Bzero;
     122      if (!nativeOrder) { SWAP_WORD; }
     123# endif
     124      if (!directCopy) { *(float *)Pout = *(float *)Pin*Bscale + Bzero; }
    125125    }
    126126  }
     
    128128    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
    129129# ifdef BYTE_SWAP
    130       SWAP_DBLE;
    131 # endif
    132       *(double *)Pout = *(double *)Pin*Bscale + Bzero;
    133     }
    134   }
     130      if (!nativeOrder) { SWAP_DBLE; }
     131# endif
     132      if (!directCopy) { *(double *)Pout = *(double *)Pin*Bscale + Bzero; }
     133    }
     134  }
     135
     136  // check that we supplied a valid type
    135137
    136138  *Ncol = Nval;
     
    174176  if (!gfits_get_bintable_column_type_by_N (header, N, type, Nval)) return FALSE;
    175177  return (TRUE);
     178}
     179
     180/***********************/
     181int gfits_get_bintable_column_raw (Header *header, FTable *table, char *label, void **data, char nativeOrder) {
     182
     183  char type[16];
     184  off_t Nrow;
     185  int Ncol;
     186
     187  char *array = gfits_get_bintable_column_data_raw (header, table, label, type, &Nrow, &Ncol, nativeOrder);
     188  if (array == NULL) return (FALSE);
     189
     190  *data = array;
     191  return TRUE;
    176192}
    177193
  • trunk/Ohana/src/libfits/table/F_read_T.c

    r37807 r38441  
    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);
  • trunk/Ohana/src/libfits/table/F_set_column.c

    r37039 r38441  
    11# include <ohana.h>
    22# include <gfitsio.h>
     3
     4# define SWAP_NONE
     5
     6# ifdef BYTE_SWAP
    37# define SWAP_BYTE { \
    48  char tmp; \
     
    1418  tmp = Pout[2]; Pout[2] = Pout[5]; Pout[5] = tmp; \
    1519  tmp = Pout[3]; Pout[3] = Pout[4]; Pout[4] = tmp; }
     20# else
     21# define SWAP_BYTE
     22# define SWAP_WORD
     23# define SWAP_DBLE
     24# endif
    1625
    1726/***********************/
    1827int gfits_set_bintable_column (Header *header, FTable *table, char *label, void *data, off_t Nrow) {
    1928
    20   off_t Nx, Ny, nbytes;
     29  off_t Nx, Ny;
    2130  int i, N, Nfields;
    2231  int Nval, Nbytes, Nstart, Nv, Nb;
     
    5564  gfits_scan (header, "NAXIS1", OFF_T_FMT, 1,  &Nx);
    5665  gfits_scan (header, "NAXIS2", OFF_T_FMT, 1,  &Ny);
     66
     67  // if no rows have yet been assigned, we need to allocate the full data buffer
    5768  if (Ny == 0) {
    5869    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;
     70    gfits_set_table_rows (header, table, Ny);
    6671  }
    6772  if (Ny != Nrow) return (FALSE);
     
    143148  }
    144149
     150  if (!strcmp (type, "var")) {
     151    for (i = 0; i < Nval*Nrow; i++, Pin+=Nbytes, Pout+=Nbytes) {
     152      *(int *)Pout = *(int *)Pin;
     153# ifdef BYTE_SWAP
     154      SWAP_WORD;
     155# endif
     156    }
     157  }
     158
     159  if (!strcmp (type, "var64")) {
     160    for (i = 0; i < Nval*Nrow; i++, Pin+=Nbytes, Pout+=Nbytes) {
     161      *(int *)Pout = *(int *)Pin;
     162# ifdef BYTE_SWAP
     163      SWAP_DBLE;
     164# endif
     165    }
     166  }
     167
    145168  /* check array space */
    146169  if (Nx*Ny < Nx*(Nrow - 1) + Nstart + Nval*Nbytes) {
     
    161184
    162185/***********************/
    163 int gfits_set_bintable_column_reformat (Header *header, FTable *table, char *label, char *intype, void *data, off_t Nrow) {
    164 
    165   off_t Nx, Ny, nbytes;
     186// convert the input data array (of the specified intype) to the desired table data type. swap unless nativeOrder is requested
     187int gfits_set_bintable_column_reformat (Header *header, FTable *table, char *label, char *intype, void *data, off_t Nrow, char nativeOrder) {
     188
     189  off_t Nx, Ny;
    166190  int i, N, Nfields;
    167191  int Nval, NbytesOut, Nstart, Nv, Nb;
     
    202226  if (Ny == 0) {
    203227    Ny = Nrow;
    204     header[0].Naxis[1] = Ny;
    205     gfits_modify (header, "NAXIS2", OFF_T_FMT, 1,  Ny);
    206 
    207     nbytes = gfits_data_size (header);
    208     REALLOCATE (table[0].buffer, char, MAX (nbytes, 1));
    209     bzero (table[0].buffer, nbytes);
    210     table[0].datasize = nbytes;
     228    gfits_set_table_rows (header, table, Ny);
    211229  }
    212230  if (Ny != Nrow) return (FALSE);
     
    230248  // if (!strcmp (intype, #ITYPE)) {
    231249
     250  int directCopy = (Bzero == 0.0) && (Bscale == 1.0);
     251
     252# define SET_VALUES(OUTNAME, OUTTYPE, INNAME, INTYPE, SWAP_OP, NBYTES_IN) \
     253  if (!strcmp (outtype, OUTNAME) && !strcmp (intype, INNAME)) {         \
     254    int NbytesIn = NBYTES_IN;                                           \
     255    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) { \
     256      if (directCopy) {                                                 \
     257        *(OUTTYPE *)Pout = *(INTYPE *)Pin;                              \
     258      } else {                                                          \
     259        *(OUTTYPE *)Pout = (*(INTYPE *)Pin - Bzero) / Bscale;           \
     260      }                                                                 \
     261      if (!nativeOrder) { SWAP_OP; }}}                                 
     262
     263  SET_VALUES("char",       char, "char", char, SWAP_NONE, 1);
     264  SET_VALUES("byte",       char, "char", char, SWAP_NONE, 1);
     265  SET_VALUES("short",     short, "char", char, SWAP_BYTE, 1);
     266  SET_VALUES("int",         int, "char", char, SWAP_WORD, 1);
     267  SET_VALUES("int64_t", int64_t, "char", char, SWAP_DBLE, 1);
     268  SET_VALUES("float",     float, "char", char, SWAP_WORD, 1);
     269  SET_VALUES("double",   double, "char", char, SWAP_DBLE, 1);
     270
     271  SET_VALUES("char",       char, "byte", char, SWAP_NONE, 1);
     272  SET_VALUES("byte",       char, "byte", char, SWAP_NONE, 1);
     273  SET_VALUES("short",     short, "byte", char, SWAP_BYTE, 1);
     274  SET_VALUES("int",         int, "byte", char, SWAP_WORD, 1);
     275  SET_VALUES("int64_t", int64_t, "byte", char, SWAP_DBLE, 1);
     276  SET_VALUES("float",     float, "byte", char, SWAP_WORD, 1);
     277  SET_VALUES("double",   double, "byte", char, SWAP_DBLE, 1);
     278
     279  SET_VALUES("char",       char, "short", short, SWAP_NONE, 2);
     280  SET_VALUES("byte",       char, "short", short, SWAP_NONE, 2);
     281  SET_VALUES("short",     short, "short", short, SWAP_BYTE, 2);
     282  SET_VALUES("int",         int, "short", short, SWAP_WORD, 2);
     283  SET_VALUES("int64_t", int64_t, "short", short, SWAP_DBLE, 2);
     284  SET_VALUES("float",     float, "short", short, SWAP_WORD, 2);
     285  SET_VALUES("double",   double, "short", short, SWAP_DBLE, 2);
     286
     287  SET_VALUES("char",       char, "int", int, SWAP_NONE, 4);
     288  SET_VALUES("byte",       char, "int", int, SWAP_NONE, 4);
     289  SET_VALUES("short",     short, "int", int, SWAP_BYTE, 4);
     290  SET_VALUES("int",         int, "int", int, SWAP_WORD, 4);
     291  SET_VALUES("int64_t", int64_t, "int", int, SWAP_DBLE, 4);
     292  SET_VALUES("float",     float, "int", int, SWAP_WORD, 4);
     293  SET_VALUES("double",   double, "int", int, SWAP_DBLE, 4);
     294
     295  SET_VALUES("char",       char, "int64_t", int64_t, SWAP_NONE, 8);
     296  SET_VALUES("byte",       char, "int64_t", int64_t, SWAP_NONE, 8);
     297  SET_VALUES("short",     short, "int64_t", int64_t, SWAP_BYTE, 8);
     298  SET_VALUES("int",         int, "int64_t", int64_t, SWAP_WORD, 8);
     299  SET_VALUES("int64_t", int64_t, "int64_t", int64_t, SWAP_DBLE, 8);
     300  SET_VALUES("float",     float, "int64_t", int64_t, SWAP_WORD, 8);
     301  SET_VALUES("double",   double, "int64_t", int64_t, SWAP_DBLE, 8);
     302
     303  SET_VALUES("char",       char, "float", float, SWAP_NONE, 4);
     304  SET_VALUES("byte",       char, "float", float, SWAP_NONE, 4);
     305  SET_VALUES("short",     short, "float", float, SWAP_BYTE, 4);
     306  SET_VALUES("int",         int, "float", float, SWAP_WORD, 4);
     307  SET_VALUES("int64_t", int64_t, "float", float, SWAP_DBLE, 4);
     308  SET_VALUES("float",     float, "float", float, SWAP_WORD, 4);
     309  SET_VALUES("double",   double, "float", float, SWAP_DBLE, 4);
     310
     311  SET_VALUES("char",       char, "double", double, SWAP_NONE, 8);
     312  SET_VALUES("byte",       char, "double", double, SWAP_NONE, 8);
     313  SET_VALUES("short",     short, "double", double, SWAP_BYTE, 8);
     314  SET_VALUES("int",         int, "double", double, SWAP_WORD, 8);
     315  SET_VALUES("int64_t", int64_t, "double", double, SWAP_DBLE, 8);
     316  SET_VALUES("float",     float, "double", double, SWAP_WORD, 8);
     317  SET_VALUES("double",   double, "double", double, SWAP_DBLE, 8);
     318
     319# if (0)
    232320  /** input == char **/
    233321  if (!strcmp (outtype, "char") && !strcmp (intype, "char")) {
    234322    int NbytesIn = 1;
    235323    for (i = 0; i < Nval*Nrow; i++, Pin+=NbytesIn, Pout+=NbytesOut) {
    236       *(char *)Pout = (*(char *)Pin - Bzero) / Bscale;
     324      if (directCopy) { *(char *)Pout = *(char *)Pin; } else {
     325        *(char *)Pout = (*(char *)Pin - Bzero) / Bscale;
     326      }
    237327    }
    238328  }
    239329  if (!strcmp (outtype, "byte") && !strcmp (intype, "char")) {
     330    int NbytesIn = 1;
     331    for (i = 0; i < Nval*Nrow; i++, Pin+=NbytesIn, Pout+=NbytesOut) {
     332      if (directCopy) { *(char *)Pout = *(char *)Pin; } else {
     333        *(char *)Pout = (*(char *)Pin - Bzero) / Bscale;
     334      }
     335    }
     336  }
     337  if (!strcmp (outtype, "short") && !strcmp (intype, "char")) {
     338    int NbytesIn = 1;
     339    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     340      if (directCopy) { *(short *)Pout = *(char *)Pin; } else {
     341        *(short *)Pout = (*(char *)Pin - Bzero) / Bscale;
     342      }
     343# ifdef BYTE_SWAP
     344      if (!nativeOrder) { SWAP_BYTE; }
     345# endif
     346    } 
     347  }
     348  if (!strcmp (outtype, "int") && !strcmp (intype, "char")) {
     349    int NbytesIn = 1;
     350    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     351      *(int *)Pout = (*(char *)Pin - Bzero) / Bscale;
     352# ifdef BYTE_SWAP
     353      if (!nativeOrder) { SWAP_WORD; }
     354# endif
     355    }
     356  }
     357  if (!strcmp (outtype, "int64_t") && !strcmp (intype, "char")) {
     358    int NbytesIn = 1;
     359    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     360      *(int64_t *)Pout = (*(char *)Pin - Bzero) / Bscale;
     361# ifdef BYTE_SWAP
     362      if (!nativeOrder) { SWAP_DBLE; }
     363# endif
     364    }
     365  }
     366  if (!strcmp (outtype, "float") && !strcmp (intype, "char")) {
     367    int NbytesIn = 1;
     368    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     369      *(float *)Pout = (*(char *)Pin - Bzero) / Bscale;
     370# ifdef BYTE_SWAP
     371      if (!nativeOrder) { SWAP_WORD; }
     372# endif
     373    }
     374  }
     375  if (!strcmp (outtype, "double") && !strcmp (intype, "char")) {
     376    int NbytesIn = 1;
     377    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     378      *(double *)Pout = (*(char *)Pin - Bzero) / Bscale;
     379# ifdef BYTE_SWAP
     380      if (!nativeOrder) { SWAP_DBLE; }
     381# endif
     382    }
     383  }
     384
     385  /** input == byte **/
     386  if (!strcmp (outtype, "char") && !strcmp (intype, "byte")) {
    240387    int NbytesIn = 1;
    241388    for (i = 0; i < Nval*Nrow; i++, Pin+=NbytesIn, Pout+=NbytesOut) {
     
    243390    }
    244391  }
    245   if (!strcmp (outtype, "short") && !strcmp (intype, "char")) {
    246     int NbytesIn = 1;
    247     for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
    248       *(short *)Pout = (*(char *)Pin - Bzero) / Bscale;
    249 # ifdef BYTE_SWAP
    250       SWAP_BYTE;
    251 # endif
    252     } 
    253   }
    254   if (!strcmp (outtype, "int") && !strcmp (intype, "char")) {
    255     int NbytesIn = 1;
    256     for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
    257       *(int *)Pout = (*(char *)Pin - Bzero) / Bscale;
    258 # ifdef BYTE_SWAP
    259       SWAP_WORD;
    260 # endif
    261     }
    262   }
    263   if (!strcmp (outtype, "int64_t") && !strcmp (intype, "char")) {
    264     int NbytesIn = 1;
    265     for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
    266       *(int64_t *)Pout = (*(char *)Pin - Bzero) / Bscale;
    267 # ifdef BYTE_SWAP
    268       SWAP_DBLE;
    269 # endif
    270     }
    271   }
    272   if (!strcmp (outtype, "float") && !strcmp (intype, "char")) {
    273     int NbytesIn = 1;
    274     for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
    275       *(float *)Pout = (*(char *)Pin - Bzero) / Bscale;
    276 # ifdef BYTE_SWAP
    277       SWAP_WORD;
    278 # endif
    279     }
    280   }
    281   if (!strcmp (outtype, "double") && !strcmp (intype, "char")) {
    282     int NbytesIn = 1;
    283     for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
    284       *(double *)Pout = (*(char *)Pin - Bzero) / Bscale;
    285 # ifdef BYTE_SWAP
    286       SWAP_DBLE;
    287 # endif
    288     }
    289   }
    290 
    291   /** input == byte **/
    292   if (!strcmp (outtype, "char") && !strcmp (intype, "byte")) {
     392  if (!strcmp (outtype, "byte") && !strcmp (intype, "byte")) {
    293393    int NbytesIn = 1;
    294394    for (i = 0; i < Nval*Nrow; i++, Pin+=NbytesIn, Pout+=NbytesOut) {
     
    296396    }
    297397  }
    298   if (!strcmp (outtype, "byte") && !strcmp (intype, "byte")) {
    299     int NbytesIn = 1;
    300     for (i = 0; i < Nval*Nrow; i++, Pin+=NbytesIn, Pout+=NbytesOut) {
    301       *(char *)Pout = (*(char *)Pin - Bzero) / Bscale;
    302     }
    303   }
    304398  if (!strcmp (outtype, "short") && !strcmp (intype, "byte")) {
    305399    int NbytesIn = 1;
     
    307401      *(short *)Pout = (*(char *)Pin - Bzero) / Bscale;
    308402# ifdef BYTE_SWAP
    309       SWAP_BYTE;
     403      if (!nativeOrder) { SWAP_BYTE; }
    310404# endif
    311405    } 
     
    316410      *(int *)Pout = (*(char *)Pin - Bzero) / Bscale;
    317411# ifdef BYTE_SWAP
    318       SWAP_WORD;
     412      if (!nativeOrder) { SWAP_WORD; }
    319413# endif
    320414    }
     
    325419      *(int64_t *)Pout = (*(char *)Pin - Bzero) / Bscale;
    326420# ifdef BYTE_SWAP
    327       SWAP_DBLE;
     421      if (!nativeOrder) { SWAP_DBLE; }
    328422# endif
    329423    }
     
    334428      *(float *)Pout = (*(char *)Pin - Bzero) / Bscale;
    335429# ifdef BYTE_SWAP
    336       SWAP_WORD;
     430      if (!nativeOrder) { SWAP_WORD; }
    337431# endif
    338432    }
     
    343437      *(double *)Pout = (*(char *)Pin - Bzero) / Bscale;
    344438# ifdef BYTE_SWAP
    345       SWAP_DBLE;
     439      if (!nativeOrder) { SWAP_DBLE; }
    346440# endif
    347441    }
     
    366460      *(short *)Pout = (*(short *)Pin - Bzero) / Bscale;
    367461# ifdef BYTE_SWAP
    368       SWAP_BYTE;
     462      if (!nativeOrder) { SWAP_BYTE; }
    369463# endif
    370464    } 
     
    375469      *(int *)Pout = (*(short *)Pin - Bzero) / Bscale;
    376470# ifdef BYTE_SWAP
    377       SWAP_WORD;
     471      if (!nativeOrder) { SWAP_WORD; }
    378472# endif
    379473    }
     
    384478      *(int64_t *)Pout = (*(short *)Pin - Bzero) / Bscale;
    385479# ifdef BYTE_SWAP
    386       SWAP_DBLE;
     480      if (!nativeOrder) { SWAP_DBLE; }
    387481# endif
    388482    }
     
    393487      *(float *)Pout = (*(short *)Pin - Bzero) / Bscale;
    394488# ifdef BYTE_SWAP
    395       SWAP_WORD;
     489      if (!nativeOrder) { SWAP_WORD; }
    396490# endif
    397491    }
     
    402496      *(double *)Pout = (*(short *)Pin - Bzero) / Bscale;
    403497# ifdef BYTE_SWAP
    404       SWAP_DBLE;
     498      if (!nativeOrder) { SWAP_DBLE; }
    405499# endif
    406500    }
     
    425519      *(short *)Pout = (*(int *)Pin - Bzero) / Bscale;
    426520# ifdef BYTE_SWAP
    427       SWAP_BYTE;
     521      if (!nativeOrder) { SWAP_BYTE; }
    428522# endif
    429523    } 
     
    434528      *(int *)Pout = (*(int *)Pin - Bzero) / Bscale;
    435529# ifdef BYTE_SWAP
    436       SWAP_WORD;
     530      if (!nativeOrder) { SWAP_WORD; }
    437531# endif
    438532    }
     
    443537      *(int64_t *)Pout = (*(int *)Pin - Bzero) / Bscale;
    444538# ifdef BYTE_SWAP
    445       SWAP_DBLE;
     539      if (!nativeOrder) { SWAP_DBLE; }
    446540# endif
    447541    }
     
    452546      *(float *)Pout = (*(int *)Pin - Bzero) / Bscale;
    453547# ifdef BYTE_SWAP
    454       SWAP_WORD;
     548      if (!nativeOrder) { SWAP_WORD; }
    455549# endif
    456550    }
     
    461555      *(double *)Pout = (*(int *)Pin - Bzero) / Bscale;
    462556# ifdef BYTE_SWAP
    463       SWAP_DBLE;
     557      if (!nativeOrder) { SWAP_DBLE; }
    464558# endif
    465559    }
     
    484578      *(short *)Pout = (*(int64_t *)Pin - Bzero) / Bscale;
    485579# ifdef BYTE_SWAP
    486       SWAP_BYTE;
     580      if (!nativeOrder) { SWAP_BYTE; }
    487581# endif
    488582    } 
     
    493587      *(int *)Pout = (*(int64_t *)Pin - Bzero) / Bscale;
    494588# ifdef BYTE_SWAP
    495       SWAP_WORD;
     589      if (!nativeOrder) { SWAP_WORD; }
    496590# endif
    497591    }
     
    502596      *(int64_t *)Pout = (*(int64_t *)Pin - Bzero) / Bscale;
    503597# ifdef BYTE_SWAP
    504       SWAP_DBLE;
     598      if (!nativeOrder) { SWAP_DBLE; }
    505599# endif
    506600    }
     
    511605      *(float *)Pout = (*(int64_t *)Pin - Bzero) / Bscale;
    512606# ifdef BYTE_SWAP
    513       SWAP_WORD;
     607      if (!nativeOrder) { SWAP_WORD; }
    514608# endif
    515609    }
     
    520614      *(double *)Pout = (*(int64_t *)Pin - Bzero) / Bscale;
    521615# ifdef BYTE_SWAP
    522       SWAP_DBLE;
     616      if (!nativeOrder) { SWAP_DBLE; }
    523617# endif
    524618    }
     
    543637      *(short *)Pout = (*(float *)Pin - Bzero) / Bscale;
    544638# ifdef BYTE_SWAP
    545       SWAP_BYTE;
     639      if (!nativeOrder) { SWAP_BYTE; }
    546640# endif
    547641    } 
     
    552646      *(int *)Pout = (*(float *)Pin - Bzero) / Bscale;
    553647# ifdef BYTE_SWAP
    554       SWAP_WORD;
     648      if (!nativeOrder) { SWAP_WORD; }
    555649# endif
    556650    }
     
    561655      *(int64_t *)Pout = (*(float *)Pin - Bzero) / Bscale;
    562656# ifdef BYTE_SWAP
    563       SWAP_DBLE;
     657      if (!nativeOrder) { SWAP_DBLE; }
    564658# endif
    565659    }
     
    570664      *(float *)Pout = (*(float *)Pin - Bzero) / Bscale;
    571665# ifdef BYTE_SWAP
    572       SWAP_WORD;
     666      if (!nativeOrder) { SWAP_WORD; }
    573667# endif
    574668    }
     
    579673      *(double *)Pout = (*(float *)Pin - Bzero) / Bscale;
    580674# ifdef BYTE_SWAP
    581       SWAP_DBLE;
     675      if (!nativeOrder) { SWAP_DBLE; }
    582676# endif
    583677    }
     
    602696      *(short *)Pout = (*(double *)Pin - Bzero) / Bscale;
    603697# ifdef BYTE_SWAP
    604       SWAP_BYTE;
     698      if (!nativeOrder) { SWAP_BYTE; }
    605699# endif
    606700    } 
     
    611705      *(int *)Pout = (*(double *)Pin - Bzero) / Bscale;
    612706# ifdef BYTE_SWAP
    613       SWAP_WORD;
     707      if (!nativeOrder) { SWAP_WORD; }
    614708# endif
    615709    }
     
    620714      *(int64_t *)Pout = (*(double *)Pin - Bzero) / Bscale;
    621715# ifdef BYTE_SWAP
    622       SWAP_DBLE;
     716      if (!nativeOrder) { SWAP_DBLE; }
    623717# endif
    624718    }
     
    629723      *(float *)Pout = (*(double *)Pin - Bzero) / Bscale;
    630724# ifdef BYTE_SWAP
    631       SWAP_WORD;
     725      if (!nativeOrder) { SWAP_WORD; }
    632726# endif
    633727    }
     
    638732      *(double *)Pout = (*(double *)Pin - Bzero) / Bscale;
    639733# ifdef BYTE_SWAP
    640       SWAP_DBLE;
    641 # endif
    642     }
    643   }
     734      if (!nativeOrder) { SWAP_DBLE; }
     735# endif
     736    }
     737  }
     738# endif
    644739
    645740  /* check array space */
     
    683778int gfits_set_table_column (Header *header, FTable *table, char *label, void *data, off_t Nrow) {
    684779
    685   off_t Nx, Ny, nbytes;
     780  off_t Nx, Ny;
    686781  int i, N, Nfields, Nval, Nbytes, Nstart, Nv, Nb;
    687782  char tlabel[80], field[80], format[80], cformat[80], type[16], tmp[16];
     
    713808  if (Ny == 0) {
    714809    Ny = Nrow;
    715     header[0].Naxis[1] = Ny;
    716     gfits_modify (header, "NAXIS2", OFF_T_FMT, 1,  Ny);
    717 
    718     nbytes = gfits_data_size (header);
    719     REALLOCATE (table[0].buffer, char, MAX (nbytes, 1));
    720     bzero (table[0].buffer, nbytes);
    721     table[0].datasize = nbytes;
     810    gfits_set_table_rows (header, table, Ny);
    722811  }
    723812  if (Ny != Nrow) return (FALSE);
  • trunk/Ohana/src/libfits/table/F_table_format.c

    r37039 r38441  
    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;
    53   case  'P':
    54     { *Nbytes = 8;  strcpy (type, "var");    *Nval = 2*Nv;             }
    55     break;
    5652  case  'C':
    5753    { *Nbytes = 8;  strcpy (type, "float");  *Nval = 2*Nv;             }
     
    5955  case  'M':
    6056    { *Nbytes = 16; strcpy (type, "double"); *Nval = 2*Nv;             }
     57    break;
     58  case  'P':
     59    { *Nbytes = 4;  strcpy (type, "var");    *Nval = 2*Nv;             }
     60    break;
     61  case  'Q':
     62    { *Nbytes = 8;  strcpy (type, "var64"); *Nval = 2*Nv;             }
    6163    break;
    6264  default:
     
    8082   C - complex float
    8183   M - complex double
    82    P - var length array descrpt (64 bit)
     84   P - var length array descrpt (32 bit pointer)
     85   Q - var length array descrpt (64 bit pointer)
    8386   
    8487   all can be preceeded by integer N which specified number
    8588   of entries in array
    86    
    8789*/
    8890
  • trunk/Ohana/src/libfits/table/F_table_row.c

    r28241 r38441  
    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}
  • trunk/Ohana/src/libfits/table/F_table_varlength.c

    r28241 r38441  
    22# include <gfitsio.h>
    33
     4// fills the column definition structure for a specific column based on a given table.
    45int gfits_varlength_column_define (FTable *ftable, VarLengthColumn *def, int column) {
    56
     
    1415  snprintf (field, 80, "TFORM%d", column);
    1516  if (!gfits_scan (ftable->header, field, "%s", 1, format)) return (FALSE);
     17
     18  def->column = column;
    1619
    1720  // find and remove the max field length element
     
    4851  if (*p1 == 'I') { def->nbytes = 2;  def->format = *p1; }
    4952  if (*p1 == 'J') { def->nbytes = 4;  def->format = *p1; }
     53  if (*p1 == 'K') { def->nbytes = 8;  def->format = *p1; }
    5054  if (*p1 == 'E') { def->nbytes = 4;  def->format = *p1; }
    5155  if (*p1 == 'D') { def->nbytes = 8;  def->format = *p1; }
     
    5458  if (!def->nbytes) { return (FALSE); }
    5559 
    56   /* scan columns to find column offset */
    57   def->Nstart = 0;
     60  /* scan columns to find column offset for metadata column*/
     61  def->offset = 0;
    5862  for (i = 1; i < column; i++) {
    5963    sprintf (field, "TFORM%d", i);
    6064    gfits_scan (ftable->header, field, "%s", 1, format);
    6165    gfits_bintable_format (format, tmpline, &Nv, &Nb);
    62     def->Nstart += Nv*Nb;
     66    def->offset += Nv*Nb;
    6367  }
    6468
    6569  // heap_start must be long long so file may be very large
    66   if (!gfits_scan (ftable->header, "THEAP", OFF_T_FMT, 1,  &def->heap_start)) {
    67     def->heap_start = ftable->header->Naxis[0]*ftable->header->Naxis[1];
    68   }
    69 
     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
    70114  return TRUE;
    71115}
     
    75119void *gfits_varlength_column_pointer (FTable *ftable, VarLengthColumn *column, off_t row, off_t *length) {
    76120
    77   void *result;
    78   off_t offset, Nx;
    79 
    80121  if ((column->mode != 'P') && (column->mode != 'Q')) abort();
    81122
    82123  // find the values for the specified row
    83124  // the values in the main table for this row and varlength column:
    84   Nx = ftable->header->Naxis[0];
    85 
    86 # define SWAP_WORD \
    87   tmp = pchar[0]; pchar[0] = pchar[3]; pchar[3] = tmp; \
    88   tmp = pchar[1]; pchar[1] = pchar[2]; pchar[2] = tmp;
    89 
    90 # ifdef BYTE_SWAP
    91   char *pchar, tmp;
    92   pchar = &ftable->buffer[row*Nx + column->Nstart];
    93   SWAP_WORD;
    94   pchar = &ftable->buffer[row*Nx + column->Nstart + 4];
    95   SWAP_WORD;
    96 # endif
     125  off_t Nx = ftable->header->Naxis[0];
     126  off_t offset = 0;
    97127
    98128  if (column->mode == 'P') {
    99129    int *ptr;
    100     ptr = (int *) &ftable->buffer[row*Nx + column->Nstart];
     130    ptr = (int *) &ftable->buffer[row*Nx + column->offset];
    101131    *length = ptr[0];
    102132    offset = ptr[1];
    103   } else if (column->mode == 'Q') {
     133  }
     134  if (column->mode == 'Q') {
    104135    off_t *ptr;
    105     ptr = (off_t *) &ftable->buffer[row*Nx + column->Nstart];
     136    ptr = (off_t *) &ftable->buffer[row*Nx + column->offset];
    106137    *length = ptr[0];
    107138    offset = ptr[1];
    108   } else {
    109         // this was actually trapped above
    110         abort();
    111   }
    112 
    113   result = (void *) (ftable->buffer + column->heap_start + offset);
     139  }
     140
     141  // fprintf (stderr, "length: %d, offset: %d\n", (int) *length, (int) offset);
     142
     143  void *result = (void *) (ftable->buffer + ftable->heap_start + offset);
    114144  return result;
    115145}
     146
     147// to generate a compressed image or a compressed table, we need to be able to add data
     148// for varlength columns to the heap.  To start, assume we have defined the cartesian
     149// portion of the table correctly, and are only extending the heap.
     150
     151int gfits_varlength_column_add_data (FTable *table, char *data, off_t Ndata, int row, VarLengthColumn *column) {
     152
     153  // find the current starting point for new data (end of current buffer main data + current heap
     154  off_t heap_offset = gfits_data_min_size (table->header);
     155
     156  // extend the buffer size to add the new data
     157  off_t Nbytes = gfits_data_pad_size (heap_offset + Ndata);
     158  REALLOCATE (table->buffer, char, Nbytes);
     159
     160  char *heap_ptr = table->buffer + heap_offset;
     161
     162  // add the new data to the table buffer
     163  memcpy (heap_ptr, data, Ndata);
     164
     165  // *** now we add the data description to the table ***
     166
     167  if ((column->mode != 'P') && (column->mode != 'Q')) abort();
     168
     169  // find the cell for the specified row & column
     170  // the values in the main table for this row and varlength column:
     171  int Nx = table->header->Naxis[0];
     172
     173  // set the cell values
     174  if (column->mode == 'P') {
     175    int *ptr;
     176    ptr = (int *) &table->buffer[row*Nx + column->offset];
     177    ptr[0] = Ndata;
     178    ptr[1] = heap_offset - table->heap_start;
     179  }
     180  if (column->mode == 'Q') {
     181    off_t *ptr;
     182    ptr = (off_t *) &table->buffer[row*Nx + column->offset];
     183    ptr[0] = Ndata;
     184    ptr[1] = heap_offset - table->heap_start;
     185  }
     186
     187  // update maxlen
     188  // fprintf (stderr, "row: %d, Ndata: %d, maxlen: %d\n", (int) row, (int) Ndata, (int) column->maxlen);
     189  column->maxlen = MAX (column->maxlen, Ndata);
     190
     191  off_t pcount;
     192  gfits_scan (table->header, "PCOUNT", OFF_T_FMT, 1, &pcount);
     193
     194  pcount += Ndata;
     195  gfits_modify (table->header, "PCOUNT", OFF_T_FMT, 1, pcount);
     196  table->header->pcount = pcount;
     197  table->datasize = gfits_data_size (table->header);
     198  myAssert (table->datasize == Nbytes, "inconsistent header and data");
     199
     200  return TRUE;
     201}
     202
     203// update the header TFORM value for this entry
     204int gfits_varlength_column_finish (FTable *ftable, VarLengthColumn *def) {
     205
     206  char field[81];
     207  char format[81];
     208
     209  // grab the value of TFORMn for this column
     210  snprintf (field, 80, "TFORM%d", def->column);
     211  if (!gfits_scan (ftable->header, field, "%s", 1, format)) return FALSE;
     212
     213  // find and update the max field length element
     214  char *p1 = strchr (format, '(');
     215  if (!p1) return (FALSE); // not a valid varlength column -- missing (e_max)
     216 
     217  p1 ++;
     218  *p1 = 0;
     219
     220  snprintf (p1, 80 - (p1 - format), "%d)", def->maxlen);
     221  if (!gfits_modify (ftable->header, field, "%s", 1, format)) return FALSE;
     222
     223  return TRUE;
     224}
  • trunk/Ohana/src/libfits/table/F_write_T.c

    r37807 r38441  
    1414 
    1515  status = fseeko (f, 0LL, SEEK_END);  /* write table to end of file! */
     16  if (status) { perror ("fseeko: "); return (FALSE);  }
    1617  status = gfits_fwrite_table (f, table);
    1718
     
    2627 
    2728  Nbytes = fwrite (table[0].buffer, sizeof(char), table[0].datasize, f);
    28  
    29   if (Nbytes != table[0].datasize) return (FALSE);
     29  if (Nbytes != table[0].datasize) { perror ("fwrite: "); return (FALSE);  }
    3030  return (TRUE);
    3131}       
     
    4545  /* file pointer is at beginning of desired table data */
    4646  start = ftello (f);
     47  if (start < 0) { perror ("ftello: "); return FALSE; }
    4748 
    4849  for (i = 0; i < Nrow; i++) {
     
    5051    fseeko (f, offset, SEEK_SET);
    5152    Nbytes = fwrite (table[0].buffer[i], sizeof (char), Nx, f);
    52     if (Nbytes != Nx) { return (FALSE); }
     53    if (Nbytes != Nx) { perror ("fwrite: "); return (FALSE); }
    5354  }
    5455 
     
    5859
    5960  offset = start + Nx*Ny;
    60   fseeko (f, offset, SEEK_SET);
     61  int status = fseeko (f, offset, SEEK_SET);
     62  if (status) { perror ("fseeko: "); return FALSE; }
     63
    6164  Nbytes = fwrite (pad, sizeof (char), Npad, f);
    62   if (Nbytes != Npad) { return (FALSE); }
     65  if (Nbytes != Npad) { perror ("fwrite: "); return (FALSE); }
    6366  free (pad);
    6467
     
    8487  Nx = ftable[0].header[0].Naxis[0]; // final output table size on disk
    8588  ftable[0].datasize = gfits_data_size (ftable[0].header);
     89  ftable[0].heap_start = gfits_heap_start (ftable[0].header);
    8690
    8791  Nskip = start * Nx;
     
    96100  // cursor must be at start of the table (after table header)
    97101  if (fseeko (f, Nskip, SEEK_CUR)) {
     102    perror ("fseeko: ");
    98103    fprintf (stderr, "can't seek table start\n");
    99104    return (FALSE);
     
    101106
    102107  Nwrite = fwrite (ftable[0].buffer, sizeof (char), Nbytes, f);
    103   if (Nwrite != Nbytes) {
    104     return (FALSE);
    105   }
     108  if (Nwrite != Nbytes) { perror ("fwrite: "); return (FALSE); }
    106109
    107110  if (Ntotal >= Ndisk) {
     
    112115    free (pad);
    113116
    114     if (Nbytes != Npad) return (FALSE);
     117    if (Nbytes != Npad) { perror ("fwrite: "); return (FALSE);  }
    115118  }
    116119
  • trunk/Ohana/src/libfits/table/F_write_TH.c

    r27435 r38441  
    1414 
    1515  status = fseeko (f, 0LL, SEEK_END);  /* write header to end of file! */
     16  if (status) { perror ("fseeko: "); return (FALSE);  }
    1617  status = gfits_fwrite_Theader (f, header);
    1718
     
    2627 
    2728  Nbytes = fwrite (header[0].buffer, sizeof(char), header[0].datasize, f);
     29  if (Nbytes != header[0].datasize) { perror ("fwrite: "); return (FALSE);  }
    2830
    29   if (Nbytes != header[0].datasize) return (FALSE);
    3031  return (TRUE);
    3132}       
Note: See TracChangeset for help on using the changeset viewer.