% load in all of the sample data

samples=[];
salt1_residual_all=[];
salt2_residual_all=[];
oxy1_residual_all=[];
oxy2_residual_all=[];
pres_all=[];

for cast_num=[2 4:39 41:62],
 
%for cast_num=[2 4:13],   
  
if cast_num<10
    cast_num_str=['00' int2str(cast_num)];
else
    cast_num_str=['0' int2str(cast_num)];
end

clear dens1 sal1 sal2 oxy1_ml oxy2_ml dens1 dens2 pres temp1 temp2 oxy1_um oxy2_um salt_ave salt1 salt2 salt3 salt_ave oxy_cal_ML_L pres_cal;
runstring=['cal_sal_oxy_pne2017_', cast_num_str]
eval(runstring)

samples=[samples;cast_data];

salt1_residual=sal1-salt_ave;
salt2_residual=sal2-salt_ave;
oxy1_residual=oxy1_ml-oxy_cal_ml;
oxy2_residual=oxy2_ml-oxy_cal_ml;

salt1_residual_all=[salt1_residual_all; salt1_residual];
salt2_residual_all=[salt2_residual_all; salt2_residual];
oxy1_residual_all=[oxy1_residual_all; oxy1_residual];
oxy2_residual_all=[oxy2_residual_all; oxy2_residual];
pres_all=[pres_all; pres];

end;

savefile=['save /Users/renellys.perez/Breve/Observations/PNE2017_RB1701/calibration/results/calibration_data samples'];
eval(savefile)

load /Users/renellys.perez/Breve/Observations/PNE2017_RB1701/calibration/results/calibration_data.mat

cast_nums=[2 4:39 41:62];

close all;

figure;
map=colormap;
map=map([1:1:64],:);
colormap(map);
plot([-0.04 0.04],[0 0],'k--','LineWidth',1);
hold on;
plot([0 0],[-0.04 0.04],'k--','LineWidth',1);
plot([-0.04 0.04],[-0.04 0.04],'k-','LineWidth',2);
for i=1:length(cast_nums),
plot(salt1_residual_all(i,:),salt2_residual_all(i,:),'*','Color',map(i,:));
end;
colorbar
xlabel('Salt Residual, Sensor 1')
ylabel('Salt Residual, Sensor 2')
axis([-0.04 0.04 -0.04 0.04])
title('Salinity residuals (color = station #)')

figure;
map=colormap;
map=map([1:1:64],:);
colormap(map);
plot([-0.01 0.01]*5,[0 0],'k--','LineWidth',1);
hold on;
plot([0 0],[-0.01 0.01]*5,'k--','LineWidth',1);
plot([-0.01 0.01]*5,[-0.01 0.01]*5,'k-','LineWidth',2);
for i=1:length(cast_nums),
plot(oxy1_residual_all(i,:),oxy2_residual_all(i,:),'*','Color',map(i,:));
end;
colorbar
xlabel('Oxygen Residual, Sensor 1')
ylabel('Oxygen Residual, Sensor 2')
axis([-0.01 0.01 -0.01 0.01]*5)
title('Oxygen residuals (color = station #)')

figure;
map=colormap;
map=map([5:5:64],:);
colormap(map);
plot([-0.04 0.04],[0 0],'k--','LineWidth',1);
hold on;
plot([0 0],[-0.04 0.04],'k--','LineWidth',1);
plot([-0.04 0.04],[-0.04 0.04],'k-','LineWidth',2);
for i=1:12,
plot(salt1_residual_all(:,i),salt2_residual_all(:,i),'*','Color',map(i,:));
end;
colorbar
xlabel('Salt Residual, Sensor 1')
ylabel('Salt Residual, Sensor 2')
axis([-0.04 0.04 -0.04 0.04])
title('Salinity residuals (color = bottle #)')

figure;
map=colormap;
map=map([5:5:64],:);
colormap(map);
plot([-0.01 0.01]*5,[0 0],'k--','LineWidth',1);
hold on;
plot([0 0],[-0.01 0.01]*5,'k--','LineWidth',1);
plot([-0.01 0.01]*5,[-0.01 0.01]*5,'k-','LineWidth',2);
for i=1:12,
plot(oxy1_residual_all(:,i),oxy2_residual_all(:,i),'*','Color',map(i,:));
end;
colorbar
xlabel('Oxygen Residual, Sensor 1')
ylabel('Oxygen Residual, Sensor 2')
axis([-0.01 0.01 -0.01 0.01]*5)
title('Oxygen residuals (color = bottle #)')

btl=6

figure
plot(cast_nums,oxy1_residual_all(:,btl),'b*','MarkerSize',8);
hold on;
plot(cast_nums,oxy2_residual_all(:,btl),'r*','MarkerSize',8);
plot(cast_nums,(nanmean(oxy1_residual_all(:,btl))+nanstd(oxy1_residual_all(:,btl)))*ones(size(cast_nums)),'b--','LineWidth',1)
plot(cast_nums,(nanmean(oxy1_residual_all(:,btl))-nanstd(oxy1_residual_all(:,btl)))*ones(size(cast_nums)),'b--','LineWidth',1)
plot(cast_nums,(nanmean(oxy2_residual_all(:,btl))+nanstd(oxy2_residual_all(:,btl)))*ones(size(cast_nums)),'r--','LineWidth',1)
plot(cast_nums,(nanmean(oxy2_residual_all(:,btl))-nanstd(oxy2_residual_all(:,btl)))*ones(size(cast_nums)),'r--','LineWidth',1)
plot(cast_nums,nanmean(oxy1_residual_all(:,btl))*ones(size(cast_nums)),'b-','LineWidth',2)
plot(cast_nums,nanmean(oxy2_residual_all(:,btl))*ones(size(cast_nums)),'r-','LineWidth',2)
legend('Sensor 1','Sensor 2');
axis([2 max(cast_nums) -0.1 0.1]);
title(['Oxygen Residuals, bottle stop ' int2str(btl)]) 


figure
plot(cast_nums,salt1_residual_all(:,btl),'b*','MarkerSize',8);
hold on;
plot(cast_nums,salt2_residual_all(:,btl),'r*','MarkerSize',8);
plot(cast_nums,(nanmean(salt1_residual_all(:,btl))+nanstd(salt1_residual_all(:,btl)))*ones(size(cast_nums)),'b--','LineWidth',1)
plot(cast_nums,(nanmean(salt1_residual_all(:,btl))-nanstd(salt1_residual_all(:,btl)))*ones(size(cast_nums)),'b--','LineWidth',1)
plot(cast_nums,(nanmean(salt2_residual_all(:,btl))+nanstd(salt2_residual_all(:,btl)))*ones(size(cast_nums)),'r--','LineWidth',1)
plot(cast_nums,(nanmean(salt2_residual_all(:,btl))-nanstd(salt2_residual_all(:,btl)))*ones(size(cast_nums)),'r--','LineWidth',1)
plot(cast_nums,nanmean(salt1_residual_all(:,btl))*ones(size(cast_nums)),'b-','LineWidth',2)
plot(cast_nums,nanmean(salt2_residual_all(:,btl))*ones(size(cast_nums)),'r-','LineWidth',2)
legend('Sensor 1','Sensor 2');
axis([2 max(cast_nums) -0.03 0.03]);
title(['Salinity Residuals, bottle stop ' int2str(btl)]) 


for i=1:length(cast_nums),
presi=[10:10:1500];
salt1_residual_alli(i,:)=interp1(pres_all(i,:),salt1_residual_all(i,:),presi);
salt2_residual_alli(i,:)=interp1(pres_all(i,:),salt2_residual_all(i,:),presi);
end;

figure;
pcolor(cast_nums,-presi,salt1_residual_alli')
shading flat
caxis([-0.005 0.005])
colorbar;
title('Salinity Residual, sensor 1');

figure;
pcolor(cast_nums,-presi,salt2_residual_alli')
shading flat
caxis([-0.005 0.005])
colorbar;
title('Salinity Residual, sensor 2');

figure;
pcolor(cast_nums,-presi,salt1_residual_alli'-salt2_residual_alli')
shading flat
caxis([-0.005 0.005])
colorbar;
title('Salinity Residual, sensor1 - sensor 2');

