# Python with image in fits format

I'm trying to write a Python program that will do the following: take an image in fits format and a reg file with a list of regions in the sky, and determine which regions in the list are inside the fits image and which ones are outside. This can be done visually with ds9, but I have no idea of how I can access the fits file and determine the coordinates of the boundary of the image.

Any advice? I don't know much about the fits format, but it won't open with a usual text editor like gedit.

Edit: Now I have a more specific problem. I have four points in the sky defined by their declination and right ascension. Is there a simple way to check if these coordinates belong to the image coded in the fits file?

Edit 2: This is what I see as the header of my fits file:

I'm no expert in physics, just a mathematician, so I'm not sure how to proceed from here. Any help would be appreciated...

Edit 3: Something seems to change when I put the terminal in full screen mode. Is there any useful info here?

• You should be able to open a fits file with any text editor, like vim, Emacs, or even, god forbid, Word. Only the first part (the "header") which contains information about the data — e.g. the boundaries — will make sense to you, though. The image itself will look like gibberish. For reading the fits file in Python, have a look at astropy. From there is should be a simple for loop.
– pela
Mar 9 '17 at 8:58
• Thank you so much! I could finally open the fits file with vim, no idea why gedit wasn't working. I will have a look at astropy, too! Mar 9 '17 at 9:13
• Just one question... You said I could find the information about the boundary of the image on the header of the fits file. But where, especifically? There are many variables there and I don't understand what all of them mean. Mar 9 '17 at 9:14
• Use the @ symbol and a users name to specifically tag them. They'll get a notification. Otherwise, they won't know you're trying to respond to them. In any case, we can't possibly know what variables are in your fits file header. That's like asking us the meaning of all the words in some random text file without showing us the text file. Mar 9 '17 at 13:59
• Oh, sorry, I forgot to add the tag. Thank you, @zephyr :) Mar 9 '17 at 16:35

I personally use Astropy, specifically astropy.io.fits, although I'm not a seasoned user of FITS files and I don't really know their layout. As an example snippet of code, I often load data from FITS files using

from astropy.io import fits
data = fits.open('data_file.fits')[0].data


1. There's no clear answer to this, it would all depend on your specific file, which of course we know nothing about. The information you need is likely going to exist in the fits header though (if it exists at all). I'm assuming you're reading your fits file using the astropy package since you're using Python (if not, definitely check it out). If that is the case, you can read all about looking at the fits file headers in the AstroPy documentation. You'll have to look at the header files and scan through to see if any information included with the fits file tells you precisely where in the sky the image is.