Beispiel #1
0
def main(path_to_file):
    print(f"Reading opus file from path" f"{path_to_file}")
    opus_data = read_file(path_to_file)

    print(f"Data fields: " f"{list(opus_data.keys())}")

    ab_x = opus_data.get_range("AB")
    # the "AB" data can contain more null values at the end (at least 1)
    # so the getting useful data requires slicing the array:
    abs = opus_data["AB"][0:len(ab_x)]
    print(f"Absorption spectrum range: " f"{ab_x[0]} {ab_x[-1]}")
    print(f"Absorption elements num: " f'{len(abs)}')

    try:
        import matplotlib.pyplot as plt

        print("Plotting AB")
        plt.plot(opus_data.get_range("AB"), abs)
        plt.show()

        print("Plotting interpolated AB")
        plt.plot(*opus_data.interpolate(ab_x[0], ab_x[-1], 100))
        plt.show()

    except ImportError:
        print(f"Install matplotlib to plot spectra")
Beispiel #2
0
def load_spectra_df(SPECTRA_DIR, AFSIS_PATH):
    names = []
    spectra = []

    for path in tqdm(AFSIS_PATH.glob(SPECTRA_DIR)):
        if path.is_file():
            spect_data = read_file(path)
            spectra.append(spect_data["AB"])
            names.append(path.stem)
    wave_nums = spect_data.get_range()

    column_names = ['{:.3f}'.format(x) for x in wave_nums]
    spectra_df = pd.DataFrame(spectra, index=names, columns=column_names)
    return spectra_df
Beispiel #3
0
def main(path_to_file):
    print(f'Reading opus file from path' f'{path_to_file}')
    opus_data = read_file(path_to_file)

    print(f'Dimension of data: ' f'{len(opus_data.wave_nums)}')

    print(f'Spectrum range: ['
          f'{min(opus_data.spectrum)}; '
          f'{max(opus_data.spectrum)}]')

    print(f'Metadata: ' f'{opus_data.meta}')

    plt.plot(opus_data.wave_nums, opus_data.spectrum)
    plt.title(f'Spectrum {path_to_file}')
    plt.show()
def load_spectra(path=DATA_SPECTRA):
    """Returns DRIFT/MIRs spectra, Petra's data, Vietnam, 2007-2008"""
    path = Path(path)
    df_list = []
    for i, f in enumerate(path.glob('*.*')):
        if f.suffix != '.xls':
            file = brukeropusreader.read_file(f)
            spectrum = pd.Series(file['AB'])
            spectrum_name = _clean_column_name(f.name)
            if i == 0:
                wavelength = pd.Series(file.get_range("AB"))
                data = {'wavenumber': wavelength, spectrum_name: spectrum}
            else:
                data = {spectrum_name: spectrum}

            df_list.append(pd.DataFrame(data))

    return pd.concat(df_list, axis=1, ignore_index=False, sort=False).set_index('wavenumber')
 def parse_values(self, filenames):
     '''parse values of .0 files from bruker opus'''
     if not hasattr(self, 'file_error'):
         self.file_error = []
     values = {}
     for filename in filenames:
         if filename[0][-2:] == '.0':
             try:
                 bruker_object = read_file(filename[0])
             except ValueError:
                 print(f'ValueError {filename}')
                 self.file_error.append(f'ValueError {filename}')
                 continue
             wave = pd.DataFrame(
                 bruker_object.spectrum,
                 1 / np.array(bruker_object.wave_nums) * 1e7)
             wave.columns = ['intensity(relative)']
             wave.index.name = 'wavelength(nm)'
             values[filename[1]] = wave
     return values
def load_spectra_rep(path=DATA_SPECTRA_REP):
    """Returns DRIFT/MIRs spectra and their replicates, Petra's data, Vietnam, 2007-2008"""
    path = Path(path)
    df_list = []
    _ids = []
    for i, f in enumerate(path.glob('*.0')):
        _id = int(re.search(r'(.*?)_', f.name).group(1))
        if (_id in range(3179, 4922)) and (_id not in _ids):
            file = brukeropusreader.read_file(f)
            if 'AB' in file:
                _ids.append(_id)
                spectrum = pd.Series(file['AB'])
                spectrum_name = _id
                if len(df_list) == 0:
                    wavelength = pd.Series(file.get_range('AB'))
                    data = {'wavenumber': wavelength, spectrum_name: spectrum}
                else:
                    data = {spectrum_name: spectrum}

                df_list.append(pd.DataFrame(data))
    df = pd.concat(df_list, axis=1, ignore_index=False, sort=False).set_index('wavenumber')
    return df.reindex(sorted(df.columns), axis=1)
Beispiel #7
0
def main(path_to_file):
    print(f"Reading opus file from path" f"{path_to_file}")
    opus_data = read_file(path_to_file)

    print(f"Data fields: " f"{list(opus_data.keys())}")

    ab_x = opus_data.get_range("AB")
    print(f"Absorption spectrum range: " f"{ab_x[0]} {ab_x[-1]}")
    print(f"Absorption elements num: " f'{len(opus_data["AB"])}')

    try:
        import matplotlib.pyplot as plt

        print("Plotting AB")
        plt.plot(opus_data.get_range("AB"), opus_data["AB"])
        plt.show()

        print("Plotting interpolated AB")
        plt.plot(*opus_data.interpolate(ab_x[0], ab_x[-1], 100))
        plt.show()

    except ImportError:
        print(f"Install matplotlib to plot spectra")
Beispiel #8
0
print(Z2)

# %% [markdown]
# Note above that a warning was issued because the `irdata/OPUS` contains a background file
# (single beam) which is not read by SpectroChemPy.
#
# Finally, supplementary information can be obtained by the direct use of
# `brukeropusreader`.
#
# For instance:

# %%
from brukeropusreader import read_file  # noqa: E402

opusfile = scp.DATADIR / "irdata" / "OPUS" / "test.0000"  # the full path of the file
Z3 = read_file(
    opusfile)  # returns a dictionary of the data and metadata extracted
for key in Z3:
    print(key)

Z3["Optik"]  # looks what is the Optik block:

# %% [markdown]
# ## Import/Export of JCAMP-DX files
#
# [JCAMP-DX](http://www.jcamp-dx.org/) is an open format initially developed for IR data and extended to
# other spectroscopies. At present, the JCAMP-DX reader implemented in Spectrochempy is limited to IR data and
# AFFN encoding (see R. S. McDonald and Paul A. Wilks, JCAMP-DX: A Standard Form for Exchange of Infrared Spectra in
# Readable Form, Appl. Spec., 1988, 1, 151–162. doi:10.1366/0003702884428734 for details).
#
# The JCAMP-DX reader of spectrochempy has been essentially written to read again the jcamp-dx files exported by
# spectrochempy `write_jdx()` writer.
Beispiel #9
0
 def __init__(self, filename):
     """Load a binary Bruker Opus file into a dictionary."""
     import brukeropusreader
     self.filename = filename
     self.data = brukeropusreader.read_file(expand_path(filename))
    shutil.rmtree("converted_data")
except:
    print("could not remove /converted_data, directory not does exist")

# Calculate files and directories that need to be created
targetDirs = list(
    map(lambda dir: dir.replace("raw_data", "converted_data", 1), dirs))
targetFiles = list(
    map(lambda file: file.replace("raw_data", "converted_data", 1), files))

targetDirs.append("converted_data")

# Create all target directories
for dir in targetDirs:
    try:
        Path(dir).mkdir(parents=True, exist_ok=True)
    except:
        print("Could not create directory: " + dir)

# Read binary OPUS files and extract ScSm data, lining up each value with it's
# corresponding X-Axis value, and save each row as an X-Axis,Y-Axis pair
for fileIndex, binaryFile in enumerate(files):
    opus_data = read_file(binaryFile)
    newFileName = targetFiles[fileIndex] + ".dpt"
    with open(newFileName, "w") as newFile:
        for index, yValue in enumerate(opus_data["ScSm"]):
            newFile.write(
                str(xValues[index]) + "," + str(
                    Decimal(str(yValue)).quantize(
                        Decimal('.00001'), rounding=ROUND_HALF_EVEN)) + "\n")
    print("Created file: " + newFileName)