clear;
%base='map_result6';
%base='../../exp_IMAEAfm/check_map/map_s1'
base='../../exp_IEAfm/cal2/scan_map_s1'
body = 7;

ORGA = 170;
ORGB = 170;

RAM_X = 128;
RAM_Y = 128;
%info = dlmread(base,' ');
[fname, vx, orx, ory, sort, time, rec] = textread(base,'%s %f %d %d %s %s %s');
[fnum, tmp] = size(fname);

for i = 1:1:fnum
    fname_ = char(fname(i));
    fname_f = ['../../exp_data/FMIEA/', fname_];
 
    range =[body 0 body+127 127];
    dat = dlmread(fname_f, ' ', range);
    hdr=textread(fname_f,'%s',10);
    SORT = char(hdr(1));
    DNA  = str2num(char(hdr(2)));
    DNB  = str2num(char(hdr(3)));
    VA0  = str2num(char(hdr(4)));
    VA1  = str2num(char(hdr(5)));
    VB0  = str2num(char(hdr(6)));
    VB1  = str2num(char(hdr(7)));
    CTRT = char(hdr(8));
    SNUM = char(hdr(9));
    ADD  = str2num(char(hdr(10)));
    tl = ['cr:',CTRT,' num:',SNUM];
    %a0 = int8(DNA*ORGA/(VA1-VA0))
    %b0 = int8(DNA*ORGA/(VA1-VA0))
    a0 = round(DNA*ORGA/(VA1-VA0))
    b0 = round(DNA*ORGA/(VA1-VA0))
    dat1(1:128,1:128)=0;
    dat2(1:128,1:128)=0;
    for and_a = a0:DNA-1
        for and_b = b0:DNB-1
            xx=round(RAM_X*(and_a+and_b)/(DNA+DNA));
            yy=round(RAM_Y*and_a/(and_a+and_b));
            
            % 原点を170,170として補正したバージョン
            and_a_ = and_a - a0;
            and_b_ = and_b - b0;
            if and_a_ + and_b_ > 0 & and_a_ > 0
                xx2 = round(RAM_X*(and_a_ + and_b_)/(DNA+DNA));        
                yy2 = round(RAM_Y*and_a_/(and_a_ + and_b_));
            else
                xx2 = 1;
                yy2 = 1;
            end
            tmp = dat1(xx, yy);
            tmp2 = dat2(xx2, yy2);
            %tmp = dat_(int16(RAM_X*(and_a+and_b)/(DNA+DNA)), int16(RAM_X*(and_a+and_b)/(DNA+DNA)));
            dat1(xx, yy) = tmp + dat(and_a, and_b);
            dat2(xx2, yy2) = tmp2 + dat(and_a, and_b);
        end
    end
   
    figure(1);
    subplot(2,2,1);
    surf(dat)
    view(0,90)
    colorbar
    title(tl);
    xlabel('A');
    ylabel('B');

    subplot(2,2,2);
    surf(dat1)
    view(0,90)
    colorbar
    title('位置検出');
    xlabel('A/A+B');
    ylabel('A+B');

    subplot(2,2,4);
    surf(dat2)
    view(0,90)
    colorbar
    title('位置検出');
    xlabel('A/A+B');
    ylabel('A+B');

    ans = input('Will you see next Ea?  1/0 (yes/no)');
    if ans ‾= 1 
       return
   end
end
