def bg_peaks(file, zero_file):
    '''
    Uses ReadInValues function and FindPeaks function to find the peak
    wavelength of each background image, and find the peak wavelength
    shift of each background image compared to the sensor peak (zero
    file). Returns three values, the file_name, the background peak and
    the peak shift value.
    Args:
        file: <string> file path to background image
        zero_file: <string> file path to sensor background image
    '''
    wav_naught, int_naught, zero_file_name = io.csv_in(zero_file)
    wavelength, intensity, file_name = io.csv_in(file)

    zero_peak = peaks(x=wav_naught,
                      y=int_naught,
                      distance=300,
                      width=20,
                      xmin=740,
                      xmax=800)

    bg_peak = peaks(x=wavelength,
                    y=intensity,
                    distance=300,
                    width=20,
                    xmin=740,
                    xmax=800)

    peak_shift = float(bg_peak[0]) - float(zero_peak[0])

    return file_name, bg_peak[0], peak_shift
Beispiel #2
0
def time_sort(in_dir_name, dir_params, main_dir):
    '''
    Spectrums/Images captured using splicco's automatic data capture/timed
    sequential function are automatically given a user defined file name and
    a time and date stamp of the moment a measurement was taken (file created).
    This function converts the time stamp given into a value in seconds by
    splitting the file name into sections, reversing the order and splitting
    the time stamp at 'h' 'm' and 's' then converting to seconds.
    The function then adds all the values together to give a total time in
    seconds, concatenates this with the original file name, and saves the
    original data out with a new file name as a numpy array.
    Args:
        in_dir_name: <string> directory name containing spectrum files
        dir_params: <array> directories are given a name equivalent to the
                    individual file names, the dir_params function splits
                    the directory name into an array that can be used to find
                    the correct spectrum files.
        main_dir: <string> current working directory
    '''
    file_string = '_'.join(dir_params)
    print(f'\n{dir_params}')
    data_files = io.extract_files(dir_name=in_dir_name,
                                  file_string=file_string)

    for index, selected_file in enumerate(data_files):
        file = os.path.join(in_dir_name, selected_file)
        wavelength, intensity, file_name = io.csv_in(file)

        data = np.vstack((wavelength, intensity)).T

        split_file = file_name.split('_')[::-1]
        date_split = split_file[1]
        hrs_split = split_file[0].split('h')
        mins_split = hrs_split[1].split('m')
        secs_split = mins_split[1].split('s')

        total_seconds = convert_to_seconds(date=date_split,
                                           hours=hrs_split[0],
                                           minutes=mins_split[0],
                                           seconds=secs_split[0],
                                           milliseconds=secs_split[1])

        out_dir_name = '_'.join(dir_params) + '_TimeAdjusted'
        out_dir = os.path.join(main_dir, out_dir_name)
        io.check_dir_exists(out_dir)

        joined = []
        joined.append(file_string)
        joined.append(str(total_seconds))
        new_file_name = '_'.join(joined)

        io.array_save(array_name=data,
                      file_name=new_file_name,
                      dir_name=out_dir)

        io.update_progress(index / len(data_files))
root = io.config_dir_path()

for date_dir in os.listdir(root):
    selected_date = os.path.join(root, date_dir)
    print(f'Looking at: {date_dir}')

    print('Background Calibration')
    bg_dir = os.path.join(selected_date, 'Background')
    bg_datafiles = io.extract_files(dir_name=bg_dir,
                                    file_string='_Background.csv')

    for index, selected_file in enumerate(bg_datafiles):
        file = os.path.join(bg_dir, selected_file)
        zero_file = os.path.join(bg_dir, f'{sensor}_Background.csv')

        wavelength, intensity, file_name = io.csv_in(file)
        wav_naught, int_naught, zero_name = io.csv_in(zero_file)

        fig, ax = plt.subplots(1, 1, figsize=[10, 7])
        ax.plot(wavelength, intensity, 'r', lw=2, label=file_name)
        ax.plot(wav_naught, int_naught, 'b', lw=2, label=zero_name)
        ax.grid(True)
        ax.legend(frameon=True, loc=0, ncol=1, prop={'size': 12})
        ax.set_xlabel('Wavelength [nm]', fontsize=14)
        ax.set_ylabel('Intensity', fontsize=14)
        ax.set_title(file_name, fontsize=18)
        ax.tick_params(axis='both', which='major', labelsize=14)
        fig.tight_layout()

        graph_out_path = os.path.join(bg_dir, f'{file_name}.png')
        plt.savefig(graph_out_path)
Beispiel #4
0
    img_dir = os.path.join(main_dir, hs_img)
    if not os.path.isdir(img_dir):
        continue

    step, wl, f, power, norm_power = io.get_pwr_spectrum(dir_name=img_dir,
                                                         plot_show=False,
                                                         plot_save=True)

    io.create_all_dirs(dir_name=img_dir)

    data_files = io.extract_files(dir_name=img_dir, file_string='img_')

    print('\nNormalising csvs...')
    for index, file in enumerate(data_files):
        file_path = os.path.join(img_dir, file)
        img, file_name = io.csv_in(file_path=file_path)

        _, img_no = file_name.split('_')
        io.png_out(image_data=img,
                   file_name=file_name,
                   dir_name=img_dir,
                   image_title=f'Image: {img_no}',
                   out_name=f'{file_name}.png',
                   plot_show=False)

        norm_img = dp.pwr_norm(image_data=img,
                               file_name=file_name,
                               norm_power=norm_power,
                               dir_name=img_dir)

        io.png_out(image_data=norm_img,