% Displays NAVDIF-generated GNSS-Reference data

function generic_plot_navdif_gnss_data( action,     ...
                                        data_path,  ...
                                        kernel,     ...
                                        start_time, ...
                                        fignum,     ...
                                        prefix,     ...
                                        fig_caption)

persistent num_plots       % Static for all instances
persistent figure_params   % Array holding a structure for each instance call of this routine

thismfile=mfilename;

switch action
    
    case 'start'
        
        figure_params(fignum).h_fig_listbox = -1;

        %------------------------------------------------------------------
        % Load the data
                
        filename = [data_path prefix kernel '.out'];
          
        if test_file(filename) == 0                	% External function
            return;
        end
                    
        % data structure has 1(time) + 10 doubles per record
        data = load_data(filename,10);              %external function
        
        %------------------------------------------------------------------
        % Plot the data
        
        % Get figure and Y-axis label captions
        
        y_label_caption = set_y_label_captions;     %private function below
        
        % Bring up new figure window and set its properties
        
        fig_title = [fig_caption ' for '];
        new_plot_figure(fignum,fig_title,kernel);
        
        num_plots = 10;
        figure_params(fignum).current_plot_num = 1;
        figure_params(fignum).used_fig_captions ={'empty'};  %allocate value as cell type

        % 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 = time(1);
        end;
        
        for i=1:num_plots
            figure_params(fignum).axis_handle(i) = axes;        
            switch i
                case 1
                    figure_params(fignum).plot_handle(i) = plot(time-start_time,data(:, 2),'k');     % N position error
                case 2
                    figure_params(fignum).plot_handle(i) = plot(time-start_time,data(:, 3),'k');     % E position error
                case 3
                    figure_params(fignum).plot_handle(i) = plot(time-start_time,data(:, 4),'k');     % D position error
                case 4
                    figure_params(fignum).plot_handle(i) = plot(time-start_time,data(:, 5),'k');     % N velocity error
                case 5
                    figure_params(fignum).plot_handle(i) = plot(time-start_time,data(:, 6),'k');     % E velocity error
                case 6
                    figure_params(fignum).plot_handle(i) = plot(time-start_time,data(:, 7),'k');     % D velocity error
                case 7
                    figure_params(fignum).plot_handle(i) = plot(time-start_time,data(:, 8),'k');     % 2D radial position error
                case 8
                    figure_params(fignum).plot_handle(i) = plot(time-start_time,data(:, 9),'k');     % 3D radial position error
                case 9
                    figure_params(fignum).plot_handle(i) = plot(time-start_time,data(:,10),'k');     % 2D radial velocity error
                case 10
                    figure_params(fignum).plot_handle(i) = plot(time-start_time,data(:,11),'k');     % 3D radial velocity error
            end
            
            if i > 1
                set(figure_params(fignum).axis_handle(i),'visible','off')
                set(figure_params(fignum).plot_handle(i),'visible','off')
            end
            
            title(fig_caption);
            ylabel(y_label_caption(i));
            x_label_caption = sprintf('Time in seconds starting at %i',start_time);
            xlabel(x_label_caption);
            axis('tight')

            figure_params(fignum).used_fig_captions(i) = y_label_caption(i);
            
        end % for i=1:num_plots
        
        % 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'

        fignum = str2num(get(gcf,'Tag'));
        figure_params(fignum).current_plot_num = figure_params(fignum).current_plot_num - 1;
        if figure_params(fignum).current_plot_num < 1
            figure_params(fignum).current_plot_num = num_plots;   % Wrap to last plot
        end
        
        for i=1:num_plots
            set(figure_params(fignum).axis_handle(i),'visible','off')
            set(figure_params(fignum).plot_handle(i),'visible','off')
        end
        set(figure_params(fignum).axis_handle(figure_params(fignum).current_plot_num),'visible','on')
        set(figure_params(fignum).plot_handle(figure_params(fignum).current_plot_num),'visible','on')
        axes(figure_params(fignum).axis_handle(figure_params(fignum).current_plot_num))

    case 'right'

        fignum = str2num(get(gcf,'Tag'));
        figure_params(fignum).current_plot_num = figure_params(fignum).current_plot_num + 1;
        if figure_params(fignum).current_plot_num > num_plots
            figure_params(fignum).current_plot_num = 1;           % Wrap to first plot
        end
        
        for i=1:num_plots
            set(figure_params(fignum).axis_handle(i),'visible','off')
            set(figure_params(fignum).plot_handle(i),'visible','off')
        end
        set(figure_params(fignum).axis_handle(figure_params(fignum).current_plot_num),'visible','on')
        set(figure_params(fignum).plot_handle(figure_params(fignum).current_plot_num),'visible','on')
        axes(figure_params(fignum).axis_handle(figure_params(fignum).current_plot_num))

    case 'list'

        fignum = str2num(get(gcf,'Tag'));
        h_fig = gcf;
        
        if ishandle(figure_params(fignum).h_fig_listbox) == 0
            figure_params(fignum).h_fig_listbox = figure('name',['Plot Selection List'],'NumberTitle', 'off','Menu','none');     % ' char(figure_caption(i))]);
            set(figure_params(fignum).h_fig_listbox,'units','characters');
            set(gcf,'Tag',num2str(fignum));

            %get location of parent window
            set(h_fig,'units','characters');
            apos=get(h_fig,'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.25*apos(3); % left +25% of width
            new_width = 40;
            apos(1) = new_left;
            apos(3) = new_width;
            set(figure_params(fignum).h_fig_listbox,'pos',apos);
    
            figure_params(fignum).h_listbox = uicontrol('style','listbox','units','normalized','pos',[.1 .05 .8 .8],'TooltipString','List box',...
              'callback',[ thismfile '(''listbox'');' ]);
            set(figure_params(fignum).h_listbox,'value',figure_params(fignum).current_plot_num);
            set(figure_params(fignum).h_listbox,'string',figure_params(fignum).used_fig_captions);
            
            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');
            figure_params(fignum).new_plot_num = 0;
        end

    case 'listbox'

        fignum = str2num(get(gcf,'Tag'));
        selectiontype = get(gcbf,'SelectionType');
        
        if selectiontype(1)=='o'
            figure_params(fignum).new_plot_num = get(figure_params(fignum).h_listbox,'value');
            figure_params(fignum).current_plot_num = ...
                update_plot_1( figure_params(fignum).new_plot_num, ...
                               figure_params(fignum).plot_handle,  ...
                               figure_params(fignum).axis_handle,  ...
                               num_plots );
            close(figure_params(fignum).h_fig_listbox);
            figure_params(fignum).h_fig_listbox = -1;
        end    
        
        if selectiontype(1)=='n'
            figure_params(fignum).new_plot_num = get(figure_params(fignum).h_listbox,'value');
        end    

    case 'ListBox_OK'

        fignum = str2num(get(gcf,'Tag'));
        if figure_params(fignum).new_plot_num ~= 0
            figure_params(fignum).current_plot_num = ...
                update_plot_1( figure_params(fignum).new_plot_num, ...
                               figure_params(fignum).plot_handle,  ...
                               figure_params(fignum).axis_handle,  ...
                               num_plots );
        end
        close(figure_params(fignum).h_fig_listbox);
        figure_params(fignum).h_fig_listbox = -1;
           
    case 'ListBox_Cancel'

        fignum = str2num(get(gcf,'Tag'));
        close(figure_params(fignum).h_fig_listbox);
        figure_params(fignum).h_fig_listbox = -1;
        
end %switch

%--------------------------------------------------------------------------
%
function y_label_captions = set_y_label_captions
           
% Use cells to handle variable length strings

y_label_captions( 1) = {'North position difference (meters)'};                           
y_label_captions( 2) = {'East position difference (meters)'};          
y_label_captions( 3) = {'Down position difference (meters)'};            
y_label_captions( 4) = {'North velocity difference (m/s)'};                                   
y_label_captions( 5) = {'East velocity difference (m/s)'};                        
y_label_captions( 6) = {'Down velocity difference (m/s)'};           
y_label_captions( 7) = {'2D radial position difference (meters)'};                                    
y_label_captions( 8) = {'3D radial position difference (meters)'};                
y_label_captions( 9) = {'2D radial velocity difference (m/s)'};                                    
y_label_captions(10) = {'3D radial velocity difference (m/s)'};                                    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Edit history
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 03/01/12  B.Scherzinger  Initial release
% 10/16/12  B.Scherzinger  Moved update_plot() to external update_plot_1()
