function pcm_ell_tables(theYears, dataDirectory, plotDirectory)
%
%   pcm_ell_tables(theYears, dataDirectory, plotDirectory)
%
%   Function to create tables of east and north components by month for
%   hourly averaged and low-passed PCM measurements for theYears.
%   Output files are named pcm_paDEPTH_H_YYYY.

%Soupy Alexander, 11/6/03


%PCM
theFiles = {'a_curr_5mbs.nc'; 'a_curr_10mab.nc'; 'a_curr_1mab.nc'};
timeVar = cell(1,3);
currVar = cell(1,3);
timeVar_lp = cell(1,3);
currVar_lp = cell(1,3);
depthList = {'5 mbs'; '10 mab'; '1 mab'};
theDepths = {'5'; '10'; '1'};

for indexFile = 1:length(theFiles)
    ncID = netcdf(fullfile(dataDirectory, theFiles{indexFile}), 'nowrite');
    fileTime = singlejd(ncID{'time'}(:), ncID{'time2'}(:));
    timeVar{indexFile} = fileTime;
    fileU = ncID{'u_1205'}(:);
    fileV = ncID{'v_1206'}(:);
    currVar{indexFile} = fileU + i.*fileV;
    
    timeNew = min(fileTime):1/24:max(fileTime);
    newU = smart_interp(fileTime, fileU, timeNew, 6);
    newV = smart_interp(fileTime, fileV, timeNew, 6);
    
    [theU, time_lp] = plfilt(newU, timeNew, 6);
    [theV, time_lp] = plfilt(newV, timeNew, 6);
    
    timeVar_lp{indexFile} = time_lp;
    currVar_lp{indexFile} = theU + i.*theV;
    
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, ['pcm_pa' theDepths{indexVar} '_h_' ...
                num2str(targetYear, '%.0f') '.txt']), 'w');
        
        fprintf(fID, ['Site A, Ellipse Parameters, ' num2str(targetYear, '%.0f') '\n']);
        fprintf(fID, ['Point Current Meter, 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, ['pcm_pa' theDepths{indexVar} '_lp_' ...
                num2str(targetYear, '%.0f') '.txt']), 'w');
        
        fprintf(fID, ['Site A, Ellipse Parameters, ' num2str(targetYear, '%.0f') '\n']);
        fprintf(fID, ['Point Current Meter, 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