function buttonloadspectra(hObject, eventdata, parentfig, Empty_Button, Buttons, Footnote, AxesAFS, AxesSOL, basicfsz, basichi, calclib, lcs, LCS, sty)

buttonreimport(hObject, eventdata, parentfig, Empty_Button, Buttons, Footnote, AxesAFS, AxesSOL, lcs, LCS, 1);
data = get(Empty_Button,'UserData');

data.isimport = 1;
data.import.s = 0;
data.import.adaptedis = 0;
data.import.adaptedI = [];
data.import.A = [];
data.import.tr = [];

onoffButton(Buttons,[],18);

if data.s ~= 3
    return
end

data = get(Empty_Button,'UserData');

fig = figure;
pause(.1);

scrsz = get(0,'ScreenSize');

set(fig,'Position',[scrsz(3)/4 scrsz(4)*3/8 scrsz(3)/2 scrsz(4)/4]);
pause(.1);

set(fig,'Name','Load spectrum from *.mat file or library','NumberTitle','off','MenuBar','none','Toolbar','none','color','white');

basicfsz = 2*basicfsz;
basichi = 5*basichi;

Textfield(1) = uicontrol('Style','text','String','current data:','HorizontalAlignment','left','Fontsize',basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.5, 0.15, basichi]);

str = ['channels interval [',num2str(min(data.x),'%5.2f'),', ',num2str(max(data.x),'%5.2f'),'],  dimension: ',num2str(length(data.x))];
Textfield(2) = uicontrol('Style','text','String',str,'HorizontalAlignment','left','Fontsize',basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.16, 0.5, 0.73, basichi]);

Textfield(3) = uicontrol('Style','text','String','loaded data:','HorizontalAlignment','left','Fontsize',basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.325, 0.15, basichi]);

Textfield(4) = uicontrol('Style','text','String','','HorizontalAlignment','left','Fontsize',basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.16, 0.325, 89, basichi]);

Textfield(5) = uicontrol('Style','text','String','','HorizontalAlignment','center','Fontsize',basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.2, 0.98, basichi]);

load([calclib,'/FACPACKlibrary.mat']);
if (exist('lib') == 0) || (length(lib) == 0)
    isnempty = 'off';
    dat = {'empty'};
else
    for i=1:length(lib)
        dat{i} = lib(i).name;
    end
    isnempty = 'on';
end

popup(1) = uicontrol('Style','popupmenu','String',dat,'HorizontalAlignment','left','Fontsize',basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.51, 0.83, 0.48, 0.12], 'Value',1, 'Enable', isnempty);


inneremptybutton = uicontrol('Style','pushbutton','Position',[0.02, 0.9, 0.095, basichi],'Visible','off');
datain.isload = 0;
set(inneremptybutton,'UserData',datain);

Button(1) = uicontrol('Style','pushbutton','UserData',0,'String','Cancel','HorizontalAlignment','center','Fontsize',basicfsz,...
    'FontWeight','bold','Units','normalized','FontName',sty,'Position',[0.01, 0.01, 0.32, basichi],'Enable','on',...
    'Callback',{@innerbuttoncancel, fig});

Button(2) = uicontrol('Style','pushbutton','UserData',0,'String','Adapt & import','HorizontalAlignment','center','Fontsize',basicfsz,...
    'FontWeight','bold','Units','normalized','FontName',sty,'Position',[0.34, 0.01, 0.32, basichi],'Enable','off',...
    'Callback',{@innerbuttonadapt, parentfig, Empty_Button, Buttons, inneremptybutton, fig, Footnote, AxesAFS, AxesSOL, lcs, LCS});

Button(3) = uicontrol('Style','pushbutton','UserData',0,'String','Import','HorizontalAlignment','center','Fontsize',basicfsz,...
    'FontWeight','bold','Units','normalized','FontName',sty,'Position',[0.67, 0.01, 0.32, basichi],'Enable','off',...
    'Callback',{@innerbuttonimport, parentfig, Empty_Button, Buttons, inneremptybutton, fig, Footnote, AxesAFS, AxesSOL, lcs, LCS});

Button(4) = uicontrol('Style','pushbutton','UserData',0,'String','Load from *.mat file','HorizontalAlignment','center',...
    'Fontsize',basicfsz,'FontWeight','bold','Units','normalized','FontName',sty,'Position',[0.01, 0.7, 0.48, 0.25],...
    'Callback',{@innerbuttonload, Empty_Button, inneremptybutton, Button, Textfield});

Button(5) = uicontrol('Style','pushbutton','UserData',0,'String','Load from library','HorizontalAlignment','center',...
    'Fontsize',basicfsz,'FontWeight','bold','Units','normalized','FontName',sty,'Position',[0.51, 0.7, 0.48, 0.12], 'Enable', isnempty,...
    'Callback',{@innerbuttonloadfromlib, Empty_Button, inneremptybutton, Button, popup, Textfield, calclib});

set(popup(1), 'Callback',{@innerbuttonreset, inneremptybutton, Button, Textfield});

if (exist('lib') == 0) || (length(lib) == 0)
    set(Button(5), 'Enable', 'off');
end

set(Empty_Button,'UserData',data);


function innerbuttonreset(hobject, eventdata, inneremptybutton, Button, Textfield)
onoffButton(Button,[],2:3);
set(Textfield(4), 'String', '');
set(Textfield(5), 'String','New selection in popup menu was done: Please press LOAD FROM LIBRARY next.','ForegroundColor','k');


function innerbuttonadapt(hobject, eventdata, parentfig, Empty_Button, Buttons, inneremptybutton, fig, Footnote, AxesAFS, AxesSOL, lcs, LCS)
data = get(Empty_Button,'UserData');
datain = get(inneremptybutton,'UserData');

I = 1:size(data.D,2);
if min(data.x) <= min(datain.x)
    i1 = find(data.x >= min(datain.x),1);
else
    i1 = 1;
end
if max(data.x) >= max(datain.x)
    i2 = find(data.x <= max(datain.x),1,'last');
else
    i2 = size(data.D,2);
end

data.import.s = size(datain.a,1);
data.import.A = interp1(datain.x, datain.a, data.x(I(i1:i2)))';
data.import.adaptedis = 1;
data.import.adaptedI = [i1 i2];

for i=1:data.import.s
    t = data.import.A(i,:)*data.V(i1:i2,1:3);
    data.import.tr(i,:) = t(2:3)/t(1);
    data.import.A(i,:) = data.import.A(i,:)/t(1);
end

if data.import.s == 1
    set(Footnote,'String',[' Spectrum import: ', num2str(data.import.s), ' spectrum loaded. '],'ForegroundColor','k');
else
    set(Footnote,'String',[' Spectra import: ', num2str(data.import.s), ' spectra loaded. '],'ForegroundColor','k');
end

data.isimport = 1;
set(Empty_Button,'UserData',data);
onoffButton(Buttons,18,[]);

if (isfield(data,'isExpert1') == 1) && (data.isExpert1 == 1);
    if (isfield(data,'isExpert2') == 1) && (data.isExpert2 == 1)
        plotlockedAFS(parentfig, data, AxesAFS, AxesSOL, lcs, 2);
        hold(AxesAFS,'on');
        if (isfield(data,'isLastClickLockMode') == 1) && (data.isLastClickLockMode == 1)
            plot(AxesAFS, data.LastClickLockMode.FixPoint(1), data.LastClickLockMode.FixPoint(2),'k+','Linewidth',2);
        end
        hold(AxesAFS,'off');
    else
        plotlockedAFS(parentfig, data, AxesAFS, AxesSOL, lcs, 1);
        hold(AxesAFS,'on');
        if (isfield(data,'isLastClickLockMode') == 1) && (data.isLastClickLockMode == 1)
            plot(AxesAFS, data.LastClickLockMode.FixPoint(1), data.LastClickLockMode.FixPoint(2),'ko','Linewidth',2);
        end
        hold(AxesAFS,'off');
    end
else
    plotAFS(parentfig, data, AxesAFS, AxesSOL, lcs, LCS);
    hold(AxesAFS,'on');
    if (isfield(data,'isLastClickLockMode') == 1) && (data.isLastClickLockMode == 1)
        plot(AxesAFS, data.LastClickLockMode.FixPoint(1), data.LastClickLockMode.FixPoint(2),'kx','Linewidth',2);
    end
    hold(AxesAFS,'off');
end

hold(AxesSOL,'on');
for i=1:data.import.s
    plot(AxesSOL, data.x(i1:i2), data.import.A(i,:),'c','Linewidth',1)
end
hold(AxesSOL,'off');
set(AxesSOL, 'Xlim',data.x([1 end]));
xlabel(AxesSOL,'channel');

close(fig);




function innerbuttonimport(hobject, eventdata, parentfig, Empty_Button, Buttons, inneremptybutton, fig, Footnote, AxesAFS, AxesSOL, lcs, LCS)
data = get(Empty_Button,'UserData');
datain = get(inneremptybutton,'UserData');

data.import.s = size(datain.a,1);
data.import.A = datain.a;
data.import.adaptedis = 0;
data.import.adaptedI = [];

for i=1:data.import.s
    t = datain.a(i,:)*data.V(:,1:3);
    data.import.tr(i,:) = t(2:3)/t(1);
    data.import.A(i,:) = data.import.A(i,:)/t(1);
end

if data.import.s == 1
    set(Footnote,'String',[' Spectrum import: ', num2str(data.import.s), ' spectrum loaded. '],'ForegroundColor','k');
else
    set(Footnote,'String',[' Spectra import: ', num2str(data.import.s), ' spectra loaded. '],'ForegroundColor','k');
end

data.isimport = 1;
set(Empty_Button,'UserData',data);
onoffButton(Buttons,18,[]);

if (isfield(data,'isExpert1') == 1) && (data.isExpert1 == 1);
    if (isfield(data,'isExpert2') == 1) && (data.isExpert2 == 1)
        plotlockedAFS(parentfig, data, AxesAFS, AxesSOL, lcs, LCS, 2);
        hold(AxesAFS,'on');
        if (isfield(data,'isLastClickLockMode') == 1) && (data.isLastClickLockMode == 1)
            plot(AxesAFS, data.LastClickLockMode.FixPoint(1), data.LastClickLockMode.FixPoint(2),'k+','Linewidth',2);
        end
        hold(AxesAFS,'off');
    else
        plotlockedAFS(parentfig, data, AxesAFS, AxesSOL, lcs, LCS, 1);
        hold(AxesAFS,'on');
        if (isfield(data,'isLastClickLockMode') == 1) && (data.isLastClickLockMode == 1)
            plot(AxesAFS, data.LastClickLockMode.FixPoint(1), data.LastClickLockMode.FixPoint(2),'ko','Linewidth',2);
        end
        hold(AxesAFS,'off');
    end
else
    plotAFS(parentfig, data, AxesAFS, AxesSOL, lcs, LCS);
    hold(AxesAFS,'on');
    if (isfield(data,'isLastClickLockMode') == 1) && (data.isLastClickLockMode == 1)
        plot(AxesAFS, data.LastClickLockMode.FixPoint(1), data.LastClickLockMode.FixPoint(2),'kx','Linewidth',2);
    end
    hold(AxesAFS,'off');
end

hold(AxesSOL,'on');
for i=1:data.import.s
    plot(AxesSOL, data.x, data.import.A(i,:),'c','Linewidth',1)
end
hold(AxesSOL,'off');
set(AxesSOL, 'Xlim',data.x([1 end]));
xlabel(AxesSOL,'channel');

close(fig);


function innerbuttonloadfromlib(hobject, eventdata, Empty_Button, inneremptybutton, Button, popup, Textfield, calclib)
dataout = get(Empty_Button,'UserData');

load([calclib,'/FACPACKlibrary.mat']);

no = get(popup(1),'Value');

datain.x = lib(no).x;
datain.a = lib(no).a;
datain.isload = 1;

set(Textfield(4), 'String', ['channels interval [',num2str(min(datain.x),'%5.2f'),', ',num2str(max(datain.x),'%5.2f'),'],  dimension: ',num2str(length(datain.x))],...
    'ForegroundColor','k');

if length(datain.x) == length(dataout.x)
    if norm(datain.x-dataout.x,'inf') < 1.E-14
        onoffButton(Button,3,2);
        set(Textfield(5), 'String','The spectrum is ready for import.','ForegroundColor','r');
    else
        onoffButton(Button,2,3);
        set(Textfield(5), 'String','The channels does not fit: The spectrum needs to be adapted.','ForegroundColor','r');
    end
else
    if (min(dataout.x) < max(datain.x)) && (min(datain.x) < max(dataout.x))
        set(Textfield(5), 'String','The channels does not fit: The spectrum needs to be adapted.','ForegroundColor','r');
        onoffButton(Button,2,3);
    else
        set(Textfield(5), 'String','ERROR: The channels does not fit at all.','ForegroundColor','r');
        datain.isload = 0;
    end
end
set(inneremptybutton,'UserData',datain);


function innerbuttonload(hObject, eventdata, Empty_Button, inneremptybutton, Button, Textfield)
dataout = get(Empty_Button,'UserData');

if ispc
    dlm = '\';
else
    dlm = '/';
end

actpwd = pwd;
cd(dataout.PathName);
[FileName, PathName, FilterIndex] = uigetfile('*.mat','Select .mat file including the data Matrix D');
cd(actpwd);

if FilterIndex == 0
    return
end

onoffButton(Button,[],2:3);

load([PathName FileName]);
if ~exist('a') && exist('A')
    a = A;
end
if ~exist('x') && exist('X')
    x = X;
end
if exist('a')*exist('x') == 0
    set(Textfield(4), 'String', 'ERROR: Missing spectrum "a" or channels vector "x".','ForegroundColor','r');
elseif (length(x) ~= size(a,2)) && (length(x) == size(a,1))
    a = a';
end
if (exist('a')*exist('x') == 1)
    if (length(x) == size(a,2))
        set(Textfield(4), 'String', ['channels interval [',num2str(min(x),'%5.2f'),', ',num2str(max(x),'%5.2f'),'],  dimension: ',num2str(length(x))],...
        'ForegroundColor','k');
        
        if x(1) > x(2)
            x = x(end:-1:1);
            a = a(:,end:-1:1);
        end
        if size(x,1) < size(x,2)
            x = x';
        end
        if length(x) == length(dataout.x)
            if norm(x-dataout.x,'inf') < 1.E-14
                onoffButton(Button,3,[]);
                if size(a,1) == 1
                    set(Textfield(5), 'String','The spectrum is ready for import.','ForegroundColor','r');
                else
                    set(Textfield(5), 'String','The spectra are ready for import.','ForegroundColor','r');
                end
            else
                onoffButton(Button,2,[]);
                if size(a,1) == 1
                    set(Textfield(5), 'String','The channels does not fit: The spectrum needs to be adapted.','ForegroundColor','r');
                else
                    set(Textfield(5), 'String','The channels does not fit: The spectra need to be adapted.','ForegroundColor','r');
                end
            end
        else
            if (min(dataout.x) < max(x)) && (min(x) < max(dataout.x))
                if size(a,1) == 1
                    set(Textfield(5), 'String','The channels does not fit: The spectrum needs to be adapted.','ForegroundColor','r');
                else
                    set(Textfield(5), 'String','The channels does not fit: The spectra need to be adapted.','ForegroundColor','r');
                end
                onoffButton(Button,2,[]);
            else
                set(Textfield(5), 'String','ERROR: The channels does not fit at all.','ForegroundColor','r');
            end
        end
        
        datain.a = a;
        datain.x = x;
        datain.isload = 1;
        
        set(inneremptybutton,'UserData',datain);
        return;
    else
        set(Textfield(4), 'String', ['ERROR: length(x): ',num2str(length(x),'%d'),' does not fit with size(a): ',num2str(size(a),'%d,%d')'.'],...
        'ForegroundColor','r');
    end
end
onoffButton(Button,2:3,[]);
datain.isloadin = 0;
set(inneremptybutton,'UserData',datain);


function innerbuttoncancel(hobject, eventdata, fig)
close(fig);
