clear;
%ser = input('Please input series number:  ');
%fname = ['../cal/', ser, '.txt'];

%2nd Integration test
fname = '../../mmo130514/dat/MEA2.lm';

dat= dlmread(fname, ' ');
n = size(dat);
i = 1;
k1 = 1;
k2 = 1;
k3 = 1;
km1 = 1;
km2 = 1;
km3 = 1;
in = 1;
ti = 0;
for i = 1:n(1)
    %MDPの時刻
    if dat(i, 2) == 9 & dat(i, 3) == 9 & dat(i,4) == 9 & dat(i,5) == 9
        mdpti = dat(i,1);
    elseif dat(i, 5) == 1111
        ti = dat(i,1);
    end
    if dat(i, 1) == 9999
        dat1(k1, 1) = ti;
        dat1(k1, 2:13*6+1) = dat(i,2:13*6+1);
        k1 = k1 + 1;
    end
    if dat(i, 1) == 8888
        dat2(k2, 1) = ti;
        dat2(k2, 2:64+1) = dat(i,2:64+1);
         k2 = k2 + 1;
    end
    if dat(i, 1) == 7777
        dat3(k2, 1) = ti;
        dat3(k2, 2:16+1) = dat(i,2:16+1);
         k3 = k3 + 1;
    end
    
    if dat(i, 1) == 331
        dam1(km1, 1) = ti
        dam1(km1, 2:13*6+1) = dat(i,2:13*6+1);
        km1 = km1 + 1;
    elseif dat(i, 1) == 332
        in = 2;
        dam1(km1, 1) = ti;
        dam1(km1, 2:13*6*2+1) = dat(i,2:13*6*2+1);
        km1 = km1 + 1;
    elseif dat(i, 1) == 221
        dam2(km2, 1) = ti;
        dam2(km2, 2:64+1) = dat(i,2:64+1);
        km2 = km2 + 1;
    elseif dat(i, 1) == 222
        dam2(km2, 1) = ti;
        dam2(km2, 2:64*2+1) = dat(i,2:64*2+1);
        km2 = km2 + 1;
    elseif dat(i, 1) == 111
        dam3(km3, 1) = ti;
        dam3(km3, 2:16+1) = dat(i,2:16+1);
         km3 = km3 + 1;
    elseif dat(i, 1) == 112
        dam3(km3, 1) = ti;
        dam3(km3, 2:16*2+1) = dat(i,2:16*2+1);
        km3 = km3 + 1;
    end
end

figure(1);
subplot(4,1,1);
plot(dat1(:,1), dat1(:,2), 'rs','MarkerSize',4);
grid on;
%title('g-factor');
xlabel('MEAtime');
ylabel('VM(1)');

subplot(4,1,2);
plot(dat1(:,1), dat1(:,3), 'rs','MarkerSize',4);
grid on;
xlabel('MEAtime');
ylabel('VM(2-4)');
hold on
plot(dat1(:,1), dat1(:,4), 'bs','MarkerSize',4);
plot(dat1(:,1), dat1(:,5), 'gs','MarkerSize',4);
hold off

subplot(4,1,3);
plot(dat1(:,1), dat1(:,6:11), 's','MarkerSize',4);
grid on;
xlabel('MEAtime');
ylabel('VM(5-10)');

subplot(4,1,4);
plot(dat1(:,1), dat1(:,12), 'rs','MarkerSize',4);
grid on;
xlabel('MEAtime');
ylabel('VM(11-13)');
hold on
plot(dat1(:,1), dat1(:,13), 'bs','MarkerSize',4);
plot(dat1(:,1), dat1(:,14), 'gs','MarkerSize',4);
hold off

figure(2);
surf(dat2(:,1), [1:64], dat2(:, 2:64+1)')
%view(0,90)
xlabel('MEAtime');
ylabel('ET-PAP');

figure(3);
surf(dat3(:,1), [1:16], dat3(:,2:16+1)')
%view(0,90)
%plot(dat3(:,1), dat3(:,2:16+1),'s','MarkerSize',4);
xlim([min(dat1(:,1)) max(dat1(:,1))]);
xlabel('MEAtime');
ylabel('ET-PAP');

figure(11);
subplot(4,1,1);
plot(dam1(:,1), dam1(:,2), 'rs','MarkerSize',4);
grid on;
%title('g-factor');
xlabel('MEAtime');
ylabel('VM(1)');
xlim([min(dat1(:,1)) max(dat1(:,1))]);

subplot(4,1,2);
plot(dam1(:,1), dam1(:,3), 'rs','MarkerSize',4);
xlim([min(dat1(:,1)) max(dat1(:,1))]);
grid on;
xlabel('MEAtime');
ylabel('VM(2-4)');
hold on
plot(dam1(:,1), dam1(:,4), 'bs','MarkerSize',4);
plot(dam1(:,1), dam1(:,5), 'gs','MarkerSize',4);
hold off

subplot(4,1,3);
plot(dam1(:,1), dam1(:,6:11), 's','MarkerSize',4);
xlim([min(dat1(:,1)) max(dat1(:,1))]);
grid on;
xlabel('MEAtime');
ylabel('VM(5-10)');

subplot(4,1,4);
plot(dam1(:,1), dam1(:,12), 'rs','MarkerSize',4);
xlim([min(dat1(:,1)) max(dat1(:,1))]);
grid on;
xlabel('MEAtime');
ylabel('VM(11-13)');
hold on
plot(dam1(:,1), dam1(:,13), 'bs','MarkerSize',4);
plot(dam1(:,1), dam1(:,14), 'gs','MarkerSize',4);
hold off

figure(12);
surf(dam2(:,1), [1:64*in], dam2(:, 2:64*in+1)')
%view(0,90)
xlabel('MEAtime');
ylabel('ET-PAP');

figure(14);
surf(dam3(:,1), [1:16*in], dam3(:,2:16*in+1)')
%view(0,90)
%plot(dat3(:,1), dat3(:,2:16+1),'s','MarkerSize',4);
xlim([min(dat1(:,1)) max(dat1(:,1))]);
xlabel('MEAtime');
ylabel('ET-PAP');
