Skip to content

Commit 10e3ed3

Browse files
committed
refactor neighboring channel chooser
1 parent 15ad955 commit 10e3ed3

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

spectral_cube/cube_utils.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ def mosaic_cubes(cubes, spectral_block_size=100, combine_header_kwargs={},
833833
output_file=None,
834834
method='cube',
835835
verbose=True,
836+
fail_if_cube_dropped=False,
836837
**kwargs):
837838
'''
838839
This function reprojects cubes onto a common grid and combines them to a single field.
@@ -1036,8 +1037,16 @@ def update(self, n=1):
10361037
# this is very verbose but quite simple & cheap
10371038
# going to spectral_slab(channel-dx, channel+dx) gives 3-pixel cubes most often,
10381039
# which results in a 50% overhead in smoothing, etc.
1039-
chans = [(cube.closest_spectral_channel(channel) if cube.spectral_axis[cube.closest_spectral_channel(channel)] < channel else cube.closest_spectral_channel(channel)+1,
1040-
cube.closest_spectral_channel(channel) if cube.spectral_axis[cube.closest_spectral_channel(channel)] > channel else cube.closest_spectral_channel(channel)-1)
1040+
def two_closest_channels(cube, channel):
1041+
dist = np.abs(cube.spectral_axis.to(channel.unit) - channel)
1042+
closest = np.argmin(dist)
1043+
dist[closest] = np.inf
1044+
next_closest = np.argmin(dist)
1045+
if closest < next_closest:
1046+
return (closest, next_closest)
1047+
else:
1048+
return (next_closest, closest)
1049+
chans = [two_closest_channels(cube, channel)
10411050
for cube in std_tqdm(cubes, delay=5, desc='ChanSel:')]
10421051
# reversed spectral axes still break things
10431052
# and we want two channels width, not one
@@ -1070,6 +1079,8 @@ def update(self, n=1):
10701079
keep = np.array(keep1) & np.array(keep2)
10711080
if sum(keep) < len(keep):
10721081
log.warn(f"Dropping {len(keep2)-sum(keep2)} cubes out of {len(keep)} because they're out of range")
1082+
if fail_if_cube_dropped:
1083+
raise ValueError(f"There were {len(keep)-sum(keep)} dropped cubes and fail_if_cube_dropped was set")
10731084
scubes = [cube for cube, kp in zip(scubes, keep) if kp]
10741085

10751086
if weightcubes is not None:
@@ -1121,4 +1132,4 @@ def update(self, n=1):
11211132
hdu.flush()
11221133
hdu.close()
11231134

1124-
return cube
1135+
return cube

0 commit comments

Comments
 (0)