clear

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

gpsfile = '/Users/hildeoliver/Documents/rgb19/gps_16to18.mat';
outfile = '/Users/hildeoliver/Documents/rgb19/adcp/os75bb_uv_blank8mbin4m.mat';

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

load /Users/hildeoliver/Documents/rgb19/adcp/os75bb_cont_uv.mat
load /Users/hildeoliver/Documents/rgb19/adcp/os75bb_cont_xy.mat

% read transect start time
addpath /Users/hildeoliver/Documents/rgb19/scripts/
rb1904_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]);
