Metadata-Version: 2.4
Name: psimager
Version: 0.4
Summary: Fast dirty imaging of measurement sets for power spectrum analyses, with the ducc0 w-gridder and WSClean's options
License-Expression: GPL-3.0-or-later
License-File: LICENSE
Author: Florent Mertens
Author-email: florent.mertens@gmail.com
Requires-Python: >=3.10,<4.0
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Provides-Extra: dev
Requires-Dist: astropy (>=5.0)
Requires-Dist: click (>=8.0)
Requires-Dist: ducc0 (>=0.27)
Requires-Dist: numpy (>=1.22)
Requires-Dist: pytest (>=8.0) ; extra == "dev"
Requires-Dist: pytest-cov (>=5.0) ; extra == "dev"
Requires-Dist: python-casacore (>=3.0)
Requires-Dist: ruff (==0.16.1) ; extra == "dev"
Project-URL: Homepage, https://gitlab.com/flomertens/psimager
Project-URL: Issues, https://gitlab.com/flomertens/psimager/-/issues
Project-URL: Repository, https://gitlab.com/flomertens/psimager
Description-Content-Type: text/markdown

# psimager

Fast dirty imaging of measurement sets for power spectrum analyses: dirty Stokes and PSF images
with the [ducc0](https://gitlab.mpcdf.mpg.de/mtr/ducc) w-gridder, with the options, file names and
FITS keywords of [WSClean](https://wsclean.readthedocs.io), so that pspipe and ps_eor read them as
WSClean images.

The MSs are read once, in large row chunks, and the output channels are gridded in parallel, each
by one ducc0 call. Imaging many channels is then many times faster than with WSClean, which
reorders the data on disk and grids one image at a time.

## Installation

```bash
pip install psimager
```

## Usage

```bash
psimager -name img -size 400 400 -scale 6amin -pol IV -channels-out 363 -weight natural \
    -minuv-l 5 -maxuv-l 100 -data-column CORRECTED_DATA -even-odd -make-psf -no-dirty obs*.MS
```

writes `img-even-0000-I-image.fits`, `img-even-0000-V-image.fits`, `img-even-0000-psf.fits`, ...,
for each output channel, for the even and the odd time steps.

## Options

As WSClean's: `-name`, `-data-column`, `-pol`, `-size`, `-scale`, `-channel-range`,
`-channels-out`, `-interval`, `-intervals-out`, `-even-timesteps`, `-odd-timesteps`, `-minuv-l`,
`-maxuv-l`, `-minuvw-m`, `-maxuvw-m`, `-weight natural|uniform|briggs ROBUST`,
`-visibility-weighting-mode`, `-taper-edge`, `-padding` (of the weighting grid),
`-wgridder-accuracy`, `-j`, `-parallel-gridding`, `-mem`, `-abs-mem`, `-make-psf`, `-no-dirty`.
See `psimager --help`.

Differences with WSClean:

- `-even-odd` images the even and the odd time steps in one pass, as `NAME-even-*` and
  `NAME-odd-*`.
- `-fits-keyword KEY=VALUE` (repeatable) adds a header keyword to every output file, written
  with the file instead of updating it afterwards.
- `-parallel-gridding` defaults to `-j`: one thread per output channel, which is the fastest.
- No deconvolution and no `-MFS-` images.
- WSClean's gridding options (`-oversampling`, `-kernel-size`, `-nwlayers`, `-grid-mode`,
  `-reorder`, `-temp-dir`, ...) are accepted and ignored, with a warning, so that a WSClean command
  line runs unchanged.
- Only single-field, single-SPW MSs with linear correlations.

The data are read in chunks of at most 30 GB by default (`-abs-mem`), or `-mem` % (33) of the
node's memory if smaller, so that several psimager runs can share a node, e.g. as job steps of
a Slurm allocation; give `-abs-mem` to use more on a node of its own.

Images are in Jy/PSF (the PSF peaks at 1), one file per output channel and Stokes parameter, with
WSClean's `WSCNVIS`, `WSCENVIS`, `WSCVWSUM`, `WSCIMGWG` and `WSCNORMF` computed from the Stokes I
weights, and `BMAJ`, `BMIN`, `BPA` fitted to the PSF.

## Image cubes (`-cube`)

With `-cube`, the output channels of an image go in one file, on its third axis: one file per
parity, interval and product, `NAME[-even|-odd][-tNNNN][-I|-V]-image.fits` and
`NAME[-even|-odd][-tNNNN]-psf.fits`. A few files instead of one per channel: on a parallel file
system, opening a file costs more than reading it.

The channels are often unevenly spaced (e.g. the flagged edge channels of each subband removed),
which a linear FITS axis cannot describe; the standard tabular axis (`FREQ-TAB`) can, but CASA
does not read it. The cubes therefore have:

- **Primary HDU**: the images, `NAXIS3` = number of output channels. The frequency axis is
  linear, from the first to the last channel (`CRPIX3 = 1`, `CRVAL3` the first frequency,
  `CDELT3` the mean spacing): exact for evenly spaced channels, approximate otherwise, for
  viewers. The keywords common to all channels are in the header (`WSCDATAC`, `WSCWEIGH`,
  `WSCCHANS`, `WSCCHANE`, `PSIVERSI`, `-fits-keyword`); the per-channel ones are in the tables.
- **`CHANNELS`** extension, one row per output channel, the reference for the frequencies:

  | Column | Format | Unit | |
  |---|---|---|---|
  | `CHAN` | `1J` | | output channel, from 0 |
  | `FREQ` | `1D` | Hz | centre: mean of the input channel frequencies |
  | `CHWIDTH` | `1D` | Hz | sum of the input channel widths |
  | `WSCNVIS`, `WSCENVIS`, `WSCVWSUM`, `WSCIMGWG`, `WSCNORMF` | `1D` | | as the keywords of WSClean |

- **`BEAMS`** extension (with `-make-psf`), CASA's per-channel beams, read by CASA,
  spectral-cube and radio-beam: columns `BMAJ`, `BMIN` (`1E`, arcsec), `BPA` (`1E`, deg; NaN
  where the PSF fit failed), `CHAN`, `POL` (`1J`, from 0; `POL` = 0), keywords `NCHAN`,
  `NPOL` = 1, and `CASAMBM = T` in the primary header. It is the first extension, before
  `CHANNELS`: with `CASAMBM`, CASA reads the beams from the first extension.

The tables are the same in the files of an image (Stokes parameters and PSF): the statistics come
from the Stokes I weights, the beams from the PSF. A channel without visibilities is a zero
plane, with a zero beam and zero statistics. With `-channels-out 1`, `-cube` changes nothing.

`psimager-cube OUTPUT FILE...` writes the single-channel files of an image (psimager or WSClean)
as such a cube, in frequency order; their keywords other than the per-channel ones must be the
same.

