function gridstats(fstr, trans, varargin)

% set grid and percentage sizes to be analyzed
grd = [60 120 180 300 450 600 900 1800]; % grid sizes
per = [30 25 20 15 10 5]/100;  % percentage cutoff
perL = {'30%','25%','20%','15%','10%','5%'}; % label for percentages

% this function generates a lot of plots, so close all open plots
close all;

%fprintf 'Hi Dru';

% set plot scale for ddd vector plots
scl = 0;
if (nargin>2 & ~isempty(varargin))
    scl = varargin{1};
end

% plot *all* DD vectors and link with subsequent plots
b = strsplit(fstr,'.'); 
bq = strsplit(fstr,'/');

celldisp(b);

celldisp(bq);

fname = [fileparts(fstr) '/vmacdhor.' sprintf('%s.%s.%s',b{4:6})];
%fname = [fileparts(fstr) '/vmacdhor.' sprintf('%s.%s.%s',b{2:4})]


%fprintf 'Dru, here is your file name of all CD vectors: ';
%disp(fname);

[pid,pt] = plotVectors(fname,1);
ax(1) = gca;

% header for output table:
fprintf('%5s%10s%10s%10s%10s%10s%10s%10s\n','min','grid',perL{:});

% loop through grids; plot and generate/output stats
%warning('off');
%fprintf 'Dru, here is length of grd: ';
%disp(length(grd));

%fprintf 'Dru, this is fstr: ';
%disp(fstr);

for i = 1:length(grd)
    fn = sprintf('%s.%d',fstr,grd(i));
%    fname = sprintf('%s.%s.%s.%d',fstr,grd(i));
%    fn = strcat(fstr,'.',grd(i));
%    fprintf 'Dru, here is fn in a loop: '
%    disp(fn);
%    [p,v] = readVectorFile(fname);
    [p,v] = readVectorFile(fn);
    
    %figure; histogram(v(:,4)); title(fname);
    
    N = length(v(:,4));
    for j = 1:length(per)
        q = find(v(:,4)>per(j)*trans); % points > per(i)% 
                                    % of transformation size
        r(j,i) = (length(q)/N)*100;
    end
    fprintf('%5.1f%10d%10.1f%10.1f%10.1f%10.1f%10.1f%10.1f\n',...
        grd(i)/60, grd(i),r(:,i));
    
    % plot vector field
    [pid,pt] = plotVectors(fn,scl);
    orient landscape
    print('-dpdf',sprintf('ddd_%d.pdf',grd(i)));
    ax(i+1) = gca;
end

linkaxes(ax,'xy');

%warning('on')
figure
bar(r);
set(gca,'XTickLabel',perL)
legend(num2str(grd'./60),'location','northwest')
grid on
xlabel('% of median transformation size')
ylabel('% of (t/d)dd vectors exceeding that level')
