function fig = PLbuild_classifier(action, varargin)
% This is the machine-generated representation of a Handle Graphics object
% and its children.  Note that handle values may change when these objects
% are re-created. This may cause problems with any callbacks written to
% depend on the value of the handle at the time the object was saved.
% This problem is solved by saving the output as a FIG-file.
%
% To reopen this object, just type the name of the M-file at the MATLAB
% prompt. The M-file and its associated MAT-file must be on your path.
% 
% NOTE: certain newer features in MATLAB may not have been saved in this
% M-file due to limitations of this format, which has been superseded by
% FIG-files.  Figures which have been annotated using the plot editor tools
% are incompatible with the M-file/MAT-file format, and should be saved as
% FIG-files.

if nargin<1,
  action='InitializePLbuild_classifier';
end;

feval(action,varargin{:});
return;

%%%
%%%   Sub-function - InitializePLbuild_classifier
%%%

function InitializePLbuild_classifier()

load PLbuild_classifier

h0 = figure('Units','normalized', ...
	'Color',[0 0.9 0.9], ...
	'Colormap',mat0, ...
	'FileName','C:\plgui\mygui\PLbuild_classifier.m', ...
	'Name','VPR: Build Classifier', ...
	'MenuBar','none', ...
	'NumberTitle','off', ...
	'PaperPosition',[18 180 576 432], ...
	'PaperUnits','points', ...
	'Position',[0.01 .1 .8 .8], ...
	'Tag','VPR Manual Sort', ...
	'ToolBar','none');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[0 0.95 0.95], ...
	'ListboxTop',0, ...
	'Position',[0.02 0.11 0.9 0.74], ...
	'Style','frame', ...
	'Tag','Frame1');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[0 0.9 0.9], ...
	'FontWeight','bold', ...
    'ListboxTop',0, ...
	'Position',[0.0195    0.8880    0.1002    0.0355], ...
	'String','Base Path', ...
	'Style','text', ...
	'Tag','StaticText1');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[0 0.9 0.9], ...
	'FontWeight','bold', ...
	'ListboxTop',0, ...
	'Position',[0.2043    0.8880    0.1002    0.0355], ...
	'String','Cruise', ...
	'Style','text', ...
	'Tag','StaticText1');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[0 0.9 0.9], ...
	'FontWeight','bold', ...
	'ListboxTop',0, ...
	'Position',[0.3794    0.8880    0.1002    0.0342], ...
	'String','VPR Tow', ...
	'Style','text', ...
	'Tag','StaticText1');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[0 0.9 0.9], ...
	'FontWeight','bold', ...
	'ListboxTop',0, ...
	'Position',[0.582    0.8880    0.1002    0.0342], ...
	'String','Day', ...
	'Style','text', ...
	'Tag','StaticText1');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[1 1 1], ...
	'Callback','PLbuild_classifier GetCruises', ...
	'FontSize',12, ...
	'HorizontalAlignment','left', ...
	'ListboxTop',0, ...
	'Position',[0.1167    0.9    0.0885    0.03], ...
	'String','c:\data', ...
	'Style','edit', ...
	'Tag','EditText Disc', ...
   'TooltipString','Enter base path and press Return (= Enter)');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[0 0.95 0.95], ...
	'Callback','close(gcbf)', ...
	'FontWeight','bold', ...
	'ListboxTop',0, ...
	'Position',[0.7 0.04 0.06 0.04], ...
	'String','EXIT', ...
	'Tag','Pushbutton1', ...
	'TooltipString','Exit this window');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[0 0.95 0.95], ...
	'Callback','sort_help', ...
	'FontWeight','bold', ...
	'ListboxTop',0, ...
	'Position',[0.55 0.04 0.06 0.04], ...
	'String','HELP', ...
	'Tag','Pushbutton Help', ...
	'TooltipString','Help for Focus Detection');

GetCruises(gcf);
if nargout > 0, fig = h0; end

function GetCruises(fig)

if nargin<1,
   h=gcbf;
else
   h=fig;
end;

if ~isempty(h)
  %delete existing popups
  h1=findobj(h,'Tag','PopupMenu Cruise'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Tow'); delete(h1);
  h1=findobj(h,'Tag','StaticText Day'); delete(h1);
  h1=findobj(h,'Tag','Listbox Hours'); delete(h1);
  h1=findobj(h,'Tag','StaticText TRDays'); delete(h1);
  h1=findobj(h,'Tag','Listbox TRHours'); delete(h1);
  %find cruises
  h1=findobj(h,'Tag','EditText Disc');
  disc=get(h1,'String');
  cruises=dir(disc);
  discarray=str2mat(cruises.name);
  if size(discarray,1)>2,    
     discarray=discarray(3:size(discarray,1),:);
     discarray=str2mat('Select',discarray);
     h1 = uicontrol('Parent',h, ...  
          'Units','normalized', ...
          'BackgroundColor',[1 1 1], ...
          'Callback','PLbuild_classifier GetTows', ...
          'ListboxTop',0, ...
          'Position',[0.282 0.89 0.0720 0.035], ...
          'String',discarray, ...
          'max',size(discarray,1), ...
          'min',1, ...
          'Style','popupmenu', ...
          'Tag','PopupMenu Cruise', ...
          'TooltipString','Select Cruise', ...
          'Value',1);
  end
end


function GetTows()

h=gcbf;
if ~isempty(h)
  %delete existing tow/day/hour objects
  h1=findobj(h,'Tag','PopupMenu Tow'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Day'); delete(h1);
  h1=findobj(h,'Tag','StaticText Hours'); delete(h1);
  h1=findobj(h,'Tag','Listbox Taxa'); delete(h1);
  h1=findobj(h,'Tag','StaticText Taxa'); delete(h1);
  h1=findobj(h,'Tag','Listbox NumRIOs'); delete(h1);
  %find tows
  h1=findobj(h,'Tag','EditText Disc');
  disc=get(h1,'String');
  h1=findobj(h,'Tag','PopupMenu Cruise');
  dummystring=get(h1,'String');v=get(h1,'value');
  cruise=deblank(dummystring(v,:));
  pathname=[disc,filesep,cruise,filesep,'trrois'];
  tows=dir([pathname '\v*']);
  towarray=str2mat(tows.name);
  if ~isempty(towarray),
     towarray=sortrows(towarray);
     towarray=str2mat('Select',towarray);
     h1 = uicontrol('Parent',h, ...  
          'Units','normalized', ...
          'BackgroundColor',[1 1 1], ...
          'Callback','PLbuild_classifier GetDays', ...
          'ListboxTop',0, ...
          'Position',[0.47 0.89 0.0720 0.035], ...
          'String',towarray, ...
          'max',size(towarray,1), ...
          'min',1, ...
          'Style','popupmenu', ...
          'Tag','PopupMenu Tow', ...
          'TooltipString','Select VPR #', ...
          'Value',1);
  end
end

function GetDays()

h=gcbf;
if ~isempty(h)
  %delete existing tow/day/hour/taxa/numrois objects
  h1=findobj(h,'Tag','PopupMenu Day'); delete(h1);
  h1=findobj(h,'Tag','StaticText Hours'); delete(h1);
  h1=findobj(h,'Tag','Listbox Taxa'); delete(h1);
  h1=findobj(h,'Tag','StaticText Taxa'); delete(h1);
  h1=findobj(h,'Tag','Listbox NumRIOs'); delete(h1);
  %find days
  h1=findobj(h,'Tag','EditText Disc');
  disc=get(h1,'String');
  h1=findobj(h,'Tag','PopupMenu Cruise');
  dummystring=get(h1,'String');v=get(h1,'value');
  cruise=deblank(dummystring(v,:));
  h1=findobj(h,'Tag','PopupMenu Tow');
  dummystring=get(h1,'String');v=get(h1,'value');
  tow=deblank(dummystring(v,:));
  pathname=[disc,filesep,cruise,filesep,'trrois',filesep,tow];
  days=dir([pathname '\d*']);
  dayarray=str2mat(days.name);
  if ~isempty(dayarray),
     dayarray=sortrows(dayarray);
     dayarray=str2mat('Select',dayarray);
     h1 = uicontrol('Parent',h, ...  
          'Units','normalized', ...
          'BackgroundColor',[1 1 1], ...
          'Callback','PLbuild_classifier ShowHoursTaxa', ...
          'ListboxTop',0, ...
          'Position',[0.66 0.89 0.0720 0.035], ...
          'String',dayarray, ...
          'max',size(dayarray,1), ...
          'min',1, ...
          'Style','popupmenu', ...
          'Tag','PopupMenu Day', ...
          'TooltipString','Select Day #', ...
          'Value',1);
  end
end


function ShowHoursTaxa()

h=gcbf;
if ~isempty(h)
   %delete existing roi tree
   h1=findobj(h,'Tag','Listbox Taxa'); delete(h1);
   %find hours for this day
   h1=findobj(h,'Tag','EditText Disc');
   disc=get(h1,'String');
   h1=findobj(h,'Tag','PopupMenu Cruise');
   dummystring=get(h1,'String');v=get(h1,'value');
   cruise=deblank(dummystring(v,:));
   h1=findobj(h,'Tag','PopupMenu Tow');
   dummystring=get(h1,'string');v=get(h1,'value');
   tow=deblank(dummystring(v,:));
   h1=findobj(h,'Tag','PopupMenu Day');
   dummystring=get(h1,'string');v=get(h1,'value');
   day=deblank(dummystring(v,:));
   pathname=[disc,filesep,cruise,filesep,'trrois',filesep,tow,filesep,day];
   hours=dir([pathname '\h*']);
   hourarray=char([]);
   if size(hours,1)~=0,
       for j=1:size(hours,1),
           if length(hours(j).name)==3,%this takes only 3-character dirs (i.e. h??)
               hourarray(j,:)=char(hours(j).name);
           end
       end
   end
   if ~isempty(hourarray),
      hourarray=sortrows(hourarray);
      for j=1:size(hourarray,1),
          h1 = uicontrol('Parent',h, ...
             'Units','normalized', ...
             'BackgroundColor',[0 0.95 0.95], ...
             'FontWeight','bold', ...
             'ListboxTop',0, ...
             'Position',[.05+0.08*(j-1)   0.7    0.06    0.04], ...
             'String',hourarray(j,:), ...
             'Style','text', ...
             'Tag','StaticText hours');
           pathname2=[pathname, filesep, hourarray(j,:)];
           pathname3=[pathname, filesep, hourarray(j,:), '\*.'];
           taxa=dir(pathname3);
           ta=str2mat(taxa.name);
           if size(ta,1)>2,
              ta=ta(3:size(ta,1),:);
              ta=sortrows(ta);
              nk=size(ta,1);
              for k=1:nk,
                 roinum(k,1)=size(dir([pathname2 filesep deblank(ta(k,:))]),1)-2;               
              end;
              roinum=num2str(roinum);
              h1 = uicontrol('Parent',h, ...
                 'Units','normalized', ...
                 'BackgroundColor',[0 .85 .85], ...
                 'Position',[.04+0.08*(j-1)   0.7-((nk+2)*.03)    0.1    (nk+2)*0.04], ...
                 'String',[blanks(size(ta,2));ta;blanks(size(ta,2))], ...
                 'fontname','helvetica', ...
                 'fontsize',10, ...
                 'Style','listbox', ...
                 'Tag','StaticText Taxa', ...
                 'min',1, ...
                 'max',max(nk)+2, ...
                 'Value',[]);
              h1 = uicontrol('Parent',h, ...
                 'Units','normalized', ...
                 'BackgroundColor',[0 .85 .85], ...
                 'Position',[.04+0.08*(j-1)+.08   0.7-((nk+2)*.03)    0.07    (nk+2)*0.04], ...
                 'String',[blanks(size(roinum,2));roinum;blanks(size(roinum,2))], ...
                 'fontname','helvetica', ...
                 'fontsize',10, ...
                 'Style','edit', ...
                 'Tag','EditText trroinum', ...
                 'min',1, ...
                 'max',max(nk)+2, ...
                 'Value',[]);
           end;%size(ta,1)>2,
       end%for j=1:size(hourarray,1)
   end%~isempty(hourarray),
end;%if ~isempty(h)


