🌌 Unveiling the Cosmos: A Pythonic Journey into Astronomy πŸš€πŸ”­ - Part 1: Astropy and FITS Files🌠

Β·

5 min read

Embark on an astronomical adventure with Python! 🌌 In Part 1 of our cosmic journey, we dive into the mesmerizing realm of AstroImage manipulation and the enigmatic FITS file format. πŸš€ Explore the celestial wonders, unlock the secrets hidden in astronomical data, and harness the power of Python to unveil the beauty of the cosmos. πŸ“Έβœ¨ Join us as we decode the language of the stars, one line of code at a time! 🌠

Unveiling the Cosmic Canvas: Libraries in Focus 🌌

In our celestial journey through Python's astral landscape, we'll navigate the vast universe of astronomical data using powerful libraries. Brace yourself as we harness the versatility of NumPy for numerical operations, wield the visual prowess of Matplotlib for crafting stunning plots, and delve into the secrets of the Astropy library for handling FITS files β€” the cosmic treasure troves of astronomical information. Each library, a stellar companion on our cosmic quest, contributes its unique prowess to unravel the mysteries hidden in the stars. Let's embark on this astronomical odyssey with the aid of these Python celestial tools! πŸš€πŸ”­

Charting the Celestial Landscape: Imports and Aesthetic Enhancements πŸŒŒπŸ“Š

Before we embark on our cosmic expedition, let's equip ourselves with the essential tools for the journey. We call upon the following stellar companions:

import numpy as np
import matplotlib.pyplot as plt

# Aesthetic enhancements for plots
import matplotlib
matplotlib.rcParams['figure.figsize'] = (10,10)
matplotlib.rcParams['font.size'] = 15

from astropy.io import fits
from astropy.visualization import astropy_mpl_style

plt.style.use(astropy_mpl_style)

Now, let's decipher the roles of each cosmic ally🌠✨:

  • NumPy (import numpy as np): Our interstellar calculator, aiding in numerical operations and data manipulation.

  • Matplotlib (import matplotlib. pyplot as plt): The cosmic cartographer, crafting visual wonders with its plotting capabilities.

  • Astropy (from astropy.io import fits and from astropy.visualization import astropy_mpl_style): Our key to deciphering FITS files, a standard in astronomical data, and the stylistic guide for visually appealing plots.

Unveiling the Mysteries of FITS Files: A Celestial Rosetta Stone πŸŒŒπŸ“

In our astronomical endeavors, we encounter the enigmatic FITS (Flexible Image Transport System) files, akin to a cosmic Rosetta Stone. Let's delve into the celestial significance of FITS and decipher its role in the cosmic narrative.

🌌 What is a FITS File?

FITS, designed for storing, transmitting, and processing scientific data, reigns supreme in astronomy. It elegantly encapsulates multidimensional arrays, metadata, and even human-readable ASCII tables, making it a universal language in the astronomical realm.

πŸš€ Why FITS?

  1. Versatility: FITS accommodates a diverse array of data types and structures, making it ideal for astronomical datasets.

  2. Metadata Mastery: Each FITS file comes adorned with metadata, offering a celestial map guiding astronomers through the intricacies of the data.

🌠 Navigating the Celestial Archive

The open() function returns an object called HDU List, which is a List of Header Data Unit (HDU) objects, i.e. when a FITS file is opened, a HDUList object is returned.

This is the top-level component of the FITS file structure.

# Opening a FITS file
hdu = fits.open('data.fits')

# Displaying the essence of the cosmic artifact
print(hdu)

πŸ›°οΈ Output: [<astropy.io.fits.hdu.image.PrimaryHDU object at 0x7f4fe118de80>]

A useful method in the HDUList Class isHDUList.info(), which summarizes the content of the opened FITS file.

# Unlocking the secrets within
hdu.info()

πŸ“‘ Output:

Filename: data/dss2ir.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU     134   (600, 600)   float32

The first element in HDU is the Primary HDU, i.e. hdul[0] is the primary HDU. The rest are secondary, if any.

Every Header Data Unit (HDU) normally has two components:

  1. Header

  2. Data array or table

In astropy these two components are accessed through, "hdu.header" and "hdu.data"

FITS image headers can contain information about one or more scientific coordinate systems that are overlaid on the image itself. Images contain an implicit Cartesian coordinate system that describes the location of each pixel in the image, but scientific uses usually require working in 'world' coordinates, for example the celestial coordinate system.

# Unlocking the secrets within header
hdu[0].header
# Unlocking the secrets within data
data = hdu[0].data
type(data)

πŸ›°οΈ Output: numpy.ndarray

Illuminating the Cosmos: Visualizing FITS Images πŸŒŒπŸ–ΌοΈ

πŸš€ Unveiling the Celestial Canvas

Our journey through the astral realms takes a visual turn as we gaze upon the celestial canvas encoded within the FITS file. Utilizing the power of Python and matplotlib, let's illuminate the cosmic image concealed within.

# Visualizing the cosmic masterpiece
plt.title('M 51 Galaxy')
plt.imshow(data, cmap='gray', origin='lower')

🌌 Output:

🌠 Observation: Behold the cosmic tableau! The cmap='gray' imparts a monochromatic aesthetic, while origin='lower' aligns the celestial vista with our earthly perspective.

Concluding Our Celestial Prelude πŸŒ πŸ“š

🌌 Navigating the Astral Tapestry

As we draw the curtains on this inaugural chapter of our cosmic voyage, we've navigated the intricacies of FITS files, deciphering the celestial language inscribed within. From libraries like NumPy, Matplotlib, to the indispensable Astropy, our toolkit is primed for astronomical endeavors.

πŸš€ So Far, We've Unveiled:

  1. Introduction to Libraries: NumPy, Matplotlib, Astropy

  2. Import Mastery: Harnessing the power of Python libraries

  3. Decoding FITS Files: Peering into the structure with fits.open() info() header data

  4. Celestial Visualization: Rendering the cosmic image with plt.imshow()

Embark with anticipation, for our cosmic odyssey has just commenced! In the upcoming chapters, we'll delve deeper into the realms of astrophysical data, unveiling the secrets of the universe. 🌌✨

Β