RACS-tools 4.2.0__tar.gz → 4.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {racs_tools-4.2.0 → racs_tools-4.3.0}/PKG-INFO +8 -5
- {racs_tools-4.2.0 → racs_tools-4.3.0}/README.md +7 -4
- {racs_tools-4.2.0 → racs_tools-4.3.0}/pyproject.toml +31 -3
- {racs_tools-4.2.0 → racs_tools-4.3.0}/racs_tools/au2.py +4 -4
- {racs_tools-4.2.0 → racs_tools-4.3.0}/racs_tools/beamcon_2D.py +4 -4
- {racs_tools-4.2.0 → racs_tools-4.3.0}/racs_tools/beamcon_3D.py +207 -99
- {racs_tools-4.2.0 → racs_tools-4.3.0}/racs_tools/convolve_uv.py +9 -10
- {racs_tools-4.2.0 → racs_tools-4.3.0}/racs_tools/gaussft.py +1 -2
- {racs_tools-4.2.0 → racs_tools-4.3.0}/racs_tools/getnoise_list.py +8 -8
- {racs_tools-4.2.0 → racs_tools-4.3.0}/racs_tools/logging.py +2 -3
- {racs_tools-4.2.0 → racs_tools-4.3.0}/racs_tools/parallel.py +4 -3
- {racs_tools-4.2.0 → racs_tools-4.3.0}/LICENSE +0 -0
- {racs_tools-4.2.0 → racs_tools-4.3.0}/racs_tools/__init__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: racs-tools
|
|
3
|
-
Version: 4.
|
|
3
|
+
Version: 4.3.0
|
|
4
4
|
Summary: Useful scripts for RACS.
|
|
5
5
|
License: BSD
|
|
6
6
|
Author: Alec Thomson
|
|
@@ -22,7 +22,7 @@ Requires-Dist: spectral_cube (>=0.6.3)
|
|
|
22
22
|
Requires-Dist: tqdm
|
|
23
23
|
Description-Content-Type: text/markdown
|
|
24
24
|
|
|
25
|
-
[](https://github.com/AlecThomson/RACS-tools/actions/workflows/docker.yml)  [](https://github.com/AlecThomson/RACS-tools/actions/workflows/python-package.yml) [](https://github.com/AlecThomson/RACS-tools/actions/workflows/pypi.yml)
|
|
25
|
+
[](https://github.com/AlecThomson/RACS-tools/actions/workflows/docker.yml)  [](https://github.com/AlecThomson/RACS-tools/actions/workflows/python-package.yml) [](https://github.com/AlecThomson/RACS-tools/actions/workflows/pypi.yml)[](https://results.pre-commit.ci/latest/github/AlecThomson/RACS-tools/master)
|
|
26
26
|
|
|
27
27
|
# RACS-tools
|
|
28
28
|
Useful scripts for RACS
|
|
@@ -39,13 +39,16 @@ conda activate racs-tools
|
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
### Docker / Singularity
|
|
42
|
-
|
|
42
|
+
NOTE: The DockerHub builds are no longer supported.
|
|
43
|
+
|
|
44
|
+
A Dockerfile is provided if you wish to build your own container. Otherwise, images are provided here on GitHub. You can pull these by running e.g.
|
|
43
45
|
```bash
|
|
44
|
-
docker pull alecthomson/racstools
|
|
46
|
+
docker pull ghcr.io/alecthomson/racstools
|
|
45
47
|
```
|
|
46
48
|
or
|
|
49
|
+
|
|
47
50
|
```bash
|
|
48
|
-
|
|
51
|
+
apptainer pull docker://ghcr.io/alecthomson/racstools
|
|
49
52
|
```
|
|
50
53
|
|
|
51
54
|
NOTE: These builds are still experimental, and have not been widely tested. In particular, parallelisation may not work as expected.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
[](https://github.com/AlecThomson/RACS-tools/actions/workflows/docker.yml)  [](https://github.com/AlecThomson/RACS-tools/actions/workflows/python-package.yml) [](https://github.com/AlecThomson/RACS-tools/actions/workflows/pypi.yml)
|
|
1
|
+
[](https://github.com/AlecThomson/RACS-tools/actions/workflows/docker.yml)  [](https://github.com/AlecThomson/RACS-tools/actions/workflows/python-package.yml) [](https://github.com/AlecThomson/RACS-tools/actions/workflows/pypi.yml)[](https://results.pre-commit.ci/latest/github/AlecThomson/RACS-tools/master)
|
|
2
2
|
|
|
3
3
|
# RACS-tools
|
|
4
4
|
Useful scripts for RACS
|
|
@@ -15,13 +15,16 @@ conda activate racs-tools
|
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
### Docker / Singularity
|
|
18
|
-
|
|
18
|
+
NOTE: The DockerHub builds are no longer supported.
|
|
19
|
+
|
|
20
|
+
A Dockerfile is provided if you wish to build your own container. Otherwise, images are provided here on GitHub. You can pull these by running e.g.
|
|
19
21
|
```bash
|
|
20
|
-
docker pull alecthomson/racstools
|
|
22
|
+
docker pull ghcr.io/alecthomson/racstools
|
|
21
23
|
```
|
|
22
24
|
or
|
|
25
|
+
|
|
23
26
|
```bash
|
|
24
|
-
|
|
27
|
+
apptainer pull docker://ghcr.io/alecthomson/racstools
|
|
25
28
|
```
|
|
26
29
|
|
|
27
30
|
NOTE: These builds are still experimental, and have not been widely tested. In particular, parallelisation may not work as expected.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "racs-tools"
|
|
3
|
-
version = "4.
|
|
3
|
+
version = "4.3.0" # A placeholder
|
|
4
4
|
description = "Useful scripts for RACS."
|
|
5
5
|
authors = ["Alec Thomson"]
|
|
6
6
|
license = "BSD"
|
|
@@ -19,8 +19,7 @@ numba = "*"
|
|
|
19
19
|
mpi4py = {version = ">=3", optional = true}
|
|
20
20
|
|
|
21
21
|
[tool.poetry.dev-dependencies]
|
|
22
|
-
|
|
23
|
-
isort = "*"
|
|
22
|
+
ruff = {version = "*", optional = true}
|
|
24
23
|
|
|
25
24
|
[tool.poetry.extras]
|
|
26
25
|
mpi = ["mpi4py"]
|
|
@@ -38,3 +37,32 @@ vcs = "git"
|
|
|
38
37
|
[build-system]
|
|
39
38
|
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"]
|
|
40
39
|
build-backend = "poetry_dynamic_versioning.backend"
|
|
40
|
+
|
|
41
|
+
[tool.ruff]
|
|
42
|
+
src = ["racs_tools"]
|
|
43
|
+
|
|
44
|
+
[tool.ruff.lint]
|
|
45
|
+
extend-select = [
|
|
46
|
+
# "B", # flake8-bugbear
|
|
47
|
+
"I", # isort
|
|
48
|
+
"ARG", # flake8-unused-arguments
|
|
49
|
+
# "C4", # flake8-comprehensions
|
|
50
|
+
# "EM", # flake8-errmsg
|
|
51
|
+
"ICN", # flake8-import-conventions
|
|
52
|
+
# "G", # flake8-logging-format
|
|
53
|
+
# "PGH", # pygrep-hooks
|
|
54
|
+
# "PIE", # flake8-pie
|
|
55
|
+
# "PL", # pylint
|
|
56
|
+
# "PT", # flake8-pytest-style
|
|
57
|
+
# "PTH", # flake8-use-pathlib
|
|
58
|
+
# "RET", # flake8-return
|
|
59
|
+
# "RUF", # Ruff-specific
|
|
60
|
+
# "SIM", # flake8-simplify
|
|
61
|
+
# "T20", # flake8-print
|
|
62
|
+
"UP", # pyupgrade
|
|
63
|
+
# "YTT", # flake8-2020
|
|
64
|
+
# "EXE", # flake8-executable
|
|
65
|
+
# "NPY", # NumPy specific rules
|
|
66
|
+
# "PD", # pandas-vet,
|
|
67
|
+
# "RET", # flake8-return
|
|
68
|
+
]
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
2
|
+
"""For getting fluxes right in Jy/beam"""
|
|
3
|
+
|
|
3
4
|
__author__ = "Tessa Vernstrom"
|
|
4
5
|
|
|
5
6
|
import math
|
|
6
|
-
from typing import List, Tuple
|
|
7
7
|
|
|
8
8
|
import numpy as np
|
|
9
9
|
|
|
@@ -75,8 +75,8 @@ def gaussianDeconvolve(smaj, smin, spa, bmaj, bmin, bpa):
|
|
|
75
75
|
|
|
76
76
|
|
|
77
77
|
def gauss_factor(
|
|
78
|
-
beamConv:
|
|
79
|
-
) ->
|
|
78
|
+
beamConv: list[float], beamOrig: list[float], dx1: float = 1, dy1: float = 1
|
|
79
|
+
) -> tuple[float, float, float, float, float]:
|
|
80
80
|
"""
|
|
81
81
|
Calculates the scaling factor to be applied after convolving
|
|
82
82
|
a map in Jy/beam with a gaussian to get fluxes in Jy/beam again.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env python
|
|
2
|
-
"""
|
|
2
|
+
"""Convolve ASKAP images to common resolution"""
|
|
3
|
+
|
|
3
4
|
from __future__ import annotations
|
|
4
5
|
|
|
5
6
|
__author__ = "Alec Thomson"
|
|
@@ -7,9 +8,8 @@ __author__ = "Alec Thomson"
|
|
|
7
8
|
|
|
8
9
|
import logging
|
|
9
10
|
import sys
|
|
10
|
-
import traceback
|
|
11
11
|
from pathlib import Path
|
|
12
|
-
from typing import Literal, NamedTuple
|
|
12
|
+
from typing import Literal, NamedTuple
|
|
13
13
|
|
|
14
14
|
import numpy as np
|
|
15
15
|
from astropy import units as u
|
|
@@ -290,7 +290,7 @@ def beamcon_2d_on_fits(
|
|
|
290
290
|
def get_common_beam(
|
|
291
291
|
files: list[Path],
|
|
292
292
|
conv_mode: Literal["robust", "scipy", "astropy", "astropy_fft"] = "robust",
|
|
293
|
-
target_beam:
|
|
293
|
+
target_beam: Beam | None = None,
|
|
294
294
|
cutoff: float | None = None,
|
|
295
295
|
tolerance: float = 0.0001,
|
|
296
296
|
nsamps: float = 200,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
2
|
+
"""Convolve ASKAP cubes to common resolution"""
|
|
3
|
+
|
|
3
4
|
from __future__ import annotations
|
|
4
5
|
|
|
5
6
|
__author__ = "Alec Thomson"
|
|
@@ -134,45 +135,44 @@ def get_beams(file: Path, header: fits.Header) -> CubeBeams:
|
|
|
134
135
|
beams = Table.read(hdu)
|
|
135
136
|
|
|
136
137
|
# Otherwise use beamlog file
|
|
138
|
+
elif beamlog.exists():
|
|
139
|
+
logger.info("No CASA beamtable found in header - looking for beamlogs")
|
|
140
|
+
logger.info(f"Getting beams from {beamlog}")
|
|
141
|
+
|
|
142
|
+
beams = Table.read(beamlog, format="ascii.commented_header")
|
|
143
|
+
# Header looks like:
|
|
144
|
+
# colnames=['Channel', 'BMAJarcsec', 'BMINarcsec', 'BPAdeg']
|
|
145
|
+
# But needs some fix up - astropy can't read the header properly
|
|
146
|
+
for col in beams.colnames:
|
|
147
|
+
idx = col.find("[")
|
|
148
|
+
if idx == -1:
|
|
149
|
+
new_col = col
|
|
150
|
+
unit = u.Unit("")
|
|
151
|
+
else:
|
|
152
|
+
new_col = col[:idx]
|
|
153
|
+
unit = u.Unit(col[idx + 1 : -1])
|
|
154
|
+
beams[col].unit = unit
|
|
155
|
+
beams[col].name = new_col
|
|
137
156
|
else:
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
unit = u.Unit(col[idx + 1 : -1])
|
|
154
|
-
beams[col].unit = unit
|
|
155
|
-
beams[col].name = new_col
|
|
156
|
-
else:
|
|
157
|
-
logger.warning("No beamlog found")
|
|
158
|
-
logger.warning("Using header beam - assuming a constant beam")
|
|
159
|
-
try:
|
|
160
|
-
beam: Beam = Beam.from_fits_header(header)
|
|
161
|
-
wcs = WCS(header)
|
|
162
|
-
spec_axis = wcs.spectral
|
|
163
|
-
_nchan = spec_axis.array_shape[0]
|
|
164
|
-
beams = Table()
|
|
165
|
-
beams.add_column(np.arange(_nchan), name="Channel")
|
|
166
|
-
beams.add_column([beam.major.to(u.arcsec)] * _nchan, name="BMAJ")
|
|
167
|
-
beams.add_column(
|
|
168
|
-
[beam.minor.to(u.arcsec)] * _nchan,
|
|
169
|
-
name="BMIN",
|
|
170
|
-
)
|
|
171
|
-
beams.add_column([beam.pa.to(u.deg)] * _nchan, name="BPA")
|
|
157
|
+
logger.warning("No beamlog found")
|
|
158
|
+
logger.warning("Using header beam - assuming a constant beam")
|
|
159
|
+
try:
|
|
160
|
+
beam: Beam = Beam.from_fits_header(header)
|
|
161
|
+
wcs = WCS(header)
|
|
162
|
+
spec_axis = wcs.spectral
|
|
163
|
+
_nchan = spec_axis.array_shape[0]
|
|
164
|
+
beams = Table()
|
|
165
|
+
beams.add_column(np.arange(_nchan), name="Channel")
|
|
166
|
+
beams.add_column([beam.major.to(u.arcsec)] * _nchan, name="BMAJ")
|
|
167
|
+
beams.add_column(
|
|
168
|
+
[beam.minor.to(u.arcsec)] * _nchan,
|
|
169
|
+
name="BMIN",
|
|
170
|
+
)
|
|
171
|
+
beams.add_column([beam.pa.to(u.deg)] * _nchan, name="BPA")
|
|
172
172
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
173
|
+
except Exception as e:
|
|
174
|
+
logger.error("Couldn't get beam from header")
|
|
175
|
+
raise e
|
|
176
176
|
|
|
177
177
|
nchan = len(beams)
|
|
178
178
|
return CubeBeams(beams, nchan, beamlog)
|
|
@@ -252,7 +252,7 @@ def smooth_plane(
|
|
|
252
252
|
slicer = tuple(slicer)
|
|
253
253
|
|
|
254
254
|
plane = cube.unmasked_data[slicer].value.astype(np.float32)
|
|
255
|
-
logger.debug(f"Size of plane is {(plane.nbytes*u.byte).to(u.MB)}")
|
|
255
|
+
logger.debug(f"Size of plane is {(plane.nbytes * u.byte).to(u.MB)}")
|
|
256
256
|
|
|
257
257
|
if mask:
|
|
258
258
|
logger.info(f"Masking channel {idx}")
|
|
@@ -316,41 +316,21 @@ def make_data(files: list[Path], outdir: list[Path]) -> list[CubeData]:
|
|
|
316
316
|
return cube_data_list
|
|
317
317
|
|
|
318
318
|
|
|
319
|
-
def
|
|
319
|
+
def _get_commonbeams(
|
|
320
320
|
cube_data_list: list[CubeData],
|
|
321
321
|
nchans: int,
|
|
322
322
|
conv_mode: Literal["robust", "scipy", "astropy", "astropy_fft"] = "robust",
|
|
323
323
|
mode: Literal["natural", "total"] = "natural",
|
|
324
|
-
suffix: str | None = None,
|
|
325
324
|
target_beam: Beam | None = None,
|
|
326
325
|
circularise: bool = False,
|
|
327
326
|
tolerance: float = 0.0001,
|
|
328
327
|
nsamps: int = 200,
|
|
329
328
|
epsilon: float = 0.0005,
|
|
330
|
-
) ->
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
Args:
|
|
335
|
-
cube_data_list (list[CubeData]): list of cube data.
|
|
336
|
-
nchans (int): Number of channels.
|
|
337
|
-
conv_mode (Literal["robust", "scipy", "astropy", "astropy_fft"], optional): Convolution mode. Defaults to "robust".
|
|
338
|
-
mode (Literal["natural", "total"], optional): Frequency mode. Defaults to "natural".
|
|
339
|
-
target_beam (Beam, optional): Target PSF. Defaults to None.
|
|
340
|
-
circularise (bool, optional): Circularise PSF. Defaults to False.
|
|
341
|
-
tolerance (float, optional): Common beam tolerance. Defaults to 0.0001.
|
|
342
|
-
nsamps (int, optional): Common beam samples. Defaults to 200.
|
|
343
|
-
epsilon (float, optional): Common beam epsilon. Defaults to 0.0005.
|
|
344
|
-
|
|
345
|
-
Raises:
|
|
346
|
-
Exception: If convolving beam will be undersampled on pixel grid.
|
|
329
|
+
) -> Beams:
|
|
330
|
+
assert isinstance(target_beam, Beam) or target_beam is None, (
|
|
331
|
+
f"Expected target_beam to be type Beam or None, got {type(target_beam)}"
|
|
332
|
+
)
|
|
347
333
|
|
|
348
|
-
Returns:
|
|
349
|
-
list[CommonBeamData]: Common beam data for each channel and cube.
|
|
350
|
-
"""
|
|
351
|
-
if suffix is None:
|
|
352
|
-
suffix = mode
|
|
353
|
-
### Natural mode ###
|
|
354
334
|
if mode == "natural":
|
|
355
335
|
big_beams = []
|
|
356
336
|
for n in trange(
|
|
@@ -453,8 +433,8 @@ def commonbeamer(
|
|
|
453
433
|
f"Smallest common Nyquist sampled beam is: {nyq_beam!r}"
|
|
454
434
|
)
|
|
455
435
|
|
|
456
|
-
logger.
|
|
457
|
-
logger.
|
|
436
|
+
logger.warning("COMMON BEAM WILL BE UNDERSAMPLED!")
|
|
437
|
+
logger.warning("SETTING COMMON BEAM TO NYQUIST BEAM")
|
|
458
438
|
commonbeam = nyq_beam
|
|
459
439
|
|
|
460
440
|
bmaj_common.append(commonbeam.major.to(u.arcsec).value)
|
|
@@ -500,12 +480,13 @@ def commonbeamer(
|
|
|
500
480
|
tolerance=tolerance, nsamps=nsamps, epsilon=epsilon
|
|
501
481
|
)
|
|
502
482
|
except BeamError:
|
|
503
|
-
logger.
|
|
504
|
-
logger.
|
|
483
|
+
logger.warning("Couldn't find common beam with defaults")
|
|
484
|
+
logger.warning("Trying again with smaller tolerance")
|
|
505
485
|
|
|
506
486
|
commonbeam = big_beams[~np.isnan(big_beams)].common_beam(
|
|
507
487
|
tolerance=tolerance * 0.1, nsamps=nsamps, epsilon=epsilon
|
|
508
488
|
)
|
|
489
|
+
|
|
509
490
|
if target_beam is not None:
|
|
510
491
|
commonbeam = target_beam
|
|
511
492
|
else:
|
|
@@ -543,11 +524,11 @@ def commonbeamer(
|
|
|
543
524
|
if target_beam is not None:
|
|
544
525
|
commonbeam = target_beam
|
|
545
526
|
if target_beam < nyq_beam:
|
|
546
|
-
logger.
|
|
527
|
+
logger.warning("TARGET BEAM WILL BE UNDERSAMPLED!")
|
|
547
528
|
raise Exception("CAN'T UNDERSAMPLE BEAM - EXITING")
|
|
548
529
|
else:
|
|
549
|
-
logger.
|
|
550
|
-
logger.
|
|
530
|
+
logger.warning("COMMON BEAM WILL BE UNDERSAMPLED!")
|
|
531
|
+
logger.warning("SETTING COMMON BEAM TO NYQUIST BEAM")
|
|
551
532
|
commonbeam = nyq_beam
|
|
552
533
|
|
|
553
534
|
# Make Beams object
|
|
@@ -565,6 +546,61 @@ def commonbeamer(
|
|
|
565
546
|
pa=commonbeams.pa * 0,
|
|
566
547
|
)
|
|
567
548
|
|
|
549
|
+
return commonbeams
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
def commonbeamer(
|
|
553
|
+
cube_data_list: list[CubeData],
|
|
554
|
+
nchans: int,
|
|
555
|
+
conv_mode: Literal["robust", "scipy", "astropy", "astropy_fft"] = "robust",
|
|
556
|
+
mode: Literal["natural", "total"] = "natural",
|
|
557
|
+
suffix: str | None = None,
|
|
558
|
+
target_beam: Beam | Beams | None = None,
|
|
559
|
+
circularise: bool = False,
|
|
560
|
+
tolerance: float = 0.0001,
|
|
561
|
+
nsamps: int = 200,
|
|
562
|
+
epsilon: float = 0.0005,
|
|
563
|
+
) -> list[CommonBeamData]:
|
|
564
|
+
"""Find common beam for all channels.
|
|
565
|
+
Computed beams will be written to convolving beam logger.
|
|
566
|
+
|
|
567
|
+
Args:
|
|
568
|
+
cube_data_list (List[CubeData]): List of cube data.
|
|
569
|
+
nchans (int): Number of channels.
|
|
570
|
+
conv_mode (Literal["robust", "scipy", "astropy", "astropy_fft"], optional): Convolution mode. Defaults to "robust".
|
|
571
|
+
mode (Literal["natural", "total"], optional): Frequency mode. Defaults to "natural".
|
|
572
|
+
target_beam (Optional[Union[Beam,List[Beam]]], optional): Target PSF. If list of beams, each corresponds to a channel resolution. Defaults to None.
|
|
573
|
+
circularise (bool, optional): Circularise PSF. Defaults to False.
|
|
574
|
+
tolerance (float, optional): Common beam tolerance. Defaults to 0.0001.
|
|
575
|
+
nsamps (int, optional): Common beam samples. Defaults to 200.
|
|
576
|
+
epsilon (float, optional): Common beam epsilon. Defaults to 0.0005.
|
|
577
|
+
|
|
578
|
+
Raises:
|
|
579
|
+
Exception: If convolving beam will be undersampled on pixel grid.
|
|
580
|
+
|
|
581
|
+
Returns:
|
|
582
|
+
List[CommonBeamData]: Common beam data for each channel and cube.
|
|
583
|
+
"""
|
|
584
|
+
if suffix is None:
|
|
585
|
+
suffix = mode
|
|
586
|
+
### Natural mode ###
|
|
587
|
+
|
|
588
|
+
if isinstance(target_beam, Beams):
|
|
589
|
+
logger.info(f"Setting the target beam for {len(target_beam)} channels")
|
|
590
|
+
commonbeams = target_beam
|
|
591
|
+
else:
|
|
592
|
+
commonbeams = _get_commonbeams(
|
|
593
|
+
cube_data_list=cube_data_list,
|
|
594
|
+
nchans=nchans,
|
|
595
|
+
conv_mode=conv_mode,
|
|
596
|
+
mode=mode,
|
|
597
|
+
target_beam=target_beam,
|
|
598
|
+
circularise=circularise,
|
|
599
|
+
tolerance=tolerance,
|
|
600
|
+
nsamps=nsamps,
|
|
601
|
+
epsilon=epsilon,
|
|
602
|
+
)
|
|
603
|
+
|
|
568
604
|
logger.info("Final beams are:")
|
|
569
605
|
for i, commonbeam in enumerate(commonbeams):
|
|
570
606
|
logger.info(f"Channel {i}: {commonbeam!r}")
|
|
@@ -731,9 +767,9 @@ def initfiles(
|
|
|
731
767
|
spec_axis = wcs.spectral
|
|
732
768
|
crpix = int(spec_axis.wcs.crpix)
|
|
733
769
|
nchans = spec_axis.array_shape[0]
|
|
734
|
-
assert nchans == len(
|
|
735
|
-
commonbeams
|
|
736
|
-
)
|
|
770
|
+
assert nchans == len(commonbeams), (
|
|
771
|
+
"Number of channels in header and commonbeams do not match"
|
|
772
|
+
)
|
|
737
773
|
chans = np.arange(nchans)
|
|
738
774
|
if ref_chan is None:
|
|
739
775
|
# Get reference channel, and attach PSF there
|
|
@@ -911,6 +947,71 @@ def smooth_and_write_plane(
|
|
|
911
947
|
logger.info(f"{outfile} - channel {chan} - Done")
|
|
912
948
|
|
|
913
949
|
|
|
950
|
+
def _get_target_beam(
|
|
951
|
+
bmaj: float | list[float] | None = None,
|
|
952
|
+
bmin: float | list[float] | None = None,
|
|
953
|
+
bpa: float | list[float] | None = None,
|
|
954
|
+
) -> None | Beam | Beams:
|
|
955
|
+
"""Appropriately handle the input target beam specification
|
|
956
|
+
|
|
957
|
+
Args:
|
|
958
|
+
bmaj (Optional[Union[float,List[float]]], optional): Target beam major axis in arcsec. If a list, this should be the same length as the number of channels to smooth. Defaults to None.
|
|
959
|
+
bmin (Optional[Union[float,List[float]]], optional): Target beam minor axis in arcsec. If a list, this should be the same length as the number of channels to smooth. Defaults to None.
|
|
960
|
+
bpa (Optional[Union[float,List[float]]], optional): Target beam position angle in deg. If a list, this should be the same length as the number of channels to smooth. Defaults to None.
|
|
961
|
+
|
|
962
|
+
Raises:
|
|
963
|
+
ValueError: Occurs if only subset of beam parameters specified
|
|
964
|
+
|
|
965
|
+
Returns:
|
|
966
|
+
Union[None, Beam, Beams]: The appropriate set of processed beams
|
|
967
|
+
"""
|
|
968
|
+
|
|
969
|
+
nonetest = [param is None for param in (bmaj, bmin, bpa)]
|
|
970
|
+
if all(nonetest):
|
|
971
|
+
return None
|
|
972
|
+
|
|
973
|
+
if any(nonetest):
|
|
974
|
+
raise ValueError("Please specify all target beam params!")
|
|
975
|
+
|
|
976
|
+
if all([isinstance(b, float) for b in (bmaj, bmin, bpa)]):
|
|
977
|
+
if any(~np.isfinite((bmaj, bmin, bpa))):
|
|
978
|
+
logger.debug(
|
|
979
|
+
f"Non-finite value detected in beam ({bmaj, bmin, bpa}). Setting all to nan."
|
|
980
|
+
)
|
|
981
|
+
bmaj = bmin = bpa = float("nan")
|
|
982
|
+
|
|
983
|
+
target_beam = Beam(bmaj * u.arcsec, bmin * u.arcsec, bpa * u.deg)
|
|
984
|
+
logger.info(f"Target beam is {target_beam!r}")
|
|
985
|
+
return target_beam
|
|
986
|
+
else:
|
|
987
|
+
assert all([isinstance(b, list) for b in (bmaj, bmin, bpa)]), (
|
|
988
|
+
"Something is not a list"
|
|
989
|
+
)
|
|
990
|
+
assert len(bmaj) == len(bmin) == len(bpa), "Unequal target beam lengths"
|
|
991
|
+
|
|
992
|
+
# Deal with moments where, potentially, a beam has a NaN but not all NaN.
|
|
993
|
+
# Maybe unnecessary.
|
|
994
|
+
for idx, beam_props in enumerate(zip(bmaj, bmin, bpa)):
|
|
995
|
+
if any(~np.isfinite(beam_props)):
|
|
996
|
+
logger.debug(
|
|
997
|
+
f"Non-finite beam property detected in {beam_props=} at {idx=}. Setting all to nan. "
|
|
998
|
+
)
|
|
999
|
+
bmaj[idx] = float("nan")
|
|
1000
|
+
bmin[idx] = float("nan")
|
|
1001
|
+
bpa[idx] = float("nan")
|
|
1002
|
+
|
|
1003
|
+
target_beams = Beams(
|
|
1004
|
+
np.array(bmaj) * u.arcsec,
|
|
1005
|
+
np.array(bmin) * u.arcsec,
|
|
1006
|
+
np.array(bpa) * u.degree,
|
|
1007
|
+
)
|
|
1008
|
+
|
|
1009
|
+
logger.info(f"{len(target_beams)} target beams have been constructed")
|
|
1010
|
+
return target_beams
|
|
1011
|
+
|
|
1012
|
+
raise ValueError("Beam parameters were not successfully processed")
|
|
1013
|
+
|
|
1014
|
+
|
|
914
1015
|
def smooth_fits_cube(
|
|
915
1016
|
infiles_list: list[Path],
|
|
916
1017
|
uselogs: bool = False,
|
|
@@ -920,9 +1021,9 @@ def smooth_fits_cube(
|
|
|
920
1021
|
prefix: str | None = None,
|
|
921
1022
|
suffix: str | None = None,
|
|
922
1023
|
outdir: Path | None = None,
|
|
923
|
-
bmaj: float | None = None,
|
|
924
|
-
bmin: float | None = None,
|
|
925
|
-
bpa: float | None = None,
|
|
1024
|
+
bmaj: float | list[float] | None = None,
|
|
1025
|
+
bmin: float | list[float] | None = None,
|
|
1026
|
+
bpa: float | list[float] | None = None,
|
|
926
1027
|
cutoff: float | None = None,
|
|
927
1028
|
circularise: bool = False,
|
|
928
1029
|
ref_chan: int | None = None,
|
|
@@ -943,11 +1044,11 @@ def smooth_fits_cube(
|
|
|
943
1044
|
dryrun (bool, optional): Do not write any images. Defaults to False.
|
|
944
1045
|
prefix (str, optional): Output filename prefix. Defaults to None.
|
|
945
1046
|
suffix (str, optional): Output filename suffix. Defaults to None.
|
|
946
|
-
outdir (Path
|
|
947
|
-
bmaj (float
|
|
948
|
-
bmin (float
|
|
949
|
-
bpa (float
|
|
950
|
-
cutoff (float
|
|
1047
|
+
outdir (Optional[Path], optional): Output directory. Defaults to None.
|
|
1048
|
+
bmaj (Optional[Union[float,List[float]]], optional): Target beam major axis in arcsec. If a list, this should be the same length as the number of channels to smooth. Defaults to None.
|
|
1049
|
+
bmin (Optional[Union[float,List[float]]], optional): Target beam minor axis in arcsec. If a list, this should be the same length as the number of channels to smooth. Defaults to None.
|
|
1050
|
+
bpa (Optional[Union[float,List[float]]], optional): Target beam position angle in deg. If a list, this should be the same length as the number of channels to smooth. Defaults to None.
|
|
1051
|
+
cutoff (Optional[float], optional): Beam cutoff in arcsec. Defaults to None.
|
|
951
1052
|
circularise (bool, optional): Set minor axis to major axis. Defaults to False.
|
|
952
1053
|
ref_chan (int | None, optional): Reference channel for PSF in header. Defaults to None.
|
|
953
1054
|
tolerance (float, optional): Radio beam tolerance. Defaults to 0.0001.
|
|
@@ -990,14 +1091,7 @@ def smooth_fits_cube(
|
|
|
990
1091
|
# Check target
|
|
991
1092
|
conv_mode = parse_conv_mode(conv_mode)
|
|
992
1093
|
|
|
993
|
-
|
|
994
|
-
if all(nonetest):
|
|
995
|
-
target_beam = None
|
|
996
|
-
elif any(nonetest):
|
|
997
|
-
raise ValueError("Please specify all target beam params!")
|
|
998
|
-
else:
|
|
999
|
-
target_beam = Beam(bmaj * u.arcsec, bmin * u.arcsec, bpa * u.deg)
|
|
1000
|
-
logger.info(f"Target beam is {target_beam!r}")
|
|
1094
|
+
target_beam = _get_target_beam(bmaj=bmaj, bmin=bmin, bpa=bpa)
|
|
1001
1095
|
|
|
1002
1096
|
files = sorted(infiles_list)
|
|
1003
1097
|
if len(files) == 0:
|
|
@@ -1011,11 +1105,14 @@ def smooth_fits_cube(
|
|
|
1011
1105
|
|
|
1012
1106
|
# Sanity check channel counts
|
|
1013
1107
|
nchans = np.array([cube_data.nchan for cube_data in cube_data_list])
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
if not check:
|
|
1108
|
+
if not all(nchans == nchans[0]):
|
|
1017
1109
|
raise ValueError(f"Unequal number of spectral channels! Got {nchans}")
|
|
1018
1110
|
|
|
1111
|
+
if isinstance(target_beam, Beams) and any(nchans != len(target_beam)):
|
|
1112
|
+
raise ValueError(
|
|
1113
|
+
f"Unequal length of target beams ({len(target_beam)}) to channels ({nchans})"
|
|
1114
|
+
)
|
|
1115
|
+
|
|
1019
1116
|
nchans = nchans[0]
|
|
1020
1117
|
|
|
1021
1118
|
# Check suffix
|
|
@@ -1215,7 +1312,8 @@ def cli():
|
|
|
1215
1312
|
dest="bmaj",
|
|
1216
1313
|
type=float,
|
|
1217
1314
|
default=None,
|
|
1218
|
-
|
|
1315
|
+
nargs="+",
|
|
1316
|
+
help="BMAJ to convolve to [max BMAJ from given image(s)]. If multiple are give they will be matched to each channel image.",
|
|
1219
1317
|
)
|
|
1220
1318
|
|
|
1221
1319
|
parser.add_argument(
|
|
@@ -1223,11 +1321,17 @@ def cli():
|
|
|
1223
1321
|
dest="bmin",
|
|
1224
1322
|
type=float,
|
|
1225
1323
|
default=None,
|
|
1226
|
-
|
|
1324
|
+
nargs="+",
|
|
1325
|
+
help="BMIN to convolve to [max BMAJ from given image(s)] .If multiple are give they will be matched to each channel image.",
|
|
1227
1326
|
)
|
|
1228
1327
|
|
|
1229
1328
|
parser.add_argument(
|
|
1230
|
-
"--bpa",
|
|
1329
|
+
"--bpa",
|
|
1330
|
+
dest="bpa",
|
|
1331
|
+
type=float,
|
|
1332
|
+
default=None,
|
|
1333
|
+
nargs="+",
|
|
1334
|
+
help="BPA to convolve to [0]. If multiple are give they will be matched to each channel image.",
|
|
1231
1335
|
)
|
|
1232
1336
|
|
|
1233
1337
|
parser.add_argument(
|
|
@@ -1309,6 +1413,10 @@ def cli():
|
|
|
1309
1413
|
verbosity=args.verbosity,
|
|
1310
1414
|
)
|
|
1311
1415
|
|
|
1416
|
+
bmaj = args.bmaj if args.bmaj is None or len(args.bmaj) > 1 else args.bmaj[0]
|
|
1417
|
+
bmin = args.bmin if args.bmin is None or len(args.bmin) > 1 else args.bmin[0]
|
|
1418
|
+
bpa = args.bpa if args.bpa is None or len(args.bpa) > 1 else args.bpa[0]
|
|
1419
|
+
|
|
1312
1420
|
_ = smooth_fits_cube(
|
|
1313
1421
|
infiles_list=args.infile,
|
|
1314
1422
|
uselogs=args.uselogs,
|
|
@@ -1318,9 +1426,9 @@ def cli():
|
|
|
1318
1426
|
prefix=args.prefix,
|
|
1319
1427
|
suffix=args.suffix,
|
|
1320
1428
|
outdir=args.outdir,
|
|
1321
|
-
bmaj=
|
|
1322
|
-
bmin=
|
|
1323
|
-
bpa=
|
|
1429
|
+
bmaj=bmaj,
|
|
1430
|
+
bmin=bmin,
|
|
1431
|
+
bpa=bpa,
|
|
1324
1432
|
cutoff=args.cutoff,
|
|
1325
1433
|
circularise=args.circularise,
|
|
1326
1434
|
ref_chan=args.ref_chan,
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
2
|
+
"""Fast convolution in the UV domain"""
|
|
3
|
+
|
|
3
4
|
__author__ = "Wasim Raja"
|
|
4
5
|
|
|
5
6
|
import gc
|
|
6
|
-
from typing import Literal, NamedTuple, Optional
|
|
7
|
+
from typing import Literal, NamedTuple, Optional
|
|
7
8
|
|
|
8
|
-
import astropy.units as units
|
|
9
9
|
import numpy as np
|
|
10
10
|
import scipy.signal
|
|
11
|
-
from astropy import convolution
|
|
11
|
+
from astropy import convolution, units
|
|
12
12
|
from astropy import units as u
|
|
13
13
|
from astropy.io import fits
|
|
14
14
|
from astropy.wcs import WCS
|
|
@@ -16,8 +16,7 @@ from astropy.wcs.utils import proj_plane_pixel_scales
|
|
|
16
16
|
from radio_beam import Beam, Beams
|
|
17
17
|
from radio_beam.utils import BeamError
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
from racs_tools import au2
|
|
19
|
+
from racs_tools import au2, gaussft
|
|
21
20
|
from racs_tools.logging import logger
|
|
22
21
|
|
|
23
22
|
ZERO_BEAM = Beam(0 * u.deg)
|
|
@@ -184,7 +183,7 @@ def convolve(
|
|
|
184
183
|
# Create a mask for the NaNs
|
|
185
184
|
mask = np.isnan(image).astype(np.int16)
|
|
186
185
|
logger.warning(
|
|
187
|
-
f"Image contains {mask.sum()} ({mask.sum()/ mask.size *100
|
|
186
|
+
f"Image contains {mask.sum()} ({mask.sum() / mask.size * 100:0.1f}%) NaNs"
|
|
188
187
|
)
|
|
189
188
|
image = np.nan_to_num(image)
|
|
190
189
|
|
|
@@ -236,7 +235,7 @@ def convolve(
|
|
|
236
235
|
# Need this to get around numerical issues
|
|
237
236
|
mask_conv = ~(mask_conv + 1 < 2)
|
|
238
237
|
logger.warning(
|
|
239
|
-
f"Convolved image contains {mask_conv.sum()} ({mask_conv.sum()/ mask_conv.size *100
|
|
238
|
+
f"Convolved image contains {mask_conv.sum()} ({mask_conv.sum() / mask_conv.size * 100:0.1f}%) NaNs"
|
|
240
239
|
)
|
|
241
240
|
im_conv[mask_conv > 0] = np.nan
|
|
242
241
|
|
|
@@ -414,7 +413,7 @@ def parse_conv_mode(
|
|
|
414
413
|
str: Convolution mode.
|
|
415
414
|
"""
|
|
416
415
|
logger.info(f"Convolution mode: {conv_mode}")
|
|
417
|
-
if conv_mode not in CONVOLUTION_FUNCTIONS
|
|
416
|
+
if conv_mode not in CONVOLUTION_FUNCTIONS:
|
|
418
417
|
raise ValueError(
|
|
419
418
|
f"Please select valid convolution method! Expected one of {list(CONVOLUTION_FUNCTIONS.keys())}, got {conv_mode}"
|
|
420
419
|
)
|
|
@@ -436,7 +435,7 @@ def get_convolving_beam(
|
|
|
436
435
|
dx: u.Quantity,
|
|
437
436
|
dy: u.Quantity,
|
|
438
437
|
cutoff: Optional[float] = None,
|
|
439
|
-
) ->
|
|
438
|
+
) -> tuple[Beam, float]:
|
|
440
439
|
"""Get the beam to use for smoothing
|
|
441
440
|
|
|
442
441
|
Args:
|
|
@@ -6,7 +6,6 @@ psf. The intrinsic psf must be specified.
|
|
|
6
6
|
|
|
7
7
|
Python version of gaussft.f by Wasim Raja
|
|
8
8
|
"""
|
|
9
|
-
from typing import Tuple
|
|
10
9
|
|
|
11
10
|
import numba as nb
|
|
12
11
|
import numpy as np
|
|
@@ -22,7 +21,7 @@ def gaussft(
|
|
|
22
21
|
bpa: float,
|
|
23
22
|
u: np.ndarray,
|
|
24
23
|
v: np.ndarray,
|
|
25
|
-
) ->
|
|
24
|
+
) -> tuple[np.ndarray, float]:
|
|
26
25
|
"""
|
|
27
26
|
Compute the Fourier transform of a 2D Gaussian for convolution.
|
|
28
27
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
2
|
+
"""Find bad channels by checking statistics of each channel image."""
|
|
3
3
|
|
|
4
4
|
import argparse
|
|
5
5
|
import warnings
|
|
6
|
-
from typing import
|
|
6
|
+
from typing import Union
|
|
7
7
|
|
|
8
8
|
import astropy.units as u
|
|
9
9
|
import numpy as np
|
|
@@ -39,7 +39,7 @@ def getbadchans(
|
|
|
39
39
|
qcube: SpectralCube,
|
|
40
40
|
ucube: SpectralCube,
|
|
41
41
|
cliplev: float = 5,
|
|
42
|
-
) ->
|
|
42
|
+
) -> tuple[np.ndarray, u.Quantity, u.Quantity]:
|
|
43
43
|
"""Find bad channels in Stokes Q and U cubes
|
|
44
44
|
|
|
45
45
|
Args:
|
|
@@ -96,7 +96,7 @@ def getbadchans(
|
|
|
96
96
|
|
|
97
97
|
def blankchans(
|
|
98
98
|
qcube: SpectralCube, ucube: SpectralCube, totalbad: np.ndarray, blank: bool = False
|
|
99
|
-
) ->
|
|
99
|
+
) -> tuple[SpectralCube, SpectralCube]:
|
|
100
100
|
"""Mask out bad channels
|
|
101
101
|
|
|
102
102
|
Args:
|
|
@@ -162,13 +162,13 @@ def main(
|
|
|
162
162
|
qcube = getcube(qfile)
|
|
163
163
|
ucube = getcube(ufile)
|
|
164
164
|
|
|
165
|
-
assert len(ucube.spectral_axis) == len(
|
|
166
|
-
|
|
167
|
-
)
|
|
165
|
+
assert len(ucube.spectral_axis) == len(qcube.spectral_axis), (
|
|
166
|
+
"Cubes have different number of channels"
|
|
167
|
+
)
|
|
168
168
|
|
|
169
169
|
# Iterate
|
|
170
170
|
for i in range(iterate):
|
|
171
|
-
logger.info(f"Flagging iteration {i+1} of {iterate}")
|
|
171
|
+
logger.info(f"Flagging iteration {i + 1} of {iterate}")
|
|
172
172
|
totalbad, qnoisevals, unoisevals = getbadchans(
|
|
173
173
|
qcube,
|
|
174
174
|
ucube,
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
2
|
|
|
4
3
|
import logging
|
|
5
4
|
import multiprocessing as mp
|
|
6
5
|
from logging.handlers import QueueHandler, QueueListener
|
|
7
|
-
from typing import Optional
|
|
6
|
+
from typing import Optional
|
|
8
7
|
|
|
9
8
|
logging.captureWarnings(True)
|
|
10
9
|
|
|
@@ -14,7 +13,7 @@ logging.captureWarnings(True)
|
|
|
14
13
|
|
|
15
14
|
def setup_logger(
|
|
16
15
|
filename: Optional[str] = None,
|
|
17
|
-
) ->
|
|
16
|
+
) -> tuple[logging.Logger, QueueListener, mp.Queue]:
|
|
18
17
|
"""Setup a logger
|
|
19
18
|
|
|
20
19
|
Args:
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
2
|
+
"""Utilities for parallelism"""
|
|
3
|
+
|
|
3
4
|
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
|
|
4
|
-
from typing import
|
|
5
|
+
from typing import Literal, TypeVar
|
|
5
6
|
|
|
6
7
|
from racs_tools.logging import logger
|
|
7
8
|
|
|
@@ -15,7 +16,7 @@ except ImportError:
|
|
|
15
16
|
|
|
16
17
|
Executor = TypeVar("Executor", ThreadPoolExecutor, ProcessPoolExecutor, MPIPoolExecutor)
|
|
17
18
|
|
|
18
|
-
EXECUTORS:
|
|
19
|
+
EXECUTORS: dict[Literal["thread", "process", "mpi"], Executor] = {
|
|
19
20
|
"thread": ThreadPoolExecutor,
|
|
20
21
|
"process": ProcessPoolExecutor,
|
|
21
22
|
"mpi": MPIPoolExecutor,
|
|
File without changes
|
|
File without changes
|