function [C,E] = conc_sw(A,h,Vw,level,Astd1,Sstd1,nstd1);
% Ledwell, 9/9/05
% Calculates concentration C and uncertainty E for profiles.m
% for samples from EDDIES Tracer 2 cruise
% A is a list of areas; h is a list of peak heights
% Vw is a list of water volumes
% level is a list of the fractions of the distance of the water level
% between the lower and upper sensor electrodes in the tower, from which
% Vw can be estimated if Vw is NaN
% The mean Vw is 46.6 ml, to be used in the absence of any other info.

Vw0=0.0465      % default value of the water volume in the tower, in liters
A0=880;			% The apparent area blank on the second day
Alim=0;			% trigger for peak height calculation (0 makes it moot)
eff=1.00;		% sparge efficiency
B=0.0;			% (fM) estimate of the system blank for a sparged sampled
					% which includes the area blank, so that A0 has been set to 0.
EA=400;			% Area error at low levels, including variations in A0 and B.
ER=0.03;		% Fractional area error at all levels, due to volume variations
				% and chromatographic variations            

par1 = []; par2 = []; C = []; E = [];
y1 = 0; y2 = 0;
fignum = 1;

  nste1 = [0 nstd1];
  Aste1 = [A0 Astd1];
  Sste1 = [EA Sstd1];
  ns1=length(nste1);
  
  % Plot of the standard curve:

  figure(1); fignum = 2; clf; plot(Aste1,nste1,'o'); hold on;
  par1(1,:)=[0 polyfit(Aste1(1:2),nste1(1:2),1)];
  scl=500; Afit1 = [0:ceil(Aste1(2)/scl)]*scl;
  plot(Afit1,polyval(par1(1,:),Afit1),'--')

  for i=2:ns1-1
    par1(i,:)=polyfit(Aste1([1 i i+1]), nste1([1 i i+1]),2);
    Afit1 = [0:ceil(Aste1(i+1)/scl)]*scl;
    plot(Afit1,polyval(par1(i,:),Afit1))
  end 
  hold off; xerrbar(Aste1,nste1,Sste1); 
   xlabel('Area'); ylabel('n  (fmoles)'); 
  title('Trap 2 Standards');
  
  % Calculation of C and E for the sample reaching the GC

n = length(A);

j=find(~isnan(A) & ~isnan(h));
figure(2)
[fit,par]=try_fit(h(j),A(j),1);
kp=find(isnan(A) & ~isnan(h));
A(kp)=polyval(par,h(kp));

Vw=Vw/1000;
iv=find(isnan(Vw) & ~isnan(level))
Vw(iv)=0.001*(43+level(iv)*5.5);

for i = 1:n
   if ~isnan(A(i))
    j=find(Aste1(1:ns1-1) <= A(i) & Aste1(2:ns1) > A(i));
     if isempty(j)
        if A(i) < 1.1*Aste1(ns1)
          j = ns1-1;
        else
          disp(['trap 2: sample ',int2str(i),' is out of range'])
          disp(A(i))
        end
     end
     if ~isempty(j)
         if isnan(Vw(i))
             Vw(i)=Vw0;
         end
      C(i) = polyval(par1(j,:),A(i))/(Vw(i)*eff) - B;
      E(i) = ((ER*C(i))^2 + (EA*par1(1,2)/Vw(i))^2)^.5;
     end
 else C(i)=NaN; E(i)=NaN;
  end
end