function [lw, K, n] = oms_lw(infile, lambda)
% OMS_LW     - Calculate water leaving radiances from Oasis Mooring Spectroradiometer data
%
% Use as: [lw k n] = oms_lw(infile, lambda)
%         
% Inputs: infile = A netcdf file object
%         lambda = Wavelength of desired attenuation coeffiecient (nanometers) (scalar)
%
% Output: lw = water leaving radiance
%         k  = diffuse-attenuation coef used to calculate lw
%         n  = Cell array containing the Names of the variables in infile 
%              used for each column of lw

% Brian Schlining
% 09 Dec 1999

OK = oms_checkfile(infile);
if ~OK
   lw = [];
   K  = [];
   return;
end

[K Z] = oms_k1(infile, lambda);
if isempty(K)
   lw = [];
   K  = [];
   n = [];
   return;
end

[r c] = size(K);
if c > 1
   useDepth = 20;
   warning(sprintf(['Several Ed[' num2str(lambda) 'nm] sensors were found.\n Using k from the sensor nearest to ' num2str(useDepth) 'm']));
   z = mean(Z);
   i = near(z,useDepth);
   K = K(:,i);
   Z = Z(:,i);
end

S = oms_getinfo(infile);

iEd = strmatch('lu',S.symbol);					% Find Ed
iW  = find(S.wavelength == lambda);				% Find the correct wavelength
good = intersect(iEd, iW);

if isempty(good)
   warning(['No Lu[' num2str(lambda) 'nm] (Upwelled radiance) sensors were found']);
   lw = [];
   n = [];
else
   for i = 1:length(good)
      n{i} = S.name{good(i)};
      v = infile{char(n{i})};
      lw(:,i) = calc_lw(v(:), K, oms_getdepth(infile, v));  
   end
end







