Skip to content

Commit e8083b7

Browse files
committed
enhance the cube header determinator by checking the spectral dimension
too
1 parent de97806 commit e8083b7

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

spectral_cube/cube_utils.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,12 +784,34 @@ def combine_headers(header1, header2, **kwargs):
784784
wcs_opt, shape_opt = find_optimal_celestial_wcs([(s1, w1), (s2, w2)], auto_rotate=False,
785785
**kwargs)
786786

787+
# find spectral coverage
788+
specw1 = WCS(header1).spectral
789+
specw2 = WCS(header2).spectral
790+
specaxis1 = [x[0] for x in WCS(header1).world_axis_object_components].index('spectral')
791+
specaxis2 = [x[0] for x in WCS(header1).world_axis_object_components].index('spectral')
792+
range1 = specw1.pixel_to_world([0,header1[f'NAXIS{specaxis1+1}']-1])
793+
range2 = specw2.pixel_to_world([0,header2[f'NAXIS{specaxis1+1}']-1])
794+
795+
# check for overlap
796+
# this will raise an exception if the headers are an different units, which we want
797+
if max(range1) < min(range2) or max(range2) < min(range1):
798+
warnings.warn(f"There is no spectral overlap between {range1} and {range2}")
799+
800+
# check cdelt
801+
dx1 = specw1.proj_plane_pixel_scales()[0]
802+
dx2 = specw2.proj_plane_pixel_scales()[0]
803+
if dx1 != dx2:
804+
raise ValueError(f"Different spectral pixel scale {dx1} vs {dx2}")
805+
806+
ranges = np.hstack([range1, range2])
807+
new_naxis = int(np.ceil((ranges.max() - ranges.min()) / np.abs(dx1)))
808+
787809
# Make a new header using the optimal wcs and information from cubes
788810
header = header1.copy()
789811
header['NAXIS'] = 3
790812
header['NAXIS1'] = shape_opt[1]
791813
header['NAXIS2'] = shape_opt[0]
792-
header['NAXIS3'] = header1['NAXIS3']
814+
header['NAXIS3'] = new_naxis
793815
header.update(wcs_opt.to_header())
794816
header['WCSAXES'] = 3
795817
return header

0 commit comments

Comments
 (0)