loadit=0;
if loadit;
    clear all; close all;
    flist=dir('./*pstat.mat'); nfiles=length(flist);
    disp(['Processing ' int2str(nfiles) ' files']);
    Q = [];
    for F=1:nfiles;
        cfile = flist(F).name; eval(['load ' cfile]);
        npart = length(PartStats);
        if npart>0;
            disp([cfile ': ' int2str(npart) ' particles']);
            q=[PartStats.MajorAxisLength PartStats.MinorAxisLength PartStats.Area PartStats.Volume];
            q=reshape(q,npart,4);
            inum = eval(cfile(5:8)); inum=repmat(inum,npart,1);
            q=[inum q];
            % keyboard
            % if any(q(:,3)>100); disp('*** contains particles with MA>100'); end
            Q=[Q;q];
        end
    end
    vn = {'Image Number','Major Axis','Minor Axis','Area','Volume'};
    save PartStats Q vn
    return
end

firstlook=1;
if firstlook
    clear all; close all;
    load PartStats
    nvars=length(vn); nim=length(unique(Q(:,1)));
    g=find(Q(:,1)>=385);
    
    edg = [0:50:1600];
    
    % Group numbers (image numbers associated with each experiment /
    % replicate).
    GN = [385 416; 461 492; 494 525; 526 557; 558 589; 598 629; 630 661; 662 694; 695 726;...
        728 754; 755 786; 788 819; 820 851; 852 883; 884 915; 916 948; 949 980; 981 1012; 1013 1044;...
        1045 1076; 1077 1108];
    ng = length(GN); 
    % edg = [0:100:1200];
    
    % Major axis, all image groups
    figure(1); clf; sfig; set(gcf,'Name','Major Axis histograms, by experiment'); 
    for G=1:ng;
        idx = find(Q(:,1)>=GN(G,1) & Q(:,1)<=GN(G,2)); % & Q(:,2)>200);
        cdat=Q(idx,2);
        eval(['subplot(3,7,' int2str(G) ')']);
        % d = histc(Q(idx,2),edg); hist(d,edg); set(gca,'Xlim',[0 1200]);
        hist(cdat);
        title(['Group ' int2str(G)]);
    end
    
    % Bin replicates to examine statistics for each of the 7 copepod
    % concentration levels
    nconc=7;
    for C=1:nconc;
        s = (C-1)*3+1; e=s+2;  % start and end group numbers
        disp(['start = ' int2str(s) ', end = ' int2str(e)]);
        % expand the image id numbers into a vector
        sub=[]; 
        for q = s:e; 
            csub = [GN(q,1):GN(q,2)]'; sub=[sub;csub]; 
        end
        eval(['group' int2str(C) '=Q(sub,:);']);
    end
    
    % view
    figure(2); clf; sfig; set(gcf,'Name','Major Axis histograms, grouped replicates'); 
    for C=1:nconc;
        eval(['cg=group' int2str(C) ';']); cdat=cg(:,2); 
        eval(['subplot(24' int2str(C) ')']);
        d = histc(cdat,edg); hist(cdat,edg);
        axis([-10 1600 0 50]);
        set(gca,'Xtick',[0:200:1600],'Xgrid','on');
        idx=find(cdat>200); 
        title(['Group ' int2str(C) ': ' int2str(length(idx)) ' particles with MA>200']); 
        % hist(cdat); 
    end
    
    % Aggregate all of the experiment data (excluding the images between
    % experiments now)
    expdata = [];
    for C=1:nconc;
        eval(['q = group' int2str(C) ';']); expdata = [expdata; q];
    end
    
    % Identify images having particles with a major axis dimension
    % exceeding 100 microns
    figure(3); set(gcf,'Name','Major axis histograms, all experiments'); 
    hist(expdata(:,2),100);
    idx=find(expdata(:,2)>200); q=unique(expdata(idx,1));
    disp('Images having particles with major axis > 200 microns:'); int2str(q)
end

