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
filename=char(ncnames(infile));
[f p e]=fileparts(filename);

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

out.lambda = [412 443 490 510 555 656 665 670 683];    % nm

if ~isempty(infile{'Ed670_0_6'}(:))
	out.Es = [infile{'Ed412_0_1'}(:) ...   % Surface radiance EP2
      infile{'Ed443_0_2'}(:) ...
      infile{'Ed490_0_3'}(:) ...
      infile{'Ed510_0_4'}(:) ...
      infile{'Ed555_0_5'}(:) ...
      infile{'Ed656_0_6'}(:) ...
      infile{'Ed670_0_6'}(:)];

out.Esname(:,1)=412;
out.Esname(:,2)=443;
out.Esname(:,3)=490;
out.Esname(:,4)=510;
out.Esname(:,5)=555;
out.Esname(:,6)=656;
out.Esname(:,7)=670;

else
   out.Es =[infile{'Ed412_0_1'}(:) ...   % Surface radiance EP1
      infile{'Ed443_0_2'}(:) ...
      infile{'Ed490_0_3'}(:) ...
      infile{'Ed510_0_4'}(:) ...
      infile{'Ed555_0_5'}(:) ...
      infile{'Ed656_0_6'}(:)];

out.Esname(:,1)=412;
out.Esname(:,2)=443;
out.Esname(:,3)=490;
out.Esname(:,4)=510;
out.Esname(:,5)=555;
out.Esname(:,6)=656;

end;

   out.Es(find(out.Es == -999)) = NaN;
   
if ~isempty(infile{'Lu656_3_6'}(:))
	out.Lu = [infile{'Lu412_3_1'}(:) ...   % 20m radiance EP2
      infile{'Lu443_3_2'}(:) ...
      infile{'Lu490_3_3'}(:) ...
      infile{'Lu510_3_4'}(:) ...
      infile{'Lu555_3_5'}(:) ...
      infile{'Lu656_3_6'}(:) ...
      infile{'Lu665_3_6'}(:) ...
      infile{'Lu670_3_6'}(:) ...
      infile{'Lu683_3_7'}(:)];

out.Luname(:,1)=412;
out.Luname(:,2)=443;
out.Luname(:,3)=490;
out.Luname(:,4)=510;
out.Luname(:,5)=555;
out.Luname(:,6)=656;
out.Luname(:,7)=670;
out.Luname(:,8)=683;

else
   out.Lu = [infile{'Lu412_3_1'}(:) ...   % 20m radiance EP1
      infile{'Lu443_3_2'}(:) ...
      infile{'Lu490_3_3'}(:) ...
      infile{'Lu510_3_4'}(:) ...
      infile{'Lu555_3_5'}(:) ...
      infile{'Lu670_3_6'}(:) ...
      infile{'Lu683_3_7'}(:)];

out.Luname(:,1)=412;
out.Luname(:,2)=443;
out.Luname(:,3)=490;
out.Luname(:,4)=510;
out.Luname(:,5)=555;
out.Luname(:,6)=670;
out.Luname(:,7)=683;

end;

out.Lu(find(out.Lu == -999)) = NaN;

if ~isempty(infile{'Ed665_2_6'}(:))
	out.Ed = [infile{'Ed412_2_1'}(:) ...   % 20m irradiance
      infile{'Ed443_2_2'}(:) ...
      infile{'Ed490_2_3'}(:) ...
      infile{'Ed510_2_4'}(:) ...
      infile{'Ed555_2_5'}(:) ...
   	infile{'Ed656_2_6'}(:) ...
      infile{'Ed665_2_6'}(:) ...
      infile{'Ed670_2_6'}(:)];

out.Edname(:,1)=(412);
out.Edname(:,2)=(443);
out.Edname(:,3)=(490);
out.Edname(:,4)=(510);
out.Edname(:,5)=(555);
out.Edname(:,6)=(656);
out.Edname(:,7)=(665);
out.Edname(:,8)=(670);

else
   out.Ed = [infile{'Ed412_2_1'}(:) ...   % 20m irradiance
      infile{'Ed443_2_2'}(:) ...
      infile{'Ed490_2_3'}(:) ...
      infile{'Ed510_2_4'}(:) ...
      infile{'Ed555_2_5'}(:) ...
   	infile{'Ed656_2_6'}(:) ...
      infile{'Ed670_2_6'}(:)];

out.Edname(:,1)=(412);
out.Edname(:,2)=(443);
out.Edname(:,3)=(490);
out.Edname(:,4)=(510);
out.Edname(:,5)=(555);
out.Edname(:,6)=(656);
out.Edname(:,7)=(670);
end;

out.Ed(find(out.Ed == -999)) = NaN;

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

%Load Frouin Model
if strcmp(p(1:3),'ep2')%to handle the real-time clock problem at ep2
	timechange=out.t;
	timeind=find(timechange>=datenum('30 Oct 2000')&timechange<=datenum('18 Jun 2001'));
	timechange(timeind)=timechange(timeind)+.04225;
	FrouinVar=frouin(timechange, infile{'latitude'}(:), infile{'longitude'}(:));
else
   FrouinVar=frouin(nctime(infile), infile{'latitude'}(:), infile{'longitude'}(:));
end;
out.Frouin=FrouinVar(:,1:5);

%Load Par Model and vals
pardates=[datenum('1 Jan 1996'):datenum('31 Dec 2005')];

if strcmp(p(1:3),'ep1')
   fid=fopen('I:\eqpac\variables\ep1par.txt');%local mount make sure to change this
elseif strcmp(p(1:3),'ep2')
   fid=fopen('I:\eqpac\variables\ep2par.txt');%local mount make sure to change this
end;

par_model=fscanf(fid,'%f',inf);
fclose(fid);
for i=1:length(out.t)
   parind(i)=find(pardates==floor(out.t(i)));
end;
parind=parind';
out.PARmodel=par_model(parind);
out.PAR=infile{'PAR_0_7'}(:)*10000;


