Skip to content

Commit 889d53f

Browse files
authored
Merge pull request #50 from AlecThomson/pure
Pythonize gaussft.f to gaussft.py
2 parents 85e3373 + 51ca420 commit 889d53f

File tree

8 files changed

+137
-321
lines changed

8 files changed

+137
-321
lines changed

pyproject.toml

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
1+
[tool.poetry]
2+
name = "racs-tools"
3+
version = "3.0.0"
4+
description = "Useful scripts for RACS."
5+
authors = ["Alec Thomson"]
6+
license = "BSD"
7+
readme = "README.md"
8+
packages = [{include = "racs_tools"}]
9+
10+
[tool.poetry.dependencies]
11+
python = "^3.8"
12+
numpy = "<2"
13+
astropy = "^5"
14+
radio_beam = "*"
15+
schwimmbad = "*"
16+
scipy = "*"
17+
spectral_cube = "^0.6.3"
18+
tqdm = "*"
19+
numba = "*"
20+
mpi4py = {version = "*", optional = true}
21+
22+
[tool.poetry.dev-dependencies]
23+
black = "*"
24+
isort = "*"
25+
26+
[tool.poetry.extras]
27+
mpi = ["mpi4py"]
28+
29+
[tool.poetry.scripts]
30+
beamcon_2D = "racs_tools.beamcon_2D:cli"
31+
beamcon_3D = "racs_tools.beamcon_3D:cli"
32+
getnoise_list = "racs_tools.getnoise_list:cli"
33+
134
[build-system]
2-
requires = [
3-
"setuptools>=42",
4-
"wheel",
5-
"numpy"]
6-
build-backend = "setuptools.build_meta"
35+
requires = ["poetry-core"]
36+
build-backend = "poetry.core.masonry.api"

racs_tools/au2.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
""" For getting fluxes right in Jy/beam """
33
__author__ = "Tessa Vernstrom"
44

55
import math
66
from typing import List, Tuple
77

88
import numpy as np
9-
from scipy import *
109

1110

1211
def gaussianDeconvolve(smaj, smin, spa, bmaj, bmin, bpa):
@@ -26,9 +25,6 @@ def gaussianDeconvolve(smaj, smin, spa, bmaj, bmin, bpa):
2625
0.5. Feel a little wary about that first change.
2726
"""
2827

29-
import numpy as np
30-
from numpy import abs, arctan2, cos, min, sin, sqrt
31-
3228
spa = np.radians(spa)
3329
bpa = np.radians(bpa)
3430
if smaj < bmaj:
@@ -37,29 +33,29 @@ def gaussianDeconvolve(smaj, smin, spa, bmaj, bmin, bpa):
3733
smin = bmin
3834

3935
alpha = (
40-
(smaj * cos(spa)) ** 2
41-
+ (smin * sin(spa)) ** 2
42-
- (bmaj * cos(bpa)) ** 2
43-
- (bmin * sin(bpa)) ** 2
36+
(smaj * np.cos(spa)) ** 2
37+
+ (smin * np.sin(spa)) ** 2
38+
- (bmaj * np.cos(bpa)) ** 2
39+
- (bmin * np.sin(bpa)) ** 2
4440
)
4541
beta = (
46-
(smaj * sin(spa)) ** 2
47-
+ (smin * cos(spa)) ** 2
48-
- (bmaj * sin(bpa)) ** 2
49-
- (bmin * cos(bpa)) ** 2
42+
(smaj * np.sin(spa)) ** 2
43+
+ (smin * np.cos(spa)) ** 2
44+
- (bmaj * np.sin(bpa)) ** 2
45+
- (bmin * np.cos(bpa)) ** 2
5046
)
5147
gamma = 2 * (
52-
(smin**2 - smaj**2) * sin(spa) * cos(spa)
53-
- (bmin**2 - bmaj**2) * sin(bpa) * cos(bpa)
48+
(smin**2 - smaj**2) * np.sin(spa) * np.cos(spa)
49+
- (bmin**2 - bmaj**2) * np.sin(bpa) * np.cos(bpa)
5450
)
5551
# print smaj,smin
5652
# print alpha,beta,gamma
5753
s = alpha + beta
58-
t = sqrt((alpha - beta) ** 2 + gamma**2)
54+
t = np.sqrt((alpha - beta) ** 2 + gamma**2)
5955
# print s,t
60-
dmaj = sqrt(0.5 * (s + t))
56+
dmaj = np.sqrt(0.5 * (s + t))
6157
if s > t:
62-
dmin = sqrt(0.5 * (s - t))
58+
dmin = np.sqrt(0.5 * (s - t))
6359
else:
6460
dmin = 0
6561
# print dmaj,dmin
@@ -71,7 +67,7 @@ def gaussianDeconvolve(smaj, smin, spa, bmaj, bmin, bpa):
7167
if abs(gamma) + abs(alpha - beta) == 0:
7268
dpa = 0
7369
else:
74-
dpa = 0.5 * arctan2(-gamma, alpha - beta)
70+
dpa = 0.5 * np.arctan2(-gamma, alpha - beta)
7571
# if((s>=t)&(bmin!=smin)):
7672
# dmin=sqrt (0.5 * (s - t))
7773

racs_tools/beamcon_3D.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
""" Convolve ASKAP cubes to common resolution """
33
__author__ = "Alec Thomson"
44

racs_tools/convolve_uv.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
""" Fast convolution in the UV domain """
33
__author__ = "Wasim Raja"
44

@@ -63,8 +63,6 @@ def convolve(
6363
bpa=new_beam.pa.to(units.deg).value,
6464
u=u_image,
6565
v=v_image,
66-
nx=nx,
67-
ny=ny,
6866
)
6967
# Perform the x-ing in the FT domain
7068
im_f = np.fft.fft2(image)

racs_tools/gaussft.f

Lines changed: 0 additions & 141 deletions
This file was deleted.

racs_tools/gaussft.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

racs_tools/getnoise_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
""" Find bad channels by checking statistics of each channel image. """
33

44
import argparse

0 commit comments

Comments
 (0)