%% Problems_home00_look.m - Look at results of  Problems_home00_map - Dave W 12/20/2004

clear

set_amp_paths
clear

FrDir = 'C:\amp\home00_gainX4\'; % Older (original) processing
ToDir = 'C:\amp\home00\'; % Newer (revised) processing  -  NOTE that
%     the primary reason for reprocessing was to correct improper gain of vx/vy
%     circuits in epsilon computation, and improved pressures were secondary
FrProb = fullfile(FrDir,'problems');
ToProb = fullfile(ToDir,'problems');

Drops = [17500 17663 17677 17789 17706 17805 17469];
mnd = min(Drops); mxd = max(Drops); nd = length(Drops);
disp(['Check ' int2str(nd) ' drops between ' int2str(mnd) ' and ' int2str(mxd)])
x = input(' Okay? ', 's');
if isempty(x) | ~strcmp(x(1),'y')
    disp('NOT STARTED!')
    return
end

fPR = figure;
fEP = figure;
fTC = figure;
   
for id=1:length(Drops)
    drop = Drops(id); dn = int2str(drop);
    % original problem,data files
    PFFvx = fullfile(FrProb,'vx',['badvx' dn '.mat']);
    PFFvy = fullfile(FrProb,'vy',['badvy' dn '.mat']);
    PFFpr = fullfile(FrProb,'pr',['badpr' dn '.mat']);
    PFFcn = fullfile(FrProb,'cn',['badcn' dn '.mat']);
    OFFcn = fullfile(FrProb,'cn',['offsetcn' dn '.mat']);
    PFFtl = fullfile(FrProb,'tl',['badtl' dn '.mat']);
    DFFvxy = fullfile(FrDir,'eps',['eps' dn '.mat']);
    DFFpr = fullfile(FrDir,'pr',['pr' dn '.mat']);
    DFFtlcn = fullfile(FrDir,'thetasd',['tc' dn '.mat']);
    % new problem,data files
    PFTvx = fullfile(ToProb,'vx',['badvx' dn '.mat']);
    PFTvy = fullfile(ToProb,'vy',['badvy' dn '.mat']);
    PFTpr = fullfile(ToProb,'pr',['badpr' dn '.mat']);
    PFTcn = fullfile(ToProb,'cn',['badcn' dn '.mat']);
    OFTcn = fullfile(ToProb,'cn',['offsetcn' dn '.mat']);
    PFTtl = fullfile(ToProb,'tl',['badtl' dn '.mat']);
    DFTvxy = fullfile(ToDir,'eps',['eps' dn '.mat']);
    DFTpr = fullfile(ToDir,'pr',['pr' dn '.mat']);
    DFTtlcn = fullfile(ToDir,'thetasd',['tc' dn '.mat']);
    
    disp(['Checking drop ' dn ' ...'])
    %% Plot pr's %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    clear badpr BP
    BP.badpr = []; badpr = [];
    if exist(PFFpr)
        BP = load(PFFpr);
        if exist(PFTpr)
            load(PFTpr)
        end
    end
    OD = load(DFFpr); ND = load(DFTpr);
    ol = length(OD.pravg); nl = length(ND.pravg);
    [prmO,imO] = max(OD.pravg);  [prmN,imN] = max(ND.pravg);
    figure(fTC), clf
    plot(1:imO-1,diff(OD.pravg(1:imO))*100*25,'g-',...
        1:imN-1,diff(ND.pravg(1:imN))*100*25,'r-'), grid on, zoom on
    title('scan vs. approx dz/dt')
    figure(fPR), clf
    plot(1:ol,OD.pravg,'g-',imO,prmO,'bx',1:nl,ND.pravg,'r-',imN,prmN,'kx')
    axis ij,grid on, hold on
    title(['scan vs. pravg ' dn])
    for i=1:size(BP.badpr,1)
        db = OD.pravg-BP.badpr(i,1);
        de = OD.pravg-BP.badpr(i,2);
        ib = find(db>=0); 
        if ~isempty(ib)
            ib = ib(1); 
            plot(ib,BP.badpr(i,1),'g+')
        end
        ie = find(de<=0); 
        if ~isempty(ie)
            ie = ie(end);
            plot(ie,BP.badpr(i,2),'b*')
        end
        pause
    end
    for i=1:size(badpr,1)
        db = ND.pravg-badpr(i,1);
        de = ND.pravg-badpr(i,2);
        ib = find(db>=0); 
        if ~isempty(ib)
            ib = ib(1); 
            plot(ib,badpr(i,1),'r+')
        end
        ie = find(de<=0); 
        if ~isempty(ie)
            ie = ie(end);
            plot(ie,badpr(i,2),'k*')
        end
        pause
    end
    clear badpr BP OD ND
    pause

    %% plot eps's %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    clear badvx badvy ShearProbeX ShearProbeY BX BY
    BX.badvx = []; BY.badvy = []; badvx = []; badvy = [];
    if exist(PFFvx)
        BX = load(PFFvx);
        if exist(PFTvx)
            load(PFTvx)
        end
    end
    if exist(PFFvy)
        BY = load(PFFvy);
        if exist(PFTvy)
            load(PFTvy)
        end
    end
    OD = load(DFFvxy); ND = load(DFTvxy);
    ol = length(OD.pr_eps); nl = length(ND.pr_eps);
    if ol~=nl
        disp(['  %% pr_eps length, old=' int2str(ol) ', new=' int2str(nl)])
        err = 1;
    end
    figure(fTC), clf
    plot(1:ol,OD.pr_eps,'g-',1:nl,ND.pr_eps,'r-'), axis ij, grid on,zoom on
    title(['epno vs. pr\_eps ' dn])
    figure(fEP), clf
    semilogx(OD.epsx,OD.pr_eps,'g-',OD.epsy,OD.pr_eps,'r-', ...
        ND.epsx,ND.pr_eps,'c-',ND.epsy,ND.pr_eps,'m-')
    hold on, grid on, axis ij
    title(['eps vs. pr ' dn])
    for ic=1:4
        switch ic
            case 1
                prbad = BX.badvx; cc = 'gr';
            case 2
                prbad = badvx; cc = 'cm';
            case 3
                prbad = BY.badvy; cc = 'rk';
            case 4
                prbad = badvy; cc = 'mb';
        end
        for i=1:size(prbad,1)
            figure(fEP)
            ha = semilogx('v6', [1e-10 1e-4], [prbad(i,1) prbad(i,1)], [cc(1) '-']);
            hb = semilogx('v6', [1e-10 1e-4], [prbad(i,2) prbad(i,2)], [cc(2) '-']);
            pause
            delete(ha); delete(hb)
        end
    end
    pause
    clear badvx badvy ShearProbeX ShearProbeY BX BY OD ND
    
    %% Map badtl,badcn,offsetcn files %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    clear badtl badcn offsetcn BT BC OC
    BT.badtl = []; BC.badcn = []; OC.offsetcn = []; badtl = []; badcn = []; offsetcn = [];
    if exist(PFFtl)
        BT = load(PFFtl);
        if exist(PFTtl)
            load(PFTtl)
        end
    end
    if exist(PFFcn)
        BC = load(PFFcn);
        if exist(PFTcn)
            load(PFTcn)
        end
    end
    if exist(OFFcn)
        OC = load(OFFcn);
        if exist(OFTcn)
            load(OFTcn)
        end
    end
    OD = load(DFFtlcn); ND = load(DFTtlcn);
    ol = length(OD.pr_thetasd); nl = length(ND.pr_thetasd);
    if ol~=nl
        disp(['  %% pr_thetasd length, old=' int2str(ol) ', new=' int2str(nl)])
        err = 1;
    end
    figure(fEP), clf
    plot(1:ol,OD.pr_thetasd,'g-',1:nl,ND.pr_thetasd,'r-'), axis ij, grid on,zoom on
    title(['scan vs. pr\_thetasd ' dn])
    figure(fTC), clf
    hT = subplot(1,2,2);
    plot(OD.temp,OD.pr_thetasd,'g-',ND.temp,ND.pr_thetasd,'r-')
    axis ij, grid on, hold on
    xlabel('TL');
    hC = subplot(1,2,1);
    plot(OD.cn,OD.pr_thetasd,'g-',ND.cn,ND.pr_thetasd,'r-')
    axis ij, grid on, hold on
    xlabel('CN');
    tR = [min(ND.temp), max(ND.temp)];
    cR = [min(ND.cn), max(ND.cn)];

    for ic=1:6
        switch ic
            case 1
                prbad = BT.badtl; cc = 'gr'; axes(hT); xd = tR;
            case 2
                prbad = badtl; cc = 'cm'; axes(hT); xd = tR;
            case 3
                prbad = BC.badcn; cc = 'gr'; axes(hC); xd = cR;
            case 4
                prbad = badcn; cc = 'cm'; axes(hC); xd = cR;
            case 5
                prbad = BC.offsetcn; cc = 'rk'; axes(hC); xd = cR;
                xlabel('CN offs');
            case 6
                prbad = offsetcn; cc = 'mb'; axes(hC); xd = cR;
                xlabel('CN offs');
        end
        for i=1:size(prbad,1)
            ha = semilogx('v6', xd, [prbad(i,1) prbad(i,1)], [cc(1) '-']);
            hb = semilogx('v6', xd, [prbad(i,2) prbad(i,2)], [cc(2) '-']);
            pause
            delete(ha); delete(hb)
        end
    end
    pause
    clear badvx badvy ShearProbeX ShearProbeY BX BY OD ND
end
    
