function CNNClassifyUnderway(qc,fig)
%This function classifies ROIs using a given CNN model.py file. The script
%that calls this script is called 'PLCNNmenu'. 
%Input options:
%'c': classify the entire cruise
%'v': classify a single VPR cast/tow
%'h': classify a specific hour
%qc (Quality Control) is the confidence threshold for an image to be sorted
%nto an autoid folder. Anything less is sorted into unknown. 
%Default is qc=0.5.
if nargin<2
    qc=0.5;
    h=gcbf;
elseif nargin<3
	h=gcbf;
else
	h=fig;
end;
if ~isempty(h)
    tic
    %Grab everything entered above
	h1=findobj(h,'Tag','EditText Classifier Path');
    dummystring=get(h1,'String');classifier=deblank(dummystring);
    h1=findobj(h,'Tag','PopupMenu AutoID Drive');
	dummystring=get(h1,'String');v=get(h1,'value');
	outputdrive=deblank(dummystring(v,:));
    h1=findobj(h,'Tag','PopupMenu AutoID Drive');
	dummystring=get(h1,'String');v=get(h1,'value');
	outputdisc=deblank(dummystring(v,:));
	h1=findobj(h,'Tag','PopupMenu AutoID Folder');
	dummystring=get(h1,'String');v=get(h1,'value');
	outputfolder=deblank(dummystring(v,:));
	h1=findobj(h,'Tag','PopupMenu ROI Drive');
	dummystring=get(h1,'String');v=get(h1,'value');
	disc=deblank(dummystring(v,:));
	h1=findobj(h,'Tag','PopupMenu ROI Folder');
	dummystring=get(h1,'String');v=get(h1,'value');
	folder=deblank(dummystring(v,:));
	h1=findobj(h,'Tag','PopupMenu Cruise');
	dummystring=get(h1,'String');v=get(h1,'value');
    cruise=deblank(dummystring(v,:));
    h1=findobj(h,'Tag','PopupMenu VPR Number');
    dummystring=get(h1,'String');v=get(h1,'value');
    vpr=deblank(dummystring(v,:));
    %Arrange for input into Python:
    inputpath=[disc folder filesep cruise filesep 'rois' filesep vpr];
    outputpath=[outputdisc outputfolder filesep cruise filesep 'CNN_csv'];
    [~,classifiername,~]=fileparts(fileparts(classifier));%Separate out the name of the classifier 
    %Loop to collect and analyze data while it is taken
    idle=0;
    yeardayind=3;%First Day
    hourind=3;%First Hour
    roiind=3;%First ROI;
    while idle<12
        vprdir=dir(inputpath);
        if(size(vprdir,1)>=yeardayind),yearday=vprdir(yeardayind).name;end
        inpath=[inputpath filesep yearday];
        yeardaydir=dir(inpath);
        if(size(yeardaydir,1)>=hourind),hour=yeardaydir(hourind).name;end
        inpath=[inpath filesep hour];
        roidir=dir(inpath);
        roilist={};
        if(size(roidir,1)>=roiind);
            idle=0;%Reset idle count
            roidir=struct2cell(roidir)';
            if roiind==3 && strcmp(roidir{3,1},'ctd.dat'),roiind=4;end
            roilist(:,2)=roidir(roiind:end,1);
            roilist(:,1)={[inpath filesep]};
            roilist(:,3)={char(10)};
            roilist=cell2mat(roilist);
            if ~exist(outputpath,'dir'),mkdir(outputpath);end
            txtfile=[outputpath filesep 'underwaylist'];
            if exist([txtfile '.txt'],'file'),movefile([txtfile,'.txt'],txtfile);end
            fid=fopen(txtfile,'w');
            fprintf(fid,'%c',roilist');
            fclose(fid);
            movefile(txtfile,[txtfile '.txt']);
            txtfile=[txtfile,'.txt'];
            outcsv=['u' classifiername '.' yearday '.' hour];
            cmdtxt=['! conda activate ifcb && cd C:\plgui\ifcb-run\ && python neuston_run.py ' txtfile ' --input-type "list" --model ' classifier ' --outdir ' outputpath ' --outfile ' outcsv '.csv' ' --loaders 4' ' --batch-size 108'];
            disp(['Classifying ' cruise ' ' vpr ' ' yearday ' ' hour '...'])
            eval(cmdtxt)
            %Identify .csvs just created (have 'c' (for cruise mode) prefix)
            disp('Converting to AutoID...')
            results=dir(outputpath);
            j=0;
            for i=3:size(results,1)
                if strcmp(results(i).name(1:min([length(results(i).name),length(outcsv)])),outcsv);
                    j=j+1;
                    result(j,:)=results(i).name;
                end
            end
            %Compile autoid files
            [~,classifiername,~]=fileparts(fileparts(classifier));%Separate out the name of the classifier
            prefix=length(inputpath);
            for i=1:size(result,1)
                data=importdata([outputpath filesep result(i,:)]);
                num=max((data.data)');
                data=data.textdata;
                if i==1,%Initialize all taxa
                    names=data(1,3:end)'; %List of all taxa names
                    names(end+1,:)={'unknown'};%For those that fail quality control
                    for j=1:size(names,1),eval([char(names(j,1)),'={};']);end
                end
                for j=roiind+1:size(data,1)
                    if num(1,j-1)>qc %Quality Control Check
                        txt=[char(data(j,2)),'(size(',char(data(j,2)),',1)+1,:)={[char(data(j,1)) char(10)]};'];
                        eval(txt);
                    else
                        unknown(size(unknown,1)+1,:)={[char(data(j,1)) char(10)]};
                    end
                end
            end
            path=[outputdisc outputfolder filesep cruise];
            for i=1:size(names,1)
                if ~exist([path filesep 'clpar' filesep classifiername '.mat'],'file'),taxas(i,:)=[char(names(i,:)),blanks(50-length(char(names(i,:))))];end
                eval(['chk=size(' char(names(i,:)) ',1);'])
                if chk>0
                    eval(['taxon=' char(names(i,:)) ';']);
                    taxon=cell2mat(taxon);
                    taxondir=[path filesep 'autoid' filesep char(names(i,:)) filesep 'aid' filesep];
                    if ~exist(taxondir,'dir'),mkdir(taxondir);end
                    aidfile=[classifiername,'aid.',yearday,'.',hour];
                    imfiles=[];
                    if ~isempty(dir([taxondir aidfile])),
                        d=dir([taxondir aidfile]);
                        if d.bytes~=0;
                            imfiles = loadname([taxondir,aidfile]);
                        end
                    end
                    imfiles=[imfiles;taxon];
                    fid=fopen([taxondir aidfile],'w');
                    fprintf(fid,'%c',imfiles');
                    fclose(fid);
            	end
            end
            if ~exist([path filesep 'clpar'],'dir'),mkdir([path filesep 'clpar']);end
            if ~exist([path filesep 'clpar' filesep classifiername '.mat'],'file'),save([path filesep 'clpar' filesep classifiername '.mat'],'taxas'),end
            roiind=size(roidir,1)+1;%Set roiind to after the last ROI classified
            disp(['Classified ' int2str(size(roilist,1)) ' ROIs.'])
        else
            pause(5)
            if idle>0 && size(yeardaydir,1)>=hourind+1
                hourind=hourind+1;
                roiind=3;
            elseif idle>0 && size(vprdir,1)>=yeardayind+1
                yeardayind=yeardayind+1;
                hourind=3;
                roiind=3;
            else
                idle=idle+1;%Increment if no new ROIs or folders
                toc
                disp(['Idle for ' int2str(idle*5) ' seconds.'])
            end
        end
    end
    disp([cruise ' ' vpr ' underway classification complete.'])
end
