% Displays IIN KAR data

function plot_kar_data(action,path,kernel,start_time);

persistent plot_handle axis_handle current_plot_num num_plots
persistent figure_caption
persistent h_listbox h_fig_listbox
persistent new_plot_num

thismfile=mfilename;

switch action
    
    case 'start'
        
        h_fig_listbox = -1;

        %%%%%%%%%%%%%%%%%%
        % Load data
        
        prefix = 'iinkar';
        suffix = '.out';
        filename = [path prefix kernel suffix];
        
        if test_file(filename) == 0                	% External function
            return;
        end
 
        % Record structure:
        % double    Record time of validity (seconds)
        % double    Number of visible GPS satellites
        % double    Number of visible GLONASS satellites
        % double    Number of used GPS satellites
        % double    Number of used GLONASS satellites
        % double    PDOP
        % double    Base-rover receiver baseline length (meters)
        % double    GNSS solution status (0-8)
        % double    Estimation mode (0-10)
        %  = 9*8

        num_items = 8;
        num_plots = num_items;

        data = load_data(filename,num_items);       % External function
        
        if isscalar(data)
          return
        end
        
        %%%%%%%%%%%%%%%%%%
        % Get figure and Y-axis label captions
        
        figure_caption  = set_figure_captions;      % Private function below
        y_label_caption = set_y_label_captions;     % Private function below

        % Bring up new figure window and set its properties
        
        new_plot_figure(0,'IIN KAR Data for ',kernel);
     	current_plot_num = 1;
        
        %%%%%%%%%%%%%%%%%%
        % Plot data
        
        % Due to plot axis labelling limitation and the number of significant digits in GPS time
        % subtract the start time and plot seconds from start - start_time is given in x-axis label
        
        time = data(:,1);
        if start_time < 0.0
            start_time = data.time(1);
        end;
        
        for i=1:num_plots
            
            axis_handle(i) = axes;        
           	plot_handle(i) = plot(time-start_time,data(:,i+1),'k');
            
            if i > 1
                set(axis_handle(i),'visible','off')
                set(plot_handle(i),'visible','off')
            end
            
            title(char(figure_caption(i)));
            ylabel(y_label_caption(i));
            x_label_caption = sprintf('Time in seconds starting at %i',start_time);
            xlabel(x_label_caption);
            axis('tight')

        end
        
        % Create buttons
        
        hleft = uicontrol('style','push','pos',[10 50 20 20],'TooltipString','Cycle left',...
            'callback',[ thismfile '(''left'');' ],'fontname','symbol','string',char(172));
        hright = uicontrol('style','push','pos',[10 30 20 20],'TooltipString','Cycle right',...
            'callback',[ thismfile '(''right'');' ],'fontname','symbol','string',char(174));
        hlist = uicontrol('style','push','pos',[10 10 40 20],'TooltipString','Plot selection list',...
            'callback',[ thismfile '(''list'');' ],'string','List');

        % Only need the one zoom -need to be after buttons for Matlab 7
        zoom on

    case 'left'
        
        current_plot_num = current_plot_num - 1;
        if current_plot_num < 1
            current_plot_num = num_plots;
        end
        
        for i=1:num_plots
            set(axis_handle(i),'visible','off')
            set(plot_handle(i),'visible','off')
        end
        set(axis_handle(current_plot_num),'visible','on')
        set(plot_handle(current_plot_num),'visible','on')
        axes(axis_handle(current_plot_num))

    case 'right'
    
        current_plot_num = current_plot_num + 1;
        if current_plot_num > num_plots
            current_plot_num = 1;
        end
        
        for i=1:num_plots
            set(axis_handle(i),'visible','off')
            set(plot_handle(i),'visible','off')
        end
        set(axis_handle(current_plot_num),'visible','on')
        set(plot_handle(current_plot_num),'visible','on')
        axes(axis_handle(current_plot_num))
         
    case 'list'
        
        if ishandle(h_fig_listbox) == 0
            h_fig_listbox = figure('name',['Plot Selection List'],'NumberTitle', 'off','Menu','none');     % ' char(figure_caption(i))]);
            set(h_fig_listbox,'units','characters');
    
            apos=get(h_fig_listbox,'pos');
            %set vertical position
            top = apos(2) + apos(4);  % bottom+ height
            new_height = num_plots + 7;
            new_bottom = top - new_height;
            apos(2) = new_bottom;
            apos(4) = new_height;
            %set horiz position
            new_left = apos(1) + 0.1*apos(3); % left +25% of width
            new_width = 70;
            apos(1) = new_left;
            apos(3) = new_width;
            set(h_fig_listbox,'pos',apos);
    
            h_listbox = uicontrol('style','listbox','units','normalized','pos',[.1 .05 .8 .8],'TooltipString','List box',...
              'callback',[ thismfile '(''listbox'');' ]);
            set(h_listbox,'value',current_plot_num);
            set(h_listbox,'string',figure_caption);
            
            h_OK = uicontrol('style','push','units','normalized','pos',[.15 .87 .25 .10],'TooltipString','OK',...
                'callback',[ thismfile '(''ListBox_OK'');' ],'string','OK');
            h_Cancel = uicontrol('style','push','units','normalized','pos',[.55 .87 .25 .10],'TooltipString','Cancel',...
                'callback',[ thismfile '(''ListBox_Cancel'');' ],'string','Cancel');
            new_plot_num = 0;
        end
        
    case 'listbox'

        selectiontype = get(gcbf,'SelectionType');
        if selectiontype(1)=='o'
            new_plot_num = get(h_listbox,'value');
            %update_plot(new_plot_num)
            current_plot_num = update_plot(new_plot_num,plot_handle, axis_handle , num_plots);

            close(h_fig_listbox);
            h_fig_listbox = -1;
        end    
        
        if selectiontype(1)=='n'
            new_plot_num = get(h_listbox,'value');
        end    

    case 'ListBox_OK'
        
        current_plot_num = update_plot(new_plot_num,plot_handle, axis_handle , num_plots);
        close(h_fig_listbox);
        h_fig_listbox = -1;
           
    case 'ListBox_Cancel'
        
        close(h_fig_listbox);
        h_fig_listbox = -1;
        
end %switch

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%function update_plot(new_plot_num)

%global plot_handle axis_handle current_plot_num num_plots

function current_plot_num = update_plot(new_plot_num,plot_handle, axis_handle , num_plots)

    if (new_plot_num > 0) && (new_plot_num <= num_plots)
    
        current_plot_num = new_plot_num;
        for i=1:num_plots
            set(axis_handle(i),'visible','off')
            set(plot_handle(i),'visible','off')
        end
        set(axis_handle(current_plot_num),'visible','on')
        set(plot_handle(current_plot_num),'visible','on')
        axes(axis_handle(current_plot_num))
        
    end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function figure_caption = set_figure_captions
           
% Use cells to handle variable length strings

figure_caption(1) = {'Number of visible GPS satellites'};                           
figure_caption(2) = {'Number of visible GLONASS satellites'};                           
figure_caption(3) = {'Number of used GPS satellites'};                           
figure_caption(4) = {'Number of used GLONASS satellites'};                           
figure_caption(5) = {'Position dilution of precision (PDOP)'};          
figure_caption(6) = {'Base-rover receiver baseline length'};            
figure_caption(7) = {'GNSS position solution status'};                                   
figure_caption(8) = {'Prefilter mode'};                                   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function y_label_caption = set_y_label_captions

% Use cells to handle variable length strings

y_label_caption(1)  = {'Number of satellites'};
y_label_caption(2)  = y_label_caption(1);
y_label_caption(3)  = y_label_caption(1);
y_label_caption(4)  = y_label_caption(1);
y_label_caption(5)  = {'PDOP'};
y_label_caption(6)  = {'Meters'};
y_label_caption(7)  = {'0=FIXED RTK - 10=NO SOLUTION'};
y_label_caption(8)  = {'0=FIXED RTK - 8=NO PREF MODE'}; 
         