function fig = buttondeletespectrumPGA(hObject, eventdata, Empty_Button, Buttons, AxesSOL, AxesAOM, RegSlider, RegText, lcs, LCS)

data = get(Empty_Button,'UserData');
fig = figure(103);
set(fig,'Name','Choose spectra to be cleared','NumberTitle','off','MenuBar','none','Toolbar','figure','color','white');
pause(.1);
set(0,'DefaultAxesTitleFontWeight','normal');
scrsz = get(0,'ScreenSize');

set(fig,'Position',[1/10*scrsz(3:4) 2/3*scrsz(3:4)]);

if ispc
    basicfsz = 4.5;
    basichi = 0.025;
else
    basicfsz = 5.5;
    basichi = 0.025;
end

Styles = {'bitstream charter';'clean';'Courier';'courier 10 pitch';'frank ruehl';'Helvetica';'Helvetica-Narrow';'itc bookman';'urw bookman l'};
sty = Styles{6};
bgc = 0.95*ones(3,1);
bgw = [1, 1, 1];

AxesACC = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.1, 0.575, 0.55, 0.375]);
plot(AxesACC,1,0,'w');

AxesREJ = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.1, 0.075, 0.55, 0.375]);
plot(AxesREJ,1,0,'w');

lgntxt = {};
oldtxt = {};
Ages = [];
for i=1:data.noc
    Ages = [Ages; data.SOL(i).a];
end
for i=1:data.noc
    if data.SOL(i).xbd(1) == data.SOL(i).xbd(2)
        oldtxt{i} = num2str(round(data.SOL(i).xbd(1)));
    else
%         oldtxt{i} = [num2str(round(data.SOL(i).xbd(1))),' - ',num2str(round(data.SOL(i).xbd(2)))];
        [tey,idmax] = max((i-1)*data.step+data.SOL(i).a(data.SOL(i).ifir:data.SOL(i).ilas));
        tex = data.x(data.SOL(i).ifir+idmax)+(max(data.x)-min(data.x))/100;
        oldtxt{i} = num2str(round(tex));
    end
    lgntxt{i} = oldtxt{i};
end

cov_level = 0.9;
no = size(Ages,1);
cov = zeros(no,no);
for i=1:no-1
    for j=i+1:no
        cov(i,j) = Ages(i,:)*Ages(j,:)'/norm(Ages(i,:),2)/norm(Ages(j,:),2);
        cov(j,i) = cov(i,j);
    end
end

if data.noc > 0
    TextSelect(1) = uicontrol('Style','text','String','current spectra','HorizontalAlignment','left','Fontsize',2.2*basicfsz,'BackgroundColor','white',...
    'FontWeight','bold','Units','normalized','FontName',sty,'Position',[0.675, 0.85, 0.2, 2*basichi]);
    
    TextSelect(4) = uicontrol('Style','text','String','keep','HorizontalAlignment','left','Fontsize',1.75*basicfsz,'BackgroundColor','white',...
        'Units','normalized','FontName',sty,'Position',[0.79, 0.835, 0.05, 1.5*basichi]);
end

box = [];
i = 0;
for i=1:data.noc
%     TextOld(i) = uicontrol('Style','text','String',['(',num2str(i),') ',oldtxt{i}],'HorizontalAlignment','left','Fontsize',1.5*basicfsz,'BackgroundColor','white',...[0.695, 0.818-i*0.03, 0.08, 2.0*basichi][0.795, 0.835-i*0.03, 0.03, 1.75*basichi]
    TextOld(i) = uicontrol('Style','text','String',oldtxt{i},'HorizontalAlignment','left','Fontsize',1.75*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.695, 0.818-(data.noc-i)*0.03, 0, 0]);
    set(TextOld(i),'Position',get(TextOld(i),'Position')+get(TextOld(i),'Extent'))
    
    box(i) = uicontrol('Style','checkbox','String','','Units','normalized','BackgroundColor','white',...
        'Position',[0.795, 0.835-(data.noc+1-i)*0.03, 0.03, 1.75*basichi],'Fontsize',1.8*basicfsz,'FontWeight','bold','FontName',sty,...
        'HandleVisibility','off','Value',1);
end

subploPGA(hObject, eventdata, AxesACC, AxesREJ, data, lgntxt, lcs, box);

set(box,'Callback',{@subploPGA, AxesACC, AxesREJ, data, lgntxt, lcs, box});

Button(1) = uicontrol('Style','pushbutton','UserData',0,'String','Apply','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.7, 0.125, 0.13, 1.5*basichi],...
    'TooltipString', sprintf('Accept the results and delete marked ones.'),...
    'Callback',{@subbuttonacceptPGA, Empty_Button, Buttons, AxesSOL, AxesAOM, RegSlider, RegText, data, box, fig, lcs, LCS});

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

function subploPGA(hObject, eventdata, AxesACC, AxesREJ, data, lgntxt, lcs, box)
lgnSOL = {};
lgnREJ = {};
i1 = 0;
i2 = 0;
for i=1:data.noc
    if get(box(i),'Value')
        plot(AxesACC, data.x, i1*data.step+data.SOL(i).a, 'color', lcs(mod(i-1,size(lcs,1))+1,:), 'Linewidth',1);
        hold(AxesACC,'on');
        plot(AxesACC, data.x([1 end]), (i1)*data.step*[1 1], 'k:','Linewidth',0.5);
        if data.SOL(i).xbd(1) < data.SOL(i).xbd(2)
            plot(AxesACC, data.x(data.SOL(i).ifir:data.SOL(i).ilas), i1*data.step+data.SOL(i).a(data.SOL(i).ifir:data.SOL(i).ilas), 'color', lcs(mod(i-1,size(lcs,1))+1,:),'Linewidth',5,'markersize',12);
            [tey,idmax] = max(i1*data.step+data.SOL(i).a(data.SOL(i).ifir:data.SOL(i).ilas));
            tex = data.x(data.SOL(i).ifir+idmax)+(max(data.x)-min(data.x))/100;
            lgntxt{i} = num2str(round(tex));
        else
            plot(AxesACC, data.x(data.SOL(i).ifir), i1*data.step+data.SOL(i).a(data.SOL(i).ifir),'*', 'color', lcs(mod(i-1,size(lcs,1))+1,:),'Linewidth',4,'markersize',12);
            lgntxt{i} = num2str(round(data.SOL(i).xbd(1)));

            if abs(data.x(data.SOL(i).ifir)-min(data.x))*0.1 < abs(data.x(data.SOL(i).ifir)-max(data.x))*0.9
                tex = data.x(data.SOL(i).ifir)+(max(data.x)-min(data.x))/100;
            else
                tex = data.x(data.SOL(i).ifir)-(max(data.x)-min(data.x))/25;
            end
            tey = i1*data.step+data.SOL(i).a(data.SOL(i).ifir);
        end
        text(AxesACC,tex,tey,lgntxt{i});
        autodetect = detectallpeaks(data.x,data.SOL(i).a,1,data.SOL(i).ifir,data.SOL(i).ilas);
        for l=1:length(autodetect)
            text(AxesACC,data.x(autodetect(l)),i1*data.step+data.SOL(i).a(autodetect(l)),num2str(round(data.x(autodetect(l)))));
        end
        i1 = i1+1;
    else
        plot(AxesREJ, data.x, i2*data.step+data.SOL(i).a, 'k', 'Linewidth',1);
        hold(AxesREJ,'on');
        plot(AxesREJ, data.x([1 end]), (i2)*data.step*[1 1], 'k:','Linewidth',0.5);
        if data.SOL(i).xbd(1) < data.SOL(i).xbd(2)
            plot(AxesREJ, data.x(data.SOL(i).ifir:data.SOL(i).ilas), i2*data.step+data.SOL(i).a(data.SOL(i).ifir:data.SOL(i).ilas), 'color', lcs(mod(i-1,size(lcs,1))+1,:),'Linewidth',5,'markersize',12);
            [tey,idmax] = max(i2*data.step+data.SOL(i).a(data.SOL(i).ifir:data.SOL(i).ilas));
            tex = data.x(data.SOL(i).ifir+idmax)+(max(data.x)-min(data.x))/100;
            lgntxt{i} = num2str(round(tex));
        else
            plot(AxesREJ, data.x(data.SOL(i).ifir), i2*data.step+data.SOL(i).a(data.SOL(i).ifir),'*', 'color', lcs(mod(i-1,size(lcs,1))+1,:),'Linewidth',4,'markersize',12);
            lgntxt{i} = num2str(round(data.SOL(i).xbd(1)));

            if abs(data.x(data.SOL(i).ifir)-min(data.x))*0.1 < abs(data.x(data.SOL(i).ifir)-max(data.x))*0.9
                tex = data.x(data.SOL(i).ifir)+(max(data.x)-min(data.x))/100;
            else
                tex = data.x(data.SOL(i).ifir)-(max(data.x)-min(data.x))/25;
            end
            tey = i2*data.step+data.SOL(i).a(data.SOL(i).ifir);
        end
        text(AxesREJ,tex,tey,lgntxt{i});
        autoold = detectallpeaks(data.x,data.SOL(i).a,1,data.SOL(i).ifir,data.SOL(i).ilas);
        for l=1:length(autoold)
            text(AxesREJ,data.x(autoold(l)),i2*data.step+data.SOL(i).a(autoold(l)),num2str(round(data.x(autoold(l)))));
        end
        i2 = i2+1;
    end
end
hold(AxesACC,'off');
hold(AxesREJ,'off');
if i1 == 0
%     plot(AxesACC,1,0,'w');
    plot(AxesACC,data.x([1 end]),0*[1,1],'w');
end
if i2 == 0
%     plot(AxesREJ,1,0,'w');
    plot(AxesREJ,data.x([1 end]),0*[1,1],'w');
end

axis(AxesACC,'tight');
yl = get(AxesACC,'ylim');
title(AxesACC,'Accepted profiles');
xlabel(AxesACC,'channel');
set(AxesACC,'Fontsize',12,'ylim',yl,'ytick',[]);
axis(AxesACC,'tight');

axis(AxesREJ,'tight');
yl = get(AxesREJ,'ylim');
title(AxesREJ,'Profiles to be deleted');
xlabel(AxesREJ,'channel');
set(AxesREJ,'Fontsize',12,'ylim',yl,'ytick',[]);
axis(AxesREJ,'tight');

function subbuttonacceptPGA(hObject, eventdata, Empty_Button, Buttons, AxesSOL, AxesAOM, RegSlider, RegText, data, box, parentfig, lcs, LCS)
keep = [];
for i=1:length(box)
    if get(box(i),'Value') == 1
        keep = [keep;i];
    end
end

if isempty(keep)
    data.isSOL = 0;
    data.noc = 0;
    data.SOL = [];
    
    set(RegSlider(9), 'Enable', 'off');
    set(RegText(9), 'Enable', 'off');
    set(RegText(29), 'Enable', 'off');
    
    onoffButton(Buttons,[],[7:10 12]);
    
    plot(AxesSOL,0,0,'w');
    set(AxesSOL, 'XTick', [], 'YTick', []);
    
    plot(AxesAOM(2),0,0,'w');
    set(AxesAOM(2), 'XTick', [], 'YTick', []);
    
    plot(AxesAOM(3),0,0,'w');
    set(AxesAOM(3), 'XTick', [], 'YTick', []);
else
    SOLnew = [];
    for i=1:data.noc
        if ismember(i,keep)
            SOLnew = [SOLnew data.SOL(i)];
%             plot(AxesAOM(2), data.x, data.SOL(i).a, 'color', lcs(mod(i-1,size(lcs,1))+1,:),'Linewidth',1);
%             hold(AxesAOM(2), 'on');
        end
    end
%     hold(AxesAOM(2), 'off');
%     axis(AxesAOM(2),'tight');
    data.SOL = SOLnew;
%     yl = get(AxesAOM(2),'ylim');
%     yl = [min(0,yl(1)), yl(2)*1.1];
%     set(AxesAOM(2),'ylim',yl, 'XTick', [], 'YTick', []);

    lc = [];
    lcf = [];
    yl = [inf, -inf];
    for i=1:length(keep)
        lc(i,:) = lcs(mod(i-1,size(lcs,1))+1,:);
        lcf(i,:) = LCS(mod(i-1,size(LCS,1))+1,:);
        yl = [min(yl(1), min(data.SOL(i).a)), max(yl(2), max(data.SOL(i).a))];
    end
    yl = [min(0,yl(1)), yl(2)*1.1];
%
    for i=1:length(keep)
        plot(AxesAOM(2), data.x, data.SOL(i).a, 'color', lcs(mod(i-1,size(lcs,1))+1,:),'Linewidth',1);
        hold(AxesAOM(2), 'on');
    end
    hold(AxesAOM(2), 'off');
    axis(AxesAOM(2),'tight');
    yl = get(AxesAOM(2),'ylim');
    yl = [min(0,yl(1)), yl(2)*1.1];
    set(AxesAOM(2),'ylim',yl, 'XTick', [], 'YTick', []);
%
    axes(AxesSOL);
%     for i=1:length(keep)
%         fill(data.SOL(i).xbd([1 2 2 1 1]), yl([1 1 2 2 1]), lcf(i,:));
%         hold(AxesSOL, 'on');
%     end
    for i=1:length(keep)
%         plot(AxesSOL, data.x, data.SOL(i).a, 'color', lc(i,:),'Linewidth',2);
        plot(AxesSOL, data.x([1 end]), (i-1)*data.step*[1 1], 'k:','Linewidth',0.5);
        hold(AxesSOL, 'on');
        plot(AxesSOL, data.x, (i-1)*data.step+data.SOL(i).a, 'color', lc(i,:),'Linewidth',2);
        if data.SOL(i).xbd(1) < data.SOL(i).xbd(2)
            plot(AxesSOL, data.x(data.SOL(i).ifir:data.SOL(i).ilas), (i-1)*data.step+data.SOL(i).a(data.SOL(i).ifir:data.SOL(i).ilas), 'color', lc(i,:),'Linewidth',5,'markersize',12);
            [tey,idmax] = max((i-1)*data.step+data.SOL(i).a(data.SOL(i).ifir:data.SOL(i).ilas));
            tex = data.x(data.SOL(i).ifir+idmax)+(max(data.x)-min(data.x))/100;
            lgntxt{i} = num2str(round(tex));
        else
            plot(AxesSOL, data.x(data.SOL(i).ifir), (i-1)*data.step+data.SOL(i).a(data.SOL(i).ifir),'*', 'color', lc(i,:),'Linewidth',4,'markersize',12);
            lgntxt{i} = num2str(round(data.SOL(i).xbd(1)));

            if abs(data.x(data.SOL(i).ifir)-min(data.x))*0.1 < abs(data.x(data.SOL(i).ifir)-max(data.x))*0.9
                tex = data.x(data.SOL(i).ifir)+(max(data.x)-min(data.x))/100;
            else
                tex = data.x(data.SOL(i).ifir)-(max(data.x)-min(data.x))/25;
            end
            tey = (i-1)*data.step+data.SOL(i).a(data.SOL(i).ifir);
        end
        text(AxesSOL,tex,tey,lgntxt{i});
        autodetect{i} = detectallpeaks(data.x,data.SOL(i).a,1,data.SOL(i).ifir,data.SOL(i).ilas);
        for l=1:length(autodetect{i})
            text(AxesSOL,data.x(autodetect{i}(l)),(i-1)*data.step+data.SOL(i).a(autodetect{i}(l)),num2str(round(data.x(autodetect{i}(l)))));
        end
    end
    hold(AxesSOL, 'off');
    axis(AxesSOL,'tight');
    yl = get(AxesSOL,'ylim');
    set(AxesSOL, 'Xlim', [min(data.x), max(data.x)], 'ylim',yl+[0 0.1],'ytick',[]);
    xlabel(AxesSOL,'channel');
    title(AxesSOL, 'All spectra');
end

data.noc = length(keep);
set(Empty_Button,'UserData',data);
if ~isempty(keep)
    buttoncomputeCPGA([], [], Empty_Button, Buttons, [], AxesAOM, lcs, RegSlider, RegText)
end
close(parentfig);

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