function [verticalMode] = getVerticalMode( xst, viz )

if nargin<2
    viz=false;
end

if isstruct(xst.verticalMode)
    vm = xst.verticalMode.verticalMode;
else
    vm = xst.verticalMode;
end

u = unique(vm);
u = u(~isnan(u));

z = NaN(sum(~isnan(u)),size(xst.z,2));
for k=1:numel(u)
    f = vm==u(k);
    z(k,f) = xst.z(f);
end



nameList(1,:) = {'NONE_0','No depth control - drift or wander'};
nameList(2,:) = {'SURFACE_1','Go to the surface'};
nameList(3,:) = {'DEPTH_2','Go to a specified depth'};
nameList(4,:) = {'DEPTH_RATE_3','Go at a specified depth rate'};
nameList(5,:) = {'PITCH_ZERO_4','Hold a zero pitch'};
nameList(6,:) = {'PITCH_5','Hold the specified pitch angle'};
nameList(7,:) = {'PITCH_RATE_6','Hold the specified pitch angle rate (until max pitch)'};
nameList(8,:) = {'MASS_AND_ELEVATOR_7','Hold the specified mass and elevator positions'};
nameList(9,:) = {'FLOAT_ON_SURFACE_8','Float on the surface'};

verticalMode.verticalMode = vm;
verticalMode.time = xst.time;
verticalMode.z = z;
verticalMode.modes = u;
verticalMode.names = nameList;
verticalMode.namelist = nameList(u+1,1);


if viz
    figure;
    set(gcf,'Units','normalized','position',[0.0 0.1 1.0 0.75]);
    
    ax1 = axes;
    set(ax1,'Units','normalized','position',[0.05 0.5 0.85 0.45])
    plot(xst.time,xst.mission.z);
    yl1 = ylabel('Depth (m)');
    lg3 = legend(xst.mission.namelist,'Interpreter','none'); %,'location','eastoutside');
    lg3.Position = [0.85, 0.52, lg3.Position(3), lg3.Position(4)];
    
    ylim(ax1,[-2 ax1.YLim(2)])
    
    ax2 = axes; cla
    set(ax2,'Units','normalized','position',[0.05 0.1 0.85 0.35])
    plot(xst.time,verticalMode.z);
    lg2 = legend(verticalMode.namelist,'Interpreter','none');
    lg2.Position = [0.85, 0.12, lg2.Position(3), lg2.Position(4)];
    yl2 = ylabel('Depth (m)');
    ylim(ax2,[-2 ax2.YLim(2)])
    
    
    set([ax1, ax2],'YDir','reverse');
    linkaxes([ax1,ax2],'xy')
    dynamicDateTicks([ax1,ax2],'link')
    set(ax1,'XTickLabel',[]);
end
end
