%Reads in the ADCp data from the Knorr ssystem for HiWinGS2013
% http://currents.soest.hawaii.edu/docs/adcp_doc/adcp_access/READING/MATLAB/index.html
% http://currents.soest.hawaii.edu/docs/adcp_doc/adcp_access/READING/MATLAB/GETMAT/index.html
% http://currents.soest.hawaii.edu/docs/adcp_doc/best_practices.html
% UHDAS cruise distributions typically have two kinds of Matlab files available. These are located in the contour and vector folders in the processing directories for each sonar (instrument+frequency).
datapath='S:\cruises\HIWINGS_2013\Knorr\SCS\Raw\kn213-03\adcp\proc\os75nb\';
ADCP=load_getmat([datapath 'contour\allbins_']);
ADCP_doy=ADCP.dday+1;
ADCP_u = ADCP.u .* ADCP.nanmask;  % ocean velocity (positive east, m/s)
ADCP_v = ADCP.v .* ADCP.nanmask;  % ocean velocity (positive north, m/s)
ADCP_w = ADCP.w .* ADCP.nanmask;  % 
% 
% figure(1); clf
% set(gcf,'defaultaxesfonts',14)
% set(gca,'fontname','time','fontsize',14);
% subplot(4,1,1);
% imagesc(ADCP_doy,ADCP.depth(:,1), ADCP_u,[-1.5 1]);title('Knorr- 75 kHz ADCPOcean Surveyor (OS75)')
% ylabel('Depth (m)');ylim([9 500])
% h = colorbar;ylabel(h, 'u (+ east, m/s)')
% subplot(4,1,2);
% imagesc(ADCP_doy,ADCP.depth(:,1), ADCP_v,[-1.5 1]);
% ylabel('Depth (m)');ylim([9 500])
% h = colorbar;ylabel(h, 'v (+ north, m/s)')
% subplot(4,1,3);
% imagesc(ADCP_doy,ADCP.depth(:,1), ADCP_w);
% ylabel('Depth (m)');ylim([9 500])
% h = colorbar;ylabel(h, 'w (up, m/s)')
% h3=get(gca,'position');
% subplot(4,1,4);
% plot(jd_pc,WXTP_TS,'b',jd_pc,SCSmet(:,5)*0.514,'r');grid;legend('WS','SOG')
% ylabel('Speed (m/s)');xlabel('DOY (2014))')
% h4=get(gca,'position'); h4(3)=h3(3); %sets same width for figure
% set(gca,'position',h4);axis([ADCP_doy(1) ADCP_doy(end) 0 35])
% orient tall

% For improved resolution at short ranges (less than 100 m depth), the 300-kHz Workhorse Mariner (WH300) may be used. It has a conventional transducer design with a separate ceramic element for each beam.
% min centered depth is ~9.07m
% 2-minute averaged values for wh300 (vs 5-min for OS75)
datapath='S:\cruises\HIWINGS_2013\Knorr\SCS\Raw\kn213-03\adcp\proc\wh300\';
WH300=load_getmat([datapath 'contour\allbins_']);
WH300_doy=WH300.dday+1;
WH300_u = WH300.u .* WH300.nanmask;  % ocean velocity (positive east, m/s)
WH300_v = WH300.v .* WH300.nanmask;  % ocean velocity (positive north, m/s)
WH300_w = WH300.w .* WH300.nanmask;  % 

figure(2); clf
set(gcf,'defaultaxesfonts',14)
set(gca,'fontname','time','fontsize',14);
subplot(4,1,1);
imagesc(WH300_doy,WH300.depth(:,1), WH300_u,[-1.5 1]);title('Knorr- 300-kHz Workhorse Mariner (WH300)')
ylabel('Depth (m)');ylim([9 50])
h = colorbar;ylabel(h, 'u (+ east, m/s)')
subplot(4,1,2);
imagesc(WH300_doy,WH300.depth(:,1), WH300_v,[-1.5 1]);
ylabel('Depth (m)');ylim([9 50])
h = colorbar;ylabel(h, 'v (+ north, m/s)')
subplot(4,1,3);
imagesc(WH300_doy,WH300.depth(:,1), WH300_w);
ylabel('Depth (m)');ylim([9 50])
h = colorbar;ylabel(h, 'w (up, m/s)')
h3=get(gca,'position');
subplot(4,1,4);
plot(jd_pc,WXTP_TS,'b',jd_pc,SCSmet(:,5)*0.514,'r');grid;legend('WS','SOG')
ylabel('Speed (m/s)');xlabel('DOY (2014))')
h4=get(gca,'position'); h4(3)=h3(3); %sets same width for figure
set(gca,'position',h4);axis([WH300_doy(1) WH300_doy(end) 0 35])
orient tall

% i=2000; %example of large diff
% lolo=find(abs(WH300_doy-ADCP_doy(i))==min(abs(WH300_doy-ADCP_doy(i))));
% figure(3);clf;
% plot(ADCP_u(:,i),ADCP.depth(:,1), WH300_u(:,lolo),WH300.depth(:,1),'g')
% 
% i=8000; %example of large diff
% lolo=find(abs(WH300_doy-ADCP_doy(i))==min(abs(WH300_doy-ADCP_doy(i))));
% figure(3);clf;
% plot(ADCP_u(:,i),ADCP.depth(:,1), WH300_u(:,lolo),WH300.depth(:,1),'g')

figure(3);clf;title('Current spped direction at 9m below WL - Knorr (WH300) ')
quiver(WH300_doy,WH300_doy*0,WH300_u(1,:),WH300_v(1,:));grid
xlim([WH300_doy(1) WH300_doy(end)])

figure;plot(WH300_doy,WH300_u(1,:),WH300_doy,WH300_v(1,:),'g');grid
hold on;plot(jd_pc,WXTP_TS/30,'k')
ylabel('Speed (m/s)');xlabel('DOY (2014))')
legend('u (+ east, m/s)','v (+ north, m/s)','WS/30 (m/s)')
axis([WH300_doy(1) WH300_doy(end) -1 1.5])

figure;
subplot(3,1,1);plot(WH300_doy,WH300_v(1,:),'g');grid;title('v (+ north, m/s)');ylim([-2 2]);xlim([WH300_doy(1) WH300_doy(end)])
subplot(3,1,2);plot(WH300_doy,WH300_u(1,:));grid;title('u (+ east, m/s)');ylim([-2 2]);xlim([WH300_doy(1) WH300_doy(end)])
subplot(3,1,3);plot(WH300_doy,sqrt(WH300_u(1,:).^2+WH300_v(1,:).^2));grid;title('Current speed (m/s)');xlabel('DOY (2014))');ylim([0 2]);xlim([WH300_doy(1) WH300_doy(end)])


%Clean up the data and average first 3 bins
Umean=nanmedian([WH300_u(1,:); WH300_u(2,:); WH300_u(3,:)]);
Vmean=nanmedian([WH300_v(1,:); WH300_v(2,:); WH300_v(3,:)]);
figure;
plot(WH300_doy,sqrt(WH300_u(1,:).^2+WH300_v(1,:).^2));
hold on;plot(WH300_doy,sqrt(WH300_u(2,:).^2+WH300_v(2,:).^2),'g');
hold on;plot(WH300_doy,sqrt(WH300_u(3,:).^2+WH300_v(3,:).^2),'r');
legend('9m','11m','13m');grid;
hold on;plot(WH300_doy,sqrt(Umean.^2+Vmean.^2),'k');

C9=sqrt(WH300_u(1,:).^2+WH300_v(1,:).^2);C11=sqrt(WH300_u(2,:).^2+WH300_v(2,:).^2);C13=sqrt(WH300_u(3,:).^2+WH300_v(3,:).^2);
C29=sqrt(WH300_u(11,:).^2+WH300_v(11,:).^2);
Cspeed=([C9; C11; C13]');NCspeed=[];Cmedian=nanmedian([C9; C11; C13]);
for l=1:length(Cspeed)
    NCspeed(l)=length(find(~isnan(Cspeed(l,:)))); %4545 profiles out of 25882 have 2points per lignes or less
end    
Cstd=nanstd(Cspeed');

Cqc=find(Cstd<0.09 & NCspeed>2);

ADCPspd=sqrt(ADCP_u(1,:).^2+ADCP_v(1,:).^2);
ADCPspdi=interp1(ADCP_doy,ADCPspd,WH300_doy);
ADCPui=interp1(ADCP_doy,ADCP_u(1,:),WH300_doy);
ADCPvi=interp1(ADCP_doy,ADCP_v(1,:),WH300_doy);

%CUrrent QC
lolo=(WH300_doy>283.108 & WH300_doy<283.130);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>283.385 & WH300_doy<283.398);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>283.385 & WH300_doy<283.398);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>285.484 & WH300_doy<285.489);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>286.253 & WH300_doy<286.26);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>286.51 & WH300_doy<286.555);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>286.6 & WH300_doy<286.63);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>287.255 & WH300_doy<287.414);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>289.695 & WH300_doy<289.705);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>290.242 & WH300_doy<290.246);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>290.48 & WH300_doy<290.487);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>291.596 & WH300_doy<291.6);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>291.649 & WH300_doy<291.6505);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>293.822 & WH300_doy<293.826);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>294.576 & WH300_doy<294.586);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>300.5 & WH300_doy<300.57);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>301.42 & WH300_doy<301.446);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>304.37 & WH300_doy<304.39);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>304.555 & WH300_doy<304.56);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
% lolo=(WH300_doy>304.8 & WH300_doy<305.4);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;Cmedian(lolo)=ADCPspdi(lolo); %ADCP 29m looks good so use this for this period
lolo=(WH300_doy>304.8 & WH300_doy<305.4);WH300_u(1,lolo)=ADCPui(lolo);WH300_v(1,lolo)=ADCPvi(lolo);WH300_u(2,lolo)=ADCPui(lolo);WH300_v(2,lolo)=ADCPvi(lolo);WH300_u(3,lolo)=ADCPui(lolo);WH300_v(3,lolo)=ADCPvi(lolo); %ADCP 29m looks good so use this for this period
lolo=(WH300_doy>305.65 & WH300_doy<305.75);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>306.558 & WH300_doy<306.64);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>307.01 & WH300_doy<307.03);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>307.7 & WH300_doy<307.89);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>311.335 & WH300_doy<311.3585);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>312.005 & WH300_doy<312.036);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>312.39 & WH300_doy<312.4);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>312.552 & WH300_doy<312.556);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>313.5385 & WH300_doy<313.582);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>314.034 & WH300_doy<314.038);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>314.495 & WH300_doy<314.496);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>314.22 & WH300_doy<314.224);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>314.24 & WH300_doy<314.25);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>314.268 & WH300_doy<314.273);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>315.455 & WH300_doy<315.495);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>316.57 & WH300_doy<316.573);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
lolo=(WH300_doy>316.625 & WH300_doy<316.88);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;
% lolo=(WH300_doy>316.95 & WH300_doy<317.6);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;Cmedian(lolo)=C29(lolo);%WH300 29m looks good so use this for this period
lolo=(WH300_doy>316.95 & WH300_doy<317.6);WH300_u(1,lolo)=WH300_u(11,lolo);WH300_v(1,lolo)=WH300_v(11,lolo);WH300_u(2,lolo)=WH300_u(11,lolo);WH300_v(2,lolo)=WH300_v(11,lolo);WH300_u(3,lolo)=WH300_u(11,lolo);WH300_v(3,lolo)=WH300_v(11,lolo);%WH300 29m looks good so use this for this period
lolo=(WH300_doy>318.37 & WH300_doy<318.5);WH300_u(1:3,lolo)=NaN;WH300_v(1:3,lolo)=NaN;

C9=sqrt(WH300_u(1,:).^2+WH300_v(1,:).^2);C11=sqrt(WH300_u(2,:).^2+WH300_v(2,:).^2);C13=sqrt(WH300_u(3,:).^2+WH300_v(3,:).^2);
C29=sqrt(WH300_u(11,:).^2+WH300_v(11,:).^2);
Cspeed=([C9; C11; C13]');NCspeed=[];Cmedian=nanmedian([C9; C11; C13]);
Umean=nanmedian([WH300_u(1,:); WH300_u(2,:); WH300_u(3,:)]);
Vmean=nanmedian([WH300_v(1,:); WH300_v(2,:); WH300_v(3,:)]);

hold on;plot(WH300_doy(Cqc),Cmedian(Cqc),'y--','LineWidth',2)


figure;quiver(WH300_doy,WH300_doy*0,Umean,Vmean,0);grid
figure;quiver(WH300_doy,WH300_doy*0,Umean,Vmean);grid
figure;feather(Umean,Vmean)
figure;plot(WH300_doy,[2*pi./(2*7.2921e-5*sin(WH300.lat*pi/180))]/3600/24)


figure;plot(WH300_doy,Umean,WH300_doy,Vmean,'g');grid
hold on;plot(jd_pc,WXTP_TS/30,'k')
ylabel('Speed (m/s)');xlabel('DOY (2014))')
legend('u (+ east, m/s)','v (+ north, m/s)','WS/30 (m/s)')
axis([WH300_doy(1) WH300_doy(end) -1 1.5])

filesave='C:\data\CRUISES\HiWinGS_2013\Knorr\flux\Processed\cleanedADCP10m';
eval(['save ' filesave ' WH300_doy Umean Vmean Cmedian'])  
clear filesave




%REads in cleaned data from Jules Hummon - see email on 04/18/2014
datapath='S:\cruises\HIWINGS_2013\Knorr\SCS\Raw\kn213_03_mat\kn213_03\wh300_clean\';
WH300_clean=load_getmat([datapath 'contour\allbins_']);
WH300_clean_doy=WH300_clean.dday+1;
WH300_clean_u = WH300_clean.u .* WH300_clean.nanmask;  % ocean velocity (positive east, m/s)
WH300_clean_v = WH300_clean.v .* WH300_clean.nanmask;  % ocean velocity (positive north, m/s)
WH300_clean_w = WH300_clean.w .* WH300_clean.nanmask;  % 
datapath='S:\cruises\HIWINGS_2013\Knorr\SCS\Raw\kn213_03_mat\kn213_03\wh300_first_edit\';
WH300_1stedit=load_getmat([datapath 'contour\allbins_']);
WH300_1stedit_doy=WH300_1stedit.dday+1;
WH300_1stedit_u = WH300_1stedit.u .* WH300_1stedit.nanmask;  % ocean velocity (positive east, m/s)
WH300_1stedit_v = WH300_1stedit.v .* WH300_1stedit.nanmask;  % ocean velocity (positive north, m/s)
WH300_1stedit_w = WH300_1stedit.w .* WH300_1stedit.nanmask;  % 

figure;plot(WH300_doy,WH300_u(1,:),'r',WH300_clean_doy,WH300_clean_u(1,:),'b',WH300_clean_doy,WH300_1stedit_u(1,:),'g')
figure;plot(WH300_clean_doy,WH300_1stedit_u(1,:),'g',WH300_clean_doy,WH300_clean_u(1,:),'b')


CurLud=load('C:\data\CRUISES\HiWinGS_2013\Knorr\flux\Processed\cleanedADCP10m');
figure;plot(CurLud.WH300_doy,CurLud.Umean,'k')
hold on;plot(WH300_clean_doy,WH300_1stedit_u(1,:),'g',WH300_clean_doy,WH300_clean_u(1,:),'b')
datapath='S:\cruises\HIWINGS_2013\Knorr\SCS\Raw\kn213_03_mat\kn213_03\os75bb\';
OS75bb=load_getmat([datapath 'contour\allbins_']);
OS75bb_doy=OS75bb.dday+1;
OS75bb_u = OS75bb.u .* OS75bb.nanmask;  % ocean velocity (positive east, m/s)
OS75bb_v = OS75bb.v .* OS75bb.nanmask;  % ocean velocity (positive east, m/s)

hold on;plot(OS75bb_doy,OS75bb_u(1,:),'m')


%Clean up the data and average first 3 bins
Umean=nanmedian([WH300_clean_u(1,:); WH300_clean_u(2,:); WH300_clean_u(3,:)]);
Vmean=nanmedian([WH300_clean_v(1,:); WH300_clean_v(2,:); WH300_clean_v(3,:)]);

C9=sqrt(WH300_clean_u(1,:).^2+WH300_clean_v(1,:).^2);C11=sqrt(WH300_clean_u(2,:).^2+WH300_clean_v(2,:).^2);C13=sqrt(WH300_clean_u(3,:).^2+WH300_clean_v(3,:).^2);
C29=sqrt(WH300_clean_u(11,:).^2+WH300_clean_v(11,:).^2);
Cspeed=([C9; C11; C13]');NCspeed=[];Cmedian=nanmedian([C9; C11; C13]);
for l=1:length(Cspeed)
    NCspeed(l)=length(find(~isnan(Cspeed(l,:)))); %4545 profiles out of 25882 have 2points per lignes or less
end    
Cstd=nanstd(Cspeed');
Cqc=find(Cstd<0.09 & NCspeed>2);

hold on;plot(WH300_clean_doy,Umean,'m')

%do some smoothing
N=5;
bf=ones(1,N)/N;  %Use this for 1 minute output
af=1;
Umeanf=Umean;
Umeanf(~isnan(Umean))=filtfilt(bf,af,Umean(~isnan(Umean)));

jdhourly=282:1/24:319;
Ulud=intavg(WH300_clean_doy,Umeanf,jdhourly);

N=30;
bf=ones(1,N)/N;  %Use this for 1 minute output
af=1;
Umeanf2=CurLud.Umean;
Umeanf2(~isnan(CurLud.Umean))=filtfilt(bf,af,CurLud.Umean(~isnan(CurLud.Umean)));
hold on;plot(CurLud.WH300_doy,Umeanf2,'g')


%
WH300_clean_ui1=interp1(WH300_clean_doy,WH300_clean_u(1,:),OS75bb_doy);
WH300_clean_ui2=interp1(WH300_clean_doy,WH300_clean_u(2,:),OS75bb_doy);
WH300_clean_ui3=interp1(WH300_clean_doy,WH300_clean_u(3,:),OS75bb_doy);

CurLud=load('C:\data\CRUISES\HiWinGS_2013\Knorr\flux\Processed\cleanedADCP10m');
datapath='S:\cruises\HIWINGS_2013\Knorr\SCS\Raw\kn213_03_mat\kn213_03\os75nb\';
OS75nb=load_getmat([datapath 'contour\allbins_']);
OS75nb_doy=OS75nb.dday+1;
OS75nb_u = OS75nb.u .* OS75nb.nanmask;  % ocean velocity (positive east, m/s)
OS75nb_v = OS75nb.v .* OS75nb.nanmask;  % ocean velocity (positive east, m/s)

hold on;plot(OS75nb_doy,OS75nb_u(1,:),'y')

% Umean=nanmedian([WH300_clean_ui1; WH300_clean_ui2; WH300_clean_ui3; OS75bb_u(1,:); OS75nb_u(1,:)]);
Umean=nanmedian([WH300_clean_ui1; WH300_clean_ui2; WH300_clean_ui3; OS75bb_u(1,:);]);

hold on;plot(OS75nb_doy,Umean,'m')
Umeanfinal=Umean;
Umeanfinal=interp1(OS75nb_doy(~isnan(Umean)),Umean(~isnan(Umean)),WH300_clean_doy,'spline','extrap');


%%%Interps 75bb onto WH300 and combine both WH300 and OS75bb
OS75ui=interp1(OS75bb_doy,OS75bb_u(1,:),WH300_clean_doy);
OS75vi=interp1(OS75bb_doy,OS75bb_v(1,:),WH300_clean_doy);
Umeanc=nanmedian([WH300_clean_u(1,:); WH300_clean_u(2,:); WH300_clean_u(3,:); OS75ui;]);Umeancf(~isnan(Umeanc))=filtfilt(bf,af,Umeanc(~isnan(Umeanc)));
Vmeanc=nanmedian([WH300_clean_v(1,:); WH300_clean_v(2,:); WH300_clean_v(3,:); OS75vi;]);Vmeancf(~isnan(Vmeanc))=filtfilt(bf,af,Vmeanc(~isnan(Vmeanc)));
hold on;plot(WH300_clean_doy,Umeanc,'g')

figure;plot(WH300_clean_doy,WH300_clean_u(1,:),'b',OS75bb_doy,OS75bb_u(1,:),'m')
hold on;plot(WH300_clean_doy,Umeanc,'g')
hold on;plot(CurLud.WH300_doy,CurLud.Umean,'k')

figure;plot(CurLud.WH300_doy,CurLud.Umean,'k');hold on;plot(WH300_clean_doy,Umeanc,'g')
figure;plot(CurLud.WH300_doy,CurLud.Vmean,'k');hold on;plot(WH300_clean_doy,Vmeanc,'g')

% Umeanfinal=Umeanc;
% Umeanfinal=interp1(WH300_clean_doy(~isnan(Umeanc)),Umeanc(~isnan(Umeanc)),WH300_clean_doy,'spline','extrap');

% Umean=nanmedian([WH300_clean_u(1,:); WH300_clean_u(2,:); WH300_clean_u(3,:)]);
%do some smoothing
N=30;
bf=ones(1,N)/N;  %Use this for 1 minute output
af=1;
CurLud.Umeanfinal=interp1(WH300_clean_doy(~isnan(CurLud.Umean)),CurLud.Umean(~isnan(CurLud.Umean)),WH300_clean_doy,'spline','extrap');
Umeanf=filtfilt(bf,af,CurLud.Umeanfinal);
CurLud.Vmeanfinal=interp1(WH300_clean_doy(~isnan(CurLud.Vmean)),CurLud.Vmean(~isnan(CurLud.Vmean)),WH300_clean_doy,'spline','extrap');
Vmeanf=filtfilt(bf,af,CurLud.Vmeanfinal);


figure;plot(WH300_clean_doy,Umeanf,'k',WH300_clean_doy,Umeanc,'g')
Umeanf(WH300_clean_doy<282.58)=NaN; %removes spikes due to spline
Umeanf(WH300_clean_doy>286.45 & WH300_clean_doy<286.6)=NaN;Vmeanf(WH300_clean_doy>286.45 & WH300_clean_doy<286.6)=NaN;
Umeanf(WH300_clean_doy>287.25 & WH300_clean_doy<287.5)=NaN;
Umeanf(WH300_clean_doy>300.44 & WH300_clean_doy<300.62)=NaN;Vmeanf(WH300_clean_doy>300.44 & WH300_clean_doy<300.62)=NaN;
Umeanf(WH300_clean_doy>305.6 & WH300_clean_doy<305.8)=NaN;
Umeanf(WH300_clean_doy>307.6 & WH300_clean_doy<307.95)=NaN;Vmeanf(WH300_clean_doy>307.6 & WH300_clean_doy<307.95)=NaN;
Umeanf(WH300_clean_doy>316.6 & WH300_clean_doy<316.88)=NaN;Vmeanf(WH300_clean_doy>316.6 & WH300_clean_doy<316.88)=NaN;
Umeanf(WH300_clean_doy>318.3)=NaN;Vmeanf(WH300_clean_doy>318.3)=NaN;
hold on;plot(WH300_clean_doy,Umeanf,'r')
Umeanf2=interp1(WH300_clean_doy(~isnan(Umeanf)),Umeanf(~isnan(Umeanf)),WH300_clean_doy,'pchip','extrap');
Vmeanf2=interp1(WH300_clean_doy(~isnan(Vmeanf)),Vmeanf(~isnan(Vmeanf)),WH300_clean_doy,'pchip','extrap');

% Umeanf2(WH300_clean_doy<282.58)=NaN; %removes spikes due to spline
hold on;plot(WH300_clean_doy,Umeanf2,'r')

N=30;
bf=ones(1,N)/N;  %Use this for 1 minute output
af=1;
Umeanc(isnan(Umeanc))=Umeanf2((isnan(Umeanc)));Vmeanc(isnan(Vmeanc))=Vmeanf2((isnan(Vmeanc)));  %replaces Nan in average by uncorrected smoothed version
Umeanfinal=filtfilt(bf,af,Umeanc);Vmeanfinal=filtfilt(bf,af,Vmeanc);
Umeanfinal(WH300_clean_doy<282.58)=Umeancf(WH300_clean_doy<282.58);Vmeanfinal(WH300_clean_doy<282.58)=Vmeancf(WH300_clean_doy<282.58);
hold on;plot(WH300_clean_doy,Umeanfinal,'k')

filesave='C:\data\CRUISES\HiWinGS_2013\Knorr\flux\Processed\cleanedADCP10m_vclean';
eval(['save ' filesave ' WH300_clean_doy Umeanfinal Vmeanfinal'])  
clear filesave