Skip to content

Commit d3fc78c

Browse files
committed
Merge branch 'pyfits/astropy' into astropy
Conflicts: README.txt Conflicts: astropy/io/fits/src/compressionmodule.c
1 parent 7b43f33 commit d3fc78c

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

‎astropy/io/fits/fitsrec.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,9 @@ def get_pdata(dtype, count):
391391
buff = np.fromstring(buff, dtype=np.uint8)
392392

393393
def get_pdata(dtype, count):
394-
slc = slice(_offset, _offset+count)
394+
dtype = np.dtype(dtype)
395+
nbytes = count * dtype.itemsize
396+
slc = slice(_offset, _offset + nbytes)
395397
return buff[slc].view(dtype=dtype)
396398

397399
if recformat.dtype == 'a':

‎astropy/io/fits/hdu/compressed.py‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
DEFAULT_BLOCK_SIZE = 32
3535
DEFAULT_BYTE_PIX = 4
3636

37+
# CFITSIO version-specific features
38+
if COMPRESSION_SUPPORTED:
39+
CFITSIO_SUPPORTS_GZIPDATA = compression.CFITSIO_VERSION >= 3.28
40+
3741

3842
class CompImageHeader(Header):
3943
"""
@@ -462,7 +466,7 @@ def updateHeaderData(self, image_header,
462466
# this behavior so the only way to determine which behavior will
463467
# be employed is via the CFITSIO version
464468

465-
if compression.cfitsio_version >= 3.28:
469+
if CFITSIO_SUPPORTS_GZIPDATA:
466470
ttype2 = 'GZIP_COMPRESSED_DATA'
467471
# The required format for the GZIP_COMPRESSED_DATA is actually
468472
# missing from the standard docs, but CFITSIO suggests it
@@ -770,7 +774,8 @@ def updateHeaderData(self, image_header,
770774
bytepix = DEFAULT_BYTE_PIX
771775

772776
self._header.set('ZVAL2', bytepix,
773-
'bytes per pixel (1, 2, 4, or 8)', after='ZNAME2')
777+
'bytes per pixel (1, 2, 4, or 8)',
778+
after='ZNAME2')
774779
afterCard = 'ZVAL2'
775780
idx = 3
776781
elif compressionType == 'HCOMPRESS_1':
@@ -942,6 +947,7 @@ def compData(self):
942947
# data) from the file, if there is any.
943948
compData = super(BinTableHDU, self).data
944949
if isinstance(compData, np.rec.recarray):
950+
del self.data
945951
return compData
946952
else:
947953
# This will actually set self.compData with the pre-allocated space
@@ -1255,6 +1261,11 @@ def updateCompressedData(self):
12551261
if dataspan < BLOCK_SIZE:
12561262
# We must a full FITS block at a minimum
12571263
dataspan = BLOCK_SIZE
1264+
else:
1265+
# Still make sure to pad out to a multiple of 2880 byte blocks
1266+
# otherwise CFITSIO can get read errors when it tries to read
1267+
# a partial block that goes past the end of the file
1268+
dataspan += _pad_length(dataspan)
12581269
self.compData = np.empty((dataspan,), dtype=np.byte)
12591270
self.compData[:tbsize] = 0
12601271

@@ -1353,7 +1364,8 @@ def updateHeader(self):
13531364
bytepix = DEFAULT_BYTE_PIX
13541365

13551366
self._header.set('ZVAL2', bytepix,
1356-
'bytes per pixel (1, 2, 4, or 8)', after='ZNAME2')
1367+
'bytes per pixel (1, 2, 4, or 8)',
1368+
after='ZNAME2')
13571369

13581370
def scale(self, type=None, option='old', bscale=1, bzero=0):
13591371
"""

‎astropy/io/fits/src/compressionmodule.c‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
#include <numpy/arrayobject.h>
101101
#include <fitsio2.h>
102102
#include <string.h>
103+
#include "compressionmodule.h"
103104

104105
/* Some defines for Python3 support--bytes objects should be used where */
105106
/* strings were previously used */
@@ -525,17 +526,21 @@ void configure_compression(fitsfile* fileptr, PyObject* header) {
525526
// BLANK in the header
526527
Fptr->cn_zblank = Fptr->cn_zzero = Fptr->cn_zscale = -1;
527528
Fptr->cn_uncompressed = 0;
529+
#ifdef CFITSIO_SUPPORTS_GZIPDATA
528530
Fptr->cn_gzip_data = 0;
531+
#endif
529532

530533
// Check for a ZBLANK, ZZERO, ZSCALE, and
531534
// UNCOMPRESSED_DATA/GZIP_COMPRESSED_DATA columns in the compressed data
532535
// table
533536
for (idx = 0; idx < tfields; idx++) {
534537
if (0 == strncmp(columns[idx].ttype, "UNCOMPRESSED_DATA", 18)) {
535538
Fptr->cn_uncompressed = idx + 1;
539+
#ifdef CFITSIO_SUPPORTS_GZIPDATA
536540
} else if (0 == strncmp(columns[idx].ttype,
537541
"GZIP_COMPRESSED_DATA", 21)) {
538542
Fptr->cn_gzip_data = idx + 1;
543+
#endif
539544
} else if (0 == strncmp(columns[idx].ttype, "ZSCALE", 7)) {
540545
Fptr->cn_zscale = idx + 1;
541546
} else if (0 == strncmp(columns[idx].ttype, "ZZERO", 6)) {
@@ -799,7 +804,6 @@ PyObject* compression_compress_hdu(PyObject* self, PyObject* args)
799804

800805
indata = (PyArrayObject*) PyObject_GetAttrString(hdu, "data");
801806

802-
// Test values
803807
fits_write_img(fileptr, datatype, 1, PyArray_SIZE(indata), indata->data,
804808
&status);
805809
if (status != 0) {
@@ -847,6 +851,9 @@ PyObject* compression_compress_hdu(PyObject* self, PyObject* args)
847851
}
848852
Py_XDECREF(indata);
849853

854+
// Clear any messages remaining in CFITSIO's error stack
855+
fits_clear_errmsg();
856+
850857
return retval;
851858
}
852859

@@ -921,6 +928,9 @@ PyObject* compression_decompress_hdu(PyObject* self, PyObject* args)
921928
}
922929
PyMem_Free(znaxis);
923930

931+
// Clear any messages remaining in CFITSIO's error stack
932+
fits_clear_errmsg();
933+
924934
return (PyObject*) outdata;
925935
}
926936

@@ -946,6 +956,7 @@ PyObject* compression_calc_max_elem(PyObject* self, PyObject* args) {
946956
/* CFITSIO version float as returned by fits_get_version() */
947957
static double cfitsio_version;
948958

959+
949960
void compression_module_init(PyObject* module) {
950961
/* Python version-indendependent initialization routine for the
951962
compression module */
@@ -961,7 +972,7 @@ void compression_module_init(PyObject* module) {
961972
cfitsio_version = floor((1000 * version_tmp + 0.5)) / 1000;
962973

963974
tmp = PyFloat_FromDouble(cfitsio_version);
964-
PyObject_SetAttrString(module, "cfitsio_version", tmp);
975+
PyObject_SetAttrString(module, "CFITSIO_VERSION", tmp);
965976
Py_XDECREF(tmp);
966977

967978
/* Needed to use Numpy routines */

0 commit comments

Comments
 (0)