Exemplo n.º 1
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)
Exemplo n.º 2
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)
Exemplo n.º 3
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))
Exemplo n.º 4
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
Exemplo n.º 5
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()