function [] = ViewROITaxaList(action, varargin)
%Use to generate list showing the Classified Taxa of all ROIs within the
%specified taxon, day, and hour.

if nargin<1
    ViewROITaxaListcontrol;
    PrepROITaxaList;
    DisplayROITaxaList
else
    feval(action,varargin{:});
end

return;

function ViewROITaxaListcontrol
h=gcbf;
h1=findobj(h,'Tag','PopupMenu Taxon');
dummystring=get(h1,'String');v=get(h1,'value');
a=(2:size(dummystring,1))';
taxalist=dummystring(a,:);
taxon=deblank(dummystring(v,:));
h1=findobj(h,'Tag','PopupMenu Yearday');
dummystring=get(h1,'String');v=get(h1,'value');
day=deblank(dummystring(v,:));

redgreenblue=[0.2157 .6353 .6353]*1.2;

h0 = figure('Units','normalized', ...
	'Color',[redgreenblue]*1.1, ...
	'Colormap','default', ...
	'MenuBar','none', ...
	'Name','VPR   View AutoID Control', ...
	'NumberTitle','off', ...
	'PaperPosition',[18 180 576 432], ...
	'PaperUnits','points', ...
	'Position',[0.005 0.05 0.25 0.9], ...
	'Tag','VPR ViewROITaxaList', ...
	'ToolBar','none');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'Position',[0.1 0.95 0.8 0.05], ...
    'Fontunits','normalized',...
    'Fontsize',0.4, ...
    'fontweight','normal', ...
    'String',['Viewing: ' taxon ' d' day], ...
	'Style','text', ...
	'Tag','ViewROITaxaList Title Text', ...
    'TooltipString','This is the taxon, day, and hour currently selected');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'Position',[0.1 0.925 0.8 0.05], ...
    'Fontunits','normalized',...
    'Fontsize',0.4, ...
    'fontweight','normal', ...
    'String','Legend: ROI Index | ROI Name | Taxa of ROI', ...
	'Style','text', ...
	'Tag','ViewROITaxaList Control Text', ...
    'TooltipString',['From Left to Right:' char(10),...
                     'Index of ROI in taxon (above) AutoID file' char(10),...
                     'ROI File Name' char(10),...
                     'Taxa the ROI is sorted into (Taxa Legend can be ' char(10),...
                     'obtained by hovering over the list below)']);
taxalegend.Help1='List of ROIs within specified Taxon, day, and hour';
taxalegend.Help2='with the AutoID taxa the ROI is sorted into       ';
taxalegend.TaxaLegend=blanks(50);
for i=1:size(taxalist,1)
    eval(['taxalegend.',taxalist(i,1:3),'=[''',taxalist(i,:),''',',blanks(50-size(taxalist,2)),'];']);
end
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
    'BackgroundColor',[redgreenblue]*1.1, ...
    'ListboxTop',1, ...
	'position',[0.05 0.13 0.9 0.82],...
	'HorizontalAlign','left',...
    'Fontunits','normalized',...
    'Fontsize',0.02, ...
    'String',['No ROIs Found'], ...
	'Min',1, ...
    'Max',1, ...
    'Value',1, ...
	'Style','listbox', ...
    'Tag','ViewROITaxaList ROI Taxa List', ...
    'TooltipString',[evalc('disp(taxalegend)')]);
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'Position',[0.65   0.01   0.25    0.05], ...
    'Fontunits','normalized',...
    'Fontsize',0.3, ...
    'String','Refresh', ...
    'callback','ViewROITaxaList Refresh', ...
	'Style','pushbutton', ...
	'Tag','ViewROITaxaList Control Text', ...
    'TooltipString',['Refresh list to reflect changes made using' char(10) 'View AutoID ROIs and/or View Duplicated ROIs']);
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'Position',[0.35   0.01   0.25    0.05], ...
    'Fontunits','normalized',...
    'Fontsize',0.3, ...
    'String','Next Hour', ...
    'callback','ViewROITaxaList NextHour', ...
	'Style','pushbutton', ...
	'Tag','ViewROITaxaList Control Text', ...
    'TooltipString','View next hour');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'Position',[0.05    0.01    0.25    0.05], ...
    'Fontunits','normalized',...
    'Fontsize',0.3, ...
    'String','Close List', ...
    'callback','close(gcbf);', ...
	'Style','pushbutton', ...
	'Tag','ViewROITaxaList Control Text', ...
    'TooltipString','Close List');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'Position',[0.15   0.07   0.65    0.05], ...
    'Fontunits','normalized',...
    'Fontsize',0.3, ...
    'String','Delete Duplicate ROIs within Taxon', ...
    'callback','ViewROITaxaList DeleteDuplicates', ...
	'Style','pushbutton', ...
	'Tag','ViewROITaxaList Control Text', ...
    'TooltipString',['Delete ROI Names that appear more than once in this taxon' char(10) '(i.e., A single ROI appears multiple times due to accidental additional duplication)']);

function PrepROITaxaList;
h=findobj('Tag','VPR View AutoID'); 
if ~isempty(h)
    h1=findobj(h,'Tag','PopupMenu AutoID Drive');
    dummystring=get(h1,'String');v=get(h1,'value');
    disc=deblank(dummystring(v,:));
    h1=findobj(h,'Tag','PopupMenu AutoID Folder');
    dummystring=get(h1,'String');v=get(h1,'value');
    folder=deblank(dummystring(v,:));
    autoidbasepath=[disc folder];
    h1=findobj(h,'Tag','PopupMenu Cruise');
    dummystring=get(h1,'String');v=get(h1,'value');
    cruise=deblank(dummystring(v,:));
    h1=findobj(h,'Tag','PopupMenu Classifier');
    dummystring=get(h1,'String');v=get(h1,'value');
    classifier=deblank(dummystring(v,:));
    clfid=classifier;
    h1=findobj(h,'Tag','PopupMenu Taxon');
    dummystring=get(h1,'String');v=get(h1,'value');
    taxon=deblank(dummystring(v,:));
    a=(2:size(dummystring,1))';
    taxalist=dummystring(a,:);
    h1=findobj(h,'Tag','PopupMenu Yearday');
    dummystring=get(h1,'String');v=get(h1,'value');
    day=deblank(dummystring(v,:));
    h1=findobj(h,'Tag','PopupMenu Hour');
    start_file=get(h1,'value');
    hours=get(h1,'string');
    start_hour=deblank(hours(start_file,:));%only removes trailing blanks
    if start_hour(1)==' ',start_hour(1)='0';end%replace leading blank w/ 0
        
    autoid_dir = [autoidbasepath,filesep cruise,filesep,'autoid',filesep];
    taxa_dir=[autoid_dir,taxon,filesep];
    aid_dir = [taxa_dir,'aid',filesep,clfid, 'aid.d', day, '.h*'];
    aid_files=dosdir3(aid_dir);
    aindx=find(aid_files==10);
    numfile = length(aindx);
    for k=1:numfile, 
        if str2num(aid_files(aindx(k)-2:aindx(k)-1))==str2num(start_hour),
            start_file=k;
        end
    end
    h1=findobj('tag','ViewROITaxaList ROI Taxa List');
    set(h1,'userdata',{start_file-1;numfile;taxa_dir;aid_files;autoid_dir;clfid;cruise;day;taxon;taxalist;[]});
end

function DisplayROITaxaList;

h1=findobj('tag','ViewROITaxaList ROI Taxa List');
dumvar=get(h1,'userdata');
j=dumvar{1}+1;
numfile=dumvar{2};
taxa_dir=dumvar{3};
aid_files=dumvar{4};
autoid_dir=dumvar{5};
clfid=dumvar{6};
cruise=dumvar{7};
day=dumvar{8};
taxon=dumvar{9};
taxalist=dumvar{10};

list=[];

if j<=numfile
    aidfile = nthfile(aid_files,j);
    d=dir([taxa_dir,'aid',filesep,aidfile]);
    if d.bytes~=0
        imfiles = loadname([taxa_dir,'aid',filesep,aidfile]);
        n=size(taxalist,1);
        for jj=1:size(imfiles,1)%Generate Names in list
            [x,roiname,z]=fileparts(imfiles(jj,:));
            if(floor(log10(jj)+1))==1,imnum=[int2str(jj),'     '];end
            if(floor(log10(jj)+1))==2,imnum=[int2str(jj),'    '];end
            if(floor(log10(jj)+1))==3,imnum=[int2str(jj),'   '];end
            if(floor(log10(jj)+1))==4,imnum=[int2str(jj),'  '];end
            if(floor(log10(jj)+1))==5,imnum=[int2str(jj),' '];end %Add more if there are more than 99,999 ROIs in one run
            
            listtemp=[imnum,roiname,blanks(n*4+1)];
            list=[list;listtemp];
            clear imnum listtemp x z;
        end
    end
    for n=1:size(taxalist,1)
        
        duptaxon=deblank(taxalist(n,:));
        duptaxa_dir=[autoid_dir,duptaxon,filesep];
        dupaid_dir = [duptaxa_dir,'aid',filesep,clfid, 'aid.d', day, '.h*'];
        dupaid_files=dosdir3(dupaid_dir);
        
        %Ensure target taxon has required time stamp.
        check=0;
        for jj=1:length(find(dupaid_files==10))
            dupaidfile=nthfile(dupaid_files,jj);
            check=check+strcmp(dupaidfile,aidfile);
        end
    
        if check>0
            e=dir([duptaxa_dir,'aid',filesep,aidfile]);
            if d.bytes~=0 && e.bytes~=0 %aidfile has data
                dupimfiles = loadname([duptaxa_dir,'aid',filesep,aidfile]);
                index=(6+length(roiname)+n+3*(n-1))+(1:3);
                for jj=1:size(imfiles,1)
                    for kk=1:size(dupimfiles,1)
                        if strcmp(imfiles(jj,:),dupimfiles(kk,:))
                            list(jj,index)=taxalist(n,1:3);
                        end
                    end 
                end
            end
        end
    end
    dumvar{1}=j;
    dumvar{11}=list;
    h1=findobj('tag','ViewROITaxaList ROI Taxa List');
    set(h1,'userdata',dumvar);
    h1=findobj('Tag','ViewROITaxaList ROI Taxa List');
    set(h1,'String',dumvar{11});
    set(h1,'Max',size(list,1));
    h1=findobj('Tag','ViewROITaxaList Title Text');
    set(h1,'String',['Viewing: ' taxon ' ' aidfile(end-7:end)]);
else
    h1=findobj('Tag','ViewROITaxaList ROI Taxa List');
    set(h1,'String','No more ROIs since it is past max hour');
end

function NextHour
h1=findobj('Tag','ViewROITaxaList ROI Taxa List');
set(h1,'Value',1);
DisplayROITaxaList;

function Refresh
h1=findobj('tag','ViewROITaxaList ROI Taxa List');
dumvar=get(h1,'userdata');
j=dumvar{1}-1;
dumvar{1}=j;
set(h1,'userdata',dumvar);
DisplayROITaxaList;

function DeleteDuplicates
h1=findobj('tag','ViewROITaxaList ROI Taxa List');
dumvar=get(h1,'userdata');
j=dumvar{1};
numfile=dumvar{2};
taxa_dir=dumvar{3};
aid_files=dumvar{4};
autoid_dir=dumvar{5};
taxon=dumvar{9};

if j<=numfile
    aidfile = nthfile(aid_files,j);
    aidmeafile=[aidfile(1:length(aidfile)-9) '.mea' aidfile(length(aidfile)-8:length(aidfile))];
    d=dir([taxa_dir,'aid',filesep,aidfile]);
    if d.bytes~=0
        imfiles=loadname([taxa_dir,'aid',filesep,aidfile]);
        %mea=loadname([taxa_dir,'aidmea',filesep,aidmeafile]);
        index=1:size(imfiles,1);
        n=ones(1,length(index));
        for jj=1:size(imfiles,1)-1
            tempindex=index(jj+1:end);
            for kk=tempindex
                if strcmp(imfiles(jj,:),imfiles(kk,:))
                    n(jj)=0;
                end
            end
            clear tempindex;
        end
    end
    
    imfiles_out=imfiles(logical(n),:);
    %mea_out=mea(logical(n),:);
    
    fid=fopen([taxa_dir 'aid' filesep aidfile],'w');
    fprintf(fid,'%c',imfiles_out');
    fclose(fid);
    
    %fid=fopen([taxa_dir 'aidmea' filesep aidmeafile],'w');
    %fprintf(fid,'%c',mea_out');
    %fclose(fid);
end

Refresh;

