function retval = dataerrornorms(A,B,C,E,X,Y,M,D,a,dr,T,dt,RadialDist,count,dataprofiles,pr)
% usage: retval = dataerrornorms(A,B,C,E,X,Y,M,D,a,dr,T,dt,RadialDist,count,dataprofiles,pr)
% description: returns average relative error norm relative to data for
% model dataprofiles input. Errors are obtained from vector pr, which is
% agumented so that the first entry is 1. No error checking.

% for predictability...unrem the next line
rand('seed',2);
retval = zeros(size(pr));
pr = pr(:)'; % make sure it's a row
pr = [1,pr];
lenpr = length(pr);
% find max radius
radmax = max([norm([X,Y]-[A,C]),norm([X,Y]-[A,E]), ...
        norm([X,Y]-[B,C]),norm([X,Y]-[B,E])]);
radii = ceil(radmax/dr); % number of space steps
spacenodes = linspace(0,dr*radii,radii+1);
spacepoints = linspace(dr/2,dr*(radii-0.5),radii);
timenodes = linspace(0,dt*T,T+1);
retval = zeros(size(pr));
% calculate norm of data generated 'N(r,t)'
integrand = zeros(size(spacepoints));
for ii = 1:T
  integrand = integrand + ...
  (dataprofiles(ii).^2).*spacepoints;
end
normN = sqrt(sum(integrand)*dr*dt);
% calculate error norm for p = 1
events = RadSim(A,B,C,E,X,Y,M,D,a,dr,T,dt,pr(1),RadialDist);
cumulprofiles = RadialProfiles(X,Y,dr,timenodes,events);
[m,n] = size(cumulprofiles);
if (n < radii) % pad the profiles
  cumulprofiles = [cumulprofiles,zeros(m,radii-n)];
end
integrand = zeros(size(spacepoints));
for ii = 1:T
  integrand = integrand + (cumulprofiles(ii,:) - ... 
  dataprofiles(ii)).^2.*spacepoints;
end
retval(1) = sqrt(sum(integrand)*dr*dt);
% calculate the errors with probability less than 1
for ii = 2:lenpr
  P = pr(ii);
  % accumulate profile samples
  for jj = 1:count
    events = RadSim(A,B,C,E,X,Y,M,D,a,dr,T,dt,P,RadialDist);
    profiles = RadialProfiles(X,Y,dr,timenodes,events);
    [m,n] = size(profiles);
    if (n < radii) % pad the profiles
      profiles = [profiles,zeros(m,radii-n)];
    end
    cumulprofiles = cumulprofiles + profiles;
  end
  cumulprofiles = cumulprofiles/count;
  integrand = zeros(size(spacepoints));
  for kk = 1:T
    integrand = integrand + (cumulprofiles(kk,:)- dataprofiles(kk)).^2.*spacepoints;
  end
  retval(ii) = sqrt(sum(integrand)*dr*dt);
end
retval = retval/normN;
end

