def find_parameter_value(line, string_name):
    if string_name in line:
        value = (find_between(line, string_name, ','))
        if value == 'Error!':
            begin = line.index(string_name) + len(string_name)
            value = line[begin:]
    else:
        value = None
    return value
Ejemplo n.º 2
0
def f_get_adr_parameters(serversocket, print_or_not):
    """
    Function requests ADR receiver parameters via specified socket and prints them
    Input parameters:
        serversocket        - handle of socket to send and receive messages from server
        print_or_not        - to print the parameters to console (1) or not (0)
    Output parameters:
        parameters_dict     - dictionary with current parameters of ADR receiver
    """
    parameters_dict = {}

    serversocket.send(
        (b'get prc/srv/ctl/pth\0'))  # read directory where data are stored
    data = f_read_adr_meassage(serversocket, 0)
    parameters_dict["save_data_path"] = find_between(data, 'SUCCESS\n', '\n')

    serversocket.send(
        (b'get prc/srv/ctl/sys\0'))  # read directory where data are stored
    data = f_read_adr_meassage(serversocket, 0)
    parameters_dict["receiver_name"] = find_between(data, 'SUCCESS\n', '\n')

    serversocket.send(
        (b'get prc/srv/ctl/plc\0'))  # read directory where data are stored
    data = f_read_adr_meassage(serversocket, 0)
    parameters_dict["observation_place"] = find_between(
        data, 'SUCCESS\n', '\n')

    serversocket.send(
        (b'get prc/srv/ctl/dsc\0'))  # read directory where data are stored
    data = f_read_adr_meassage(serversocket, 0)
    parameters_dict["file_description"] = find_between(data, 'SUCCESS\n', '\n')

    serversocket.send((b'get prc/dsp/ctl/opt\0'))  #
    data = f_read_adr_meassage(serversocket, 0)
    parameters_dict["synchro_start"] = find_between(data, 'SyncStart: ', '\n')
    parameters_dict["external_clock"] = find_between(data, 'Ext.CLC: ', '\n')
    parameters_dict["fft_window"] = find_between(data, 'FFT_Window: ', '\n')
    parameters_dict["sum_diff_mode"] = find_between(data, 'A+B/A-B: ', '\n')

    serversocket.send((b'get prc/dsp/ctl/mdo\0'))  #
    data = f_read_adr_meassage(serversocket, 0)
    parameters_dict["operation_mode_num"] = int(
        find_between(data, 'SUCCESS\n', ' - Mode'))
    if parameters_dict["operation_mode_num"] == 0:
        parameters_dict["operation_mode_str"] = 'Waveform ch. A'
    elif parameters_dict["operation_mode_num"] == 1:
        parameters_dict["operation_mode_str"] = 'Waveform ch. B'
    elif parameters_dict["operation_mode_num"] == 2:
        parameters_dict["operation_mode_str"] = 'Waveform ch. A and B'
    elif parameters_dict["operation_mode_num"] == 3:
        parameters_dict["operation_mode_str"] = 'Spectra ch. A'
    elif parameters_dict["operation_mode_num"] == 4:
        parameters_dict["operation_mode_str"] = 'Spectra ch. B'
    elif parameters_dict["operation_mode_num"] == 5:
        parameters_dict["operation_mode_str"] = 'Spectra ch. A and B'
    elif parameters_dict["operation_mode_num"] == 6:
        parameters_dict["operation_mode_str"] = 'Correlation ch. A and B'
    else:
        parameters_dict["operation_mode_str"] = 'Unknown mode'

    parameters_dict["FFT_size_samples"] = int(
        find_between(data, 'Mode (index)\n', ' - FFT Size'))
    parameters_dict["spectra_averaging"] = int(
        find_between(data, 'FFT Size (samples)\n', ' - Averaging'))
    parameters_dict["start_line_freq"] = int(
        find_between(data, 'Averaging (Spectra count)\n', ' - Start line'))
    parameters_dict["width_line_freq"] = int(
        find_between(data, 'Start line (count)\n', ' - Width'))
    parameters_dict["clock_frequency"] = int(
        find_between(data, 'Width (count)\n', ' - ADC CLOCK'))

    serversocket.send((b'get prc/srv/ctl/srd\0'))  #
    data = f_read_adr_meassage(serversocket, 0)
    parameters_dict["data_recording"] = int(
        find_between(data, 'SUCCESS\n', ' - Save on/off'))
    parameters_dict["files_autocreation"] = int(
        find_between(data, 'on/off  (On/Off)\n', ' - Autocreation'))
    parameters_dict["size_of_file"] = int(
        find_between(data, 'Autocreation  (On/Off)\n', ' - Size restriction'))
    parameters_dict["time_of_file"] = int(
        find_between(data, 'restriction  (MB)\n', ' - Time restriction'))

    # Calculation of frequency and time parameters
    parameters_dict["time_resolution"] = parameters_dict["spectra_averaging"] * \
        (parameters_dict["FFT_size_samples"] / float(parameters_dict["clock_frequency"]))
    parameters_dict["frequency_resolution"] = \
        float(parameters_dict["clock_frequency"]) / parameters_dict["FFT_size_samples"]
    parameters_dict["number_of_channels"] = int(
        parameters_dict["width_line_freq"] * 1024)
    parameters_dict["lowest_frequency"] = \
        parameters_dict["start_line_freq"] * 1024 * parameters_dict["frequency_resolution"]
    parameters_dict["highest_frequency"] = (
        parameters_dict["lowest_frequency"] +
        parameters_dict["number_of_channels"] *
        parameters_dict["frequency_resolution"])

    # Printing the parameters to console
    if print_or_not > 0:
        print('\n * Current ADR parameters:')
        print('\n   File description: \n\n  ',
              parameters_dict["file_description"], '\n')
        print('   Path to save data:            ',
              parameters_dict["save_data_path"])
        print('   Observation place:            ',
              parameters_dict["observation_place"])
        print('   Receiver name:                ',
              parameters_dict["receiver_name"])

        print('\n   Time resolution:              ',
              round(parameters_dict["time_resolution"], 3), ' s.')
        print('   Frequency resolution:         ',
              round(parameters_dict["frequency_resolution"] / 1000, 3),
              ' kHz.')
        print('   Frequency range:              ',
              round(parameters_dict["lowest_frequency"] / 1000000, 3), ' - ',
              round(parameters_dict["highest_frequency"] / 1000000, 3), ' MHz')

        print('\n   ADR operation mode:           ',
              parameters_dict["operation_mode_str"])
        if parameters_dict["external_clock"] == 'OFF':
            print('   External 160 MHz clock:        (!)',
                  parameters_dict["external_clock"])
        else:
            print('   External 160 MHz clock:       ',
                  parameters_dict["external_clock"])

        print(
            '   Sampling frequency:           ',
            format(parameters_dict["clock_frequency"],
                   ',').replace(',', ' ').replace('.', ','), ' Hz')
        print('   FFT samples number:           ',
              parameters_dict["FFT_size_samples"])
        print('   Number of frequency channels: ',
              parameters_dict["number_of_channels"])
        if parameters_dict["sum_diff_mode"] == 'ON':
            print('   Sum/diff mode:                 (!)',
                  parameters_dict["sum_diff_mode"])
        else:
            print('   Sum/diff mode:                ',
                  parameters_dict["sum_diff_mode"])

        if parameters_dict["files_autocreation"] == 1:
            print('   Files autocreation:            ON')
        else:
            print('   Files autocreation:             (!) OFF')

    return parameters_dict
Ejemplo n.º 3
0
    only_subfolder_list = []
    only_subfolder_list.append(temp_subfolder_list[2*subfolder])
    only_subfolder_list.append(temp_subfolder_list[2*subfolder+1])

    # Finding the folder that ends with needed source names
    # !!! There should be only one subfolder with this source name !!!

    for i in range(len(only_subfolder_list)):
        if only_subfolder_list[i].endswith('3C405'):
            path_to_data_SygA = only_subfolder_list[i]+'/'
        if only_subfolder_list[i].endswith('3C461'):
            path_to_data_CasA = only_subfolder_list[i]+'/'


    # Finding the initial data files names in the folder names
    file_name_SygA = find_between(path_to_data_SygA, 'Results_', '_3C')
    file_name_CasA = find_between(path_to_data_CasA, 'Results_', '_3C')

    '''
    if   '.jds' in path_to_data_SygA and '.jds' in path_to_data_CasA:
        result_path = 'CasA secular decrease UTR2 time variations'
    elif '.adr' in path_to_data_SygA and '.adr' in path_to_data_CasA:
        result_path = 'CasA secular decrease GURT time variations'
    else:
        print ('   ERROR! Cannot find the correct file extension in folder name!!! \n\n')
        sys.exit('           Program stopped! \n\n')

    if not os.path.exists(result_path):
        os.makedirs(result_path)
    '''
Ejemplo n.º 4
0
def f_SMD_analyzer_param_reader():
    '''
    Reads parameters of average pulsar pulse analysis from file "script_SMD_analyzer_parameters.py"
    and passes them to the main script "script_SMD_analyzer.py"
    '''

    file = open('package_pulsar_processing/param_SMD_analyzer_parameters.py',
                'r')

    print('\n  * Parameters of analysis: \n')

    for line in file:
        if not line.startswith('#') and len(line) > 1:

            if line.startswith('path ='):
                path = find_between(line.split()[2], "'", "'")
                print(' File path:  ', path, '\n')

            if line.startswith('filename ='):
                filename = find_between(line.split()[2], "'", "'")
                print(' File name:  ', filename, '\n')

            if line.startswith('no_of_DM_steps ='):
                no_of_DM_steps = int(line.split()[2])
                print(' Number of DM analysis steps =        ', no_of_DM_steps)

            if line.startswith('DM_var_step ='):
                DM_var_step = float(line.split()[2])
                print(' Step of DM analysis =                ', DM_var_step)

            if line.startswith('save_intermediate_data ='):
                save_intermediate_data = int(line.split()[2])
                print(' Save intermediate data?              ',
                      save_intermediate_data)

            if line.startswith('AverageChannelNumber ='):
                AverageChannelNumber = int(line.split()[2])
                print(' Number of channels to average =      ',
                      AverageChannelNumber)

            if line.startswith('AverageTPointsNumber ='):
                AverageTPointsNumber = int(line.split()[2])
                print(' Number of time points to average =   ',
                      AverageTPointsNumber)

            if line.startswith('frequency_band_cut ='):
                frequency_band_cut = int(line.split()[2])
                print(' Make cuts of frequency bands?        ',
                      frequency_band_cut)

            if line.startswith('specify_freq_range ='):
                specify_freq_range = int(line.split()[2])
                print(' Specify particular frequency range?  ',
                      specify_freq_range)

            if line.startswith('frequency_cuts ='):
                temp = find_between(line, '[', ']')
                frequency_cuts = []
                for i in range(len(temp.split(','))):
                    frequency_cuts.append(float(temp.split(',')[i]))
                print(' Frequencies to cut the range         ', frequency_cuts)

            if line.startswith('colormap ='):
                colormap = find_between(line.split()[2], "'", "'")
                print(' Color map =                          ', colormap)
            if line.startswith('customDPI ='):
                customDPI = int(line.split()[2])
                print(' DPI of plots =                       ', customDPI)
            if line.startswith('freqStartArray ='):
                freqStartArray = float(line.split()[2])
                print(' Lowest frequency of the band =       ', freqStartArray)
            if line.startswith('freqStopArray ='):
                freqStopArray = float(line.split()[2])
                print(' Highest frequency of the band =      ', freqStopArray)
            if line.startswith('DM ='):
                DM = float(line.split()[2])
                print(' Initial Dispersion measure =         ', DM,
                      ' pc / cm3 \n')

    return filename, path, DM, no_of_DM_steps, DM_var_step, save_intermediate_data, AverageChannelNumber, AverageTPointsNumber, frequency_band_cut, specify_freq_range, frequency_cuts, colormap, customDPI, freqStartArray, freqStopArray
def cut_needed_time_points_from_dat_to_txt(path, filename):
    import matplotlib
    matplotlib.use('TkAgg')

    """
    Function to cut the part of pulsar period data with the pulse from dat file to the txt file
    User is asked to specify the start and stop point of the selected pulse period.
    Returns the start and stop point numbers, the data and image are saved to HDD
    """
    software_version = '2021.08.08'
    current_time = time.strftime("%H:%M:%S")
    current_date = time.strftime("%d.%m.%Y")

    data_file = open(path + '/' + filename, 'rb')
    df_filepath = data_file.read(32).decode('utf-8').rstrip('\x00')  # Initial data file name
    data_file.close()

    if df_filepath[-4:] == '.jds':  # If data obtained from DSPZ receiver

        [df_filepath, df_filesize, df_system_name, df_obs_place, df_description,
         CLCfrq, df_creation_timeUTC, SpInFile, ReceiverMode, Mode, Navr, time_resolution, fmin, fmax,
         df, frequency, freq_points_num, dataBlockSize] = FileHeaderReaderJDS(path + '/' + filename, 0, 0)

    spectra_to_read = int((df_filesize - 1024) / (8 * freq_points_num))

    data_file = open(path + '/' + filename, 'rb')
    data_file.seek(1024, os.SEEK_SET)
    array = np.fromfile(data_file, dtype=np.float64, count=spectra_to_read * freq_points_num)
    array = np.reshape(array, [freq_points_num, spectra_to_read], order='F')
    data_file.close()
    print('  Shape of the array from dat file: ', array.shape)

    # Logging data
    array = 10 * np.log10(array)

    # Normalizing data
    array = array - np.mean(array)

    # Show the pulse profile to select the start and end points of the cut
    fig = plt.figure(figsize=(16.0, 9.0))
    ax1 = fig.add_subplot(111)
    ax1.plot(np.mean(array, axis=0), linewidth='0.50')
    ax1.set_xlim(xmin=0, xmax=array.shape[1])
    ax1.tick_params(axis='both', which='major', labelsize=10)
    ax1.set_xlabel('Time points to select', fontsize=8, fontweight='bold')
    ax1.set_ylabel('Amplitude, a.u.', fontsize=8, fontweight='bold')
    plt.show()

    # Enter the points from the keyboard
    start_point = int(input('\n  Begin from point:                  '))
    end_point = int(input('  End at point:                      '))

    result_array = array[:, start_point: end_point]
    del array
    print('\n  Shape of result array:            ', result_array.shape)

    # Save cut data to the new txt file
    single_pulse_txt = open(path + '/Extracted_s' + filename[1:-4] + '.txt', "w")
    for freq in range(result_array.shape[0] - 1):
        single_pulse_txt.write(' '.join('  {:+12.7E}'.format(result_array[freq, i]) \
                                        for i in range(result_array.shape[1])) + ' \n')
    single_pulse_txt.close()

    # Making result figure with dynamic spectra and profiles in full band and split in subbands
    fig = plt.figure(figsize=(16.0, 8.0))
    gs = GridSpec(3, 5, figure=fig)
    rc('font', size=8, weight='bold')

    ax1 = fig.add_subplot(gs[0:2, 0])
    ax1.set_title('Full band, points: ' + str(start_point) + ' - ' + str(end_point), fontsize=8, fontweight='bold')
    ax1.imshow(np.flipud(result_array), aspect='auto', cmap=colormap, vmin=spectrum_pic_min, vmax=spectrum_pic_max,
               extent=[0, result_array.shape[1], 16.5, 33.0])
    ax1.xaxis.set_ticklabels([])
    ax1.set_ylabel('Frequency, MHz', fontsize=10, fontweight='bold')
    ax2 = fig.add_subplot(gs[2, 0])
    ax2.plot(np.mean(result_array, axis=0), linewidth='0.50', color='C1')
    ax2.set_xlim(xmin=0, xmax=result_array.shape[1])
    ax2.set_xlabel('Time points', fontsize=10, fontweight='bold')
    ax2.set_ylabel('Amplitude, a.u.', fontsize=10, fontweight='bold')

    ax3 = fig.add_subplot(gs[0:2, 1])
    ax3.set_title('16.5 - 20.5 MHz', fontsize=8, fontweight='bold')
    ax3.imshow(np.flipud(result_array[0:1986]), aspect='auto', cmap=colormap, vmin=spectrum_pic_min,
               vmax=spectrum_pic_max, extent=[0, result_array.shape[1], 16.5, 20.5])
    ax3.xaxis.set_ticklabels([])
    ax4 = fig.add_subplot(gs[2, 1])
    ax4.plot(np.mean(result_array[0:1986], axis=0), linewidth='0.50', color='C4')
    ax4.set_xlim(xmin=0, xmax=result_array.shape[1])
    ax4.set_yticklabels([])
    ax4.set_yticks([])
    ax4.set_xlabel('Time points', fontsize=10, fontweight='bold')

    ax5 = fig.add_subplot(gs[0:2, 2])
    ax5.set_title('20.5 - 24.5 MHz', fontsize=8, fontweight='bold')
    ax5.imshow(np.flipud(result_array[1986:3972]), aspect='auto', cmap=colormap, vmin=spectrum_pic_min,
               vmax=spectrum_pic_max, extent=[0, result_array.shape[1], 20.5, 24.5])
    ax5.xaxis.set_ticklabels([])
    ax6 = fig.add_subplot(gs[2, 2])
    ax6.plot(np.mean(result_array[1986:3972], axis=0), linewidth='0.50', color='C4')
    ax6.set_xlim(xmin=0, xmax=result_array.shape[1])
    ax6.set_yticklabels([])
    ax6.set_yticks([])
    ax6.set_xlabel('Time points', fontsize=10, fontweight='bold')

    ax7 = fig.add_subplot(gs[0:2, 3])
    ax7.set_title('24.5 - 28.5 MHz', fontsize=8, fontweight='bold')
    ax7.imshow(np.flipud(result_array[3972:5958]), aspect='auto', cmap=colormap, vmin=spectrum_pic_min, vmax=spectrum_pic_max,
               extent=[0, result_array.shape[1], 24.5, 28.5])
    ax7.xaxis.set_ticklabels([])
    ax8 = fig.add_subplot(gs[2, 3])
    ax8.plot(np.mean(result_array[3972:5958], axis=0), linewidth='0.50', color='C4')
    ax8.set_xlim(xmin=0, xmax=result_array.shape[1])
    ax8.set_yticklabels([])
    ax8.set_yticks([])
    ax8.set_xlabel('Time points', fontsize=10, fontweight='bold')

    ax9 = fig.add_subplot(gs[0:2, 4])
    ax9.set_title('28.5 - 32.5 MHz', fontsize=8, fontweight='bold')
    ax9.imshow(np.flipud(result_array[5958:7944]), aspect='auto', cmap=colormap,
               vmin=spectrum_pic_min, vmax=spectrum_pic_max, extent=[0, result_array.shape[1], 28.5, 32.5])
    ax9.xaxis.set_ticklabels([])
    ax10 = fig.add_subplot(gs[2, 4])
    ax10.plot(np.mean(result_array[5958:7944], axis=0), linewidth='0.50', color='C4')
    ax10.set_xlim(xmin=0, xmax=result_array.shape[1])
    ax10.set_yticklabels([])
    ax10.set_yticks([])
    ax10.set_xlabel('Time points', fontsize=10, fontweight='bold')

    fig.subplots_adjust(hspace=0.00, wspace=0.25, top=0.93)
    fig.suptitle('Result pulse cut from ' + df_filepath + ' (' + df_description + '), DM: ' +
                 find_between(filename, 'DM_', '_') + r' $pc * cm^{-3}$', fontsize=10, fontweight='bold')  #
    fig.text(0.80, 0.05, 'Processed ' + current_date + ' at ' + current_time,
             fontsize=5, transform=plt.gcf().transFigure)
    fig.text(0.09, 0.05, 'Software version: '+software_version+', [email protected], IRA NASU',
             fontsize=5, transform=plt.gcf().transFigure)
    pylab.savefig(path + '/Extracted_s' + filename[1:-4] + '.png', bbox_inches='tight', dpi=customDPI)
    plt.close('all')

    # from PIL import Image
    # img = Image.open(path + '/Extracted_s' + filename[1:-4] + '.png')
    # img.show()

    # import sys
    # import subprocess
    #
    # def openImage(path):
    #     imageViewerFromCommandLine = {'linux': 'xdg-open',
    #                                   'win32': 'explorer',
    #                                   'darwin': 'open'}[sys.platform]
    #     subprocess.Popen([imageViewerFromCommandLine, path])
    #
    # openImage(path + '/Extracted_s' + filename[1:-4] + '.png')

    # # Array preprocessing
    #
    # array = np.flipud(result_array[1024:3972])
    # freq_aver_const = 2
    # time_aver_const = 1
    #
    # # Cutting the array ends which are not divisible to average constants
    # array = array[:(array.shape[0] // freq_aver_const) * freq_aver_const,
    #               :(array.shape[1] // time_aver_const) * time_aver_const]
    #
    # # Time averaging
    # if time_aver_const > 1:
    #     array = array.reshape(array.shape[0], -1, time_aver_const)
    #     array = np.mean(array, axis=2)
    #
    # # Frequency averaging
    # if freq_aver_const > 1:
    #     array = np.transpose(array)
    #     array = np.mean(array.reshape(array.shape[0], -1, freq_aver_const), axis=2)
    #     array = np.transpose(array)
    #
    # # Plot of the pulse dynamic spectrum
    # rc('font', size=8, weight='bold')
    # fig = plt.figure(figsize=(5.0, 8.0))
    # ax1 = fig.add_subplot()
    # # ax1.set_title('Full band, points: ', fontsize=8, fontweight='bold')
    # ax1.imshow(array, aspect='auto', cmap='Greys', vmin=-1.0, vmax=10.0,
    #            extent=[0, array.shape[1], 0, array.shape[0]])
    # ax1.set_ylabel('Frequency, MHz', fontsize=10, fontweight='bold')
    # # fig.subplots_adjust(hspace=0.00, wspace=0.25, top=0.93)
    # # fig.suptitle('Result pulse cut for further processing', fontsize=10, fontweight='bold')
    # # fig.text(0.80, 0.05, 'Processed ' + current_date + ' at ' + current_time,
    # #          fontsize=5, transform=plt.gcf().transFigure)
    # # fig.text(0.09, 0.05, 'Software version: '+software_version+', [email protected], IRA NASU',
    # #          fontsize=5, transform=plt.gcf().transFigure)
    # pylab.savefig(path + '/Extracted_s' + filename[1:-4] + '_dynamic_spectrum.png', bbox_inches='tight', dpi=customDPI)
    # plt.close('all')

    return start_point, end_point
Ejemplo n.º 6
0
    filename[i] = common_path + filename[i]

#*******************************************************************************
#                          R E A D I N G   D A T A                             *
#*******************************************************************************

# *** Reading files ***
[x_value, y_value] = read_date_time_and_one_value_txt(filename)

y_value = np.array(y_value)
a, b = y_value.shape
date_time = x_value[0][:]

text_freqs = []
for i in range(len(filename)):
    text_freqs.append(find_between(filename[i], 'at ', '.txt'))

list_text_freqs = ''
for i in range(len(filename)):
    list_text_freqs = list_text_freqs + find_between(filename[i], 'at ',
                                                     ' MHz')
    if i < len(filename) - 1: list_text_freqs = list_text_freqs + ', '

parent_filename = find_between(filename[0], common_path, ' Intensity')

#*******************************************************************************
#                                F I G U R E S                                 *
#*******************************************************************************

print('\n\n\n  *** Building images *** \n\n')