|
14 | 14 | import numpy as np |
15 | 15 | from astropy.wcs.utils import proj_plane_pixel_area |
16 | 16 | from astropy.wcs import (WCSSUB_SPECTRAL, WCSSUB_LONGITUDE, WCSSUB_LATITUDE) |
| 17 | +from astropy.wcs import WCS |
17 | 18 | from . import wcs_utils |
18 | 19 | from .utils import FITSWarning, AstropyUserWarning, WCSCelestialError |
19 | 20 | from astropy import log |
@@ -750,3 +751,102 @@ def bunit_converters(obj, unit, equivalencies=(), freq=None): |
750 | 751 | else: |
751 | 752 | # Slice along first axis to return a 1D array. |
752 | 753 | return factors[0] |
| 754 | + |
| 755 | +def combine_headers(header1, header2): |
| 756 | + ''' |
| 757 | + Given two Header objects, this function returns a fits Header of the optimal wcs. |
| 758 | +
|
| 759 | + Parameters |
| 760 | + ---------- |
| 761 | + header1 : astropy.io.fits.Header |
| 762 | + A Header. |
| 763 | + header2 : astropy.io.fits.Header |
| 764 | + A Header. |
| 765 | +
|
| 766 | + Returns |
| 767 | + ------- |
| 768 | + header : astropy.io.fits.Header |
| 769 | + A header object of a field containing both initial headers. |
| 770 | +
|
| 771 | + ''' |
| 772 | + |
| 773 | + from reproject.mosaicking import find_optimal_celestial_wcs |
| 774 | + |
| 775 | + # Get wcs and shape of both headers |
| 776 | + w1 = WCS(header1).celestial |
| 777 | + s1 = w1.array_shape |
| 778 | + w2 = WCS(header2).celestial |
| 779 | + s2 = w2.array_shape |
| 780 | + |
| 781 | + # Get the optimal wcs and shape for both fields together |
| 782 | + wcs_opt, shape_opt = find_optimal_celestial_wcs([(s1, w1), (s2, w2)], auto_rotate=False) |
| 783 | + |
| 784 | + # Make a new header using the optimal wcs and information from cubes |
| 785 | + header = header1.copy() |
| 786 | + header['NAXIS'] = 3 |
| 787 | + header['NAXIS1'] = shape_opt[1] |
| 788 | + header['NAXIS2'] = shape_opt[0] |
| 789 | + header['NAXIS3'] = header1['NAXIS3'] |
| 790 | + header.update(wcs_opt.to_header()) |
| 791 | + header['WCSAXES'] = 3 |
| 792 | + return header |
| 793 | + |
| 794 | +def mosaic_cubes(cubes, spectral_block_size=100, **kwargs): |
| 795 | + ''' |
| 796 | + This function reprojects cubes onto a common grid and combines them to a single field. |
| 797 | +
|
| 798 | + Parameters |
| 799 | + ---------- |
| 800 | + cubes : iterable |
| 801 | + Iterable list of SpectralCube objects to reproject and add together. |
| 802 | + spectral_block_size : int |
| 803 | + Block size so that reproject does not run out of memory. |
| 804 | +
|
| 805 | + Outputs |
| 806 | + ------- |
| 807 | + cube : SpectralCube |
| 808 | + A spectral cube with the list of cubes mosaicked together. |
| 809 | + ''' |
| 810 | + |
| 811 | + cube1 = cubes[0] |
| 812 | + header = cube1.header |
| 813 | + |
| 814 | + # Create a header for a field containing all cubes |
| 815 | + for cu in cubes[1:]: |
| 816 | + header = combine_headers(header, cu.header) |
| 817 | + |
| 818 | + # Prepare an array and mask for the final cube |
| 819 | + shape_opt = (header['NAXIS3'], header['NAXIS2'], header['NAXIS1']) |
| 820 | + final_array = np.zeros(shape_opt) |
| 821 | + mask_opt = np.zeros(shape_opt[1:]) |
| 822 | + |
| 823 | + for cube in cubes: |
| 824 | + # Reproject cubes to the header |
| 825 | + try: |
| 826 | + if spectral_block_size is not None: |
| 827 | + cube_repr = cube.reproject(header, block_size=[spectral_block_size, cube.shape[1], cube.shape[2]], **kwargs) |
| 828 | + else: |
| 829 | + cube_repr = cube.reproject(header, **kwargs) |
| 830 | + except TypeError: |
| 831 | + warnings.warn("The block_size argument is not accepted by `reproject`. A more recent version may be needed.") |
| 832 | + cube_repr = cube.reproject(header, **kwargs) |
| 833 | + |
| 834 | + # Create weighting mask (2D) |
| 835 | + mask = (cube_repr[0:1].get_mask_array()[0]) |
| 836 | + mask_opt += mask.astype(float) |
| 837 | + |
| 838 | + # Go through each slice of the cube, add it to the final array |
| 839 | + for ii in range(final_array.shape[0]): |
| 840 | + slice1 = np.nan_to_num(cube_repr.unitless_filled_data[ii]) |
| 841 | + final_array[ii] = final_array[ii] + slice1 |
| 842 | + |
| 843 | + # Dividing by the mask throws errors where it is zero |
| 844 | + with np.errstate(divide='ignore'): |
| 845 | + |
| 846 | + # Use weighting mask to average where cubes overlap |
| 847 | + for ss in range(final_array.shape[0]): |
| 848 | + final_array[ss] /= mask_opt |
| 849 | + |
| 850 | + # Create Cube |
| 851 | + cube = cube1.__class__(data=final_array * cube1.unit, wcs=WCS(header)) |
| 852 | + return cube |
0 commit comments