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

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

isaccepted = 1;
iscurrent = 1;
if (isfield(data, 'isSOL')*isfield(data,'noc') == 0) || (data.isSOL == 0) || (data.noc < 1)
    isaccepted = 0;
end
if (isfield(data,'issol') == 0) || (data.issol == 0)
    iscurrent = 0;
end
if isaccepted+iscurrent == 0
    return
end

fig = figure;
pause(.1);

scrsz = get(0,'ScreenSize');

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

set(fig,'Name','Export a computed spectrum to the library','NumberTitle','off','MenuBar','none','Toolbar','none','color','white');

Textfield(1) = uicontrol('Style','text','String','Select spectrum:','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.76, 0.45, 0.2]);

Textfield(2) = uicontrol('Style','text','String','Name:','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.52, 0.45, 0.2]);

Textfield(3) = uicontrol('Style','edit','String','','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.48, 0.52, 0.51, 0.2]);

Textfield(4) = uicontrol('Style','text','String','','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'FontWeight','bold','Position',[0, 0.25, 1, 0.2]);

if iscurrent
    dat = {'current spectrum'};
end
if isaccepted
    for i=1:data.noc
        if exist('dat')
            dat = {dat{:}, [num2str(i),'. accepted spectrum']};
        else
            dat = {[num2str(i),'. accepted spectrum']};
        end
    end
end

popup(1) = uicontrol('Style','popupmenu','String',dat,'HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.48, 0.775, 0.51, 0.2], 'Value',1);

Button(1) = uicontrol('Style','pushbutton','UserData',0,'String','Add to library','HorizontalAlignment','center','Fontsize',1.5*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.51, 0.04, 0.48, 0.2],...
    'Callback',{@innerbuttonadd, Empty_Button, popup, Textfield, fig, calclib, version});

Button(2) = uicontrol('Style','pushbutton','UserData',0,'String','Cancel','HorizontalAlignment','center','Fontsize',1.5*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.04, 0.48, 0.2],...
    'Callback',{@innerbuttoncancel, fig});



function innerbuttonadd(hobject, eventdata, Empty_Button, popup, Textfield, parentfig, calclib, version)
data = get(Empty_Button,'UserData');
name = get(Textfield(3), 'String');
if strcmp(name,'') == 1
    set(Textfield(4), 'String' ,'Export not successful: Please enter a NAME','Foregroundcolor','red');
    return
end

load([calclib,'/FACPACKlibrary.mat']);
no = length(lib);
for i=1:no
    if strcmp(lib(i).name, name) == 1
        set(Textfield(4), 'String' ,'Export not successful: NAME exists already in the library','Foregroundcolor','red');
        return
    end
end
if (isfield(data,'issol') == 1) && (data.issol == 1)
    if get(popup(1),'Value') == 1
        a = data.a;
    else
        a = data.SOL(get(popup(1),'Value')-1).a;
    end
else
    a = data.SOL(get(popup(1),'Value')).a;
end

cln = clock;
cln = [num2str(cln(4),'%2.2d'), ':', num2str(cln(5),'%2.2d'), ':', num2str(round(cln(6)),'%2.2d')];

lib(no+1).name = name;
lib(no+1).x = data.x;
lib(no+1).a = a;
lib(no+1).date = date;
lib(no+1).clock = cln;
lib(no+1).module = 'PGA';
lib(no+1).dataset = [data.PathName, data.FileName];
lib(no+1).version = version;

set(Textfield(4), 'String' ,'Export successful','Foregroundcolor','black');

save([calclib,'/FACPACKlibrary.mat'],'lib');
close(parentfig);


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