% paths={'D:\georg\work\delta_undulator_me\20130612_1630run4_15mmpsec\';
%     'D:\georg\work\delta_undulator_me\20130612_1607run1_5mmpsec\'
%     'D:\georg\work\delta_undulator_me\20130612_1615run2_5mmpsec\'
%     'D:\georg\work\delta_undulator_me\20130612_1624run3_10mmpsec\'
%     'D:\georg\work\delta_undulator_me\20130612_1636run5_20mmpsec\'};
paths={'V:\MET\MagServe\MagData\LCLS\Undulator\DeltaPrototype\20140506_test\'};
for pnum=1:length(paths)
    pathn=paths{pnum};
    load([pathn 'compd']);
    
    
    filesus=dir([pathn '*.bmp']);
    di=[];
    for i=1:length(filesus)
        di(i)=filesus(i).datenum;
    end
    
    [s, idx]=sort(di);
    
    files=filesus(idx);
    
    imtrigger=[];
    for i=1:length(files)
        str=(files(i).name);
        id=strfind(str,'.BMP');
        str(id:end)=[];
        id=strfind(str,'_');
        if id
            str(id)='.';
        end
        imtrigger(i)=str2double(str(2:end));
    end
    
    imy=mean(bsxfun(@minus,fftsoly,fftsoly(:,1)));
    imx=mean(bsxfun(@minus,fftsoly,fftsoly(:,1)));
    
    for i=100:100:length(ipos)
        idx=find(isnan(ipos(:,2)));
        ipos(idx,1)=NaN;
        idx =find(ipos(:,1)>i-500 & ipos(:,1)<i+500);
        avgn=mean(ipos(idx,2)-nominalpos(idx)');
        idn=find(abs(ipos(idx,2)-nominalpos(idx)'-avgn)>0.04);
        ipos(idx(idn),:)=NaN;
    end
    
    ipos(:,1)=[1:length(ipos)]';
    ipos(:,3)=ipos(:,2);
    for i=1:length(ipos)
        if (isnan(ipos(i,2)))
            idlo=find(~isnan(ipos(1:i,2)));
            if (~isempty(idlo))
                idlo=idlo(end);
            end
            idup=find(~isnan(ipos(i:end,2)));
            if (~isempty(idup))
                idup=idup(1)+i-1;
            end
            if (~isempty(idlo) && ~isempty(idup))
                ipos(i,3)=(ipos(idup,2)-ipos(idlo,2))/(idup-idlo)*(i-idlo)+ipos(idlo,2);
            end
        end
    end
    
    idx=find(~isnan(ipos(:,3)));
    stepsize=mean(diff(ipos(idx,3)));
    
    idx=find(isnan(ipos(:,3)));
    idxn=find(idx>200 & idx<round(length(ipos)/2));
    if ~isempty(idxn)
        ipos(idx(idxn),3)=ipos(idx(idxn(end))+1,3)+stepsize*(idx(idxn)-(idx(idxn(end))+1));
    end
    
    idx=find(isnan(ipos(:,3)));
    idxn=find(idx<length(ipos)-200 & idx>round(length(ipos)/2));
    if ~isempty(idxn)
        ipos(idx(idxn),3)=ipos(idx(idxn(end))+1,3)+stepsize*(idx(idxn(1))-1-(idx(idxn)));
    end
    
    z=ipos;
    z(:,2)=-z(:,2);
    z(:,3)=-z(:,3);
    
    z(:,2)=z(:,2)-z(201,3);
    z(:,3)=z(:,3)-z(201,3);
    nominalpos=nominalpos-nominalpos(201);
    
    
    ix=mean(bsxfun(@minus,fftsolx,fftsolx(:,1)));
    iy=mean(bsxfun(@minus,fftsoly,fftsoly(:,1)));
    
    idx=imtrigger==0;
    imtrigger(idx)=1;
    
    x=[];
    x(:,1)=[1:length(ipos)]';
    x(:,2:3)=NaN;
    x(imtrigger,2)=ix;
    x(imtrigger,3)=ix;
    y=[];
    y(:,1)=[1:length(ipos)]';
    y(:,2:3)=NaN;
    y(imtrigger,2)=iy;
    y(imtrigger,3)=iy;
    
    
    
    for i=1:length(x)
        if (isnan(x(i,2)))
            idlo=find(~isnan(x(1:i,2)));
            if (~isempty(idlo))
                idlo=idlo(end);
            end
            idup=find(~isnan(x(i:end,2)));
            if (~isempty(idup))
                idup=idup(1)+i-1;
            end
            if (~isempty(idlo) && ~isempty(idup))
                x(i,3)=(x(idup,2)-x(idlo,2))/(idup-idlo)*(i-idlo)+x(idlo,2);
                y(i,3)=(y(idup,2)-y(idlo,2))/(idup-idlo)*(i-idlo)+y(idlo,2);
            end
        end
    end
    
    x(:,4)=detrend(x(:,3),1);
    y(:,4)=detrend(y(:,3),1);
    
    
    
    figure
    plot(z(:,3),(z(:,3)+nominalpos')*1000)
    xlabel('Distance [mm]')
    ylabel('\DeltaZ Interferometer vs. Stage[\mum]')
    idx=find(pathn=='\');
    t=pathn(idx(end-1)+1:end-1);
    idx=find(t=='_');
    t(idx)=' ';
    idx=find(t=='.');
    t(idx)=' ';
    title(t)
    print (gcf,[pathn 'dz_' t],'-dpng'); 

    figure
    plot(z(:,3),x(:,4),'b')
    hold on
    plot(z(:,3),y(:,4),'r')
    xlabel('Distance [mm]')
    ylabel('\Delta CCD[\mum]')
    legend('\DeltaX','\DeltaY','location','NorthWest')
    title(t)
    print (gcf,[pathn 'CCD_pos_' t],'-dpng'); 

    save([pathn 'res_' t],'z','x','y');
end
