|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Code to generate FT of final 2D-Gaussian to be used |
| 4 | +for convolving an image. The code deconvolves the input |
| 5 | +psf. The intrinsic psf must be specified. |
| 6 | +
|
| 7 | +Python version of gaussft.f by Wasim Raja |
| 8 | +""" |
| 9 | +from typing import Tuple |
| 10 | + |
| 11 | +import numba as nb |
| 12 | +import numpy as np |
| 13 | + |
| 14 | + |
| 15 | +@nb.njit(cache=True) |
| 16 | +def gaussft( |
| 17 | + bmin_in: float, |
| 18 | + bmaj_in: float, |
| 19 | + bpa_in: float, |
| 20 | + bmin: float, |
| 21 | + bmaj: float, |
| 22 | + bpa: float, |
| 23 | + u: np.ndarray, |
| 24 | + v: np.ndarray, |
| 25 | +) -> Tuple[np.ndarray, float]: |
| 26 | + """ |
| 27 | + Compute the Fourier transform of a 2D Gaussian for convolution. |
| 28 | +
|
| 29 | + Parameters: |
| 30 | + bmin_in (float): Intrinsic psf BMIN (degrees) |
| 31 | + bmaj_in (float): Intrinsic psf BMAJ (degrees) |
| 32 | + bpa_in (float): Intrinsic psf BPA (degrees) |
| 33 | + bmin (float): Final psf BMIN (degrees) |
| 34 | + bmaj (float): Final psf BMAJ (degrees) |
| 35 | + bpa (float): Final psf BPA (degrees) |
| 36 | + u (np.ndarray): Fourier coordinates corresponding to image coord x |
| 37 | + v (np.ndarray): Fourier coordinates corresponding to image coord y |
| 38 | +
|
| 39 | + Returns: |
| 40 | + g_final (np.ndarray): Final array to be multiplied to FT(image) for convolution |
| 41 | + in the FT domain. |
| 42 | + g_ratio (float): Factor for flux scaling |
| 43 | + """ |
| 44 | + deg2rad = np.pi / 180.0 |
| 45 | + |
| 46 | + bmaj_in_rad, bmin_in_rad, bpa_in_rad = ( |
| 47 | + bmaj_in * deg2rad, |
| 48 | + bmin_in * deg2rad, |
| 49 | + bpa_in * deg2rad, |
| 50 | + ) |
| 51 | + bmaj_rad, bmin_rad, bpa_rad = bmaj * deg2rad, bmin * deg2rad, bpa * deg2rad |
| 52 | + |
| 53 | + sx, sy = bmaj_rad / (2 * np.sqrt(2.0 * np.log(2.0))), bmin_rad / ( |
| 54 | + 2 * np.sqrt(2.0 * np.log(2.0)) |
| 55 | + ) |
| 56 | + sx_in, sy_in = bmaj_in_rad / (2.0 * np.sqrt(2.0 * np.log(2.0))), bmin_in_rad / ( |
| 57 | + 2.0 * np.sqrt(2.0 * np.log(2.0)) |
| 58 | + ) |
| 59 | + |
| 60 | + u_cosbpa, u_sinbpa = u * np.cos(bpa_rad), u * np.sin(bpa_rad) |
| 61 | + u_cosbpa_in, u_sinbpa_in = u * np.cos(bpa_in_rad), u * np.sin(bpa_in_rad) |
| 62 | + |
| 63 | + v_cosbpa, v_sinbpa = v * np.cos(bpa_rad), v * np.sin(bpa_rad) |
| 64 | + v_cosbpa_in, v_sinbpa_in = v * np.cos(bpa_in_rad), v * np.sin(bpa_in_rad) |
| 65 | + |
| 66 | + g_amp = np.sqrt(2.0 * np.pi * sx * sy) |
| 67 | + |
| 68 | + dg_amp = np.sqrt(2.0 * np.pi * sx_in * sy_in) |
| 69 | + |
| 70 | + g_ratio = g_amp / dg_amp |
| 71 | + |
| 72 | + # Vectorized calculation of ur, vr, g_arg, and dg_arg |
| 73 | + ur = u_cosbpa[:, np.newaxis] - v_sinbpa[np.newaxis, :] |
| 74 | + vr = u_sinbpa[:, np.newaxis] + v_cosbpa[np.newaxis, :] |
| 75 | + g_arg = -2.0 * np.pi**2 * ((sx * ur) ** 2 + (sy * vr) ** 2) |
| 76 | + |
| 77 | + ur_in = u_cosbpa_in[:, np.newaxis] - v_sinbpa_in[np.newaxis, :] |
| 78 | + vr_in = u_sinbpa_in[:, np.newaxis] + v_cosbpa_in[np.newaxis, :] |
| 79 | + dg_arg = -2.0 * np.pi**2 * ((sx_in * ur_in) ** 2 + (sy_in * vr_in) ** 2) |
| 80 | + |
| 81 | + # Vectorized calculation of g_final |
| 82 | + g_final = g_ratio * np.exp(g_arg - dg_arg) |
| 83 | + |
| 84 | + return g_final, g_ratio |
0 commit comments