function wentropy_dyn(infile,prognozfile,entrfile);
% динамическая визуализация вейвлет-єнтропии (волн)

%infile='dj1900_20110320.txt';
infile='dj_29.txt';
datafile='dj_29_dt.txt';
Y=dlmread(infile);
D_Y=importdata(datafile);
Y=transpose(Y);
N=length(Y);
%if length(Y)>2000
%    Y=Y(length(Y)-2000:length(Y));
%    fprintf(1,'Time series are cutted: last 2000 points are used.\n');
%end
wtype=2;
wetype=1;
plotOn=0;
%nend=12200;
%nbeg=nend-1000;
N=300;
window=1998;%890;%200;
%figure;
%ax1=axes;
cp=[0.0680   -0.2144    3.6526]*1000;
cp1=[0.0680   -0.2144    3.6526]*1000;
if (1)
figure;
p1=subplot(3,1,1);
p2=subplot(3,1,2);
p3=subplot(3,1,3);
end
figure;
ax1=subplot(3,1,1);%axes;
ax2=subplot(3,1,2);%axes;
ax_y=subplot(3,1,3);
mastimes=[];
masmintimes=[];
masmaxtimes=[];
masmaxotnosh=[];
mascurotnosh=[];
mascuravgotnosh=[];
mast=[];
cpset=1;
maxscale=254;

for i=1999:2000%26800:27300%7449:7500%21970:22000%20160:1:20420%19597:5:20197%1400:1510%900:10:3300%19997:20097%:9100:19400%14606:10:15300
    nend=i*1;%+window;
    nbeg=i-window;%600;%19667;%9100;%i*1;
    Y1=Y(nbeg:nend);
    [SWT,POWER,Pj]=waventropy_stepen(Y1,wtype,wetype,0,1,maxscale,2,1);%80,200);%74,76);%244,246);
    axes(ax1);
    mesh(POWER);
    [SWT2,POWER2,Pj2]=waventropy_stepen(Y1,wtype,wetype,0,1,maxscale,3,1);%80,200);%74,76);%244,246);
    axes(ax2);
    mesh(POWER2);
    axes(ax_y);
    h=plot(Y1);
    xtick=get(gca,'XTick');
    xticklabel={};
    for kk=1:length(xtick)
        xticklabel{kk,1}=num2str(xtick(kk)+i-window);
    end
    set(gca,'XTickLabel',xticklabel);
    ff=xlabel(D_Y(i));
    set(ff,'FontWeight','Bold');

    [n,m]=size(POWER);
    if (1)
        axes(p1);
        hold off;
        plot(POWER(maxscale,:)); %bulo 70
        %mesh(POWER);
        axes(p2)
    end
    
    
    Y_w=max(POWER);%POWER(70,:);
    if (1)
        Y_w=POWER(maxscale,:);%Pj; %pered 200 bulo uroven 70
        hold off;
        plot(Y_w);
        
    end
    %figure;
    [y_cosinuses,rr,times, extrems]=serier2rr(Y_w,50,0);
    [last_peak_value0,last_peak_value1,last_peak_value2,avglastpeek,maxlastpeek]=get_last_peek_value(Y_w,times, extrems);
    last_value=Y_w(length(Y_w));
    ifirst_predict=find(times>length(Y_w));
    %ifirst_predict=11;%для неизменного слежения за конкретным экстремумом
    if ~isempty(ifirst_predict)
        ifirst_predict=ifirst_predict(1);
        ifirstmax=find(extrems(ifirst_predict:length(extrems))>0);
        %ifirstmax=1; %для неизменного слежения за конкретным экстремумом
        if ~isempty(ifirstmax)
            ifirstmax=ifirstmax(1);
            timefirst=times(ifirst_predict+ifirstmax-1);
            Razmah=max(rr)-min(rr);
        else
            timefirst=NaN;
            Razmah=inf;
        end
    else
        timefirst=NaN;
        Razmah=inf;
    end
    mastimes=[mastimes,timefirst+nbeg];
    masmintimes=[masmintimes,timefirst+nbeg-Razmah];
    masmaxtimes=[masmaxtimes,timefirst+nbeg+Razmah];
    mast=[mast,i];
    %otnosh_maxs=(last_peak_value2-last_peak_value1)/(last_peak_value1+last_peak_value2);
    otnosh_maxs=(last_peak_value2/last_peak_value1);%/(last_peak_value1+last_peak_value2);
    otnosh_maxs0=(last_peak_value1/last_peak_value0);%/(last_peak_value1+last_peak_value2);
    otnosh_last=(last_value/max(last_peak_value1,last_peak_value2));%/(last_value+max(last_peak_value1,last_peak_value2));
    %otnosh_max_avg=avglastpeek;
    otnosh_max_avg=maxlastpeek/avglastpeek;
    masmaxotnosh=[masmaxotnosh,otnosh_maxs];
    mascurotnosh=[mascurotnosh,otnosh_max_avg];
    mascuravgotnosh=[mascuravgotnosh,maxlastpeek];

    if otnosh_maxs>0
        info='crisis max';
        if otnosh_last>1
            info='crisis!!!!';
        end
    else
        if otnosh_last>1
            info='crisis last???';
        else
            info='ok';
        end
    end
    fp=fopen('log.txt','a');
    fprintf(fp,'t=%d\tTimeMax=%d, rozmah=%d, nmax=%d, otn_maxs=%d, otn_maxs0=%d, otn_last=%d, maxpeek=%d, info=%s\n',i-window,timefirst+nbeg,Razmah,ifirst_predict+ifirstmax-1,otnosh_maxs,otnosh_maxs0,otnosh_last,otnosh_max_avg,info);
    fprintf(1,'t=%d\tTimeMax=%d, rozmah=%d, nmax=%d, otn_maxs=%d, otn_maxs0=%d, otn_last=%d, maxpeek=%d, info=%s\n',i-window,timefirst+nbeg,Razmah,ifirst_predict+ifirstmax-1,otnosh_maxs,otnosh_maxs0,otnosh_last,otnosh_max_avg,info);
    fclose(fp);
    if (1)
        hold on;
        pp3=plot(y_cosinuses,'r');
        set(gca,'XTick',times);
        xtick2=times;
        xticklabel2={};
        for kk=1:length(xtick2)
            xticklabel2{kk,1}=num2str(xtick2(kk)+i-window);
        end
        set(gca,'XTickLabel',xticklabel2);
        axes(p3);
        hold off;
        plot(Y1);
        ff=xlabel(D_Y(i));
        set(ff,'FontWeight','Bold');
        set(gca,'XTickLabel',xticklabel);
    end
    set(ax1,'CameraPosition',cp);
    set(ax2,'CameraPosition',cp1);
    if (cpset)
        input('Выставьте угол обзора и нажмите ентер!');
        cpset=0;
    end
    savemeshfigure(ax1,nend-nbeg,nbeg,'fig.bmp');
    axes(p1);
    savemeshfigure(p1,0,nbeg,'figpredict.bmp');
    %input('press enter','s')
    %pause(1);
    %pause on;
    %input('Выставьте угол обзора и нажмите ентер!');
    cp=get(ax1,'CameraPosition');
    cp1=get(ax1,'CameraPosition');
end
figure;
plot(mastimes,'r');
hold on;
plot(masmintimes);
plot(masmaxtimes);
plot(mast,'k');
figure;
plot(masmaxotnosh);
title ('max otnosh');
figure;
plot(mascurotnosh);
title ('отношение мах к среднему');

figure;
plot(mascuravgotnosh);
title ('Max peek');

