function CNNClassify(input,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)
    %Grab everything entered above
	tic
    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,:));
    if ~strcmp(input,'c')
        h1=findobj(h,'Tag','PopupMenu VPR Number');
        dummystring=get(h1,'String');v=get(h1,'value');
        vpr=deblank(dummystring(v,:));
    end
    if strcmp(input,'h')
    	h1=findobj(h,'Tag','PopupMenu Yearday');
        dummystring=get(h1,'String');v=get(h1,'value');
        yearday=deblank(dummystring(v,:));
    	h1=findobj(h,'Tag','PopupMenu Hour');
        dummystring=get(h1,'String');v=get(h1,'value');
        hour=deblank(dummystring(v,:));
    end
    %Arrange for input into Python:
    inputpath=[disc folder filesep cruise filesep 'rois'];
    if(~strcmp(input,'c')),inputpath=[inputpath filesep vpr];end
    if(strcmp(input,'h')),inputpath=[inputpath filesep yearday filesep hour];end
    outputpath=[outputdisc outputfolder filesep cruise filesep 'CNN_csv'];
    [~,classifiername,~]=fileparts(fileparts(classifier));%Separate out the name of the classifier
    if(strcmp(input,'c')),outcsv=[input classifiername];end
    if(strcmp(input,'v')),outcsv=[vpr '.' classifiername];end
    if(strcmp(input,'h')),outcsv=[vpr '.' yearday '.' hour '.' classifiername];end
    cmdtxt=['! conda activate ifcb && cd C:\plgui\ifcb-run\ && python neuston_run.py ' inputpath ' --model ' classifier ' --outdir ' outputpath ' --outfile ' outcsv '{dir}.csv'];
    if(strcmp(input,'c')),disp(['Classifying all of ' cruise '...']),end
    if(strcmp(input,'v')),disp(['Classifying ' cruise ' ' vpr '...']),end
    if(strcmp(input,'h')),disp(['Classifying ' cruise ' ' vpr ' ' yearday ' ' hour '...']),end
    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=2:size(data,1)
            if ~strcmp(input,'h')
                [yearday,hour,~]=fileparts(fileparts(char(data(j,1))));
                [~,yearday,~]=fileparts(yearday);
            end
            if num(1,j-1)>qc %Quality Control Check
                txt=[char(data(j,2)),'(size(',char(data(j,2)),',1)+1,:)={yearday,hour,data(j,1)};'];
                eval(txt);
            else
                unknown(size(unknown,1)+1,:)={yearday,hour,data(j,1)};
            end
        end
    end
    path=[outputdisc outputfolder filesep cruise];
    for i=1:size(names,1)
        taxas(i,:)=[char(names(i,:)),blanks(50-length(char(names(i,:))))];
        eval(['chk=size(' char(names(i,:)) ',1);'])
        if chk>0
            eval(['taxon=' char(names(i,:)) ';']);
            taxondir=[path filesep 'autoid' filesep char(names(i,:)) filesep 'aid' filesep];
            if ~exist(taxondir,'dir'),mkdir(taxondir);end
            for j=1:size(taxon,1)
                aidfile=[classifiername,'aid.',char(taxon(j,1)),'.',char(taxon(j,2))];
                imfiles=[];
                if ~isempty(dir([taxondir aidfile])),
                    d=dir([taxondir aidfile]);
                    if d.bytes~=0;
                        imfiles = loadname([taxondir,aidfile]);
                    end
                end
                imfiles=[imfiles;[char(taxon{j,3}) char(10)]];
                fid=fopen([taxondir aidfile],'w');
                fprintf(fid,'%c',imfiles');
                fclose(fid);
            end
        end
    end
    if ~exist([path filesep 'clpar'],'dir'),mkdir([path filesep 'clpar']);end
    save([path filesep 'clpar' filesep classifiername '.mat'],'taxas')
    if(strcmp(input,'c')),disp([cruise ' classification complete.']),end
    if(strcmp(input,'v')),disp([cruise ' ' vpr ' classification complete.']),end
    if(strcmp(input,'h')),disp([cruise ' ' vpr ' ' yearday ' ' hour ' classification complete.']),end
    toc
end

