def process_image(data_file, continuum_file, subtraction_coefficient=1.0, multiplication_coefficient=1.0):
"""
Process uncalibrated data and continuum images to produce a calibrated image and error maps.
This function performs the following steps:
1) Read the data and continuum images from FITS files.
2) Align the continuum image to the data image using astroalign.
3) Smooth the images with a Gaussian PSF to correct for seeing variations.
4) Estimate and subtract the sky background from both images.
5) Subtract the scaled continuum image from the data image.
6) Apply flux calibration by multiplying with a calibration coefficient.
7) Propagate errors at each step to create an error map.
Parameters:
-----------
data_file : str
Path to the FITS file containing the data image (emission-line image).
continuum_file : str
Path to the FITS file containing the continuum image.
subtraction_coefficient : float, optional
Coefficient used for continuum subtraction (default is 1.0).
multiplication_coefficient : float, optional
Flux calibration coefficient (default is 1.0).
Returns:
--------
calibrated_image : numpy.ndarray
The calibrated image after processing.
error_map : numpy.ndarray
The corresponding error map of the calibrated image.
"""
import numpy as np
from astropy.io import fits
import astroalign
from scipy.ndimage import gaussian_filter
from astropy.convolution import Gaussian2DKernel, convolve
from astropy.stats import sigma_clipped_stats
# Step 1: Read data and continuum images from FITS files
# Read data image
data_hdulist = fits.open(data_file)
data_image = data_hdulist[0].data.astype(float)
data_hdulist.close()
# Read continuum image
cont_hdulist = fits.open(continuum_file)
cont_image = cont_hdulist[0].data.astype(float)
cont_hdulist.close()
# Initialize variance maps assuming Poissonian noise (variance equals counts)
data_variance = data_image.copy()
cont_variance = cont_image.copy()
# Ensure counts are non-negative
data_variance[data_variance < 0] = 0
cont_variance[cont_variance < 0] = 0
# Step 2: Align the continuum image to the data image using astroalign
# Find the coordinate transformation between the continuum and data images
transf, (source_list, target_list) = astroalign.find_transform(cont_image, data_image)
# Apply the transformation to align the continuum image
aligned_cont_image = astroalign.apply_transform(transf, cont_image, data_image)
# Apply the same transformation to the continuum variance map
aligned_cont_variance = astroalign.apply_transform(transf, cont_variance, data_image)
# Step 3: Smooth images with a Gaussian PSF to correct for seeing variations
# Estimate Gaussian PSF parameters (here we assume sigma=1.0 pixels)
sigma = 1.0 # Standard deviation of the Gaussian kernel in pixels
# Smooth the data image
smoothed_data_image = gaussian_filter(data_image, sigma)
# Smooth the data variance map
# For variance, convolve with the square of the Gaussian kernel
# Create a normalized Gaussian kernel
kernel_size = int(8 * sigma + 1)
gaussian_kernel = Gaussian2DKernel(x_stddev=sigma, y_stddev=sigma, x_size=kernel_size, y_size=kernel_size)
gaussian_kernel.normalize()
# Square the kernel for variance convolution
kernel_squared = gaussian_kernel.array ** 2
# Convolve the data variance with the squared kernel
smoothed_data_variance = convolve(data_variance, kernel_squared, normalize_kernel=False)
# Smooth the aligned continuum image
smoothed_cont_image = gaussian_filter(aligned_cont_image, sigma)
# Smooth the aligned continuum variance map
smoothed_cont_variance = convolve(aligned_cont_variance, kernel_squared, normalize_kernel=False)
# Step 4: Estimate and subtract the sky background from both images
# Estimate background level in the smoothed data image using sigma-clipped statistics
mean_data, median_data, std_data = sigma_clipped_stats(smoothed_data_image, sigma=3.0)
background_level_data = median_data
# Subtract background from the smoothed data image
background_subtracted_data = smoothed_data_image - background_level_data
# The variance remains the same when subtracting a constant
background_subtracted_variance = smoothed_data_variance
# Estimate background level in the smoothed continuum image
mean_cont, median_cont, std_cont = sigma_clipped_stats(smoothed_cont_image, sigma=3.0)
background_level_cont = median_cont
# Subtract background from the smoothed continuum image
background_subtracted_cont = smoothed_cont_image - background_level_cont
# The variance remains the same when subtracting a constant
background_subtracted_cont_variance = smoothed_cont_variance
# Step 5: Subtract the scaled continuum image from the data image
# Scale the continuum image and its variance with the subtraction coefficient
scaled_cont_image = subtraction_coefficient * background_subtracted_cont
scaled_cont_variance = subtraction_coefficient ** 2 * background_subtracted_cont_variance
# Subtract the scaled continuum image from the data image
continuum_subtracted_image = background_subtracted_data - scaled_cont_image
# Propagate the variance (add the variances of data and scaled continuum)
continuum_subtracted_variance = background_subtracted_variance + scaled_cont_variance
# Step 6: Apply flux calibration by multiplying with the calibration coefficient
# Multiply the image with the multiplication coefficient
calibrated_image = multiplication_coefficient * continuum_subtracted_image
# Propagate the variance (multiply the variance by the square of the coefficient)
calibrated_variance = multiplication_coefficient ** 2 * continuum_subtracted_variance
# Ensure that variance is non-negative
calibrated_variance[calibrated_variance < 0] = 0
# Step 7: Compute the error map (standard deviation)
error_map = np.sqrt(calibrated_variance)
# Return the calibrated image and error map
return calibrated_image, error_map