# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# ***
#
# * [Outline](../0_Introduction/0_introduction.ipynb)
# * [Glossary](../0_Introduction/1_glossary.ipynb)
# * [1. Building the Concepts](01_00_introduction.ipynb)  
#     * Previous: [1.3 Fitting Observations of Rotating Discs using Tirific](01_03_fitting_observations_of_rotating_discs_using_tirific.ipynb)
#     * Next: [1.5 Visualising Numpy Arrays with Matplotlib](01_05_visualising_numpy_arrays_with_matplotlib.ipynb)
#
# ***

# Import standard modules:

import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline
from IPython.display import HTML 
HTML('../style/course.css') #apply general CSS

# import specific modules

import pyfits

# 1. Reading and Writing Fits Files
#     1. [Reading Fits Files](#fitsFiles:sec:reading_fits_files)
#     2. [Writing Fits Files](#fitsFiles:sec:writing_fits_files)

# ## 1.4 Reading and Writing Fits Files with PyFITS, Numpy and Scipy

# Fits represents a way of storing scientific images. The file usually consists of a header unit and a data unit. The header constituent of the fits files specifies certain things like the number of data axes, the number of bits and some other variables related to the wcs coordinates.
# Below illustrates how fits files are read and written to using Python.

# ### 1.4.1 Reading Fits Files<a id='fitsFiles:sec:reading_fits_files'></a><!--\label{fitsFiles:sec:reading_fits_files}-->

# PyFITS provides an easy avenue for reading fits files as a collection of HDU objects. The header and data units of the fits file can be conveniently stored in a variable when the fits file has been opened. The open method is used for such a purpose. PyFITS also allows to access the contents of fits files without having to worry about the open function. Find below a code to illustrate this.

# +
#Read fits file
hdu = pyfits.open('/home/samuel/software/samtirifik-master/chapter_01_concepts/01_data/n2541_lr_in.fits') #open
header = hdu[0].header #assign content of header to variable header
data = hdu[0].data #assign content of data to variable data

"""
#alternatively data and header units can be accessed without the open method
data, header = pyfits.getdata('/home/samuel/software/samtirifik-master/chapter_01_concepts/01_data/n2541_lr_in.fits',header=True)

"""
# -

# ### 1.4.2 Writing Fits Files<a id='fitsFiles:sec:writing_fits_files'></a><!--\label{fitsFiles:sec:writing_fits_files}-->

# To create a new fits file, the 'writeto' function can be used for such a purpose. The function would be provided with inputs, namely: filename, data & header. Code below illustrates this.

pyfits.writeto('/home/samuel/software/samtirifik-master/chapter_01_concepts/01_data/newFile.fits',data,header)

# ***
#
# * Next: [1.5 Visualising Numpy Arrays with Matplotlib](01_05_visualising_numpy_arrays_with_matplotlib.ipynb)

StarcoderdataJupyterScriptsDedupFiltered