% Calibrate the 1hz cnv and cnv_load file using the cond_coeff from the 1db
% calibrated data.  Also, check the 1 hz calibrated data with the 1 db
% calibrated data at the bottle stops to assure proper calibration of the
% 1Hz data.

%cruiseid='ab1202';

%load 1hz files
if 0,
    [cnv_1hz cnv_load_1hz]=load_new_cnv_1Hz(cruiseid)
end;

%load which proper sensor coefficients to be used
disp('Change conductivity coefficient being loaded')
pause
load cond_coeff_pre_10_34

for i = 1:length(cnv_load_1hz)
    
    cnv_loadc=cnv_load_1hz;
    sta=cnv_loadc(i).station;
    
    %apply conductivity calibration coefficients
    %condc needs to be in mS/m so multiply by 10(I load it in S/m)
    cond_aux=cnv_loadc(i).condpri.*10;
    pres_aux=cnv_loadc(i).pressure;
    condc=((cond_coeff(1)*cond_aux)+(cond_coeff(2).*sta)...
        +cond_coeff(3)+cond_coeff(4)*pres_aux);
    
    %calculating the salinity
    new_sal=sw_salt((condc)/sw_c3515,cnv_loadc(i).temppri...
        ,cnv_loadc(i).pressure);
    
    cnv_cal_1hz(i).station=cnv_loadc(i).station;
    cnv_cal_1hz(i).latitude=cnv_loadc(i).latitude;
    cnv_cal_1hz(i).longitude=cnv_loadc(i).longitude;   
    cnv_cal_1hz(i).gtime=cnv_loadc(i).gtime;
    cnv_cal_1hz(i).pressure=cnv_loadc(i).pressure;
    cnv_cal_1hz(i).depth=sw_dpth(cnv_loadc(i).pressure,cnv_loadc(i).latitude); 
    cnv_cal_1hz(i).elap_time_sec=cnv_loadc(i).timeS;
    cnv_cal_1hz(i).temperature=cnv_loadc(i).temppri;
    cnv_cal_1hz(i).conductivity(:,1)=condc;
    cnv_cal_1hz(i).salinity(:,1)=new_sal;
    cnv_cal_1hz(i).julian_day=cnv_1hz(i).timeJ;
    %cnv_cal_1hz(i).elap_time_sec=cnv_loadc(i).data(:,2);   
    clear condc new_sal
end;

%%%%%%%%%%%%%%%%%%%%%%%%% Some quality control %%%%%%%%%%%%%%%%%%%%%%%%%%%
for jj=1:length(cnv_cal_1hz)
    indp=find(abs(cnv_cal_1hz(jj).pressure)<.1);
    indt=find(abs(cnv_cal_1hz(jj).temperature)<1);
    inds=find(abs(cnv_cal_1hz(jj).salinity)<1 | abs(cnv_cal_1hz(jj).salinity)>40);
        if ~isempty(indp)
            cnv_cal_1hz(jj).pressure(indp)=NaN;
        end
        if ~isempty(indt)
            cnv_cal_1hz(jj).temperature(indt)=NaN;
        end
        if ~isempty(inds)
            cnv_cal_1hz(jj).salinity(inds)=NaN;
        end
end

clear cnv_loadc sta 
save ab1202_1hz_precal.mat cnv_cal_1hz



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%% Plot Comparisons %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

today = datestr(now)
lyear=num2str(str2num(today(8:11))-1)
lmonth='12'
lday='31'
ldate=datenum(str2num(lyear),str2num(lmonth),str2num(lday))
datestr(ldate)

for ii=1:length(btl)
    st=ii-1;
    matchtime=ldate+cnv_cal_1hz(ii).julian_day;
    %find the bin averaged 1 hz conductivity closest to how the 1 db bottle
    %value is calculated at trip depths
        for jj = 1:length(btl(ii).time)
        tt=find((btl(ii).time(jj) - 0.00007) < matchtime & matchtime < (btl(ii).time(jj) + 0.00007));
            if ~isempty(tt)
                cond_bin(jj,ii)=nanmean(cnv_cal_1hz(ii).conductivity(tt,:));
                sal_bin(jj,ii)=nanmean(cnv_cal_1hz(ii).salinity(tt,:));
            end;
            cond_bin(cond_bin==0)=NaN;
            sal_bin(sal_bin==0)=NaN;
        end;
     %this is used to find the single conductivity value closest to the
     %bottle time
     %cond_1hz=interp1(matchtime,cnv_cal_1hz(ii).conductivity,btl(ii).time,'nearest')';
     %cond_1hz=interp1(matchtime(tt,:),cnv_cal_1hz(tt,ii).conductivity,btl(ii).time,'nearest')';
     %chz(1:length(btl(ii).time),ii)=cond_1hz
     %clear cond_1hz
end;
    sal_aux=sal_bin(~isnan(sal_bin));
    cond_aux=cond_bin(~isnan(cond_bin));
    cond_match_cal.btlsal(cond_match_cal.btlsal == -999)=NaN;
    btlcnd=sw_cndr(cond_match_cal.btlsal,cond_match_cal.ctdtmp,cond_match_cal.ctdprs)*sw_c3515;
    %con_hz=chz(find(chz~=0));
    
    deep_idx=find(cond_match_cal.ctdprs>=500);
    
    
    %%%%%%%%%% Plot Conductivity differences against ctd cond %%%%%%%%%%%%%
    figure
    plot(cond_match_cal.stnnbr,cond_match_cal.ctdcon-cond_aux,'+')
    diff_median=nanmedian(cond_match_cal.ctdcon-cond_aux)
    diff_stddev=nanstd(cond_match_cal.ctdcon-cond_aux)
    ylabel('Conductivity Difference (mS/m)')
    xlabel('Pressure')
    title(['Median Diff: ',num2str(diff_median),' Std Dev: ',num2str(diff_stddev)],'fonts',16)  

    
    figure
    plot(cond_match_cal.ctdprs,cond_match_cal.ctdcon-cond_aux,'b+')
    hold on;
    plot(cond_match_cal.ctdprs(deep_idx),cond_match_cal.ctdcon(deep_idx)-cond_aux(deep_idx),'+')
    diff_median=nanmedian(cond_match_cal.ctdcon(deep_idx)-cond_aux(deep_idx))
    diff_stddev=nanstd(cond_match_cal.ctdcon(deep_idx)-cond_aux(deep_idx))
    ylabel('Conductivity Difference (mS/m)')
    xlabel('Station')
    title(['Deep Median Diff: ',num2str(diff_median),' Std Dev: ',num2str(diff_stddev)],'fonts',16)  
    
    %%%%%%%%%% Plot Conductivity differences against autosal %%%%%%%%%%%%%%
    figure
    plot(cond_match_cal.ctdprs,btlcnd-cond_aux,'g+')
    hold on;
    plot(cond_match_cal.ctdprs(deep_idx),btlcnd(deep_idx)-cond_aux(deep_idx),'+')
    diff_median=nanmedian(btlcnd(deep_idx)-cond_aux(deep_idx));
    diff_stddev=nanstd(btlcnd(deep_idx)-cond_aux(deep_idx));
    ylabel('Conductivity Difference (mS/m)','fonts',16)
    xlabel('Pressure','fonts',16)
    title(['Deep Median Diff: ',num2str(diff_median),' Std Dev: ',num2str(diff_stddev)],'fonts',16)  
    
    figure
    plot(cond_match_cal.stnnbr,btlcnd-cond_aux,'+')
    diff_median=nanmedian(btlcnd-cond_aux);
    diff_stddev=nanstd(btlcnd-cond_aux);
    ylabel('Conductivity Difference (mS/m)','fonts',16)
    xlabel('Station','fonts',16)
    title(['Median Diff: ',num2str(diff_median),' Std Dev: ',num2str(diff_stddev)],'fonts',16) 
    
    %%%%%%%%%% Plot Salinity differences%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    figure
    plot(cond_match_cal.ctdprs,cond_match_cal.btlsal-sal_aux,'g+')
    hold on;
    plot(cond_match_cal.ctdprs(deep_idx),cond_match_cal.btlsal(deep_idx)-sal_aux(deep_idx),'+')
    diff_median=nanmedian(cond_match_cal.btlsal(deep_idx)-sal_aux(deep_idx));
    diff_stddev=nanstd(cond_match_cal.btlsal(deep_idx)-sal_aux(deep_idx));
    ylabel('Salinity Difference (psu)','fonts',16)
    xlabel('Pressure','fonts',16)
    title(['Deep Median Diff: ',num2str(diff_median),' Std Dev: ',num2str(diff_stddev)],'fonts',16)  
    
    figure
    plot(cond_match_cal.stnnbr,cond_match_cal.btlsal-sal_aux,'g+')
    hold on;
    plot(cond_match_cal.stnnbr(deep_idx),cond_match_cal.btlsal(deep_idx)-sal_aux(deep_idx),'+')
    diff_median=nanmedian(cond_match_cal.btlsal-sal_aux);
    diff_stddev=nanstd(cond_match_cal.btlsal-sal_aux);
    ylabel('Salinity Difference (psu)','fonts',16)
    xlabel('Station','fonts',16)
    title(['Median Diff: ',num2str(diff_median),' Std Dev: ',num2str(diff_stddev)],'fonts',16)

    
