% Renellys' quick code to load in the bottle calibration data for 
% salinity and oxygen for cast 020
%
% Cast details: We fired 12 bottles numbered 1-7,13-17.  Last cast on the
% small frame! 
% Sample details: We sampled 1-7,13-17. Number 4 was leaking. Anomalous, so
% I'm NaNing
% Cal salinity bottle details: The salinity bottles were run sequentially
% 1801-1812
% Cal oxygen bottle details: The oxygen bottles were run sequentially
% 33,2-13 duplicate on 300m

close all;

beg_cast_num_str='018'; beg_cast_num=str2num(beg_cast_num_str)
end_cast_num_str='021';
cast_num_str='020'; cast_num=str2num(cast_num_str)

% Load in salt samples %

fid_salt=fopen(['/Users/renellys.perez/Breve/Observations/PNE2017_RB1701/calibration/salts/pne17_' beg_cast_num_str '_' end_cast_num_str '.dat'],'r');

skip_lines=89

num1=82+10;
num2=90+10;
nshift=7;
nshift2=1;

kk=1;
            
%read in bottle sample values for salts (average, reading 1, reading 2, reading 3)
for i=1:skip_lines
     fgetl(fid_salt);
end

for i=1:12,

         a=fgetl(fid_salt);
         if i<10 & beg_cast_num==cast_num,
         salt_ave(i)=str2num(a(num1:num2))
         a=fgetl(fid_salt);
         salt1(i)=str2num(a(num1-nshift:num2-nshift));
         a=fgetl(fid_salt);
         salt2(i)=str2num(a(num1-nshift:num2-nshift));
         a=fgetl(fid_salt);
         salt3(i)=str2num(a(num1-nshift:num2-nshift));
         else,
         salt_ave(i)=str2num(a(num1+nshift2:num2+nshift2))
         a=fgetl(fid_salt);
         salt1(i)=str2num(a(num1-nshift+nshift2:num2-nshift+nshift2));
         a=fgetl(fid_salt);
         salt2(i)=str2num(a(num1-nshift+nshift2:num2-nshift+nshift2));
         a=fgetl(fid_salt);
         salt3(i)=str2num(a(num1-nshift+nshift2:num2-nshift+nshift2));
end;
end;

fclose(fid_salt)

%read in CTD bottle sensor values
fid_bot=fopen(['/Users/renellys.perez/Breve/Observations/PNE2017_RB1701/ctd/pne17/hydro/bottle/pne17_' cast_num_str '.btl'],'r');

for i=1:270
    ff=fgetl(fid_bot);
end

    beg_bot_ctd=1;
    num_bot_ctd=12;
    
    jj=1;
for zz=beg_bot_ctd:num_bot_ctd
     if jj==1
         for qq=1:4*(beg_bot_ctd-1)
             a=fgetl(fid_bot);
         end
     end
     
     a=fgetl(fid_bot)
     
     %if zz<9 | zz>13,
     niskin(jj)=str2num(a(1:7));
     sal1(jj)=str2num(a(45:55));
     sal2(jj)=str2num(a(56:66));
     oxy1_ml(jj)=str2num(a(67:77));    %ml/l
     oxy1_um(jj)=str2num(a(100:110));   %uM/kg
     oxy2_ml(jj)=str2num(a(111:121));    %ml/l
     oxy2_um(jj)=str2num(a(144:154));   %uM/kg
     dens1(jj)=str2num(a(166:176));
     dens2(jj)=str2num(a(177:187));
     pres(jj)=str2num(a(211:221));
     temp1(jj)=str2num(a(222:232));
     temp2(jj)=str2num(a(233:243));
     jj=jj+1;
     jj
     %end;
     
     for qq=1:3
         ff=fgetl(fid_bot);
     end

end;

fclose(fid_bot)

% Read in calibration bottle oxygen data.
 
fid_oxy=fopen(['/Users/renellys.perez/Breve/Observations/PNE2017_RB1701/calibration/oxygen/pne17_' cast_num_str '.dat'],'r');
   % a=fgetl(fid_oxy);
   %  a=fgetl(fid_oxy);
    for kk=1:13,
        a=fgetl(fid_oxy);
        pres_cal(kk)=str2num(a(18:24));
        if pres_cal(kk)>999,
    oxy_cal_ML_L(kk)=str2num(a(136:142));
        elseif pres_cal(kk)>99,
    oxy_cal_ML_L(kk)=str2num(a(134:140));
        elseif pres_cal(kk)>9,
    oxy_cal_ML_L(kk)=str2num(a(134:140));
        else,
                oxy_cal_ML_L(kk)=str2num(a(134:140));
        end;
    end;
    fclose(fid_oxy);


    for kk=2:13,
        if pres_cal(kk)==pres_cal(kk-1);
            kk_dup=kk;
            diff(oxy_cal_ML_L(kk-1:kk))
        end;
    end;
    
    oxy_cal_ML_L(kk_dup-1)=mean(oxy_cal_ML_L(kk_dup-1:kk_dup));
    oxy_cal_ML_L=oxy_cal_ML_L([1:kk_dup-1 kk_dup+1:13]);
    pres_cal=pres_cal([1:kk_dup-1 kk_dup+1:13]);
    
    % Bottle 4 was leaking %
    oxy_cal_ML_L(4)=NaN;
    salt_ave(4)=NaN; salt1(4)=NaN; salt2(4)=NaN; salt3(4)=NaN;
    
% %oxy_cal is in uM/kg. convert to ml/l
% oxy_cal_ml=oxy_cal*0.0223916./(dens1+1000)*1000;
% 
oxy_cal_ml=oxy_cal_ML_L*0.0223916./(dens1+1000)*1000;
%oxy_cal_ml=oxy_cal_ML_L*0.0223916./(dens2+1000)*1000;

    
%RMS difference, mean difference for each cast
indg=find(~isnan(sal1)&~isnan(sal2)&~isnan(salt_ave));
rms_sal1_cast=std(sal1(indg)-salt_ave(indg));
rms_sal2_cast=std(sal2(indg)-salt_ave(indg));
mean_sal1_cast=mean(sal1(indg)-salt_ave(indg));
mean_sal2_cast=mean(sal2(indg)-salt_ave(indg));


indg=find(~isnan(oxy1_ml)&~isnan(oxy2_ml)&~isnan(oxy_cal_ml));
rms_oxy1_cast=std(oxy1_ml(indg)-oxy_cal_ml(indg));
rms_oxy2_cast=std(oxy2_ml(indg)-oxy_cal_ml(indg));
mean_oxy1_cast=mean(oxy1_ml(indg)-oxy_cal_ml(indg));
mean_oxy2_cast=mean(oxy2_ml(indg)-oxy_cal_ml(indg));


%store values at certain depth levels for each cast
dlev=2;

sal1_diff=sal1(dlev)-salt_ave(dlev);
sal2_diff=sal2(dlev)-salt_ave(dlev);
 
oxy1_diff=oxy1_ml(dlev)-oxy_cal_ml(dlev);
oxy2_diff=oxy2_ml(dlev)-oxy_cal_ml(dlev);



% if plot_figures
% %FIGURES
figure;
plot(oxy1_ml,-pres);
hold on;
plot(oxy2_ml,-pres,'r');
plot(oxy_cal_ml,-pres,'sk');
tt=title(['Cast ' cast_num_str '   Oxygen']);
set(tt,'fontsize',16);
ll=legend('Sensor 1','Sensor 2','Bottle',3);
yy=ylabel('Pressure (db)');
xx=xlabel('Oxygen conc. (ml l^{-1})');
set(yy,'fontsize',14);
set(xx,'fontsize',14);
printstring=['print -depsc /Users/renellys.perez/Breve/Observations/PNE2017_RB1701/calibration/plots/oxy_', cast_num_str '.eps']
eval(printstring)

% if get_salt
% 
figure;
plot(sal1,-pres);
hold on;
plot(sal2,-pres,'r');
plot(salt_ave,-pres,'sk');
plot(salt1,-pres,'og');
plot(salt2,-pres,'og');
plot(salt3,-pres,'og');
tt=title(['Cast ' cast_num_str '   Salinity']);
set(tt,'fontsize',16);
ll=legend('Sensor 1','Sensor 2','Bottle',4);
yy=ylabel('Pressure (db)');
xx=xlabel('Salinity');
set(yy,'fontsize',14);
set(xx,'fontsize',14);
printstring=['print -depsc /Users/renellys.perez/Breve/Observations/PNE2017_RB1701/calibration/plots/salt_', cast_num_str '.eps']
eval(printstring)


cast_data=[cast_num*ones(length(pres),1),[1:length(pres)]',sal1',sal2',oxy1_ml',oxy2_ml',temp1',temp2',pres',salt_ave',oxy_cal_ml'];

savefile=['save /Users/renellys.perez/Breve/Observations/PNE2017_RB1701/calibration/results/results', cast_num_str ,' cast_data cast_num']
eval(savefile)


