 % use FFT with sliding gaussian or cosine window to produce spectrogram within a minute
 % analyze cross-loop antennas
function detailsp

clear all
global aH xxx fmax_disp h seltrace maxtraces fmax1d fmax2d fmax3d fmax4d fmax5d isfin

%Enter type of receiver, 0-stary, 1-novy (X), 2-novy (Y), 3-novy s pameti,
%4..cross-loop
prijimac=4;  % for South Africa should be always 3, the third generation
             % 4..dual receiver with cross-loop antennas
% froffset=80; 
% numsss=3; % number of subplots
             
minutaposun=0;
if prijimac==3
   minutaposun=1; % compensates one minute shift for some receivers of type 3
end
% Enter frequency parameter 
HF = 1; %3.59 MHz
%HF = 2; % 4.268 MHz        
%HF = 0; % 7 MHz
dalsidny=0;  %0, don't change, will be controlled by software in multidays processing


fid=fopen('S:\INFRA\INFRA.OBR\Sopron\detail_ini2.txt');
i=0; 
while ~feof(fid);
   i=i+1;
   line=fgets(fid);  
   if i==3
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       rokH=line(1:sem(1)-jj)
       roki=str2num(rokH);
   end
   if i==4
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       mesicH=line(1:sem(1)-jj)
       mesici=str2num(mesicH);
   end
   if i==5
       sem=find(line==';');
       sem1=find(line=='-');
       odj=1;
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       if ~isempty(sem1) % two parameters
         jj=1;
         while line(sem1(1)-jj)==' '
            jj=jj+1
         end
         denH=line(1:sem1(1)-jj)
         deni0=str2num(denH);
         jj+1;
         while line(sem1(1)+jj)==' '
             jj=jj+1;
         end
         odj=sem1(1)+jj;
       end  
       jj=1;
       while line(sem(1)-jj)==' '
            jj=jj+1
       end
       denH=line(odj:sem(1)-jj)
       deni=str2num(denH);
       if isempty(sem1)
           deni0=deni;
       end
   end
   if i==6
       sem=find(line==';');
       sem1=find(line=='-');
       odj=1;
       if ~isempty(sem1) % two parameters
         jj=1;
         while line(sem1(1)-jj)==' '
            jj=jj+1
         end
         hodinaH=line(1:sem1(1)-jj)
         hodinai0=str2num(hodinaH);
         jj+1;
         while line(sem1(1)+jj)==' '
             jj=jj+1;
         end
         odj=sem1(1)+jj;
       end  
       jj=1;
       while line(sem(1)-jj)==' '
            jj=jj+1
       end
       hodinaH=line(odj:sem(1)-jj)
       hodinai=str2num(hodinaH);
       if isempty(sem1)
           hodinai0=hodinai;
       end
   end
   if i==7
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       delkaH=line(1:sem(1)-jj)
       delkai=str2num(delkaH);
   end
   if i==8
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       numdH=line(1:sem(1)-jj)
       numd=str2num(numdH);
       minpi=15*floor(numd/15+0.01);
       pocmini=mod(numd,15);
   end
   if i==9
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       cestazdroj=line(1:sem(1)-jj)
   end
   if i==10
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       cestaprint=line(1:sem(1)-jj)
   end
   if i==11
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       cminH=line(1:sem(1)-jj)
       cmin=str2num(cminH);
   end
   if i==12
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       cmaxH=line(1:sem(1)-jj)
       cmax=str2num(cmaxH);
   end
   if i==13
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       fityH=line(1:sem(1)-jj)
       fity=str2num(fityH);
   end
   if i==14
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       pominuteH=line(1:sem(1)-jj)
       pominute=str2num(pominuteH);
   end
   if i==15
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       krokH=line(1:sem(1)-jj)
       krok=str2num(krokH);
   end
   if i==16
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       yminH=line(1:sem(1)-jj)
       yDmin=str2num(yminH);
   end
   if i==17
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       ymaxH=line(1:sem(1)-jj)
       yDmax=str2num(ymaxH);
   end
   if i==18
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       ythres=line(1:sem(1)-jj)
       fdolhz=str2num(ythres);
   end
   if i==19
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       ythres=line(1:sem(1)-jj)
       fdolhzm1=str2num(ythres);
   end
   if i==20
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       ythres=line(1:sem(1)-jj)
       fhorhzm1=str2num(ythres);
   end
   if i==21
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       ythres=line(1:sem(1)-jj)
       fhorhzm2=str2num(ythres);
   end
   if i==22
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       ythres=line(1:sem(1)-jj)
       fremoveHz=str2num(ythres);
   end 
   if i==23
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       amplitudyH=line(1:sem(1)-jj)
       amplitudy=str2num(amplitudyH);
   end
   if i==24
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       maxtracesH=line(1:sem(1)-jj)
       maxtraces=str2num(maxtracesH);
   end
   if i==25
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       hUTH=line(1:sem(1)-jj)
       hUT=str2num(hUTH);
   end
   if i==26
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       froffsetH=line(1:sem(1)-jj)
       froffset=str2num(froffsetH);
   end
   if i==27
       sem=find(line==';');
       jj=1;
       while line(sem(1)-jj)==' '
           jj=jj+1
       end
       numsssH=line(1:sem(1)-jj)
       numsss=str2num(numsssH);
   end  
   
end
statustext=fclose(fid);

   fdolhz=fdolhz+froffset;
   fdolhzm1=fdolhzm1+froffset;
   fhorhzm1=fhorhzm1+froffset;
   fhorhzm2=fhorhzm2+froffset;

isfin=0;
if fity==2
    isfin=1;
end
if prijimac==0
    cestazdroj='V:\preproc\'  
end
% if prijimac==2
%     cestazdroj='V:\infra.rec\2011\2011.03\' %'I:\infra.new\'   
% end
if prijimac==1
    cestazdroj='S:\kvetna\2008.11\' %'I:\infra.new\'   
end

% if prijimac==3
%     cestazdroj='C:\Doppler_Tucuman\Tucuman8\';     % the data are read from here
%     %cestazdroj='V:\Doppler_data3\Praha\2012.11\'
%        % Since 29-10-2012 Praha.3 for 4.27 MHz
%        %                  Praha   for 3.59 MHz
% end
% 
% cestaprint='C:\infra\infradetailed\Tucuman8\' % the path to store results (figures)
% %  if prijimac==3
% %    cestaprint='C:\infra\infradetailed\Hermanus\'
% %    %cestaprint='G:\Hermanus\test2\' % smery\'
% %  end

tiskni=1; %not used  %  1.. to store results, 0..otherwise
WT=0; % 1 to compute wavelet transform, 0..not to comnpute WT 
%selforWT=1; %select path for WT and special analysis

sumuj=0;  % sum WTscalograms to obtain average periods over several days, used by WT_FFT_AGW
          % should be 0 for individula detailed spectrograms
          
dodmin=4*16; % define number of spectral lines which can be different from 1 minute maximum when searching the maximum  
seltrace=1; % 0.. save the fit as mass center point for all traces, 
            % 1-5 ..save the fit as mass center point for selected trace,
%maxtraces=3; % max number of traces typically 3..5            
%pominute=2; % 2..two values per minute derived from minute average, 
            % 1..one value per minute
            % 0..1/krok values per minute derived from fine time resolution
%amplitudy=0; % save amplitudes and Doppler shift for Tx1, in former version fity should be 1 !!!!            
%krok=0.5; % krok in fraction of minute to fit data (recomended 0.1-0.5)
%fity=1; % 1.. save fits

%Enter the initial date and time
steph=floor(delkai/60)
if steph==0
    steph=1;
end
rok=roki; % year
mesic=mesici; % month
for den=deni0:deni;%11 % from day to day, should be the same days for a single spectrogram, deni
for hodina=hodinai0:steph:hodinai; % hour; hodinai
minp=minpi; %initial minute, should only be 45 30 15 0; % 
delka=1/4; % 1/4 don't  change
pocmin=pocmini; %0 initial minute in a file of 15 minutes, has to be 0 if the file is not complete !!!

pocetmin=delkai; % the length of spectrogram in minutes

if fity<2
    isfin=0;
end
seltrace=1;

% the parametrs for FFT - the length of the time window can be changed 
% on line 155 , polosirkatime=

%definuj zakladni parametry
pocetfilu=round(delka*4);
pocetFFT=15;
pocetmin1=pocetmin;
pocetminr=pocetmin;
if (pocetminr+pocmin)<=pocetFFT
    pocetmin2=pocetminr+pocmin;
end    
while (pocetminr+pocmin) >= pocetFFT
    if pocetfilu==1 
        pocetmin1=pocetFFT-pocmin; 
    end;
    pocetfilu=pocetfilu+1;
    pocetminr=pocetminr-pocetFFT;
    pocetmin2=pocetminr+pocmin;
end;
pocetvzorku=32768;
fsample=610.35;
if (prijimac==1)|(prijimac==2)
    pocetvzorku=32768/2;
    fsample=315.0769;
end
if prijimac>=3
  pocetvzorku=32768/2;
  fsample=305.1758;  
end

hodina0=hodina+minp/60;

Sdelkapom=num2str(100+delka,'%3.0f');
Sdelka=Sdelkapom(2:3);

zpracovano=datenum(rok,mesic,den);
nyni=datenum(rok,mesic,den) +hodina/24+ (minp+15)/24/60 % +3/24+30/24/60;
%nyni=now;

%ferr = fopen('I:\infra\infraobrprehled\notopened.err','a+');
%ferr = fopen('D:\preproc\notopened.err','a+');
ferr = fopen([cestaprint 'notopened.err'],'a+');

while nyni > zpracovano

%generuj nazvy filu
if prijimac==0
    gennazvyfilu
end
if prijimac>=1
    gennazvyfilu_n
end

zpracovano=datenum(rok,mesic,den)+hodina/24+minp/24/60;
    
% fdolhz=74; %74   73.5; %73.5 77.5; 68
% fdolhzm1=78;  %77.5;
% fhorhzm1=82; %82.5; %82.5
% fhorhzm2=86; %86.5;
% fhorhz=90; %90.5; %90.5 82.5 + 4; 92

fdolhz5=froffset-15; %70 - 5 vysilacu
% fdolhz=72.5; %74 -4 vysilace; 
% fdolhzm1=78;  %78 77.5;
% fhorhzm1=82; %82 82.5; %82.5
% fhorhzm2=86; %86 86.5;
fhorhz=froffset+15; %90; %90.5 82.5 + 4; 92

fdol5=floor(fdolhz5*pocetvzorku/fsample+1);
fdol=floor(fdolhz*pocetvzorku/fsample+1);
fdolm1=floor(fdolhzm1*pocetvzorku/fsample+1);
fhorm1=floor(fhorhzm1*pocetvzorku/fsample+1);
fhorm2=floor(fhorhzm2*pocetvzorku/fsample+1);
fhor=floor(fhorhz*pocetvzorku/fsample+1);


offset2=0.056;
offset1=0.112;
%od 22.3.2004
offset2=0.1118;
offset1=0.2608;
%od 1.11.2004
offset2=0.1304;
offset1=0.2608;
%od 18.3.2005
shift2_2=4;
offset2_2=-0.163;
%od 1.3.2006 ??
%shift2_2=4;
%offset2_2=-0.2189;

%od 20.11.2006 - 3 vysilace
shift2_2=4;
offset2=-0.163;
offset2_2=-0.0559;   %-0.2608;
offset2_3=-0.447;
% 4 vysilace
shift2_4=8;
offset2_4=0;
offset2_2=0.1676;
% 5 vysilacu
shift2_5=-8;
offset2_5=0;

offset2_2=4.117-4.0; % 3 Mhz
offset2_2=4.08-4.0; % 4 Mhz

%fstred2_1=round((fdol+fhorm1)/2+offset2*pocetvzorku/fsample); % 2 vysilace
fstred2_1=round((fdolm1+fhorm1)/2+offset2*pocetvzorku/fsample); % 3 vysilace
fstred2_2=round((fhorm1+fhorm2)/2+(offset2_2)*pocetvzorku/fsample);
fstred2_3=round((fdol+fhorm1)/2+(offset2_3)*pocetvzorku/fsample);
fstred2_4=round((fhorm2+fhor)/2+(offset2_4)*pocetvzorku/fsample);
fstred1=round((fdol+fhor)/2+offset1*pocetvzorku/fsample);
polosirka=0.9*(fhor-fdol)/2;

fremove=round((fremoveHz+froffset)*pocetvzorku/fsample);

polosirkatime=8192; % nebo 8192 4096 2048 1024 512
if prijimac>=1
    polosirkatime=polosirkatime/2;
end
if ((pocetvzorku/polosirkatime*pocetmin)>800)   
   slide=floor(2*polosirkatime/4); 
else    
   slide=floor(2*polosirkatime/8); %8
end;   
%slide=polosirkatime/2;

%polosirkafr=round(pocetvzorku/polosirkatime/5);
polosirkafr=4; %2
okoli=12; %12 %/8;  % 6 64 95 40
%bodu=64;

zjemneni=round(pocetvzorku/slide);
if amplitudy==2
    zjemneni=1;
end

%define size to compute faster
vstup1(pocetvzorku)=0;
vstup2(pocetvzorku)=0;
amplitspectr_s2(zjemneni,pocetvzorku)=0;
ALPd(zjemneni,pocetvzorku)=0;
DCPd(zjemneni,pocetvzorku)=0;
DPd(zjemneni,pocetvzorku)=0;


pocitejminuty=0;

frmaxtot2_1=[];
sigmaftot2_1=[];
frmaxtot2_2=[];
sigmaftot2_2=[];
frmaxtot2_3=[];
sigmaftot2_3=[];
vlfrmaxtot=[];
pausapixtot=0;

frmaxw1tot=[];
frmaxw2tot=[];
frmaxw3tot=[];
frmaxw4tot=[];
frmaxw5tot=[];

priznaktot=[];
RMSftot=[];

fmax_1tot=[];
fmax_2tot=[];
fmax_3tot=[];
fmax_4tot=[];
fmax_5tot=[];

pomer_1tot=[];
pomer_2tot=[];
pomer_3tot=[];
pomer_4tot=[];
pomer_5tot=[];

color2=[]; colorALP=[]; colorDCP=[]; colorDP=[];

frmax1tot=[];
sigmaftot=[];
RMS1tot=[]; RMS2tot=[]; RMS3tot=[]; RMS4tot=[]; RMS5tot=[];
RMSbw1tot=[]; RMS2bwtot=[]; RMS3bwtot=[]; RMS4bwtot=[]; RMS5bwtot=[];
ftez1tot=[];


clear priznak1 priznak2 priznak3 priznak4 priznak5 priznak RMSf;

%  repeat reading files and calculate
for cislfilu=1:pocetfilu
cislfilu
    
% otervri soubor a najdi pozadovanou minutu
spravnadelkafilu=-1;    
if HF>=1 
    %[cestazdroj sjmenofilub(cislfilu,:)]
    fidb = fopen([cestazdroj sjmenofilub(cislfilu,:)],'r');
end
if HF==0 
    fidb = fopen([cestazdroj sjmenofilua(cislfilu,:)],'r');
end
if fidb ~= -1 % file has been opened sucesfully  
spravnadelkafilu=1;    
for dataset=1:1 % pocetFFT to check 15 minutes, shorter if less minutes is OK (for receiver 3)
  if prijimac==0  
    vstupf1= fread(fidb,2*pocetvzorku,'uint8');
    Nf=length(vstupf1);
    N = floor(Nf/2)
    if N~=pocetvzorku % file has the correct length
      spravnadelkafilu=-1;
    end;
  end;
  if prijimac>=1
   if prijimac==4
      hlavicka=fread(fidb,128,'uchar');
      vstupf1= fread(fidb,8*pocetvzorku,'uint8'); 
      Nf=length(vstupf1);
      N = floor(Nf/8) %/2  
      if (N~=pocetvzorku) % file has not the correct length
       spravnadelkafilu=-1;
      end;
   else
    if prijimac==3
      hlavicka=fread(fidb,64,'uchar');
      vstupf1= fread(fidb,4*pocetvzorku,'uint8');
    else
      hlavicka=fread(fidb,4,'uchar');
      vstupf1= fread(fidb,4*pocetvzorku,'uint8');
      konec=fread(fidb,4,'uchar');
    end
    Nf=length(vstupf1);
    N = floor(Nf/4) %/2  
    if (N~=pocetvzorku)&(prijimac<3) % file has not the correct length
       spravnadelkafilu=-1;
    end;
    
   end 
  end 
end; 

statusa =fclose(fidb);    
%end %fid 
else %file was not opened or found
  %fprintf(ferr,[sjmenofilun(cislfilu,:) '\n']);
  'file was not opened or find'
end;

if spravnadelkafilu==1 & fidb ~= -1

t=0:pocetvzorku-1;
f=1:(pocetvzorku/2);
x=1:pocetvzorku;
xdf=1:floor(pocetvzorku/2);
xhf=floor(pocetvzorku/2)+1:pocetvzorku;

okenko=x./x; %rectangular

%okenkofrd=exp(-(xdf-fstred2).^2/2/polosirka^2);
%okenkofrh=exp(-(xhf-(N-fstred2)+2).^2/2/polosirka^2);

  % zde byly definice prazdnych poli, debugging

if HF>=1
    fidb = fopen([cestazdroj sjmenofilub(cislfilu,:)],'r');
end
if HF==0
    fidb = fopen([cestazdroj sjmenofilua(cislfilu,:)],'r');
end

if cislfilu==1 
  for dataset=1:pocmin    
    if prijimac==0
      vstupf2= fread(fidb,2*pocetvzorku,'uint8');
    end;
    if prijimac>=1
     if prijimac==4
       hlavicka=fread(fidb,128,'uchar');
       vstupf1= fread(fidb,8*pocetvzorku,'uint8');   
     else
      if prijimac==3
        hlavicka=fread(fidb,64,'uchar');
        vstupf1= fread(fidb,4*pocetvzorku,'uint8');
      else
        hlavicka=fread(fidb,4,'uchar');      
        vstupf= fread(fidb,4*pocetvzorku,'uint8');
        konec=fread(fidb,4,'uchar');
      end
      
     end 
    end
  end;
  %pocetminfile=pocetmin1 % bylo puvodne pred casem v datech
  pocetminfile=pocetmin1+pocmin;
  pocminr=pocmin+1;
end;
if (cislfilu > 1)&(cislfilu < pocetfilu) 
   pocetminfile=15;
   pocminr=1;
end;    
if (cislfilu == pocetfilu)&(cislfilu~=1)
   pocetminfile=pocetmin2;
   pocminr=1;
end;      
 
for dataset=pocminr:pocetminfile  %dataset=1:pocetminfile % bylo puvodne pred casem v datech
    
  minutaok=1;
  pocitejminuty=pocitejminuty+1;    
  
  if prijimac==0
    vstupf2= fread(fidb,2*pocetvzorku,'uint8');
    Nf=length(vstupf2);
    N = floor(Nf/2)
    for i=1:2:Nf;
      j=floor((i+2)/2);  
      if mod(i,2) == 1 
         vstup2(j) = vstupf2(i)+256*vstupf2(i+1);      
      end;
    end; 
  end;
  if (prijimac==1)|(prijimac==2)
    hlavicka=fread(fidb,4,'uchar')   
    vstupf= fread(fidb,4*pocetvzorku,'uint8');
    konec=fread(fidb,4,'uchar')
    Nf=length(vstupf);
    N = floor(Nf/4)
    for i=1:4:Nf; %4
      j=floor((i+4)/4); %4  
      %if mod(i,4) == 1 %4
         vstup1(j) = 256*vstupf(i)+vstupf(i+1); 
         vstup2(j) = 256*vstupf(i+2)+vstupf(i+3); 
      %end;
    end; 
  end
  if prijimac==3
    if minutaok==1  
       hlavicka=fread(fidb,64,'uchar');
       if ~isempty(hlavicka)
           IDnumber=hlavicka(9);
           Smin=hlavicka(19);
       else
           minutaok=0;
       end
    end
    if ((IDnumber==1)|(IDnumber==2)|(IDnumber))& (mod(Smin,15)==dataset-1)&(minutaok==1) % check ID and minute in the header
      minutaok=1;  
      starttitle=char(hlavicka(57:64))';
      frec=3.59;
      firmver=char(hlavicka(7:8))'
      if strcmp(firmver,'RX')
          minutaposun=1;
      else
          minutaposun=0;
      end;
      minutaposun
      if ~((strcmp(starttitle,'Hermanus'))|(strcmp(starttitle,'  Prague')))
             lat=(256^3*hlavicka(57)+256^2*hlavicka(58)+256*hlavicka(59)+hlavicka(60));
             if hlavicka(57)>=128
               lat=lat-256^4;
             end  
             lat=lat/1e7;
             long=(256^3*hlavicka(61)+256^2*hlavicka(62)+256*hlavicka(63)+hlavicka(64));
             if hlavicka(61)>=128
                 long=long-256^4;
             end
             long=long/1e7;
             Fmul=floor(hlavicka(38)/8);
             Fdiv=256^3*hlavicka(39)+256^2*hlavicka(40)+256*hlavicka(41)+hlavicka(42);
             frec=round(Fdiv*Fmul*20000000/2^32/4)/1e6;
                starttitle=['f=' num2str(frec,'%3.2f') ' MHz;  lat=' num2str(lat,'%5.2f') '; long=' num2str(long,'%5.2f')];      
      end
      vstupf= fread(fidb,4*pocetvzorku,'uint8');
      Nf=length(vstupf);
      N = floor(Nf/4);
      for i=1:4:Nf; %4
        j=floor((i+4)/4); %4  
         vstup1(j) = 256*vstupf(i+3)+vstupf(i+2); 
         vstup2(j) = 256*vstupf(i+1)+vstupf(i); 
      end;  
      %vstup1=vstup1-mean(vstup1);
      %vstup2=vstup2-mean(vstup2);
    else  % a minute is missing in the file
        minutaok=0;
        for j=1:pocetvzorku
            vstup1(j)=0;
            vstup2(j)=0;
        end
    end  
    
  end

  if prijimac==4
     if minutaok==1  
       hlavicka=fread(fidb,128,'uchar');
       if ~isempty(hlavicka)
           IDnumber=hlavicka(9);
           Syear=2000+10*bitand(hlavicka(14),240)/16+bitand(hlavicka(14),15);
           Smonth=10*bitand(hlavicka(15),240)/16+bitand(hlavicka(15),15);
           Sday=10*bitand(hlavicka(16),240)/16+bitand(hlavicka(16),15);
           Shour=10*bitand(hlavicka(17),240)/16+bitand(hlavicka(17),15);
           Smin=10*bitand(hlavicka(18),240)/16+bitand(hlavicka(18),15);
       else
           minutaok=0;
       end
     end 
     if(mod(Smin,15)==dataset-1)&(minutaok==1) % check ID and minute in the header
       minutaok=1;
             lat1=10*bitand(hlavicka(31),240)/16+bitand(hlavicka(31),15);
             lat2=10*bitand(hlavicka(32),240)/16+bitand(hlavicka(32),15);
             lat3=10*bitand(hlavicka(33),240)/16+bitand(hlavicka(33),15);
             lat4=10*bitand(hlavicka(34),240)/16+bitand(hlavicka(34),15);
             lat5=10*bitand(hlavicka(35),240)/16+bitand(hlavicka(35),15);
       lat=lat1+lat2/100+lat3/1e4+lat4/1e6+lat5/1e8;
             if hlavicka(31)>=128
               lat=-lat;
             end  
             %lat=lat/1e7;
             %long=(256^3*hlavicka(61)+256^2*hlavicka(62)+256*hlavicka(63)+hlavicka(64));
             lon1=10*bitand(hlavicka(36),240)/16+bitand(hlavicka(36),15);
             lon2=10*bitand(hlavicka(37),240)/16+bitand(hlavicka(37),15);
             lon3=10*bitand(hlavicka(38),240)/16+bitand(hlavicka(38),15);
             lon4=10*bitand(hlavicka(39),240)/16+bitand(hlavicka(39),15);
             lon5=10*bitand(hlavicka(40),240)/16+bitand(hlavicka(40),15);
             lon6=10*bitand(hlavicka(41),240)/16+bitand(hlavicka(41),15);
       long=100*lon1+lon2+lon3/100+lon4/1e4+lon5/1e6+lon6/1e8;
             if hlavicka(36)>=128
               long=-long;
             end  
             %long=long/1e7;
             Fmul=floor(hlavicka(42)/8);
             Fdiv=256^3*hlavicka(46)+256^2*hlavicka(45)+256*hlavicka(44)+hlavicka(43);
        frec=round(Fdiv*Fmul*20000000/2^32/4)/1e6;
        starttitle=['f=' num2str(frec,'%3.2f') ' Hz;  lat=' num2str(lat,'%5.2f') '; long=' num2str(long,'%5.2f')]; 

        vstupf= fread(fidb,8*pocetvzorku,'uint8');
        Nf=length(vstupf);
        N = floor(Nf/8);
        for i=1:8:8*pocetvzorku-1; %4  
         j=floor((i+8)/8); %4  
         vstup1(j) = 256*vstupf(i+3)+vstupf(i+2); 
         vstup2(j) = 256*vstupf(i+1)+vstupf(i+0); 
         vstup3(j) = 256*vstupf(i+7)+vstupf(i+6); 
         vstup4(j) = 256*vstupf(i+5)+vstupf(i+4); 
       end; 
       
     else % a minute is missing in the file
        minutaok=0;
        for j=1:pocetvzorku
            vstup1(j)=0;
            vstup2(j)=0;
        end
     end
  end
  
%for dataset=minuta:minuta %pocetFFT  

%N=pocetvzorku;  

%simulace
%yproc1=100*cos(2*pi*froffset.5/fsample*(1:pocetvzorku))+froffset*cos(2*pi*froffset.4/fsample*(1:pocetvzorku));
fmf=0.03;
yproc1=1000*cos(2*pi*81/fsample.*(1:pocetvzorku) + 0.1/fmf*cos(2*pi*fmf/fsample.*(1:pocetvzorku)));
%% POZOR !!!!! v pripade obecne modulacni funkce, je ji nutno integrovat pro kazdy cas t, nebot v argument cosinu je okamzita faze v kazdem case

% nacti spocetny integral modulacnui funcke
% vstupKrasnov;
% yproc1=700*cos(2*pi*71.5/fsample.*(1:pocetvzorku) + yIKi);

% if dataset==3
%     vstup2=vstup2+yproc1;
% end    
%vstup2=yproc1;

%realny signal

if prijimac==0
  vstupj=vstup2.*okenko;
end;
if (prijimac>=1)&(prijimac<=3)
  spectr1=fft(vstup1)/N*2;
 %powerspectr1=spectr1.*conj(spectr1);
 spectr2=fft(vstup2)/N*2;
 %powerspectr2=spectr2.*conj(spectr2);
        
 %otoc o 90 a secti
 phiii=90 ;
 spectrplus90=(cos(pi/180*phiii)+1i*sin(pi/180*phiii))*spectr2; 
 spectrminus90=(cos(pi/180*phiii)-1i*sin(pi/180*phiii))*spectr2;
 
 %spectrplus90=1i*spectr2; 
 %spectrminus90=-1i*spectr2;
 
 soucetspectr(1:N/2)=spectr1(1:N/2) + spectrplus90(1:N/2);
 soucetspectr(N/2+1:N)=spectr1(N/2+1:N) + spectrminus90(N/2+1:N);
 powerspectr=soucetspectr.*conj(soucetspectr);
 vstupsouc=ifft(soucetspectr)*N/2;
 %konec otoc o 90 a secti
 
 vstupsouc=vstupsouc-mean(vstupsouc);
 vstupj=vstupsouc.*okenko; %vstupsouc
end;

if prijimac<4
 spectr2=fft(vstupj);
end
 
if (prijimac>=1)&(prijimac<=3)
 rychlost=0
 figure(200)
 if rychlost
      faxis=-(((fdol5:fhor)-1)*fsample/N-froffset-4.545)*0.5*2.99792458e8/(frec*1e6);     
      semilogy(faxis,powerspectr(fdol5:fhor),'b');
      xlabel('velocity [m/s]');
      ylabel('bit^2/Hz^1^/^2');
      xlim([-100 100])
      ylim([1e-1 1e5])
      Shour=(hlavicka(18));
      title(['f=' num2str(frec,'%3.2f') ' MHz;  ' num2str(Shour) ':' num2str(Smin) ' UT'])  
      grid on
        print('-dpng', '-r150', [cestaprint 'velocity_' num2str(Shour) '_' num2str(Smin)])
      hold off  
 else
  subplot(2,1,1)
  plot(1:N,real(vstupsouc),'b');
  hold on
  plot(1:N,vstup1,'r');
  plot(1:N,vstup2,'g');
  %ylim([-2000 2000])
  grid on
  hold off

  subplot(2,1,2)
  faxis=((fdol5:fhor)-1)*fsample/N-froffset ;
      semilogy(faxis,powerspectr(fdol5:fhor),'b');
      xlabel('frequency [Hz]');
      ylabel('bit^2/Hz^1^/^2');
      xlim([fdolhz5-froffset fhorhz-froffset])
        if length(hlavicka)>=18
          Shour=(hlavicka(18));
          title(['f=' num2str(frec,'%3.2f') ' MHz;  ' num2str(Shour) ':' num2str(Smin) ' UT'])
        end
      %semilogy(5:N-4,spectr2(5:N-4).*conj(spectr2(5:N-4)),'b');
   grid on
   hold off   
 end
  
%  uicontrol(gcf,'pos',[10 10 30 20],...
%     'style','togglebutton',...
%     'value',0,...
%     'string','Stop',...
%      'tag','tlacitko',...
%      'callback',@zastav);
     
end

if prijimac<4
%pozor na symetrii
    spectr2(fremove-3:fremove+3)=0.5*(spectr2(fremove-4)+spectr2(fremove+4));   %% tady odstran Pruhonice
     
%symetricky
%spectr2(floor(N/2)+1:N)=okenkofrh.*spectr2(floor(N/2)+1:N);
    spectr2(N-(fremove+3)+2:N-(fremove-3)+2)=0.5*(spectr2(N-(fremove+4)+2)+spectr2(N-(fremove-4)+2));  %% tady odstran Pruhonice

powerspectr=spectr2.*conj(spectr2);
end
if prijimac==4
     N=pocetvzorku;
      spectr1=fft(vstup1)/N*2;
      %powerspectr1=spectr1.*conj(spectr1);
      spectr2=fft(vstup2)/N*2;
      %powerspectr2=spectr2.*conj(spectr2);
      
      spectr3=fft(vstup3)/N*2;
      %powerspectr1=spectr1.*conj(spectr1);
      spectr4=fft(vstup4)/N*2;
      %powerspectr2=spectr2.*conj(spectr2);
      
        
      %otoc o 90 a secti
      spectrplus90=1i*spectr2; 
      spectrminus90=-1i*spectr2;
      soucetspectr(1:N/2)=spectr1(1:N/2) + spectrplus90(1:N/2);
      soucetspectr(N/2:N)=spectr1(N/2:N) + spectrminus90(N/2:N);
      %soucetspectr(1)=0;
      soucetspectr(1)=0.5*(real(spectr1(1))+real(spectr2(1)));
      powerspectr=soucetspectr.*conj(soucetspectr);
      vstupsoucd=ifft(soucetspectr)*N/2;
      %konec otoc o 90 a secti
      vstupsouc=vstupsoucd-mean(vstupsoucd);
      
      
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %otoc o 90 a secti antena 2
      spectrplus90=1i*spectr4; 
      spectrminus90=-1i*spectr4;
      soucetspectr2(1:N/2)=spectr3(1:N/2) + spectrplus90(1:N/2);
      soucetspectr2(N/2:N)=spectr3(N/2:N) + spectrminus90(N/2:N);
      %soucetspectr(1)=0;
      soucetspectr2(1)=0.5*(real(spectr3(1))+real(spectr4(1)));
      powerspectr2=soucetspectr2.*conj(soucetspectr2);
      vstupsoucd2=ifft(soucetspectr2)*N/2;
      %konec otoc o 90 a secti antena 2
      vstupsouc2=vstupsoucd2-mean(vstupsoucd2);
      
      figure(200)
      subplot(2,2,3)
      hold off
      %faxis=((1:N/2)-1)*fs/N;
      faxis=((fdol5:fhor)-1)*fsample/N-froffset ;
      semilogy(faxis,powerspectr(fdol5:fhor),'b');
      xlabel('frequency [Hz]');
      ylabel('bit^2/Hz^1^/^2');
      ylim([1e-10 1e10]);
      grid on
      title(['Antena 1']);
     
      subplot(2,2,1)
      hold off
      time=(0:pocetvzorku-1)/fsample;
      plot(time,vstup1,'-b.')
      hold on
      plot(time,vstup2,'-r.')
      plot(time,real(vstupsoucd),'-g.')
      xlabel(['time (s); ' datestr(datenum(Syear,Smonth,Sday,Shour,Smin,0))]); 
      ylabel('A1 bit')
      %ylim([0 65536])
      grid on
      
      %antena 2
      subplot(2,2,4)
      hold off
      %faxis=((1:N/2)-1)*fs/N;
      semilogy(faxis,powerspectr2(fdol5:fhor),'b');
      xlabel('frequency [Hz]');
      ylabel('bit^2/Hz^1^/^2');
      ylim([1e-10 1e10]);
      grid on
      title(['Antena 2']);
     
      subplot(2,2,2)
      hold off
      time=(0:pocetvzorku-1)/fsample;
      plot(time,vstup3,'-b.')
      hold on
      plot(time,vstup4,'-r.')
      plot(time,real(vstupsoucd2),'-g.')
      xlabel(['time (s); ' datestr(datenum(Syear,Smonth,Sday,Shour,Smin,0))]); 
      ylabel('A2 bit')
      %ylim([0 65536])
      grid on
       
      powerspectr=powerspectr+powerspectr2;    
end % prijimac==4


%hledej maximum z minutoveho prumeru

%hledej maximum pro prvni vysilac (Panska Ves)
[C,I]=max(powerspectr(fdolm1:fhorm1));
fmax_1=fdolm1+I-1;
vyktot_1=sum(powerspectr(fdolm1:fhorm1));
vykpeak_1=sum(powerspectr(round(fmax_1)-okoli:round(fmax_1)+okoli));
RMS1=vykpeak_1/vyktot_1;
vyrez=fdolm1:fhorm1;
  % filter
%    powerspectr(vyrez(3:length(vyrez)-2)) = 1/5*(powerspectr(vyrez(1:length(vyrez)-4))+...
%     powerspectr(vyrez(2:length(vyrez)-3))+powerspectr(vyrez(3:length(vyrez)-2))+...
%     powerspectr(vyrez(4:length(vyrez)-1))+powerspectr(vyrez(5:length(vyrez)-0)));
%orez=quantile(powerspectr(vyrez),0.95);
%tyto=find((powerspectr(vyrez)>=orez)|(powerspectr(vyrez)>=0.2*C));
prah=1*1e11;
if floor(frec)==4
   prah=1*1e10; 
end
if vykpeak_1>prah;
    tyto=find((powerspectr(vyrez)>=0.15*prah/vykpeak_1*C));
else
    tyto=[];
end
  powerpom=[];
  powerpom=powerspectr(vyrez);
  powerspectr(vyrez)=0;
  powerspectr(vyrez(tyto))=powerpom(tyto);
ftez1=sum(powerspectr(vyrez(tyto)).*(vyrez(tyto)))/sum(powerspectr(vyrez(tyto)));
RMSbw1=sqrt(sum(powerspectr(vyrez(tyto)).*((vyrez(tyto))-ftez1).^2)/sum(powerspectr(vyrez(tyto))));
  if ftez1>0 
     ftez1=(vykpeak_1/prah*ftez1+fmax_1)/(1+vykpeak_1/prah); 
  end
%RMSbw1=(sum(powerspectr(fdolm1:fhorm1).*abs((fdolm1:fhorm1)-ftez1))/sum(powerspectr(fdolm1:fhorm1)));
%  med1=median(powerspectr(vyrez));
%  gaussEqn = 'ag*exp(-((x-bg)/cg)^2)+dg';
%  startPoints = [C-med1 I RMSbw1*sqrt(2) med1];
%  exclude=find(powerspectr(vyrez)<orez);
%  fg = fit((vyrez)',powerspectr(vyrez)',gaussEqn,'Start', startPoints,'Exclude',exclude); 
%  ftez1=fg.bg+fdolm1-1; 
%  RMSbw1=fg.cg/sqrt(2);
 
%hledej maximum pro druhy vysilac (Pruhonice)
[C_2,I_2]=max(powerspectr(fhorm1:fhorm2));
fmax_2=fhorm1+I_2-1;
vyktot_2=sum(powerspectr(fhorm1:fhorm2));
vykpeak_2=sum(powerspectr(round(fmax_2)-okoli:round(fmax_2)+okoli));
RMS2=vykpeak_2/vyktot_2;

%hledej maximum pro treti vysilac (Dlouha Louka)
[C_3,I_3]=max(powerspectr(fdol:fdolm1));
fmax_3=fdol+I_3-1;
vyktot_3=sum(powerspectr(fdol:fdolm1));
vykpeak_3=sum(powerspectr(round(fmax_3)-okoli:round(fmax_3)+okoli));
RMS3=vykpeak_3/vyktot_3;

%hledej maximum pro ctvrty vysilac vysilac (Kasperske Hory)
[C_4,I_4]=max(powerspectr(fhorm2:fhor));
fmax_4=fhorm2+I_4-1;
vyktot_4=sum(powerspectr(fhorm2:fhor));
vykpeak_4=sum(powerspectr(round(fmax_4)-2:round(fmax_4)+2));
RMS4=vykpeak_4/vyktot_4;
% if pomer_4 < 0.2  % spocti jako teziste
%    %fmax2=sum(powerspectr(fhorm1:fhor).*(fhorm1:fhor))/sum(powerspectr(fhorm1:fhor));   
% end;

%hledej maximum pro paty vysilac vysilac (Novy Kostel)
[C_5,I_5]=max(powerspectr(fdol5:fdol));
fmax_5=fdol5+I_5-1;
vyktot_5=sum(powerspectr(fdol5:fdol));
vykpeak_5=sum(powerspectr(round(fmax_5)-2:round(fmax_5)+2));
RMS5=vykpeak_5/vyktot_5;
% if pomer_5 < 0.2  % spocti jako teziste
%    %fmax2=sum(powerspectr(fhorm1:fhor).*(fhorm1:fhor))/sum(powerspectr(fhorm1:fhor));   
% end;


    fmax_1tot((cislfilu-1)*pocetFFT+dataset)=fmax_1;
    pomer_1tot((cislfilu-1)*pocetFFT+dataset)=vykpeak_1;   % drive pomer_1;
    RMS1tot((cislfilu-1)*pocetFFT+dataset)=RMS1;   %vykpeak/vyktot;
    ftez1tot((cislfilu-1)*pocetFFT+dataset)=ftez1; %teziste
    RMSbw1tot((cislfilu-1)*pocetFFT+dataset)=RMSbw1; %RMS bandwidth 
    
    fmax_2tot((cislfilu-1)*pocetFFT+dataset)=fmax_2;
    pomer_2tot((cislfilu-1)*pocetFFT+dataset)=vykpeak_2;
    RMS2tot((cislfilu-1)*pocetFFT+dataset)=RMS2;   %vykpeak/vyktot;
    
    fmax_3tot((cislfilu-1)*pocetFFT+dataset)=fmax_3;
    pomer_3tot((cislfilu-1)*pocetFFT+dataset)=vykpeak_3;
    RMS3tot((cislfilu-1)*pocetFFT+dataset)=RMS3;   %vykpeak/vyktot;
    
    fmax_4tot((cislfilu-1)*pocetFFT+dataset)=fmax_4;
    pomer_4tot((cislfilu-1)*pocetFFT+dataset)=vykpeak_4;
    RMS4tot((cislfilu-1)*pocetFFT+dataset)=RMS4;   %vykpeak/vyktot;
    
    fmax_5tot((cislfilu-1)*pocetFFT+dataset)=fmax_5;
    pomer_5tot((cislfilu-1)*pocetFFT+dataset)=vykpeak_5;
    RMS5tot((cislfilu-1)*pocetFFT+dataset)=RMS5;   %vykpeak/vyktot;

   
yproc1=ifft(spectr2);

%cind=colormap;

for jemne=1:zjemneni;   
  %[dataset jemne]
  okenkotime=exp(-(x-((jemne-1)+0.5)*slide).^2/2/polosirkatime^2); %gaussian
  ik=find( -((x-((jemne-1)+0.5)*slide).^2/2/polosirkatime^2) > -0.5);
  okenkotimer(1:length(okenkotime))=0;
  okenkotimer(ik)=1;
  okenkotimec=cos(pi/2*(x-((jemne-1)+0.5)*slide)/polosirkatime);
  okenkotimec=okenkotimec.*okenkotimer;
  okenkotime=okenkotimer; % rectangular
  okenkotime=okenkotimec; % cosine
   %keyboard
   %subplot(2,1,2);
%    figure(3)
%    plot(x,okenkotime,'color',cind((jemne-1)*4+1,:));
%    hold on

if prijimac<4
 if amplitudy<2 
   vstupj=yproc1.*okenkotime;
 else
   vstupj=yproc1;  
 end
  spectr2=fft(vstupj);
  powerspectr=spectr2.*conj(spectr2);
  %amplitspectr_s2(dataset,:)=powerspectr.^0.5;
  amplitspectr_s2(jemne,:)=powerspectr;  
  
else %prijimac==4
  if amplitudy<2 
   vstupj=vstupsouc.*okenkotime;
   vstupj2=vstupsouc2.*okenkotime;
  else
   vstupj=vstupsouc; 
   vstupj2=vstupsouc2; 
  end

  vstupj2=-vstupj2; 

  spectr2=fft(vstupj);
  powerspectr=spectr2.*conj(spectr2);
  spectr4=fft(vstupj2);
  powerspectr2=spectr4.*conj(spectr4);
  
  % Stokes I as a total power spectrum
  powerspectr=powerspectr+powerspectr2; 
    %tytone=find(powerspectr<1e11);
  %amplitspectr_s2(dataset,:)=powerspectr.^0.5;
  amplitspectr_s2(jemne,:)=powerspectr;  
  
  % Other Stokes parameters
  StokesQ=powerspectr-2*powerspectr2; % 2 because of last assignment to powerspectr (powerspectr=powerspectr+powerspectr2;)
  StokesU=2*real(spectr2.*conj(spectr4));
  StokesV=2*imag(spectr2.*conj(spectr4));
  % angle of linear polarisation
  ALP=0.5*atan2(StokesU,StokesQ);
  ALPdeg=180/pi*ALP;
    %ALPdeg(tytone)=NaN;
  ALPd(jemne,:)=ALPdeg;
  % degree of circular polarisation (Poincare)
  DCP=atan(StokesV./sqrt(StokesQ.^2+StokesU.^2));
  DCPdeg=180/pi*DCP;
    %DCPdeg(tytone)=NaN;
  DCPd(jemne,:)=DCPdeg;
  % degree of polarisation
  DP=sqrt(StokesQ.^2+StokesU.^2+StokesV.^2)./powerspectr;
    %DP(tytone)=NaN;
  DPd(jemne,:)=DP;
  
  
end
  
  
%   %najdi maximum a stredni frekvenci 
%   %hledej v okoli maxima z minutoveho prumeru
  
  
  %najdi maximum a stredni frekvenci jako vazeny prumer v sirsim okoli
  %maxima z minutoveho prumeru
  
  frmaxw1(jemne)=sum(powerspectr(fmax_1-okoli:fmax_1+okoli).*(fmax_1-okoli:fmax_1+okoli))/sum(powerspectr(fmax_1-okoli:fmax_1+okoli));
  frmaxw2(jemne)=sum(powerspectr(fmax_2-okoli:fmax_2+okoli).*(fmax_2-okoli:fmax_2+okoli))/sum(powerspectr(fmax_2-okoli:fmax_2+okoli)); 
  frmaxw3(jemne)=sum(powerspectr(fmax_3-okoli:fmax_3+okoli).*(fmax_3-okoli:fmax_3+okoli))/sum(powerspectr(fmax_3-okoli:fmax_3+okoli));
  frmaxw4(jemne)=sum(powerspectr(fmax_4-okoli:fmax_4+okoli).*(fmax_4-okoli:fmax_4+okoli))/sum(powerspectr(fmax_4-okoli:fmax_4+okoli));
  frmaxw5(jemne)=sum(powerspectr(fmax_5-okoli:fmax_5+okoli).*(fmax_5-okoli:fmax_5+okoli))/sum(powerspectr(fmax_5-okoli:fmax_5+okoli));
  
  if seltrace==1
     wmax=round(frmaxw1(jemne));   
  end
  if seltrace==2
     wmax=round(frmaxw2(jemne));   
  end
  if seltrace==3
     wmax=round(frmaxw3(jemne));   
  end
  if seltrace==4
     wmax=round(frmaxw4(jemne));   
  end
  if seltrace==5
     wmax=round(frmaxw5(jemne));   
  end
  if ~isnan(wmax)
    priznak(jemne)=sum(powerspectr(wmax-polosirkafr:wmax+polosirkafr))/sum(powerspectr(wmax-okoli:wmax+okoli)); 
      %RMSf(jemne)=sum((log10(powerspectr(wmax-okoli:wmax+okoli))-5).*(-okoli:okoli).^2)/sum(log10(powerspectr(wmax-okoli:wmax+okoli))-5);
      %RMSf(jemne)=sqrt(RMSf(jemne));
    priznak(jemne)=sqrt(sum(powerspectr(fdolm1:fhorm1)));
    RMSf(jemne)=sqrt(sum(powerspectr(fdol5:fhor)));
  else
      priznak(jemne)=0;
  end
  
end % for jemne

dataset



% build array longer than 1 minute

colorcur=amplitspectr_s2(:,fdol5:fhor)/(N/2);
colorcurALP=ALPd(:,fdol5:fhor);
colorcurDCP=DCPd(:,fdol5:fhor);
colorcurDP=DPd(:,fdol5:fhor);

pausas=(60-N/fsample); % gap in he data in seconds
pausapix=round(pausas/slide*fsample); 
pausapixtot=pausapixtot+pausapix;
%oprav zaokrouhlovani aby se chyba nekumulovala
if (pausapixtot-pocitejminuty*pausas/slide*fsample) > 0.5
   pausapixtot=pausapixtot-1; 
   pausapix=pausapix-1; 
   if pausapix==0 
       pausapix=1; 
       pausapixtot=pausapixtot+1;
   end
end;    
if (pausapixtot-pocitejminuty*pausas/slide*fsample) < -0.5
   pausapixtot=pausapixtot+1; 
   pausapix=pausapix+1; 
end;   
 if pocitejminuty==1 pausapixp=pausapix; end; 

clear colorgap; clear frgap2_1; clear frgap2_2;  clear frgap2_3; clear frgap2_4; clear frgap2_5; clear sigmagap; 
clear frmaxgap;
 [extri extrj]=size(colorcur); 

%interpolate
colorgap(extrj,pausapix)=0;  %define size colorgap(extrj,pausapixp)=0
if (pocitejminuty >=1)
   for pausaind=1:pausapix  % pausapixp
      colorgap(:,pausaind)=0; %colorinterp1' + (colorinterp2'-colorinterp1')*pausaind/pausapixp;
      frmaxgap(pausaind)=0;
   end;   
%colorinterp1=colorinterp3;
pausapixp=pausapix; 
end;  
 
if pocitejminuty==1 
  if amplitudy<2  
    color2=[colorgap colorcur']; 
  else
    color2=colorcur'  
  end
  if prijimac==4
     if amplitudy<2  
       colorALP=[colorgap colorcurALP']; 
       colorDCP=[colorgap colorcurDCP'];
       colorDP=[colorgap colorcurDP'];
     else
       colorALP=colorcurALP'
       colorDCP=colorcurDCP'
       colorDP=colorcurDP'
     end 
  end
  
  
%   frmax1tot=[frmaxgap frmax1];
%   vlfrmax1tot=[frmaxgap vlfrmax1];
   frmaxw1tot=[frmaxgap frmaxw1];
%   priznak1tot=[frmaxgap priznak1];
%   
%   frmax2tot=[frmaxgap frmax2];
%   vlfrmax2tot=[frmaxgap vlfrmax2];
   frmaxw2tot=[frmaxgap frmaxw2];
%   priznak2tot=[frmaxgap priznak2];
%   
%   frmax3tot=[frmaxgap frmax3];
%   vlfrmax3tot=[frmaxgap vlfrmax3];
   frmaxw3tot=[frmaxgap frmaxw3];
%   priznak3tot=[frmaxgap priznak3];
%   
%   frmax4tot=[frmaxgap frmax4];
%   vlfrmax4tot=[frmaxgap vlfrmax4];
   frmaxw4tot=[frmaxgap frmaxw4];
%   priznak4tot=[frmaxgap priznak4];
%   
%   frmax5tot=[frmaxgap frmax5];
%   vlfrmax5tot=[frmaxgap vlfrmax5];
   frmaxw5tot=[frmaxgap frmaxw5];
%   priznak5tot=[frmaxgap priznak5];
  
  priznaktot=[frmaxgap priznak];
  RMSftot=[frmaxgap RMSf]
end  
 
%colorgap(1:length(colorcur),1:pausapix)=0;
%color2=[color2 colorgap];

if (pocitejminuty >1) 
    
   %color2=[color2 colorgap]; 
   if amplitudy<2 
     color2=[color2 colorgap colorcur'];
   else
     color2=[color2 colorcur'];     
   end
   
   if prijimac==4
      if amplitudy<2 
         colorALP=[colorALP colorgap colorcurALP'];
         colorDCP=[colorDCP colorgap colorcurDCP'];
         colorDP=[colorDP colorgap colorcurDP'];
      else
         colorALP=[colorALP colorcurALP'];   
         colorDCP=[colorDCP colorcurDCP'];   
         colorDP=[colorDP colorcurDP'];   
      end 
   end
       
%    %frmax1tot=[frmax1tot frmaxgap];
%    frmax1tot=[frmax1tot frmaxgap frmax1];
%    %vlfrmax1tot=[vlfrmax1tot frmaxgap];
%    vlfrmax1tot=[vlfrmax1tot frmaxgap vlfrmax1];
%    %frmaxw1tot=[frmaxw1tot frmaxgap];
   frmaxw1tot=[frmaxw1tot frmaxgap frmaxw1];
%   priznak1tot=[priznak1tot frmaxgap priznak1];
  
%    %frmax2tot=[frmax2tot frmaxgap];
%    frmax2tot=[frmax2tot frmaxgap frmax2];
%    %vlfrmax2tot=[vlfrmax2tot frmaxgap];
%    vlfrmax2tot=[vlfrmax2tot frmaxgap vlfrmax2];
%    %frmaxw2tot=[frmaxw2tot frmaxgap];
   frmaxw2tot=[frmaxw2tot frmaxgap frmaxw2];
%   priznak2tot=[priznak2tot frmaxgap priznak2];
   
%    %frmax3tot=[frmax3tot frmaxgap];
%    frmax3tot=[frmax3tot frmaxgap frmax3];
%    %vlfrmax3tot=[vlfrmax3tot frmaxgap];
%    vlfrmax3tot=[vlfrmax3tot frmaxgap vlfrmax3];
%    %frmaxw3tot=[frmaxw3tot frmaxgap];
   frmaxw3tot=[frmaxw3tot frmaxgap frmaxw3];
%    priznak3tot=[priznak3tot frmaxgap priznak3];
   
%    %frmax4tot=[frmax4tot frmaxgap];
%    frmax4tot=[frmax4tot frmaxgap frmax4];
%    %vlfrmax4tot=[vlfrmax4tot frmaxgap];
%    vlfrmax4tot=[vlfrmax4tot frmaxgap vlfrmax4];
%    %frmaxw4tot=[frmaxw4tot frmaxgap];
   frmaxw4tot=[frmaxw4tot frmaxgap frmaxw4];
%    priznak4tot=[priznak4tot frmaxgap priznak4];
   
%    frmax5tot=[frmax5tot frmaxgap frmax5];
%    vlfrmax5tot=[vlfrmax5tot frmaxgap vlfrmax5];
   frmaxw5tot=[frmaxw5tot frmaxgap frmaxw5];
%    priznak5tot=[priznak5tot frmaxgap priznak5];
    
    priznaktot=[priznaktot frmaxgap priznak];
    RMSftot=[RMSftot frmaxgap RMSf];
    
end;
% if pocitejminuty==pocetmin 
%    clear colorgap; clear frmaxgap;
%    colorgap(extrj,pausapixp)=0; %define size
%    for pausaind=1:pausapix
%        colorgap(:,pausaind)=0; %colorinterp3';
%        frmaxgap(pausaind)=0;
%    end;    
%    color2=[colorgap color2]; 
%    
%    frmax1tot=[frmaxgap frmax1tot];
%    vlfrmax1tot=[frmaxgap vlfrmax1tot];
%    frmaxw1tot=[frmaxgap frmaxw1tot];
%    
%    frmax2tot=[frmaxgap frmax2tot];
%    vlfrmax2tot=[frmaxgap vlfrmax2tot];
%    frmaxw2tot=[frmaxgap frmaxw2tot];
%    
%    frmax3tot=[frmaxgap frmax3tot];
%    vlfrmax3tot=[frmaxgap vlfrmax3tot];
%    frmaxw3tot=[frmaxgap frmaxw3tot];
%    
%    frmax4tot=[frmaxgap frmax4tot];
%    vlfrmax4tot=[frmaxgap vlfrmax4tot];
%    frmaxw4tot=[frmaxgap frmaxw4tot];
% end;


%frgap2_1(1:pausapix)= frmax2_1(zjemneni);
%frgap2_2(1:pausapix)= frmax2_2(zjemneni);
sigmagap(1:pausapix)=-3;

%frmaxtot2_1=[frmaxtot2_1 frmax2_1 frgap2_1];
%sigmaftot2_1=[sigmaftot2_1 sigmaf2_1 sigmagap];
%frmaxtot2_2=[frmaxtot2_2 frmax2_2 frgap2_2];
%sigmaftot2_2=[sigmaftot2_2 sigmaf2_2 sigmagap];
%vlfrmaxtot=[vlfrmaxtot vlfrmax frgap];

end; %dataset  minuta-1

statusb = fclose(fidb);
%  spravna delka filu
else
%   for dataset=1:pocetFFT  
%     amplitspectr_s2(dataset,:)=zeros(1,pocetvzorku);
%   end
 if pocitejminuty<1440 
  colorcur=zeros(pocetFFT,fhor-fdol5+1);
  color2=[color2 colorcur']; 
  if prijimac==4
      colorcurALP=zeros(pocetFFT,fhor-fdol5+1);
      colorcurDCP=zeros(pocetFFT,fhor-fdol5+1);
      colorcurDP=zeros(pocetFFT,fhor-fdol5+1);
      colorALP=[colorALP colorcurALP'];
      colorDCP=[colorDCP colorcurDCP'];
      colorDP=[colorDP colorcurDP'];
  end
  pocitejminuty=pocitejminuty+pocetFFT; 
 end
end % N~=pocetvzorku tedy spatna delka filu


end % cilso filu


N=pocetvzorku;


%%%now displaying
x=1:zjemneni*pocitejminuty+pausapixtot;
xx=1:pocitejminuty;

y1(1:zjemneni*pocitejminuty+pausapixtot)=offset2;
y2(1:zjemneni*pocitejminuty+pausapixtot)=shift2_2+offset2_2;
y3(1:zjemneni*pocitejminuty+pausapixtot)=-shift2_2+offset2_3;
y4(1:zjemneni*pocitejminuty+pausapixtot)=shift2_4+offset2_4;
y5(1:zjemneni*pocitejminuty+pausapixtot)=shift2_5+offset2_5;


%faxis=1:fhor-fdol+1;
faxis=((fdol5:fhor)-1)*fsample/N-froffset ; %-offset2;

% frmax1tot=(frmax1tot-1)*fsample/N-froffset; %-offset2;
% vlfrmax1tot=(vlfrmax1tot-1)*fsample/N-froffset; %-offset2;
frmaxw1tot=(frmaxw1tot-1)*fsample/N-froffset; %-offset2;

% frmax2tot=(frmax2tot-1)*fsample/N-froffset; % -(shift2_2+offset2_2);
% vlfrmax2tot=(vlfrmax2tot-1)*fsample/N-froffset ; %-(shift2_2+offset2_2);
frmaxw2tot=(frmaxw2tot-1)*fsample/N-froffset; 

% frmax3tot=(frmax3tot-1)*fsample/N-froffset; % -(-shift2_2+offset2_2);
% vlfrmax3tot=(vlfrmax3tot-1)*fsample/N-froffset ; %-(-shift2_2+offset2_2);
frmaxw3tot=(frmaxw3tot-1)*fsample/N-froffset; 

% frmax4tot=(frmax4tot-1)*fsample/N-froffset; % 
% vlfrmax4tot=(vlfrmax4tot-1)*fsample/N-froffset ; %
frmaxw4tot=(frmaxw4tot-1)*fsample/N-froffset; 

% frmax5tot=(frmax5tot-1)*fsample/N-froffset; % 
% vlfrmax5tot=(vlfrmax5tot-1)*fsample/N-froffset ; %
frmaxw5tot=(frmaxw5tot-1)*fsample/N-froffset; 

fmax_1tot=(fmax_1tot-1)*fsample/N-froffset; % 
fmax_2tot=(fmax_2tot-1)*fsample/N-froffset; % 
fmax_3tot=(fmax_3tot-1)*fsample/N-froffset; % 
fmax_4tot=(fmax_4tot-1)*fsample/N-froffset; % 
fmax_5tot=(fmax_5tot-1)*fsample/N-froffset; % 

ftez1tot=(ftez1tot-1)*fsample/N-froffset; % 
RMSbw1tot=RMSbw1tot*fsample/N;

% %mean values
% y1i=find(abs(frmax1tot-0)<20);
% y2i=find(abs(frmax2tot-0)<20);
% y3i=find(abs(frmax3tot-0)<20);
% y4i=find(abs(frmax4tot-0)<20);
% y5i=find(abs(frmax5tot-0)<20);
% 
% y1(1:zjemneni*pocitejminuty+pausapixtot)=mean(frmax1tot(y1i));
% y2(1:zjemneni*pocitejminuty+pausapixtot)=mean(frmax2tot(y2i)); %+shift2_2;
%  y2(1:zjemneni*pocitejminuty+pausapixtot)=shift2_2+offset2_2;
% y3(1:zjemneni*pocitejminuty+pausapixtot)=mean(frmax3tot(y3i)); %-shift2_2;
% y4(1:zjemneni*pocitejminuty+pausapixtot)=mean(frmax4tot(y4i)); %+shift2_4;
% y5(1:zjemneni*pocitejminuty+pausapixtot)=mean(frmax5tot(y5i)); %+shift2_4;

%oprav casy  
%pocminut=1:dataset;
%datind=(zjemneni+pausapix).*(pocminut-1)+1;
%pausind=zjemneni.*pocminut+1;

%numsss=4;
sss=0; numssc=1;
sunrise=0; %1 display sunrise and sunset
tsrise=7+36/60-1; % time of sunrise
tsset=16+54/60-1; % time of sunset
   hg=figure(1);
if sss==0 
   close(hg);
end  
figure(1)

ppp=1; slet='';
if prijimac==4
   ppp=numsss;
   numssc=1; 
end

for pppi=1:ppp
    if prijimac==4
       sss=pppi;
       if pppi==1
           slet='I '
       end
       if pppi==2
           slet='DCP (^o) '
       end
       if pppi==3
           slet='ALP (^o) '
       end
       if pppi==4
           slet='DP (1) '
       end
       
       
    end

if sss==0
    slet=' ';
end
if sss==1
    subplot(numsss,numssc,1)
    slet=['a) ' slet];
end
if (numsss>2)&(sss==2)
    param=get(figure(1),'outerposition')
    set(figure(1),'outerposition',[param(1) param(2)-param(4) param(3) numsss/2*param(4)]);
end
if sss==2
    subplot(numsss,numssc,sss+floor(numssc/2))
    slet=['b) ' slet];
end
if sss==3
    subplot(numsss,numssc,sss+numssc-mod(numssc,2))
    slet=['c) ' slet];
end
if sss==4
    subplot(numsss,numssc,2+2*mod(numssc,2))
    slet=['d) ' slet];
end
if sss==5
    subplot(numsss,numssc,4)
    slet='e) ';
end
if sss==6
    subplot(numsss,numssc,6)
    slet='f) ';
end


if prijimac<4
 colorlog2=log10(color2);
else
 if pppi==1
   colorlog2=log10(color2);  
      tytone=find(colorlog2<2.9);
 end
 if pppi==2
   colorlog2=colorDCP;  
      colorlog2(tytone)=NaN;
 end
 if pppi==3
   colorlog2=colorALP;  
      colorlog2(tytone)=NaN; 
 end
 if pppi==4
   colorlog2=colorDP;  
      colorlog2(tytone)=NaN;
 end
 
end

colormap(1-gray);
colormap(jet);
if minutaposun
  if amplitudy==2
      timeaxis=xx-0.5;
  else
      timeaxis=pocetvzorku/fsample*x/zjemneni/60-1;
  end
else
  if amplitudy==2
    timeaxis=xx+0.5;
  else
    timeaxis=pocetvzorku/fsample*x/zjemneni/60;  
  end
end
    %timeaxis=timeaxis-(1+24/60);  %% !!!! just for special display, Tohoku 2011
    %timeaxis=timeaxis-(4+11/60);  %% !!!! just for special display, Chile 2010
    if hUT==1
      timeaxis=timeaxis/60+hodinai0+minpi/60;  %% !!!! just for special display in UT
    end
    if hUT==2
      timeaxis=(timeaxis/60+hodinai0+minpi/60)/24; %% !!!! just for special display in UT format HH:MM
    end 
   
   parametry1=get(colorbar('NorthOutside'),'Position')  
  surf=pcolor(timeaxis,faxis,colorlog2);
  if sunrise 
    hold on
    plot([tsrise tsrise],[-11.5 11.5],'m--')
    plot([tsset tsset],[-11.5 11.5],'m--')
  end
   parametry2=get(colorbar('EastOutside'),'Position')
       parametry2(1)=parametry1(1)+parametry1(3)*1.04;
       parametry2(3)=parametry2(3)*0.25
           set(colorbar('EastOutside'),'Position',parametry2); 
   %save([cestaprint sjmenofilu '_' int2str(floor(frec)) 'MHz.mat'],'colorlog2','timeaxis','faxis');
  set(surf,'ButtonDownFcn', @setFcn);
%set(gca,'Ylim',[-10.5 10.5],'Xlim',[timeaxis(1) timeaxis(end)])
  shading flat
  %xlim([0 70]) %% !!!! just for special display
hold on
if (amplitudy==2)&(sss<1)
 plot(timeaxis,ftez1tot,'k','Linewidth',2)
 plot(timeaxis,ftez1tot-RMSbw1tot,'k','Linewidth',1)
 plot(timeaxis,ftez1tot+RMSbw1tot,'k','Linewidth',1)
end 
 
ylim([yDmin yDmax]);   %ylim([-10.5 10.5]);
%title(['Common logarithm of Power spectrum for Dopler shift measurement at f=3.5 MHz;'])';
%title(['Power spectrum for Dopler shift measurement at f=3.5 MHz;  time=0 is at ' casnula])';
set(gca,'FontSize',9); %15
%title(['f=3.59 MHz,  time=0 is at ' casnula ' UT'],'FontSize',12)'; % 9
title(['b) Doppler shift spectrogram at f=3.59 MHz '])';
if hUT==0
  xlabel('time (min)');
end
if hUT>=1
  xlabel('Time (UT)');
end
ylabel('\Deltaf [Hz]');
if prijimac==0
    caxis([6.5 11])  %caxis([8.0 11]) caxis([8.5 11])  caxis([6 12]);
end
if prijimac==2
    %caxis([7.0 10.5])  % 6 caxis([8.5 11])  caxis([6 12]);
    caxis([cmin cmax])
end
if prijimac==1
    caxis([7 11]) %   caxis([7 10])  6 caxis([8.5 11])  caxis([6 12]);
end
if prijimac>=3
    if hUT==0
      title([starttitle ', time=0 is at ' casnula ' UT'],'FontSize',12)';  
    end
    if hUT>=1
      if numssc<2  
        title([slet starttitle '; ' casnula(1:10)],'FontSize',11)';  
      else
        title([slet ' ' casnula(1:10)],'FontSize',11)';   
      end
      %title(['b) ' starttitle(1:10) ', ' casnula ' UT'],'FontSize',10)';
    end
    if hUT==2
      set(gca,'XTick',[0:1/24/2:1]) % for time in HH:MM
      datetick ('x','HH:MM','keeplimits')    
    end
   %title(['a) Time=0 at ' casnula ' UT'],'FontSize',10)';
   %title(['a) f=3.59 MHz, ' casnula ' UT'],'FontSize',10)';
       %text(5,7.5,'a)','FontSize',12,'Color','y');
   
      caxis([cmin cmax])
      if prijimac==4
          if pppi==2
             caxis([-90 90]) 
          end
          if pppi==3
             caxis([-90 90]) 
          end
          if pppi==4
             caxis([0 1]) 
          end
          
      end
      
      %xlim([10.50/24 11.2333/24])
end

end % for pppi


%text(hodina0,-2.8,['time=0 is at ' casnula]);
%colorbar

if amplitudy==2
%  figure(2)
%  yyaxis left
%  plot(timeaxis,RMS1tot)
%  yyaxis right
%  plot(timeaxis,pomer_1tot)
%  ylim([0 1e13])
%  xlim([0 24])
%  hold on
%  plot([0 24],[prah prah],'k--')
end

% AUTOMATIC FITS

if (pominute==2)&fity
    xxx=0.5:0.5:xx(end)-0.5;
    fmax1d=interp1(xx-0.5,fmax_1tot(end-length(xx)+1:end),xxx);
    fmax2d=interp1(xx-0.5,fmax_2tot(end-length(xx)+1:end),xxx);
    fmax3d=interp1(xx-0.5,fmax_3tot(end-length(xx)+1:end),xxx);
    fmax4d=interp1(xx-0.5,fmax_4tot(end-length(xx)+1:end),xxx);
    fmax5d=interp1(xx-0.5,fmax_5tot(end-length(xx)+1:end),xxx);
    
    pomer_1totd=interp1(xx-0.5,pomer_1tot(end-length(xx)+1:end),xxx);
    pomer_2totd=interp1(xx-0.5,pomer_2tot(end-length(xx)+1:end),xxx);
    pomer_3totd=interp1(xx-0.5,pomer_3tot(end-length(xx)+1:end),xxx);
    pomer_4totd=interp1(xx-0.5,pomer_4tot(end-length(xx)+1:end),xxx);
    pomer_5totd=interp1(xx-0.5,pomer_5tot(end-length(xx)+1:end),xxx);
    
end
if (pominute==1)&fity
    xxx=xx-0.5;
    fmax1d=fmax_1tot;
    fmax2d=fmax_2tot;
    fmax3d=fmax_3tot;
    fmax4d=fmax_4tot;
    fmax5d=fmax_5tot;
    
    pomer_1totd=pomer_1tot;
    pomer_2totd=pomer_2tot;
    pomer_3totd=pomer_3tot;
    pomer_4totd=pomer_4tot;
    pomer_5totd=pomer_5tot;  
end
if (pominute==0)&fity
    xxx=krok:krok:xx(end)-krok; % required times in minutes
    xxxo=pocetvzorku/fsample*(x-0.5)/zjemneni/60; % original times in minutes
    %xxx=xxxo;
    for ix=1:length(xxx)
      oxpi=find(((xxx(ix)-xxxo)<0)&(priznaktot~=0));
      oxni=find(((xxx(ix)-xxxo)>0)&(priznaktot~=0));
      if~isempty(oxpi)
         oxp=oxpi(1); % next closest value of original time (data)
      else
         NaN; 
      end
      if ~isempty(oxni)
          oxn=oxni(end); % previous closest values of original time (data)
      else
          oxn=oxp;
      end
      fmax1d(ix)=frmaxw1tot(oxn)+(frmaxw1tot(oxp)-frmaxw1tot(oxn))/(xxxo(oxp)-xxxo(oxn))*(xxx(ix)-xxxo(oxn));
      fmax2d(ix)=frmaxw2tot(oxn)+(frmaxw2tot(oxp)-frmaxw2tot(oxn))/(xxxo(oxp)-xxxo(oxn))*(xxx(ix)-xxxo(oxn));
      fmax3d(ix)=frmaxw3tot(oxn)+(frmaxw3tot(oxp)-frmaxw3tot(oxn))/(xxxo(oxp)-xxxo(oxn))*(xxx(ix)-xxxo(oxn));
      trace1(ix)=priznaktot(oxn)+(priznaktot(oxp)-priznaktot(oxn))/(xxxo(oxp)-xxxo(oxn))*(xxx(ix)-xxxo(oxn));
      traceall(ix)=RMSftot(oxn)+(RMSftot(oxp)-RMSftot(oxn))/(xxxo(oxp)-xxxo(oxn))*(xxx(ix)-xxxo(oxn));
      
      if maxtraces==5
          fmax4d(ix)=frmaxw4tot(oxn)+(frmaxw4tot(oxp)-frmaxw4tot(oxn))/(xxxo(oxp)-xxxo(oxn))*(xxx(ix)-xxxo(oxn));
          fmax5d(ix)=frmaxw5tot(oxn)+(frmaxw5tot(oxp)-frmaxw5tot(oxn))/(xxxo(oxp)-xxxo(oxn))*(xxx(ix)-xxxo(oxn));
      end
    end
end

if fity
    if minutaposun==1
        xxx=xxx-1;
    end
    plot((xxx-0.0),fmax1d,'-k.','Markersize',4.7,'ButtonDownFcn', @setFcn);  
    plot((xxx-0.0),fmax2d,'-k.','Markersize',4.7,'ButtonDownFcn', @setFcn);  
    plot((xxx-0.0),fmax3d,'-k.','Markersize',4.7,'ButtonDownFcn', @setFcn);  
    if maxtraces==5
        plot((xxx-0.0),fmax4d,'-k.','Markersize',4.7,'ButtonDownFcn', @setFcn);  
        plot((xxx-0.0),fmax5d,'-k.','Markersize',4.7,'ButtonDownFcn', @setFcn);
    end  
    
  if seltrace==1
    fmax_disp=fmax1d;
  end
  if seltrace==2
    fmax_disp=fmax2d; 
  end
  if seltrace==3
   fmax_disp=fmax3d;
  end
  if seltrace==4
   fmax_disp=fmax4d;
  end
  if seltrace==5
     fmax_disp=fmax5d;
  end
  h=plot((xxx-0.0),fmax_disp,'-m.','Markersize',4.7);  
  %h=plot(xx,fmax_disp,'Marker','o');
  aH=gca;
  set(aH,'ButtonDownFcn', @setFcn);
  %drawnow
  
  uicontrol(gcf,'pos',[20 10 60 20],...
    'style','pushbutton',...
    'value',0,...
    'string','Next trace',...
     'tag','tlacitko',...
     'callback',@vyber);
  
  uicontrol(gcf,'pos',[90 10 60 20],...
    'style','pushbutton',...
    'value',0,...
    'string','Finish',...
     'tag','tlacitko2',...
     'callback',@ukonci);
 
  %keyboard 
  
  while ~isfin
      pause(10)
  end
  
  if seltrace==1
    fmax1d=fmax_disp; 
  end
  if seltrace==2
    fmax2d=fmax_disp;
  end
  if seltrace==3
   fmax3d=fmax_disp;
  end
  if seltrace==4
    fmax4d=fmax_disp;
  end
  if seltrace==5
     fmax5d=fmax_disp; 
  end
  
end

dobre=find(priznaktot>0.01);  

% %plot(pocetvzorku/fsample*x/zjemneni/60,frmax1tot,'m.','Markersize',2.7);
%   %plot(pocetvzorku/fsample*x/zjemneni/60,vlfrmax1tot,'k.','Markersize',2.7);
 %plot(pocetvzorku/fsample*x(dobre)/zjemneni/60,frmaxw1tot(dobre),'m.','Markersize',4.7);
        %plot((1:length(RMS1))-0.5,RMS1,'c','Linewidth',2);
        %plot(pocetvzorku/fsample*x(dobre)/zjemneni/60,(RMSftot(dobre)-mean(frmaxw1tot(dobre)))*fsample/N*4,'c','Linewidth',2);
        %plot(pocetvzorku/fsample*x(dobre)/zjemneni/60,1-priznaktot(dobre),'y','Linewidth',2);
 % 
%%plot(pocetvzorku/fsample*x/zjemneni/60,frmax2tot,'m.','Markersize',4.7);
%   %plot(pocetvzorku/fsample*x/zjemneni/60,vlfrmax2tot,'k.','Markersize',2.7);
% plot(pocetvzorku/fsample*x(dobre)/zjemneni/60,frmaxw2tot(dobre),'m.','Markersize',4.7);
% 
% %plot(pocetvzorku/fsample*x/zjemneni/60,frmax3tot,'m.','Markersize',2.7);
 %plot(pocetvzorku/fsample*x(dobre)/zjemneni/60,frmaxw3tot(dobre),'m.','Markersize',4.7);
% 
% %plot(pocetvzorku/fsample*x/zjemneni/60,frmax4tot,'m.','Markersize',2.7);
% plot(pocetvzorku/fsample*x(dobre)/zjemneni/60,frmaxw4tot(dobre),'m.','Markersize',4.7);
% 
% %plot(pocetvzorku/fsample*x/zjemneni/60,frmax5tot,'m.','Markersize',2.7);
 %plot(pocetvzorku/fsample*x(dobre)/zjemneni/60,frmaxw5tot(dobre),'m.','Markersize',4.7);
 
 %plot((xx-0.5),fmax_1tot,'m.','Markersize',5.7);
 

% CORRECTED FIT 
% %  
% vstupfit=load([cestaprint sjmenofilu '_Tx1.dat']);
%   timefit=vstupfit(:,1);
%   dfit=vstupfit(:,2);
%   prizfit=vstupfit(:,3);
%   ifit=find(prizfit>=0.01);
%   plot((timefit(ifit)+pocetvzorku/fsample*0.5/zjemneni)/60,dfit(ifit)+offset2,'k.','Markersize',4.7)
  %plot((timefit(ifit)+pocetvzorku/fsample*0.5/zjemneni)/60,dfit(ifit)+(-shift2_2+offset2_3),'k.','Markersize',4.7)
  %plot((timefit(ifit)+pocetvzorku/fsample*0.5/zjemneni)/60,dfit(ifit)+(shift2_4+offset2_4),'k.','Markersize',4.7)
  %plot((timefit(ifit)+pocetvzorku/fsample*0.5/zjemneni)/60,dfit(ifit)+(shift2_5+offset2_5),'k.','Markersize',4.7)
  
  
%plot(pocetvzorku/fsample*x/zjemneni,sigmaftot,'m');


%popis1;
%popis2;
%popisis;

%hold off

%subplot(2,1,2)
%plot(pocetvzorku/fsample*x/zjemneni,frmaxtot,'b.');
%hold on
%plot(pocetvzorku/fsample*x/zjemneni,sigmaftot,'m.');
%xlabel('time [s]');
%ylabel('frequency [Hz]');
%grid on
%axis([0  900 -2.5 2.5]);
%xlabel('time [s]');
%ylabel('Dopplershift frequency [Hz]');
%title('Found trace of Doppler shift frequency in blue, (magenta .. accuracy)');

%hold off
%print('-dtiff', '-r150', ['I:\infra\infradetailedumosearch\' sjmenofilu]);
%print('-dtiff', '-r150', ['I:\infra\infradetailed\umo_1s_4q']);


if tiskni==1
if HF>=1
  if prijimac==0  
    print('-dtiff', '-r150', [cestaprint sjmenofilu '_3MHz_a']);  
    %print('-dtiff', '-r200', [cestaprint 'umos.tif']);
  end;
  if prijimac==1  
    print('-dtiff', '-r200', [cestaprint sjmenofilu '_3MHz_c']);  
  end;
  if prijimac==2  
    print('-djpeg', '-r300', [cestaprint sjmenofilu '_3MHz_d']);  
  end;
  if (prijimac>=3)&(floor(frec)==3)  
    print('-djpeg', '-r150', [cestaprint sjmenofilu '_3MHz_f']);  
  end;
  if (prijimac>=3)&(floor(frec)==4)  
    print('-djpeg', '-r300', [cestaprint sjmenofilu '_4MHz_f']);  
  end;
  if (prijimac>=3)&(floor(frec)==5)  
    print('-djpeg', '-r300', [cestaprint sjmenofilu '_5MHz_f']);  
  end;
  if (prijimac>=3)&(floor(frec)==6)  
    print('-djpeg', '-r300', [cestaprint sjmenofilu '_6MHz_f']);  
  end;
  if (prijimac>=3)&(floor(frec)==7)  
    print('-djpeg', '-r300', [cestaprint sjmenofilu '_7MHz_f']);  
  end;
  
end
if HF==0
 print('-dtiff', '-r150', [cestaprint sjmenofilu '_7MHz_b']);
end
end % tiskni

%nadefinuj priznaky, pred vizuelni kontrolou se predpoklada, ze plati prvni
%maxima, mezery definuj jako neplatna data .. 0

%priznak(1:length(frmaxw1tot))=1;  % pokud ma byt 1
i=find(frmaxw1tot<-20);
priznaktot(i)=0;
RMSftot(i)=0;

if (tiskni==1)&fity
  
% if (prijimac==0) & (HF==1)
%    fidtextb=fopen([cestaprint sjmenofilu '_a.txt'],'w');
% end;
% if (prijimac==0) & (HF==0)
%    fidtextb=fopen([cestaprint sjmenofilu '_b.txt'],'w');
% end;
% if prijimac==1
%    fidtextb=fopen([cestaprint sjmenofilu '_c.txt'],'w');
% end;
% if prijimac==2
%    fidtextb=fopen([cestaprint sjmenofilu '_d.txt'],'w');
% end;
% if (prijimac==3)&(floor(frec)==3)  
%    fidtextb=fopen([cestaprint sjmenofilu '_3MHz_f.txt'],'w');
% end;
% if (prijimac==3)&(floor(frec)==4)  
%    fidtextb=fopen([cestaprint sjmenofilu '_4MHz_f.txt'],'w');
% end;
% 
% 
% if  (fidtextb ~= -1)
%   %fprintf(fidtextb,['time0=' casnula '\r\n'])  
%   %fprintf(fidtextb,'time[s]  Doppler_f1_main[Hz]  Doppler_f1_aux[Hz]  mark  Doppler_f2_main[Hz]   Doppler_f2_aux[Hz] mark \r\n')  
%   fprintf(fidtextb,'%8.2f %6.2f %6.2f %4.2f %6.2f %6.2f %4.2f %6.2f %6.2f %4.2f %6.2f %6.2f %4.2f %6.2f %6.2f %4.2f\r\n',[pocetvzorku/fsample*(x-0.5)/zjemneni; frmax1tot-offset2; vlfrmax1tot-offset2; priznak1tot;...
%       frmax2tot-(shift2_2+offset2_2); vlfrmax2tot-(shift2_2+offset2_2); priznak2tot; frmax3tot-(-shift2_2+offset2_3); vlfrmax3tot-(-shift2_2+offset2_3); priznak3tot;...
%       frmax4tot-(shift2_4+offset2_4); vlfrmax4tot-(shift2_4+offset2_4); priznak4tot; frmax5tot-(shift2_5+offset2_5); vlfrmax5tot-(shift2_5+offset2_5); priznak5tot;]); 
% end;    
% statustextb=fclose(fidtextb);

if fity

 if (prijimac==0) & (HF==1)
    fidtextb=fopen([cestaprint sjmenofilu '_a.dat'],'w');
 end;
 if (prijimac==0) & (HF==0)
   fidtextb=fopen([cestaprint sjmenofilu '_b.dat'],'w');
 end;
 if prijimac==1
   fidtextb=fopen([cestaprint sjmenofilu '_c.dat'],'w');
 end;
 if prijimac==2
   fidtextb=fopen([cestaprint sjmenofilu '_d.dat'],'w');
 end;
 if (prijimac>=3)&(amplitudy<2) 
   %fidtextb=fopen([cestaprint sjmenofilu '_7MHz_f.dat'],'w');
   fidtextb=fopen([cestaprint sjmenofilu '_' num2str(floor(frec)) 'MHz_f.dat'],'w');
 end;
 if (prijimac>=3)&(amplitudy==2)
   fidtextsf=fopen([cestaprint sjmenofilu '_SF_' num2str(floor(frec)) 'MHz_f.dat'],'w');  
 end
 
end % fity

if fity
  
%   if pominute 
%     if seltrace==0
%      fprintf(fidtextb,'%8.2f %6.2f %4.2f %6.2f %4.2f %6.2f %4.2f %6.2f %4.2f %6.2f %4.2f\r\n',[60*(xx-0.5); fmax_1tot-offset2; pomer_1tot; fmax_2tot-(shift2_2+offset2_2); ppomer_2tot;...
%       fmax_3tot-(-shift2_2+offset2_3); pomer_3tot; fmax_4tot-(shift2_4+offset2_4); pomer_4tot; fmax_5tot-(shift2_5+offset2_5); pomer_5tot;]); 
%    end
   if seltrace>=1
      if (amplitudy==1)&(pominute>0)
         if maxtraces==3
          fprintf(fidtextb,'%8.2f %6.2f %6.2f %6.2f %18.0f %18.0f %18.0f %7.5f %7.5f %7.5f\r\n',[60*(xxx+0.0); fmax1d; fmax2d; fmax3d; pomer_1totd; pomer_2totd; pomer_3totd; RMS1tot; RMS2tot; RMS3tot;]); 
         end
         if maxtraces==5
          fprintf(fidtextb,'%8.2f %6.2f %6.2f %6.2f %6.2f %6.2f %18.0f %18.0f %18.0f %18.0f %18.0f %7.5f %7.5f %7.5f %7.5f %7.5f\r\n',[60*(xxx+0.0); fmax1d; fmax2d; fmax3d; fmax4d; fmax5d; pomer_1totd; pomer_2totd; pomer_3totd; pomer_4totd; pomer_5totd; RMS1tot; RMS2tot; RMS3tot; RMS4tot; RMS5tot;]); 
         end
      end
      if amplitudy==2
        fprintf(fidtextsf,'%8.2f %6.2f %8.4f %18.0f\r\n',[60*(xxx+0.0); ftez1tot; RMSbw1tot; pomer_1totd;]);   
      end
      if (amplitudy==0)
        if maxtraces==3 
          fprintf(fidtextb,'%8.2f %6.2f %6.2f %6.2f \r\n',[60*(xxx); fmax1d; fmax2d; fmax3d;]);
        end
        if maxtraces==5
         fprintf(fidtextb,'%8.2f %6.2f %6.2f %6.2f %6.2f %6.2f \r\n',[60*(xxx); fmax1d; fmax2d; fmax3d; fmax4d; fmax5d;]);
         %fprintf(fidtextb,'%8.2f %6.2f %6.2f %6.2f \r\n',[60*(xxx); fmax1d; fmax2d; fmax4d;]); % for a part 10/2013 Tucuman 
        end
      end 
      if (amplitudy==1)&(pominute==0)
          dtt=mean(diff(timeaxis*60)); 
         %fprintf(fidtextb,'%8.2f %8.2f %8.2f\r\n',[timeaxis*60-dtf/2; priznaktot; RMSftot;]);  
         fprintf(fidtextb,'%8.2f %12.2f %12.2f\r\n',[60*(xxx); trace1; traceall;]);  
      end
   end 
   
%   else     
%    if seltrace==0
%      fprintf(fidtextb,'%8.2f %6.2f %4.2f %6.2f %4.2f %6.2f %4.2f %6.2f %4.2f %6.2f %4.2f\r\n',[pocetvzorku/fsample*(x-0.5)/zjemneni; frmaxw1tot-offset2; priznak; frmaxw2tot-(shift2_2+offset2_2); priznak;...
%       frmaxw3tot-(-shift2_2+offset2_3); priznak; frmaxw4tot-(shift2_4+offset2_4); priznak; frmaxw5tot-(shift2_5+offset2_5); priznaktot;]); 
%    end
%    if seltrace==1
%       fprintf(fidtextb,'%8.2f %6.2f %4.2f\r\n',[pocetvzorku/fsample*(x-0.5)/zjemneni; frmaxw1tot-offset2; priznaktot;]);  
%    end    
%    if seltrace==2
%       fprintf(fidtextb,'%8.2f %6.2f %4.2f\r\n',[pocetvzorku/fsample*(x-0.5)/zjemneni; frmaxw2tot-(shift2_2+offset2_2); priznaktot;]);  
%    end    
%    if seltrace==3
%       fprintf(fidtextb,'%8.2f %6.2f %4.2f\r\n',[pocetvzorku/fsample*(x-0.5)/zjemneni; frmaxw3tot-(-shift2_2+offset2_3); priznaktot;]);  
%    end    
%    if seltrace==4
%       fprintf(fidtextb,'%8.2f %6.2f %4.2f\r\n',[pocetvzorku/fsample*(x-0.5)/zjemneni; frmaxw4tot-(shift2_4+offset2_4); priznaktot;]);  
%    end    
%    if seltrace==5
%       fprintf(fidtextb,'%8.2f %6.2f %4.2f\r\n',[pocetvzorku/fsample*(x-0.5)/zjemneni; frmaxw5tot-(shift2_5+offset2_5); priznaktot;]);  
%    end
%   end

if amplitudy<2
  statustextb=fclose(fidtextb);
end
if amplitudy==2
  statustextsf=fclose(fidtextsf);  
end

end;   

end % tiskni

end %while 

statuserr =fclose(ferr);    

%Doppler_korelace
%cross_2D;

if WT
   WT_FFT_AGW
end
   
dalsidny=1;
end % hour
end % for den

return

% function zastav(obj,evt)
%    pause(5)
% return

 %function setFcn(varargin)
 function setFcn(obj,evt)     
        global aH xxx fmax_disp h
        pt = get(aH, 'CurrentPoint')
        [~, b]=min(abs(pt(1)-xxx+0.0));
%         x(b)=pt(1,1);
        fmax_disp(b)=pt(1,2);
%         set(h,'XData', x);
        set(h,'YData', fmax_disp);
    return
    
function vyber(obj,evt)  
  global seltrace maxtraces h fmax_disp fmax1d fmax2d fmax3d fmax4d fmax5d xxx
  if seltrace==1
    fmax1d=fmax_disp; 
    plot((xxx-0.0),fmax1d,'-m.','Markersize',4.7); 
    seltracenext=2;
    fmax_disp=fmax2d;
  end
  if seltrace==2
    fmax2d=fmax_disp;
    plot((xxx-0.0),fmax2d,'-m.','Markersize',4.7);   
    seltracenext=3;
    fmax_disp=fmax3d; 
  end
  if seltrace==3
   fmax3d=fmax_disp;
   plot((xxx-0.0),fmax3d,'-m.','Markersize',4.7);   
   if seltrace==maxtraces
     seltracenext=1;
     fmax_disp=fmax1d;
   else
     seltracenext=4;
     fmax_disp=fmax4d;
   end
  end
  if seltrace==4
    fmax4d=fmax_disp;
    plot((xxx-0.0),fmax4d,'-m.','Markersize',4.7);   
   if seltrace==maxtraces
     seltracenext=1;
     fmax_disp=fmax1d;
   else
     seltracenext=5;
     fmax_disp=fmax5d;
   end
  end
  if seltrace==5
     fmax5d=fmax_disp; 
     plot((xxx-0.0),fmax5d,'-m.','Markersize',4.7);   
     seltracenext=1 
     fmax_disp=fmax1d;
  end
  seltrace=seltracenext
  h=plot((xxx-0.0),fmax_disp,'-m.','Markersize',4.7,'ButtonDownFcn', @setFcn);  
return

    function ukonci(obj,event)
        global isfin
       isfin=1; 
    return
        
