Reading and writing fits files with Python (memo)

Introduction

The fits file is an image file format used for exploration data and astronomical data, and consists of a part called a header that contains various information such as time and posture, and an array part of image size (for example, 1024x1024). There is also a cube-shaped fits file, which consists of n headers and an image array. https://ja.wikipedia.org/wiki/FITS About reading and displaying fits data using the Python library astropy.io.fits. There is DS9 etc. as a viewer of fits.

PDS Planetary Data System NASA exploration data archive. Anyone can use it for free. If you download the data from here, you can see the image of your favorite planet! Images are published in the fits file format.

For example, the data for Hayabusa is here. All image data taken by Hayabusa is open to the public.

installation of astropy

Install with anaconda.

conda install astropy

Load fits file

Read the 0th data. The image consists of a header + an image array.

import astropy.io.fits as fits
import matplotlib.pyplot as plt
hdulist=pyfits.open('file.fits')
hdu=hdulist[0]
data=hdu.data #data=fits.getdata('file.fits',0)But you can.
header=hdu.header
plt.imshow(data)
plt.show()

When retrieving header items, such as TI_0

print header["TI_0"]

Specifying the X and Yth pixels

data[Y][X]

Create a new fits file

hdu = fits.PrimaryHDU(im)
hdulist = fits.HDUList([hdu])
hdulist.writeto('new.fits',overwrite=True)

fits is one set (hdu) with header and image array. It consists of hdu = PrimaryHDU (data, header). When using a large number of extensions, create multiple second and subsequent hdu with fits.ImageHDU, and use one like HDUList ([hdu1, hdu2, hdu3]) Make it a file. That is, it is possible to save a plurality of images in one file. It can be overwritten by setting overwrite = True.

Add keywords

hdu.header['NEWKEYWORD']='something'

Recommended Posts

Reading and writing fits files with Python (memo)
Reading and writing JSON files with Python
Reading and writing NetCDF with Python
Reading and writing CSV with Python
Study from Python Reading and writing Hour9 files
Reading and writing CSV and JSON files in Python
Example of reading and writing CSV with Python
Reading and writing csv files
Reading .txt files with Python
[Introduction for beginners] Reading and writing Python CSV files
Character code for reading and writing csv files with python ~ windows environment ver ~
Python CSV file reading and writing
Reading and writing text in Python
"System trade starting with Python3" reading memo
[Easy Python] Reading Excel files with openpyxl
uproot: Python / Numpy based library for reading and writing ROOT files
Drag and drop local files with Selenium (Python)
Download and import files with Splunk external python
Case sensitive when reading and writing INI files
[Python] Reading CSV files
Read and write files with Slackbot ~ Bot development with Python ~
Reading, displaying and speeding up gifs with python [OpenCV]
Programming with Python and Tkinter
Encryption and decryption with Python
Twitter graphing memo with Python
Python and ruby slice memo
Sorting image files with Python (2)
Sort huge files with python
Sorting image files with Python
Integrate PDF files with Python
python with pyenv and venv
Works with Python and R
[Python] Easy reading of serial number image files with OpenCV
Notes on reading and writing float32 TIFF images in python
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
Difference between java and python (memo)
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Recursively unzip zip files with python
Manipulating EAGLE .brd files with Python
Scraping with Python, Selenium and Chromedriver
python memo --Specify options with getopt
Scraping with Python and Beautiful Soup
Manipulate files and folders in Python
Read and use Python files from Python
Decrypt files encrypted with OpenSSL with Python 3
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Save and retrieve files with Pepper
Handle Excel CSV files with Python
Multi-line size specification reading with python
I played with PyQt5 and Python3
Read files in parallel with Python
Python memo ① Folder and file operations
Export and output files in Python
Multiple integrals with Python and Sympy
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python