1
1
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2
2
"""
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
4
4
the header(s) of a FITS file to the standard output.
5
5
6
- Example uses of fitshead :
6
+ Example uses of fitsheader :
7
7
8
8
1. Print the header of all the HDUs of a single .fits file:
9
9
10
- $ fitshead filename.fits
10
+ $ fitsheader filename.fits
11
11
12
12
2. Print the header of the third HDU extension:
13
13
14
- $ fitshead --ext 3 filename.fits
14
+ $ fitsheader --ext 3 filename.fits
15
15
16
16
3. Print the header of the extension with EXTNAME='SCI' and EXTVER='2':
17
17
18
- $ fitshead --ext "SCI,2" filename.fits
18
+ $ fitsheader --ext "SCI,2" filename.fits
19
19
20
20
4. Print the headers of all fits files in a directory:
21
21
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.
23
30
"""
24
31
from __future__ import (absolute_import , division , print_function ,
25
32
unicode_literals )
@@ -41,11 +48,12 @@ class HeaderFormatter(object):
41
48
filename : str
42
49
Path to the FITS file.
43
50
"""
44
- def __init__ (self , filename ):
51
+ def __init__ (self , filename , compressed = False ):
45
52
try :
46
53
self .hdulist = fits .open (filename )
47
54
except IOError as e :
48
55
raise FormattingException (e .message )
56
+ self .compressed = compressed
49
57
50
58
def parse (self , extension = None ):
51
59
"""Returns the FITS file header(s) in a readable format.
@@ -83,7 +91,12 @@ def parse(self, extension=None):
83
91
def _get_header (self , hdukey ):
84
92
"""Returns the `astropy.io.fits.header.Header` object for the HDU."""
85
93
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
87
100
except IndexError :
88
101
raise FormattingException ('{0}: Extension #{1} not found.' .
89
102
format (self .hdulist .filename (), hdukey ))
@@ -116,16 +129,20 @@ def main(args=None):
116
129
"By default, all HDU extensions are shown." ))
117
130
parser .add_argument ('-e' , '--ext' , metavar = 'hdu' ,
118
131
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' )
119
136
parser .add_argument ('filename' , nargs = '+' ,
120
137
help = 'path to one or more FITS files to display' )
121
138
args = parser .parse_args (args )
122
139
123
140
try :
124
141
for filename in args .filename :
125
- print (HeaderFormatter (filename ).parse (args .ext ))
142
+ print (HeaderFormatter (filename , args . compressed ).parse (args .ext ))
126
143
except FormattingException as e :
127
144
log .error (e )
128
145
except IOError as e :
129
146
# 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.
131
148
pass
0 commit comments