function E = ep_cmpl(Flag)
% EP_CMPL - Compile eqpac files. 
% Reads files in ep1 and ep2, compiles them.
%
% EP_CMPL Reads all the files created by Bob Herlein's programs in directories ep1 and eo2
%
% Use As: E = ep_cmpl;
%         E = ep_cmpl(EditFlag);
%
% Inputs: EditFlag can equal 0 or 1
%         1: (default) edit for outliers in pressure, salinity, and temperature
%           If bad data is found the entire row is marked bad.
%         0: Do not edit for outliers
%
% Ouputs: E = Structure with the following fields
%             id: ep id# (ex. 'ep1')
%             dat: matrix of data, refer to ep_ncini for column names

% $Id: ep_cmpl.m,v 1.1 2005/04/13 20:31:03 brian Exp $
% Brian Schlining
% 29 Jan 1999
% 06 Jun 2000; Creted a new version that does not derive products
% 12 Apr 2005; Modified PathS evaluation to be cross-platform

if ~nargin
   EditFlag = 1;
end


%=======================================
% Loop through ep1 and ep 2 directories
%=======================================
for ep = 1:2
   
   %=================================
   % Set path based on computer type
   %=================================
   PathS = fullfile(getOasisPath, 'eqpac', ['ep' num2str(ep)]);
   
   %=================================================================
   % Combine the noon and 12:30 files. Most of the work is done here
   % using the subfunction COALLATE.
   %=================================================================
   epx = coallate(PathS);   
   
   %=============================================================
   % Locate important variables in epx to simplify the equations
   % If modifications are made to COALLATE these may have to be
   % changes
   %=============================================================
   Pressure     = repmat(epx(:,2),1,7);
   Lu_2m        = epx(:,13:19);  % Actually Lu(1.5m) but rounded for naming purposes, can't use decimals in variable names
   Lu_20m       = epx(:,28:34);
   Ed_20m       = epx(:,21:26);
   Lu490_0m     = epx(:,15);
   Lu555_0m     = epx(:,17);
   Lu490_20m    = epx(:,30);
   Lu555_20m    = epx(:,32);
   Lu443_0m     = epx(:,14);
   Lu443_20m    = epx(:,29);
   Lu683_0m     = epx(:,19);
   Lu683_20m    = epx(:,34);
   Ed_0m        = epx(:,6:11);
   
   % Edit for outliers. This assumes pressure is in column 2, Salinity in column
   if EditFlag
      epx = editep(epx);
   end
   
   %=================================================
   % Calculate Derived products from measured values
   %================================================+   
   E(ep).id  = ['ep' num2str(ep)];
   E(ep).dat = epx;
   
end

%======================================================================================
%======================================================================================
% subfunctions
%======================================================================================
%======================================================================================

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% COALLATE - Reads the files in ep1 and ep2 and combine into one file
% with alternating noon and 1230 lines the columns are as follows
%  1          -  1200 or 1230 (indicating sampiling at noon or 1230am
%  2          - Pressure
%  3          - DecimalYear
%  4:5        - Temperature Salinity
%  6:19  (0m) - Ed412 Ed443 Ed490 Ed510 Ed555 Ed656 PAR Lu412 Lu443 Lu490 Lu510 Lu555 Lu670 Lu683
% 20          - MCP at 10 m  
% 21:34 (20m) - Ed412 Ed443 Ed490 Ed510 Ed555 Ed670 PAR Lu412 Lu443 Lu490 Lu510 Lu555 Lu670 Lu683
% 35          - MCP at 30 m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Dat = coallate(PathS)
%=========================================
% Loop through each directory ep1 and ep2
%=========================================

%===============================================
% Read in the files needed for processing
%===============================================
% Noon Files ===================================
S00a       = readeqpac_(fullfile(PathS, 'spec.noon'));
[t g]      = unique(S00a(:,1));
S00a       = S00a(g,:);
[r l00a]   = size(S00a);

S20a       = readeqpac_(fullfile(PathS, 'spec.20m'));
[t g]      = unique(S20a(:,1));
S20a       = S20a(g,:);
[r l20a]   = size(S20a);

[M10 M30]  = readmcp(fullfile(PathS, 'mcp'));
[t g]      = unique(M10(:,1));
M10        = M10(g,:);
[r lM10]    = size(M10);

[t g]      = unique(M30(:,1));
M30        = M30(g,:);
[r lM30]    = size(M30);

% 12:30 Files ==================================
S00b       = readeqpac_(fullfile(PathS,'spec.0m.1000'));
[r l00b]   = size(S00b);
if l00b ~= 16
   S00b = [S00b ones(r, 16-l00b)*NaN];   
end
[r l00b]   = size(S00b);

[t g]      = unique(S00b(:,1));                 % Get rid of repetitive dates
S00b       = S00b(g,:);
[r l00b]   = size(S00b);

S20b       = readeqpac_(fullfile(PathS, 'spec.20m.1000')); 
[t g]      = unique(S20b(:,1));
S20b       = S20b(g,:);
[r l20b]    = size(S20b);

% 12:30 and Noon Files =========================
Misc       = readeqpac_(fullfile(PathS, 'spec.misc'));
[r lMisc]   = size(Misc);
if lMisc == 3
   Misc = [Misc(:,1) ones(length(Misc(:,1)), 2)*NaN Misc(:, 2:3)];
   [r lMisc]   = size(Misc);
end

if lMisc ~= 9
   Misc = [Misc ones(r, 9-lMisc)*NaN];   
end
P          = Misc(:,[1 5 9]);       % DecimalYear PressureNoon Pressure1230
[r lMisc]   = size(Misc);


CTD        = readeqpac_(fullfile(PathS, 'temp.0m'));
CTD(:,3)   = salin_(CTD(:,3)/4.2914,CTD(:,2),0);  

%=============================================
% correct the times and convert to local time
%+============================================
StartDate  = min([S00a(1,1) S20a(1,1) M10(1,1) M30(1,1)]);                             % Earliest Date
EndDate    = max([max(S00a(end,1)) max(S20a(end,1)) max(M10(end,1)) max(M30(end,1))]); % Latest Date
nextDate   = StartDate;                                                                % Start here
n = 1;                                                                                 % Increment

%===================================================
% Loop through every single date found in the files
%===================================================
while ~isempty(nextDate) 
   
   % Find the maximum occurence of nextDate in each file. Because files occasionally have two
   % lines with the same date but different data, it was easiest to grab only one of the lines 
   % and ignore the other(s).
   r00a   = max(find(S00a(:,1) == nextDate));
   r00b   = max(find(S00b(:,1) == nextDate));
   rM10   = max(find(M10(:,1)  == nextDate));
   rM30   = max(find(M30(:,1)  == nextDate));
   r20a   = max(find(S20a(:,1) == nextDate));
   r20b   = max(find(S20b(:,1) == nextDate));
   rP     = max(find(P(:,1)    == nextDate));
   rCTD   = max(find(CTD(:,1)  == nextDate));
   
   %==========================================================================================
   % Write the data to a buffer variable. If no data is found use -999 as a missing data code
   %==========================================================================================
   if ~isempty(rP)
      P1230 = P(rP, 3);
      X = [1200 P(rP,2)];
   else
      X = [1200 NaN];
      if exist('P1230') | isempty(P1230)
         P1230 = NaN;
      end
   end   
   
   if ~isempty(r00a)
      X = [X S00a(r00a,1)];
   else
      X = [X nextDate];
   end
   
   if ~isempty(rCTD)
      X = [X CTD(rCTD,2:end)];
   else
      X = [X NaN NaN];
   end
   
   % Noon
   if ~isempty(r00a)
      X = [X S00a(r00a,2:end-1)];
   else
      X = [X ones(1,l00a-2)*NaN];
   end
   
   if ~isempty(rM10)
      X = [X M10(rM10,3:end)];
   else
      X = [X ones(1,lM10-2)*NaN];
   end
   
   if ~isempty(r20a)
      X = [X S20a(r20a,2:end)];
   else
      X = [X ones(1,l20a-1)*NaN];
   end
   
   if ~isempty(rM30)
      X = [X M30(rM30,3:end)];
   else
      X = [X ones(1,lM30-2)*NaN];
   end
   
   % 12:30am  
   if ~isempty(rP)
      Y = [1230 P(rP,3)];
   else
      Y = [1230 P1230];
   end
   
   
   %if isempty(rP)
   %   Y = [1230 lP];
   %else
   %   Y = [1230 NaN];
   %end
   
   
   if ~isempty(r00b)
      Y = [Y S00b(r00b,1) NaN NaN S00b(r00b,2:end-1)];
   else
      Y = [Y nextDate NaN NaN ones(1,l00b-2)*NaN];
   end
   
   if ~isempty(r20b)
      Y = [Y ones(1,lM10-2)*NaN S20b(r20b,2:end) ones(1,lM30-2)*NaN];
   else
      Y = [Y ones(1,lM10-2)*NaN ones(1,l20b-1)*NaN ones(1,lM30-2)*NaN];
   end
   
   %====================================
   % Increment to the next largest date
   %====================================
   i = min(find(S00a(:,1)>StartDate));
   if ~isempty(i);
      nextDate = S00a(i,1); 
   else 
      nextDate = []; 
   end
   
   i = min(find(S00b(:,1)>StartDate));
   if ~isempty(i); 
      nextDate = min([S00b(i,1) nextDate]);
   end
   
   i = min(find(S20a(:,1)>StartDate));
   if ~isempty(i); 
      nextDate = min([S20a(i,1) nextDate]);
   end
   
   i = min(find(S20b(:,1)>StartDate));
   if ~isempty(i); 
      nextDate = min([S20b(i,1) nextDate]); 
   end
   
   i = min(find(M10(:,1)>StartDate));
   if ~isempty(i); 
      nextDate = min([M10(i,1) nextDate]); 
   end
   
   i = min(find(M30(:,1)>StartDate));
   if ~isempty(i);
      nextDate = min([M30(i,1) nextDate]);
   end
   
   %==========================================================
   % Write data from the buffers to the output variable, Dat.
   %==========================================================
   Dat(n,:) = X;      
   n        = n + 1;
   Dat(n,:) = Y;
   n        = n + 1;
   
   StartDate = nextDate;
   
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% EDITEP
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function epx = editep(epx);
i = find(epx(:,2) > 30 | epx(:,2) < 10);
if ~isempty(i)
   epx(i,6:end) = NaN;
end

i = find(epx(:,5) > 38 | epx(:,5) < 32);
if ~isempty(i)
   epx(i,6:end) = NaN;
end

i = find(epx(:,4) > 35 | epx(:,4) < 15);
if ~isempty(i)
   epx(i,6:end) = NaN;
end

i = find(isinf(epx));
if ~isempty(i)
   epx(i) = NaN;
end




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% READMCP
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [x10, x30] = readmcp(FileName)
x   = readeqpac_(FileName);
if isempty(x)
   x10 = [NaN NaN NaN];
   x30 = [NaN NaN NaN];
else
   x   = sortrows(x,[2 1]);
   i   = min(find(x(:,2) == 30));
   x10 = x(1:(i - 1),:);
   x30 = x(i:end,:);
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% READEQPAC_
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function X = readeqpac_(FileLoc, NumOfDays)
%fid = fopen(FileLoc,'r');
%OK  = 1;
%while OK		 						    	% Trash the header
%   S = fgetl(fid);
%   if ~strcmp(S(1),'#')
%      break
%   end
%end
%i = 1;
%while ~feof(fid)					     	% Read the data
%   XS     = fgetl(fid);			    	% Get a line from the file
%   X(i,:) = sscanf(XS,'%f')';		   % Read the data from the line
%   i      = i + 1;
%end
%fclose(fid);
X = readflatfile(FileLoc);
X = unique(X,'rows');               % Remove duplicate rows
