% cppow2.m
% 
% format: cppow2(fname,ID,DT)
%
% This program generates a summary of the signal amplitudes
% for all available GPS satellites for one evening.  The code uses 
% a .sum file to generate the figure.
%
% input:
%   fname - filename of the .sum file generated by sum.exe
%   ID - station ID which is simply a character
%   DT - time difference in hours between UTC and LT (DT = UTC - LT)
%
% example parameters:
%
% fname='000716H0.sum';
% ID = 'H';
% DT = 3;  
% 
% original:   h. kil
% update:     b. ledvina  9.17.2001   v2.0


function cppow2(fname,ID,DT)

%******************************************************************
% example
%fname='000716h0.sum';
%ID = 'H';
%DT = 3;  % time difference between UTC and LT (UTC - LT = DT)
%*******************************************************************


fid=fopen(fname,'r');
%RR=[fname(1:2),'.',fname(3:4),'.',fname(5:6),'.'];
%*** READ HEADER ***
      LOCA=fgets(fid);
      COMM=fgets(fid);

      if length(LOCA) >= 30
        CITY = LOCA(1:30);
      else
        CITY = LOCA(1:end);
      end
      %dum1=find(LOCA==13 | LOCA==10);
      %  if (~isempty(dum1))
      %     LOCA=LOCA(1:(min(dum1)-1));
      %  end
      %dum2=find(COMM==13 | COMM==10);
      %  if (~isempty(dum2))
      %     COMM=COMM(1:(min(dum2)-1));
      %  end

      HD=fscanf(fid,'%f %f %f %d %d %d %d %d %d',[9]); 
         Xo=HD(1); Yo=HD(2); Zo=HD(3); YE  =HD(4); MO=HD(5);
         DA=HD(6); HO=HD(7); MI=HD(8); Nrec=HD(9);
 
     RR =  [num2str(DA),'.',num2str(MO),'.',num2str(YE)];       
     %*** READ DATA ***   
      i=ones(32,1); UT=zeros(32,800); WBP=ones(32,800);   
     
      stop=0;
      OLDTIME=9999;

      UUU=1;


  while(stop==feof(fid))

     TIME=fscanf(fid,'%f %d %f',[3]);
       if (isempty(TIME)==1), break,end
           if (TIME(1)<1500)
              TIME(1)=TIME(1)+2400;
           end       

       if(TIME(1) == OLDTIME | TIME(1)<2100)
          for Nsat=1:TIME(2)
            
            ENTRY=fscanf(fid,'%d %f %f %f %d %d %d %f %d %d',[10]);
           
          end
       else  
          for Nsat=1:TIME(2)
            ENTRY=fscanf(fid,'%d %f %f %f %d %d %d %f %d %d',[10]);
              if (ENTRY(8)<2 & ENTRY(8)>0)
                            PRN                = ENTRY(1);
                                                          
                 m=TIME(1)-100*floor(TIME(1)/100);
                 h=(TIME(1)-m)/100;

                 UT  (PRN,i(PRN,1)) = (h*3600+m*60)/3600;                
                 WBP (PRN,i(PRN,1)) = 10*log10(ENTRY(6));
                        i(PRN,1)    = i(PRN,1)+1;
              end      
          end
       end
                OLDTIME = TIME(1);  
  end
  status=fclose(fid);
  %---------------------	END OF DATA READING --------------------------
    
pos=[.1 .80 .25 .1; .1 .70 .25 .1; .1 .60 .25 .1; .1 .50 .25 .1; 
     .1 .40 .25 .1; .1 .30 .25 .1; .1 .20 .25 .1; .1 .10 .25 .1;
     .4 .80 .25 .1; .4 .70 .25 .1; .4 .60 .25 .1; .4 .50 .25 .1; 
     .4 .40 .25 .1; .4 .30 .25 .1; .4 .20 .25 .1; .4 .10 .25 .1;
     .7 .80 .25 .1; .7 .70 .25 .1; .7 .60 .25 .1; .7 .50 .25 .1; 
     .7 .40 .25 .1; .7 .30 .25 .1; .7 .20 .25 .1; .7 .10 .25 .1];
  
  subplot('position',[0.1 0.93 0.85 0.05])
  plot([],[])
  axis([0 10 1 2])
  axis off
  text(0,1.3,['SCINTMON WBP [dB]   ',CITY '(',ID,')   ',RR],'fontsize',16)
  
  
  k=0;
  for PRN=1:32      
     NI=i(PRN,1)-1;
   if (NI>60)
      k=k+1;
      
      subplot('position',pos(k,:))
      plot(UT(PRN,1:NI)-DT,WBP(PRN,1:NI))
      axis([18 30 23 52])
      text(28.5,47,num2str(PRN))
        
      if (k==8 | k==16)
         set(gca,'xtickmode','manual','xtick',[18 20 22 24 26 28 30])
         xlabel('LT (hour)','fontsize',13)
      else
         set(gca,'xticklabel',[],'xtickmode','manual','xtick',[18 20 22 24 26 28 30])
      end
      
   end
  end
  set(gca,'xticklabel',[18 20 22 24 26 28 30],'xtickmode','manual','xtick',[18 20 22 24 26 28 30])
  xlabel('LT (hour)','fontsize',13)

   
   % set(gcf,'paperposition',[0.2 0.5 8 10.2])
   set(gcf,'paperposition',[0.1 0.6 8 10.5])

      
