% Displays a priori primary GNSS antenna position and Astra position update

function plot_snv_pri_gnss_pos(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 = 'snv_pri_pos';
        suffix = '.out';
        filename = [path prefix kernel suffix];
        
        if test_file(filename) == 0                	% External function
            return;
        end

        % Data structure has 1(time) + 9 doubles per record:
        
        % 1    time         Record time of validity (seconds)                           8
        % 2    latitude     GPS latitude as sent to Astra by SNV (degrees)              8
        % 3    longitude    GPS longitude as sent to Astra by SNV (degrees)             8
        % 4    altitude     GPS ellipsoidal altitude as sent to Astra by SNV (meters)   8
        % 5-7  delta_pos[3] NED delta-positions (meters).                              24
        %                   (Corrections to the SNV solution given above calculated by Astra.)
        % 8-10 integ_vel[3] XYZ integrated velocities (meters)                         24

        data = load_data(filename,9);               % 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,'A Priori Primary Antenna Position for ',kernel);
        current_plot_num = 1;
        
        %%%%%%%%%%%%%%%%%%
        % plot data
        
        num_plots = 10;
        
        % 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
            axis_handle(i) = axes;        
            switch i
                case 1
                    plot_handle(i) = plot(time-start_time,data(:,2),'k');
                case 2
                    plot_handle(i) = plot(time-start_time,data(:,3),'k');
                case 3
                    plot_handle(i) = plot(time-start_time,data(:,4),'k');   
                case 4
                    plot_handle(i) = plot(data(:,6),data(:,5),'k');
                case 5
                    plot_handle(i) = plot(time-start_time,data(:,5),'k');
                case 6
                    plot_handle(i) = plot(time-start_time,data(:,6),'k');
                case 7
                    plot_handle(i) = plot(time-start_time,data(:,7),'k');
                case 8
                    plot_handle(i) = plot(time-start_time,data(:,8),'k');
                case 9
                    plot_handle(i) = plot(time-start_time,data(:,9),'k');
                case 10
                    plot_handle(i) = plot(time-start_time,data(:,10),'k');   
            end
            
            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));
            if i==4
                x_label_caption = sprintf('East (meters)');
                xlabel(x_label_caption);
            else
                x_label_caption = sprintf('Time in seconds starting at %i',start_time);
                xlabel(x_label_caption);
                axis('tight')
            end
            
        end % for i=1:num_plots
        
        axes(axis_handle(current_plot_num));
        
        %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;   % Wrap to last plot
        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;           % Wrap to first plot
        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.25*apos(3); % left +25% of width
            new_width = 40;
            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 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 y_label_caption = set_y_label_captions

% Use cells to handle variable length strings

y_label_caption(1)  = {'Latitude (degrees)'};
y_label_caption(2)  = {'Longitude (degrees)'};
y_label_caption(3)  = {'Altitude (meters)'};

y_label_caption(4)  = {'North (meters)'};

y_label_caption(5)  = y_label_caption(4);
y_label_caption(6)  = {'East (meters)'};
y_label_caption(7)  = {'Down (meters)'};
                   
y_label_caption(8)  = {'X (meters)'};
y_label_caption(9)  = {'Y (meters)'};
y_label_caption(10) = {'Z (meters)'};

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

function figure_caption = set_figure_captions

%use cells to handle variable length strings

figure_caption(1)  = {'A Priori Primary Antenna Position'};
figure_caption(2)  = figure_caption(1);
figure_caption(3)  = figure_caption(1);
figure_caption(4)  = {'Astra Position Update Plan View'};
figure_caption(5)  = {'Astra Position Update'};
figure_caption(6)  = figure_caption(5);
figure_caption(7)  = figure_caption(5);
figure_caption(8)  = {'ECEF Integrated Velocity'};                        
figure_caption(9)  = figure_caption(8);    
figure_caption(10) = figure_caption(8);    

