clear

% The bin size is 4 m and blanking depth is 8 m

gpsfile = '~/research/cruise/2019_Thompson_TN368_0703-0719/data/gps/gps.mat';
outfile = '~/research/cruise/2019_Thompson_TN368_0703-0719/data/adcp/os75bb_uv_blank8mbin8m.mat';

gpsdata = load(gpsfile);
gpstime = datenum(gpsdata.gpsdate);
gpslon  = gpsdata.lon;
gpslat  = gpsdata.lat;

%load ~/research/cruise/2019_Thompson_TN368_0703-0719/data/adcp/contour_uv.mat
%load ~/research/cruise/2019_Thompson_TN368_0703-0719/data/adcp/contour_xy.mat
load /Volumes/tgt-data/adcp/proc/os75bb/contour/contour_uv.mat
load /Volumes/tgt-data/adcp/proc/os75bb/contour/contour_xy.mat

% read transect start time
addpath ~/research/cruise/2019_Thompson_TN368_0703-0719/data/scripts/
tn368_transect_startime


x    = xyt(1,:)';
y    = xyt(2,:)';
dday = xyt(3,:)';
u    = uv(:,1:2:end);
v    = uv(:,2:2:end);
%zc is center of depth bins

badi       = find(isnan(x));
x(badi)    = [];
y(badi)    = [];
dday(badi) = [];
u(:,badi)  = [];
v(:,badi)  = [];

adcptime = dday + datenum([2019 1 1]);
adcplon  = interp1(gpstime,gpslon,adcptime);
adcplat  = interp1(gpstime,gpslat,adcptime);
surfu    = u(2,:);
surfv    = v(2,:);

for it = 1:length(tstime)-1
   trind      = find(adcptime > tstime(it) & adcptime < tstime(it+1));
   tru{it}    = u(:,trind);
   trv{it}    = v(:,trind);
   trlon{it}  = adcplon(trind);
   trlat{it}  = adcplat(trind);
   trtime{it} = adcptime(trind);
   
end

if 0
    clr = {'r','b','g','m','k','r','b','g','m','k','r','b','g','m','k'};
    stl = {'-','-','-','-','-',':',':',':',':',':','-.','-.','-.','-.','-.'};
    figure(1);clf;
    for it = 1:length(tstime)-1
        tlat = trlat{it};
        tu   = tru{it};
        tusf = tu(2,:);
        plot(tlat,tusf,'color',clr{it},'linestyle',stl{it},'linewidth',2); hold on;
    end
end

save(outfile,'u','v','adcptime','adcplat','adcplon','zc');
disp(['Saved file: ' outfile]);
