def main(file_name, ch_number, polarization, bins: int):
    psr = PulsarInformationUtility(file_name)  # "B0834+06_20090725_114903"
    channel_number = int(ch_number[2:4])
    root_dirname = str(Path(__file__).parent.parent.parent.absolute()) + '/'

    with open(
            utils.get_time_series_filename(channel_number, root_dirname,
                                           polarization, psr),
            'r') as time_series_file:
        time_series = np.loadtxt(time_series_file)
    total_periods = int(time_series[-1, 0] / psr.period) + 1

    interpolated_intermediate, interpolated_count = np.zeros(bins * total_periods, dtype="float"), \
                                                    np.zeros(bins * total_periods, dtype="float")
    for time_series_quanta in time_series:
        n_bin = (time_series_quanta[0] / psr.period) * bins
        j = int(n_bin)
        if j == bins - 1:
            k = 0
        else:
            k = j + 1
        delta = n_bin - j

        if 0 <= j < bins * total_periods - 1:
            if not np.isnan(time_series_quanta[1]):
                interpolated_intermediate[j] = interpolated_intermediate[
                    j] + time_series_quanta[1] * (1 - delta)
                interpolated_intermediate[k] = interpolated_intermediate[
                    k] + time_series_quanta[1] * delta
                interpolated_count[j] = interpolated_count[j] + 1 - delta
                interpolated_count[k] = interpolated_count[k] + delta
        else:
            print("else condition of 0 <= j < bins - 1: j value is ", j)

    binned_time_series = np.divide(
        interpolated_intermediate,
        interpolated_count,
        out=np.zeros_like(interpolated_intermediate),
        where=interpolated_count != 0)
    binned_time_series_filename = utils.get_binned_time_series_filename(
        channel_number, root_dirname, polarization, psr)
    pulse_stack_filename = utils.get_pulse_stack_filename(
        channel_number, root_dirname, polarization, psr)
    np.savetxt(binned_time_series_filename, binned_time_series)
    print(f"saved binned time series to: ", binned_time_series_filename)
    np.savetxt(pulse_stack_filename, binned_time_series.reshape(-1, bins))
    print(f"saved pulse stack to: ", pulse_stack_filename)
    integrated_filename = utils.get_integrated_pulse_stack_filename(
        channel_number, root_dirname, polarization, psr)
    np.savetxt(integrated_filename,
               np.sum(binned_time_series.reshape(-1, bins), axis=0))
    print(f"saved integrated pulse stack to: ", pulse_stack_filename)
def main(file_name, ch_number, polarization):
    psr = PulsarInformationUtility(file_name)
    root_dirname = str(Path(__file__).parent.parent.parent.absolute()) + '/'
    ch_number = int(ch_number[2:4])
    psr_name = psr.psr_name_date_time

    plt.figure("average pulse profile " + psr_name + " " + str(ch_number) +
               " " + polarization)
    app = np.loadtxt(
        utils.get_average_pulse_file_name(root_dirname, psr, ch_number,
                                          polarization)).T
    mean_subtracted = (
        app.T - utils.get_robust_mean_rms_2d(app.T, psr.sigma_threshold)[0]).T

    plt.figure("average pulse profile " + psr_name + " " + str(ch_number) +
               " " + polarization)
    utils.plot_DS(app.T)

    plt.figure("mean subtracted average pulse profile " + psr_name + " " +
               str(ch_number) + " " + polarization)
    utils.plot_DS(mean_subtracted.T)

    cent_freq = psr.get_central_frequency(ch_number)
    n_col = int(
        round(utils.ms_time_delay_to_time_quanta(psr.period, ch_number, psr)))

    ref_ch = int(input(
        "Reference channel (Refer Figure): "))  # where center of pulse is seen
    pulse_start_bin = int(input("Pulse start col number (Refer Figure): "))
    pulse_end_bin = int(input("Pulse end col number (Refer Figure): ")
                        )  # where center of pulse is seen

    pulse_width = int((pulse_end_bin - pulse_start_bin) / 2) + 1
    print(pulse_width)
    ref_col = int((pulse_end_bin + pulse_start_bin) / 2)
    print(ref_col)
    mask = np.ones((psr.n_channels, n_col), dtype=float)
    print(app.shape)
    print(mask.shape)
    if ref_col - pulse_width < 0:
        mask[:, :ref_col + pulse_width] = np.nan
        mask[:, n_col - (pulse_width - ref_col):] = np.nan

    elif ref_col - pulse_width >= 0 and ref_col + pulse_width <= n_col:
        mask[:, ref_col - pulse_width:ref_col + pulse_width] = np.nan

    elif ref_col + pulse_width > n_col:
        mask[:, ref_col - pulse_width:] = np.nan
        mask[:, :ref_col + pulse_width - n_col] = np.nan

    for ch in range(psr.n_channels):
        col_delay = int(
            round(calculate_dispersion_delay(ch, ref_ch, psr, ch_number)))
        temp = np.array(np.roll(mask[ch, :], col_delay))
        mask[ch, :] = temp

    #utils.plot_DS(mask.T)
    np.savetxt(utils.get_pulse_mask_filename(ch_number, root_dirname,
                                             polarization, psr),
               np.transpose(mask),
               fmt='%1.1f')

    masked_app = (app * mask).T
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        average_spectrum = np.nanmean(masked_app, axis=0)
    #plt.plot(average_spectrum)
    #plt.show()
    np.savetxt(utils.get_average_spectrum_filename(ch_number, root_dirname,
                                                   polarization, psr),
               average_spectrum,
               fmt='%1.1f')

    # print("Now Computing Spectrum Template")
    #
    # plt.figure("average pulse profile " + psr_name + " " + str(ch_number) + " " + polarization)
    # plt.imshow(app, interpolation="nearest")
    # plt.title("Note start and end column " + psr_name + " " + str(ch_number) + " " + polarization)
    # plt.show()
    #
    # col_start = int(input("Enter Starting Column (Refer Figure):"))
    # col_end = int(input("Enter End Column (Refer Figure):"))
    #
    # spec_template = np.mean(app[:, col_start:col_end],axis=1)
    # spec_template[spec_template == 0] = np.nan
    # plt.plot(spec_template)
    # plt.show()
    #
    # np.savetxt(get_avg_spectrum_template_filename(ch_number, root_dirname, polarization, psr), spec_template)
    quit()
Exemple #3
0
def main(file_name,
         ch_number,
         polarization,
         pulse_width_spec,
         chunk_rows=5000,
         decompression_method1=False,
         decompression_method2=False,
         plot_ds_ts_flag=False):
    global channel_number
    global psr

    if decompression_method1:
        print("decompressions by method 1 set to True")
    if decompression_method2:
        print("decompressions by method 2 set to True")

    polarization = polarization.upper()

    psr = PulsarInformationUtility(file_name)  # "B0834+06_20090725_114903"
    channel_number = int(ch_number[2:4])
    half_pulse_width_ch = int(
        round(psr.n_channels * int(pulse_width_spec) / 200))

    end_spec_file_flag = False
    root_dirname = str(Path(__file__).parent.parent.parent.absolute()) + '/'
    time_quanta_start = 0

    channel_to_frequency_array = get_channel_frequency()
    channel_to_time_delay_array = get_time_delay_array(
        channel_to_frequency_array)
    channel_to_column_delay = ms_time_delay_to_time_quanta(
        channel_to_time_delay_array, psr)
    max_dispersion_delay = int(round(channel_to_column_delay[0]))

    spec_file_paths = get_spec_file_paths(channel_number, polarization, psr,
                                          root_dirname)

    gain_correction_factor = 1
    if polarization not in polarizations:
        gain_correction_factor = get_gain_correction_factor(
            channel_number, psr, root_dirname)

    overflow_buffer = create_nan_array(int(round(channel_to_column_delay[0])),
                                       psr.n_channels)
    overflow_buffer_flag = False
    intensities, global_time_series = np.array([]), np.array([])

    pulse_mask = get_pulse_mask(channel_number, polarization, psr,
                                root_dirname)

    with ExitStack() as stack:
        spec_files = [
            stack.enter_context(open(filename, 'r'))
            for filename in spec_file_paths
        ]

        while not end_spec_file_flag:
            # read spec file and get dyn spec (gain corrected dyn spectrum incase of 'Q'
            dyn_spec, end_spec_file_flag = get_dyn_spec(
                chunk_rows, end_spec_file_flag, gain_correction_factor,
                polarization, spec_files)

            if dyn_spec.shape[0] == 0:
                print("dynamic spectrum chunk read is empty... Exiting")
                break

            if channel_number == 1:
                print("flagging majority channels for band 1")
                dyn_spec[:, :17] = np.nan
                dyn_spec[:, 108:] = np.nan
                dyn_spec[:, 39:44] = np.nan
                dyn_spec[:, 51:57] = np.nan
                dyn_spec[:, 59:70] = np.nan
                dyn_spec[:, 76:87] = np.nan
                dyn_spec[:, 89:94] = np.nan
                dyn_spec[13:17] = np.nan
                dyn_spec[:, 157:166] = np.nan
                dyn_spec[:, 149:153] = np.nan
                dyn_spec[:, 120:130] = np.nan

            # get time series in mili seconds and update next time quanta start
            dyn_spec_time_series = get_time_array(time_quanta_start,
                                                  dyn_spec.shape[0])

            print(
                f"read till time quanta: {time_quanta_start} -> {round(dyn_spec_time_series[-1], 2)} ms  --> "
                f"{round(dyn_spec_time_series[-1] / psr.period, 2)} periods")
            time_quanta_start = time_quanta_start + dyn_spec.shape[0]

            # remove rfi
            dyn_spec = utils.remove_rfi(dyn_spec, psr)

            # decompression
            if True:
                template_offpulse_spectrum = utils.get_robust_mean_rms_2d(
                    dyn_spec, psr.sigma_threshold)[0]
                decompress(decompression_method1, decompression_method2,
                           dyn_spec, template_offpulse_spectrum,
                           dyn_spec_time_series, half_pulse_width_ch,
                           pulse_mask, psr)
            if channel_number == 1:
                print("flagging majority channels for band 1")
                dyn_spec[:, :17] = np.nan
                dyn_spec[:, 108:] = np.nan
                dyn_spec[:, 39:44] = np.nan
                dyn_spec[:, 51:57] = np.nan
                dyn_spec[:, 59:70] = np.nan
                dyn_spec[:, 76:87] = np.nan
                dyn_spec[:, 89:94] = np.nan
                dyn_spec[13:17] = np.nan
                dyn_spec[:, 157:166] = np.nan
                dyn_spec[:, 149:153] = np.nan
                dyn_spec[:, 120:130] = np.nan

            if True:
                for index, spectrum in enumerate(dyn_spec):
                    if np.isnan(
                            spectrum).all():  # skip missing packets spectrum
                        continue
                    t = dyn_spec_time_series[index]
                    fractional_ms_time_in_a_period = t - int(
                        t / psr.period) * psr.period
                    mask_index = int(
                        round(
                            ms_time_delay_to_time_quanta(
                                fractional_ms_time_in_a_period, psr)))
                    if mask_index == pulse_mask.shape[0]:
                        mask_index = 0
                    invert_pulse_mask = np.array(np.isnan(pulse_mask),
                                                 dtype="float")
                    invert_pulse_mask[invert_pulse_mask == 0] = np.nan
                    dyn_spec[index] = spectrum * invert_pulse_mask[mask_index]

            # de disperse and add buffer
            dedispersed, overflow_buffer = de_disperse(
                dyn_spec, channel_to_column_delay, overflow_buffer,
                overflow_buffer_flag)
            # subtract robust mean
            subtract_robust_mean(dedispersed, psr.sigma_threshold)

            # freq integrate to find intensities and append to global array
            with warnings.catch_warnings():
                warnings.simplefilter("ignore", category=RuntimeWarning)
                count_nonnan = np.sum(~np.isnan(dedispersed), axis=1)
                count_filter_flag = count_nonnan > 10  # todo - name this well
                integrated = np.nanmean(dedispersed,
                                        axis=1) * count_filter_flag
                intensities = np.append(intensities, integrated)

            # plot DS and corresponding TS
            # continue_flag = True
            # if int(dyn_spec_time_series[-1] / psr.period) > 400:
            #     continue_flag = False
            # if not continue_flag:
            #     break
            if plot_ds_ts_flag:
                plot_DS_and_TS(dedispersed, intensities[-1 * chunk_rows:])

            overflow_buffer_flag = True

        global_time_series = get_time_array(0, intensities.shape[0])
        # plt.plot(global_time_series, intensities)
        # plt.show()

        # save data
        time_series_filename = utils.get_time_series_filename(
            channel_number, root_dirname, polarization, psr)

        np.savetxt(time_series_filename,
                   np.array((global_time_series, intensities)).T)
        print("time series data saved in file: ", time_series_filename)
def main(file_name, ch_number, polarization, p4: int, p1bins: int,
         p4bins: int):
    psr = PulsarInformationUtility(file_name)
    channel_number = int(ch_number[2:4])
    root_dirname = str(Path(__file__).parent.parent.parent.absolute()) + '/'

    binned_time_series_filename = utils.get_binned_time_series_filename(
        channel_number, root_dirname, polarization, psr)

    with open(
            binned_time_series_filename, 'r'
    ) as binned_time_series_file:  # todo - can be extracted to a common util
        binned_time_series = np.loadtxt(binned_time_series_file)

    pulse_stack = binned_time_series.reshape(-1, p1bins)

    std = np.nanstd(pulse_stack[:, 800:], axis=1)

    # plt.figure()
    # plt.title("std")
    # plt.plot(std, np.arange(pulse_stack.shape[0]))
    # plt.ylim(0, pulse_stack.shape[0])
    # plt.show()

    # std_cutoff = float(input("enter cutoff std value: "))
    std_cutoff = {1: 4, 3: 1.6}.get(channel_number)
    std_arr = std < std_cutoff

    for i in range(pulse_stack.shape[0]):
        pulse_stack[i] = pulse_stack[i] * std_arr[i]

    # plt.imshow(pulse_stack.reshape(-1, p1bins), extent=[0, 1, 0, pulse_stack.shape[0]], origin="lower",
    #            aspect="auto")
    # plt.xlabel("Longitude")
    # plt.ylabel("Period")
    # plt.show()

    pulse_stack_properties_filename = utils.get_pulse_stack_properties_filename(
        root_dirname)
    pulse_stack_properties = np.loadtxt(pulse_stack_properties_filename)
    previous_properties_flag = False
    if pulse_stack_properties.shape[0] == 4:
        print(
            f"previous data used: \n"
            f"Input longitude where the pulse starts: {pulse_stack_properties[0]}\n"
            f"Input longitude where the pulse ends: {pulse_stack_properties[1]}\n"
            f"Input start period number : {pulse_stack_properties[2]}\n"
            f"Input end period number : {pulse_stack_properties[3]}")
        # previous_properties_flag = True if input("use same? (y/n): ").lower() == 'y' else False
        previous_properties_flag = True

    if not previous_properties_flag:
        plot_pulse_stack(pulse_stack)
        plot_integrated_pulse_profile(ch_number, file_name, pulse_stack)
        longitude_start = int(
            float(input("Input longitude where the pulse starts: ")) * p1bins)
        longitude_end = int(
            float(input("Input longitude where the pulse ends: ")) * p1bins)
        period_start = int(input("Input start period number : "))
        period_end = int(input("Input end period number : "))
        # np.savetxt(pulse_stack_properties_filename,
        # np.array([longitude_start/1000, longitude_end/1000, period_start, period_end]))
    else:
        longitude_start = int(float(pulse_stack_properties[0]) * p1bins)
        longitude_end = int(float(pulse_stack_properties[1]) * p1bins)
        period_start = int(pulse_stack_properties[2])
        period_end = int(pulse_stack_properties[3])

    pulse_stack = pulse_stack[period_start:period_end,
                              longitude_start:longitude_end]

    if channel_number == 1:
        ch_1_period_flags = [
            27, 84, 56, 69, 111, 114, 123, 125, 128, 131, 170, 181, 195, 198,
            221, 222, 219, 247, 273, 279, 291, 316, 341, 342, 361, 383, 407,
            416, 438, 459, 467, 469, 485, 488, 492, 493, 519, 520, 527, 531,
            560, 586, 588, 591, 596, 626, 636, 693, 703, 714, 733, 745, 774,
            788
        ]
        pulse_stack[ch_1_period_flags] = 0

    folding_parameter_list = np.linspace(5, 25, 2001)

    sum_of_squares_list = np.zeros(folding_parameter_list.shape)
    for i in range(folding_parameter_list.shape[0]):
        folded_pulse_stack = get_foldedpulsestack.main(
            file_name, ch_number, polarization, folding_parameter_list[i],
            p1bins, p4bins, pulse_stack)
        sum_of_squares_list[i] = np.nansum(np.square(folded_pulse_stack))
        print(f"Done for {folding_parameter_list[i]}")

    save = np.zeros((folding_parameter_list.shape[0], 2))
    save[:, 0] = folding_parameter_list
    save[:, 1] = sum_of_squares_list
    #np.savetxt(f"~/RRIProject/desh_mail/FindP4/B0809+74_ch0{channel_number}_FindP4.dat", save)
    plt.plot(folding_parameter_list, sum_of_squares_list)
    plt.xlim(3, 25)
    plt.xlabel("P3 -->")
    plt.title("Auto correlation to find P3")
    max_argument = np.argmax(sum_of_squares_list)
    plt.scatter(folding_parameter_list[max_argument],
                sum_of_squares_list[max_argument],
                color="r",
                marker="x")
    #plt.savefig(f"~/RRIProject/desh_mail/FindP4/B0809+74_ch0{channel_number}_FindP4.png")
    plt.show()
    print("end")
def main(pulsar_name, ref_ch_number, second_ch_number):
    ref_channel_number = int(ref_ch_number[2:4])
    second_channel_number = int(second_ch_number[2:4])
    root_dirname = str(Path(__file__).parent.parent.parent.absolute()) + '/'
    psr = PulsarInformationUtility(pulsar_name)

    ref_pulse_stack_filename = utils.get_folded_pulse_stack_filename(
        ref_channel_number, root_dirname, psr)
    with open(ref_pulse_stack_filename) as ref_pulse_stack_file:
        ref_pulse_stack = np.loadtxt(ref_pulse_stack_file)
    # plt.imshow(ref_pulse_stack, origin="lower")
    # plt.show()

    second_pulse_stack_filename = utils.get_folded_pulse_stack_filename(
        second_channel_number, root_dirname, psr)
    with open(second_pulse_stack_filename) as second_pulse_stack_file:
        second_pulse_stack = np.loadtxt(second_pulse_stack_file)
    # plt.imshow(second_pulse_stack, origin="lower")
    # plt.show()

    correlation_matrix = np.zeros(ref_pulse_stack.shape)
    for row_shift in range(-99, 101):  # range(-49, 51)
        row_shifted = np.roll(second_pulse_stack, row_shift, axis=0)
        for col_shift in range(-100, 99):  # range(-100, 99)
            correlation_matrix[row_shift + 99, col_shift + 100] = np.sum(
                ref_pulse_stack * np.roll(row_shifted, col_shift, axis=1))

    # correlation_matrix = signal.correlate2d(ref_pulse_stack, second_pulse_stack)

    plt.imshow(correlation_matrix,
               origin="lower",
               extent=[-100 / 1000, 99 / 1000, -99 / 100, 100 / 100],
               cmap="coolwarm",
               aspect="auto")
    plt.colorbar()
    plt.xlabel("Shift in pulse phase")
    plt.ylabel("Shift in modulation phase")
    plt.title(
        f"Cross correlation between band {ref_ch_number} and {second_ch_number}"
    )
    # plt.text(0.5, 0.5, "sample", col)

    position_max = np.unravel_index(correlation_matrix.argmax(),
                                    correlation_matrix.shape)
    print(position_max)
    shift_pulse_phase = (position_max[1] - 100) / 1000
    shift_modulation_phase = (position_max[0] - 99) / 200
    plt.scatter(shift_pulse_phase,
                shift_modulation_phase,
                color="y",
                marker="x")
    plt.annotate(
        f"Max: {round(correlation_matrix[position_max[0], position_max[1]], 2)} \n"
        f"({shift_pulse_phase},{shift_modulation_phase})",
        (shift_pulse_phase, shift_modulation_phase),
        xytext=(shift_pulse_phase + 0.002, shift_modulation_phase - 0.03))
    plt.show()
    print(
        f"shift_pulse_phase: {shift_pulse_phase}... shift_modulation_phase: {shift_modulation_phase}"
    )

    plt.xlabel("Shift in modulation phase")
    plt.title(f"Cross correlation at max allignment in pulse phase\n"
              f"between bands {ref_ch_number} and {second_ch_number}")
    plt.plot(np.linspace(-0.5, 0.495, 200),
             correlation_matrix[:, position_max[1]])
    plt.show()
def main(file_name, ch_number, polarization, specfile_chunk_size=5000):
    global channel_number
    global psr

    psr = PulsarInformationUtility(file_name)  # "B0834+06_20090725_114903"
    channel_number = int(ch_number[2:4])
    bins = int(round(ms_time_delay_to_time_quanta(psr.period, psr)))
    sum_pulse_profile = create_nan_array(bins, psr.n_channels)
    counts_array = np.zeros(sum_pulse_profile.shape)
    end_spec_file_flag = False
    root_dirname = str(Path(__file__).parent.parent.parent.absolute()) + '/'
    time_quanta_start = 0

    spec_file_path = utils.get_spec_file_name(root_dirname, psr,
                                              channel_number, polarization)
    print("spec file path: ", spec_file_path)

    if not isfile(spec_file_path):
        print(
            f"{bcolors.FAIL}file '{spec_file_path}' does not exist.\nExiting...{bcolors.ENDC}"
        )
        exit()
    print(f"reading file {spec_file_path[112:]}")

    # read
    with open(spec_file_path, 'r') as spec_file:
        while not end_spec_file_flag:
            # read file
            dyn_spec, end_spec_file_flag = read_spec_file(
                end_spec_file_flag, specfile_chunk_size, spec_file)

            # get time series in mili seconds
            time_array, time_quanta_start = get_time_array(
                time_quanta_start, dyn_spec.shape[0])
            print(
                f"read till time quanta: {time_quanta_start} -> {round(time_array[-1], 2)} ms  --> "
                f"{round(time_array[-1] / psr.period, 2)} periods")

            # remove rfi
            dyn_spec = utils.remove_rfi(dyn_spec, psr)

            # interpolate
            interpolated = interpolate_2D(dyn_spec, time_array, bins, psr)

            counts_array = counts_array + (~np.isnan(interpolated)).astype(int)
            with warnings.catch_warnings():
                warnings.simplefilter("ignore", category=RuntimeWarning)
                sum_pulse_profile = np.nansum(np.dstack(
                    (sum_pulse_profile, interpolated)),
                                              axis=2)
            sum_pulse_profile[sum_pulse_profile == 0] = np.nan

            continue_flag = True
            if int(time_array[-1] / psr.period) > 400:
                continue_flag = False
            if not continue_flag:
                break
            if end_spec_file_flag:
                break

        # utils.plot_DS(average_pulse_profile, color="hot")
        average_pulse_profile = sum_pulse_profile / counts_array
        output_filename = utils.get_average_pulse_file_name(
            root_dirname, psr, channel_number, polarization)
        np.savetxt(output_filename, average_pulse_profile)
        print("average pulse profile saved in file: ", output_filename)
def main(file_name, ch_number, polarization):
    global channel_number
    global psr

    psr = PulsarInformationUtility(file_name)  # "B0834+06_20090725_114903"
    channel_number = int(ch_number[2:4])
    root_dirname = str(Path(__file__).parent.parent.parent.absolute()) + '/'

    average_pulse_file_path = utils.get_average_pulse_file_name(
        root_dirname, psr, channel_number, polarization)

    if not isfile(average_pulse_file_path):
        print(
            f"{bcolors.FAIL}file '{average_pulse_file_path}' does not exist.\nExiting...{bcolors.ENDC}"
        )
        exit()
    print(f"reading file {average_pulse_file_path[112:]}")
    average_pulse = np.loadtxt(average_pulse_file_path)
    bins = average_pulse.shape[0]

    # de disperse:
    channel_to_frequency_array = get_channel_frequency(psr, channel_number)

    dm_initial = psr.dm
    dm_linspace = np.linspace(dm_initial - 3, dm_initial + 3, 601)
    # dm_linspace = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.88])
    result = np.zeros(dm_linspace.shape)

    for i in range(dm_linspace.shape[0]):
        print(i)

        dedisperse_pulse = de_disperse(average_pulse,
                                       channel_to_frequency_array,
                                       dm_linspace[i], bins)
        integrated = np.nanmean(dedisperse_pulse, axis=1)
        # isnan_count = np.count_nonzero(np.isnan(dedisperse_pulse), axis = 1)
        result[i] = np.nansum(np.square(integrated))

        if False:
            figure, axis = plt.subplots(2, 1)
            n_rows = dedisperse_pulse.shape[0]
            axis[0].imshow(np.transpose(dedisperse_pulse),
                           interpolation="nearest",
                           aspect='auto',
                           cmap="gray",
                           extent=[0, 1, 256, 0])
            axis[0].xaxis.set_label_position('top')
            axis[0].set_xlabel('Folded Pulse Profile')
            axis[0].set_ylabel('Freq Channel')
            # axis[0].
            axis[1].plot(np.linspace(0, 1, n_rows), integrated)
            axis[1].xaxis.set_label_position('bottom')
            axis[1].set_xlabel("Frequency Integrated")
            #axis[1].axis(xmin=0, xmax=1, ymax=36.7, ymin=29.9)
            axis[1].text(0.9,
                         0.5,
                         "DM = " + str(round(dm_linspace[i], 2)),
                         horizontalalignment='center',
                         verticalalignment='center',
                         transform=axis[1].transAxes)
            plt.show()

    plt.plot(dm_linspace, result)
    plt.show()
Exemple #8
0
def main(file_name,
         synchronization_flag=False,
         plot_dynamic_spectrum_flag_xx=False,
         plot_dynamic_spectrum_flag_yy=False,
         plot_dynamic_spectrum_flag_realxy=False,
         plot_dynamic_spectrum_flag_imagxy=False,
         pulsar_information_utility_flag=False,
         timer_flag=False):
    psr = PulsarInformationUtility(file_name[5:])  # "B0834+06_20090725_114903"

    if pulsar_information_utility_flag:
        # todo -> print config and ask to run()
        psr.run()

    # declare variables and constants
    seq_number = 0
    global_packet_count = 0
    first_packet_flag = True
    channel_number = int(file_name[2:4])
    root_dirname = str(Path(__file__).parent.parent.parent.absolute()) + '/'
    missing_packets_array = np.empty(psr.n_channels * 2)
    missing_packets_array.fill(np.nan)
    skip_to_n_packets = int(psr.band[channel_number].sync_first_packet) + \
                        int(psr.band[channel_number].sync_dispersion_delay_packet)
    plot_dynamic_spectra_flag = plot_dynamic_spectrum_flag_xx or plot_dynamic_spectrum_flag_yy or \
                                plot_dynamic_spectrum_flag_realxy or plot_dynamic_spectrum_flag_yy

    # check if output data already exists and ask to delete it
    check_existing_spec_data(root_dirname, channel_number, psr)

    if timer_flag:
        time_arr = []
    while True:
        current_mbr_filename = get_mbr_filename(root_dirname, file_name,
                                                channel_number, seq_number)
        mbr_filesize = getsize(current_mbr_filename)
        packets_in_mbr_file = int(mbr_filesize / packet.packetSize)
        mbr_file = read_mbr_file(current_mbr_filename)

        for part_i in range(psr.n_parts):
            if timer_flag:
                start = time.perf_counter()
                print("timer started")

            print_count = 0  # todo just for printing status of completion(can add a status bar)

            packet_list = packet.mbr2packetList(
                mbr_file.read(int(mbr_filesize / psr.n_parts)))
            print("packets read")

            zeroth_packet_number = packet_list[0].packetNumber

            if synchronization_flag:
                # skip whole part if skip_n_packets > last_packet_number
                # todo check this logic
                if skip_to_n_packets > packet_list[-1].packetNumber:
                    continue
                else:
                    if skip_to_n_packets >= packet_list[0].packetNumber:
                        zeroth_packet_number = skip_to_n_packets
                    else:
                        zeroth_packet_number = packet_list[0].packetNumber

            empty_dynamic_sequence = np.empty(
                (packet_list[-1].packetNumber - zeroth_packet_number + 1,
                 psr.n_channels * 2))
            empty_dynamic_sequence.fill(np.nan)
            x_polarization_dynamic_seq = np.array(empty_dynamic_sequence,
                                                  copy=True)
            y_polarization_dynamic_seq = np.array(empty_dynamic_sequence,
                                                  copy=True)

            dynamic_sequence_packet_count = 0

            for pkt in packet_list:
                current_pkt_number = pkt.packetNumber
                if synchronization_flag:
                    if current_pkt_number < skip_to_n_packets:
                        continue

                if print_count % 25000 == 1:  # todo add status bar
                    print("Ch:" + str(channel_number) + "	Seq Number:" +
                          str(seq_number) + "		" +
                          "{:.2f}".format((
                              (packets_in_mbr_file / psr.n_parts) * part_i +
                              dynamic_sequence_packet_count) * 100 /
                                          packets_in_mbr_file) + "% complete")
                print_count = print_count + 1

                if not first_packet_flag:
                    if global_packet_count != current_pkt_number:
                        # if missing packet, increase global count and col count, ie dynamic_sequence_packet_count
                        dynamic_sequence_packet_count = current_pkt_number - zeroth_packet_number
                        global_packet_count = current_pkt_number

                        # add read packet data to dynamic seq array
                        dynamic_sequence_packet_count, global_packet_count = write_packet_data_to_dynamic_sequence(
                            dynamic_sequence_packet_count, global_packet_count,
                            pkt, x_polarization_dynamic_seq,
                            y_polarization_dynamic_seq)

                        # todo handle edge condition where missing packets between two mbr files

                    else:
                        dynamic_sequence_packet_count, global_packet_count = write_packet_data_to_dynamic_sequence(
                            dynamic_sequence_packet_count, global_packet_count,
                            pkt, x_polarization_dynamic_seq,
                            y_polarization_dynamic_seq)

                elif first_packet_flag:
                    first_packet_number = current_pkt_number
                    global_packet_count = current_pkt_number
                    first_packet_flag = False

                    # add read packet data to dynamic seq array
                    dynamic_sequence_packet_count, global_packet_count = write_packet_data_to_dynamic_sequence(
                        dynamic_sequence_packet_count, global_packet_count,
                        pkt, x_polarization_dynamic_seq,
                        y_polarization_dynamic_seq)

            # do fft for dyn seq to get dynamic spectra
            dynamic_spectrum_cross, dynamic_spectrum_x, dynamic_spectrum_y = compute_dynamic_spectrum_all(
                x_polarization_dynamic_seq, y_polarization_dynamic_seq)

            # remove first and last 5 columns
            clean_dynamic_spectrum_all(dynamic_spectrum_cross,
                                       dynamic_spectrum_x, dynamic_spectrum_y)

            # integrate this dynamic spectrum
            # this can be an object and method
            dynamic_spectrum_cross, dynamic_spectrum_x, dynamic_spectrum_y = integrate_dynamic_spectrum_all(
                dynamic_spectrum_cross, dynamic_spectrum_x, dynamic_spectrum_y,
                missing_packets_array, psr.n_channels,
                psr.n_packet_integration)

            if plot_dynamic_spectra_flag:
                plot_dynamic_spectra_flag = plotting_util(
                    dynamic_spectrum_cross, dynamic_spectrum_x,
                    dynamic_spectrum_y, plot_dynamic_spectra_flag,
                    plot_dynamic_spectrum_flag_imagxy,
                    plot_dynamic_spectrum_flag_realxy,
                    plot_dynamic_spectrum_flag_xx,
                    plot_dynamic_spectrum_flag_yy)

            save_spec_file_all(channel_number, dynamic_spectrum_cross,
                               dynamic_spectrum_x, dynamic_spectrum_y,
                               file_name, root_dirname, seq_number, psr)

            if timer_flag:
                timer_util(part_i, psr, start, time_arr)

        print(f"closing mbr file: {mbr_file.name}")
        mbr_file.close()
        seq_number = seq_number + 1
        if not isfile(get_mbr_filename(root_dirname, file_name, channel_number, seq_number)) \
                or seq_number > psr.last_sequence_number:
            break