clear
close all


eval('! ls -1 Data/transport/XBT_newsal_goes_noyear_noint/ > files.txt');

lista = importdata('files.txt');


direc = {'Data/transport/XBT_newsal_goes_noyear_noint/',...
         'Data/transport/XBT_newsal_thacker_noyear_noint/',...
         'Data/transport/XBT_newsal_annual_noyear_noint/',...
         'Data/transport/XBT_newsal_stomlin_noyear_noint/'}

x_int = -60:.5:20;
ni = length(x_int);
nf = length(lista);
nz = 3000; %length(zmid);

dist = zeros(ni,nf);
v1 = zeros(ni,nz,nf);
v2 = v1;v3=v1;v4=v1;

for ii=1:nf
    filename = [direc{1},lista{ii}];
    load(filename)
   [~,oi] = unique(lonv);
   x_int = linspace(lonv(1),lonv(end),161);
   v = interp1(lonv(oi),vel(oi,:),x_int);%F(x,y);
      dist1 = (sw_dist(x_int*0-34.5,x_int,'km'));
      dist(:,ii) = cumsum([0 dist1]'.*double(~isnan(v(:,1))));
    v1(:,:,ii) = v;
end

for ii=1:nf
    filename = [direc{2},lista{ii}];
    load(filename)
   [~,oi] = unique(lonv);
   x_int = linspace(lonv(1),lonv(end),161);
   v = interp1(lonv(oi),vel(oi,:),x_int);%F(x,y);
   v2(:,:,ii) = v;
end

for ii=1:nf
    filename = [direc{3},lista{ii}];
    load(filename)
    [~,oi] = unique(lonv);
   x_int = linspace(lonv(1),lonv(end),161);
     v=interp1(lonv(oi),vel(oi,:),x_int);%F(x,y);
   v3(:,:,ii) = v;
end

for ii=1:nf
    filename = [direc{4},lista{ii}];
    load(filename)
    [~,oi] = unique(lonv);
   x_int = linspace(lonv(1),lonv(end),161);
     v=interp1(lonv(oi),vel(oi,:),x_int);%F(x,y);
   v4(:,:,ii) = v;
end

load mapa;
%%
figure(1),clf
set(gcf,'PaperPosition',[1 1 5 4]); 
set(gcf,'DefaultLineLineWidth',1.5)
wysiwyg;
pcolor(x_int,zmid,nanmean(v1,3)');caxis([-0.1 0.1])
pcolor(x_int,zmid,nanstd(v1,[],3)');caxis([0 .16])
%pcolor(x_int,zmid,rmse(v2,v1,3)')
shading flat

%
ylim([0 800]);xlim([-53 20])
set(gca,'ydir','reverse','fontsize',12)
col=colorbar;%set(col,'ticks',0:.005:.05,'ticklabels',0:.01:.05)
mycolorbar(col,mapa)
text(-50,750,'a)','backgroundcolor','w','fontsize',18,'fontweight','bold')

%%
load newdefault.spk
mapa = interp1(newdefault(:,1),newdefault(:,2:4),0:10:100);
mapa=mapa/100;

figure(1),clf
set(gcf,'PaperPosition',[1 1 5 4]); 
set(gcf,'DefaultLineLineWidth',1.5)
wysiwyg;
%pcolor(x_int,zmid,nanstd(v2-v1,[],3)')
pcolor(x_int,zmid,rmse(v2,v1,3)')
shading flat
%caxis([0 .14])
caxis([0 .04])
ylim([0 800]);xlim([-53 20])
set(gca,'ydir','reverse','fontsize',12)
col=colorbar;%set(col,'ticks',0:.005:.05,'ticklabels',0:.01:.05)
mycolorbar(col,mapa)
text(-50,750,'d)','backgroundcolor','w','fontsize',18,'fontweight','bold')
%print -dpng -r300 vel_res_2_1.png
stop
figure(2),clf
set(gcf,'PaperPosition',[1 1 5 4]); 
set(gcf,'DefaultLineLineWidth',1.5)
wysiwyg;
%pcolor(x_int,zmid,nanstd(v3-v1,[],3)')
pcolor(x_int,zmid,rmse(v3,v1,3)')
shading flat
caxis([0 .16])
%caxis([0 .4])
ylim([0 800]);xlim([-53 20])
set(gca,'ydir','reverse','fontsize',12)
col = colorbar;set(col,'ticks',0:.02:.16,'ticklabels',0:.02:.16)
%col = colorbar;set(col,'ticks',0:.04:.4,'ticklabels',0:.02:.4)
mycolorbar(col,mapa)
text(-50,750,'e)','backgroundcolor','w','fontsize',18,'fontweight','bold')
%print -dpng -r300 vel_res_3_1.png

figure(3),clf
set(gcf,'PaperPosition',[1 1 5 4]); 
set(gcf,'DefaultLineLineWidth',1.5)
wysiwyg;
%pcolor(x_int,zmid,nanstd(v2-v3,[],3)')
pcolor(x_int,zmid,rmse(v2,v3,3)')
shading flat
caxis([0 .16])
%caxis([0 .4])
ylim([0 800]);xlim([-53 20])
set(gca,'ydir','reverse','fontsize',12)
col = colorbar;set(col,'ticks',0:.02:.16,'ticklabels',0:.02:.16)
%col = colorbar;set(col,'ticks',0:.04:.4,'ticklabels',0:.02:.4)
mycolorbar(col,mapa)
text(-50,750,'g)','backgroundcolor','w','fontsize',18,'fontweight','bold')
%print -dpng -r300 vel_res_2_3.png

figure(4),clf
set(gcf,'PaperPosition',[1 1 5 4]); 
set(gcf,'DefaultLineLineWidth',1.5)
wysiwyg;
%pcolor(x_int,zmid,nanstd(v2-v4,[],3)')
pcolor(x_int,zmid,rmse(v2,v4,3)')
shading flat
caxis([0 .04])
%caxis([0 .14])
ylim([0 800]);xlim([-53 20])
set(gca,'ydir','reverse','fontsize',12)
%col = colorbar;set(col,'ticks',0:.02:.14,'ticklabels',0:.02:.16)
col = colorbar;%set(col,'ticks',0:.04:.4,'ticklabels',0:.02:.4)
mycolorbar(col,mapa)
text(-50,750,'f)','backgroundcolor','w','fontsize',18,'fontweight','bold')
%print -dpng -r300 vel_res_2_4.png

% for ii=1:nf
%     x0 = x_int;x0(isnan(v(:,1))=nan;
% dist(:,ii) = sw_dist(x0*0-34.5,x0,'km')
% end
