function [bugs] = realbugplot(taxon,roidir,vprnum,LoadBugDataval,SaveBugDataval);


global basepathcruise b_full classid nxgrd nygrd imdir fn;

b=b_full;
p=b(:,4);
dtime=b(:,1);
lat=b(:,2);
lon=b(:,3);

fn2=fn;findx=find(fn2=='.');if ~isempty(findx),fn2=fn2(1:findx-1);end%remove file extension
basepathcruise=imdir(1:length(imdir)-13);


% VPR_Cal TEMPORARY UNTIL I FIX CAL FILE
image_volume = 10.557;
camrate = 30;
mm2pp = .00017778;

units='(number per liter)';
tdir=[basepathcruise,'\autoid\',taxon,'\'];

if LoadBugDataval,%load processed bug data    
    eval(['load ' basepathcruise filesep 'processed' filesep 'bugs' filesep 'binned' filesep taxon filesep  taxon '_' fn2 '_' classid]);
    if exist('bugsb'),aidmea=1;else,aidmea=0;end
else
    jtmin=min(dtime);
    jtmax=max(dtime);
    bugtime=[];
    bugarea=[];
    tday=[];
    a=dir([tdir 'aid\',classid,'*']);
    m=str2mat(a.name);
    tdirtmp=[];
    for j=1:size(m,1);
        tdirtmp=[tdirtmp;[tdir 'aid' filesep]];
    end
    m=[tdirtmp m];
    len=size(m,2);
    ftime=str2num(m(:,(len-6):(len-4)))+str2num(m(:,(len-1):(len)))/24;
    i=find(ftime>=jtmin-1/24 & ftime<=jtmax);
    
    aidmea=exist([tdir 'aidmea',filesep]);
    if aidmea
        a=dir([tdir 'aidmea',filesep,classid,'*']);
        mb=str2mat(a.name);
        tdirtmp=[];
        for j=1:size(mb,1);
            tdirtmp=[tdirtmp;[tdir 'aidmea',filesep]];
        end
        mb=[tdirtmp mb];
    end
    
    %loop for reading aid files
    firsttime=1;
    for j=1:length(i);
        if aidmea,
            eval(['atmp=load(''' mb(i(j),:) ''');'])
            eval(['bugarea1=atmp; clear atmp']);
        end
        [s,f]=dos(['type ' m(i(j),:)]);
        if isempty(f),continue,end
        k=find(f==setstr(10));
        ik=find(diff(diff(k))~=0);
        if isempty(ik);%if filenames dont change in #of chars 
            %(eg. dont cross over 100000000)
            k=k(1)-1;
            f=reshape(f(find(f~=setstr(10))),k,length(find(f==setstr(10))))';
            msindx=find(f(1,:)=='.');%# dots in string bounding ms time.
            ms1=msindx(1)+1;ms2=ms1+7;
            tday=str2num(f(:,ms1:ms2))/1000/60/60/24;
            %The following adds 1 ms between multiple rois on an image. 
            %Older autoid results only had ms and not roi #, so 1st checks # characters
            if (msindx(2)-msindx(1))==11,
                tday=tday+str2num(f(:,ms2+1:ms2+2))/86400000;
            end
            if firsttime,
                vprindx=strfind(f(1,:),'vpr');%these three lines find the starting vpr tow #
                firsttime=0;
                if ~isempty(vprindx),
                    slashindx=findstr(f(1,:),filesep);
                    vprtowstr=f(1,(vprindx+3:slashindx(find(slashindx==vprindx-1)+1)-1));
                else
                    vprtowstr='0';
                end
            end
        else %ie there is a shift in # chars, assume shift across 100000000
            k1=k(1)-1;
            k2=k(ik+2)-1-k(ik+1);
            f1=f(1:k(ik+1));
            f2=f(k(ik+1)+1:length(f));
            f1=reshape(f1(find(f1~=setstr(10))),k1,length(find(f1==setstr(10))))';
            f2=reshape(f2(find(f2~=setstr(10))),k2,length(find(f2==setstr(10))))';
            tday=str2num(f1(:,(k1-14):(k1-7)))/1000/60/60/24;%>10000/0000 lists first
            tday=[tday;str2num(f2(:,(k2-15):(k2-7)))/1000/60/60/24];
        end;
        bugtmp=tday+str2num(m(i(j),(len-6):(len-4)));
        if str2num(m(i(j),(len-1):(len)))<23,
            bugtmp(find(tday>=1))=bugtmp(find(tday>=1))-1;%if time> 1 day
        end;
        bugtime=[bugtime;bugtmp];
        if aidmea,bugarea=[bugarea;bugarea1(:,2)];end
    end;
    
    ibt=find(bugtime>jtmin&bugtime<jtmax);
    bugtime=bugtime(ibt);
    
    if aidmea,
        bugarea=bugarea(ibt)*mm2pp;%convert from units of pixels to mm2
        bugradius=sqrt(bugarea/pi);
        bugvolume=bugarea.*bugradius*4/3; % equivalent spherical volume in cubic millimeters.
        %We can roughly approximate micrograms of carbon by dividing above by 1000 to get cubic 
        %centimeters = milliliters = grams of water at STD ~= grams of plankton since 
        %density of plankton is near unity for the wet mass conversion.
        %then multiply by 10^6 to get microliters or micrograms wet weight
        %then carbon estimated as 10% of wet weight since dry weight about 30% of wet 
        %weight and carbon is about 30% of dry weight
        %so multiply above by 100.
        bugcarbonmass=bugvolume*100;
    end
    
    clear bugs;bugs=[];
    [bugs indx]=histc(bugtime,dtime,1);
    
    if aidmea,
        bugsb=zeros(size(bugs));
        if length(bugcarbonmass)~=length(indx);
            mlen=min([length(bugcarbonmass) length(indx)]);
            bugcarbonmass=bugcarbonmass(1:mlen);
            indx=indx(1:mlen);
        end
        for k=1:length(indx),
            bugsb(indx(k))=sum(bugcarbonmass(indx==indx(k)));
        end
        bugsb=bugsb(1:length(bugsb)-1)./diff(dtime);
        bugsb(isnan(bugsb))=0;
        bugsb=bugsb/86400;% bug carbon biomass / s
        bugsb=[bugsb; bugsb(length(bugsb))];
        bugsb=bugsb/(image_volume*camrate/1000); % carbon biomass per liter
    end
    
    bugs=bugs(1:length(bugs)-1)./diff(dtime);
    bugs(isnan(bugs))=0;
    bugs=bugs/86400;%# bugs / s
    bugs=[bugs; bugs(length(bugs))];%repeat last point to make array same size as hydro data
    bugs=bugs/(image_volume*camrate/1000); % number of individuals per liter; camrate is # images/second, 
    
end%if LoadBugDataval: i.e., if load processed bug data

if SaveBugDataval,%if save processed bug data
    name=taxon;
    bugtime_all=interp1(b(:,1),b(:,2:10),bugtime);%interpolate b to bugtime
    if aidmea,
        bugtime_all=[bugtime,bugtime_all,bugradius];
    else
        bugtime_all=[bugtime,bugtime_all];%add 1st column = bugtime
    end
    
    col_headings='1=bugtime  2=lat  3=lon  4=press  5=temp  6=salt  7=sigma  8=fluo  9=atte  10=light  11=bug_radius';
    if exist([basepathcruise '\processed\bugs\individuals\' name],'dir')~=7,
        dos(['mkdir ' basepathcruise '\processed\bugs\individuals\' name]);
    end
    if exist([basepathcruise '\processed\bugs\binned\' name],'dir')~=7,
        dos(['mkdir ' basepathcruise '\processed\bugs\binned\' name]);
    end
    eval(['save ' basepathcruise '\processed\bugs\individuals\' name '\'  name '_all_' fn2 '_' classid ' bugtime_all col_headings']);
    if aidmea,
        eval(['save ' basepathcruise '\processed\bugs\binned\' name '\'  name '_' fn2 '_' classid ' dtime bugs bugsb']);
    else
        eval(['save ' basepathcruise '\processed\bugs\binned\' name '\'  name '_' fn2 '_' classid ' dtime bugs']);
    end
end

