function adcp_ell_tables(theYears, dataDirectory, plotDirectory, siteArchive, theSite)
%
%   adcp_ell_tables(theYears, dataDirectory, plotDirectory, siteArchive, theSite)
%
%   Function to create tables of east and north components by month for
%   hourly averaged and low-passed ADCP measurements for theYears.
%   Output files are named SITE_paDEPTH_YYYY.

%Soupy Alexander, 11/8/03

theta=2*pi*(1:64)/64;
theta=[0 theta];

colList = {'r'; 'g'; 'b'; 'c'; 'm'; 'k'; 'r'; 'g'; 'b'; 'c'; 'k'; 'm'};
monthList = {'Jan'; 'Feb'; 'Mar'; 'Apr'; 'May'; 'Jun'; 'Jul'; 'Aug'; 'Sep'; ...
        'Oct'; 'Nov'; 'Dec'};
theSpacing = 10;
theLimit = 15;

%ADCP
timeVar = cell(1,3);
timeVar_lp = cell(1,3);
currVar = cell(1,3);
currVar_lp = cell(1,3);

if strcmp(lower(theSite), 'a')
    depthList = {'5 mbs'; '15 mbs'; 'Deepest Bin'};
    theDepths = {'5'; '15'; '1'};
else
    depthList = {'5 mbs'; '10 mbs'; 'Deepest Bin'};
    theDepths = {'5'; '10'; '1'};
end

moorList = cell(0);

%Load in the site A data
for indexFile = 1:length(siteArchive)
    if rem(indexFile,10) == 0
        fprintf(['On file number ' num2str(indexFile, '%.0f') ' of ' ...
                num2str(length(siteArchive), '%.0f') '.\n']);
    end
    fileName = siteArchive(indexFile).fileList;
    if isempty(strfind(fileName, 'adc'))
        continue
    end
    moorNum = fileName(1:4);
    if strmatch(moorNum, moorList)
        continue
    end
    moorList = [moorList(:); {moorNum}];
    currFileA = netcdf(fullfile(dataDirectory, siteArchive(indexFile).fileList), 'nowrite');
    fileDepth = currFileA{'depth'}(:);
    fileTime = singlejd(currFileA{'time'}(:), currFileA{'time2'}(:));
    fileU = currFileA{'u_1205'}(:);
    fileV = currFileA{'v_1206'}(:);
    bads = find(fileU > 1e3 | fileV > 1e3);
    fileU(bads) = NaN;
    fileV(bads) = NaN;
    
    theHit = value2Index(fileDepth, 5);
    theU = fileU(:, theHit);
    try
        [theU_lp, fileTime_lp] = plfilt(theU, fileTime, 6); 
    catch
        continue
    end
    theV = fileV(:, theHit);
    [theV_lp, fileTime_lp] = plfilt(theV, fileTime, 6); 
        
    fileCurr = theU + i.*theV;
    fileCurr_lp = theU_lp + i.*theV_lp;
    
    oldTime = timeVar{1};
    timeVar{1} = [oldTime(:); fileTime(:)]; 
    oldTime_lp = timeVar_lp{1};
    timeVar_lp{1} = [oldTime_lp(:); fileTime_lp(:)];
    
    oldCurr = currVar{1};
    currVar{1} = [oldCurr(:); fileCurr(:)]; 
    oldCurr_lp = currVar_lp{1};
    currVar_lp{1} = [oldCurr_lp(:); fileCurr_lp(:)];
    
    
    if strcmp(lower(theSite), 'a')
        theHit = value2Index(fileDepth, 15);
    elseif strcmp(lower(theSite), 'b')
        theHit = value2Index(fileDepth, 10);
    end
    theU = fileU(:, theHit);
    try
        [theU_lp, fileTime_lp] = plfilt(theU, fileTime, 6); 
    catch
        continue
    end
    theV = fileV(:, theHit);
    [theV_lp, fileTime_lp] = plfilt(theV, fileTime, 6); 
        
    fileCurr = theU + i.*theV;
    fileCurr_lp = theU_lp + i.*theV_lp;
    
    oldTime = timeVar{2};
    timeVar{2} = [oldTime(:); fileTime(:)]; 
    oldTime_lp = timeVar_lp{2};
    timeVar_lp{2} = [oldTime_lp(:); fileTime_lp(:)];
    
    oldCurr = currVar{2};
    currVar{2} = [oldCurr(:); fileCurr(:)]; 
    oldCurr_lp = currVar_lp{2};
    currVar_lp{2} = [oldCurr_lp(:); fileCurr_lp(:)];
    
    
    theHit = find(fileDepth == max(fileDepth));
        theU = fileU(:, theHit);
    try
        [theU_lp, fileTime_lp] = plfilt(theU, fileTime, 6); 
    catch
        continue
    end
    theV = fileV(:, theHit);
    [theV_lp, fileTime_lp] = plfilt(theV, fileTime, 6); 
        
    fileCurr = theU + i.*theV;
    fileCurr_lp = theU_lp + i.*theV_lp;
    
    oldTime = timeVar{3};
    timeVar{3} = [oldTime(:); fileTime(:)]; 
    oldTime_lp = timeVar_lp{3};
    timeVar_lp{3} = [oldTime_lp(:); fileTime_lp(:)];
    
    oldCurr = currVar{3};
    currVar{3} = [oldCurr(:); fileCurr(:)]; 
    oldCurr_lp = currVar_lp{3};
    currVar_lp{3} = [oldCurr_lp(:); fileCurr_lp(:)];
end

for indexYear = 1:length(theYears)
    
    targetYear = theYears(indexYear);
    
    for indexVar = 1:3
        
        theTime = timeVar{indexVar};
        theU = real(currVar{indexVar});
        theV = imag(currVar{indexVar});
        
        bads = find(isnan(theU + theV));
        theTime(bads) = [];
        theU(bads) = [];
        theV(bads) = [];
        
        gregTime = gregorian(theTime);
        
        if isempty(find(gregTime(:,1) == targetYear))
            continue
        end
        
        fID = fopen(fullfile(plotDirectory, [lower(theSite) '_pa' theDepths{indexVar} '_h_' ...
                num2str(targetYear, '%.0f') '.txt']), 'w');
        
        fprintf(fID, ['Site ' upper(theSite) ', Ellipse Parameters, ' ...
                num2str(targetYear, '%.0f') '\n']);
        fprintf(fID, ['ADCP, Hourly Averaged Data, ' depthList{indexVar} '\n\n\n']);
        fprintf(fID, 'Month\tPoints\tMajor\tMinor\tMajor\tEllipticity\n');
        fprintf(fID, '\t\tAxis\tAxis\tOrientation\n');
        fprintf(fID, '\t(Hours)\t(cm/s)\t(cm/s)\t(degrees)\n\n');
        
        
        for indexMonth = 1:12
            inYear = find(gregTime(:,1) == targetYear & ...
                gregTime(:,2) == indexMonth);
            
            theInTime = theTime(inYear);
            theInU = theU(inYear);
            theInV = theV(inYear);
            
            bads = find(isnan(theInU + theInV));
            theInTime(bads) = [];
            theInU(bads) = [];
            theInV(bads) = [];
            fprintf(fID, [num2str(indexMonth, '%.0f') '\t']);
            
            
            if length(theInU) > 0
                [majAx, majAz, minAx, minAz, theEllip] = pcaben(theInU, theInV);
                fprintf(fID, [num2str(length(inYear), '%.0f') '\t']);
                fprintf(fID, [num2str(majAx, '%.2f') '\t']);
                fprintf(fID, [num2str(minAx, '%.2f') '\t']);
                fprintf(fID, [num2str(majAz, '%.0f') '\t']);
                fprintf(fID, [num2str(theEllip, '%.2f') '\n']);
            else
                fprintf(fID, 'No Data\n');
            end  
        end
        
        inYear = find(gregTime(:,1) == targetYear);
        
        theInTime = theTime(inYear);
        theInU = theU(inYear);
        theInV = theV(inYear);
        
        bads = find(isnan(theInU + theInV));
        theInTime(bads) = [];
        theInU(bads) = [];
        theInV(bads) = [];
        fprintf(fID, 'All Year\t');
        
        
        [majAx, majAz, minAx, minAz, theEllip] = pcaben(theInU, theInV);
        fprintf(fID, [num2str(length(inYear), '%.0f') '\t']);
        fprintf(fID, [num2str(majAx, '%.2f') '\t']);
        fprintf(fID, [num2str(minAx, '%.2f') '\t']);
        fprintf(fID, [num2str(majAz, '%.0f') '\t']);
        fprintf(fID, [num2str(theEllip, '%.2f') '\n']);
        
        fclose(fID);
        
    end
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    for indexVar = 1:3
        
        theTime = timeVar_lp{indexVar};
        theU = real(currVar_lp{indexVar});
        theV = imag(currVar_lp{indexVar});
        
        bads = find(isnan(theU + theV));
        theTime(bads) = [];
        theU(bads) = [];
        theV(bads) = [];
        
        gregTime = gregorian(theTime);
        
        if isempty(find(gregTime(:,1) == targetYear))
            continue
        end
        
        fID = fopen(fullfile(plotDirectory, [lower(theSite) '_pa' theDepths{indexVar} '_lp_' ...
                num2str(targetYear, '%.0f') '.txt']), 'w');
        
        fprintf(fID, ['Site ' upper(theSite) ', Ellipse Parameters, ' ...
                num2str(targetYear, '%.0f') '\n']);
        fprintf(fID, ['ADCP, Low-Passed Data, ' depthList{indexVar} '\n\n\n']);
        fprintf(fID, 'Month\tPoints\tMajor\tMinor\tMajor\tEllipticity\n');
        fprintf(fID, '\t\tAxis\tAxis\tOrientation\n');
        fprintf(fID, '\t(Points)\t(cm/s)\t(cm/s)\t(degrees)\n\n');
        
        
        for indexMonth = 1:12
            inYear = find(gregTime(:,1) == targetYear & ...
                gregTime(:,2) == indexMonth);
            
            theInTime = theTime(inYear);
            theInU = theU(inYear);
            theInV = theV(inYear);
            
            bads = find(isnan(theInU + theInV));
            theInTime(bads) = [];
            theInU(bads) = [];
            theInV(bads) = [];
            fprintf(fID, [num2str(indexMonth, '%.0f') '\t']);
            
            
            if length(theInU) > 0
                [majAx, majAz, minAx, minAz, theEllip] = pcaben(theInU, theInV);
                fprintf(fID, [num2str(length(inYear), '%.2f'), '\t']);
                fprintf(fID, [num2str(majAx, '%.2f') '\t']);
                fprintf(fID, [num2str(minAx, '%.2f') '\t']);
                fprintf(fID, [num2str(majAz, '%.0f') '\t']);
                fprintf(fID, [num2str(theEllip, '%.2f') '\n']);
            else
                fprintf(fID, 'No Data\n');
            end  
        end
        
        inYear = find(gregTime(:,1) == targetYear);
        theInTime = theTime(inYear);
        theInU = theU(inYear);
        theInV = theV(inYear);
        
        bads = find(isnan(theInU + theInV));
        theInTime(bads) = [];
        theInU(bads) = [];
        theInV(bads) = [];
        fprintf(fID, 'All Year\t');
        [majAx, majAz, minAx, minAz, theEllip] = pcaben(theInU, theInV);
        fprintf(fID, [num2str(length(inYear), '%.2f'), '\t']);
        fprintf(fID, [num2str(majAx, '%.2f') '\t']);
        fprintf(fID, [num2str(minAx, '%.2f') '\t']);
        fprintf(fID, [num2str(majAz, '%.0f') '\t']);
        fprintf(fID, [num2str(theEllip, '%.2f') '\n']);
        fclose(fID);
        
    end
end