Skip to content

Commit

Permalink
Fix autos to be real on file write-out by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhazelton authored and mkolopanis committed May 10, 2022
1 parent 771e491 commit 1e17641
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
9 changes: 9 additions & 0 deletions pyuvsim/tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def test_run_paramfile_uvsim(goto_tempdir, paramfile):
uv_ref.read_uvfits(os.path.join(SIM_DATA_PATH, 'testfile_singlesource.uvfits'))
uv_ref.unphase_to_drift(use_ant_pos=True)
uv_ref.reorder_blts("time", minor_order="baseline")
# This is an old file with the bug that added one to the
# antenna numbers for uvfits files. Fix them (if pyuvdata is recent)
if np.min(np.union1d(uv_ref.ant_1_array, uv_ref.ant_2_array)) > 0:
uv_ref.ant_1_array = uv_ref.ant_1_array - 1
uv_ref.ant_2_array = uv_ref.ant_2_array - 1
uv_ref.antenna_numbers = uv_ref.antenna_numbers - 1
uv_ref.baseline_array = uv_ref.antnums_to_baseline(
uv_ref.ant_1_array, uv_ref.ant_2_array
)

# set the x_orientation
uv_ref.x_orientation = "east"
Expand Down
10 changes: 10 additions & 0 deletions pyuvsim/tests/test_simsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,16 @@ def test_param_reader():
param_filename = os.path.join(SIM_DATA_PATH, "test_config", "param_10time_10chan_0.yaml")
hera_uv = UVData()
hera_uv.read_uvfits(triangle_uvfits_file)
# This is an old file with the bug that added one to the
# antenna numbers for uvfits files. Fix them (if pyuvdata is recent)
if np.min(np.union1d(hera_uv.ant_1_array, hera_uv.ant_2_array)) > 0:
hera_uv.ant_1_array = hera_uv.ant_1_array - 1
hera_uv.ant_2_array = hera_uv.ant_2_array - 1
hera_uv.antenna_numbers = hera_uv.antenna_numbers - 1
hera_uv.baseline_array = hera_uv.antnums_to_baseline(
hera_uv.ant_1_array, hera_uv.ant_2_array
)

hera_uv.use_future_array_shapes()
# set missing x_orientation
hera_uv.x_orientation = "east"
Expand Down
22 changes: 22 additions & 0 deletions pyuvsim/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

import numpy as np
import pytest
from packaging import version # packaging is installed with setuptools
import pyuvdata
from pyuvdata import UVData
import pyuvdata.tests as uvtest

from pyuvsim import utils as simutils
from pyuvsim.data import DATA_PATH as SIM_DATA_PATH
Expand Down Expand Up @@ -223,6 +226,25 @@ def test_write_uvdata_clobber(save_format, tmpdir):
assert uv2 == uv


@pytest.mark.filterwarnings("ignore:LST values stored in this file are not self-consistent")
def test_write_fix_autos(tmpdir):
uv = UVData()
uv.read_uvfits(triangle_uvfits_file)
uv.set_lsts_from_time_array()

auto_screen = uv.ant_1_array == uv.ant_2_array
uv.data_array[auto_screen] += 1e-11 * complex(0, 1)

ofname = str(tmpdir.join('test_file'))
filing_dict = {'outfile_name': ofname}

if version.parse(pyuvdata.__version__) >= version.parse("2.2.7"):
with uvtest.check_warnings(
UserWarning, match="Fixing auto-correlations to be be real-only"
):
simutils.write_uvdata(uv, filing_dict, return_filename=True, out_format='uvh5')


@pytest.mark.filterwarnings("ignore:LST values stored in this file are not self-consistent")
def test_write_error_with_no_format(tmpdir):
"""Test write_uvdata will error if no format is given."""
Expand Down
25 changes: 21 additions & 4 deletions pyuvsim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import numpy as np
import psutil
from packaging import version # packaging is installed with setuptools
import pyuvdata

from . import __version__

Expand Down Expand Up @@ -200,6 +202,7 @@ def write_uvdata(
return_filename=False,
dryrun=False,
out_format=None,
fix_autos=True,
):
"""
Parse output file information from parameters and write uvfits to file.
Expand All @@ -216,6 +219,10 @@ def write_uvdata(
(Default false) Don't write to file.
out_format : Str
(Default uvfits) Write as uvfits/miriad/uvh5/ms
fix_autos : bool
If auto-correlations with imaginary values are found, fix those values so
that they are real-only in data_array. This is only passed if the pyuvdata
version is >= 2.2.7.
Returns
-------
Expand Down Expand Up @@ -263,14 +270,24 @@ def write_uvdata(
if noclobber:
outfile_name = check_file_exists_and_increment(outfile_name)

if version.parse(pyuvdata.__version__) >= version.parse("2.2.7"):
write_kwargs = {'fix_autos': fix_autos}
else:
write_kwargs = {}

print('Outfile path: ', outfile_name, flush=True)
if not dryrun:
if out_format == 'uvfits':
uv_obj.write_uvfits(outfile_name, force_phase=True, spoof_nonessential=True)
uv_obj.write_uvfits(
outfile_name,
force_phase=True,
spoof_nonessential=True,
**write_kwargs,
)
elif out_format == 'miriad':
uv_obj.write_miriad(outfile_name, clobber=not noclobber)
uv_obj.write_miriad(outfile_name, clobber=not noclobber, **write_kwargs)
elif out_format == 'uvh5':
uv_obj.write_uvh5(outfile_name, clobber=not noclobber)
uv_obj.write_uvh5(outfile_name, clobber=not noclobber, **write_kwargs)
elif out_format == 'ms':
try:
import casacore.tables # noqa
Expand All @@ -286,7 +303,7 @@ def write_uvdata(
uv_obj.antenna_diameters, dtype=np.float64
)
try:
uv_obj.write_ms(outfile_name, clobber=not noclobber)
uv_obj.write_ms(outfile_name, clobber=not noclobber, **write_kwargs)
except AttributeError as err: # pragma: no cover
raise AttributeError(
"Writing measurement sets requires pyuvdata version >= 2.2.2"
Expand Down

0 comments on commit 1e17641

Please sign in to comment.