% PosTrack.m
%
% Loads a Matlab format position file exported from viewdat and plots
% the number of satellites used/tracked as a function of position type
%
% Outside of Matlab run (for a file with 35:2 records)
% viewdat -d35:2 --mat=pos.mat -h Data.T04
%
% Run the following for a file with record 29 position records:
% viewdat -d29 --mat=pos.mat -h Data.T04
%
% This will generate a file "mat_key.m" we'll load this as it gives a
% key index for the fields. You'll also need load_viewdat.m on your
% path.
%
%

Data = load_viewdat('pos.mat');
clear key;
mat_key;

MaxSVs = 100;

Labels = [ {'Autonomous- LSQ'}, ...
           {'DGNSS - LSQ'}, ...
           {'SBAS - LSQ'}, ...
           {'RTK Float'}, ...
           {'RTK Fixed'}, ...
           {'WA RTK Float'}, ...
           {'WA RTK Fixed'}, ...
           {'OmniSTAR'}, ...
           {'CDPGS'}, ...
           {'Autonomous - KF'}, ...
           {'DGNSS - KF'}, ...
           {'SBAS - KF'}, ...
           {'CDGPS - KF'}, ...
           {'SBAS+ - KF'}, ...
           {'CDGPS+ - KF'}, ...
           {'RTX'}, ...
           {'Terralite - Coarse'}, ...
           {'Terralite - Fine'}, ...
           {'INS(Autonomous)'}, ...
           {'INS(RTK)'}, ...
           {'INS(DGNSS)'}, ...
           {'INS(SBAS)'}, ...
           {'INS(RTX)'}, ...
           {'INS(GVBS)'}, ...
           {'INS(OmniSTAR)'}, ...
           {'INS(DR)'}, ...
           {'GVBS'}, ...
           {'RTX Fast'}, ...
           {'xFillx'}, ...
           {'RTX-Lite'}, ...
           {'RTX-Lite L1'} ];


for k = 1: length(Labels)
  i = find(Data(:,key.FIXTYPE) == (k-1)); 

  if(length(i) > 0)
    clear SVs;

    % Used / Tracked satellites
    SVs(:,1) = hist(Data(i,key.NUSED),0:MaxSVs);
    Max = max(find(SVs(:,1) ~= 0));
    SVs(:,2) = hist(Data(i,key.NTRK),0:MaxSVs);
    MaxTrack = max(find(SVs(:,2) ~= 0));
    if(MaxTrack > Max)
      Max = MaxTrack;
    end

    figure;
    bar(0:(Max-1),SVs(1:Max,1:2));
    legend('SVs Used','SVs Tracked','Location','Best');
    title(Labels(k));
    xlabel('Number of Satellites');
    ylabel('Number of Epochs');
    grid

    % Only the 35:2 has the breakdown of the satellites used, the older
    % format doesn't so skip this plot
    if(isfield(key,'GPSUsd'))
      clear SVs;
      % GPS - Used
      SVs(:,1) = hist(Data(i,key.GPSUsd),0:MaxSVs);
      Max = max(find(SVs(:,1) ~= 0));

      % GLN - Used
      SVs(:,2) = hist(Data(i,key.GLOUsd),0:MaxSVs);
      Max2 = max(find(SVs(:,2) ~= 0));
      if(Max2 > Max)
        Max = Max2;
      end

      % GPS - Used
      SVs(:,3) = hist(Data(i,key.GPSTrk),0:MaxSVs);
      Max2 = max(find(SVs(:,3) ~= 0));
      if(Max2 > Max)
        Max = Max2;
      end

      % GLN - Used
      SVs(:,4) = hist(Data(i,key.GLOTrk),0:MaxSVs);
      Max2 = max(find(SVs(:,4) ~= 0));
      if(Max2 > Max)
        Max = Max2;
      end

      figure;
      bar(0:(Max-1),SVs(1:Max,1:4));
      legend('GPS Used','GLN Used','GPS Track','GLN Track','Location','Best');
      title(Labels(k));
      xlabel('Number of Satellites');
      ylabel('Number of Epochs');
      grid
    end
  end
end
