# -*- coding: utf-8 -*- """ Created on Mon Sep 27 17:41:00 2021 @author: Zdenek Navratil Sample program for conversion of Bruker files to txt files. How to install bruker reader with anaconda: anaconda > conda install git conda install pip pip install git+http://github.com/qedsoftware/brukeropusreader.git https://github.com/qedsoftware/brukeropusreader """ import numpy as np import matplotlib.pyplot as mp from brukeropusreader import read_file import os """**************************************************************************** CONVERT .0 FILES TO TXT FILES ****************************************************************************""" def Bruker2Txt(filename, new_filename_prefix="", SaveT=True, SaveS=False, Correct=False, CT=1): filename_list = filename.split(".") index = int(filename_list[-1]) if new_filename_prefix == "": new_filename_prefix = filename_list[0] opus_data = read_file(filename) txtfilename = "{:s}_{:02d}.txt".format(new_filename_prefix, index) txtfilename_S = "{:s}_{:02d}_S.txt".format(new_filename_prefix, index) pngfilename = "{:s}_{:02d}.png".format(new_filename_prefix, index) X=np.asarray(opus_data.get_range("ScSm")) S=np.asarray(opus_data["ScSm"]) R=np.asarray(opus_data["ScRf"]) #T=S/R T = np.asarray(opus_data["AB"]) if Correct: T = np.divide(T, CT) if SaveT: Z=np.column_stack((X,T)) np.savetxt(txtfilename, Z, delimiter='\t', newline='\n') #np.savetxt(txtfilename,Z[Z[:,0]>700], delimiter='\t', newline='\n') if SaveS: Z=np.column_stack((X,S)) np.savetxt(txtfilename_S,Z, delimiter='\t', newline='\n') # mp.figure() fig, ax = mp.subplots() mp.title(os.path.basename(txtfilename)) mp.xlabel("wavenumber (cm$^{-1}$)") mp.ylabel("intensity (a. u.)") ax.invert_xaxis() mp.plot(opus_data.get_range("ScSm"), S, label="signal", linewidth=0.5) mp.plot(opus_data.get_range("ScRf"), R, label="reference", linewidth=0.5) mp.legend() mp.show() # mp.figure() fig, ax = mp.subplots() mp.title(os.path.basename(txtfilename)) mp.xlabel("wavenumber (cm$^{-1}$)") mp.ylabel("transmittance") #mp.ylim(0.0,1.1) ax.invert_xaxis() mp.plot(opus_data.get_range("ScSm"), T, linewidth=0.5) if SaveT: mp.savefig(pngfilename, dpi=300) mp.show() print("File {:s} converted.".format(txtfilename)) return X, S, R, T global AA def MultipleBruker2Txt(filename, new_filename_prefix, Count, SaveT=True, SaveS=False, Correct=False, CT=1): added_vals_before=8 added_vals_after=30 filename_list = filename.split(".") index = int(filename_list[-1]) if new_filename_prefix == "": new_filename_prefix = filename_list[0] opus_data = read_file(filename) print("Scan time {:.3f} s".format(opus_data['Instrument']['DUR'])) X=np.asarray(opus_data.get_range("ScSm")) Sfull=np.asarray(opus_data["ScSm"]) R=np.asarray(opus_data["ScRf"]) Tfull=np.asarray(opus_data["AB"]) periodicity = len(X) + added_vals_before + added_vals_after print(len(X)) S = np.empty([Count,len(X)]) T = np.empty([Count,len(X)]) fig, (axT, axS) = mp.subplots(2,1,sharex=True, figsize=(14,14), dpi=600) axS.set_xlim(600,3000) axT.set_xlim(600,3000) axS.invert_xaxis() axS.autoscale(enable=True, axis='y') axT.autoscale(enable=True, axis='y') for spi in range(Count): #for j in range(10): # AA[j,spi]= Tfull[(spi)*periodicity+j] T[spi] = Tfull[(spi*periodicity + added_vals_before): ((spi+1)*periodicity-added_vals_after)] S[spi] = Sfull[(spi*periodicity + added_vals_before): ((spi+1)*periodicity-added_vals_after)] if Correct: T[spi] = np.divide(T[spi], CT) axS.plot(X, S[spi], linewidth=0.5, label="{:02d}".format(spi)) axT.plot(X, T[spi], linewidth=0.5, label="{:02d}".format(spi)) if SaveT: Z=np.column_stack((X,T[spi])) txtfilename = "{:s}_{:02d}_{:02d}.txt".format(new_filename_prefix, index, spi) np.savetxt(txtfilename, Z[Z[:,0]>700], delimiter='\t', newline='\n') #np.savetxt(txtfilename, Z, delimiter='\t', newline='\n') print("File {:s} converted.".format(txtfilename)) # # pngfilename = "{:s}_{:02d}_{:02d}.png".format(new_filename_prefix, index, spi) # mp.savefig(pngfilename, dpi=300) if SaveS: Z=np.column_stack((X,S[spi])) txtfilename_S = "{:s}_{:02d}_{:02d}_S.txt".format(new_filename_prefix, index, spi) np.savetxt(txtfilename_S, Z, delimiter='\t', newline='\n') print("File {:s} converted.".format(txtfilename_S)) axS.set_title( "{:s}, series {:d}".format(filename, Count)) axS.set_xlabel("Wavenumber (cm$^{-1}$)") axS.set_ylabel("Signal") #axS.ylim(0.0,1.1) #mp.ylim(0.0,1.1) axT.set_title( "{:s}, series {:d}".format(filename, Count)) axT.set_xlabel("Wavenumber (cm$^{-1}$)") axT.set_ylabel("Transmittance") #axT.set_xlim(600,3000) #axT.invert_xaxis() #axT.invert_xaxis() axS.legend(loc="upper right") if SaveT: pngfilename = "{:s}_{:02d}.png".format(new_filename_prefix, index) mp.savefig(pngfilename, dpi=600) mp.show() return X, T, Tfull #============================================================================== def ConvertAll(MaxIndex): for i in range(MaxIndex): Bruker2Txt(".{:04d}".format(i)) def ConvertDir(Directory): files = os.listdir(Directory) for f in files: if f.split(".")[-1].isnumeric(): ffull = Directory+ "\\" + f print(ffull) Bruker2Txt(ffull, SaveS=False) def MultiFileConvertDir(Directory, Count, SaveT=True, SaveS=False, Correct=False, CT=1): # Count - pocet mereni v jednom souboru files = os.listdir(Directory) for f in files: if f.split(".")[-1].isnumeric(): ffull = Directory+ "\\" + f print(ffull) MultipleBruker2Txt(ffull, '', Count, SaveT, SaveS, Correct, CT) #MultipleBruker2Txt(r'e:\Spektra\_FTIR\2023-grafenoxid\2023-06-09-FZelenak\GOSheet.20', '', 50) #MultipleBruker2Txt(r'e:\e:\Spektra\_FTIR\2023-grafenoxid\2023-06-09-FZelenak\GO_Sheet.0', '', 50) MultiFileConvertDir(r'e:\Spektra\_FTIR\2023-grafenoxid\2023-06-09-FZelenak', 50) #ConvertDir(r"e:\Spektra\_FTIR\2023-grafenoxid\2023-03-30")