"""DADA to measurement set conversion wrapper.
Provides Python interface to Stephen Bourke's dada2ms tool for
converting OVRO-LWA correlator DADA files to CASA measurement sets.
"""
import logging
from os import path
import os
import subprocess
# TODO turn this into a class and pull theses things into a config.
[docs]
dada2ms_exec = '/opt/astro/dada2ms/bin/dada2ms-tst3'
[docs]
dada2ms_config = '/home/yuping/dada2ms.cfg'
NEW_ENV = new_env = dict(os.environ, LD_LIBRARY_PATH='/opt/astro/mwe/usr/lib64:/opt/astro/lib/:/opt/astro/casacore-1.7.0/lib',
AIPSPATH='/opt/astro/casa-data dummy dummy')
[docs]
def dada2ms(dada_file: str, out_ms: str, gaintable: str = None, addspw: bool = False) -> str:
"""Wrapper around Stephen Bourke's dada2ms.
Optionally apply a gaintable (only gaintable of type bandpass has been tested). It will write a ms with the data in
the DATA column. If the directory of the out_ms does not exist, it will create the directory.
Args:
dada_file: Path to the dada file.
out_ms: Path to the output measurement set.
gaintable: Path to the gaintable. Default is None which means don't apply the calibration.
addspw: Use the --apend --addspw options.
Returns: Path to the output ms. The same as out_ms
"""
if not addspw:
os.makedirs(out_ms, exist_ok=False)
if gaintable and (not addspw):
proc = subprocess.Popen(
[dada2ms_exec, '-c', dada2ms_config, '--cal', gaintable, dada_file, out_ms], env=NEW_ENV,
stderr=subprocess.PIPE, stdout=subprocess.PIPE
)
elif gaintable and addspw:
proc = subprocess.Popen(
[dada2ms_exec, '-c', dada2ms_config, '--cal', gaintable, '--append', '--addspw', dada_file, out_ms], env=NEW_ENV,
stderr=subprocess.PIPE, stdout=subprocess.PIPE
)
elif addspw:
proc = subprocess.Popen(
[dada2ms_exec, '-c', dada2ms_config, '--append', '--addspw', dada_file, out_ms], env=NEW_ENV, stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
else:
proc = subprocess.Popen(
[dada2ms_exec, '-c', dada2ms_config, dada_file, out_ms], env=NEW_ENV, stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
stdoutdata, stderrdata = proc.communicate()
if not (proc.returncode == 0):
logging.error(f'Error in data2ms: {stderrdata.decode()}')
raise Exception('Error in dada2ms.')
return path.abspath(out_ms)