# -*- coding: utf-8 -*-
"""
Created on Tue Dec 15 11:48:50 2020

evaluate the ftir data recorded with the Bruker Lumos by Felix Netzer 
on the 30.03.2021 for the CA treated Viscose fibres, both, CA direct or viscose immersion
in NaOH first

todo:
    minmax for all different columns of the data
    
Extract_4ca für den Müll

12.10.2022 weitere ftir daten wurden vom FTIR Computer geholt und sind jetzt in 2021/cerdia/data/ftir_files
- es wurden die eigenen FTIR Daten in die Auswertung inkludiert
- Die Auswertung der Daten wird überarbeitet

Felixnotierung: ?CA als %-CA Lösung, TAc - Triacetin, 
Floriannotierung: ?pc_CA als %-CA Lösung
    
@author: csag4207
"""

import sys
sys.path.append('C:/Users/csag4207/Documents/forschung/python/scripts') # this has to be imported until the pythonpath is permanently changed to find the module
from bruker_ftir import extract_lumos
# from brukeropusreader import read_file
from os import listdir
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from sklearn.preprocessing import MinMaxScaler

from pyPreprocessing.baseline_correction import generate_baseline 
# https://alexandersouthan.github.io/pyPreprocessing/

from PIL import Image

# it seems the automated bruker software uses some kind of minmax scaler of the system

filenames = listdir('C:/Users/csag4207/Documents/forschung/arbeit/2021/cerdia/data/ftir_files')

selection = [i for i in filenames if '.dpt' in i]        

data = {}
for i in selection:
    data[i[:-6]] = extract_lumos('C:/Users/csag4207/Documents/forschung/arbeit/2021/cerdia/data/ftir_files/'+ i, ',')

#%% delete the trash

# data['6CA2.5NaOH_h2o_t3'] = np.delete(data['6CA2.5NaOH_h2o_t3'], 1, 1)

spot=list(data.keys())[34]

plt.figure()
# for i in np.arange(1, data[spot].shape[1]):
plt.plot(data[spot][:,0], data[spot][:,1:])
plt.xlim([4000, 600])
plt.title(spot)
plt.show()

#%% bring to a different dictionary

special={}

skipping = np.concatenate((np.arange(7,25), np.arange(26, 34)))
skipping = np.append(skipping, [35,36])
throwing = [list(data.keys())[i] for i in skipping]

# special['Viskose-Av'] = data.pop['Viskose-Av']

for i in throwing:
    special[i]=data.pop(i)
    
#%% delete the false data manually

data['2.5pc_NaOH_10pc_CA_viscose_28dtex']= np.delete(data['2.5pc_NaOH_10pc_CA_viscose_28dtex'], 4, axis=1)
data['2.5pc_NaOH_0pc_CA_viscose_28dtex']= np.delete(data['2.5pc_NaOH_0pc_CA_viscose_28dtex'], 3, axis=1)
data['2.5pc_NaOH_8pc_CA_viscose_28dtex']= np.delete(data['2.5pc_NaOH_8pc_CA_viscose_28dtex'], 1, axis=1)

#%% throw the bad data after visual observation

for i in data:
    plt.figure()
    for j in np.arange(1, data[i].shape[1]):
        plt.plot(data[i][:,0], data[i][:,j], label=j)
    plt.title(i)
    plt.legend()
    plt.xlim([4000, 550])
    plt.show()

#%% do a baseline removal

data_base={}

for i in data:
    data_base[i]={}
    for j in np.arange(1,data[i].shape[1]):
        data_base[i][j] = generate_baseline(data[i][:,j].reshape(1, data[i][:,j].shape[0]), mode='ModPoly', poly_order=2)

#%% check if it worked

for i in data:
    plt.figure()
    for j in np.arange(1, data[i].shape[1]):
        plt.plot(data[i][:,0], data[i][:,j], label=j)
        # plt.plot(data[i][:,0], data_base[i][j][0], linestyle=':', alpha=0.7)
    plt.title(i)
    plt.legend()
    plt.xlim([4000, 550])
    plt.show()

#%% baseline correct the data

for i in data:
    for j in np.arange(1, data[i].shape[1]):
        data[i][:,j]= np.subtract(data[i][:,j], data_base[i][j][0])

#%% calculate the mean from the baseline corrected array

data_mean={}

for i in data:
    data_mean[i] = np.mean(data[i][:,1:], axis=1)
    data_mean[i + '_err'] = np.std(data[i][:,1:], axis=1)

data_min= {}

scaler = MinMaxScaler()

for i in data:
    data_min[i] = scaler.fit_transform(data_mean[i].reshape(-1,1))

#%%

plt.figure()
for i in data:
    if 'pap' not in i:
        plt.plot(data[i][:,0], data_min[i][:,0], label=i)
# plt.plot(data[i][:,0], data_base[i][j][0], linestyle=':', alpha=0.7)
plt.legend()
plt.xlim([1800, 800])
plt.show()

#%% plot sth that might be roughly interesting for a manuscript

selection_0 = ['2.5pc_NaOH_0pc_CA_viscose_28dtex', '2.5pc_NaOH_2pc_CA_viscose_28dtex', '2.5pc_NaOH_4pc_CA_viscose_28dtex', '2.5pc_NaOH_6pc_CA_viscose_28dtex', '2.5pc_NaOH_8pc_CA_viscose_28dtex', '2.5pc_NaOH_10pc_CA_viscose_28dtex', 'CA-pure']

linestyles=[':', '-.', '--']
cmap = plt.get_cmap('RdPu')
colors = [cmap(i) for i in np.linspace(0.5, 1, selection_0.__len__())]

#%%

fig = plt.figure()
ax = fig.gca(projection='3d')
k = 0
for i in selection_0:
    ax.plot(data[i][:,0], data_min[i][:,0], np.divide(k,10), zdir= 'y', color=colors[k], label=i)
    k = k+1
ax.set_xlim([1800, 1170])
# plt.legend()
plt.show()

#%% try a two-dim version

plt.figure()
k=0
for i in selection_0:
    plt.plot(data[i][:,0], np.add(data_min[i][:,0], k/5), color=colors[k], label=i)
    k=k+1
plt.xlim([1800, 940])
plt.ylabel('arb. units')
plt.xlabel(r'wavenumber $cm^{-1}$')
plt.show()

# plt.savefig('C:/Users/csag4207/Documents/forschung/arbeit/2021/cerdia/pic/ca-visc/ftir.png', dpi=500)
# to_save= Image.open('C:/Users/csag4207/Documents/forschung/arbeit/2021/cerdia/pic/ca-visc/ftir.png').convert('RGB')
# to_save.save('C:/Users/csag4207/Documents/forschung/arbeit/2021/cerdia/pic/ca-visc/ftir.jpg')


#%% check sth is wrong with the viskose foil

spot='Viskose_Folie'

plt.figure()
plt.plot(special[spot][:,0], special[spot][:,1:])
plt.plot(data['Viskose'][:,0], data['Viskose'][:,1:])
plt.xlim([4000, 600])
plt.show()

#it seems the viscose systems either were by coincidence similarly weak or they don't show such absorptions

#%%
# #%%plot the data without NaOH and NaClAcetate treatment

# plt.figure()
# for i in data:
#     if '6CA' in i and '10NaOH' not in i and 'EXT' not in i:
#         plt.plot(data[i][:,0], data[i][:,1], label=i)
# plt.xlabel(r'wavenumber /$\frac{1}{cm}$')
# plt.ylabel('arb. units')
# plt.legend()
# plt.tight_layout()
# plt.xlim([4000, 500])
# plt.show()

# # check which of the column is used by the plotting tool
# # on single fibres there were several ftir spectra that refer to different areas of the fibre
# # and thereby different possibilities to encounter CA on the viscose fibre
# # it has to be found a method for uniform CA distribution first


# #%% evaluate the minmax approach, another possible approach is some signal which will not be influenced (e.g. 1450 C-H stretching, should be present in alginate as in cellulose)

# data_min = {}
# scaler = MinMaxScaler()

# for i in data:
#     data_min[i] = scaler.fit_transform(data[i][:,1:])

# # check if calculation was correct

# plt.figure()
# for i in data_min:
#     if '4CA' in i and 'EXTRACT' not in i and 'NaOH' not in i:
#         plt.plot(data[i][:,0], data_min[i][:,0], label=i + '0_MM')
#         plt.plot(data[i][:,0], data_min[i][:,1], label=i + '1_MM')
#         plt.plot(data[i][:,0], data[i][:,1], label=i + '0')
#         plt.plot(data[i][:,0], data[i][:,2], label=i + '1')
# plt.xlabel(r'wavenumber /$\frac{1}{cm}$')
# # plt.vlines(3331, 0, 0.35, color='r')
# plt.ylabel('arb. units')
# plt.legend()
# plt.tight_layout()
# plt.xlim([4000, 500])
# plt.show()


# #%%

# plt.figure()
# for i in data_min:
#     if '6CA' in i and 'EXTRACT' not in i:
#         plt.plot(data[i][:,0], data_min[i], label=i)

# plt.xlabel(r'wavenumber /$\frac{1}{cm}$')
# # plt.vlines(3331, 0, 0.35, color='r')
# plt.ylabel('arb. units')
# plt.legend()
# plt.tight_layout()
# plt.xlim([4000, 500])
# plt.show()


# #%% compare the different 4 pc data

# plt.figure()
# for i in data_min:
#     if '4CA' in i: 
#         plt.plot(data[i][:,0], data_min[i], label=i)

# plt.xlabel(r'wavenumber /$\frac{1}{cm}$')
# # plt.vlines(3331, 0, 0.35, color='r')
# plt.ylabel('arb. units')
# plt.legend()
# plt.tight_layout()
# plt.xlim([4000, 500])
# plt.show()



# #%% compare the h2o data

# colors = ['r', 'g', 'b']
# add_on = [0, 0.2, 0.4]
# from matplotlib.lines import Line2D # from https://matplotlib.org/3.1.1/gallery/text_labels_and_annotations/custom_legends.html

# plt.figure()
# k = 0
# for i in data_min:
#     if 'h2o' in i and '2.5NaOH' in i and 'T' not in i:
#         plt.plot(data[i][:,0], np.add(data_min[i], add_on[k]), color = colors[k])
#         k = k + 1
        
# plt.xlabel(r'wavenumber /$\frac{1}{cm}$')
# # plt.vlines(3331, 0, 0.35, color='r')
# plt.ylabel('arb. units')
# plt.legend(handles = [Line2D([0], [0], color=colors[0], lw = 3, label = '6CA2.5NaOH_h2o_t0'), Line2D([0], [0], color=colors[1], lw = 3, label = '6CA2.5NaOH_h2o_t2'), Line2D([0], [0], color=colors[2], lw = 3, label = '6CA2.5NaOH_h2o_t3')])
# plt.tight_layout()
# plt.xlim([1800, 800])
# plt.show()

# #%% compare the

# plt.figure()
# for i in data_min:
#     if 'TAc' in i:
#         plt.plot(data[i][:,0], data_min[i], label= i)
        
# plt.xlabel(r'wavenumber /$\frac{1}{cm}$')
# # plt.vlines(3331, 0, 0.35, color='r')
# plt.ylabel('arb. units')
# plt.legend()
# plt.tight_layout()
# plt.xlim([4000, 800])
# plt.show()


# #%% check how the signals shift with the different washings


# # select = ['3pc_I5_gyoxal_6pc_130degC_3min_ph12_di-h2o_wash', '3pc_I5_gyoxal_6pc_130degC_3min_ph12_HCl_wash', '3pc_I5_gyoxal_6pc_130degC_3min_ph12_NaOH_wash', '3pc_I5_gyoxal_6pc_130degC_3min_ph11.6_direct_h2o_wash', '3pc_I5_gyoxal_6pc_130degC_3min_ph11.6_foulard_h2o_wash']

# # plt.figure()
# # for i in select:
# #     plt.plot(data[i][:,0], data_min[i], label=i)
# # plt.plot(data['st_41'][:,0], data_min['st_41'], label= 'st_41')
# # plt.plot(data['kimica_alginate_i5'][:,0], data_min['kimica_alginate_i5'], label= 'kimica_alginate_i5')
# # plt.xlabel(r'wavenumber /$\frac{1}{cm}$')
# # # plt.vlines(3331, 0, 0.35, color='r')
# # plt.ylabel('arb. units')
# # plt.legend()
# # plt.tight_layout()
# # plt.xlim([4000, 500])
# # plt.show()

# # during the mixing and pH adaption, the solution turned yellow. Possibly glyoxal reacted
# # however in ph12 sample, the 50% NaOH was added with a pipette

# #either the glyoxal was already gone in ph11.6 samples, or the surplus of naOh produced the ~1600 peak
# # due to the amount of alkali

# # ph12 war wahrscheinlich nicht ph12 sondern höher



# #%%check if the slow preparation of alkaline glyxoal changed sth.

# # selem = ['3pc_I5_gyoxal_6pc_130degC_3min_ph12_di-h2o_wash', '3pc_I5_gyoxal_6pc_130degC_3min_ph12_HCl_wash', '3pc_I5_gyoxal_6pc_130degC_3min_ph12_NaOH_wash', ]

# # plt.figure()
# # for i in selem:
# #     plt.plot(data[i][:,0], data_min[i], label=i)
# # plt.plot(data['st_41'][:,0], data_min['st_41'], label= 'st_41')
# # plt.plot(data['kimica_alginate_i5'][:,0], data_min['kimica_alginate_i5'], label= 'kimica_alginate_i5')
# # plt.xlabel(r'wavenumber /$\frac{1}{cm}$')
# # # plt.vlines(3331, 0, 0.35, color='r')
# # plt.ylabel('arb. units')
# # plt.legend()
# # plt.tight_layout()
# # plt.xlim([4000, 500])
# # plt.show()


# #%% calculate the ratio of the peak around 1610 and compare to OH (3300 peak)
# # use argmax() to find the maximum of the peaks

# ratio = {}

# for i in data:
#     ratio[i] = {}
#     ratio[i]['oh'] = np.argmax(data[i][418:555], axis=0)
#     ratio[i]['ror'] = np.argmax(data[i][2069:2098], axis=0)
#     ratio[i]['cooh'] = np.argmax(data[i][1630:1690], axis=0)
#     ratio[i]['oh-ratio'] = np.divide(data[i][ratio[i]['cooh'][1] + 1630][1], data[i][ratio[i]['oh'][1] + 418][1])
#     ratio[i]['ror-ratio'] = np.divide(data[i][ratio[i]['cooh'][1] + 1630][1], data[i][ratio[i]['ror'][1] + 2069][1])
#     # ratio[I]

# for i in ratio:
#     print(i, ratio[i]['oh-ratio'])

# # it seems the more alginate is on, the more is adding, but this might just be a skew of the data.
# # somehow the reference also changes little - is there something happening via NaOH swelling?

#  #%% respective wavenumbers

# # 1200 cm**-1 CO schwingung Ester
# # 1425 cm**-1 symmetric COO-
# # 1590 cm**-1 asymmetric carboxy Natriumsalz (COONa)
# # 1645 cm**-1 OH group possibly water
# # 1725 cm**-1 COOH

# # peak at 808 for mannuronic acid residue (Mackie 1971)
# # 

# # therefore most of the signal changes come from incoorporation of carboxy moeities in the cellulose and from incomplete drying or partly from the cellulose matrix (1640)


# #%% ftir banden

# # 1580 +- 40 Valenzschwingungen COO- assym
# # 1390 +- 40 Valenzschwingungen COO- symm
# # 1735 +- 50 Valenzschwingungen C=O(OH) dimer tiefer, monomer 1740
# # 1750-1735 Valenzschwingung saturated aliphatic ester (RCOOR') (https://orgchemboulder.com/Spectroscopy/irtutor/carbonylsir.shtml)

# #for more information : https://www.researchgate.net/post/How-range-peak-IR-spectrum-for-carboxylic-acid-salts-ex-calcium-lactate


# #%% comments from previous evaluation

# # plt.text(3000, 0.1,'sp3 stretch 2850-3000')
# # plt.text(1680, 0.16 , 'C=O stretching unsaturated')
# # plt.text(1200, 0.11, 'C-OH stretch 1200')
# # plt.text(800, 0.09, 'p-disubstituted sp2 aromatic')
# # plt.text(1400, 0.13, 'S=O')
# # plt.text(700, 0.085, 'C=C bending disubstituted (cis)')

# # #undefined
# # plt.text(1600, 0.15, '? (N-H, C=C aromatic...)')
# # plt.text(1000, 0.10, '? (C-F, C=C bending monosubstituted, S=O)')
# # plt.text(1290, 0.12, '? (C-F, COOH-acids)')

# # #others
# # plt.text(2400, 0.07, 'CO2')

# # #halogens
# # plt.text(620, 0.08, 'C-Br stretch 750-500')


# #%% possibly the evaluation of the semi-quantitative approach of cross-linking can be brought from ratios of ether bands (1020-1040) compared to the maximum of the carboxyl band (1590-1620) (suggestion by TB)

# # find some automated routine that searches for a maximum in a given interval
# # and compare the values of interest


