function fig = PLviewautoID(action, varargin)

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

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

%%%
%%%   Sub-function - InitializePLviewautoID
%%%

function InitializePLviewautoID()

%First get information from calling window 
%if it exists
h=gcbf;
if ~isempty(h),
    oh=findobj(h,'tag','PopupMenu Disc');
    autoiddrives=get(oh,'string');
    valueautoiddrive=get(oh,'value');
    oh=findobj(h,'tag','PopupMenu Folder');
    autoidfolders=get(oh,'string');
    valueautoidfolder=get(oh,'value');
    oh=findobj(h,'tag','PopupMenu Cruise');
    cruises=get(oh,'string');
    valuecruise=get(oh,'value');
    oh=findobj(h,'tag','PopupMenu GetClassifier');
    classifiers=get(oh,'string');
    valueclassifier=get(oh,'value');
else
    autoiddrives=str2mat(getdrives('-nofloppy'));
    autoiddrives=str2mat('Select',autoiddrives);
    valueautoiddrive=1;
    valueautoidfolder=1;
    valuecruise=1;
    valueclassifier=1;
end

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

h0 = figure('Units','normalized', ...
	'Color',[redgreenblue], ...
	'Colormap','default', ...
	'FileName','c:\plgui\mygui\PLviewautoID.m', ...
	'MenuBar','none', ...
	'Name','VPR   View AutoID', ...
	'NumberTitle','off', ...
	'PaperPosition',[18 180 576 432], ...
	'PaperUnits','points', ...
	'Position',[0.0059 0.3802 0.2598 0.5443], ...
	'Tag','VPR View AutoID', ...
	'ToolBar','none');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'ListboxTop',0, ...
	'Position',[0.086 0.053 0.808 0.899], ...
	'Style','frame', ...
	'Tag','Frame1');

%SELECT DISPLAY ROWS X COLUMNS
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.65, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.294 0.875 0.173 0.041], ...
	'String','Display', ...
	'Style','text', ...
	'Tag','StaticText Display', ...
	'TooltipString','Select # Rows x Columns of ROIs to Display');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[1 1 1], ...
	'HorizontalAlignment','left', ...
	'ListboxTop',0, ...
	'Position',[0.5 0.87 0.351 0.053], ...
	'String',str2mat('3x3','4x4','5x5','7x7','10x10'), ...
	'fontunits','normalized','fontsize',.4, ...
    'value',3, ...
	'FontWeight','normal', ...
	'Style','popupmenu', ...
	'Tag','PopupMenu Display', ...
	'TooltipString','Select # Rows x Columns of ROIs to Display') ;

%SELECT AUTOID DRIVE LETTER
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.7, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.197 0.815 0.293 0.041], ...
	'String','AutoID Drive', ...
	'Style','text', ...
	'Tag','StaticText AutoID Drive', ...
	'TooltipString','Select AutoID Drive Letter');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[1 1 1], ...
	'Callback','PLviewautoID GetAutoIDFolders', ...
	'HorizontalAlignment','left', ...
	'ListboxTop',0, ...
	'Position',[0.5 0.81 0.351 0.053], ...
	'String',autoiddrives, ...
    'value',valueautoiddrive, ...
	'fontunits','normalized','fontsize',.4, ...
	'FontWeight','normal', ...
	'Style','popupmenu', ...
	'Tag','PopupMenu AutoID Drive', ...
	'TooltipString','Select AutoID Drive Letter') ;

%SELECT AUTOID BASE FOLDER
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.7, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.171 0.755 0.323 0.041], ...
	'String','AutoID Folder', ...
	'Style','text', ...
	'Tag','StaticText AutoID Folder', ...
	'TooltipString','Select AutoID Base Folder');
if ~isempty(h),
  h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[1 1 1], ...
	'HorizontalAlignment','left', ...
	'ListboxTop',0, ...
	'Position',[0.5 0.75 0.351 0.053], ...
    'Callback','PLviewautoID GetCruises', ...
	'String',autoidfolders, ...
    'value',valueautoidfolder, ...
	'fontunits','normalized','fontsize',.4, ...
	'FontWeight','normal', ...
	'Style','popupmenu', ...
	'Tag','PopupMenu AutoID Folder', ...
	'TooltipString','Select AutoID Base Folder') ;
end


%SELECT ROI DRIVE LETTER

comp=computer;
if strcmp(comp(1:3),'MAC');
    a=dir('/Volumes');
    for j=3:size(a,1),a(j).name=['/Volumes/' a(j).name '/'];end
    roidrives=str2mat(a(3:end).name);
else
    roidrives=str2mat(getdrives('-nofloppy'));
end

roidrives=str2mat('Select',roidrives);
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.7, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.25 0.695 0.229 0.041], ...
	'String','ROI Drive', ...
	'Style','text', ...
	'Tag','StaticText ROI Drive', ...
	'TooltipString','Select Drive Letter where ROIs are located');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[1 1 1], ...
	'Callback','PLviewautoID GetRoiFolders', ...
	'HorizontalAlignment','left', ...
	'ListboxTop',0, ...
	'Position',[0.5 0.69 0.351 0.053], ...
	'String',roidrives, ...
    'value',1, ...
	'fontunits','normalized','fontsize',.4, ...
	'FontWeight','normal', ...
	'Style','popupmenu', ...
	'Tag','PopupMenu ROI Drive', ...
	'TooltipString','Select Drive Letter where ROIs are located') ;

%SELECT ROI BASE FOLDER
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.65, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.223 0.635 0.259 0.041], ...
	'String','ROI Folder', ...
	'Style','text', ...
	'Tag','StaticText ROI Folder', ...
	'TooltipString','Select ROI Base Folder');

%SELECT Cruise
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.65, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.299 0.575 0.169 0.041], ...
	'String','Cruise', ...
	'Style','text', ...
	'Tag','StaticText Cruise', ...
	'TooltipString','Select Cruise');
if ~isempty(h),
  h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[1 1 1], ...
	'HorizontalAlignment','left', ...
	'ListboxTop',0, ...
	'Position',[0.5 0.57 0.351 0.053], ...
    'Callback','PLviewautoID GetClassifier', ...
	'String',cruises, ...
    'value',valuecruise, ...
	'fontunits','normalized','fontsize',.4, ...
	'FontWeight','normal', ...
	'Style','popupmenu', ...
	'Tag','PopupMenu Cruise', ...
	'TooltipString','Select Cruise') ;
end

%SELECT Classifier
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.7, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.243 0.515 0.233 0.041], ...
	'String','Classifier', ...
	'Style','text', ...
	'Tag','StaticText Classifier', ...
	'TooltipString','Select Classifier');
if ~isempty(h),
  h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[1 1 1], ...
	'HorizontalAlignment','left', ...
	'ListboxTop',0, ...
	'Position',[0.5 0.51 0.351 0.053], ...
    'Callback','PLviewautoID GetTaxon', ...
	'String',classifiers, ...
    'value',valueclassifier, ...
	'fontunits','normalized','fontsize',.4, ...
	'FontWeight','normal', ...
	'Style','popupmenu', ...
	'Tag','PopupMenu Classifier', ...
	'TooltipString','Select Classifier') ;
end

%SELECT Taxon
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.7, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.324 0.455 0.124 0.041], ...
	'String','Taxon', ...
	'Style','text', ...
	'Tag','StaticText Taxon', ...
	'TooltipString','Select Taxon to View');

%SELECT Yearday
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.7, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.266 0.395 0.199 0.041], ...
	'String','Yearday', ...
	'Style','text', ...
	'Tag','StaticText Yearday', ...
	'TooltipString','Select Yearday');

%SELECT Hour
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.65, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.336 0.335 0.113 0.041], ...
	'String','Hour', ...
	'Style','text', ...
	'Tag','StaticText Hour', ...
	'TooltipString','Select Hour');

%SELECT Duplicated Taxon
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.61, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.16 0.135 0.296 0.041], ...
	'String','Duplicated Taxon', ...
	'Style','text', ...
	'Tag','StaticText Duplicated Taxon', ...
	'TooltipString','Select taxon containing ROIs shared with the above taxon');

if nargout > 0, fig = h0; end


function GetAutoIDFolders(fig)

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

if ~isempty(h)
  %delete existing popups
  h1=findobj(h,'Tag','PopupMenu AutoID Folder'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Cruise'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Classifier'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Taxon'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Yearday'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Hour'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartViewAutoID'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Duplicated Taxon'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartViewDuplicatedID'); delete(h1);
  %find folders
  h1=findobj(h,'Tag','PopupMenu AutoID Drive');
  dummystring=get(h1,'String');v=get(h1,'value');
  disc=deblank(dummystring(v,:));
  folders=dir(disc);
  folderarray=str2mat(folders.name);
  folderarray=lower(folderarray([folders.isdir]',:));
  sz=size(folderarray,1);
  if sz>2,    
     folderarray=sortrows(folderarray(3:sz,:));
     folderarray=str2mat('Select',folderarray);
     h1 = uicontrol('Parent',h, ...  
          'Units','normalized', ...
          'BackgroundColor',[1 1 1], ...
          'ListboxTop',0, ...
          'Position',[0.5 0.63 0.351 0.053], ...
          'Callback','PLviewautoID GetCruises', ...
          'String',folderarray, ...
	      'fontunits','normalized','fontsize',.4, ...
	      'FontWeight','normal', ...
          'max',sz+1, ...
          'min',1, ...
          'Style','popupmenu', ...
          'Tag','PopupMenu AutoID Folder', ...
          'TooltipString','Select Folder', ...
          'Value',1);
  end
end

function GetRoiFolders(fig)

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

if ~isempty(h)
  %delete existing popups
  h1=findobj(h,'Tag','PopupMenu RoiFolder'); delete(h1);
  %find folders
  h1=findobj(h,'Tag','PopupMenu ROI Drive');
  dummystring=get(h1,'String');v=get(h1,'value');
  disc=deblank(dummystring(v,:));
  folders=dir(disc);
  folderarray=str2mat(folders.name);
  folderarray=lower(folderarray([folders.isdir]',:));
  sz=size(folderarray,1);
  if sz>2,
     folderarray=sortrows(folderarray(3:sz,:));
     folderarray=str2mat('Select',folderarray);
     h1 = uicontrol('Parent',h, ...
          'Units','normalized', ...
          'BackgroundColor',[1 1 1], ...
          'ListboxTop',0, ...
          'Position',[0.5 0.63 0.351 0.053], ...
          'String',folderarray, ...
	      'fontunits','normalized','fontsize',.4, ...
	      'FontWeight','normal', ...
          'max',sz+1, ...
          'min',1, ...
          'Style','popupmenu', ...
          'Tag','PopupMenu ROI Folder', ...
          'TooltipString','Select Folder', ...
          'Value',1);
  end
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 Classifier'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Taxon'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Yearday'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Hour'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartViewAutoID'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Duplicated Taxon'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartViewDuplicatedID'); delete(h1);
  %find cruises
  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,:));
  basepath=[disc folder];
  cruises=dir(basepath);
  cruisearray=str2mat(cruises.name);
  sz=size(cruisearray,1);
  if sz>2,    
     cruisearray=sortrows(cruisearray(3:sz,:));
     cruisearray=str2mat('Select',cruisearray);
     h1 = uicontrol('Parent',h, ...  
          'Units','normalized', ...
          'BackgroundColor',[1 1 1], ...
          'Callback','PLviewautoID GetClassifier', ...
          'ListboxTop',0, ...
          'Position',[0.5 0.57 0.351 0.053], ...
          'String',cruisearray, ...
	      'fontunits','normalized','fontsize',.4, ...
	      'FontWeight','normal', ...
          'max',sz+1, ...
          'min',1, ...
          'Style','popupmenu', ...
          'Tag','PopupMenu Cruise', ...
          'TooltipString','Select Cruise', ...
          'Value',1);
  end
end


function GetClassifier
h=gcbf; 
if ~isempty(h)
  %delete existing GetClassifier popup
  h1=findobj(h,'Tag','PopupMenu Classifier'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Taxon'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Yearday'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Hour'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartViewAutoID'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Duplicated Taxon'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartViewDuplicatedID'); delete(h1);
  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,:));
  basepath=[disc folder];
  h1=findobj(h,'Tag','PopupMenu Cruise');
  dummystring=get(h1,'String');v=get(h1,'value');
  cruise=deblank(dummystring(v,:));
  classifiers=dir([basepath, filesep, cruise, filesep,'clpar',filesep,'*.mat']);
  datarray=str2mat(classifiers.name);
  sd=size(datarray,1);
  if sd>0,    
     datarray=datarray(1:sd,:);
     datarray=sortrows(datarray);
     for j=1:sd,
         k=findstr('.mat',datarray(j,:));
         datarray(j,k:end)=' ';
     end
     datarray=str2mat('Select',datarray);
     h1 = uicontrol('Parent',h, ...  
          'Units','normalized', ...
          'BackgroundColor',[1 1 1], ...
          'Callback','PLviewautoID GetTaxon', ...
          'ListboxTop',0, ...
          'Position',[0.5 0.51 0.351 0.053], ...
          'String',datarray, ...
	      'fontunits','normalized','fontsize',.4, ...
	      'FontWeight','normal', ...
          'max',sd, ...
          'min',1, ...
          'Style','popupmenu', ...
          'Tag','PopupMenu Classifier', ...
          'TooltipString','Select classifier', ...
          'Value',1);
  end
end


function GetTaxon
h=gcbf; 
if ~isempty(h)
  %delete existing GetClassifier popup
  h1=findobj(h,'Tag','PopupMenu Taxon'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Yearday'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Hour'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartViewAutoID'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Duplicated Taxon'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartViewDuplicatedID'); delete(h1);
  %do the following because it may take a while to get the taxa info
  h1_working = uicontrol('Parent',h, ...
      'Units','normalized', ...
      'BackgroundColor',[0.2157 .6353 .6353]*1.2*1.1, ...
      'ForegroundColor',[1 1 0], ...
      'fontunits','normalized','fontsize',.7, ...
      'FontWeight','bold', ...
      'Position',[0.5 0.45 0.3 0.041], ...
      'String','Working...', ...
      'Style','text', ...
      'Tag','StaticText Working', ...
      'TooltipString','Getting list of Taxa');
  drawnow;
  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,:));
  basepath=[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,:));
  taxa1=dir([basepath, filesep, cruise, filesep,'autoid']);
  datarray=str2mat(taxa1.name);%some taxa here may be from other classifiers
  sd=size(datarray,1);
  if sd>2,
      taxa2=[];
      for j = 3:sd,%check for taxa having the right classifier aid files
          dum=dir([basepath, filesep, cruise filesep 'autoid' filesep deblank(datarray(j,:)) filesep 'aid' filesep classifier 'aid.*']);
          if ~isempty(dum),%at least one aid file was found in this taxon for the specified classifier
              if j==3,
                  taxa2=deblank(datarray(j,:));%taxa2 has good taxa
              else
                  taxa2=strvcat(taxa2,deblank(datarray(j,:)));%taxa2 has good taxa
              end
          end
      end
  end;
  datarray=taxa2;
  datarray=sortrows(datarray);
  datarray=str2mat('Select',datarray);
  sd=size(datarray,1);
  set(h1_working,'visible','off');
  h1 = uicontrol('Parent',h, ...  
      'Units','normalized', ...
      'BackgroundColor',[1 1 1], ...
      'Callback','PLviewautoID GetYearday', ...
      'ListboxTop',0, ...
      'Position',[0.5 0.45 0.351 0.053], ...
      'String',datarray, ...
      'fontunits','normalized','fontsize',.4, ...
      'FontWeight','normal', ...
      'max',sd, ...
      'min',1, ...
      'Style','popupmenu', ...
      'Tag','PopupMenu Taxon', ...
      'TooltipString','Select Taxon', ...
      'Value',1);
end

function GetYearday
h=gcbf; 
if ~isempty(h)
  %delete existing Yearday and Hour popups
  h1=findobj(h,'Tag','PopupMenu Yearday'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Hour'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartViewAutoID'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Duplicated Taxon'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartViewDuplicatedID'); delete(h1);
  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,:));
  basepath=[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,:));
  h1=findobj(h,'Tag','PopupMenu Taxon');
  dummystring=get(h1,'String');v=get(h1,'value');
  taxon=deblank(dummystring(v,:));
  aa=dir([basepath,filesep,cruise,filesep,'autoid',filesep,taxon,filesep,'aid',filesep,classifier,'aid.*']);
  bb=str2mat(aa.name);
  sbb=size(bb,2);
  cc=sort(str2num(bb(:,sbb-6:sbb-4)));
  dd=cc(2:length(cc));
  yeardays=num2str([cc(1);dd(find(diff(cc)>0))]);
  yeardays(yeardays(:)==' ')='0';
  if (size(yeardays,2)<=2),%this pads yeardays w/ leading zero if <2 digits
      if (size(yeardays,2)==1),%this pads yeardays w/ leading zero if only 1 digit
         yeardays=[num2str(zeros(size(yeardays,1),1)),yeardays];
      end
      yeardays=[num2str(zeros(size(yeardays,1),1)),yeardays];
  end
  datarray=yeardays;
  datarray=str2mat('Select',datarray);
  sd=size(datarray,1);
  h1 = uicontrol('Parent',h, ...  
      'Units','normalized', ...
      'BackgroundColor',[1 1 1], ...
      'Callback','PLviewautoID GetHour', ...
      'ListboxTop',0, ...
      'Position',[0.5 0.39 0.351 0.053], ...
      'String',datarray, ...
      'fontunits','normalized','fontsize',.4, ...
      'FontWeight','normal', ...
      'max',sd, ...
      'min',1, ...
      'Style','popupmenu', ...
      'Tag','PopupMenu Yearday', ...
      'TooltipString','Select Yearday', ...
      'Value',1);
end


function GetHour
h=gcbf; 
if ~isempty(h)
  %delete existing Hour popup and start pushbutton
  h1=findobj(h,'Tag','PopupMenu Hour'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartViewAutoID'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Duplicated Taxon'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartViewDuplicatedID'); delete(h1);
  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,:));
  basepath=[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,:));
  h1=findobj(h,'Tag','PopupMenu Taxon');
  dummystring=get(h1,'String');v=get(h1,'value');
  taxon=deblank(dummystring(v,:));
  h1=findobj(h,'Tag','PopupMenu Yearday');
  dummystring=get(h1,'String');v=get(h1,'value');
  yearday=deblank(dummystring(v,:));
  aa=dir([basepath,filesep,cruise,filesep,'autoid',filesep,taxon,filesep,'aid',filesep,classifier,'aid.d' yearday '.*']);
  bb=str2mat(aa.name);
  bb=bb([aa.bytes]'~=0,:);%only lists non-empty aid files
  sbb=size(bb,2);
  cc=sort(str2num(bb(:,sbb-1:sbb)));
  hours=num2str(cc);
  datarray=hours;
  datarray=str2mat('Select',datarray);
  sd=size(datarray,1);
  h1 = uicontrol('Parent',h, ...  
      'Units','normalized', ...
      'BackgroundColor',[1 1 1], ...
      'Callback','PLviewautoID StartViewAutoID; PLviewautoID GetDuplicatedTaxon', ...
      'ListboxTop',0, ...
      'Position',[0.5 0.33 0.351 0.053], ...
      'String',datarray, ...
      'fontunits','normalized','fontsize',.4, ...
      'FontWeight','normal', ...
      'max',sd, ...
      'min',1, ...
      'Style','popupmenu', ...
      'Tag','PopupMenu Hour', ...
      'TooltipString','Select Hour', ...
      'Value',1);
end


function StartViewAutoID
h=gcbf; 
redgreenblue=get(h,'color');
if ~isempty(h)
    %Original Position:[0.293 0.129 0.429 0.058]
    h1 = uicontrol('Parent',h, ...  
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue]*1.1, ...
        'Callback','ViewAutoID', ...
        'Position',[0.293 0.265 0.429 0.058], ...
        'String','View AutoID ROIs', ...
        'fontunits','normalized','fontsize',.528, ...
        'FontWeight','normal', ...
        'Style','pushbutton', ...
        'Tag','PushButton StartViewAutoID', ...
        'TooltipString','Push to view and/or manually re-sort AutoID ROIs');
    h1 = uicontrol('Parent',h, ...  
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue]*1.1, ...
        'Callback','ViewROITaxaList', ...
        'Position',[0.293 0.195 0.429 0.058], ...
        'String','View List of Taxa of ROIs', ...
        'fontunits','normalized','fontsize',.528, ...
        'FontWeight','normal', ...
        'Style','pushbutton', ...
        'Tag','PushButton StartViewAutoID', ...
        'TooltipString',['Push to view a list showing all taxa of the ROIs' char(10) 'within the selected taxon, day, and hour' char(10) 'selected above' char(10) '(useful for comparing ROIs using View Duplicated ROIs)'] );
   
end

function GetDuplicatedTaxon
h=gcbf; 
if ~isempty(h)
  %do the following because it may take a while to get the taxa info
  h1_working = uicontrol('Parent',h, ...
      'Units','normalized', ...
      'BackgroundColor',[0.2157 .18 .6353]*1.2*1.1, ...
      'ForegroundColor',[1 1 0], ...
      'fontunits','normalized','fontsize',.7, ...
      'FontWeight','bold', ...
      'Position',[0.5 0.13 0.3 0.041], ...
      'String','Working...', ...
      'Style','text', ...
      'Tag','StaticText Working', ...
      'TooltipString','Getting list of Taxa');
  drawnow;
  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,:));
  basepath=[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,:));
  taxa1=dir([basepath, filesep, cruise, filesep,'autoid']);
  datarray=str2mat(taxa1.name);%some taxa here may be from other classifiers
  sd=size(datarray,1);
  if sd>2,
      taxa2=[];
      for j = 3:sd,%check for taxa having the right classifier aid files
          dum=dir([basepath, filesep, cruise filesep 'autoid' filesep deblank(datarray(j,:)) filesep 'aid' filesep classifier 'aid.*']);
          if ~isempty(dum),%at least one aid file was found in this taxon for the specified classifier
              if j==3,
                  taxa2=deblank(datarray(j,:));%taxa2 has good taxa
              else
                  taxa2=strvcat(taxa2,deblank(datarray(j,:)));%taxa2 has good taxa
              end
          end
      end
  end;
  datarray=taxa2;
  datarray=sortrows(datarray);
  datarray=str2mat('Select',datarray);
  sd=size(datarray,1);
  set(h1_working,'visible','off');
  h1 = uicontrol('Parent',h, ...  
      'Units','normalized', ...
      'BackgroundColor',[1 1 1], ...
      'Callback','PLviewautoID StartViewDuplicatedID', ...
      'ListboxTop',0, ...
      'Position',[0.5 0.13 0.351 0.053], ...
      'String',datarray, ...
      'fontunits','normalized','fontsize',.4, ...
      'FontWeight','normal', ...
      'max',sd, ...
      'min',1, ...
      'Style','popupmenu', ...
      'Tag','PopupMenu Duplicated Taxon', ...
      'TooltipString',['Select taxon containing ROIs shared with the above taxon' char(10),...
                       'If the program is showing an error, change any of the above boxes' char(10),...
                       'such that this box disappears and try again' char(10) ' ' char(10),... 
                       'To easily duplicate ROIs into multiple taxa, enter the same taxon ' char(10),...
                       'as the one above and use the ''Save'' button'], ...
      'Value',1);
end

function StartViewDuplicatedID
h=gcbf; 
redgreenblue=get(h,'color');
if ~isempty(h)
    h1 = uicontrol('Parent',h, ...  
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue]*1.1, ...
        'Callback','ViewDuplicatedID', ...
        'Position',[0.293 0.065 0.429 0.058], ...
        'String','View Duplicated ROIs', ...
        'fontunits','normalized','fontsize',.528, ...
        'FontWeight','normal', ...
        'Style','pushbutton', ...
        'Tag','PushButton StartViewDuplicatedID', ...
        'TooltipString',['Push to view and/or manually re-sort ROIs that share a taxon' char(10) 'with the taxon selected as ''Duplicated Taxon''' char(10) 'If the program is showing an error, change any of the above boxes' char(10) 'such that the above box disappears and try again']);
end