clear
close all


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

lista = importdata('files.txt');


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

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

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

for ii=1:nf
    filename = ['Data/',direc{1},lista{ii}(6:10),'_data_full'];
    load(filename)
        S = squeeze(data_full(:,4,:));
     zmid = squeeze(data_full(:,6,1));
   [~,oi] = unique(lon);
   x_int = linspace(lon(1),lon(end),161);
   v = interp1(lon(oi),S(:,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;
   % figure(1),contourf(S,34:.1:36);colormap(jet);ylim([0 300]);pause(1)
   % ii
end
%stop
for ii=1:nf
    filename = ['Data/',direc{2},lista{ii}(6:10),'_data_full'];
load(filename)
        S = squeeze(data_full(:,4,:));
     zmid = squeeze(data_full(:,6,1));
   [~,oi] = unique(lon);
   x_int = linspace(lon(1),lon(end),161);
   v = interp1(lon(oi),S(:,oi)',x_int);%F(x,y);
    v2(:,:,ii) = v;
 %   figure(1),contourf(S,34:.1:36);colormap(jet);ylim([0 300]);pause(1)
end

for ii=1:nf
    filename = ['Data/',direc{3},lista{ii}(6:10),'_data_full'];
    load(filename)
         S = squeeze(data_full(:,4,:));
      zmid = squeeze(data_full(:,6,1));
    [~,oi] = unique(lon);
    x_int = linspace(lon(1),lon(end),161);
  v = interp1(lon(oi),S(:,oi)',x_int);%F(x,y);
   v3(:,:,ii) = v;
 %   figure(1),contourf(S,34:.1:36);colormap(jet);pause(1)
end

for ii=1:nf
    filename = ['Data/',direc{4},lista{ii}(6:10),'_data_full'];
    load(filename)
         S = squeeze(data_full(:,4,:));
      zmid = squeeze(data_full(:,6,1));
    [~,oi] = unique(lon);
    x_int = linspace(lon(1),lon(end),161);
   v = interp1(lon(oi),S(:,oi)',x_int);%F(x,y);
   v4(:,:,ii) = v;
 %   figure(1),contourf(S,34:.1:36);colormap(jet);pause(1)
end

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 .2])
ylim([0 800]);xlim([-53 20])
set(gca,'ydir','reverse','fontsize',12)
col=colorbar;set(col,'ticks',0:.025:.2,'ticklabels',0:.025:.2)
mycolorbar(col,mapa)
text(-50,750,'a)','backgroundcolor','w','fontsize',18,'fontweight','bold')
print -dpng -r300 sal_res_2_1.png

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 .2])
ylim([0 800]);xlim([-53 20])
set(gca,'ydir','reverse','fontsize',12)
col = colorbar;set(col,'ticks',0:.025:.2,'ticklabels',0:.025:.2)
mycolorbar(col,mapa)
text(-50,750,'b)','backgroundcolor','w','fontsize',18,'fontweight','bold')
print -dpng -r300 sal_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 .2])
ylim([0 800]);xlim([-53 20])
set(gca,'ydir','reverse','fontsize',12)
col = colorbar;set(col,'ticks',0:.025:.2,'ticklabels',0:.025:.2)
mycolorbar(col,mapa)
text(-50,750,'d)','backgroundcolor','w','fontsize',18,'fontweight','bold')
print -dpng -r300 sal_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-v3,[],3)')
pcolor(x_int,zmid,rmse(v2,v4,3)');
shading flat
caxis([0 .2])
ylim([0 800]);xlim([-53 20])
set(gca,'ydir','reverse','fontsize',12)
col = colorbar;set(col,'ticks',0:.025:.2,'ticklabels',0:.025:.2)
mycolorbar(col,mapa)
text(-50,750,'c)','backgroundcolor','w','fontsize',18,'fontweight','bold')
print -dpng -r300 sal_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
