function createbugsfile(basepath,cruise,vprnum,classifier,taxa)
%This code combines Visual Plankton Matlab output into vpr#bugs.m files in
%the form of sensordata+all taxa output from batch exporting.

variables=[];data=[];%For only vpr output
%Uncomment for ctd data
sn=[basepath filesep cruise filesep 'processed' filesep 'sensordata' filesep vprnum '.mat'];
if exist(sn,'file')
    load(sn);
    if strcmp(vprnum(1:3),'vpr')%If vpr, assume DAVPR
        variables=['pressure',blanks(50-length('pressure'));...  
                   'temperature',blanks(50-length('temperature'));...
                   'salinity',blanks(50-length('salinity'));...
                   'sigma-t',blanks(50-length('sigma-t'));... 
                   'chlorophyll',blanks(50-length('chlorophyll'));...
                   'optbackscatter',blanks(50-length('optbackscatter'))];
        data=b(:,4:9);
        data(:,1)=-data(:,1);
    else%If not, assume VPRII
        variables=['Year-Day',blanks(50-length('Year-Day'));...
                   'Latitude',blanks(50-length('Latitude'));...
                   'Longitude',blanks(50-length('Longitude'));...
                   'Depth',blanks(50-length('Depth'));...
                   'Temperature',blanks(50-length('Temperature'));...
                   'Salinity',blanks(50-length('Salinity'));...
                   'Density',blanks(50-length('Density'));...
                   'Fluorescence',blanks(50-length('Fluorescence'));...
                   'Turbidity',blanks(50-length('Turbidity'));...
                   'PAR',blanks(50-length('PAR'));...
                   'Oxygen',blanks(50-length('Oxygen'))];
         data=b(:,[1:10,12]);
         if strcmp(vprnum(end-5:end),'_combo')%Check for ECO Triplet
             variables=[variables;...
                   'ECO Bb',blanks(50-length('ECO Bb'));...
                   'ECO Chlorophyll',blanks(50-length('ECO Chlorophyll'));...
                   'ECO CDOM',blanks(50-length('ECO CDOM'))];
             data=[data,b(:,16:18)];
             vprnum=vprnum(1:end-6);%remove _combo
         else %Assume .y## vprlog, remove _y##
             vprnum=vprnum(1:end-4);
         end
    end
    volumeimaged=1;

    for j=1:size(taxa,1)
        taxon=deblank(taxa(j,:));
        fn=[basepath filesep cruise filesep 'processed' filesep 'bugs' filesep 'binned' filesep taxon filesep taxon '_' vprnum '_' classifier '.mat'];
        if exist(fn,'file')
            load(fn);
            n=1;
            if b(1,1)<=dtime(n)
                m=find(b(:,1)==dtime(n));
            else
                n=find(dtime(:)==b(1,1));
            end
            o=length(dtime);
            if b(end,1)<dtime(end)
                o=find(dtime(:)==b(end,1));
            end
            temp=bugs(n:o);
            if length(data(m:end,1))<length(temp)%Weird camera rate issue not matching exactly for VPRII
                temp=interp1(dtime(n:o),bugs(n:o),data(m:end,1));
            end
            var=[taxon,blanks(50-length(taxon))];
            variables=[variables;
                    var];
            data(m:end,end+1)=temp/volumeimaged;
            clear m n o bugs bugsb dtime temp;
        end
        clear fn;
    end
    
    if exist([basepath filesep cruise filesep 'processed' filesep 'merged'],'dir')~=7,
        dos(['mkdir ' basepath filesep cruise filesep 'processed' filesep 'merged']);
    end
    
    savepath=[basepath filesep cruise filesep 'processed' filesep 'merged' filesep vprnum 'bugs.mat'];
    eval(['save ' savepath ' data variables'])
    
    clear b b_column_headings data 'variables';

end