
%loop to plot ctd as in calctd if were not plotted before
clear all
o2_params
p_ylim=[0 3500]
p_filter=9; %median and running filter over 9 points (m)

%
%Location of final CTD data
%
rootdir='./';
dirctdf=[rootdir 'Caliprofiles/'];
read_moorspice_nc

%
% LOAD DATA
%
    gipbeg=find(actd.profileid==100*statbeg+castbeg);
    gipend=find(actd.profileid==100*statend+castend);
    giprof=gipbeg:gipend;
    if isempty(giprof),error('check castbeg and castend exist'),end

% load oxygen coefficients (and btl variable)
strf=sprintf('load Califiles/ox_cals_%s_is%d_ws%d_%s.mat',cname,iswitch,wswitch,csuf);
disp(strf)
eval(strf) %loads calib matrix 'final'
nprof=length(giprof);
figure(1);clf

% FIRT SUBSAMPLE MAIN STATIONS
for ipp=1:length(giprof) 
    iprof=giprof(ipp);
    ctd.stat=Stat(iprof); %current station
    ctd.cast=Cast(iprof); %current cast
    eval(sprintf('load %s/ox_cal_%s_STN%03d_CAST%02d.mat ',...
        dirctdf,cname,ctd.stat,ctd.cast))
       figure(1);clf;
    % selects bottle data for graphics, later on
    %select bottles for that profile (for graphic purpose)
    gistatb=find((ismember(btl0.stat,Stat(iprof))==1)&(ismember(btl0.cast,Cast(iprof))==1));
    if isempty(gistatb)
        gistatb=find((ismember(btl0.stat,Stat(iprof))==1));
        %get bottles from other cast of that station
        p_othercast=1;
    else
        p_othercast=0;
    end
    %variable btl contains only bottles for current ctd profile
    btl=keep_data(btl0, gistatb);
    %find flagged bottles for graphics
    %good bottles
    gigoodbtl=find(btl.wnklo2_flag==2);
    %bad bottles
    gibadbtl=find(btl.wnklo2_flag==3);
    %measured salinit bottles:
    gigoodbtls=find(btl.bsali~=35); %35 exact means no data

    %Use interpolated if available
    if isfield(ctd,'oxcali');
        ctdox=ctd.oxcali;
    else
        ctdox=ctd.oxcal;
    end
    if isfield(ctd,'dsalii')
        ctdsal=ctd.dsalii;
    else
        ctdsal=ctd.dsali;
    end
    if isfield(ctd,'dtemp68i')
        ctdtemp=ctd.dtemp68i;
    else
        ctdtemp=ctd.dtemp68;
    end
    %RED crosses for bottles correspond to current cast; blue to another
    %cast
        %Graphic to save
  
    subplot(1,2,1);
    [ax1,ax2,hl1,hl2]=plotxx(ctdtemp,-ctd.dpres,ctdsal,-ctd.dpres);grid on;
    hold on;
    if p_othercast
        plo2s=plot(btl.bsali(gigoodbtls),-btl.dpres(gigoodbtls),'bx',btl.bsali(gigoodbtls),-btl.dpres(gigoodbtls),'bo');
    else
        plo2s=plot(btl.bsali(gigoodbtls),-btl.dpres(gigoodbtls),'rx',btl.bsali(gigoodbtls),-btl.dpres(gigoodbtls),'bo');
    end
    gibadbtls=intersect(gigoodbtls,gibadbtl); %this will report flaged oxygen but existing salinities
    plo2sb=plot(btl.bsali(gibadbtls),-btl.dpres(gibadbtls),'kd');
    for itx=1:length(gigoodbtls)
        if btl.dpres(gigoodbtls(itx)) <= max(ctd.dpres)
            text(btl.bsali(gigoodbtls(itx))+0.02,-btl.dpres(gigoodbtls(itx)),sprintf('%d',btl.btlid(gigoodbtls(itx))))
        end
    end
    set(ax2,'YTickLabel',[])
    set(ax2,'position',[0.1300    0.1100    0.3347    0.800],'ylim',[-max(ctd.dpres) 0]); %to avoid having PSU label outside
    set(ax1,'position',[0.1300    0.1100    0.3347    0.800],'ylim',[-max(ctd.dpres) 0])
    xlabel('PSU')
    axes(ax1);xlabel('deg C');axes(ax2)
    
    subplot(1,2,2);
    pl0=plot(ctdox,-ctd.dpres,'r'); hold on;grid on;
    title(sprintf('Oxygen station %d cast %d',ctd.stat, ctd.cast))
    set(gca,'position',[0.5703    0.1100    0.3347    0.800],'ylim',[-max(ctd.dpres) 0])
  % glazeo - for station 40 bifurcation :  set(gca,'xlim',[140 220],'XTick',[140:20:220])
    plo3=plot(btl.wnklo2um(gibadbtl),-btl.dpres(gibadbtl),'kd');
    if p_othercast
        plo2=plot(btl.wnklo2um(gigoodbtl),-btl.dpres(gigoodbtl),'bx',btl.wnklo2um(gigoodbtl),-btl.dpres(gigoodbtl),'bo');
    else
        plo2=plot(btl.wnklo2um(gigoodbtl),-btl.dpres(gigoodbtl),'rx',btl.wnklo2um(gigoodbtl),-btl.dpres(gigoodbtl),'bo');
        for itx=1:length(btl.wnklo2um)
            if btl.dpres(itx)<= max(ctd.dpres)
                text(btl.wnklo2um(itx)+2,-btl.dpres(itx),sprintf('%d',btl.btlid(itx)))
            end
        end
    end
    if ~isempty(plo2)
        if ~isempty(plo3)
            legend([pl0, plo2(2), plo3],'CTD', 'sig-matched samples', 'o2-flagged','Location','southeast')
        else
            legend([pl0, plo2(2)],'CTD', 'sig-matched samples', 'Location','southeast')
        end
    else
        legend([pl0],'CTD', 'Location','southeast')
    end
    xlabel('umol/kg')
    
    if p_final
                
        namefig=sprintf('ox_cal_%s_STN%03d_CAST%02d',cname,ctd.stat,ctd.cast);
        set(1,'paperunits','inch','paperorient','port')
        set(1,'paperposition',[0.3 0.5 5 6])
        
        nfopeps=sprintf('%s%s.eps',direps,namefig)
        exportfig(1,nfopeps,'preview','tiff','LineMode','scaled','color','rgb',...
            'FontMode','fixed','FontSize',13,'height',12);
        
        nfopjpg=sprintf('%s%s.png',dirjpg,namefig)
        exportfig(1,nfopjpg,'format','png','LineMode','scaled','color','rgb',...
            'FontMode','fixed','FontSize',13,'height',12);
        
        pause(2)
    end
 
    
end


