Skip to content

Commit 00b4769

Browse files
committed
Merge branch 'fits/astropy' into astropy
This merges up through r1927 from pyfits' trunk in SVN. Closes the following issues in Astropy: Closes astropy#339 astropy.io.fits.Header totextfile and Header.__init__ disagree on 'endcard' convention Closes astropy#318 Move io.fits external C dependencies into cextern Conflicts: README.txt astropy/io/fits/src/compress.c astropy/io/fits/src/crc32.h astropy/io/fits/src/deflate.h astropy/io/fits/src/fits_hcompress.c astropy/io/fits/src/fits_hdecompress.c astropy/io/fits/src/fitsio.c astropy/io/fits/src/fitsio.h astropy/io/fits/src/inffast.c astropy/io/fits/src/inffast.h astropy/io/fits/src/inffixed.h astropy/io/fits/src/inflate.h astropy/io/fits/src/inftrees.c astropy/io/fits/src/inftrees.h astropy/io/fits/src/pliocomp.c astropy/io/fits/src/quantize.c astropy/io/fits/src/ricecomp.c astropy/io/fits/src/ricecomp.h astropy/io/fits/src/trees.c astropy/io/fits/src/trees.h astropy/io/fits/src/zlib.c astropy/io/fits/src/zlib.h astropy/io/fits/src/zutil.h cextern/cfitsio/crc32.h cextern/cfitsio/deflate.h cextern/cfitsio/fits_hcompress.c cextern/cfitsio/fits_hdecompress.c cextern/cfitsio/inffast.c cextern/cfitsio/inffixed.h cextern/cfitsio/inftrees.c cextern/cfitsio/pliocomp.c cextern/cfitsio/quantize.c cextern/cfitsio/ricecomp.c cextern/cfitsio/trees.c cextern/cfitsio/trees.h cextern/cfitsio/zutil.h setup.cfg src/crc32.h src/deflate.h src/fits_hcompress.c src/fits_hdecompress.c src/inffast.c src/inffixed.h src/inftrees.c src/pliocomp.c src/quantize.c src/ricecomp.c src/trees.c src/trees.h src/zutil.h tox.ini Conflicts: astropy/io/fits/diff.py astropy/io/fits/hdu/base.py astropy/io/fits/hdu/compressed.py astropy/io/fits/hdu/image.py astropy/io/fits/src/compressionmodule.c astropy/io/fits/tests/cfitsio_verify.c astropy/io/fits/tests/test_core.py astropy/io/fits/tests/test_header.py astropy/io/fits/tests/test_image.py astropy/io/fits/tests/test_uint.py
1 parent 1fd58c4 commit 00b4769

128 files changed

Lines changed: 138220 additions & 14753 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎astropy/io/fits/card.py‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,11 @@ def pop(self, index=-1):
229229

230230
@deprecated('3.1', alternative=':meth:`Header.index`')
231231
def index(self, card):
232-
return self._header.cards.index(card)
232+
return self._header._cards.index(card)
233233

234234
@deprecated('3.1', alternative=':meth:`Header.count`')
235235
def count(self, card):
236-
return self._header.cards.count(card)
236+
return self._header._cards.count(card)
237237

238238
@deprecated('3.1', alternative=':meth:`Header.index`', pending=False)
239239
def index_of(self, key, backward=False):
@@ -382,13 +382,13 @@ class Card(_Verify):
382382
_rvkc_identifier = r'[a-zA-Z_]\w*'
383383
_rvkc_field = _rvkc_identifier + r'(\.\d+)?'
384384
_rvkc_field_specifier_s = r'%s(\.%s)*' % ((_rvkc_field,) * 2)
385-
_rvkc_field_specifier_val = (r'(?P<keyword>%s): (?P<val>%s\s*)' %
385+
_rvkc_field_specifier_val = (r'(?P<keyword>%s): (?P<val>%s)' %
386386
(_rvkc_field_specifier_s, _numr_FSC))
387-
_rvkc_keyword_val = r'\'%s\'' % _rvkc_field_specifier_val
387+
_rvkc_keyword_val = r'\'(?P<rawval>%s)\'' % _rvkc_field_specifier_val
388388
_rvkc_keyword_val_comm = (r' +%s *(/ *(?P<comm>[ -~]*))?$' %
389389
_rvkc_keyword_val)
390390

391-
_rvkc_field_specifier_val_RE = re.compile(_rvkc_field_specifier_val)
391+
_rvkc_field_specifier_val_RE = re.compile(_rvkc_field_specifier_val + '$')
392392

393393
# regular expression to extract the key and the field specifier from a
394394
# string that is being used to index into a card list that contains
@@ -412,6 +412,7 @@ def __init__(self, keyword=None, value=None, comment=None, **kwargs):
412412
self._keyword = None
413413
self._value = None
414414
self._comment = None
415+
self._rawvalue = None
415416
self._image = None
416417

417418
# This attribute is set to False when creating the card from a card
@@ -596,6 +597,12 @@ def value(self):
596597
raise AttributeError('Values cannot be deleted from record-valued '
597598
'keyword cards')
598599

600+
@property
601+
def rawvalue(self):
602+
if self._rawvalue is None and self.value:
603+
self._rawvalue = self.value
604+
return self._rawvalue
605+
599606
@property
600607
def comment(self):
601608
"""Get the comment attribute from the card image if not already set."""
@@ -774,18 +781,23 @@ def _check_if_rvkc(self, *args):
774781
if eq_idx < 0 or eq_idx > 9:
775782
return False
776783
keyword, rest = image.split('=', 1)
784+
if keyword in self._commentary_keywords:
785+
return False
777786
match = self._rvkc_keyword_val_comm_RE.match(rest)
778787
if match:
779788
field_specifier = match.group('keyword')
780789
self._keyword = '.'.join((keyword.strip().upper(),
781790
field_specifier))
782791
self._field_specifier = field_specifier
783792
self._value = _int_or_float(match.group('val'))
793+
self._rawvalue = match.group('rawval')
784794
return True
785795
elif len(args) == 2:
786796
keyword, value = args
787797
if not isinstance(keyword, basestring):
788798
return False
799+
if keyword in self._commentary_keywords:
800+
return False
789801
match = self._rvkc_keyword_name_RE.match(keyword)
790802
if match and isinstance(value, (int, float)):
791803
field_specifier = match.group('field_specifier')
@@ -802,6 +814,7 @@ def _check_if_rvkc(self, *args):
802814
field_specifier))
803815
self._field_specifier = field_specifier
804816
self._value = _int_or_float(match.group('val'))
817+
self._rawvalue = value
805818
return True
806819

807820
def _parse_keyword(self):

‎astropy/io/fits/column.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ def _makep(input, desp_output, format, nrows=None):
988988
if format.dtype == 'a':
989989
_nbytes = 1
990990
else:
991-
_nbytes = np.array([], dtype=np.typeDict[format.dtype]).itemsize
991+
_nbytes = np.array([], dtype=format.dtype).itemsize
992992

993993
for idx in range(nrows):
994994
if idx < len(input):

‎astropy/io/fits/diff.py‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def _report(self):
301301
if self.ignore_comments:
302302
ignore_comments = ' '.join(sorted(self.ignore_comments))
303303
self._writeln(u' Keyword(s) whose comments are not to be compared:'
304-
'\n%s' % wrapper.fill(ignore_keywords))
304+
'\n%s' % wrapper.fill(ignore_comments))
305305
if self.ignore_fields:
306306
ignore_fields = ' '.join(sorted(self.ignore_fields))
307307
self._writeln(u' Table column(s) not to be compared:\n%s' %
@@ -1025,6 +1025,11 @@ def _diff(self):
10251025
self.diff_rows = (len(self.a), len(self.b))
10261026
return
10271027

1028+
# If the tables contain no rows there's no data to compare, so we're
1029+
# done at this point. (See ticket #178)
1030+
if len(self.a) == len(self.b) == 0:
1031+
return
1032+
10281033
# Like in the old fitsdiff, compare tables on a column by column basis
10291034
# The difficulty here is that, while FITS column names are meant to be
10301035
# case-insensitive, PyFITS still allows, for the sake of flexibility,
@@ -1119,10 +1124,10 @@ def _report(self):
11191124
ind=self._indent + 1)
11201125

11211126
if self.diff_rows:
1122-
self._writeln(' Table rows differ:')
1123-
self._writeln(' a: %s' % self.diff_rows[0])
1124-
self._writeln(' b: %s' % self.diff_rows[1])
1125-
self._writeln(' No further data comparison performed.')
1127+
self._writeln(u' Table rows differ:')
1128+
self._writeln(u' a: %s' % self.diff_rows[0])
1129+
self._writeln(u' b: %s' % self.diff_rows[1])
1130+
self._writeln(u' No further data comparison performed.')
11261131
return
11271132

11281133
if not self.diff_values:
@@ -1177,7 +1182,7 @@ def report_diff_values(fileobj, a, b, ind=0):
11771182
report_diff_values(fileobj, a[idx], b[idx], ind=ind + 1)
11781183

11791184
if num_diffs:
1180-
fileobj.write(indent(' ...and at %d more indices.\n' %
1185+
fileobj.write(indent(u' ...and at %d more indices.\n' %
11811186
(num_diffs - 3), ind))
11821187
return
11831188

@@ -1206,7 +1211,7 @@ def report_diff_keyword_attr(fileobj, attr, diffs, keyword, ind=0):
12061211
dup = ''
12071212
else:
12081213
dup = '[%d]' % (idx + 1)
1209-
fileobj.write(indent(' Keyword %-8s%s has different %s:\n' %
1214+
fileobj.write(indent(u' Keyword %-8s%s has different %s:\n' %
12101215
(keyword, dup, attr), ind))
12111216
report_diff_values(fileobj, val[0], val[1], ind=ind + 1)
12121217

‎astropy/io/fits/fitsrec.py‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def __new__(subtype, input):
178178
self._nfields = len(self.dtype.names)
179179
self._convert = [None] * len(self.dtype.names)
180180
self._heapoffset = 0
181+
self._heapsize = 0
181182
self._file = None
182183
self._buffer = None
183184
self._coldefs = None
@@ -193,6 +194,7 @@ def __array_finalize__(self, obj):
193194
if isinstance(obj, FITS_rec):
194195
self._convert = obj._convert
195196
self._heapoffset = obj._heapoffset
197+
self._heapsize = obj._heapsize
196198
self._file = obj._file
197199
self._buffer = obj._buffer
198200
self._coldefs = obj._coldefs
@@ -207,6 +209,7 @@ def __array_finalize__(self, obj):
207209
self._convert = [None] * len(obj.dtype.names)
208210

209211
self._heapoffset = getattr(obj, '_heapoffset', 0)
212+
self._heapsize = getattr(obj, '_heapsize', 0)
210213
self._file = getattr(obj, '_file', None)
211214
self._buffer = getattr(obj, '_buffer', None)
212215

@@ -362,6 +365,7 @@ def field(self, key):
362365
self._get_scale_factors(indx)
363366

364367
# for P format
368+
buff = None
365369
if isinstance(recformat, _FormatP):
366370
dummy = _VLF([None] * len(self), dtype=recformat.dtype)
367371
for i in range(len(self)):
@@ -373,10 +377,22 @@ def get_pdata(dtype, count):
373377
return _array_from_file(self._file, dtype=dtype,
374378
count=count, sep='')
375379
else: # There must be a _buffer or something is wrong
376-
buff = self._buffer[_offset:]
380+
# Sometimes the buffer is already a Numpy array; in
381+
# particular this can occur in compressed HDUs.
382+
# Hypothetically other cases as well.
383+
if buff is None:
384+
buff = self._buffer
385+
if not isinstance(buff, np.ndarray):
386+
# Go ahead and great a single ndarray from the
387+
# buffer if it is not already one; we will then
388+
# take slices from it. This is more efficient than
389+
# the previous approach that created separate
390+
# arrays for each VLA.
391+
buff = np.fromstring(buff, dtype=np.uint8)
392+
377393
def get_pdata(dtype, count):
378-
return np.fromstring(buff, dtype=dtype,
379-
count=count, sep='')
394+
slc = slice(_offset, _offset+count)
395+
return buff[slc].view(dtype=dtype)
380396

381397
if recformat.dtype == 'a':
382398
count = field[i, 0]
@@ -546,7 +562,6 @@ def _scale_back(self):
546562
widths.append(f[1])
547563
loc.append(loc[-1] + super(FITS_rec, self).field(idx).itemsize)
548564

549-
self._heapsize = 0
550565
for indx in range(len(self.dtype.names)):
551566
recformat = self._coldefs._recformats[indx]
552567
field = super(FITS_rec, self).field(indx)
@@ -564,10 +579,20 @@ def _scale_back(self):
564579
# add the location offset of the heap area for each
565580
# variable length column
566581
if isinstance(recformat, _FormatP):
582+
# Reset the heapsize and recompute it starting from the first P
583+
# column
584+
if indx == 0:
585+
self._heapsize = 0
586+
567587
field[:] = 0 # reset
568588
npts = map(len, self._convert[indx])
589+
590+
# Irritatingly, this can return a different dtype than just
591+
# doing np.dtype(recformat.dtype); but this returns the results
592+
# that we want. For example if recformat.dtype is 'a' we want
593+
# an array of characters.
594+
dtype = np.array([], dtype=recformat.dtype).dtype
569595
field[:len(npts), 0] = npts
570-
dtype = np.array([], dtype=recformat.dtype)
571596
field[1:, 1] = (np.add.accumulate(field[:-1, 0]) *
572597
dtype.itemsize)
573598
field[:, 1][:] += self._heapsize

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

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def __init__(self, data=None, header=None, *args, **kwargs):
113113
elif 'CHECKSUM' in self._header:
114114
self._output_checksum = True
115115

116-
self.name = ''
116+
self._name = ''
117117

118118
@property
119119
def header(self):
@@ -123,14 +123,24 @@ def header(self):
123123
def header(self, value):
124124
self._header = value
125125

126+
@property
127+
def name(self):
128+
return self._name
129+
130+
@name.setter
131+
def name(self, value):
132+
if not isinstance(value, basestring):
133+
raise TypeError("'name' attribute must be a string")
134+
self._name = value
135+
126136
@property
127137
def is_image(self):
128138
return (
129139
self.name == 'PRIMARY' or
130-
('XTENSION' in self.header and
131-
(self.header['XTENSION'] == 'IMAGE' or
132-
(self.header['XTENSION'] == 'BINTABLE' and
133-
'ZIMAGE' in self.header and self.header['ZIMAGE'] == True))))
140+
('XTENSION' in self._header and
141+
(self._header['XTENSION'] == 'IMAGE' or
142+
(self._header['XTENSION'] == 'BINTABLE' and
143+
'ZIMAGE' in self._header and self._header['ZIMAGE'] == True))))
134144

135145
@property
136146
def _data_loaded(self):
@@ -689,6 +699,34 @@ class _ValidHDU(_BaseHDU, _Verify):
689699
Base class for all HDUs which are not corrupted.
690700
"""
691701

702+
def __init__(self, data=None, header=None, name=None, **kwargs):
703+
super(_ValidHDU, self).__init__(data=data, header=header)
704+
if name is not None:
705+
self.name = name
706+
707+
if header and not hasattr(self, '_extver'):
708+
self._extver = header.get('EXTVER', 1)
709+
710+
@property
711+
def name(self):
712+
# Convert the value to a string to be flexible in some pathological
713+
# cases (see ticket #96)
714+
if self._header and 'EXTNAME' in self._header:
715+
self._name = str(self._header['EXTNAME'])
716+
return self._name
717+
718+
@name.setter
719+
def name(self, value):
720+
if not isinstance(value, basestring):
721+
raise TypeError("'name' attribute must be a string")
722+
if not EXTENSION_NAME_CASE_SENSITIVE():
723+
value = value.upper()
724+
if self._header:
725+
if 'EXTNAME' in self._header:
726+
self._header['EXTNAME'] = value
727+
else:
728+
self._header.append(('EXTNAME', value, 'extension name'))
729+
692730
@classmethod
693731
def match_header(cls, header):
694732
"""
@@ -832,7 +870,7 @@ def update_ext_name(self, value, comment=None, before=None,
832870
if 'extname' in self._header and savecomment:
833871
comment = None
834872

835-
self._header.set('extname', value, comment, before, after)
873+
self._header.set('EXTNAME', value, comment, before, after)
836874
self.name = value
837875

838876
def update_ext_version(self, value, comment=None, before=None,
@@ -924,6 +962,19 @@ def fix(self=self, keyword=keyword):
924962
self.run_option(option=option, err_text=err_text,
925963
fix=fix, fix_text="Deleted."))
926964

965+
966+
# Verify that the EXTNAME keyword exists and is a string
967+
if 'EXTNAME' in self._header:
968+
if not isinstance(self._header['EXTNAME'], basestring):
969+
err_text = 'The EXTNAME keyword must have a string value.'
970+
fix_text = 'Converted the EXTNAME keyword to a string value.'
971+
972+
def fix(header=self._header):
973+
header['EXTNAME'] = str(header['EXTNAME'])
974+
975+
errs.append(self.run_option(option, err_text=err_text,
976+
fix_text=fix_text, fix=fix))
977+
927978
# verify each card
928979
for card in self._header.cards:
929980
errs.append(card._verify(option))
@@ -1402,38 +1453,6 @@ class ExtensionHDU(_ValidHDU):
14021453

14031454
_extension = ''
14041455

1405-
def __init__(self, data=None, header=None, name=None, **kwargs):
1406-
super(ExtensionHDU, self).__init__(data=data, header=header)
1407-
if header:
1408-
if name is None:
1409-
if not self.name and 'EXTNAME' in header:
1410-
self.name = header['EXTNAME']
1411-
else:
1412-
self.name = name
1413-
1414-
if not hasattr(self, '_extver'):
1415-
if 'EXTVER' in header:
1416-
self._extver = header['EXTVER']
1417-
else:
1418-
self._extver = 1
1419-
1420-
def __setattr__(self, attr, value):
1421-
"""
1422-
Set an HDU attribute.
1423-
"""
1424-
1425-
if attr == 'name' and value:
1426-
if not isinstance(value, basestring):
1427-
raise TypeError("'name' attribute must be a string")
1428-
if not EXTENSION_NAME_CASE_SENSITIVE():
1429-
value = value.upper()
1430-
if 'EXTNAME' in self._header:
1431-
self._header['EXTNAME'] = value
1432-
else:
1433-
self._header.append(('EXTNAME', value, 'extension name'))
1434-
1435-
super(ExtensionHDU, self).__setattr__(attr, value)
1436-
14371456
@classmethod
14381457
def match_header(cls, header):
14391458
"""
@@ -1469,6 +1488,7 @@ def _verify(self, option='warn'):
14691488
0, option, errs)
14701489
self.req_cards('GCOUNT', naxis + 4, lambda v: (_is_int(v) and v == 1),
14711490
1, option, errs)
1491+
14721492
return errs
14731493
# For backwards compatilibity, though this needs to be deprecated
14741494
# TODO: Mark this as deprecated

0 commit comments

Comments
 (0)