Skip to content

Commit 79fd8b7

Browse files
committed
Adds support for compressed headers + renamed to fitsheader
1 parent 4593346 commit 79fd8b7

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

Diff for: astropy/io/fits/scripts/fitshead.py renamed to astropy/io/fits/scripts/fitsheader.py

+27-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22
"""
3-
``fitshead`` is a command line script based on astropy.io.fits for printing
3+
``fitsheader`` is a command line script based on astropy.io.fits for printing
44
the header(s) of a FITS file to the standard output.
55
6-
Example uses of fitshead:
6+
Example uses of fitsheader:
77
88
1. Print the header of all the HDUs of a single .fits file:
99
10-
$ fitshead filename.fits
10+
$ fitsheader filename.fits
1111
1212
2. Print the header of the third HDU extension:
1313
14-
$ fitshead --ext 3 filename.fits
14+
$ fitsheader --ext 3 filename.fits
1515
1616
3. Print the header of the extension with EXTNAME='SCI' and EXTVER='2':
1717
18-
$ fitshead --ext "SCI,2" filename.fits
18+
$ fitsheader --ext "SCI,2" filename.fits
1919
2020
4. Print the headers of all fits files in a directory:
2121
22-
$ fitshead *.fits
22+
$ fitsheader *.fits
23+
24+
25+
Note that compressed images (HDUs of type `CompImageHDU`) really have two
26+
headers; a real BINTABLE header to describe the compressed data, and a fake
27+
IMAGE header representing the image that was compressed. Astropy returns the
28+
latter by default. You must supply the "--compressed" option if you require the
29+
header in its compressed form.
2330
"""
2431
from __future__ import (absolute_import, division, print_function,
2532
unicode_literals)
@@ -41,11 +48,12 @@ class HeaderFormatter(object):
4148
filename : str
4249
Path to the FITS file.
4350
"""
44-
def __init__(self, filename):
51+
def __init__(self, filename, compressed=False):
4552
try:
4653
self.hdulist = fits.open(filename)
4754
except IOError as e:
4855
raise FormattingException(e.message)
56+
self.compressed = compressed
4957

5058
def parse(self, extension=None):
5159
"""Returns the FITS file header(s) in a readable format.
@@ -83,7 +91,12 @@ def parse(self, extension=None):
8391
def _get_header(self, hdukey):
8492
"""Returns the `astropy.io.fits.header.Header` object for the HDU."""
8593
try:
86-
return self.hdulist[hdukey].header
94+
if self.compressed:
95+
# In the case of a compressed image, return the header before
96+
# decompression (not the default behavior)
97+
return self.hdulist[hdukey]._header
98+
else:
99+
return self.hdulist[hdukey].header
87100
except IndexError:
88101
raise FormattingException('{0}: Extension #{1} not found.'.
89102
format(self.hdulist.filename(), hdukey))
@@ -116,16 +129,20 @@ def main(args=None):
116129
"By default, all HDU extensions are shown."))
117130
parser.add_argument('-e', '--ext', metavar='hdu',
118131
help='specify the HDU extension number or name')
132+
parser.add_argument('-c', '--compressed', action='store_true',
133+
help='for compressed image data, '
134+
'show the original header which describes '
135+
'the compression rather than the data')
119136
parser.add_argument('filename', nargs='+',
120137
help='path to one or more FITS files to display')
121138
args = parser.parse_args(args)
122139

123140
try:
124141
for filename in args.filename:
125-
print(HeaderFormatter(filename).parse(args.ext))
142+
print(HeaderFormatter(filename, args.compressed).parse(args.ext))
126143
except FormattingException as e:
127144
log.error(e)
128145
except IOError as e:
129146
# A 'Broken pipe' IOError may occur when stdout is closed prematurely,
130-
# eg when using `fitshead file.fits | head`. We let this pass quietly.
147+
# eg when using `fitsheader file.fits | head`. We let this pass.
131148
pass

Diff for: scripts/fitshead

-5
This file was deleted.

Diff for: scripts/fitsheader

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env python
2+
3+
import astropy.io.fits.scripts.fitsheader
4+
5+
astropy.io.fits.scripts.fitsheader.main()

0 commit comments

Comments
 (0)