<ol>
<li>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 <code>astropy</code> 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 <a href="http://docs.astropy.org/en/stable/io/fits/#working-with-fits-headers" rel="nofollow noreferrer">in the AstroPy documentation</a>. 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.</li>
<li>No image is going to be "circular". Images are taken on square CCDs and thus produce square images of the sky. A "regular" image will be square.</li>
</ol>

CC BY-SA 3.0
2017-03-09T13:56:29.577Z
2017-03-09T13:56:29.577Z
zephyr
<p>There's also a software package, called <a href="http://ds9.si.edu/site/Download.html" rel="nofollow noreferrer">DS9</a>, which I use for viewing both the image and the header. I edit the headers in Python using astropy.</p>

CC BY-SA 3.0
2017-09-24T21:35:19.003Z
2017-09-24T21:35:19.003Z
Jim421616
<p>I personally use <a href="http://www.astropy.org/" rel="nofollow noreferrer">Astropy</a>, specifically <code>astropy.io.fits</code>, 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</p>

<pre><code>from astropy.io import fits
data = fits.open('data_file.fits')[0].data
</code></pre>

<p>You'll find more information in the <a href="http://docs.astropy.org/en/stable/io/fits/index.html" rel="nofollow noreferrer">documentation</a> on the FITS module.</p>

CC BY-SA 3.0
2017-03-09T10:53:59.630Z
Thank you. I'm going to edit my question, since I've had an idea for my program and the problem I'm having is now more specific.
Wild Feather
CC BY-SA 3.0
2017-03-09T09:34:39.793Z
2017-03-09T09:34:39.793Z
Warrick
<p>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. </p>

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

<p><strong>Edit:</strong> 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? </p>

<p><strong>Edit 2:</strong> This is what I see as the header of my fits file:
<a href="https://i.sstatic.net/kX26H.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kX26H.jpg" alt="enter image description here"></a></p>

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

<p><strong>Edit 3:</strong> Something seems to change when I put the terminal in full screen mode. Is there any useful info here?
<a href="https://i.sstatic.net/9VsAk.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/9VsAk.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2017-03-09T08:58:48.863Z
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](http://docs.astropy.org/en/stable/io/fits/). From there is should be a simple for loop.
pela
CC BY-SA 3.0
2017-03-09T09:13:04.107Z
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!
Wild Feather
CC BY-SA 3.0
2017-03-09T09:14:43.153Z
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.
Wild Feather
CC BY-SA 3.0
2017-03-09T13:59:30.287Z
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.
zephyr
CC BY-SA 3.0
2017-03-09T16:35:39.167Z
Oh, sorry, I forgot to add the tag. Thank you, @zephyr :)
Wild Feather
CC BY-SA 3.0
2017-03-09T16:36:39.350Z
@pela,  I'm going to edit my question again and add what I see as the header of my fits file using vim, just in case it seems familiar to you and you know how I could do what I need. Thanks again!
Wild Feather
CC BY-SA 3.0
2017-03-09T16:41:23.637Z
@WildFeather: Maybe you got the answer now, but your header should contain information such as RA and dec for one of the pixels, info about whether this pixel is the central on, uppermost left one, etc., as well as infor about the resolution, i.e. number of arcsec per pixel. These three pieces of info should be enough to tell you the RA,dec values spanned by your image.
pela
CC BY-SA 3.0
2017-03-09T16:52:10.380Z
@pela, I don't know if I'm getting this right, but is there a list inside the fits file with the coordinates (declination and right ascension) of each one of the pixels? Since the image has a "weird" shape (not circular and not square), I can't see how I would be able to deduce from the header if a specific point belongs to the image...
Wild Feather
CC BY-SA 3.0
2017-03-09T17:21:20.900Z
Looks like the CRVAL1 and CRVAL2 header entries are the right ascension and declination of your image. Likely this will be the position of the center of your image. I don't see anything in this header that allows you to know (or calculate) the size of your image on the sky though. Perhaps there's more to your header or else another header in your file. You might need to look for something like a plate scale or pixel scale.
zephyr
CC BY-SA 3.0
2017-03-09T17:29:07.090Z
Thank you! I have no idea where to look for extra info in the file, though. I scrolled it all the way down and the rest is just incomprehensible symbols. Isn't there usually a table or something similar, with the coordinates of every pixel? Since I can open the file and get an image with ds9, I suppose that ds9 must get this information from the file. But thank you very much anyway!
Wild Feather
CC BY-SA 3.0
2017-03-09T17:54:51.693Z
@zephyr, does my last edit add anything that could be useful? Sorry to be so insistent, but I'm pretty desperate trying to figure it out, and maybe it's easy for you. And thank you!
Wild Feather
CC BY-SA 3.0
2017-03-09T18:04:06.453Z
Looks like your fits file came from the Atacama Pathfinder Experiment (APEX). I [found a document](https://fits.gsfc.nasa.gov/registry/mbfits/APEX-MPI-ICD-0002-R1_64.pdf) which describes the APEX fits file format. Of particular interest to you may be the section on WCS Coordinates (Section 16.5) which describes how the images exactly coordinates are stored in the file and what they mean. This will allow you to determine the coordinates of every pixel. Astropy also has the ability to pull out WCS coordinate information, as [described here](http://docs.astropy.org/en/stable/wcs/).
zephyr
CC BY-SA 3.0
2017-03-09T18:08:51.703Z
Thank you so much! I will work on it and see what I can do.
Wild Feather
CC BY-SA 3.0
2017-03-10T09:47:15.977Z
Looks like the two of you got it sorted out :)
pela
CC BY-SA 3.0
2017-03-10T10:33:54.780Z
@pela Yes, I think it finally works! Thank you, you helped a lot too :)
Wild Feather
<data-analysis><python>
Python with image in fits format
Wild Feather
# Python with image in fits format

(Asked by: username_0 on 2017-03-09)

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:
[![enter image description here](https://i.sstatic.net/kX26H.jpg)](https://i.sstatic.net/kX26H.jpg)

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?
[![enter image description here](https://i.sstatic.net/9VsAk.png)](https://i.sstatic.net/9VsAk.png)

**Tags:** data-analysis, python
> **Comments:**
> **username_1**: 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](http://docs.astropy.org/en/stable/io/fits/). From there is should be a simple for loop.  
> **username_0**: 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!  
> **username_0**: 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.  
> **username_2**: 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.  
> **username_0**: Oh, sorry, I forgot to add the tag. Thank you, @zephyr :)  
> **username_0**: @pela,  I'm going to edit my question again and add what I see as the header of my fits file using vim, just in case it seems familiar to you and you know how I could do what I need. Thanks again!  
> **username_1**: @WildFeather: Maybe you got the answer now, but your header should contain information such as RA and dec for one of the pixels, info about whether this pixel is the central on, uppermost left one, etc., as well as infor about the resolution, i.e. number of arcsec per pixel. These three pieces of info should be enough to tell you the RA,dec values spanned by your image.  
> **username_0**: @pela, I don't know if I'm getting this right, but is there a list inside the fits file with the coordinates (declination and right ascension) of each one of the pixels? Since the image has a "weird" shape (not circular and not square), I can't see how I would be able to deduce from the header if a specific point belongs to the image...  
> **username_2**: Looks like the CRVAL1 and CRVAL2 header entries are the right ascension and declination of your image. Likely this will be the position of the center of your image. I don't see anything in this header that allows you to know (or calculate) the size of your image on the sky though. Perhaps there's more to your header or else another header in your file. You might need to look for something like a plate scale or pixel scale.  
> **username_0**: Thank you! I have no idea where to look for extra info in the file, though. I scrolled it all the way down and the rest is just incomprehensible symbols. Isn't there usually a table or something similar, with the coordinates of every pixel? Since I can open the file and get an image with ds9, I suppose that ds9 must get this information from the file. But thank you very much anyway!  
> **username_0**: @zephyr, does my last edit add anything that could be useful? Sorry to be so insistent, but I'm pretty desperate trying to figure it out, and maybe it's easy for you. And thank you!  
> **username_2**: Looks like your fits file came from the Atacama Pathfinder Experiment (APEX). I [found a document](https://fits.gsfc.nasa.gov/registry/mbfits/APEX-MPI-ICD-0002-R1_64.pdf) which describes the APEX fits file format. Of particular interest to you may be the section on WCS Coordinates (Section 16.5) which describes how the images exactly coordinates are stored in the file and what they mean. This will allow you to determine the coordinates of every pixel. Astropy also has the ability to pull out WCS coordinate information, as [described here](http://docs.astropy.org/en/stable/wcs/).  
> **username_0**: Thank you so much! I will work on it and see what I can do.  
> **username_1**: Looks like the two of you got it sorted out :)  
> **username_0**: @pela Yes, I think it finally works! Thank you, you helped a lot too :)  

---

## 3 answers

### Answer by username_4 on 2017-03-09. Score: 4 | Accepted answer

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

You'll find more information in the documentation on the FITS module.

> **Comments:**
> **username_0**: Thank you. I'm going to edit my question, since I've had an idea for my program and the problem I'm having is now more specific.  

---

### Answer by username_2 on 2017-03-09. Score: 1

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](http://docs.astropy.org/en/stable/io/fits/#working-with-fits-headers). 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.
2. No image is going to be "circular". Images are taken on square CCDs and thus produce square images of the sky. A "regular" image will be square.


---

### Answer by username_3 on 2017-09-24. Score: 1

There's also a software package, called [DS9](http://ds9.si.edu/site/Download.html), which I use for viewing both the image and the header. I edit the headers in Python using astropy.


---

