Skip to content

Commit 2bbd073

Browse files
committed
fix handling for spws with descending freqs
1 parent 4fdc045 commit 2bbd073

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

src/pyuvdata/uvdata/uvdata.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,34 @@ def flt_ind_str_arr(*, fltarr, intarr, flt_tols, flt_first=True):
4646
prec_int = 8
4747
flt_str_list = ["{1:.{0}f}".format(prec_flt, flt) for flt in fltarr]
4848
int_str_list = [str(intv).zfill(prec_int) for intv in intarr]
49+
list_of_lists = []
4950
if flt_first:
50-
zipped = zip(flt_str_list, int_str_list, strict=True)
51+
list_of_lists = [flt_str_list, int_str_list]
5152
else:
52-
zipped = zip(int_str_list, flt_str_list, strict=True)
53+
list_of_lists = [int_str_list, flt_str_list]
54+
zipped = zip(*list_of_lists, strict=True)
5355
return np.array(["_".join(zpval) for zpval in zipped])
5456

5557

58+
def _get_freq_order(spw_id, freq_arr):
59+
spws = np.unique(spw_id)
60+
f_order = np.concatenate([np.where(spw_id == spw)[0] for spw in np.unique(spw_id)])
61+
62+
# With spectral windows sorted, check and see if channels within
63+
# windows need sorting. If they are ordered in ascending or descending
64+
# fashion, leave them be. If not, sort in ascending order
65+
for spw in spws:
66+
select_mask = spw_id[f_order] == spw
67+
check_freqs = freq_arr[f_order[select_mask]]
68+
if not np.all(np.diff(check_freqs) > 0) and not np.all(
69+
np.diff(check_freqs) < 0
70+
):
71+
subsort_order = f_order[select_mask]
72+
f_order[select_mask] = subsort_order[np.argsort(check_freqs)]
73+
74+
return f_order
75+
76+
5677
def _axis_add_helper(this, other, axis_name: str, other_inds, final_order=None):
5778
update_params = this._get_param_axis(axis_name, single_named_axis=True)
5879
other_form_dict = {axis_name: other_inds}
@@ -5699,7 +5720,6 @@ def __add__(
56995720
"Nfreqs": {"method": "reorder_freqs", "parameter": "channel_order"},
57005721
"Npols": {"method": "reorder_pols", "parameter": "order"},
57015722
}
5702-
order_dict = {"Nblts": None, "Nfreqs": None, "Npols": None}
57035723
for axis, ind_dict in axis_inds.items():
57045724
if len(ind_dict["this"]) != 0:
57055725
# there is some overlap, so sorting matters
@@ -5717,13 +5737,31 @@ def __add__(
57175737

57185738
# Pad out self to accommodate new data
57195739
new_axis_inds = {}
5740+
order_dict = {"Nblts": None, "Nfreqs": None, "Npols": None}
57205741
for axis_ind, axis in enumerate(axes):
57215742
if len(new_inds[axis]) > 0:
57225743
new_axis_inds[axis] = np.concatenate(
57235744
(axis_vals[axis]["this"], axis_vals[axis]["other"][new_inds[axis]])
57245745
)
57255746
if axis == "Npols":
57265747
order_dict[axis] = np.argsort(np.abs(new_axis_inds[axis]))
5748+
elif axis == "Nfreqs" and (
5749+
np.any(np.diff(this.freq_array) < 0)
5750+
or np.any(np.diff(other.freq_array) < 0)
5751+
):
5752+
# deal with the possibility of spws with channels in
5753+
# descending order.
5754+
order_dict[axis] = _get_freq_order(
5755+
np.concatenate(
5756+
(
5757+
this.flex_spw_id_array,
5758+
other.flex_spw_id_array[new_inds[axis]],
5759+
)
5760+
),
5761+
np.concatenate(
5762+
(this.freq_array, other.freq_array[new_inds[axis]])
5763+
),
5764+
)
57275765
else:
57285766
order_dict[axis] = np.argsort(new_axis_inds[axis])
57295767

tests/uvdata/test_mir.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,11 +592,9 @@ def test_flex_pol_add(sma_mir_filt):
592592
sma_yy_copy._make_flex_pol()
593593

594594
# Add the two back together here, and make sure we can the same value out,
595-
# modulo the history and sorting.
595+
# modulo the history.
596596
sma_check = sma_yy_copy + sma_xx_copy
597597

598-
sma_mir_filt.reorder_freqs(channel_order="freq")
599-
600598
assert sma_check.history != sma_mir_filt.history
601599
sma_check.history = sma_mir_filt.history = None
602600

0 commit comments

Comments
 (0)