示例#1
0
def convert(map_file, history_file=None):
    """Main function."""

    map = algebra.load(map_file)
    map = algebra.make_vect(map)

    if map.axes != ('freq', 'ra', 'dec'):
        raise NotImplementedError("Exepected input map to be organized "
                                  "('freq', 'ra', 'dec').")

    new_shape = map.shape[1:] + (map.shape[0], )

    # Make the out file name assuming the input file end in .npy.  This is a
    # hack and someone should fix it sometime.
    out_fname = map_file.split('/')[-1][:-4] + '.fits'

    Map_fits = data_map.DataMap(sp.rollaxis(map, 0, 3))
    # Set axis names
    Map_fits.set_field('CTYPE3', 'FREQ--HZ', (), '32A')
    Map_fits.set_field('CTYPE1', 'RA---DEG', (), '32A')
    Map_fits.set_field('CTYPE2', 'DEC--DEG', (), '32A')
    # Copy frequency axis (now the third axis not the first).
    Map_fits.set_field('CRVAL3', map.info['freq_centre'], (), 'D')
    Map_fits.set_field('CRPIX3', new_shape[2] // 2 + 1, (), 'D')
    Map_fits.set_field('CDELT3', map.info['freq_delta'], (), 'D')
    # Set the other two axes.
    Map_fits.set_field('CRVAL1', map.info['ra_centre'], (), 'D')
    Map_fits.set_field('CRPIX1', new_shape[0] // 2 + 1, (), 'D')
    Map_fits.set_field('CDELT1', map.info['ra_delta'], (), 'D')
    Map_fits.set_field('CRVAL2', map.info['dec_centre'], (), 'D')
    Map_fits.set_field('CRPIX2', new_shape[1] // 2 + 1, (), 'D')
    Map_fits.set_field('CDELT2', map.info['dec_delta'], (), 'D')

    # Copy the file history if provided.
    if not history_file is None:
        history = hist.read(history_file)
        history.add("Converted map to fits.", ("File name: " + out_fname, ))
        Map_fits.history = history

    # Verify contents and write out.
    Map_fits.verify()
    fits_map.write(Map_fits, out_fname)
示例#2
0
def convert(map_file, history_file=None):
    """Main function."""

    map = algebra.load(map_file)
    map = algebra.make_vect(map)

    if map.axes != ("freq", "ra", "dec"):
        raise NotImplementedError("Exepected input map to be organized " "('freq', 'ra', 'dec').")

    new_shape = map.shape[1:] + (map.shape[0],)

    # Make the out file name assuming the input file end in .npy.  This is a
    # hack and someone should fix it sometime.
    out_fname = map_file.split("/")[-1][:-4] + ".fits"

    Map_fits = data_map.DataMap(sp.rollaxis(map, 0, 3))
    # Set axis names
    Map_fits.set_field("CTYPE3", "FREQ--HZ", (), "32A")
    Map_fits.set_field("CTYPE1", "RA---DEG", (), "32A")
    Map_fits.set_field("CTYPE2", "DEC--DEG", (), "32A")
    # Copy frequency axis (now the third axis not the first).
    Map_fits.set_field("CRVAL3", map.info["freq_centre"], (), "D")
    Map_fits.set_field("CRPIX3", new_shape[2] // 2 + 1, (), "D")
    Map_fits.set_field("CDELT3", map.info["freq_delta"], (), "D")
    # Set the other two axes.
    Map_fits.set_field("CRVAL1", map.info["ra_centre"], (), "D")
    Map_fits.set_field("CRPIX1", new_shape[0] // 2 + 1, (), "D")
    Map_fits.set_field("CDELT1", map.info["ra_delta"], (), "D")
    Map_fits.set_field("CRVAL2", map.info["dec_centre"], (), "D")
    Map_fits.set_field("CRPIX2", new_shape[1] // 2 + 1, (), "D")
    Map_fits.set_field("CDELT2", map.info["dec_delta"], (), "D")

    # Copy the file history if provided.
    if not history_file is None:
        history = hist.read(history_file)
        history.add("Converted map to fits.", ("File name: " + out_fname,))
        Map_fits.history = history

    # Verify contents and write out.
    Map_fits.verify()
    fits_map.write(Map_fits, out_fname)
示例#3
0
    def execute(self, nprocesses=1) :
        """Worker funciton."""
        params = self.params
        # Make parent directory and write parameter file.
        kiyopy.utils.mkparents(params['output_root'])
        parse_ini.write_params(params, params['output_root'] + 'params.ini',
                               prefix='mm_')
        save_noise_diag = params['save_noise_diag']
        in_root = params['input_root']
        all_out_fname_list = []
        all_in_fname_list = []
        # Loop over files to process.
        for pol_str in params['polarizations']:
            dmap_fname = in_root + 'dirty_map_' + pol_str + '.npy'
            noise_fname = in_root + 'noise_inv_' + pol_str + '.npy'
            all_in_fname_list.append(
                kiyopy.utils.abbreviate_file_path(dmap_fname))
            all_in_fname_list.append(
                kiyopy.utils.abbreviate_file_path(noise_fname))
            # Load the dirty map and the noise matrix.
            dirty_map = algebra.load(dmap_fname)
            dirty_map = algebra.make_vect(dirty_map)
            if dirty_map.axes != ('freq', 'ra', 'dec') :
                raise ce.DataError("Expeced dirty map to have axes "
                                   "('freq', 'ra', 'dec'), but it has axes: "
                                   + str(dirty_map.axes))
            shape = dirty_map.shape
            noise_inv = algebra.open_memmap(noise_fname, 'r')
            noise_inv = algebra.make_mat(noise_inv)
            # Initialize the clean map.
            clean_map = algebra.info_array(sp.zeros(dirty_map.shape))
            clean_map.info = dict(dirty_map.info)
            clean_map = algebra.make_vect(clean_map)
            # If needed, initialize a map for the noise diagonal.
            if save_noise_diag :
                noise_diag = algebra.zeros_like(clean_map)
            # Two cases for the noise.  If its the same shape as the map then
            # the noise is diagonal.  Otherwise, it should be block diagonal in
            # frequency.
            if noise_inv.ndim == 3 :
                if noise_inv.axes != ('freq', 'ra', 'dec') :
                    raise ce.DataError("Expeced noise matrix to have axes "
                                       "('freq', 'ra', 'dec'), but it has: "
                                       + str(noise_inv.axes))
                # Noise inverse can fit in memory, so copy it.
                noise_inv_memory = sp.array(noise_inv, copy=True)
                # Find the non-singular (covered) pixels.
                max_information = noise_inv_memory.max()
                good_data = noise_inv_memory < 1.0e-10*max_information
                # Make the clean map.
                clean_map[good_data] = (dirty_map[good_data] 
                                        / noise_inv_memory[good_data])
                if save_noise_diag :
                    noise_diag[good_data] = 1/noise_inv_memory[good_data]
            elif noise_inv.ndim == 5 :
                if noise_inv.axes != ('freq', 'ra', 'dec', 'ra', 'dec') :
                    raise ce.DataError("Expeced noise matrix to have axes "
                                       "('freq', 'ra', 'dec', 'ra', 'dec'), "
                                       "but it has: "
                                       + str(noise_inv.axes))
                # Arrange the dirty map as a vector.
                dirty_map_vect = sp.array(dirty_map) # A view.
                dirty_map_vect.shape = (shape[0], shape[1]*shape[2])
                frequencies = dirty_map.get_axis('freq')/1.0e6
                # Allowcate memory only once.
                noise_inv_freq = sp.empty((shape[1], shape[2], shape[1],
                                           shape[2]), dtype=float)
                if self.feedback > 1 :
                    print "Inverting noise matrix."
                # Block diagonal in frequency so loop over frequencies.
                for ii in xrange(dirty_map.shape[0]) :
                    if self.feedback > 1:
                        print "Frequency: ", "%5.1f"%(frequencies[ii]),
                    if self.feedback > 2:
                        print ", start mmap read:",
                        sys.stdout.flush()
                    noise_inv_freq[...] = noise_inv[ii, ...]
                    if self.feedback > 2:
                        print "done, start eig:",
                        sys.stdout.flush()
                    noise_inv_freq.shape = (shape[1]*shape[2],
                                            shape[1]*shape[2])
                    # Solve the map making equation by diagonalization.
                    noise_inv_diag, Rot = sp.linalg.eigh(noise_inv_freq, 
                                                         overwrite_a=True)
                    if self.feedback > 2:
                        print "done",
                    map_rotated = sp.dot(Rot.T, dirty_map_vect[ii])
                    # Zero out infinite noise modes.
                    bad_modes = noise_inv_diag < 1.0e-5*noise_inv_diag.max()
                    if self.feedback > 1:
                        print ", discarded: ",
                        print "%4.1f"%(100.0*sp.sum(bad_modes)/bad_modes.size),
                        print "% of modes",
                    if self.feedback > 2:
                        print ", start rotations:",
                        sys.stdout.flush()
                    map_rotated[bad_modes] = 0.
                    noise_inv_diag[bad_modes] = 1.0
                    # Solve for the clean map and rotate back.
                    map_rotated /= noise_inv_diag
                    map = sp.dot(Rot, map_rotated)
                    if self.feedback > 2:
                        print "done",
                        sys.stdout.flush()
                    # Fill the clean array.
                    map.shape = (shape[1], shape[2])
                    clean_map[ii, ...] = map
                    if save_noise_diag :
                        # Using C = R Lambda R^T 
                        # where Lambda = diag(1/noise_inv_diag).
                        temp_noise_diag = 1/noise_inv_diag
                        temp_noise_diag[bad_modes] = 0
                        # Multiply R by the diagonal eigenvalue matrix.
                        # Broadcasting does equivalent of mult by diag matrix.
                        temp_mat = Rot*temp_noise_diag
                        # Multiply by R^T, but only calculate the diagonal
                        # elements.
                        for jj in range(shape[1]*shape[2]) :
                            temp_noise_diag[jj] = sp.dot(temp_mat[jj,:], 
                                                         Rot[jj,:])
                        temp_noise_diag.shape = (shape[1], shape[2])
                        noise_diag[ii, ...] = temp_noise_diag
                    # Return workspace memory to origional shape.
                    noise_inv_freq.shape = (shape[1], shape[2],
                                            shape[1], shape[2])
                    if self.feedback > 1:
                        print ""
                        sys.stdout.flush()
            elif noise_inv.ndim == 6 :
                raise NotImplementedError("Full noise matrix not yet "
                                          "implemented.  Best we can do is "
                                          "block diagonal in frequency.")
            else :
                raise ce.DataError("Noise matrix has bad shape.")
            # Write the clean map to file.
            out_fname = params['output_root'] + 'clean_map_' + pol_str + '.npy'
            algebra.save(out_fname, clean_map)
            all_out_fname_list.append(
                kiyopy.utils.abbreviate_file_path(out_fname))
            if save_noise_diag :
                noise_diag_fname = (params['output_root'] + 'noise_diag_'
                                    + pol_str + '.npy')
                algebra.save(noise_diag_fname, noise_diag)
                all_out_fname_list.append(
                    kiyopy.utils.abbreviate_file_path(noise_diag_fname))

        # Finally update the history object.
        history = hist.read(in_root + 'history.hist')
        history.add('Read map and noise files:', all_in_fname_list)
        history.add('Converted dirty map to clean map.', all_out_fname_list)
        h_fname = params['output_root'] + "history.hist"
        history.write(h_fname)