Beispiel #1
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))
Beispiel #2
0
def time_correct(in_dir_name, dir_params, main_dir):
    '''
    Spectrums/Images time adjusted in TimeSort function above are loaded in
    and the data is maintained. The file name is split and the first file
    captured is set to 0, the following files within the directory are given
    a time stamp respective to the zero file (a time step). This is useful
    for later processing.
    Args:
        in_dir_name: <string> directory name containind time adjusted
                     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[0:2])
    print(' ')
    print(dir_params)
    data_files = io.extract_files(dir_name=in_dir_name,
                                  file_string=file_string)

    zero_file_name = data_files[0]
    zero_time_stamp = (zero_file_name.split('_')[::-1])[0]

    for index, selected_file in enumerate(data_files):
        file = os.path.join(in_dir_name, selected_file)
        data = np.load(file)

        file_name = io.get_filename(file)
        split_file = file_name.split('_')[::-1]
        time_correction = int(
            float(split_file[0]) - float(zero_time_stamp[0:-4]))

        out_dir_name = '_'.join(dir_params[0:-1]) + '_TimeCorrected'
        out_dir = os.path.join(main_dir, out_dir_name)
        io.check_dir_exists(out_dir)

        joined = []
        joined.append(file_string)
        joined.append(str(time_correction))
        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))
        else:
            print('\nCorrecting Time Stamp')
            dir_params = dprep.solute_finder(solute_dir)
            dprep.time_sort(in_dir_name=solute_dir,
                            dir_params=dir_params,
                            main_dir=selected_date)
            dir_params = dprep.solute_finder(f'{solute_dir}_TimeAdjusted')
            dprep.time_correct(in_dir_name=f'{solute_dir}_TimeAdjusted',
                               dir_params=dir_params,
                               main_dir=selected_date)
            shutil.rmtree(f'{solute_dir}_TimeAdjusted')

            print('\nFinding Peaks')
            timec_dir = f'{solute_dir}_TimeCorrected'
            results_dir = os.path.join(selected_date, 'Results')
            io.check_dir_exists(results_dir)

            dir_params = dprep.solute_finder(timec_dir)
            data_files = io.extract_files(dir_name=timec_dir,
                                          file_string='_'.join(
                                              dir_params[0:2]))

            zero_file = os.path.join(timec_dir, data_files[0])

            outfile_name = (str('_'.join(dir_params[0:-1])) + '_Peaks.csv')
            with open(outfile_name, 'a', newline='') as outfile:
                writer = csv.writer(outfile, delimiter=',')
                writer.writerow(['Wavelength [nm]'] + ['Peak [nm]'] +
                                ['Peak Shift [nm]'])

                for index, selected_file in enumerate(data_files):