function adcp_vpv(theYears, dataDirectory, plotDirectory, siteArchive, theSite)
%
%   adcp_vpv(theYears, dataDirectory, plotDirectory, siteArchive, theSite)
%
%   Function to create the vpv files for the MBLT ADCP data.
%   Files are output as SITE_vpv.

%Soupy Alexander, 11/5/03

%Load in the site A data
depthVec = fliplr([4:2:30]);
timeVec = [julian(1996, 1, 1, 00):6/24:julian(max(theYears), 12, 31, 24)];
timeVecH = [julian(1996, 1, 1, 00):1/24:julian(max(theYears), 12, 31, 24)];
allU = repmat(NaN, length(depthVec), length(timeVec));
allV = repmat(NaN, length(depthVec), length(timeVec));
allUH = repmat(NaN, length(depthVec), length(timeVecH));
allVH = repmat(NaN, length(depthVec), length(timeVecH));

warning off MATLAB:divideByZero

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
    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;
    
    for indexDepth = 1:length(depthVec)
        hitDepth = value2index(fileDepth, depthVec(indexDepth), 2.1);
        
        if isnan(hitDepth)
            continue
        end
        
        fileUHit = fileU(:,hitDepth);
        fileVHit = fileV(:,hitDepth);
        bads = find(isnan(fileUHit + fileVHit));
        
        fileTimeI = fileTime;
        fileTimeI(bads) = [];
        fileUHit(bads) = [];
        fileVHit(bads) = [];
        
        if isempty(fileTimeI)
            continue
        end
        
        try
            [fileIU, fileTime_lp] = plfilt(fileUHit, fileTimeI, 6);
            [fileIV, fileTime_lp] = plfilt(fileVHit, fileTimeI, 6);
        catch
            continue
        end
        
        timeStart = max(find(timeVec <= min(fileTime_lp)));
        timeEnd = min(find(timeVec >= max(fileTime_lp)));
        
        fileIU2 = smart_interp(fileTime_lp, fileIU, timeVec(timeStart:timeEnd), 12);
        fileIV2 = smart_interp(fileTime_lp, fileIV, timeVec(timeStart:timeEnd), 12);
        
        allU(indexDepth, timeStart:timeEnd) = fileIU2;
        allV(indexDepth, timeStart:timeEnd) = fileIV2;
        
        timeStart = max(find(timeVecH <= min(fileTimeI)));
        timeEnd = min(find(timeVecH >= max(fileTimeI)));
        
        fileIU2 = smart_interp(fileTimeI, fileUHit, timeVecH(timeStart:timeEnd), 2);
        fileIV2 = smart_interp(fileTimeI, fileVHit, timeVecH(timeStart:timeEnd), 2);
        
        allUH(indexDepth, timeStart:timeEnd) = fileIU2;
        allVH(indexDepth, timeStart:timeEnd) = fileIV2;
    end    
end  

timeVec = julian2datenum(timeVec);
timeVecH = julian2datenum(timeVecH);

test = nanmean((allU + allV));
bads = find(isnan(test));
timeVec(bads) = [];
allU(:, bads) = [];
allV(:, bads) = [];

test = nanmean((allUH + allVH));
bads = find(isnan(test));
timeVecH(bads) = [];
allUH(:,bads) = [];
allVH(:,bads) = [];

outName = fullfile(plotDirectory, [lower(theSite) '_vpv_lp.vel']);
outNameH = fullfile(plotDirectory, [lower(theSite) '_vpv_h.vel']);

write_vpv(outName, timeVec, allU', allV');
write_vpv(outNameH, timeVecH, allUH', allVH');


