function recombine_adcp
%
%   Function to recombine the corrected ADCP data and the non-internal wave
%   ADCP data to create a continuous time series.

%Soupy Alexander, 4/2004

%This Matlab m-file was used to create portions of U.S. Geological Survey 
%Data Series 85. Although this program has been used by the USGS, no 
%warranty, expressed or implied, is made by the USGS or the United States 
%Government as to the accuracy and functioning of the program and related 
%program material nor shall the fact of distribution constitute any such 
%warranty, and no responsibility is assumed by the USGS in connection 
%therewith.

%Citation:  Butman, Bradford, Alexander, P. Soupy, Anderson, S.P., 
%Lightsom, F.L., Scotti, Alberto, and Beardsley, R.C., 2004, The 
%Massachusetts Bay Internal Wave Experiment, August 1998:  Data Report:  
%U.S. Geological Survey Data Series 85, 1 DVD-ROM.

dataDir = 'C:\SCHTUFF\MASS_BAY\MBAY_IWAVE\REPORT\DATAFILES';

%Site B
ncID = netcdf('5372adc-a_wave.nc', 'write');
origTime = singlejd(ncID{'time'}(:), ncID{'time2'}(:));
origU = ncID{'u_1205'}(:);
origV = ncID{'v_1206'}(:);
origW = ncID{'w_1204'}(:);

%Set up the new variables
newU = origU;
bads = find(newU > 1e30);
newU(bads) = NaN;
newV = origV;
bads = find(newV > 1e30);
newV(bads) = NaN;
newW = origW;
bads = find(newW > 1e30);
newW(bads) = NaN;

%Load in the lagged data
lagTime = load(fullfile(dataDir, 'site_b_lag_adcp.mat'), 'time');
lagTime = lagTime.time;
lagU = load(fullfile(dataDir, 'site_b_lag_adcp.mat'), 'uadcp');
lagU = lagU.uadcp;
lagV = load(fullfile(dataDir, 'site_b_lag_adcp.mat'), 'vadcp');
lagV = lagV.vadcp;
lagW = load(fullfile(dataDir, 'site_b_lag_adcp.mat'), 'wadcp');
lagW = lagW.wadcp;

lagTimeJul = datenum2julian(lagTime + datenum('1-Jan-1998'));

for targetEvent = 1:46;
    thisLagTime = lagTimeJul(:,targetEvent);
    [junk, thisStart] = min(abs(origTime - thisLagTime(1)));
    toReplace = thisStart:thisStart+129;
    
    newU(toReplace,:) = lagU(:,:,targetEvent);
    newV(toReplace,:) = lagV(:,:,targetEvent);
    newW(toReplace,:) = lagW(:,:,targetEvent);
end

ncID{'u_1205'}(:) = newU;
ncID{'v_1206'}(:) = newV;
ncID{'w_1204'}(:) = newW;

close(ncID)

ncclear
dataDir = 'C:\SCHTUFF\MASS_BAY\MBAY_IWAVE\REPORT\DATAFILES';
%Site C
ncID = netcdf('5352adc-a_wave.nc', 'write');
origTime = singlejd(ncID{'time'}(:), ncID{'time2'}(:));
origU = ncID{'u_1205'}(:);
origV = ncID{'v_1206'}(:);
origW = ncID{'w_1204'}(:);

%Set up the new variables
newU = origU;
bads = find(newU > 1e30);
newU(bads) = NaN;
newV = origV;
bads = find(newV > 1e30);
newV(bads) = NaN;
newW = origW;
bads = find(newW > 1e30);
newW(bads) = NaN;

%Load in the lagged data
lagTime = load(fullfile(dataDir, 'site_c_lag_adcp.mat'), 'time');
lagTime = lagTime.time;
lagU = load(fullfile(dataDir, 'site_c_lag_adcp.mat'), 'uadcp');
lagU = lagU.uadcp;
lagV = load(fullfile(dataDir, 'site_c_lag_adcp.mat'), 'vadcp');
lagV = lagV.vadcp;
lagW = load(fullfile(dataDir, 'site_c_lag_adcp.mat'), 'wadcp');
lagW = lagW.wadcp;

lagTimeJul = datenum2julian(lagTime + datenum('1-Jan-1998'));

for targetEvent = 1:23;
    thisLagTime = lagTimeJul(:,targetEvent);
    [junk, thisStart] = min(abs(origTime - thisLagTime(1)));
    toReplace = thisStart:thisStart+129;
    
    newU(toReplace,:) = lagU(:,:,targetEvent);
    newV(toReplace,:) = lagV(:,:,targetEvent);
    newW(toReplace,:) = lagW(:,:,targetEvent);
end

ncID{'u_1205'}(:) = newU;
ncID{'v_1206'}(:) = newV;
ncID{'w_1204'}(:) = newW;

close(ncID)
ncclear