%% Step 3 of CNN Threshold Process:
%Copy a subset of ROIs into sperate folders for classification.

roinum=200;%Number of rois to copy
outputfolder='diatom_test';%Output is within threshold folder
disp('Running...')
%To Seperate from index position in output vprtimebugs
vpr='vpr2';%Cast or tow#, in rois
row=21220;%Target time stamp, creates a sub folder within output folder

drive=['F:' filesep 'data' filesep];%Change to fit appropriate VP input
cruise='TN368_VPRII';
filename='07072053_vprtimebugs';%Everything before .mat
load([drive cruise filesep 'processed' filesep 'merged' filesep filename '.mat']);
time=b(row,1);
day=['d' int2str(floor(time))];
if(length(day)<4),day=['d0' day(2:end)];end
if(length(day)<4),day=['d0' day(2:end)];end%duplicated in case it's a single digit day
time=time-floor(time);%Remove day to get hour and bugtime
hour=['h' int2str(floor(time*24))];
if(length(hour)<3),hour=['h0' hour(2)];end
bugtime=time*60*60*24*1000;%1000 is for ms.
roipath=[drive cruise filesep 'rois' filesep vpr filesep day filesep hour filesep];
d=dir([roipath 'roi*']);
e='';
for i=1:length(d)
    e(i,1:8)=d(i).name(6:13);%Grab just bugtime from roiname (last two zeroes are not bugtime)
end
[e,eind]=sort(str2num(e));%This is to remove numbers before decimal.
[~,ref]=min(abs(e-bugtime));%find nearest bugtime
%Copy rois to output folder
outpath=[drive cruise filesep 'threshold' filesep outputfolder filesep int2str(row)];
if(~exist(outpath,'dir')),mkdir(outpath);end
for i=0:roinum-1
    copyfile([roipath d(eind(ref+i)).name],[outpath filesep d(eind(ref+i)).name]);
end
disp('Complete.')