clear;

%ser = input('Please input series number:  ');

%drc = ['../../exp_IMAEAfm/check_ea/out_', ser, '/'];
k = 1;
drc = '../out_090909/';
if k == 1
ser(2,:) = 'ch01u00d85';
ser(3,:) = 'ch01u00d27';
ser(1,:) = 'ch01u00d00';
ser(4,:) = 'ch01u27d00';
ser(5,:) = 'ch01u85d00';
elseif k == 2
ser(2,:) = 'ch08u00d85';
ser(3,:) = 'ch08u00d27';
ser(1,:) = 'ch08u00d00';
ser(4,:) = 'ch08u27d00';
ser(5,:) = 'ch08u85d00';
elseif k == 3
ser(2,:) = 'ch16u00d85';
ser(3,:) = 'ch16u00d27';
ser(1,:) = 'ch16u00d00';
ser(4,:) = 'ch16u27d00';
ser(5,:) = 'ch16u85d00';

end
tit(2,:) = 'Vup/|Vin| =  0.85';
tit(3,:) = 'Vup/|Vin| =  0.24';
tit(1,:) = 'Vup = Vlw = GND  ';
tit(4,:) = 'Vlw/|Vin| =  0.27';
tit(5,:) = 'Vlw/|Vin| =  0.85';

fnm = dir(drc);
[num, nn] = size(fnm);

%drcc = ['../check_ea/out_', ser, '/VX000*'];
%drcc = ['../../exp_IMAEAfm/check_ea/out_', ser, '/VX000*'];
%drcc = ['../../exp_ESA_S1fm/check_ea/out_', ser, '/VX300*'];
%drcc = ['../../exp_ESA_S2fm/check_ea/out_', ser, '/VX030*'];
%fnm = dir(drcc);
%[num, nn] = size(fnm);



v = [1 2 3 4 5 6 7 8 9 10]*170; %[200,200,170]
%v = [100 1200];
figure(k)
subplot(2,1,1);
xlabel('Alpha (deg)');
ylabel('Energy(%)');
ylim([-15 15]);
xlim([-25 25]);
grid on;
hold on

subplot(2,1,2);
xlabel('Alpha (deg)');
ylabel('Count');
ylim([0 2000]);
xlim([-25 25]);
grid on;
hold on

for i = 1:5
%for i = 1:2:num-1
    dat_fnm = [drc, ser(i,:), '_dat']
    lbl_fnm = [drc, ser(i,:), '_lbl']
    tl = dlmread(lbl_fnm, ' ', [0 0 2 2]);
    G_FCTR = tl(2,1);
    NOTHNG = tl(2,2);
    AVE_E =  tl(2,3);
    [ENERGY, ALPHA, SECTOR] = textread(dat_fnm,'%f %f %f', 1);

    % TATE YOKO
    range=[1 0 ENERGY 1];
    ee = dlmread(dat_fnm, ' ', range);
    range=[ENERGY+1 0 ENERGY+ALPHA 1];
    aa = dlmread(dat_fnm, ' ', range);
    range=[ENERGY+ALPHA+1 0 ENERGY+ALPHA+SECTOR 1];
    pp = dlmread(dat_fnm, ' ', range);
    range=[ENERGY+ALPHA+SECTOR+1 0 2*ENERGY+ALPHA+SECTOR ALPHA-1];
    ea = dlmread(dat_fnm, ' ', range);

    delt_a(1) = abs(aa(2,1) - aa(1,1));
    delt_a(ALPHA) = abs(aa(ALPHA,1) - aa(ALPHA-1,1));
    for j = 2:ALPHA-1
        delt_a(j) = abs(delt_a(j+1) - delt_a(j-1))/2.;
    end
    sum_a = 0.;
    sum_d = 0.;
    for j = 1:ALPHA
        sum_a = sum_a + aa(j,2)*aa(j,1)*delt_a(j);
        sum_d = sum_d + aa(j,2)*delt_a(j);
    end
    ave_a = sum_a/sum_d;
        if i == 1
        NORM_EA1 = G_FCTR*AVE_E/sum(sum(ea));
        NORM_A1 = G_FCTR/sum(aa(:,2));
        MEAN_E = AVE_E;
    end
    NORM_EA = G_FCTR*AVE_E/sum(sum(ea));
    NORM_A = G_FCTR/sum(aa(:,2));
    
    subplot(2,1,1);
    contour(aa(:,1), (ee(:,1)-MEAN_E)*100/MEAN_E, ea*NORM_EA/NORM_EA1, v);
   
    subplot(2,1,2);
    plot(aa(:,1), aa(:,2)*NORM_A/NORM_A1, 'r-','MarkerSize',4);
    %MCPの半径の違いを補正
    %plot(pp(:,1)-90, pp(:,2), '-rs','MarkerSize',4);
    %plot(pp(:,1)*31.75/50-20, pp(:,2), '-rs','MarkerSize',4);
    %grid on;
    %title(tit(i,:));
    %xlim([40 140]);
    %xlabel('Sector');
    %ylabel('Counts');

end

