function fig=PLbatchexport(action, varargin)
%This is to set up the batch export window for batch exporting files.

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

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

function InitializePLbatchexport()

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   Batch Export', ...
	'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');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.7, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.197 0.885 0.293 0.041], ...
	'String','AutoID Drive', ...
	'Style','text', ...
	'Tag','StaticText AutoID Drive', ...
	'TooltipString','Select AutoID 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
    autoiddrives=str2mat(a(3:end).name);
else
    autoiddrives=str2mat(getdrives('-nofloppy'));
end

autoiddrives=str2mat('Select',autoiddrives);
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[1 1 1], ...
	'Callback','PLbatchexport GetAutoIDFolders', ...
	'HorizontalAlignment','left', ...
	'ListboxTop',0, ...
	'Position',[0.5 0.88 0.351 0.053], ...
	'String',autoiddrives, ...
	'fontunits','normalized','fontsize',.4, ...
	'FontWeight','normal', ...
	'Style','popupmenu', ...
	'Tag','PopupMenu AutoID Drive', ...
	'TooltipString','Select AutoID Drive Letter') ;
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.7, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.171 0.815 0.323 0.041], ...
	'String','AutoID Folder', ...
	'Style','text', ...
	'Tag','StaticText AutoID Folder', ...
	'TooltipString','Select AutoID Base Folder');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.65, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.299 0.745 0.169 0.041], ...
	'String','Cruise', ...
	'Style','text', ...
	'Tag','StaticText Cruise', ...
	'TooltipString','Select Cruise');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.61, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.13 0.675 0.296 0.041], ...
	'String','Processed CTD Files', ...
	'Style','text', ...
	'Tag','StaticText ProcessedCTDFiles', ...
	'TooltipString',['Select Processed CTD File(s)' char(10) ' ' char(10) '''All Processed Files'' means select all processed CTD files']);
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.7, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.244 0.605 0.233 0.041], ...
	'String','Classifier', ...
	'Style','text', ...
	'Tag','StaticText Classifier', ...
	'TooltipString','Select Classifier');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[redgreenblue]*1.1, ...
	'fontunits','normalized','fontsize',.65, ...
	'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[0.324 0.535 0.124 0.041], ...
	'String','Taxon', ...
	'Style','text', ...
	'Tag','StaticText Taxon', ...
	'TooltipString',['Select Taxon/Taxa' char(10) ' ' char(10) '''All Taxa'' means select all taxa']);
h1 = uicontrol('Parent',h0, ...
    'Units','normalized', ...
    'BackgroundColor',[redgreenblue]*1.1, ...
    'fontunits','normalized','fontsize',.61, ...
    'FontWeight','normal', ...
    'ListboxTop',0, ...
    'Position',[0.105 0.235 0.34 0.041], ...
    'String','Cruise Metadata Path', ...
    'Style','text', ...
    'Tag','StaticText Metadata', ...
    'TooltipString',['Enter Cruise Metadata File Path (optional)' char(10) ...
                     'bugs2avgbugs will still be made if no path is entered' char(10) ' ' char(10) ...
                     'Required Format of Cruise Metadata (Use NaN for missing information): ' char(10) ...
                     'Column 1: Month' char(10) ...
                     'Column 2: Day' char(10) ...
                     'Column 3: Year' char(10) ...
                     'Column 4: Time (Decimal Hour) ' char(10) ...
                     'Column 5: Station (if applicable, integer)' char(10) ...
                     'Column 6: VPR# (integer)' char(10) ...
                     'Column 7: Latitude (Decimal Degrees)' char(10) ...
                     'Column 8: Longitude (Decimal Degrees)']);
h1 = uicontrol('Parent',h0, ...
    'Units','normalized', ...
    'BackgroundColor',[redgreenblue]*1.1, ...
    'Position',[0.193    0.075    0.3    .0656], ...
    'Fontunits','normalized',...
    'Fontsize',0.4, ...
    'String','Exit', ...
    'callback','close(gcbf);', ...
    'Style','pushbutton', ...
    'Tag','Pushbutton Exit', ...
    'TooltipString','Close Window');
h1 = uicontrol('Parent',h0, ...
    'Units','normalized', ...
    'BackgroundColor',[redgreenblue]*1.1, ...
    'Callback','PLbatchexport batch_help', ...
    'Position',[0.522    0.075    0.3    0.0656], ...
    'fontunits','normalized', ...
    'fontsize',0.4, ...
    'String','Help', ...
    'Style','pushbutton', ...
    'Tag','Pushbutton Help', ...
    'TooltipString','Help for Batch Export');

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 ProcessedCTDFiles'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Classifier'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Taxon'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartBatchExport'); delete(h1);
  h1=findobj(h,'Tag','EditText MetaData'); 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.81 0.351 0.053], ...
          'Callback','PLbatchexport 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 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 ProcessedCTDFiles'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Classifier'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Taxon'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartBatchExport'); delete(h1);
  h1=findobj(h,'Tag','EditText MetaData'); 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','PLbatchexport GetProcessedCTDFiles', ...
          'ListboxTop',0, ...
          'Position',[0.5 0.74 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 GetProcessedCTDFiles(fig)

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

if ~isempty(h)
  %delete existing popups
  h1=findobj(h,'Tag','PopupMenu ProcessedCTDFiles'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Classifier'); delete(h1);
  h1=findobj(h,'Tag','PopupMenu Taxon'); delete(h1);
  h1=findobj(h,'Tag','PushButton StartBatchExport'); delete(h1);
  h1=findobj(h,'Tag','EditText MetaData'); delete(h1);
  %find processed ctd files
  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,:));
  datafiles=dir([basepath, filesep, cruise, filesep,'processed',filesep,'sensordata']);
  datarray=str2mat(datafiles.name);
  for(i=1:size(datarray,1)),datarray(i,:)=strrep(datarray(i,:),'.mat','    ');end
  sd=size(datarray,1);
  if sd>2,  
     datarray=datarray(3:sd,:);
     datarray=sortrows(datarray);
     datarray=str2mat('Select','All Processed Files',datarray);
     h1 = uicontrol('Parent',h, ...  
          'Units','normalized', ...
          'BackgroundColor',[1 1 1], ...
          'Callback','PLbatchexport GetClassifier', ...
          'ListboxTop',0, ...
          'Position',[0.5 0.67 0.351 0.053], ...
          'String',datarray, ...
	      'fontunits','normalized','fontsize',.4, ...
	      'FontWeight','normal', ...
          'max',sd+2, ...
          'min',1, ...
          'Style','popupmenu', ...
          'Tag','PopupMenu ProcessedCTDFiles', ...
          'TooltipString',['Select Processed CTD File(s)' char(10) ' ' char(10) '''All Processed Files'' means select all processed CTD files'], ...
          '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','PushButton StartBatchExport'); 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','PLbatchexport GetTaxon', ...
          'ListboxTop',0, ...
          'Position',[0.5 0.60 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','PushButton StartBatchExport'); 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.53 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','All Taxa',datarray);
  sd=size(datarray,1);
  set(h1_working,'visible','off');
  h1 = uicontrol('Parent',h, ...  
      'Units','normalized', ...
      'BackgroundColor',[1 1 1], ...
      'Callback','PLbatchexport StartBatchExport', ...
      'ListboxTop',0, ...
      'Position',[0.5 0.53 0.351 0.053], ...
      'String',datarray, ...
      'fontunits','normalized','fontsize',.4, ...
      'FontWeight','normal', ...
      'max',sd+1, ...
      'min',1, ...
      'Style','popupmenu', ...
      'Tag','PopupMenu Taxon', ...
      'TooltipString',['Select Taxon/Taxa' char(10) ' ' char(10) '''All Taxa'' means select all taxa'], ...
      'Value',1);
end

function StartBatchExport
h=gcbf; 
redgreenblue=get(h,'color');
if ~isempty(h)
    h1=findobj(h,'Tag','PopupMenu Cruise');
    dummystring=get(h1,'String');v=get(h1,'value');
    cruise=deblank(dummystring(v,:));

    h1 = uicontrol('Parent',h, ...  
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue]*1.1, ...
        'Callback','PLbatchexport DeleteOldBugData', ...
        'Position',[0.193 0.455 0.629 0.058], ...
        'String','Delete Previously Exported Data', ...
        'fontunits','normalized',...
        'fontsize',.5, ...
        'FontWeight','normal', ...
        'Style','pushbutton', ...
        'Tag','PushButton StartBatchExport', ...
        'TooltipString',['Push to delete previously exported plankton data' char(10) ...
                         'so that outdated data is not accidentally retained' char(10) ' ' char(10) ... 
                         'Note: Only bugs data of the Processed Files (i.e., vpr#.mat) ' char(10) ...
                         'and Taxa selected will be deleted']);
    h1 = uicontrol('Parent',h, ...  
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue]*1.1, ...
        'Callback','PLbatchexport RunBatchExport', ...
        'Position',[0.193 0.375 0.629 0.058], ...
        'String','Batch Export Plankton Data', ...
        'fontunits','normalized',...
        'fontsize',.45, ...
        'FontWeight','normal', ...
        'Style','pushbutton', ...
        'Tag','PushButton StartBatchExport', ...
        'TooltipString',['Push to batch export plankton data of the ' char(10) ...
                         'selected processed files and taxa']);
    h1 = uicontrol('Parent',h, ...  
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue]*1.1, ...
        'Callback','PLbatchexport output2bugs', ...
        'Position',[0.193 0.295 0.629 0.058], ...
        'String','Convert Output to ''bugs'' Files', ...
        'fontunits','normalized',...
        'fontsize',.5, ...
        'FontWeight','normal', ...
        'Style','pushbutton', ...
        'Tag','PushButton StartBatchExport', ...
        'TooltipString',['Push to convert batch export output into ' char(10) ...
                         'merged files containing sensordata and all' char(10) ...
                         'taxa (only time averaged)']);
    h1 = uicontrol('Parent',h, ...
        'Units','normalized', ...
        'BackgroundColor',[1 1 1], ...
        'fontunits','normalized','fontsize',.7, ...
        'FontWeight','normal', ...
        'Position',[0.50 0.24 0.34 0.041], ...
        'String',['C:\Users\drewhirzel\Documents\MATLAB\',cruise,'_VPR_data\bugdata\',cruise,'vprinfo_date_pos.txt'], ...
        'Style','edit', ...
        'Tag','EditText MetaData', ...
        'TooltipString',['Enter Cruise Metadata File Path (optional)' char(10) ...
                         'bugs2avgbugs will still be made if no path is entered' char(10) ' ' char(10) ...
                         'Required Format of Cruise Metadata (Use NaN for missing information): ' char(10) ...
                         'Column 1: Month' char(10) ...
                         'Column 2: Day' char(10) ...
                         'Column 3: Year' char(10) ...
                         'Column 4: Time (Decimal Hour) ' char(10) ...
                         'Column 5: Station (if applicable, integer)' char(10) ...
                         'Column 6: VPR# (integer)' char(10) ...
                         'Column 7: Latitude (Decimal Degrees)' char(10) ...
                         'Column 8: Longitude (Decimal Degrees)']);
	h1 = uicontrol('Parent',h, ...  
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue]*1.1, ...
        'Callback','PLbatchexport runbugs2avgbugs', ...
        'Position',[0.193 0.165 0.629 0.058], ...
        'String','Convert ''bugs'' Files to ''avgbugs'' Files', ...
        'fontunits','normalized',...
        'fontsize',.45, ...
        'FontWeight','normal', ...
        'Style','pushbutton', ...
        'Tag','PushButton StartBatchExport', ...
        'TooltipString',['Push to convert time averaged ''bugs'' files' char(10) ...
                         'into depth averaged ''avgbugs'' files containing' char(10) ...
                         'sensordata and all taxa']);
end

function DeleteOldBugData
h=gcbf; 
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,:));
    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 ProcessedCTDFiles');
    dummystring=get(h1,'String');v=get(h1,'value');
    vprnums=deblank(dummystring(v,:));
    if(strcmp(vprnums,'All Processed Files')),vprnums=dummystring(3:end,:);end
    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');
    taxa=deblank(dummystring(v,:));
    if(strcmp(taxa,'All Taxa')),taxa=dummystring(3:end,:);end
    for i=1:size(vprnums,1)
        vprnum=deblank(vprnums(i,:));
        for j=1:size(taxa,1)
            taxon=deblank(taxa(j,:));
            path=[deblank(basepath) filesep deblank(cruise) filesep 'processed' filesep 'bugs'];
            fn1=[path filesep 'binned' filesep taxon filesep taxon '_' vprnum '_' classifier '.mat'];
            fn2=[path filesep 'individuals' filesep taxon filesep taxon '_all_' vprnum '_' classifier '.mat'];
            if(exist(fn1,'file')),delete(fn1);end
            if(exist(fn2,'file')),delete(fn2);end
        end
    end
    redgreenblue=[0.2157 .6353 .6353]*1.2;
    h0 = figure('Units','normalized', ...
        'Color',[redgreenblue], ...
        'MenuBar','none', ...
        'Name','VPR:  Batch Export', ...
        'NumberTitle','off', ...
        'Position',[.3    .5    0.4    0.2], ...
        'ToolBar','none');
    h1 = uicontrol('Parent',h0, ...
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue], ...
        'fontunits','normalized','fontsize',.67, ...
        'FontWeight','normal', ...
        'Position',[.05 .77 .4 .15], ...
        'String','Delete Files Progress:', ...
        'Style','text', ...
        'fontweight','bold',...
        'HorizontalAlignment','Left',...
        'Tag','StaticText Header', ...
        'TooltipString','This window displays the progress of delete files');
    h1 = uicontrol('Parent',h0, ...
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue], ...
        'fontunits','normalized','fontsize',.60, ...
        'FontWeight','normal', ...
        'Position',[.05 .6 .8 .15], ...
        'String',['Processed CTD File:  ',vprnum,'         ',int2str(i),' / ',int2str(size(vprnums,1))], ...
        'Style','text', ...
        'HorizontalAlignment','Left',...
        'Tag','StaticText Names', ...
        'TooltipString','Current VPR# being batched with fraction processed');
    h1 = uicontrol('Parent',h0, ...
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue], ...
        'fontunits','normalized','fontsize',.60, ...
        'FontWeight','normal', ...
        'Position',[.05 .45 .8 .15], ...
        'String',['Plankton Taxon:  ',taxon,'         ',int2str(j),' / ',int2str(size(taxa,1))], ...
        'Style','text', ...
        'HorizontalAlignment','Left',...
        'Tag','StaticText Names', ...
        'TooltipString','Current taxon being batched with fraction processed');
    uicontrol('style','pushbutton','units','normalized','position',[.2 .1 .6 .25], ...
       'string','Delete Complete','backgroundcolor',redgreenblue, ...
       'callback','close(gcbf)','fontsize',12);
end

function RunBatchExport
h=gcbf; 
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,:));
    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 ProcessedCTDFiles');
    dummystring=get(h1,'String');v=get(h1,'value');
    vprnums=deblank(dummystring(v,:));
    if(strcmp(vprnums,'All Processed Files')),vprnums=dummystring(3:end,:);end
    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');
    taxa=deblank(dummystring(v,:));
    if(strcmp(taxa,'All Taxa')),taxa=dummystring(3:end,:);end  
    for i=1:size(vprnums,1)
        vprnum=deblank(vprnums(i,:));
        for j=1:size(taxa,1)
            taxon=deblank(taxa(j,:));
            BatchExport(basepath,cruise,vprnum,classifier,taxon);
            disp(['Batching of ',vprnum,' ',taxon,' completed.' char(10) 'Progress: VPR ',int2str(i),' / ',int2str(size(vprnums,1)),' Taxa: ',int2str(j),' / ',int2str(size(taxa,1))])
        end
    end
    
    redgreenblue=[0.2157 .6353 .6353]*1.2;
    h0 = figure('Units','normalized', ...
        'Color',[redgreenblue], ...
        'MenuBar','none', ...
        'Name','VPR:  Batch Export', ...
        'NumberTitle','off', ...
        'Position',[.3    .5    0.4    0.2], ...
        'ToolBar','none'); 
    h1 = uicontrol('Parent',h0, ...
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue], ...
        'fontunits','normalized','fontsize',.67, ...
        'FontWeight','normal', ...
        'Position',[.05 .77 .4 .15], ...
        'String','Batch Export Progress:', ...
        'Style','text', ...
        'fontweight','bold',...
        'HorizontalAlignment','Left',...
        'Tag','StaticText Header', ...
        'TooltipString','This window displays the progress of the batch export');
    h1 = uicontrol('Parent',h0, ...
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue], ...
        'fontunits','normalized','fontsize',.60, ...
        'FontWeight','normal', ...
        'Position',[.05 .6 .8 .15], ...
        'String',['Processed CTD File:  ',vprnum,'         ',int2str(i),' / ',int2str(size(vprnums,1))], ...
        'Style','text', ...
        'HorizontalAlignment','Left',...
        'Tag','StaticText Names', ...
        'TooltipString','Current VPR# being batched with fraction processed');
    h1 = uicontrol('Parent',h0, ...
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue], ...
        'fontunits','normalized','fontsize',.60, ...
        'FontWeight','normal', ...
        'Position',[.05 .45 .8 .15], ...
        'String',['Plankton Taxon:  ',taxon,'         ',int2str(j),' / ',int2str(size(taxa,1))], ...
        'Style','text', ...
        'HorizontalAlignment','Left',...
        'Tag','StaticText Names', ...
        'TooltipString','Current taxon being batched with fraction processed');
    uicontrol('style','pushbutton','units','normalized','position',[.2 .1 .6 .25], ...
       'string','Batch Export Complete','backgroundcolor',redgreenblue, ...
       'callback','close(gcbf)','fontsize',12);
end

function output2bugs
h=gcbf; 
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,:));
    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 ProcessedCTDFiles');
    dummystring=get(h1,'String');v=get(h1,'value');
    vprnums=deblank(dummystring(v,:));
    if(strcmp(vprnums,'All Processed Files')),vprnums=dummystring(3:end,:);end
    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');
    taxa=deblank(dummystring(v,:));
    if(strcmp(taxa,'All Taxa')),taxa=dummystring(3:end,:);end  
    for i=1:size(vprnums,1)
        vprnum=deblank(vprnums(i,:));
        createbugsfile(basepath,cruise,vprnum,classifier,taxa);
        disp(['Conversion of ',vprnum,' completed. Progress: VPR ',int2str(i),' / ',int2str(size(vprnums,1))])
    end
    
    redgreenblue=[0.2157 .6353 .6353]*1.2;
    h0 = figure('Units','normalized', ...
        'Color',[redgreenblue], ...
        'MenuBar','none', ...
        'Name','VPR:  Convert Output to ''bugs'' Files', ...
        'NumberTitle','off', ...
        'Position',[.3    .5    0.4    0.2], ...
        'ToolBar','none'); 
    h1 = uicontrol('Parent',h0, ...
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue], ...
        'fontunits','normalized','fontsize',.67, ...
        'FontWeight','normal', ...
        'Position',[.05 .77 .4 .15], ...
        'String','Conversion Progress:', ...
        'Style','text', ...
        'fontweight','bold',...
        'HorizontalAlignment','Left',...
        'Tag','StaticText Header', ...
        'TooltipString','This window displays the progress of the batch export');
    h1 = uicontrol('Parent',h0, ...
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue], ...
        'fontunits','normalized','fontsize',.60, ...
        'FontWeight','normal', ...
        'Position',[.05 .6 .8 .15], ...
        'String',['Processed CTD File:  ',vprnum,'         ',int2str(i),' / ',int2str(size(vprnums,1))], ...
        'Style','text', ...
        'HorizontalAlignment','Left',...
        'Tag','StaticText Names', ...
        'TooltipString','Current VPR# being converted with fraction processed');
    h1 = uicontrol('Parent',h0, ...
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue], ...
        'fontunits','normalized','fontsize',.60, ...
        'FontWeight','normal', ...
        'Position',[.05 .45 .8 .15], ...
        'String',['Plankton Taxa Number:  ',int2str(size(taxa,1))], ...
        'Style','text', ...
        'HorizontalAlignment','Left',...
        'Tag','StaticText Names', ...
        'TooltipString','Number of taxa being converted');
    uicontrol('style','pushbutton','units','normalized','position',[.2 .1 .6 .25], ...
       'string','Conversion Complete','backgroundcolor',redgreenblue, ...
       'callback','close(gcbf)','fontsize',12);
end

function runbugs2avgbugs
h=gcbf; 
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,:));
    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 ProcessedCTDFiles');
    dummystring=get(h1,'String');v=get(h1,'value');
    vprnums=deblank(dummystring(v,:));
    if(strcmp(vprnums,'All Processed Files')),vprnums=dummystring(3:end,:);end
    h1=findobj(h,'Tag','PopupMenu Taxon');
    dummystring=get(h1,'String');v=get(h1,'value');
    taxa=deblank(dummystring(v,:));
    if(strcmp(taxa,'All Taxa')),taxa=dummystring(3:end,:);end
    h1=findobj(h,'Tag','EditText MetaData');
    metapath=get(h1,'String');
    for i=1:size(vprnums,1)
        vprnum=deblank(vprnums(i,:));
        bugs2avgbugs(basepath,cruise,vprnum,metapath);
        disp(['Conversion of ',vprnum,' completed. Progress: VPR ',int2str(i),' / ',int2str(size(vprnums,1))])
    end
    
    redgreenblue=[0.2157 .6353 .6353]*1.2;
    h0 = figure('Units','normalized', ...
        'Color',[redgreenblue], ...
        'MenuBar','none', ...
        'Name','VPR:  Convert ''bugs'' Files to ''avgbugs'' Files', ...
        'NumberTitle','off', ...
        'Position',[.3    .5    0.4    0.2], ...
        'ToolBar','none'); 
    h1 = uicontrol('Parent',h0, ...
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue], ...
        'fontunits','normalized','fontsize',.67, ...
        'FontWeight','normal', ...
        'Position',[.05 .77 .4 .15], ...
        'String','Conversion Progress:', ...
        'Style','text', ...
        'fontweight','bold',...
        'HorizontalAlignment','Left',...
        'Tag','StaticText Header', ...
        'TooltipString','This window displays the progress of the batch export');
    h1 = uicontrol('Parent',h0, ...
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue], ...
        'fontunits','normalized','fontsize',.60, ...
        'FontWeight','normal', ...
        'Position',[.05 .6 .8 .15], ...
        'String',['Processed CTD File:  ',vprnum,'         ',int2str(i),' / ',int2str(size(vprnums,1))], ...
        'Style','text', ...
        'HorizontalAlignment','Left',...
        'Tag','StaticText Names', ...
        'TooltipString','Current VPR# being converted with fraction processed');
    h1 = uicontrol('Parent',h0, ...
        'Units','normalized', ...
        'BackgroundColor',[redgreenblue], ...
        'fontunits','normalized','fontsize',.60, ...
        'FontWeight','normal', ...
        'Position',[.05 .45 .8 .15], ...
        'String',['Plankton Taxa Number:  ',int2str(size(taxa,1))], ...
        'Style','text', ...
        'HorizontalAlignment','Left',...
        'Tag','StaticText Names', ...
        'TooltipString','Number of taxa being converted');
    uicontrol('style','pushbutton','units','normalized','position',[.2 .1 .6 .25], ...
       'string','Conversion Complete','backgroundcolor',redgreenblue, ...
       'callback','close(gcbf)','fontsize',12);
end
   
function batch_help
load plot_help
load batch_help_text;

h0 = figure('Color',[0 0.85 0.85], ...
	'Colormap',mat0, ...
	'FileName','C:\plgui\mygui\plot_help.m', ...
    'MenuBar','none', ...
    'numbertitle','off', ...
	'Name','Batch Export Help', ...
	'PaperPosition',[18 180 576 432], ...
	'Units','normalized', ...
	'Position',[0.35 0.3 0.45 0.5], ...
	'Tag','batch_help_window', ...
	'ToolBar','none');
h1 = uicontrol('Parent',h0, ...
    'Units','normalized', ...
    'BackgroundColor',[0 .85 .85], ...
    'ForegroundColor',[0 0 .1], ...
	'ListboxTop',0, ...
    'Position',[.1 .85 .8 .1], ...
    'horizontalalignment','center', ...
    'fontunits','normalized','fontsize',.4,'fontweight','bold','fontname','helvetica',...
	'String',['Steps for Batch Exporting Plankton Data'], ...
	'Style','text', ...
	'Tag','StaticText1');
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[0 0.85 0.85], ...
	'Position',[.05 .1 .9 .8], ...
    'Style','listbox', ...
    'selectionhighlight','off', ...
    'String',batch_help_text, ...
    'fontunits','normalized','fontsize',.05, ...
    'fontname','helvetica',...
	'Tag','Listbox batch_help', ...
    'Value',1);
h1 = uicontrol('Parent',h0, ...
	'Units','normalized', ...
	'BackgroundColor',[0 0.85 0.85], ...
	'Callback','close(gcbf)', ...
	'fontunits','normalized','fontsize',.7,'FontWeight','normal', ...
	'ListboxTop',0, ...
	'Position',[.7 .025 .2 .05], ...
	'String','Exit Help', ...
	'Tag','Pushbutton1 Quit', ...
	'TooltipString','Exit this window');