Montage Montage is an astronomical image toolkit with components for reprojection, background matching, coaddition and visualization of FITS files. It can be used as a set of command-line tools (Linux, OS X and Windows), C library calls (Linux and OS X) and as Python binary extension modules.
The Montage source is written in ANSI-C and code can be downloaded from GitHub ( https://github.com/Caltech-IPAC/Montage ). The Python package can be installed from PyPI ("</i>pip install MontagePy"). The package has no external dependencies. See http://montage.ipac.caltech.edu/ for details on the design and applications of Montage.
FITS headers are both quite straightforward and fairly complicated. The structure is straightforward, consisting of a sequence of 80-character "card" images in blocks of 2880 bytes at the beginning of the file and, in the case of multi-HDU files, at the beginning of each data block. It is complicated because it can contain hundreds of parameters, all in a flat namespace.
On top of this, there is a specific subset of these parameters that define the structure of the data (row, columns, datatype) and the image projection and coordinate system. Small errors, especially in this last subset, can make the file unusable. When this happens, frequently the best way to fix things is to simply edit the header.
But since the header is in some respects binary (it is composed of ASCII characters but there are no newlines and the overall size must be an integer multiple of 2880 bytes), it can't be edited directly.
Instead, we have two Montage modules: mGetHdr to extract the FITS header into an editable text file and mPutHdr to replace the FITS header with new data from an (edited) text file version, padding everything as needed.
from MontagePy.main import mGetHdr, mPutHdr, mViewer
help(mGetHdr)
We have a file whose header has a simple mistake: The CTYPE1 variable has an incorrect syntax. CTYPE1 and CTYPE2 define both the coordinate system and projection. The first four characters specify the coordinate "axes" (e.g., "GLON", "ELAT") and the second four the projection (e.g., "-TAN", "-AIT"). For Equatorial coordinates in a Gnomonic projection the correct values for CTYPE1 and CTYPE2 are "RA---TAN" and "DEC--TAN" (since the system name here are less than four characters they were padded with trailing "-" characters.
People often make the mistake of using "RA--TAN" instead of "RA---TAN" (one too few dashes) and this breaks the FITS reading library. Similarly, they might have the wrong coordinate system altogether or the wrong projection or might have a mistake in the reference coordinates for the image, etc.
To fix our image, first extract the header:
rtn = mGetHdr("other/bad_hdr.fits", "work/fix_hdr.hdr")
print(rtn)
Take a look:
f = open('work/fix_hdr.hdr', 'r')
hdr = f.read()
print(hdr)
f.close()
If we try to use the image (e.g., for display or reprojection), it will fail:
rtn = mViewer('-color yellow -grid eq j2000 \
-ct 4 -gray other/bad_hdr.fits -2s max gaussian-log \
-out work/good_hdr.png',
'', mode=2 )
print(rtn)
Outside of Jupyter (or inside it if you can invoke a text editor) to fix the CTYPE line.
rtn = mPutHdr('other/bad_hdr.fits', 'work/good_hdr.fits', 'other/fix.hdr')
print(rtn)
Not much to look at, but it worked. The file can now be used:
from IPython.display import Image
rtn = mViewer('-color yellow -grid eq j2000 \
-ct 1 -gray work/good_hdr.fits -2s max gaussian-log \
-out work/good_hdr.png',
'', mode=2 )
Image(filename='work/good_hdr.png')