Skip to content

Instantly share code, notes, and snippets.

@seanhess
Last active July 3, 2024 15:44
Show Gist options
  • Save seanhess/062f14615e77825ecfce28076e13dff4 to your computer and use it in GitHub Desktop.
Save seanhess/062f14615e77825ecfce28076e13dff4 to your computer and use it in GitHub Desktop.
Graph L2 FIts Files
Here's a Python script to open a directory of FITS files, combine them, and render an image with WCS axis labels using astropy and numpy. This script assumes that all FITS files are in the specified directory, and the files are named in a way that allows them to be sorted correctly to reconstruct the Y axis.
import os
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.wcs import WCS
from astropy.visualization import astropy_mpl_style
plt.style.use(astropy_mpl_style)
# Directory containing FITS files
directory = 'path_to_fits_files'
# List all FITS files in the directory
fits_files = sorted([f for f in os.listdir(directory) if f.endswith('.fits')])
# Initialize lists to store data and WCS information
data_list = []
wcs_list = []
for fits_file in fits_files:
with fits.open(os.path.join(directory, fits_file)) as hdul:
# Assuming the 2nd HDU (index 1) contains the temperature data
temp_data = hdul[1].data
wcs_info = WCS(hdul[1].header)
data_list.append(temp_data)
wcs_list.append(wcs_info)
# Stack the data along the Y axis
combined_data = np.concatenate(data_list, axis=0)
# Assuming WCS is consistent across all files, use the WCS from the first file for plotting
wcs = wcs_list[0]
# Plotting the combined data
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, p
Explanation:
Directory Listing and Sorting: The script lists all FITS files in the specified directory and sorts them to ensure correct reconstruction of the Y axis.
Opening FITS Files: Each FITS file is opened, and the temperature data from the second HDU is extracted along with the WCS information.
Data Stacking: The temperature data from each file is concatenated along the Y axis to create a combined dataset.
Plotting with WCS: The combined data is plotted using matplotlib with WCS axis labels. The WCS from the first file is used for the plot, assuming WCS consistency across all files.
Make sure to replace 'path_to_fits_files' with the actual path to your directory of FITS files. Also, you may need to adjust the colormap (cmap) or other plot parameters to suit your specific data and preferences.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment