%can try using ctd_rd3.m to load in CTD .cnv files

%all station numbers
stations=[0:49];

save_data=1;

%get data from these stations
station1=0;
station2=49;

%max, min allowed values of oxygen titration, in ml/liter
max_oxy_cal_val=0.1;
min_oxy_cal_val=-0.5;

%---------------------------------------

samples=[];
for zz=station1:station2
station=int2str(stations(zz+1));
if stations(zz+1)<10
    station=['00' station];
else
    station=['0' station];
end


%----------------------------
%oxygen values from titration
%----------------------------
a=textread(['/Users/gregory.foltz/pne_cruises/pne17/cal_data/oxygen/PNE2018_STA' station '.DAT_ABR'],'','headerlines',1);

if sum(station=='000')==3
    a=a([1:4,7:14],:);
    a(4,:)=nan; %titration failed to find a value for this bottle
elseif sum(station=='001')==3
    a=a([1:2:end],:);
elseif sum(station=='002')==3
    a=a([3:end-1],:);
elseif sum(station=='003')==3
    a=a([2:end],:);
elseif sum(station=='007')==3
    a=a([2,3,11:16,18:21],:);
elseif sum(station=='009')==3
    a=a([4:15],:);
elseif sum(station=='011')==3
    a=a([1:8,10:13],:);
elseif sum(station=='015')==3
    a=a([1:9,11:13],:);
elseif sum(station=='016')==3
    a=a([1:3,5:13],:);
elseif sum(station=='020')==3
    a=a([2:12,1],:);
elseif sum(station=='021')==3
    a=a([1:6,8:13],:);
elseif sum(station=='026')==3
    a=a([1:3,5:13],:);
elseif sum(station=='029')==3
    a=a([1:6,8:13],:);
elseif sum(station=='030')==3
    a=a([1:2,6:15],:);
elseif sum(station=='032')==3
    a=a([1:3,5:13],:);
elseif sum(station=='034')==3
    a=a([1:2,5:14],:);
elseif sum(station=='035')==3
    a=a([1:8,10:13],:);
elseif sum(station=='041')==3
    a=a([1:4,6:12,14],:);
elseif sum(station=='044')==3
    a=a([1:10,12:13],:);
elseif sum(station=='047')==3
    a=a([1:6,9:14],:);
end

%bad titration values for casts 007, 008, 012, 013, 014
if stations(zz+1)==7|stations(zz+1)==8|stations(zz+1)==12|stations(zz+1)==13|stations(zz+1)==14
    a=a*nan;
end

%microMol/liter
oxy_titrate_mmol=a(:,8);
%convert to ml/liter
oxy_titrate_ml=oxy_titrate_mmol/44.66;


%----------------------------
%oxygen values from CTD
%----------------------------
for i=1:12
    if sum(station=='004')==3 | sum(station=='029')==3
        hdrlines=263+((i-1)*2);
    else
        hdrlines=264+((i-1)*2);
    end
    [b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26]=textread(['/Users/gregory.foltz/pne_cruises/pne17/proc_data/ctd/bottle/pne2018_' station '.btl'],'%n%s%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%s',1,'headerlines',hdrlines);

    temp1(i)=b15;
    temp2(i)=b16;
    sal1(i)=b7;
    sal2(i)=b8;
    %ml/liter
    oxy_ml1(i)=b9;
    oxy_ml2(i)=b10;
    pres(i)=b14;
    %convert to microMol/liter
    oxy_ctd_mmol1(i)=oxy_ml1(i)*44.66;
    oxy_ctd_mmol2(i)=oxy_ml2(i)*44.66;
end
oxy_titrate_ml(oxy_ml1'-oxy_titrate_ml<min_oxy_cal_val)=nan;
oxy_titrate_ml(oxy_ml1'-oxy_titrate_ml>max_oxy_cal_val)=nan;
%figure;plot(oxy_titrate_mmol,'sk');hold on;plot(oxy_ctd_mmol1,'or');plot(oxy_ctd_mmol2,'ob');

clear oxy_ctd_mmol;

%----------------------------------
%----------------------------------


%----------------------------
%salinity values from autosal
%----------------------------
%output from process_autosal_pne17.m
cal_data=load('/Users/gregory.foltz/pne_cruises/pne17/cal_data/salinity/process_output_salts.txt');
%salinity for each bottle, all casts
cal_sal=cal_data(:,5);
%cal_sal(49:8*12)=nan;

station=int2str(stations(zz+1));
if stations(zz+1)<10
    station=['00' station];
else
    station=['0' station];
end

%autosal salinity values for given cast
cal_sal_cast=cal_sal(12*(zz+1-1)+1:12*(zz+1));

%these bottles were accidentally dumped and filled with samples from a
%different cast
if stations(zz+1)==36
    cal_sal_cast(7:8)=nan;
end

%----------------------------
%salinity values from CTD
%----------------------------
for i=1:12
    if sum(station=='004')==3 | sum(station=='029')==3
        hdrlines=263+((i-1)*2);
    else
        hdrlines=264+((i-1)*2);
    end
    [b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26]=textread(['/Users/gregory.foltz/pne_cruises/pne17/proc_data/ctd/bottle/pne2018_' station '.btl'],'%n%s%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%s',1,'headerlines',hdrlines);

    ctd_sal(i)=b7;
end

%figure;plot(cal_sal_cast,'sk');hold on;plot(sal1,'or');plot(sal2,'ob');

clear ctd_sal;

%------------------------------
%------------------------------
%put all data into one variable, write to file

cast_data=[stations(zz+1)*ones(length(pres),1),[1:length(pres)]',sal1',sal2',oxy_ml1',oxy_ml2',temp1',temp2',pres',cal_sal_cast,oxy_titrate_ml];
samples=[samples;cast_data];

end %loop through casts

if save_data
    save('/Users/gregory.foltz/pne_cruises/pne17/proc_data/calibration_data.mat', 'samples');
end


%plot residuals
figure;
subplot(2,1,1);
plot(samples(:,3)-samples(:,10),'.k');
hold on;
plot(zeros(1,length(samples(:,1))),'r');
set(gca,'xlim',[0,length(samples(:,1))],'xtick',[1,[5*12:5*12:length(samples(:,1))]],'xticklabel',cellstr(int2str([1,[5:5:69]]')));
set(gca,'fontsize',14);
tt=text(750,0.12,'(a)');
set(tt,'fontsize',14);
yy=ylabel('Salin. diff.: sensor #1 - bottle (psu)');
set(yy,'fontsize',14);

subplot(2,1,2);
plot(samples(:,5)-samples(:,11),'.k');
hold on;
plot(zeros(1,length(samples(:,1))),'r');
set(gca,'xlim',[0,length(samples(:,1))],'xtick',[1,[5*12:5*12:length(samples(:,1))]],'xticklabel',cellstr(int2str([1,[5:5:69]]')));
set(gca,'fontsize',14);
tt=text(750,0.2,'(b)');
set(tt,'fontsize',14);
yy=ylabel('O_{2} diff.: sensor #1 - bottle (ml l^{-1})');
set(yy,'fontsize',14);
xx=xlabel('Cast number (12 samples per cast)');
set(xx,'fontsize',14);
pp=get(gca,'position');
set(gca,'position',[pp(1) pp(2)+0.06 pp(3) pp(4)]);