function buttonloadgivenspecPGA(hObject, eventdata, Empty_Button, Buttons, Recondition, basichi, basicfsz, sty, calclib, RegSlider, RegText, usmn, usmf)
data = get(Empty_Button,'UserData');

data.isred = 0;
data.issol = 0;
data.islive = 0;
data.livet = [];
set(usmn,'Value',0);
set(usmf,'Value',1);
SliderOnOffPGA(RegSlider, RegText, data, 0);
set(Empty_Button,'UserData',data);

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)
    dat = {'empty'};
else
    for i=1:length(lib)
        dat{i} = lib(i).name;
    end
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);


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, Empty_Button, Buttons, inneremptybutton, fig, Recondition});

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, Empty_Button, Buttons, inneremptybutton, fig, Recondition});

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],...
    'Callback',{@innerbuttonloadfromlib, Empty_Button, inneremptybutton, Button, popup, Textfield, calclib});

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

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


function innerbuttonreset(hobject, eventdata, popup, inneremptybutton, Button, Textfield)
onoffButton(Button,[],2:3);
set(Textfield(4), 'String', '');
if strcmp(get(popup(1),'String'),'empty') == 0
    set(Textfield(5), 'String','New selection in popup menu was done: Please press LOAD FROM LIBRARY next.','ForegroundColor','k');
else
    set(Textfield(5), 'String','Library is empty.','ForegroundColor','k');
end


function innerbuttonadapt(hobject, eventdata, Empty_Button, Buttons, inneremptybutton, fig, Recondition)
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.givena = interp1(datain.x, datain.a, data.x(I(i1:i2)));
data.givenadaptedis = 1;
data.givenadaptedI = [i1 i2];
data.isgivenspec = 1;

set(Recondition(10), 'String', 'online','ForegroundColor','k');
set(Empty_Button,'UserData',data);

if (isfield(data,'isdata')*isfield(data,'isbd')*isfield(data,'issvd')*isfield(data,'isnovctrs') == 1) && (data.isdata*data.isbd*data.issvd*data.isnovctrs == 1)
    set(Buttons(4), 'Enable', 'on'); 
end
close(fig);


function innerbuttonimport(hobject, eventdata, Empty_Button, Buttons, inneremptybutton, fig, Recondition)
data = get(Empty_Button,'UserData');
datain = get(inneremptybutton,'UserData');

data.givena = datain.a;
data.isgivenspec = 1;
data.givenadaptedis = 0;
data.givenadaptedI = [];

set(Recondition(10), 'String', 'online','ForegroundColor','k');
set(Empty_Button,'UserData',data);

if (isfield(data,'isdata')*isfield(data,'isbd')*isfield(data,'issvd')*isfield(data,'isnovctrs') == 1) && (data.isdata*data.isbd*data.issvd*data.isnovctrs == 1)
    set(Buttons(4), 'Enable', 'on'); 
end
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 length(x) == length(dataout.x)
            if size(x) ~= size(dataout.x)
                dataout.x = dataout.x';
            end
            if norm(x-dataout.x,'inf') < 1.E-14
                onoffButton(Button,3,[]);
                set(Textfield(5), 'String','The spectrum is ready for import.','ForegroundColor','r');
            else
                onoffButton(Button,2,[]);
                set(Textfield(5), 'String','The channels does not fit: The spectrum needs to be adapted.','ForegroundColor','r');
            end
        else
            if (min(dataout.x) < max(x)) && (min(x) < max(dataout.x))
                set(Textfield(5), 'String','The channels does not fit: The spectrum needs to be adapted.','ForegroundColor','r');
                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);
