Example #1
0
def plot_healpix_file(data_filename, save_filename):

    #nside = 256
    print 'Loading data...'
    data, nside_old, nest = healpix_utils.load_map(data_filename)
    #print 'Downsampling...'
    #data = healpix_utils.healpix_downsample(data, nside_old, nside, nest)
    nside = nside_old
    print 'Plotting...'
    plot_filled_pixels(data, nside, nest, save_filename)
Example #2
0
def plot_nearest_data():

    obs_list_file = '/Users/ruby/EoR/obs_lists/diffuse_survey_good_pointings_successful.txt'
    obs_info_file = '/Users/ruby/EoR/sidelobe_survey_obsinfo.txt'
    data_loc = '/Users/ruby/EoR/Healpix_fits'
    nside = 256

    obs_list = open(obs_list_file, 'r').readlines()
    obs_list = [obs.strip() for obs in obs_list]  # strip newline characters
    obs_list = list(set(obs_list))  # remove duplicates
    #obs_list = obs_list[:10]
    observations = surveyview.load_survey(obs_info_file)  # get obs metadata
    observations = [obs for obs in observations if obs.obsid in obs_list]

    indices = []
    signal_vals = []
    distances = []
    for i, obs in enumerate(observations):
        print 'Gathering data from obsid {} of {}'.format(i + 1, len(obs_list))
        print 'Loading data'
        data, nside_old, nest = healpix_utils.load_map(
            '{}/{}_uniform_Residual_I_HEALPix.fits'.format(
                data_loc, obs.obsid))
        data = healpix_utils.healpix_downsample(data, nside_old, nside, nest)

        for data_point in data:
            data_point.get_ra_dec(nside, nest)
            data_dist = hp.rotator.angdist([obs.ra, obs.dec],
                                           [data_point.ra, data_point.dec],
                                           lonlat=True)[0] / np.pi * 180.
            if data_point.pixelnum in indices:
                list_pos = indices.index(data_point.pixelnum)
                if data_dist < distances[list_pos]:
                    signal_vals[list_pos] = data_point.signal
                    distances[list_pos] = data_dist
            else:
                indices.append(data_point.pixelnum)
                signal_vals.append(data_point.signal)
                distances.append(data_dist)

    use_data = [
        healpix_utils.HealpixPixel(indices[i], signal_vals[i])
        for i in range(len(indices))
    ]

    #healpix_utils.write_data_to_fits(data, nside, nest, '/Users/ruby/Desktop/nearest_data.fits')

    print 'Plotting'
    plot_filled_pixels(use_data, nside, nest,
                       '/Users/ruby/Desktop/nearest_data_test.png')
Example #3
0
def calculate_mean_and_var():

    obs_list_file = '/Users/ruby/EoR/obs_lists/diffuse_survey_good_pointings_successful.txt'
    data_loc = '/Users/ruby/EoR/Healpix_fits'
    nside = 256

    obs_list = open(obs_list_file, 'r').readlines()
    obs_list = [obs.strip() for obs in obs_list]  # strip newline characters
    obs_list = list(set(obs_list))  # remove duplicates

    all_data = []
    for i, obs in enumerate(obs_list):
        print 'Gathering data from obsid {} of {}'.format(i + 1, len(obs_list))
        print 'Loading data'
        data, nside_old, nest = healpix_utils.load_map(
            '{}/{}_uniform_Residual_I_HEALPix.fits'.format(data_loc, obs))
        data = healpix_utils.healpix_downsample(data, nside_old, nside, nest)

        print 'Assembling data'
        # Convert to implicit indexing
        signal_vals_implicit = [hp.pixelfunc.UNSEEN] * 12 * nside**2
        for j in range(len(data)):
            signal_vals_implicit[data[j].pixelnum] = data[j].signal

        all_data.append(signal_vals_implicit)

    print 'Calculating data statistics'
    data_ave = []
    data_var = []
    data_npoints = []
    for i in range(12 * nside**2):
        use_data = [
            data_set[i] for data_set in all_data
            if data_set[i] != hp.pixelfunc.UNSEEN
        ]
        n_points = len(use_data)
        if n_points > 0:
            data_npoints.append(healpix_utils.HealpixPixel(i, n_points))
            data_ave.append(healpix_utils.HealpixPixel(i, np.mean(use_data)))
            data_var.append(healpix_utils.HealpixPixel(i, np.var(use_data)))

    print 'Saving data statistics'
    healpix_utils.write_data_to_fits(data_ave, nside, nest,
                                     '/Users/ruby/Desktop/data_ave.fits')
    healpix_utils.write_data_to_fits(data_var, nside, nest,
                                     '/Users/ruby/Desktop/data_var.fits')
    healpix_utils.write_data_to_fits(data_npoints, nside, nest,
                                     '/Users/ruby/Desktop/data_npoints.fits')
Example #4
0
def average_stokes_v(fhd_run_path, qa_params_filepath):

    data_files = os.listdir('{}/output_data/'.format(fhd_run_path))
    data_files = [
        file for file in data_files
        if '_uniform_Residual_V_HEALPix.fits' in file
    ]
    obs_list = [file[0:10] for file in data_files]

    ave_power = []
    for obsid in obs_list:
        map = healpix_utils.load_map(
            '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_survey_decon_4pol_Feb2019/output_data/{}_uniform_Residual_V_HEALPix.fits'.format(
                obsid
            )
        )
        map.implicit_to_explicit_ordering()
        ave_power.append(np.mean(map.signal_arr**2.)/len(map.signal_arr))

    qa_utils.write_params_to_csv(
        qa_params_filepath, 'average Stokes V power', obs_list, ave_power,
        overwrite=True
    )
def get_radial_variance(
    fhd_run_path, average_maps_paths, weights_map_path, savepath,
    cube_names=['Residual_I']
):

    if fhd_run_path[-1] == '/':
        fhd_run_path = fhd_run_path[:-1]

    average_maps = []
    for map_ind in range(len(cube_names)):
        map = healpix_utils.load_map(average_maps_paths[map_ind])
        map.explicit_to_implicit_ordering()
        average_maps.append(map)
    weights = healpix_utils.load_map(weights_map_path)
    if weights.nside != average_maps[0].nside:
        weights.resample(average_maps[0].nside)
    weights.explicit_to_implicit_ordering()

    data_files = os.listdir('{}/output_data/'.format(fhd_run_path))
    data_files = [
        file for file in data_files
        if '_uniform_Residual_I_HEALPix.fits' in file
    ]
    obs_list = [file[0:10] for file in data_files]

    bin_edges = np.arange(0., 15., .05)
    count = np.zeros(len(bin_edges)-1, dtype=long)
    dev_from_mean = np.zeros((len(bin_edges)-1, len(cube_names)), dtype=float)

    for obs_ind, obsid in enumerate(obs_list):
        print 'Loading observation {} of {}'.format(obs_ind, len(obs_list))
        obs_struct = scipy.io.readsav(
            '{}/metadata/{}_obs.sav'.format(fhd_run_path, obsid)
        )['obs']
        obs_vec = hp.pixelfunc.ang2vec(
            float(obs_struct['obsra']), float(obs_struct['obsdec']),
            lonlat=True
        )
        maps = []
        for cube_ind, cube in enumerate(cube_names):
            map = healpix_utils.load_map('{}/output_data/{}_uniform_{}_HEALPix.fits'.format(
                fhd_run_path, obsid, cube
            ))
            map.resample(average_maps[cube_ind].nside)
            if map.nest != average_maps[cube_ind].nest:
                print 'Map nesting conventions do not match. Converting.'
                if average_maps[cube_ind].nside:
                    map.reorder_ring_to_nest()
                else:
                    map.reorder_nest_to_ring()
            if map.coords != average_maps[cube_ind].coords:
                print 'ERROR: Map coordinates do not match. Exiting.'
                sys.exit(1)
            maps.append(map)

        for pix in maps[0].pix_arr:  # Use the first cube for the pixel list
            pix_vec = hp.pix2vec(maps[0].nside, pix, nest=maps[0].nest)
            rad_dist = hp.rotator.angdist(pix_vec, obs_vec)*180./np.pi
            bin_index = 0
            while rad_dist > bin_edges[bin_index+1] and bin_index < len(bin_edges)-1:
                bin_index += 1
            if bin_index < len(bin_edges)-1 and weights.signal_arr[pix] > 1:
                count[bin_index] += 1
                for cube_ind in range(len(cube_names)):
                    dev_from_mean[bin_index, cube_ind] += (
                        maps[cube_ind].signal_arr[
                            np.where(maps[cube_ind].pix_arr == pix)
                        ][0] - average_maps[cube_ind].signal_arr[pix]
                    )**2
    average_dev = dev_from_mean/count[:, None]
    average_dev[np.where(count == 0), :] = 0.
    xvals = np.array([
        (bin_edges[ind+1]+bin_edges[ind])/2. for ind in range(len(bin_edges)-1)
    ])

    plt.figure()
    colors = ['black', 'red', 'blue']
    for cube_ind, cube in enumerate(cube_names):
        plt.scatter(
            xvals, average_dev[:, cube_ind], marker='o', s=1.,
            color=colors[cube_ind], label=cube
        )
    plt.xlabel('Distance from Observation Center')
    plt.ylabel('Average of the Deviation from the Mean Squared')
    plt.ylim(0,.0001)
    plt.legend(loc='upper left')
    print 'Saving plot to {}'.format(savepath)
    plt.savefig(savepath, dpi=300, bbox_inches='tight')
    plt.close()
Example #6
0
def plot_individual_images():

    field_center = hp.pixelfunc.ang2vec(0., -27., lonlat=True)
    field_radius = 13.5

    map = healpix_utils.load_map(
        '/Users/rubybyrne/diffuse_survey_plotting_May2020/nsamples_map_more_obs.fits'
    )

    pix_use = []
    signal_use = []
    for ind, pix in enumerate(map.pix_arr):
        pix_vec = hp.pix2vec(map.nside, pix, nest=map.nest)
        dist_deg = hp.rotator.angdist(pix_vec, field_center) * 180. / np.pi
        if dist_deg <= field_radius:
            pix_use.append(pix)
            signal_use.append(map.signal_arr[ind])

    map.signal_arr = np.array(signal_use)
    map.pix_arr = np.array(pix_use)

    obs_groups = [['1131454296'], ['1131455736'], ['1131710392'],
                  ['1131708952'], ['1131710032', '1131713632'],
                  ['1131457176', '1131715432'], ['1131453936', '1131716512'],
                  ['1131537104', '1131709912', '1131456096']]

    for group_ind, group in enumerate(obs_groups):

        if len(group) > 1:
            for obs_ind, obs in enumerate(group):
                pols = ['I', 'Q', 'U', 'V']
                if obs in obs_list_1:
                    path = '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Feb2020',
                else:
                    path = '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Mar2020'

                combined_maps, weight_maps = healpix_utils.average_healpix_maps(
                    path,
                    obs_lists=[obs],
                    nside=128,
                    cube_names=[
                        'Residual_I', 'Residual_Q', 'Residual_U', 'Residual_V'
                    ],
                    weighting='weighted',
                    apply_radial_weighting=False,
                    apply_rm_correction=True)
                pols = ['I', 'Q', 'U', 'V']
                for map_ind in range(len(combined_maps)):
                    pol = pols[map_ind]
                    add_pixels = np.setdiff1d(map.pix_arr,
                                              combined_maps[map_ind].pix_arr)
                    combined_maps[map_ind].pix_arr = np.concatenate(
                        (np.array(combined_maps[map_ind].pix_arr), add_pixels))
                    combined_maps[map_ind].signal_arr = np.concatenate(
                        (np.array(combined_maps[map_ind].signal_arr),
                         np.zeros(np.shape(add_pixels)[0])))
                    combined_maps[map_ind].signal_arr = np.array([
                        combined_maps[map_ind].signal_arr[np.where(
                            combined_maps[map_ind].pix_arr == pix)[0]][0]
                        for pix in map.pix_arr
                    ])
                    combined_maps[map_ind].pix_arr = map.pix_arr

                    if pol == 'I':
                        colorbar_range = [-1e4, 1e4]
                    else:
                        colorbar_range = [-5e3, 5e3]
                    plot_healpix_map.plot_filled_pixels(
                        combined_maps[map_ind],
                        '/Users/rubybyrne/diffuse_survey_plotting_May2020/eor0_plots/Stokes{}_eor0_map{}_obs{}.png'
                        .format(pol, group_ind + 1, obs_ind + 1),
                        colorbar_range=colorbar_range)
Example #7
0
def make_combined_images():

    field_center = hp.pixelfunc.ang2vec(0., -27., lonlat=True)
    field_radius = 13.5

    map = healpix_utils.load_map(
        '/Users/rubybyrne/diffuse_survey_plotting_May2020/nsamples_map_more_obs.fits'
    )

    pix_use = []
    signal_use = []
    for ind, pix in enumerate(map.pix_arr):
        pix_vec = hp.pix2vec(map.nside, pix, nest=map.nest)
        dist_deg = hp.rotator.angdist(pix_vec, field_center) * 180. / np.pi
        if dist_deg <= field_radius:
            pix_use.append(pix)
            signal_use.append(map.signal_arr[ind])

    map.signal_arr = np.array(signal_use)
    map.pix_arr = np.array(pix_use)

    obs_groups = [['1131454296'], ['1131455736'], ['1131710392'],
                  ['1131708952'], ['1131710032', '1131713632'],
                  ['1131457176', '1131715432'], ['1131453936', '1131716512'],
                  ['1131537104', '1131709912', '1131456096']]

    if False:
        for pol in ['I', 'Q', 'U', 'V']:
            full_map = healpix_utils.load_map(
                '/Users/rubybyrne/diffuse_survey_plotting_May2020/Stokes{}_average_map_more_obs.fits'
                .format(pol))
            full_map.signal_arr = np.array([
                full_map.signal_arr[np.where(full_map.pix_arr == pix)[0]][0]
                for pix in map.pix_arr
            ])
            full_map.pix_arr = map.pix_arr
            full_map.write_data_to_fits(
                '/Users/rubybyrne/diffuse_survey_plotting_May2020/eor0_plots/Stokes{}_eor0_full_map.fits'
                .format(pol))
            if pol == 'I':
                colorbar_range = [-1e4, 1e4]
            else:
                colorbar_range = [-2e3, 2e3]
            plot_healpix_map.plot_filled_pixels(
                full_map,
                '/Users/rubybyrne/diffuse_survey_plotting_May2020/eor0_plots/Stokes{}_eor0_full_map.png'
                .format(pol),
                colorbar_range=colorbar_range)

    for group_ind, group in enumerate(obs_groups):

        list_1 = [obs for obs in group if obs in obs_list_1]
        list_2 = [obs for obs in group if obs in obs_list_2]
        obs_lists_together = [list_1, list_2]
        paths_list = [
            '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Feb2020',
            '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Mar2020'
        ]
        if len(list_1) == 0:
            paths_list = paths_list[1]
            obs_lists_together = obs_lists_together[1]
        if len(list_2) == 0:
            paths_list = paths_list[0]
            obs_lists_together = obs_lists_together[0]
        combined_maps, weight_maps = healpix_utils.average_healpix_maps(
            paths_list,
            obs_lists=obs_lists_together,
            nside=128,
            cube_names=[
                'Residual_I', 'Residual_Q', 'Residual_U', 'Residual_V'
            ],
            weighting='weighted',
            apply_radial_weighting=True,
            apply_rm_correction=True,
            rm_file='/Users/rubybyrne/diffuse_survey_rm_empirical.csv')
        pols = ['I', 'Q', 'U', 'V']
        for map_ind in range(len(combined_maps)):
            pol = pols[map_ind]
            combined_maps[map_ind].signal_arr = np.array([
                combined_maps[map_ind].signal_arr[np.where(
                    combined_maps[map_ind].pix_arr == pix)[0]][0]
                for pix in map.pix_arr
            ])
            combined_maps[map_ind].pix_arr = map.pix_arr

            combined_maps[map_ind].write_data_to_fits(
                '/Users/rubybyrne/diffuse_survey_plotting_May2020/eor0_plots/Stokes{}_eor0_map{}_tapered_empirical_rm.fits'
                .format(pol, group_ind + 1))
            if pol == 'I':
                colorbar_range = [-1e4, 1e4]
            else:
                colorbar_range = [-5e3, 5e3]
            plot_healpix_map.plot_filled_pixels(
                combined_maps[map_ind],
                '/Users/rubybyrne/diffuse_survey_plotting_May2020/eor0_plots/Stokes{}_eor0_map{}_tapered_empirical_rm.png'
                .format(pol, group_ind + 1),
                colorbar_range=colorbar_range)
Example #8
0
def find_obs_groups():

    field_center = hp.pixelfunc.ang2vec(0., -27., lonlat=True)
    field_radius = 13.5

    map = healpix_utils.load_map(
        '/Users/rubybyrne/diffuse_survey_plotting_May2020/nsamples_map_more_obs.fits'
    )

    pix_use = []
    signal_use = []
    for ind, pix in enumerate(map.pix_arr):
        pix_vec = hp.pix2vec(map.nside, pix, nest=map.nest)
        dist_deg = hp.rotator.angdist(pix_vec, field_center) * 180. / np.pi
        if dist_deg <= field_radius:
            pix_use.append(pix)
            signal_use.append(map.signal_arr[ind])

    map.signal_arr = np.array(signal_use)
    map.pix_arr = np.array(pix_use)

    map.write_data_to_fits(
        '/Users/rubybyrne/diffuse_survey_plotting_May2020/eor0_plots/nsamples_eor0.fits'
    )
    plot_healpix_map.plot_filled_pixels(
        map,
        '/Users/rubybyrne/diffuse_survey_plotting_May2020/eor0_plots/nsamples_eor0.png'
    )

    obs_use_list = []
    pix_use_list = []
    for obsid in obs_list_1 + obs_list_2:
        if obsid in obs_list_1:
            path = '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Feb2020'
        elif obsid in obs_list_2:
            path = '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Mar2020'

        obs_struct = scipy.io.readsav('{}/metadata/{}_obs.sav'.format(
            path, obsid))['obs']
        obs_vec = hp.pixelfunc.ang2vec(float(obs_struct['obsra']),
                                       float(obs_struct['obsdec']),
                                       lonlat=True)
        dist_deg = hp.rotator.angdist(obs_vec, field_center) * 180. / np.pi
        if dist_deg <= 30.:
            obs_reference_map = healpix_utils.load_map(
                '{}/output_data/{}_weighted_Residual_I_HEALPix.fits'.format(
                    path, obsid))
            if np.shape(np.intersect1d(obs_reference_map.pix_arr,
                                       map.pix_arr))[0] > 0:
                obs_use_list.append(obsid)
                pix_use_list.append(obs_reference_map.pix_arr)

    least_sampled_pixels = map.pix_arr[np.where(
        map.signal_arr == np.min(map.signal_arr))[0]]
    obs_use_least_sampled = []
    obs_to_match = [obsid for obsid in obs_use_list]
    for obsind, obsid in enumerate(obs_use_list):
        if np.shape(np.intersect1d(least_sampled_pixels,
                                   pix_use_list[obsind]))[0] > 0:
            obs_use_least_sampled.append(obsid)
            obs_to_match.remove(obsid)

    print np.shape(obs_use_least_sampled)

    obs_groups = []

    # Check for one-obs groups
    obs_to_remove = []
    for obsid in obs_use_least_sampled:
        if np.shape(
                np.intersect1d(pix_use_list[obs_use_list.index(obsid)],
                               map.pix_arr))[0] == np.shape(map.pix_arr)[0]:
            print 'Obsid {} is an obs group'.format(obsid)
            obs_groups.append([obsid])
            obs_to_remove.append(obsid)
    for obsid in obs_to_remove:
        obs_use_least_sampled.remove(obsid)

    # Check for two-obs groups
    if len(obs_use_least_sampled) > 0:
        obs_to_remove = []
        for obsid in obs_use_least_sampled:
            #see if two obs cover the whole field
            pix_list_1 = pix_use_list[obs_use_list.index(obsid)]
            no_match = True
            obsind = 0
            while no_match and obsind < len(obs_to_match):
                obsid_2 = obs_to_match[obsind]
                print obsid_2
                print obs_use_list.index(obsid_2)
                pix_list_2 = pix_use_list[obs_use_list.index(obsid_2)]
                if np.shape(
                        np.intersect1d(
                            np.concatenate((pix_list_1, pix_list_2)),
                            map.pix_arr))[0] == np.shape(map.pix_arr)[0]:
                    print 'Obsids {} and {} are an obs group'.format(
                        obsid, obsid_2)
                    obs_groups.append([obsid, obsid_2])
                    obs_to_match.remove(obsid_2)
                    obs_to_remove.append(obsid)
                    no_match = False
                obsind += 1
        for obsid in obs_to_remove:
            obs_use_least_sampled.remove(obsid)

    # Check for three-obs groups
    if len(obs_use_least_sampled) > 0:
        obs_to_remove = []
        for obsid in obs_use_least_sampled:
            #see if two obs cover the whole field
            pix_list_1 = pix_use_list[obs_use_list.index(obsid)]
            no_match = True
            obsind_2 = 0
            while no_match and obsind_2 < len(obs_to_match):
                obsid_2 = obs_to_match[obsind_2]
                pix_list_2 = pix_use_list[obs_use_list.index(obsid_2)]
                obsind_3 = 0
                while no_match and obsind_3 < len(obs_to_match):
                    if obsind_2 != obsind_3:
                        obsid_3 = obs_to_match[obsind_3]
                        pix_list_3 = pix_use_list[obs_use_list.index(obsid_3)]
                        if np.shape(
                                np.intersect1d(
                                    np.concatenate(
                                        (pix_list_1, pix_list_2, pix_list_3)),
                                    map.pix_arr))[0] == np.shape(
                                        map.pix_arr)[0]:
                            print 'Obsids {}, {}, and {} are an obs group'.format(
                                obsid, obsid_2, obsid_3)
                            obs_groups.append([obsid, obsid_2, obsid_3])
                            obs_to_match.remove(obsid_2)
                            obs_to_match.remove(obsid_3)
                            obs_to_remove.append(obsid)
                            no_match = False
                    obsind_3 += 1
                obsind_2 += 1
        for obsid in obs_to_remove:
            obs_use_least_sampled.remove(obsid)

    print obs_groups
Example #9
0
def hist2d_datastat_plot():

    obs_list_file = '/Users/ruby/EoR/obs_lists/diffuse_survey_good_pointings_successful.txt'
    data_loc = '/Users/ruby/EoR/Healpix_fits'

    obs_list = open(obs_list_file, 'r').readlines()
    obs_list = [obs.strip() for obs in obs_list]  # strip newline characters
    obs_list = list(set(obs_list))  # remove duplicates
    #obs_list = obs_list[:4]
    observations = surveyview.load_survey(
        '/Users/ruby/EoR/sidelobe_survey_obsinfo.txt')
    observations = [obs for obs in observations if obs.obsid in obs_list]

    var_data, nside, nest = healpix_utils.load_map(
        '/Users/ruby/Desktop/data_var_nside256.fits')
    var_pixelvals = [data_point.pixelnum for data_point in var_data]

    data_diff = []
    data_dist = []
    for i, obs in enumerate(observations):
        print 'Gathering data from obsid {} of {}'.format(i + 1, len(obs_list))
        print 'Loading data'
        data, nside_old, nest = healpix_utils.load_map(
            '{}/{}_uniform_Residual_I_HEALPix.fits'.format(
                data_loc, obs.obsid))
        data = healpix_utils.healpix_downsample(data, nside_old, nside, nest)

        print 'Calculating data statistics'
        for data_point in data:
            data_diff.append(
                data_point.signal -
                var_data[var_pixelvals.index(data_point.pixelnum)].signal)
            data_point.get_ra_dec(nside, nest)
            data_dist.append(
                hp.rotator.angdist([obs.ra, obs.dec],
                                   [data_point.ra, data_point.dec],
                                   lonlat=True)[0] / np.pi * 180.)

    hist, dist_edges, diff_edges = np.histogram2d(data_dist,
                                                  data_diff,
                                                  bins=100)
    hist = hist.T

    for j in range(len(hist[0])):
        total = sum([hist[i][j] for i in range(len(hist))])
        if total != 0:
            for i in range(len(hist)):
                hist[i][j] = hist[i][j] / total

    fig, ax = plt.subplots()
    plt.imshow(
        hist,
        origin='lower',
        interpolation='none',
        extent=[dist_edges[0], dist_edges[-1], diff_edges[0], diff_edges[-1]],
        vmin=0,
        vmax=0.1)
    ax.set_aspect(
        (dist_edges[-1] - dist_edges[0]) / (diff_edges[-1] - diff_edges[0]))
    plt.xlabel('Distance from Pointing Center (deg)')
    plt.ylabel('Difference from Mean (Jy/sr)')
    cbar = plt.colorbar()
    cbar.ax.set_ylabel('Counts, Normalized', rotation=270)  # label colorbar
    plt.savefig('/Users/ruby/Desktop/data_var_2dhist_testing.png',
                format='png',
                dpi=500)
Example #10
0
def plot_healpix_tiling():

    #data_type = 'Residual_I'
    data_type = 'Dirty_I'
    normalization = 'uniform'
    #data_dir = '/Users/ruby/EoR/Healpix_fits'
    data_dir = '/Users/ruby/EoR/Healpix_fits/haslam_sim'

    tile_center_ras = [
        100, 100, 100, 100, 100, 90, 90, 90, 90, 90, 80, 80, 80, 80, 80, 70,
        70, 70, 70, 70, 60, 60, 60, 60, 60, 50, 50, 50, 50, 50, 40, 40, 40, 40,
        40, 30, 30, 30, 30, 30, 20, 20, 20, 20, 20, 10, 10, 10, 10, 10, 0, 0,
        0, 0, 0, -10, -10, -10, -10, -10, -20, -20, -20, -20, -20
    ]

    tile_center_decs = [
        -5, -15, -25, -35, -45, -5, -15, -25, -35, -45, -5, -15, -25, -35, -45,
        -5, -15, -25, -35, -45, -5, -15, -25, -35, -45, -5, -15, -25, -35, -45,
        -5, -15, -25, -35, -45, -5, -15, -25, -35, -45, -5, -15, -25, -35, -45,
        -5, -15, -25, -35, -45, -5, -15, -25, -35, -45, -5, -15, -25, -35, -45,
        -5, -15, -25, -35, -45
    ]

    obsids = [
        1131478056, 1131564464, 1131477936, 1130787784, 1131477816, 1131733672,
        1131562544, 1131733552, 1130785864, 1131475896, 1131559064, 1131558944,
        1131731752, 1130782264, 1130783824, 1131470736, 1131557144, 1131470616,
        1130780464, 1131470496, 1131468936, 1131553544, 1131726352, 1130778664,
        1131468696, 1131724672, 1131551744, 1131465216, 1130776864, 1130776624,
        1131463536, 1131549944, 1131463416, 1130773264, 1131463296, 1131461616,
        1131548024, 1131461496, 1130773144, 1131461376, 1131717352, 1131544424,
        1131717232, 1131459696, 1131459576, 1131456216, 1131542624, 1131456096,
        1131715432, 1131715312, 1131711952, 1131540824, 1131454296, 1131713632,
        1131454176, 1131710152, 1131537224, 1131710032, 1131710032, 1131709912,
        1131535544, 1131535424, 1131535304, 1131710032, 1131709912
    ]

    data = []
    #nside = 512
    nside = 256
    for i, obs in enumerate(obsids):
        print 'Gathering pixels from obsid {} of {}.'.format(
            i + 1, len(obsids))
        obs_data, nside_old, nest = healpix_utils.load_map(
            '{}/{}_{}_{}_HEALPix.fits'.format(data_dir, obs, normalization,
                                              data_type))
        obs_data = healpix_utils.healpix_downsample(obs_data, nside_old, nside,
                                                    nest)
        tile_bounds_radec = [[tile_center_ras[i] - 5, tile_center_decs[i] - 5],
                             [tile_center_ras[i] - 5, tile_center_decs[i] + 5],
                             [tile_center_ras[i] + 5, tile_center_decs[i] + 5],
                             [tile_center_ras[i] + 5, tile_center_decs[i] - 5]]
        tile_bounds_vec = np.array([
            hp.pixelfunc.ang2vec(corner[0], corner[1], lonlat=True)
            for corner in tile_bounds_radec
        ])
        use_pixels = hp.query_polygon(nside, tile_bounds_vec, nest=nest)
        data.extend([
            data_point for data_point in obs_data
            if data_point.pixelnum in use_pixels
        ])

    #healpix_utils.write_data_to_fits(data, nside, nest, '/Users/ruby/EoR/mosaic_data.fits')

    # Define Healpix pixels to plot
    patches = []
    colors = []
    for point in data:
        point.get_pixel_corners(nside, nest)
        polygon = Polygon(zip(point.pix_corner_ras, point.pix_corner_decs))
        patches.append(polygon)
        colors.append(point.signal)

    collection = PatchCollection(patches, cmap='Greys_r', lw=0.04)
    collection.set_array(np.array(colors))  # set the data colors
    collection.set_edgecolor('face')  # make the face and edge colors match

    fig, ax = plt.subplots(figsize=(24, 8), dpi=1000)
    ax.add_collection(collection)  # plot data

    # plot lines between tiles
    line_width = 2.0
    color = 'gray'
    order = 8
    plt.plot([130, -45], [0, 0], lw=line_width, c=color, zorder=order)
    plt.plot([130, -45], [-10, -10], lw=line_width, c=color, zorder=order)
    plt.plot([130, -45], [-20, -20], lw=line_width, c=color, zorder=order)
    plt.plot([130, -45], [-30, -30], lw=line_width, c=color, zorder=order)
    plt.plot([130, -45], [-40, -40], lw=line_width, c=color, zorder=order)
    plt.plot([130, -45], [-50, -50], lw=line_width, c=color, zorder=order)
    plt.plot([105, 105], [-75, 20], lw=line_width, c=color, zorder=order)
    plt.plot([95, 95], [-75, 20], lw=line_width, c=color, zorder=order)
    plt.plot([85, 85], [-75, 20], lw=line_width, c=color, zorder=order)
    plt.plot([75, 75], [-75, 20], lw=line_width, c=color, zorder=order)
    plt.plot([65, 65], [-75, 20], lw=line_width, c=color, zorder=order)
    plt.plot([55, 55], [-75, 20], lw=line_width, c=color, zorder=order)
    plt.plot([45, 45], [-75, 20], lw=line_width, c=color, zorder=order)
    plt.plot([35, 35], [-75, 20], lw=line_width, c=color, zorder=order)
    plt.plot([25, 25], [-75, 20], lw=line_width, c=color, zorder=order)
    plt.plot([15, 15], [-75, 20], lw=line_width, c=color, zorder=order)
    plt.plot([5, 5], [-75, 20], lw=line_width, c=color, zorder=order)
    plt.plot([-5, -5], [-75, 20], lw=line_width, c=color, zorder=order)
    plt.plot([-15, -15], [-75, 20], lw=line_width, c=color, zorder=order)
    plt.plot([-25, -25], [-75, 20], lw=line_width, c=color, zorder=order)

    plt.xlabel('RA (deg)')
    plt.ylabel('Dec (deg)')
    plt.axis('equal')
    ax.set_facecolor('gray')  # make plot background gray
    plt.axis([110, -30, -50, 0])
    cbar = fig.colorbar(collection, ax=ax, extend='both')  # add colorbar
    cbar.ax.set_ylabel('Flux Density (Jy/sr)', rotation=270)  # label colorbar

    plt.savefig(
        '/Users/ruby/Desktop/haslam_mosaicplot_nside{}.png'.format(nside),
        format='png',
        dpi=1000)
Example #11
0
def overplot_haslam_contour():

    data, nside, nest = healpix_utils.load_map(
        '/Users/ruby/EoR/mosaic_data.fits')

    fig, ax = plt.subplots(figsize=(24, 8), dpi=1000)

    # Define Healpix pixels to plot
    patches = []
    colors = []
    for point in data:
        point.get_pixel_corners(nside, nest)
        polygon = Polygon(zip(point.pix_corner_ras, point.pix_corner_decs))
        patches.append(polygon)
        colors.append(point.signal)

    collection = PatchCollection(patches, cmap='Greys_r', lw=0.04)
    collection.set_array(np.array(colors))  # set the data colors
    collection.set_edgecolor('face')  # make the face and edge colors match
    ax.add_collection(collection)  # plot data

    # plot lines between tiles
    line_width = 2.0
    color = 'gray'
    order = 8
    plt.plot([130, -45], [-10, -10], lw=line_width, c=color, zorder=order)
    plt.plot([130, -45], [-20, -20], lw=line_width, c=color, zorder=order)
    plt.plot([130, -45], [-30, -30], lw=line_width, c=color, zorder=order)
    plt.plot([130, -45], [-40, -40], lw=line_width, c=color, zorder=order)
    plt.plot([130, -45], [-50, -50], lw=line_width, c=color, zorder=order)
    plt.plot([95, 95], [-75, -20], lw=line_width, c=color, zorder=order)
    plt.plot([85, 85], [-75, -20], lw=line_width, c=color, zorder=order)
    plt.plot([75, 75], [-75, -20], lw=line_width, c=color, zorder=order)
    plt.plot([65, 65], [-75, -20], lw=line_width, c=color, zorder=order)
    plt.plot([55, 55], [-75, -20], lw=line_width, c=color, zorder=order)
    plt.plot([45, 45], [-75, -20], lw=line_width, c=color, zorder=order)
    plt.plot([35, 35], [-75, -20], lw=line_width, c=color, zorder=order)
    plt.plot([25, 25], [-75, -20], lw=line_width, c=color, zorder=order)
    plt.plot([15, 15], [-75, -20], lw=line_width, c=color, zorder=order)
    plt.plot([5, 5], [-75, -20], lw=line_width, c=color, zorder=order)
    plt.plot([-5, -5], [-75, -20], lw=line_width, c=color, zorder=order)
    plt.plot([-15, -15], [-75, -20], lw=line_width, c=color, zorder=order)

    plt.xlabel('RA (deg)')
    plt.ylabel('Dec (deg)')
    plt.axis('equal')
    ax.set_facecolor('gray')  # make plot background gray
    plt.axis([110, -30, -50, 0])
    cbar = fig.colorbar(collection, ax=ax, extend='both')  # add colorbar
    cbar.ax.set_ylabel('Flux Density (Jy/sr)', rotation=270)  # label colorbar

    data, nside, nest = healpix_utils.load_global_map(
        '/Users/ruby/EoR/Healpix_fits/lambda_haslam408_dsds.fits')
    data = healpix_utils.healpix_downsample(data, nside, 512, nest)
    for point in data:
        point.get_ra_dec(nside, nest, coords='galactic')

    plt.tricontour([point.ra for point in data], [point.dec for point in data],
                   [point.signal for point in data],
                   500,
                   linewidths=0.5,
                   colors='cyan')

    plt.savefig('/Users/ruby/EoR/haslam_overplot.png', format='png', dpi=1000)
Example #12
0
def calculate_empirical_rm_Jul2021():

    eor0_obslist = [
        '1131454296', '1131713632', '1131710032', '1131456096', '1131540824',
        '1131539024', '1131538904', '1131540704', '1131455736', '1131710392',
        '1131708952', '1131457176', '1131716512', '1131458976', '1131712192',
        '1131453936', '1131457536', '1131537704', '1131543584'
    ]
    obslist_full = [
        '1131551744',
        '1130783824',
        '1131562544',
        '1131709912',
        '1130776864',
        '1131461496',
        '1130782264',
        '1131715432',
        '1131733552',
        '1131542624',
        '1130773144',
        '1131461376',
        '1131557144',
        '1131454296',
        '1131731752',
        '1130778664',
        '1131470496',
        '1131559064',
        '1131717232',
        '1131463536',
        '1130773264',
        '1131463416',
        '1131717352',
        '1131713632',
        '1131478056',
        '1131468936',
        '1131468696',
        '1131535424',
        '1131463296',
        '1131465216',
        '1131710032',
        '1130776624',
        '1131456096',
        '1131540824',
        '1131711952',
        '1131459576',
        '1131477936',
        '1131733672',
        '1131564464',
        '1130787784',
        '1131461616',
        '1131558944',
        '1131470616',
        '1131549944',
        '1131553544',
        '1131459696',
        '1130780464',
        '1131726352',
        '1131470736',
        '1131548024',
        '1131710152',
        '1130785864',
        '1131544424',
        '1131542504',
        #'1131733432',
        '1131735232',
        '1131553664',
        '1131724432',
        '1131542744',
        '1131455976',
        '1131719152',
        '1131454416',
        '1130787544',
        '1130776744',
        '1130780224',
        '1131551624',
        '1131722632',
        '1131547904',
        '1131562664',
        '1131550064',
        '1131537104',
        '1131555224',
        '1131467136',
        '1131539024',
        '1131555344',
        '1131546104',
        '1131548144',
        '1131472416',
        '1131558824',
        '1131544304',
        '1130789584',
        '1131476136',
        '1130789344',
        '1131722872',
        '1130785744',
        '1131730072',
        '1131459816',
        '1131564584',
        '1131457776',
        '1131724552',
        '1130787664',
        '1130778424',
        '1131728152',
        '1131722752',
        '1131538904',
        '1131544544',
        '1130778544',
        '1131467016',
        '1131546344',
        '1130789464',
        '1131713512',
        '1131546224',
        '1131474336',
        '1130782144',
        '1131735472',
        '1130775064',
        '1130774824',
        '1131720832',
        '1130774944',
        '1131557264',
        '1130783944',
        '1131472296',
        '1131465096',
        '1131457896',
        '1131555464',
        '1131562424',
        '1131551864',
        '1131540704',
        '1130780344',
        '1131731632',
        '1131468816',
        '1131472536',
        '1130773024',
        '1131474096',
        '1131465336',
        '1131715552',
        '1131458016',
        '1131540944',
        '1131557024',
        '1131731872',
        '1131553424',
        '1131560864',
        '1130784064',
        '1131466896',
        '1130782024',
        '1131560624',
        '1131474216',
        '1131564344',
        '1131729952',
        '1131560744',
        '1130785624',
        '1131709432',
        '1131536624',
        '1131536384',
        '1131711112',
        '1131709192',
        '1131710992',
        '1131453456',
        '1131565304',
        '1131478776',
        '1131566504',
        '1131565184',
        '1131566624',
        '1131566744',
        '1131565064',
        '1131567944',
        '1131478656',
        '1131568544',
        '1131739432',
        '1130788504',
        '1130788264',
        '1131740752',
        '1131455736',
        '1131710392',
        '1131708952',
        '1131457176',
        '1131716512',
        '1131458976',
        '1131712192',
        '1131453936',
        '1131457536',
        '1131537704',
        '1131543584'
    ]
    start_freq_mhz = 167.
    end_freq_mhz = 198.

    original_rm_path = '/Users/rubybyrne/diffuse_survey_rm_tot.csv'
    rm_outfile = '/Users/rubybyrne/diffuse_survey_rm_empirical_Jul2021.csv'
    run_path = '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey_May2021/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Jun2021'
    averaged_Q_path = '/Users/rubybyrne/diffuse_survey_plotting_Jun2021/StokesQ_average_map_first_rm_correction.fits'
    averaged_U_path = '/Users/rubybyrne/diffuse_survey_plotting_Jun2021/StokesU_average_map_first_rm_correction.fits'

    # Get RMs
    rm_data = np.genfromtxt(original_rm_path,
                            delimiter=',',
                            dtype=None,
                            names=True,
                            encoding=None)
    rms_orig = np.array([
        rm_data['RM'][np.where(rm_data['ObsID'] == int(obsid))][0]
        for obsid in eor0_obslist
    ])

    # Create lookup table:
    rot_angles_lookup, rms_lookup = create_rm_lookup_table(
        start_freq_mhz, end_freq_mhz)

    rms_use = np.copy(rms_orig)
    rot_angle_deltas_list = np.zeros(len(eor0_obslist))
    eff_rot_angle_start = get_effective_rotation_angles(
        rms_use, start_freq_mhz, end_freq_mhz)

    q_average_map = healpix_utils.load_map(averaged_Q_path)
    u_average_map = healpix_utils.load_map(averaged_U_path)
    q_average_map.explicit_to_implicit_ordering()
    u_average_map.explicit_to_implicit_ordering()

    # Calculate the empirical rotation angles
    for obsind, obsid in enumerate(eor0_obslist):

        q_map = healpix_utils.load_map(
            '{}/output_data/{}_optimal_Residual_Q_HEALPix.fits'.format(
                run_path, obsid),
            quiet=True)
        u_map = healpix_utils.load_map(
            '{}/output_data/{}_optimal_Residual_U_HEALPix.fits'.format(
                run_path, obsid),
            quiet=True)

        # Apply RM correction to maps
        maps_rot = healpix_utils.rm_correction(obsid,
                                               [None, q_map, u_map, None],
                                               rm_file=None,
                                               start_freq_mhz=start_freq_mhz,
                                               end_freq_mhz=end_freq_mhz,
                                               use_single_freq_calc=False,
                                               use_rm=rms_use[obsind])
        q_map_rot = maps_rot[1]
        u_map_rot = maps_rot[2]

        # Confirm that pixel ordering matches
        if np.sum(np.abs(q_map_rot.pix_arr - u_map_rot.pix_arr)) != 0:
            print 'ERROR: Different pixel ordering.'

        q_average_map_signal = np.array(
            [q_average_map.signal_arr[ind] for ind in q_map_rot.pix_arr])
        u_average_map_signal = np.array(
            [u_average_map.signal_arr[ind] for ind in q_map_rot.pix_arr])

        tangent_numerator = np.sum(q_average_map_signal *
                                   u_map_rot.signal_arr -
                                   u_average_map_signal * q_map_rot.signal_arr)
        tangent_denominator = np.sum(
            q_average_map_signal * q_map_rot.signal_arr +
            u_average_map_signal * u_map_rot.signal_arr)
        rot_angle_deltas_list[obsind] = np.arctan2(tangent_numerator,
                                                   tangent_denominator)

    # Ensure that the change in the rotation angles is mean-zero
    mean_angle = np.arctan2(np.sum(np.sin(rot_angle_deltas_list)),
                            np.sum(np.cos(rot_angle_deltas_list)))
    rot_angle_deltas_list -= mean_angle

    eff_rot_angle = eff_rot_angle_start + rot_angle_deltas_list
    # Ensure that the rotation angles are within +/- pi
    eff_rot_angle = np.arctan2(np.sin(eff_rot_angle), np.cos(eff_rot_angle))

    # Convert effective rotation angles to RMs
    for obsind in range(len(eor0_obslist)):
        rms_use[obsind] = interpolate_rms(rms_lookup, rot_angles_lookup,
                                          rms_orig[obsind],
                                          eff_rot_angle[obsind],
                                          start_freq_mhz, end_freq_mhz)

    # Save RMs to a CSV file
    rms_orig_all = np.array([
        rm_data['RM'][np.where(rm_data['ObsID'] == int(obsid))][0]
        for obsid in obslist_full
    ])

    csv_outfile = open(rm_outfile, 'w')
    outfile_writer = csv.writer(csv_outfile)
    outfile_writer.writerow(['ObsID', 'RM'])
    for obsind, obsid in enumerate(obslist_full):
        if obsid in eor0_obslist:
            outfile_writer.writerow(
                [obsid, rms_use[eor0_obslist.index(obsid)]])
        else:
            outfile_writer.writerow([obsid, rms_orig_all[obsind]])
    csv_outfile.close()
Example #13
0
def calculate_empirical_rm_no_iteration():

    rm_file = '/Users/rubybyrne/diffuse_survey_rm_tot.csv'
    rm_outpath = '/Users/rubybyrne/rm_empirical_calculation/Jul2020_align_with_avg'
    rm_outfile = '{}/diffuse_survey_rm_empirical_Jul2020.csv'.format(
        rm_outpath)
    start_freq_mhz = 167.
    end_freq_mhz = 198.

    # Get RMs
    rm_data = np.genfromtxt(rm_file,
                            delimiter=',',
                            dtype=None,
                            names=True,
                            encoding=None)
    rms_orig = np.array([
        rm_data['RM'][np.where(rm_data['ObsID'] == int(obsid))][0]
        for obsid in obs_list_1 + obs_list_2
    ])

    # Create lookup table:
    rot_angles_lookup, rms_lookup = create_rm_lookup_table(
        start_freq_mhz, end_freq_mhz)

    rms_use = np.copy(rms_orig)

    rot_angle_deltas_list = np.zeros(len(obs_list_1) + len(obs_list_2))
    eff_rot_angle_start = get_effective_rotation_angles(
        rms_use, start_freq_mhz, end_freq_mhz)
    # Create average maps
    combined_maps, weight_map = healpix_utils.average_healpix_maps(
        [
            '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Feb2020',
            '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Mar2020'
        ],
        obs_lists=[obs_list_1, obs_list_2],
        nside=128,
        cube_names=['Residual_I', 'Residual_Q', 'Residual_U', 'Residual_V'],
        weighting='weighted',
        apply_radial_weighting=True,
        apply_rm_correction=True,
        use_rms=rms_use,
        quiet=True)
    q_average_map = combined_maps[1]
    u_average_map = combined_maps[2]

    # Plot
    colorbar_range = [-2e3, 2e3]
    plot_healpix_map.plot_filled_pixels(
        q_average_map,
        '{}/StokesQ_averaged_initial.png'.format(rm_outpath),
        colorbar_range=colorbar_range)
    plot_healpix_map.plot_filled_pixels(
        u_average_map,
        '{}/StokesU_averaged_initial.png'.format(rm_outpath),
        colorbar_range=colorbar_range)
    # Save maps
    q_average_map.write_data_to_fits(
        '{}/StokesQ_averaged_initial.fits'.format(rm_outpath))
    u_average_map.write_data_to_fits(
        '{}/StokesU_averaged_initial.fits'.format(rm_outpath))

    q_average_map.explicit_to_implicit_ordering()
    u_average_map.explicit_to_implicit_ordering()

    # Calculate the empirical rotation angles
    for obsind, obsid in enumerate(obs_list_1 + obs_list_2):
        if obsid in obs_list_1:
            path = '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Feb2020'
        elif obsid in obs_list_2:
            path = '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Mar2020'

        q_map = healpix_utils.load_map(
            '{}/output_data/{}_weighted_Residual_Q_HEALPix.fits'.format(
                path, obsid),
            quiet=True)
        u_map = healpix_utils.load_map(
            '{}/output_data/{}_weighted_Residual_U_HEALPix.fits'.format(
                path, obsid),
            quiet=True)

        # Apply RM correction to maps
        maps_rot = healpix_utils.rm_correction(obsid,
                                               [None, q_map, u_map, None],
                                               rm_file=None,
                                               start_freq_mhz=start_freq_mhz,
                                               end_freq_mhz=end_freq_mhz,
                                               use_single_freq_calc=False,
                                               use_rm=rms_use[obsind])
        q_map_rot = maps_rot[1]
        u_map_rot = maps_rot[2]

        # Confirm that pixel ordering matches
        if np.sum(np.abs(q_map_rot.pix_arr - u_map_rot.pix_arr)) != 0:
            print 'ERROR: Different pixel ordering.'

        q_average_map_signal = np.array(
            [q_average_map.signal_arr[ind] for ind in q_map_rot.pix_arr])
        u_average_map_signal = np.array(
            [u_average_map.signal_arr[ind] for ind in q_map_rot.pix_arr])

        tangent_numerator = np.sum(q_average_map_signal *
                                   u_map_rot.signal_arr -
                                   u_average_map_signal * q_map_rot.signal_arr)
        tangent_denominator = np.sum(
            q_average_map_signal * q_map_rot.signal_arr +
            u_average_map_signal * u_map_rot.signal_arr)
        rot_angle_deltas_list[obsind] = np.arctan2(tangent_numerator,
                                                   tangent_denominator)

    # Ensure that the change in the rotation angles is mean-zero
    mean_angle = np.arctan2(np.sum(np.sin(rot_angle_deltas_list)),
                            np.sum(np.cos(rot_angle_deltas_list)))
    rot_angle_deltas_list = rot_angle_deltas_list - mean_angle
    rot_angle_list = rot_angle_deltas_list

    eff_rot_angle = eff_rot_angle_start + rot_angle_list
    # Ensure that the rotation angles are within +/- pi
    eff_rot_angle = np.arctan2(np.sin(eff_rot_angle), np.cos(eff_rot_angle))

    # Convert effective rotation angles to RMs
    for obsind in range(len(obs_list_1) + len(obs_list_2)):
        rms_use[obsind] = interpolate_rms(rms_lookup, rot_angles_lookup,
                                          rms_orig[obsind],
                                          eff_rot_angle[obsind],
                                          start_freq_mhz, end_freq_mhz)

    # Save RMs to a CSV file
    csv_outfile = open(rm_outfile, 'w')
    outfile_writer = csv.writer(csv_outfile)
    outfile_writer.writerow(['ObsID', 'RM'])
    for obsind, obsid in enumerate(obs_list_1 + obs_list_2):
        outfile_writer.writerow([obsid, rms_use[obsind]])
    csv_outfile.close()

    # Create new average maps
    combined_maps, weight_map = healpix_utils.average_healpix_maps(
        [
            '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Feb2020',
            '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Mar2020'
        ],
        obs_lists=[obs_list_1, obs_list_2],
        nside=128,
        cube_names=['Residual_I', 'Residual_Q', 'Residual_U', 'Residual_V'],
        weighting='weighted',
        apply_radial_weighting=True,
        apply_rm_correction=True,
        use_rms=rms_use,
        quiet=True)
    q_average_map = combined_maps[1]
    u_average_map = combined_maps[2]

    # Plot
    colorbar_range = [-2e3, 2e3]
    plot_healpix_map.plot_filled_pixels(
        q_average_map,
        '{}/StokesQ_averaged_final.png'.format(rm_outpath),
        colorbar_range=colorbar_range)
    plot_healpix_map.plot_filled_pixels(
        u_average_map,
        '{}/StokesU_averaged_final.png'.format(rm_outpath),
        colorbar_range=colorbar_range)
    # Save maps
    q_average_map.write_data_to_fits(
        '{}/StokesQ_averaged_final.fits'.format(rm_outpath))
    u_average_map.write_data_to_fits(
        '{}/StokesU_averaged_final.fits'.format(rm_outpath))
Example #14
0
def calculate_empirical_rm_iterative():

    n_iter = 1000
    step_size = .3

    #obs_list_1 = [obs_list_1[0]]
    #obs_list_2 = []

    rm_file = '/Users/rubybyrne/diffuse_survey_rm_tot.csv'
    rm_outpath = '/Users/rubybyrne/rm_empirical_calculation/Jul2020_vanilla'
    rm_outfile = '{}/diffuse_survey_rm_empirical_Jul2020.csv'.format(
        rm_outpath)
    start_freq_mhz = 167.
    end_freq_mhz = 198.

    # Get RMs
    rm_data = np.genfromtxt(rm_file,
                            delimiter=',',
                            dtype=None,
                            names=True,
                            encoding=None)
    rms_orig = np.array([
        rm_data['RM'][np.where(rm_data['ObsID'] == int(obsid))][0]
        for obsid in obs_list_1 + obs_list_2
    ])

    # Create lookup table:
    rot_angles_lookup, rms_lookup = create_rm_lookup_table(
        start_freq_mhz, end_freq_mhz)

    rms_use = np.copy(rms_orig)
    for iter_ind in range(n_iter):
        rot_angle_deltas_list = np.zeros(len(obs_list_1) + len(obs_list_2))
        eff_rot_angle_start = get_effective_rotation_angles(
            rms_use, start_freq_mhz, end_freq_mhz)
        # Create average maps
        combined_maps, weight_map = healpix_utils.average_healpix_maps(
            [
                '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Feb2020',
                '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Mar2020'
            ],
            obs_lists=[obs_list_1, obs_list_2],
            nside=128,
            cube_names=[
                'Residual_I', 'Residual_Q', 'Residual_U', 'Residual_V'
            ],
            weighting='weighted',
            apply_radial_weighting=True,
            apply_rm_correction=True,
            use_rms=rms_use,
            quiet=True)
        q_average_map = combined_maps[1]
        u_average_map = combined_maps[2]

        # Plot
        colorbar_range = [-2e3, 2e3]
        plot_healpix_map.plot_filled_pixels(
            q_average_map,
            '{}/StokesQ_averaged_iter{}.png'.format(rm_outpath, iter_ind),
            colorbar_range=colorbar_range)
        plot_healpix_map.plot_filled_pixels(
            u_average_map,
            '{}/StokesU_averaged_iter{}.png'.format(rm_outpath, iter_ind),
            colorbar_range=colorbar_range)

        q_average_map.explicit_to_implicit_ordering()
        u_average_map.explicit_to_implicit_ordering()
        if False:
            weight_map.explicit_to_implicit_ordering()

        # Calculate the empirical rotation angles
        for obsind, obsid in enumerate(obs_list_1 + obs_list_2):
            if obsid in obs_list_1:
                path = '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Feb2020'
            elif obsid in obs_list_2:
                path = '/Volumes/Bilbo/rlb_fhd_outputs/diffuse_survey/fhd_rlb_diffuse_baseline_cut_optimal_weighting_Mar2020'

            q_map = healpix_utils.load_map(
                '{}/output_data/{}_weighted_Residual_Q_HEALPix.fits'.format(
                    path, obsid),
                quiet=True)
            u_map = healpix_utils.load_map(
                '{}/output_data/{}_weighted_Residual_U_HEALPix.fits'.format(
                    path, obsid),
                quiet=True)

            # Apply RM correction to maps
            maps_rot = healpix_utils.rm_correction(
                obsid, [None, q_map, u_map, None],
                rm_file=None,
                start_freq_mhz=start_freq_mhz,
                end_freq_mhz=end_freq_mhz,
                use_single_freq_calc=False,
                use_rm=rms_use[obsind])
            q_map_rot = maps_rot[1]
            u_map_rot = maps_rot[2]

            # Confirm that pixel ordering matches
            if np.sum(np.abs(q_map_rot.pix_arr - u_map_rot.pix_arr)) != 0:
                print 'ERROR: Different pixel ordering.'

            q_average_map_signal = np.array(
                [q_average_map.signal_arr[ind] for ind in q_map_rot.pix_arr])
            u_average_map_signal = np.array(
                [u_average_map.signal_arr[ind] for ind in q_map_rot.pix_arr])
            if False:
                total_weights = np.array(
                    [weight_map.signal_arr[ind] for ind in q_map_rot.pix_arr])

            #Get radial weighting
            if False:
                obs_struct = scipy.io.readsav('{}/metadata/{}_obs.sav'.format(
                    path, obsid))['obs']
                obs_vec = hp.pixelfunc.ang2vec(float(obs_struct['obsra']),
                                               float(obs_struct['obsdec']),
                                               lonlat=True)
                rad_weights = np.ones(np.shape(q_map_rot.pix_arr)[0])
                for pixind, pix in enumerate(q_map_rot.pix_arr):
                    pix_vec = hp.pix2vec(q_map_rot.nside,
                                         pix,
                                         nest=q_map_rot.nest)
                    rad_weights[
                        pixind] = healpix_utils.obs_radial_weighting_function(
                            hp.rotator.angdist(pix_vec, obs_vec) * 180. /
                            np.pi)

            rot_angle_delta = calculate_rotation_angle_analytic(
                q_average_map_signal, u_average_map_signal,
                q_map_rot.signal_arr, u_map_rot.signal_arr)
            rot_angle_deltas_list[obsind] = rot_angle_delta

        # Ensure that the change in the rotation angles is mean-zero
        mean_angle = np.arctan2(np.sum(np.sin(rot_angle_deltas_list)),
                                np.sum(np.cos(rot_angle_deltas_list)))
        rot_angle_deltas_list = rot_angle_deltas_list - mean_angle
        rot_angle_list = step_size * rot_angle_deltas_list
        print np.sum(rot_angle_deltas_list**2.)

        eff_rot_angle = eff_rot_angle_start + rot_angle_list
        # Ensure that the rotation angles are within +/- pi
        eff_rot_angle = np.arctan2(np.sin(eff_rot_angle),
                                   np.cos(eff_rot_angle))

        # Convert effective rotation angles to RMs
        for obsind in range(len(obs_list_1) + len(obs_list_2)):
            rms_use[obsind] = interpolate_rms(rms_lookup, rot_angles_lookup,
                                              rms_orig[obsind],
                                              eff_rot_angle[obsind],
                                              start_freq_mhz, end_freq_mhz)

        # Save each iteration's RMs to a CSV file
        rm_outfile_iter = '{}/diffuse_survey_rm_empirical_iter{}.csv'.format(
            rm_outpath, iter_ind + 1)
        csv_outfile = open(rm_outfile_iter, 'w')
        outfile_writer = csv.writer(csv_outfile)
        outfile_writer.writerow(['ObsID', 'RM'])
        for obsind, obsid in enumerate(obs_list_1 + obs_list_2):
            outfile_writer.writerow([obsid, rms_use[obsind]])
        csv_outfile.close()

    # Save RMs to a CSV file
    csv_outfile = open(rm_outfile, 'w')
    outfile_writer = csv.writer(csv_outfile)
    outfile_writer.writerow(['ObsID', 'RM'])
    for obsind, obsid in enumerate(obs_list_1 + obs_list_2):
        outfile_writer.writerow([obsid, rms_use[obsind]])
    csv_outfile.close()
Example #15
0
def compare_observations():

    obs_list_file = '/Users/ruby/EoR/obs_lists/diffuse_survey_good_pointings_successful.txt'
    data_loc = '/Users/ruby/EoR/Healpix_fits'
    save_path = '/Users/ruby/Documents/2018 Fall Quarter/phys576b/jackknife_assignment'
    obs_list = open(obs_list_file, 'r').readlines()
    obs_list = [obs.strip() for obs in obs_list]  # strip newline characters
    obs_list = list(set(obs_list))  # remove duplicates
    observations = surveyview.load_survey(
        '/Users/ruby/EoR/sidelobe_survey_obsinfo.txt')
    center_ra = 40.
    center_dec = -35.
    use_observations = []
    for obs in observations:
        if obs.obsid in obs_list:
            if (obs.ra - center_ra)**2. + (obs.dec - center_dec)**2. < 10**2.:
                use_observations.append(obs)

    all_data = []
    for obs in use_observations:
        data, nside_old, nest = healpix_utils.load_map(
            '{}/{}_uniform_Residual_I_HEALPix.fits'.format(
                data_loc, obs.obsid))
        #plot_filled_pixels(data, nside, nest,       '{}/{}_residual_I.png'.format(save_path, obs.obsid))
        nside = 256
        data = healpix_utils.healpix_downsample(data, nside_old, nside, nest)
        all_data.append(data)

    print 'Calculating data average...'
    ave_data, var_data, nsamples_data, ston_data = healpix_utils.average_healpix_maps(
        all_data)

    max_intersect_pixels = set(
        [data_point.pixelnum for data_point in all_data[0]])
    for data_set in all_data[1:]:
        max_intersect_pixels = max_intersect_pixels.intersection(
            [data_point.pixelnum for data_point in data_set])

    ave_data_sum = np.sum([
        data_point.signal for data_point in ave_data
        if data_point.pixelnum in max_intersect_pixels
    ])
    all_data_norm = []
    for i, data_set in enumerate(all_data):
        data_sum = np.sum([
            data_point.signal for data_point in data_set
            if data_point.pixelnum in max_intersect_pixels
        ])
        norm_factor = ave_data_sum / data_sum
        for point in data_set:
            point.signal = point.signal * norm_factor

    ave_data_norm, var_data_norm, nsamples_data, ston_data_norm = healpix_utils.average_healpix_maps(
        all_data)

    print 'Plotting data average...'
    plot_filled_pixels(ave_data_norm, nside, nest,
                       '{}/ave_norm_residual_I.png'.format(save_path))

    data_diff = healpix_utils.difference_healpix_maps(ave_data_norm, ave_data,
                                                      nside, nest)

    plot_filled_pixels(
        data_diff, nside, nest,
        '{}/ave_norm_minus_orig_residual_I.png'.format(save_path))
#!/usr/bin/python

import numpy as np
import healpy as hp
import sys
import os
import healpix_utils
import pyradiosky
from astropy.units import Quantity

sourcedir = '/Users/ruby/Downloads'
pols = ['I', 'Q', 'U', 'V']
maps = []
for pol_ind, pol_name in enumerate(pols):
    new_map = healpix_utils.load_map(
        '{}/Stokes{}_average_map_empirical_rm_in_eor0.fits'.format(
            sourcedir, pol_name))
    maps.append(new_map)

# Check that the pixel arrays are identical
for pol_ind in range(len(pols) - 1):
    if np.max(np.abs(maps[pol_ind].pix_arr - maps[pol_ind + 1].pix_arr)) != 0:
        print('ERROR: Mismatched pixel arrays.')
        sys.exit(1)

Ncomponents = np.shape(maps[0].signal_arr)[0]
stokes = np.zeros((4, 1, Ncomponents))
for pol_ind in range(len(pols)):
    stokes[pol_ind, :, :] = maps[pol_ind].signal_arr
stokes = Quantity(stokes, 'Jy/sr')
freq_array = Quantity([182000000], 'Hz')