function out =  ep_cmplNc(infile)
% ep_cmplNc - Compile useful values from a eqpac OMS netcdf file
%
% Use as: out =  ep_cmplNc(infile)
%
% Inputs: infile = an eqpac OMS netcdf object
%
% Ouputs: out    = structure with the following fields:
%          .t     = time (corresdponds to rows in the variables
%          .lambda = wavelengths of columns (nm)
%          .Es     = Surface Irradiance
%          .Lu     = 20m Radiance
%          .Ed     = 20m Irradiance
%          .z      = Depth of 20m sensors (m)
%          .K      = Attenuation coefficeint
%          .Lw     = Water-leaving radiance 
%          .Rrs    = Remote sensing ration (Lw/Es)
%          .oc     = OC chlorphyll
%          .oc_cov = variance between varios oc values
%
% Note: missing values (i.e -999) are removed

% Brian Schlining
% 11 May 2000
% 06 Jun 2000; Modified name and help

out.t = nctime(infile);                % Time in serial days

out.lambda = [412 443 490 510 555];    % nm

out.Es = [infile{'Ed412_0_1'}(:) ...   % Surface radiance
      infile{'Ed443_0_2'}(:) ...
      infile{'Ed490_0_3'}(:) ...
      infile{'Ed510_0_4'}(:) ...
      infile{'Ed555_0_5'}(:)];
out.Es(find(out.Es == -999)) = NaN;

out.Lu = [infile{'Lu412_3_1'}(:) ...   % 20m radiance
      infile{'Lu443_3_2'}(:) ...
      infile{'Lu490_3_3'}(:) ...
      infile{'Lu510_3_4'}(:) ...
      infile{'Lu555_3_5'}(:)];
out.Lu(find(out.Lu == -999)) = NaN;

out.Ed = [infile{'Ed412_2_1'}(:) ...   % 20m irradiance
      infile{'Ed443_2_2'}(:) ...
      infile{'Ed490_2_3'}(:) ...
      infile{'Ed510_2_4'}(:) ...
      infile{'Ed555_2_5'}(:)];
out.Ed(find(out.Ed == -999)) = NaN;

out.z = infile{'Depth_2_10'}(:);       % Depth (m) of 20m sensor
out.z(find(out.z == -999)) = NaN;

out.K  = calc_k(out.Es*0.98, out.Ed, -20);  % Attenuation Coeff. (1/m)
out.Lw = calc_lw(out.Lu, out.K, -20);  % Water-leaving radiance
%out.K  = calc_k(out.Es, out.Ed, repmat(out.z, 1, 5));
%out.Lw = calc_lw(out.Lu, out.K, repmat(out.z, 1, 5));

out.Rrs = out.Lw./out.Es;              % Remote sensing ratio

%=========
% OC2 CHL
%=========
%Coefficients of the OC2 MCP equations in O'Reilly et al. 1998 
c15 = [0.2457, -1.7620, 0.2830, 0.1035, -0.0388]; 
c25 = [0.1909, -1.9961, 1.3020, -0.5091, -0.0815]; 
c35 = [0.3410, -3.0010, 2.8110, -2.0410, -0.0400]; 
c45 = [0.4487, -4.3665, 2.7130, -0.2698, -0.0821]; 

%Compute ratios and log-transform
R15 = log10(out.Rrs(:,1)./out.Rrs(:,5)); 
R25 = log10(out.Rrs(:,2)./out.Rrs(:,5)); 
R35 = log10(out.Rrs(:,3)./out.Rrs(:,5)); 
R45 = log10(out.Rrs(:,4)./out.Rrs(:,5)); 

% Calculate chlorophyll concentration (ug/l) 
out.oc(:,1) = 10.0.^(c15(1) + c15(2).*R15 +  c15(3).*R15.^2 + c15(4).*R15.^3) + c15(5); 
out.oc(:,2) = 10.0.^(c25(1) + c25(2).*R25 +  c25(3).*R25.^2 + c25(4).*R25.^3) + c25(5); 
out.oc(:,3) = 10.0.^(c35(1) + c35(2).*R35 +  c35(3).*R35.^2 + c35(4).*R35.^3) + c35(5);
out.oc(:,4) = 10.0.^(c45(1) + c45(2).*R45 +  c45(3).*R45.^2 + c45(4).*R45.^3) + c45(5);

out.oc_cov  = std([out.oc(:,1) out.oc(:,2) out.oc(:,3) out.oc(:,4)]')'./mean([out.oc(:,1) out.oc(:,2) out.oc(:,3) out.oc(:,4)]')';

