Skip to content

Commit 313c85b

Browse files
committed
Merge pull request #1 from adrn/fits-header
Add jonathan's examples to the repo
2 parents 0145783 + 7500dd3 commit 313c85b

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import astropy.io.fits as fits
2+
data, hdr = fits.getdata("inputfile.fits",header=True)
3+
hdr['OBJECT'] = "M31"
4+
fits.writeto('outputfile.fits',data,hdr)
5+
6+
data,hdr = fits.getdata("inputfile.fits",ext=2,header=True)
7+
fits.writeto('outputfile.fits',data,hdr,clobber=True)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
.. _edit_fits_header:
2+
3+
Tutorial : Edit a FITS header
4+
-----------------------------
5+
6+
Here is how to edit a FITS header by hand. In this example we're going
7+
to change the header so that the correct object is listed.
8+
9+
This tutorial uses ``astropy.io.fits``, which was formerly released
10+
separately as ``pyfits``. If you have used `pyfits` to manipulate
11+
FITS files then you may already be familiar with this functionality.
12+
13+
``astropy.io.fits`` provides a lot of flexibility for reading FITS
14+
files and headers, but most of the time the convenience functions are
15+
the easiest way to access the data::
16+
17+
from astropy.io import fits
18+
data, header = fits.getdata("inputfile.fits", header=True)
19+
20+
The import line simply imports the `fits` subpackage into our local
21+
namespace and allows us to access the functions and classes as
22+
`fits.name_of_function()`. For example, to access the `getdata()`
23+
function, we _don't_ have to do `astropy.io.fits.getdata()` and can
24+
instead simple use `fits.getdata()`.
25+
26+
You may run across documentation or tutorials that use the name
27+
`pyfits`. Such examples will begin with `import pyfits` and then
28+
the command `fits.getdata()` (for example) would be written as
29+
`pyfits.getdata()`. Be careful of the naming convention when
30+
following other examples.
31+
32+
``fits.getdata()`` reads the data and header from a FITS file on disk
33+
(inputfile.fits, in this case). Note that you have to specify the
34+
keyword argument `header=True` to get both the header and the actual
35+
data array. There is also a dedicated function for reading just the
36+
header (`getheader('filename.fits',hdu_number)`), but `getdata()` can
37+
get both the data and the header, so it is a useful command to
38+
remember. Since the primary HDU of a FITS file must contain image data,
39+
the data is now stored in a ``numpy`` array. The header is stored in an
40+
object that acts like a standard Python dictionary.
41+
42+
Now let's change the header to give it the correct object::
43+
44+
hdr['OBJECT'] = "M31"
45+
46+
Finally, we have to write out the FITS file. Again, the convenience
47+
function for this is the most useful command to remember::
48+
49+
fits.writeto('outputfile.fits',data,hdr)
50+
51+
That's it; you're done.
52+
53+
Two common more complicated cases are worth mentioning (if your needs
54+
are more complex you should consult the full documentation).
55+
56+
The first complication is that the FITS file you're examining and
57+
editing might have multiple HDU's (extensions), in which case you can
58+
specify the extension like this::
59+
60+
data,hdr = fits.getdata("inputfile.fits",ext=2,header=True)
61+
62+
This will get you the data and header associated with the 2nd extension
63+
in the FITS file. Without specifiying a number, getdata() will get the
64+
0th extension (equivalent to saying `ext=0`).
65+
66+
The second complication is if you want to overwrite an existing FITS
67+
file. By default, writeto() won't let you do this, and you need to
68+
explicitly give it persmission::
69+
70+
fits.writeto('outputfile.fits',data,hdr,clobber=True)
71+
72+
The complete code for the above example is included below for reference.
73+
74+
Complete Code for Example
75+
=========================
76+
.. literalinclude:: edit-fits-header.py
77+
:linenos:
78+
:language: python

0 commit comments

Comments
 (0)