def culmination_time_utc(source_name, date, print_or_not):
    '''
    '''

    # Coordinates of UTR-2 radio telescope
    longitude = '36d56m27.560s'
    latitude = '+49d38m10.310s'
    elevation = 156 * u.m
    observatory = 'UTR-2, Ukraine'
    utr2_location = EarthLocation.from_geodetic(longitude, latitude, elevation)

    start_time = Time(date + ' 00:00:00')
    end_time = Time(date + ' 23:59:59')
    time_line = time_window = start_time + (
        end_time - start_time) * np.linspace(0, 1, 86400)
    frame = AltAz(obstime=time_window, location=utr2_location)

    if print_or_not == 1: print('\n  Observatory:', observatory, '\n')
    if print_or_not == 1:
        print(
            '  Coordinates: \n  * Longitude: ',
            str(utr2_location.lon).replace("d", "\u00b0 ").replace(
                "m", "\' ").replace("s", "\'\' "), ' \n  * Latitude:   ' +
            str(utr2_location.lat).replace("d", "\u00b0 ").replace(
                "m", "\' ").replace("s", "\'\' ") + '\n')
    if print_or_not == 1: print('  Source:', source_name, '\n')

    if source_name.lower() == 'sun':
        source_alt_az = get_sun(time_window).transform_to(frame)
    elif source_name.lower() == 'jupiter':
        with solar_system_ephemeris.set('builtin'):
            source_alt_az = get_body('jupiter', time_window,
                                     utr2_location).transform_to(frame)
    elif source_name.startswith('B') or source_name.startswith('J'):
        alt, az, DM = catalogue_pulsar(source_name)
        coordinates = SkyCoord(alt, az, frame='icrs')
        source_alt_az = coordinates.transform_to(frame)
    elif source_name.startswith('3C') or source_name.startswith('4C'):
        alt, az = catalogue_sources(source_name)
        coordinates = SkyCoord(alt, az, frame='icrs')
        source_alt_az = coordinates.transform_to(frame)
    else:
        print('Source not found!')

    xmax, ymax = find_max_altitude(source_alt_az)

    culm_time = str(time_line[int(xmax)])[0:19]

    if print_or_not == 1: print('  Culmination time:', culm_time, ' UTC \n')

    return culm_time
Exemple #2
0
def pulsar_incoherent_dedispersion(
        common_path, filename, pulsar_name, average_const, profile_pic_min,
        profile_pic_max, cleaning_Iana, cleaning, no_of_iterations,
        std_lines_clean, pic_in_line, std_pixels_clean, SpecFreqRange,
        freqStart, freqStop, save_profile_txt, save_compensated_data,
        customDPI, colormap):

    previousTime = time.time()
    currentTime = time.strftime("%H:%M:%S")
    currentDate = time.strftime("%d.%m.%Y")

    rc('font', size=6, weight='bold')
    data_filename = common_path + filename

    # *** Creating a folder where all pictures and results will be stored (if it doesn't exist) ***
    newpath = 'RESULTS_pulsar_single_pulses_' + pulsar_name + '_' + filename
    if not os.path.exists(newpath):
        os.makedirs(newpath)

    # Path to timeline file to be analyzed:
    time_line_file_name = common_path + filename[-31:-13] + '_Timeline.txt'

    if save_profile_txt > 0:
        # *** Creating a name for long timeline TXT file ***
        profile_file_name = newpath + '/' + filename + '_time_profile.txt'
        profile_txt_file = open(
            profile_file_name,
            'w')  # Open and close to delete the file with the same name
        profile_txt_file.close()

    # *** Opening DAT datafile ***
    file = open(data_filename, 'rb')

    # reading FHEADER
    df_filesize = os.stat(data_filename).st_size  # Size of file
    df_filename = file.read(32).decode('utf-8').rstrip(
        '\x00')  # Initial data file name
    file.close()

    receiver_type = df_filename[-4:]

    # Reading file header to obtain main parameters of the file
    if receiver_type == '.adr':
        [TimeRes, fmin, fmax, df, frequency_list,
         FFTsize] = FileHeaderReaderADR(data_filename, 0, 1)

    if receiver_type == '.jds':
        [
            df_filename, df_filesize, df_system_name, df_obs_place,
            df_description, CLCfrq, df_creation_timeUTC, sp_in_file,
            ReceiverMode, Mode, Navr, TimeRes, fmin, fmax, df, frequency_list,
            FFTsize, dataBlockSize
        ] = FileHeaderReaderJDS(data_filename, 0, 1)

    # Manually set frequencies for two channels mode
    if int(CLCfrq / 1000000) == 33:
        #FFTsize = 8192
        fmin = 16.5
        fmax = 33.0
        frequency_list = np.linspace(fmin, fmax, FFTsize)

    sp_in_file = int(
        ((df_filesize - 1024) / (len(frequency_list) * 8)
         ))  # the second dimension of the array: file size - 1024 bytes

    pulsar_ra, pulsar_dec, DM, p_bar = catalogue_pulsar(pulsar_name)

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

    # Time line file reading
    timeline, dt_timeline = time_line_file_reader(time_line_file_name)

    # Selecting the frequency range of data to be analyzed
    if SpecFreqRange == 1:
        A = []
        B = []
        for i in range(len(frequency_list)):
            A.append(abs(frequency_list[i] - freqStart))
            B.append(abs(frequency_list[i] - freqStop))
        ifmin = A.index(min(A))
        ifmax = B.index(min(B))
        shift_vector = DM_full_shift_calc(ifmax - ifmin, frequency_list[ifmin],
                                          frequency_list[ifmax],
                                          df / pow(10, 6), TimeRes, DM,
                                          receiver_type)
        print(' Number of frequency channels:  ', ifmax - ifmin)

    else:
        shift_vector = DM_full_shift_calc(
            len(frequency_list) - 4, fmin, fmax, df / pow(10, 6), TimeRes, DM,
            receiver_type)
        print(' Number of frequency channels:  ', len(frequency_list) - 4)
        ifmin = 0
        ifmax = int(len(frequency_list) - 4)

    if save_compensated_data > 0:
        with open(data_filename, 'rb') as file:
            file_header = file.read(1024)  # Data file header read

        # *** Creating a binary file with data for long data storage ***
        new_data_file_name = pulsar_name + '_DM_' + str(DM) + '_' + filename
        new_data_file = open(new_data_file_name, 'wb')
        new_data_file.write(file_header)
        new_data_file.seek(624)  # Lb place in header
        new_data_file.write(np.int32(ifmin).tobytes())
        new_data_file.seek(628)  # Hb place in header
        new_data_file.write(np.int32(ifmax).tobytes())
        new_data_file.seek(632)  # Wb place in header
        new_data_file.write(
            np.int32(ifmax -
                     ifmin).tobytes())  # bytes([np.int32(ifmax - ifmin)]))
        new_data_file.close()

        # *** Creating a name for long timeline TXT file ***
        new_TLfile_name = pulsar_name + '_DM_' + str(
            DM) + '_' + data_filename[:-13] + '_Timeline.txt'
        new_TLfile = open(
            new_TLfile_name,
            'w')  # Open and close to delete the file with the same name
        new_TLfile.close()

        del file_header

    max_shift = np.abs(shift_vector[0])

    if SpecFreqRange == 1:
        buffer_array = np.zeros((ifmax - ifmin, 2 * max_shift))
    else:
        buffer_array = np.zeros((len(frequency_list) - 4, 2 * max_shift))

    num_of_blocks = int(sp_in_file / (1 * max_shift))

    print(' Number of spectra in file:     ', sp_in_file, ' ')
    print(' Maximal shift is:              ', max_shift, ' pixels ')
    print(' Number of blocks in file:      ', num_of_blocks, ' ')
    print(' Dispersion measure:            ', DM, ' pc / cm3 \n')
    print(' Pulsar name:                   ', pulsar_name, '  \n')

    if receiver_type == '.jds':
        num_frequencies_initial = len(frequency_list) - 4

    frequency_list_initial = np.empty_like(frequency_list)
    frequency_list_initial[:] = frequency_list[:]

    dat_file = open(data_filename, 'rb')
    dat_file.seek(1024)  # Jumping to 1024 byte from file beginning

    for block in range(num_of_blocks):  # main loop by number of blocks in file

        print(
            '\n * Data block # ', block + 1, ' of ', num_of_blocks,
            '\n ******************************************************************'
        )

        # Time line arrangements:
        fig_time_scale = []
        fig_date_time_scale = []
        for i in range(block * max_shift, (block + 1) * max_shift
                       ):  # Shows the time of pulse end (at lowest frequency)
            fig_time_scale.append(timeline[i][11:23])
            fig_date_time_scale.append(timeline[i][:])
        print(' Time: ', fig_time_scale[0], ' - ', fig_time_scale[-1],
              ', number of points: ', len(fig_time_scale))

        # Data block reading
        if receiver_type == '.jds':
            data = np.fromfile(dat_file,
                               dtype=np.float64,
                               count=(num_frequencies_initial + 4) * 1 *
                               max_shift)  # 2
            data = np.reshape(data,
                              [(num_frequencies_initial + 4), 1 * max_shift],
                              order='F')  # 2
            data = data[:
                        num_frequencies_initial, :]  # To delete the last channels of DSP data where time is stored

        # Cutting the array in predefined frequency range
        if SpecFreqRange == 1:
            data, frequency_list, fi_start, fi_stop = specify_frequency_range(
                data, frequency_list_initial, freqStart, freqStop)
            num_frequencies = len(frequency_list)
        else:
            num_frequencies = num_frequencies_initial

        # Normalization of data
        Normalization_lin(data, num_frequencies, 1 * max_shift)

        nowTime = time.time()
        print('\n  *** Preparation of data took:              ',
              round((nowTime - previousTime), 2), 'seconds ')
        previousTime = nowTime

        if cleaning_Iana > 0:
            data = survey_cleaning(
                data)  # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        if cleaning > 0:

            # Cleaning vertical and horizontal lines of RFI
            data, mask, cleaned_pixels_num = clean_lines_of_pixels(
                data, no_of_iterations, std_lines_clean, pic_in_line)

            plt.figure(1, figsize=(10.0, 6.0))
            plt.subplots_adjust(left=None,
                                bottom=0,
                                right=None,
                                top=0.86,
                                wspace=None,
                                hspace=None)
            ImA = plt.imshow(mask, aspect='auto', vmin=0, vmax=1, cmap='Greys')
            plt.title('Full log initial data',
                      fontsize=10,
                      fontweight='bold',
                      style='italic',
                      y=1.025)
            plt.ylabel('One dimension', fontsize=10, fontweight='bold')
            plt.xlabel('Second dimensions', fontsize=10, fontweight='bold')
            plt.colorbar()
            plt.yticks(fontsize=8, fontweight='bold')
            plt.xticks(fontsize=8, fontweight='bold')
            pylab.savefig(newpath + '/00_10' + ' fig. ' + str(block + 1) +
                          ' - Result mask after lines cleaning.png',
                          bbox_inches='tight',
                          dpi=300)
            plt.close('all')

            # Cleaning remaining 1 pixel splashes of RFI
            data, mask, cleaned_pixels_num = array_clean_by_STD_value(
                data, std_pixels_clean)

            plt.figure(1, figsize=(10.0, 6.0))
            plt.subplots_adjust(left=None,
                                bottom=0,
                                right=None,
                                top=0.86,
                                wspace=None,
                                hspace=None)
            ImA = plt.imshow(mask, aspect='auto', vmin=0, vmax=1, cmap='Greys')
            plt.title('Full log initial data',
                      fontsize=10,
                      fontweight='bold',
                      style='italic',
                      y=1.025)
            plt.ylabel('One dimension', fontsize=10, fontweight='bold')
            plt.xlabel('Second dimensions', fontsize=10, fontweight='bold')
            plt.colorbar()
            plt.yticks(fontsize=8, fontweight='bold')
            plt.xticks(fontsize=8, fontweight='bold')
            pylab.savefig(newpath + '/00_11' + ' fig. ' + str(block + 1) +
                          ' - Mask after fine STD cleaning.png',
                          bbox_inches='tight',
                          dpi=300)
            plt.close('all')

            nowTime = time.time()
            print('\n  *** Normalization and cleaning took:       ',
                  round((nowTime - previousTime), 2), 'seconds ')
            previousTime = nowTime
        '''
        # Logging the data
        with np.errstate(invalid='ignore'):
            data[:,:] = 10 * np.log10(data[:,:])
        data[np.isnan(data)] = 0

        # Normalizing log data
        data = data - np.mean(data)
        '''

        # Dispersion delay removing
        data_space = np.zeros((num_frequencies, 2 * max_shift))
        data_space[:, max_shift:] = data[:, :]
        temp_array = pulsar_DM_compensation_with_indices_changes(
            data_space, shift_vector)
        del data, data_space

        nowTime = time.time()
        # print('\n  *** Dispersion compensation took:          ', round((nowTime - previousTime), 2), 'seconds ')
        print('\n  *** Dispersion delay removing took:        ',
              round((nowTime - previousTime), 2), 'seconds ')
        previousTime = nowTime

        # Adding the next data block
        buffer_array += temp_array

        # Making and filling the array with fully ready data for plotting and saving to a file
        array_compensated_DM = buffer_array[:, 0:max_shift]

        if block > 0:
            # Saving data with compensated DM to DAT file
            if save_compensated_data > 0 and block > 0:
                temp = array_compensated_DM.transpose().copy(order='C')
                new_data_file = open(new_data_file_name, 'ab')
                new_data_file.write(temp)
                new_data_file.close()

                # Saving time data to ling timeline file
                with open(new_TLfile_name, 'a') as new_TLfile:
                    for i in range(max_shift):
                        new_TLfile.write((fig_date_time_scale[i][:]))  # str

            # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            # Logging the data
            with np.errstate(divide='ignore'):
                array_compensated_DM[:, :] = 10 * np.log10(
                    array_compensated_DM[:, :])
            array_compensated_DM[array_compensated_DM == -np.inf] = 0

            # Normalizing log data
            array_compensated_DM = array_compensated_DM - np.mean(
                array_compensated_DM)
            # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

            # Preparing single averaged data profile for figure
            profile = array_compensated_DM.mean(axis=0)[:]
            profile = profile - np.mean(profile)

            # Save full profile to TXT file
            if save_profile_txt > 0:
                profile_txt_file = open(profile_file_name, 'a')
                for i in range(len(profile)):
                    profile_txt_file.write(str(profile[i]) + ' \n')
                profile_txt_file.close()

            # Averaging of the array with pulses for figure
            averaged_array = average_some_lines_of_array(
                array_compensated_DM, int(num_frequencies / average_const))
            freq_resolution = (df *
                               int(num_frequencies / average_const)) / 1000.
            max_time_shift = max_shift * TimeRes

            # NEW start
            averaged_array = averaged_array - np.mean(averaged_array)
            # NEW stop

            plot_ready_data(profile, averaged_array, frequency_list,
                            num_frequencies, fig_time_scale, newpath, filename,
                            pulsar_name, DM, freq_resolution, TimeRes,
                            max_time_shift, block, num_of_blocks - 1, block,
                            profile_pic_min, profile_pic_max, df_description,
                            colormap, customDPI, currentDate, currentTime,
                            Software_version)

        # Rolling temp_array to put current data first
        buffer_array = np.roll(buffer_array, -max_shift)
        buffer_array[:, max_shift:] = 0

    dat_file.close()

    # Fourier analysis of the obtained time profile of pulses
    if save_profile_txt > 0:
        print('\n\n  *** Making Fourier transform of the time profile...')
        pulsar_pulses_time_profile_FFT(newpath + '/',
                                       filename + '_time_profile.txt',
                                       pulsar_name, TimeRes, profile_pic_min,
                                       profile_pic_max, customDPI, colormap)

    return new_data_file_name
Exemple #3
0
#*******************************************************************************


print ('\n\n\n\n\n\n\n\n   **************************************************************')
print ('   *    ', Software_name,' v.',Software_version,'     *      (c) YeS 2019')
print ('   ************************************************************** \n\n\n')

startTime = time.time()
previousTime = startTime
currentTime = time.strftime("%H:%M:%S")
currentDate = time.strftime("%d.%m.%Y")
print ('  Today is ', currentDate, ' time is ', currentTime, ' \n')

filepath = folder_path + filename

pulsar_ra, pulsar_dec, DM = catalogue_pulsar(pulsar_name)


print ('\n  * Parameters of analysis: \n')
print (' Path to folder:  ', folder_path, '\n')
print (' File name:  ', filename, '\n')
print (' Number of DM analysis steps =        ', no_of_DM_steps)
print (' Step of DM analysis =                ', DM_var_step)
print (' Save intermediate data?              ', save_intermediate_data)
print (' Number of channels to average =      ', AverageChannelNumber)
print (' Number of time points to average =   ', AverageTPointsNumber)
print (' Make cuts of frequency bands?        ', frequency_band_cut)
print (' Specify particular frequency range?  ', specify_freq_range)
print (' Frequencies to cut the range         ', frequency_cuts)
print (' Color map =                          ', colormap)
print (' DPI of plots =                       ', customDPI)
Exemple #4
0
def pulsar_pulses_time_profile_FFT(common_path, filename, pulsar_name,
                                   time_resolution, profile_pic_min,
                                   profile_pic_max, customDPI, colormap):

    currentTime = time.strftime("%H:%M:%S")
    currentDate = time.strftime("%d.%m.%Y")

    data_filename = common_path + filename

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

    # Opening TXT file with pulsar observations long time profile
    profile_txt_file = open(data_filename, 'r')
    profile_data = []
    for line in profile_txt_file:
        profile_data.append(np.float(line))
    profile_txt_file.close()

    print('\n  Number of samples in text file:  ', len(profile_data), ' \n')

    pulsar_ra, pulsar_dec, DM, pulsar_period = catalogue_pulsar(pulsar_name)

    pulsar_frequency = 1 / pulsar_period  # frequency of pulses, Hz
    frequency_resolution = 1 / (time_resolution * len(profile_data)
                                )  # frequency resolution, Hz

    profile_spectrum = np.power(np.real(np.fft.fft(profile_data[:])),
                                2)  # calculation of the spectrum
    profile_spectrum = profile_spectrum[0:int(
        len(profile_spectrum) / 2)]  # delete second part of the spectrum

    #profile_spectrum[0:100] = 0.0

    frequency_axis = [
        frequency_resolution * i for i in range(len(profile_spectrum))
    ]

    # Making result picture
    fig = plt.figure(figsize=(9.2, 4.5))
    rc('font', size=5, weight='bold')
    ax1 = fig.add_subplot(211)
    ax1.plot(profile_data,
             color=u'#1f77b4',
             linestyle='-',
             alpha=1.0,
             linewidth='0.60',
             label='Pulses time profile')
    ax1.legend(loc='upper right', fontsize=5)
    ax1.grid(b=True,
             which='both',
             color='silver',
             linewidth='0.50',
             linestyle='-')
    ax1.axis([0, len(profile_data), profile_pic_min, profile_pic_max])
    ax1.set_ylabel('Amplitude, AU', fontsize=6, fontweight='bold')
    ax1.set_title('File: ' + filename + '  Pulsar period: ' +
                  str(np.round(pulsar_period, 3)) + ' s.',
                  fontsize=5,
                  fontweight='bold')
    ax1.tick_params(axis='x',
                    which='both',
                    bottom=False,
                    top=False,
                    labelbottom=False)
    ax2 = fig.add_subplot(212)
    ax2.axvline(x=pulsar_frequency,
                color='C1',
                linestyle='-',
                linewidth=2.0,
                alpha=0.5,
                label='Frequency of pulses')
    ax2.plot(frequency_axis,
             profile_spectrum,
             color=u'#1f77b4',
             linestyle='-',
             alpha=1.0,
             linewidth='0.60',
             label='Time series spectrum')
    #ax2.axis([frequency_axis[0], frequency_axis[-1], -np.max(profile_spectrum)*0.05, np.max(profile_spectrum)*1.05])  #
    ax2.axis([frequency_axis[0], frequency_axis[-1], -10, spectrum_max])
    ax2.legend(loc='upper right', fontsize=5)
    ax2.set_xlabel('Frequency, Hz', fontsize=6, fontweight='bold')
    ax2.set_ylabel('Amplitude, AU', fontsize=6, fontweight='bold')
    fig.subplots_adjust(hspace=0.05, top=0.91)
    fig.suptitle('Single pulses of ' + pulsar_name + ' in time and frequency',
                 fontsize=7,
                 fontweight='bold')
    fig.text(0.80,
             0.04,
             'Processed ' + currentDate + ' at ' + currentTime,
             fontsize=3,
             transform=plt.gcf().transFigure)
    fig.text(0.09,
             0.04,
             'Software version: ' + Software_version +
             ', [email protected], IRA NASU',
             fontsize=3,
             transform=plt.gcf().transFigure)
    pylab.savefig(common_path + '/' + filename[0:-4] + ' and its spectrum.png',
                  bbox_inches='tight',
                  dpi=customDPI)
    plt.close('all')

    return
# *******************************************************************************
#                            M A I N    P R O G R A M                           *
# *******************************************************************************


if __name__ == '__main__':

    print('\n\n\n\n\n\n\n\n   ********************************************************************')
    print('   * ', Software_name, ' v.', Software_version, ' *      (c) YeS 2020')
    print('   ******************************************************************** \n\n')
    
    start_time = time.time()
    print('  Today is ', time.strftime("%d.%m.%Y"), ' time is ', time.strftime("%H:%M:%S"), '\n\n')

    # Take pulsar parameters from catalogue
    pulsar_ra, pulsar_dec, pulsar_dm, p_bar = catalogue_pulsar(pulsar_name)

    # Calculation of the maximal time shift for dispersion delay removing
    shift_vector = DM_full_shift_calc(8192, 16.5, 33.0, 2014 / pow(10, 6), 0.000496, pulsar_dm, 'jds')
    max_shift = np.abs(shift_vector[0])
    print('  * Pulsar ', pulsar_name)
    print('                               Period: ', p_bar, 's.')
    print('                   Dispersion measure:  {} pc・cm\u00b3'.format(pulsar_dm))
    print('    Maximal shift of dynamic spectrum: ', max_shift, ' points')
    print('                                  or : ', np.round(max_shift * 16384/33000000, 1), ' seconds')  # nfft/f_cl
    print('                                  or :  ~', int(np.ceil((max_shift * 16384/33000000) / 16)),
          ' files in 2 ch 33 MHz mode\n\n')

    # Reading initial jds file list to save the list of files in the result folder
    file_list = find_files_only_in_current_folder(source_directory, '.jds', 0)
def sources_positions_on_the_sky(start_time, end_time, pulsars_list, sources_list):
    '''
    '''

    currentDate = time.strftime("%Y-%m-%d")
    currentTime = time.strftime("%H:%M:%S")
    print ('\n  Today is ', currentDate, ', local time is ', currentTime, '\n')

    newpath = "Sources_positions"
    if not os.path.exists(newpath):
        os.makedirs(newpath)

    n_points = 1441 # Number of points to calculate within the time window
    center = int(n_points/2) + 1
    time_window = start_time + (end_time - start_time) * np.linspace(0, 1, n_points)

    # Coordinates of UTR-2 radio telescope
    longitude = '36d56m27.560s'
    latitude = '+49d38m10.310s'
    elevation = 156 * u.m
    observatory = 'UTR-2, Ukraine'
    utr2_location = EarthLocation.from_geodetic(longitude, latitude, elevation)

    print('  Observatory:', observatory, '\n')
    print('  Coordinates: \n  * Longitude: ', str(utr2_location.lon).replace("d", "\u00b0 " ).replace("m", "\' " ).replace("s", "\'\' " ),' \n  * Latitude:   '+ str(utr2_location.lat).replace("d", "\u00b0 " ).replace("m", "\' " ).replace("s", "\'\' " ) + '\n')
    print('  Plot time window: \n  * From: ', str(start_time)[0:19],' UTC \n  * Till:  '+ str(end_time)[0:19] + '  UTC \n')

    frame = AltAz(obstime = time_window, location = utr2_location)

    # Coordiantes of Sun
    sun_alt_az = get_sun(time_window).transform_to(frame)

    # Coordinates of Jupiter
    with solar_system_ephemeris.set('builtin'):
        jupiter_alt_az = get_body('jupiter', time_window, utr2_location).transform_to(frame)

    # Coordinates of pulsars
    pulsars_alt_az = []
    for i in range (len(pulsars_list)):
        alt, az, DM = catalogue_pulsar(pulsars_list[i])
        coordinates = SkyCoord(alt, az, frame='icrs')
        pulsars_alt_az.append(coordinates.transform_to(frame))

    # Coordinates of sources
    sources_alt_az = []
    for i in range (len(sources_list)):
        alt, az = catalogue_sources(sources_list[i])
        coordinates = SkyCoord(alt, az, frame='icrs')
        sources_alt_az.append(coordinates.transform_to(frame))


    ticks_list = [0, 120, 240, 360, 480, 600, 720, 840, 960, 1080, 1200, 1320, 1440]
    time_ticks_list = []
    for i in range(len(ticks_list)):
        time_ticks_list.append(str(time_window[ticks_list[i]])[10:16])
    color = ['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9']


    # *** Sources elevation above horizon with time plot figure ***
    rc('font', size = 6, weight='bold')
    fig = plt.figure(figsize = (9, 6))
    ax1 = fig.add_subplot(111)

    xmax, ymax = find_max_altitude(sun_alt_az) # Sun
    plt.axvline(x=xmax, linewidth = '0.8' , color = 'C1', alpha=0.5)
    xmax, ymax = find_max_altitude(jupiter_alt_az) # Sun
    plt.axvline(x=xmax, linewidth = '0.8' , color = 'C4', alpha=0.5)
    for i in range (len(pulsars_list)):
        xmax, ymax = find_max_altitude(pulsars_alt_az[i]) # Pulsars
        plt.axvline(x=xmax, linewidth = '0.8' , color = color[i], alpha=0.5)
    for i in range (len(sources_list)):
        xmax, ymax = find_max_altitude(sources_alt_az[i]) # Sources
        plt.axvline(x=xmax, linewidth = '0.8' , color = color[i], alpha=0.5)

    # Sun
    ax1.plot(sun_alt_az.alt, label='Sun', linewidth = '2.5', color = 'C1')
    xmax, ymax = find_max_altitude(sun_alt_az)
    ax1.annotate('Sun', xy=(xmax+1, ymax+1), color = 'C1', fontsize = 6, rotation=45)  #xytext=(xmax, ymax)
    ax1.plot(xmax, ymax, marker='o', markersize=4, color="red")
    del xmax, ymax

    # Jupiter
    ax1.plot(jupiter_alt_az.alt, label='Jupiter', linewidth = '2.5', color = 'C4')
    xmax, ymax = find_max_altitude(jupiter_alt_az)
    ax1.annotate('Jupiter', xy=(xmax+1, ymax+1), color = 'C4', fontsize = 6, rotation=45)  #xytext=(xmax, ymax)
    ax1.plot(xmax, ymax, marker='o', markersize=4, color="red")
    del xmax, ymax

    # Pulsars
    for i in range (len(pulsars_list)):
        ax1.plot(pulsars_alt_az[i].alt, label=pulsars_list[i], color = color[i], linestyle = '--', linewidth = '0.8')
        xmax, ymax = find_max_altitude(pulsars_alt_az[i])
        ax1.annotate(pulsars_list[i], xy=(xmax+1, ymax+1), color = color[i], fontsize = 6, rotation=45) #ha='center'
        ax1.plot(xmax, ymax, marker='o', markersize = 3, color = color[i])
        del xmax, ymax

    # Sources
    for i in range (len(sources_list)):
        ax1.plot(sources_alt_az[i].alt, label=sources_list[i], color = color[i], linestyle = '-', linewidth = '0.7')
        xmax, ymax = find_max_altitude(sources_alt_az[i])
        ax1.annotate(sources_list[i], xy=(xmax+1, ymax+1), color = color[i], fontsize = 6, rotation=45)
        ax1.plot(xmax, ymax, marker='o', markersize = 3, color = color[i])
        del xmax, ymax

    plt.fill_between(np.linspace(0, n_points, n_points), 0, 10, color='0.7')
    plt.fill_between(np.linspace(0, n_points, n_points), 10, 20, color='0.8')
    plt.fill_between(np.linspace(0, n_points, n_points), 20, 30, color='0.9')
    plt.fill_between(np.linspace(0, n_points, n_points), -10, 0, color='0.4')
    ax1.set_xlim([0, n_points-1])
    ax1.set_ylim([-10, 95])
    ax1.xaxis.set_major_locator(mtick.LinearLocator(15))
    ax1.xaxis.set_minor_locator(mtick.LinearLocator(25))
    plt.xticks(ticks_list, time_ticks_list)
    plt.yticks([-10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
    ax1.grid(b = True, which = 'both', color = 'silver', linestyle = '-', linewidth = '0.5')
    fig.suptitle('Elevation above horizon of sources for ' + observatory, fontsize = 8, fontweight='bold')
    ax1.set_title('Time period: '+ str(start_time)[0:19] + ' - ' + str(end_time)[0:19], fontsize = 7)
    fig.subplots_adjust(top=0.9)
    #ax1.legend(loc='center right', fontsize = 8, bbox_to_anchor=(1.17, 0.5))
    plt.ylabel('Altitude above horizon, deg', fontsize = 8, fontweight='bold')
    plt.xlabel('UTC time', fontsize = 8, fontweight='bold')
    ax2 = ax1.twiny()
    ax2.set_xlim(ax1.get_xlim())
    ax2.set_xticks(ax1.get_xticks())
    ax2.set_xticklabels(ax1.get_xticklabels())
    fig.text(0.79, 0.04, 'Processed '+currentDate+ ' at '+currentTime, fontsize=4, transform=plt.gcf().transFigure)
    fig.text(0.11, 0.04, 'Software version: '+Software_version+', [email protected], IRA NASU', fontsize=4, transform=plt.gcf().transFigure)
    pylab.savefig(newpath + '/'+ str(start_time)[0:10] + ' sources elevation.png', bbox_inches='tight', dpi = 300)
    #plt.show()


    # Preparing data for correct sky plot (Altitutde / Azimuth)
    # Sun
    r_sun = np.zeros(len(sun_alt_az))
    r_sun[:] = sun_alt_az[:].alt.degree
    r_sun[r_sun < 0] = 0
    r_sun[:] = (90 - r_sun[:])
    r_sun[r_sun > 89] = np.inf

    # Jupiter
    r_jupiter = np.zeros(len(jupiter_alt_az))
    r_jupiter[:] = jupiter_alt_az[:].alt.degree
    r_jupiter[r_jupiter < 0] = 0
    r_jupiter[:] = (90 - r_jupiter[:])
    r_jupiter[r_jupiter > 89] = np.inf


    r_sources = np.zeros((len(sources_alt_az), n_points), dtype=np.float)
    for i in range (len(sources_list)):
        r_sources[i][:] = sources_alt_az[i][:].alt.degree
    r_sources[r_sources < 0] = 0
    for i in range (len(sources_list)):
        r_sources[i][:] = (90 - r_sources[i][:])

    r_pulsars = np.zeros((len(pulsars_alt_az), n_points), dtype=np.float)
    for i in range (len(pulsars_list)):
        r_pulsars[i][:] = pulsars_alt_az[i][:].alt.degree
    r_pulsars[r_pulsars < 0] = 0
    for i in range (len(pulsars_list)):
        r_pulsars[i][:] = (90 - r_pulsars[i][:])


    # *** Sky altitude/azimuth polar plot figure ***

    rc('font', size = 6, weight='bold')
    fig = plt.figure(figsize = (9, 6))

    ax1 = fig.add_subplot(111, projection='polar')
    ax1.set_theta_zero_location("N")
    ax1.plot(sun_alt_az.az.rad, r_sun, label = 'Sun', color = 'C1', linewidth = 5, alpha = 0.5) #
    ax1.plot(sun_alt_az[center].az.rad,  r_sun[center], marker='o', markersize = 5, color ='C1')
    ax1.plot(jupiter_alt_az.az.rad, r_jupiter, label = 'Jupiter', color = 'C4', linewidth = 5, alpha = 0.5) #
    ax1.plot(jupiter_alt_az[center].az.rad,  r_jupiter[center], marker='o', markersize = 5, color = 'C4')

    for i in range (len(sources_list)):
        ax1.plot(sources_alt_az[i].az.rad, r_sources[i], label = sources_list[i], linewidth = 1, color = color[i]) #
        ax1.plot(sources_alt_az[i][center].az.rad,  r_sources[i][center], marker='o', markersize = 3, color = color[i])
    for i in range (len(pulsars_list)):
        ax1.plot(pulsars_alt_az[i].az.rad, r_pulsars[i], label = pulsars_list[i], linestyle = '--', linewidth = 0.5, color = color[i]) #
        ax1.plot(pulsars_alt_az[i][center].az.rad,  r_pulsars[i][center], marker='o', markersize = 3, color = color[i])

    ax1.set_ylim(0, 90)
    ax1.set_yticks(np.arange(0, 90, 10))
    ax1.set_rlabel_position(180)
    plt.legend(loc='upper right', bbox_to_anchor=(1.10, 0.22))

    fig.text(0.497, 0.890, 'North', fontsize=6, color = 'b', transform=plt.gcf().transFigure)
    fig.text(0.225, 0.470, 'East', fontsize=6, transform=plt.gcf().transFigure)
    fig.text(0.775, 0.470, 'West', fontsize=6, transform=plt.gcf().transFigure)
    fig.text(0.496, 0.090, 'South', fontsize=6, color = 'r', transform=plt.gcf().transFigure)
    fig.text(0.210, 0.900, 'Position of sources in the sky', fontsize=8, transform=plt.gcf().transFigure)
    fig.text(0.210, 0.880, 'Observatory: ' + observatory, fontsize=6, transform=plt.gcf().transFigure)
    fig.text(0.210, 0.860, 'Lon:', fontsize=5, transform=plt.gcf().transFigure)
    fig.text(0.210, 0.845, 'Lat:', fontsize=5, transform=plt.gcf().transFigure)
    fig.text(0.230, 0.860, str(utr2_location.lon).replace("d", "\u00b0 " ).replace("m", "\' " ).replace("s", "\'\' " ), fontsize=5, transform=plt.gcf().transFigure)
    fig.text(0.230, 0.845, str(utr2_location.lat).replace("d", "\u00b0 " ).replace("m", "\' " ).replace("s", "\'\' " ), fontsize=5, transform=plt.gcf().transFigure)
    fig.text(0.640, 0.890, 'From:', fontsize=5, transform=plt.gcf().transFigure)
    fig.text(0.640, 0.875, 'Till:', fontsize=5, transform=plt.gcf().transFigure)
    fig.text(0.682, 0.890, str(start_time)[0:19] + ' UTC', fontsize=5, transform=plt.gcf().transFigure)
    fig.text(0.682, 0.875, str(end_time)[0:19] + ' UTC', fontsize=5, transform=plt.gcf().transFigure)
    fig.text(0.640, 0.860, 'Markers:', fontsize=5, transform=plt.gcf().transFigure)
    fig.text(0.682, 0.860, str(time_window[center])[0:19] + ' UTC', fontsize=5, transform=plt.gcf().transFigure)
    fig.text(0.640, 0.845, '(for middle of predefined time window)', fontsize=5, transform=plt.gcf().transFigure)

    fig.text(0.210, 0.080, 'Processed '+currentDate+ ' at '+currentTime, fontsize=4, transform=plt.gcf().transFigure)
    fig.text(0.210, 0.070, 'Software version: '+Software_version+', [email protected], IRA NASU', fontsize=4, transform=plt.gcf().transFigure)

    pylab.savefig(newpath + '/'+ str(start_time)[0:10] + ' sky map.png', bbox_inches='tight', dpi = 300)
    #plt.show()


    # Plot of culmination times
    objects = ['', 'Sun', 'Jupiter']
    for i in range (len(sources_list)):
        objects.append(sources_list[i])
    for i in range (len(pulsars_list)):
        objects.append(pulsars_list[i])
    objects.append('')


    rc('font', size = 6, weight='bold')
    fig, ax1 = plt.subplots(figsize=(9, 6))


    # Sun
    xmax, ymax = find_max_altitude(sun_alt_az) # Sun
    x_rise, x_set = find_rize_and_set_points(sun_alt_az)
    plt.axvline(x=xmax, linewidth = '0.8' , color = 'C1', alpha=0.7)
    if x_rise < xmax and xmax < x_set:
        plt.barh(1, x_set-x_rise, left = x_rise, height = 0.5, color = 'C1', alpha = 0.4)
    plt.barh(1, 241, left = xmax-120, height = 0.5, color = 'C1', alpha = 0.7)
    plt.barh(1, 121, left = xmax-60,  height = 0.5, color = 'C1')
    plt.barh(1, 3,   left = xmax-1,   height = 0.5, color = 'r')
    plt.annotate(str(time_window[int(xmax)])[11:19], xy=(xmax, 1 + 0.3), fontsize = 6, ha='center')
    if x_rise > 0: plt.annotate(str(time_window[x_rise])[11:19], xy=(x_rise, 1 + 0.3), fontsize = 6, ha='left')
    if x_set < n_points-1: plt.annotate(str(time_window[x_set])[11:19], xy=(x_set, 1 + 0.3), fontsize = 6, ha='right')

    # Jupiter`
    xmax, ymax = find_max_altitude(jupiter_alt_az) # Jupiter`
    x_rise, x_set = find_rize_and_set_points(jupiter_alt_az)
    if x_rise < xmax and xmax < x_set:
        plt.barh(2, x_set-x_rise, left = x_rise, height = 0.5, color = 'C4', alpha = 0.4)
    if x_rise > xmax and xmax > x_set:
        plt.barh(2, x_set, left = 0, height = 0.5, color = 'C4', alpha = 0.4)
    plt.axvline(x=xmax, linewidth = '0.8' , color = 'C4', alpha=0.7)
    plt.barh(2, 241, left = xmax-120, height = 0.5, color = 'C4', alpha = 0.7)
    plt.barh(2, 121, left = xmax-60,  height = 0.5, color = 'C4')
    plt.barh(2, 3,   left = xmax-1,   height = 0.5, color = 'r')
    plt.annotate(str(time_window[int(xmax)])[11:19], xy=(xmax, 2 + 0.3), fontsize = 6, ha='center')
    if x_rise > 0: plt.annotate(str(time_window[x_rise])[11:19], xy=(x_rise, 2 + 0.3), fontsize = 6, ha='left')
    if x_set < n_points-1: plt.annotate(str(time_window[x_set])[11:19], xy=(x_set, 2 + 0.3), fontsize = 6, ha='right')



    '''
    # Jupiter`
    xmax, ymax = find_max_altitude(jupiter_alt_az) # Jupiter`
    x_rise, x_set = find_rize_and_set_points(jupiter_alt_az, 0)

    print(x_rise, xmax, x_set)

    k_rize = 0
    k_sets = 0
    if len(x_rise) == 0 and len(x_set) == 0 and ymax > 0:
        plt.barh(2, n_points, left = 0, height = 0.5, color = 'C4', alpha = 0.4)
    if len(x_rise) > len(x_set):
        k_rize = 1
        plt.barh(2, n_points-x_rise[-1], left = x_rise[-1], height = 0.5, color = 'C4', alpha = 0.4)
    if len(x_rise) < len(x_set):
        k_sets = 1
        plt.barh(2, x_set[-1], left = 0, height = 0.5, color = 'C4', alpha = 0.4)

    num = np.min([len(x_rise) - k_rize, len(x_set) - k_sets])
    for i in range (num):
        plt.barh(2, x_set[i]-x_rise[i], left = x_rise[i], height = 0.5, color = 'C4', alpha = 0.4)

    plt.axvline(x=xmax, linewidth = '0.8' , color = 'C4', alpha=0.7)
    plt.barh(2, 241, left = xmax-120, height = 0.5, color = 'C4', alpha = 0.7)
    plt.barh(2, 121, left = xmax-60,  height = 0.5, color = 'C4')
    plt.barh(2, 3,   left = xmax-1,   height = 0.5, color = 'r')
    plt.annotate(str(time_window[int(xmax)])[11:19], xy=(xmax, 2 + 0.3), fontsize = 6, ha='center')
    if len(x_rise) > 0:
        if x_rise[0] > 0: plt.annotate(str(time_window[x_rise[0]])[11:19], xy=(x_rise[0], 2 + 0.3), fontsize = 6, ha='left')
    if len(x_set) > 0:
        if x_set[0] < n_points-1: plt.annotate(str(time_window[x_set[0]])[11:19], xy=(x_set[0], 2 + 0.3), fontsize = 6, ha='right')
    '''


    for i in range (len(sources_list)):
        n = i+3
        xmax, ymax = find_max_altitude(sources_alt_az[i]) # Sources
        x_rise, x_set = find_rize_and_set_points(sources_alt_az[i])
        if x_rise < xmax and xmax < x_set:
            plt.barh(n, x_set-x_rise, left = x_rise, height = 0.5, color = color[i], alpha = 0.4)
        plt.axvline(x=xmax, linewidth = '0.8' , color = color[i], alpha=0.7)
        plt.barh(n, 241, left = xmax-120, height = 0.5, color = color[i], alpha = 0.7)
        plt.barh(n, 121, left = xmax-60,  height = 0.5, color = color[i])
        plt.barh(n, 3,   left = xmax-1,   height = 0.5, color = 'r')
        plt.annotate(str(time_window[int(xmax)])[11:19], xy=(xmax, n + 0.3), fontsize = 6, ha='center')
        if x_rise > 0: plt.annotate(str(time_window[x_rise])[11:19], xy=(x_rise, n + 0.3), fontsize = 6, ha='left')
        if x_set < n_points-1: plt.annotate(str(time_window[x_set])[11:19], xy=(x_set, n + 0.3), fontsize = 6, ha='right')


    for i in range (len(pulsars_list)):
        n = i+3 + len(sources_list)
        xmax, ymax = find_max_altitude(pulsars_alt_az[i]) # Pulsars
        x_rise, x_set = find_rize_and_set_points(pulsars_alt_az[i])
        if x_rise < xmax and xmax < x_set:
            plt.barh(n, x_set-x_rise, left = x_rise, height = 0.5, color = color[i], alpha = 0.4)
        plt.axvline(x=xmax, linewidth = '0.8' , color = color[i], alpha=0.7)
        plt.barh(n, 241, left = xmax-120, height = 0.5, color = color[i], alpha = 0.7)
        plt.barh(n, 121, left = xmax-60,  height = 0.5, color = color[i])
        plt.barh(n, 3,   left = xmax-1,   height = 0.5, color = 'r')
        plt.annotate(str(time_window[int(xmax)])[11:19], xy=(xmax, n + 0.3), fontsize = 6, ha='center')
        if x_rise > 0: plt.annotate(str(time_window[x_rise])[11:19], xy=(x_rise, n + 0.3), fontsize = 6, ha='left')
        if x_set < n_points-1: plt.annotate(str(time_window[x_set])[11:19], xy=(x_set, n + 0.3), fontsize = 6, ha='right')


    plt.xlabel('UTC time', fontsize = 8, fontweight='bold')
    ax1.set_xlim([0, n_points-1])
    ax1.yaxis.set_major_locator(mtick.LinearLocator(len(objects)))
    ax1.set_ylim([0, len(objects) - 1])
    ax1.set_yticklabels(objects[:])

    ax1.xaxis.set_major_locator(mtick.LinearLocator(15))
    ax1.xaxis.set_minor_locator(mtick.LinearLocator(25))
    plt.xticks(ticks_list, time_ticks_list)
    ax2 = ax1.twiny()
    ax2.set_xlim(ax1.get_xlim())
    ax2.set_xticks(ax1.get_xticks())
    ax2.set_xticklabels(ax1.get_xticklabels())
    fig.suptitle('Culmination and observation time of sources for ' + observatory, fontsize = 8, fontweight='bold')
    ax1.set_title('Time period: '+ str(start_time)[0:19] + ' - ' + str(end_time)[0:19], fontsize = 7)
    fig.subplots_adjust(top=0.9)
    fig.text(0.79, 0.04, 'Processed '+currentDate+ ' at '+currentTime, fontsize=4, transform=plt.gcf().transFigure)
    fig.text(0.11, 0.04, 'Software version: '+Software_version+', [email protected], IRA NASU', fontsize=4, transform=plt.gcf().transFigure)

    pylab.savefig(newpath + '/'+ str(start_time)[0:10] +' culmination times.png', bbox_inches='tight', dpi = 300)

    return
def pulsar_period_DM_compensated_pics(common_path, filename, pulsar_name,
                                      normalize_response, profile_pic_min,
                                      profile_pic_max, spectrum_pic_min,
                                      spectrum_pic_max, periods_per_fig,
                                      customDPI, colormap, save_strongest,
                                      threshold):

    current_time = time.strftime("%H:%M:%S")
    current_date = time.strftime("%d.%m.%Y")

    # Creating a folder where all pictures and results will be stored (if it doesn't exist)
    result_path = "RESULTS_pulsar_n_periods_pics_" + filename
    if not os.path.exists(result_path):
        os.makedirs(result_path)
    if save_strongest:
        best_result_path = result_path + '/Strongest_pulses'
        if not os.path.exists(best_result_path):
            os.makedirs(best_result_path)

    # Taking pulsar period from catalogue
    pulsar_ra, pulsar_dec, DM, p_bar = catalogue_pulsar(pulsar_name)

    # DAT file to be analyzed:
    filepath = common_path + filename

    # Timeline file to be analyzed:
    timeline_filepath = common_path + filename.split(
        '_Data_')[0] + '_Timeline.txt'

    # Opening DAT datafile
    file = open(filepath, 'rb')

    # Data file header read
    df_filesize = os.stat(filepath).st_size  # Size of file
    df_filepath = file.read(32).decode('utf-8').rstrip(
        '\x00')  # Initial data file name
    file.close()

    if df_filepath[-4:] == '.adr':

        [
            df_filepath, df_filesize, df_system_name, df_obs_place,
            df_description, CLCfrq, df_creation_timeUTC, ReceiverMode, Mode,
            sumDifMode, NAvr, time_resolution, fmin, fmax, df, frequency,
            FFTsize, SLine, Width, BlockSize
        ] = FileHeaderReaderADR(filepath, 0, 0)

        freq_points_num = len(frequency)

    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(filepath, 0, 1)

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

    # Time line file reading
    timeline, dt_timeline = time_line_file_reader(timeline_filepath)

    # Calculation of the dimensions of arrays to read taking into account the pulsar period
    spectra_in_file = int(
        (df_filesize - 1024) /
        (8 * freq_points_num))  # int(df_filesize - 1024)/(2*4*freq_points_num)
    spectra_to_read = int(
        np.round((periods_per_fig * p_bar / time_resolution), 0))
    num_of_blocks = int(np.floor(spectra_in_file / spectra_to_read))

    print('   Pulsar period:                           ', p_bar, 's.')
    print('   Time resolution:                         ', time_resolution,
          's.')
    print('   Number of spectra to read in', periods_per_fig, 'periods:  ',
          spectra_to_read, ' ')
    print('   Number of spectra in file:               ', spectra_in_file, ' ')
    print('   Number of', periods_per_fig, 'periods blocks in file:      ',
          num_of_blocks, '\n')

    # Data reading and making figures
    print('\n\n  *** Data reading and making figures *** \n\n')

    data_file = open(filepath, 'rb')
    data_file.seek(
        1024, os.SEEK_SET
    )  # Jumping to 1024+number of spectra to skip byte from file beginning

    bar = IncrementalBar('   Making pictures of n periods: ',
                         max=num_of_blocks,
                         suffix='%(percent)d%%')
    bar.start()

    for block in range(num_of_blocks + 1):  # Main loop by blocks of data

        # bar.next()

        # current_time = time.strftime("%H:%M:%S")
        # print(' * Data block # ', block + 1, ' of ', num_of_blocks + 1, '  started at: ', current_time)

        # Reading the last block which is less then 3 periods
        if block == num_of_blocks:
            spectra_to_read = spectra_in_file - num_of_blocks * spectra_to_read

        # Reading and preparing block of data (3 periods)
        data = np.fromfile(data_file,
                           dtype=np.float64,
                           count=spectra_to_read * len(frequency))
        data = np.reshape(data, [len(frequency), spectra_to_read], order='F')
        data = 10 * np.log10(data)
        if normalize_response > 0:
            Normalization_dB(data.transpose(), len(frequency), spectra_to_read)

        # Preparing single averaged data profile for figure
        profile = data.mean(axis=0)[:]
        profile = profile - np.mean(profile)
        data = data - np.mean(data)

        # Time line
        fig_time_scale = timeline[block * spectra_to_read:(block + 1) *
                                  spectra_to_read]

        # Making result picture
        fig = plt.figure(figsize=(9.2, 4.5))
        rc('font', size=5, weight='bold')
        ax1 = fig.add_subplot(211)
        ax1.plot(profile,
                 color=u'#1f77b4',
                 linestyle='-',
                 alpha=1.0,
                 linewidth='0.60',
                 label='3 pulses time profile')
        ax1.legend(loc='upper right', fontsize=5)
        ax1.grid(b=True,
                 which='both',
                 color='silver',
                 linewidth='0.50',
                 linestyle='-')
        ax1.axis([0, len(profile), profile_pic_min, profile_pic_max])
        ax1.set_ylabel('Amplitude, AU', fontsize=6, fontweight='bold')
        ax1.set_title('File: ' + filename + '  Description: ' +
                      df_description + '  Resolution: ' +
                      str(np.round(df / 1000, 3)) + ' kHz and ' +
                      str(np.round(time_resolution * 1000, 3)) + ' ms.',
                      fontsize=5,
                      fontweight='bold')
        ax1.tick_params(axis='x',
                        which='both',
                        bottom=False,
                        top=False,
                        labelbottom=False)
        ax2 = fig.add_subplot(212)
        ax2.imshow(np.flipud(data),
                   aspect='auto',
                   cmap=colormap,
                   vmin=spectrum_pic_min,
                   vmax=spectrum_pic_max,
                   extent=[0, len(profile), frequency[0], frequency[-1]])
        ax2.set_xlabel('Time UTC (at the lowest frequency), HH:MM:SS.ms',
                       fontsize=6,
                       fontweight='bold')
        ax2.set_ylabel('Frequency, MHz', fontsize=6, fontweight='bold')
        text = ax2.get_xticks().tolist()
        for i in range(len(text) - 1):
            k = int(text[i])
            text[i] = fig_time_scale[k][11:23]
        ax2.set_xticklabels(text, fontsize=5, fontweight='bold')
        fig.subplots_adjust(hspace=0.05, top=0.91)
        fig.suptitle('Single pulses of ' + pulsar_name + ' (DM: ' + str(DM) +
                     r' $\mathrm{pc \cdot cm^{-3}}$' + ', Period: ' +
                     str(p_bar) + ' s.), fig. ' + str(block + 1) + ' of ' +
                     str(num_of_blocks + 1),
                     fontsize=7,
                     fontweight='bold')
        fig.text(0.80,
                 0.04,
                 'Processed ' + current_date + ' at ' + current_time,
                 fontsize=3,
                 transform=plt.gcf().transFigure)
        fig.text(0.09,
                 0.04,
                 'Software version: ' + Software_version +
                 ', [email protected], IRA NASU',
                 fontsize=3,
                 transform=plt.gcf().transFigure)
        pylab.savefig(result_path + '/' + filename + ' fig. ' +
                      str(block + 1) + ' - Combined picture.png',
                      bbox_inches='tight',
                      dpi=customDPI)

        # If the profile has points above threshold save picture also into separate folder
        if save_strongest and np.max(profile) > threshold:
            pylab.savefig(best_result_path + '/' + filename + ' fig. ' +
                          str(block + 1) + ' - Combined picture.png',
                          bbox_inches='tight',
                          dpi=customDPI)
        plt.close('all')

        bar.next()

    bar.finish()
    data_file.close()
def cut_needed_pulsar_period_from_dat_to_dat(common_path, filename,
                                             pulsar_name, period_number,
                                             profile_pic_min, profile_pic_max,
                                             spectrum_pic_min,
                                             spectrum_pic_max, periods_per_fig,
                                             customDPI, colormap):
    """
    Function to find and cut the selected pulsar period (by its number) from the DAT files
    """

    software_version = '2021.08.07'

    current_time = time.strftime("%H:%M:%S")
    current_date = time.strftime("%d.%m.%Y")

    # Creating a folder where all pictures and results will be stored (if it doesn't exist)
    result_path = "RESULTS_pulsar_extracted_pulse_" + filename
    if not os.path.exists(result_path):
        os.makedirs(result_path)

    # Taking pulsar period from catalogue
    pulsar_ra, pulsar_dec, DM, p_bar = catalogue_pulsar(pulsar_name)

    # DAT file to be analyzed:
    filepath = common_path + filename

    # Timeline file to be analyzed:
    timeline_filepath = common_path + filename.split(
        '_Data_')[0] + '_Timeline.txt'

    # Opening DAT datafile
    file = open(filepath, 'rb')

    # Data file header read
    df_filesize = os.stat(filepath).st_size  # Size of file
    df_filepath = file.read(32).decode('utf-8').rstrip(
        '\x00')  # Initial data file name
    file.close()

    if df_filepath[-4:] == '.adr':
        [
            df_filepath, df_filesize, df_system_name, df_obs_place,
            df_description, CLCfrq, df_creation_timeUTC, ReceiverMode, Mode,
            sumDifMode, NAvr, time_resolution, fmin, fmax, df, frequency,
            FFTsize, SLine, Width, BlockSize
        ] = FileHeaderReaderADR(filepath, 0, 0)

        freq_points_num = len(frequency)

    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(filepath, 0, 0)

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

    # Time line file reading
    timeline, dt_timeline = time_line_file_reader(timeline_filepath)

    # Calculation of the dimensions of arrays to read taking into account the pulsar period
    spectra_in_file = int(
        (df_filesize - 1024) /
        (8 * freq_points_num))  # int(df_filesize - 1024)/(2*4*freq_points_num)
    spectra_to_read = int(
        np.round((periods_per_fig * p_bar / time_resolution), 0))
    spectra_per_period = int(np.round((p_bar / time_resolution), 0))
    num_of_blocks = int(np.floor(spectra_in_file / spectra_to_read))

    print('\n   Pulsar name:                             ', pulsar_name, '')
    print('   Pulsar period:                           ', p_bar, 's.')
    print('   Time resolution:                         ', time_resolution,
          's.')
    print('   Number of spectra to read in', periods_per_fig, 'periods:  ',
          spectra_to_read, ' ')
    print('   Number of spectra in file:               ', spectra_in_file, ' ')
    print('   Number of', periods_per_fig, 'periods blocks in file:      ',
          num_of_blocks, '\n')

    # Data reading and making figures
    print('\n   Data reading and making figure...')

    data_file = open(filepath, 'rb')

    # Jumping to 1024+number of spectra to skip bytes from file beginning
    data_file.seek(
        1024 + (period_number - 1) * spectra_per_period * len(frequency) * 8,
        os.SEEK_SET)

    # Reading and preparing block of data (3 periods)
    data = np.fromfile(data_file,
                       dtype=np.float64,
                       count=spectra_to_read * len(frequency))
    data_file.close()

    data = np.reshape(data, [len(frequency), spectra_to_read], order='F')

    # Read data file header from initial file
    with open(filepath, 'rb') as file:
        file_header = file.read(1024)

    # Create binary file with the header and pulsar one or two periods data
    dat_file_name = 'Single_pulse_' + filename
    file_data = open(result_path + '/' + dat_file_name, 'wb')
    file_data.write(file_header)
    del file_header
    # Prepare data to save to the file
    temp = data.transpose().copy(order='C')
    file_data.write(np.float64(temp))
    del temp
    file_data.close()

    # Time line
    fig_time_scale = timeline[(period_number - 1) *
                              spectra_per_period:(period_number - 1 +
                                                  spectra_to_read) *
                              spectra_per_period]

    # Prepared code to save timeline to a file, but as the timeline is wrong comented them temporarily
    # # Creating and filling a new timeline TXT file for results
    # new_tl_file_name = dat_file_name.split('_Data_', 1)[0] + '_Timeline.txt'
    # new_tl_file = open(result_path + '/' + new_tl_file_name, 'w')
    # # Saving time data to new file
    # for j in range(len(fig_time_scale)):
    #     new_tl_file.write((fig_time_scale[j][:]) + '')
    # new_tl_file.close()

    # Logging data for figure
    data = 10 * np.log10(data)

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

    # Making result picture
    fig = plt.figure(figsize=(9.2, 4.5))
    rc('font', size=5, weight='bold')
    ax2 = fig.add_subplot(111)
    ax2.set_title('File: ' + filename + '  Description: ' + df_description +
                  '  Resolution: ' + str(np.round(df / 1000, 3)) +
                  ' kHz and ' + str(np.round(time_resolution * 1000, 3)) +
                  ' ms.',
                  fontsize=5,
                  fontweight='bold')
    ax2.imshow(
        np.flipud(data),
        aspect='auto',
        cmap=colormap,
        vmin=spectrum_pic_min,
        vmax=spectrum_pic_max,
        extent=[0, data.shape[1], frequency[0] + 16.5,
                frequency[-1] + 16.5])  # len(profile)
    ax2.set_xlabel('Time UTC (at the lowest frequency), HH:MM:SS.ms',
                   fontsize=6,
                   fontweight='bold')
    ax2.set_ylabel('Frequency, MHz', fontsize=6, fontweight='bold')
    text = ax2.get_xticks().tolist()
    for i in range(len(text) - 1):
        k = int(text[i])
        text[i] = fig_time_scale[k][11:23]
    ax2.set_xticklabels(text, fontsize=5, fontweight='bold')
    fig.subplots_adjust(hspace=0.05, top=0.91)
    fig.suptitle('Extracted single pulse of ' + pulsar_name + ' (DM: ' +
                 str(DM) + r' $\mathrm{pc \cdot cm^{-3}}$' + ', Period: ' +
                 str(p_bar) + ' s.)',
                 fontsize=7,
                 fontweight='bold')
    fig.text(0.80,
             0.04,
             'Processed ' + current_date + ' at ' + current_time,
             fontsize=3,
             transform=plt.gcf().transFigure)
    fig.text(0.09,
             0.04,
             'Software version: ' + software_version +
             ', [email protected], IRA NASU',
             fontsize=3,
             transform=plt.gcf().transFigure)
    pylab.savefig(result_path + '/' + 'Single_pulse_' + filename[:-4] + '.png',
                  bbox_inches='tight',
                  dpi=customDPI)
    plt.close('all')

    return result_path, 'Single_pulse_' + filename, filename + 'Single_pulse_' + filename[:
                                                                                          -4] + '.png'