%%
clear

DISP = 25285;
[ tI, yI ] = leia_TCABR( DISP, 'IP' );
[ tn, yn ] = leia_TCABR( DISP, 'ne' );
[ tM, yM ] = leia_TCABR( DISP, 'Mref' );

figure( 'position', [50 50 800 650] )
handle_MASTER = subplot( 5, 1, 1:3 );
plot( tI, yI*1e-3, '-b', tn, yn*1e-20, '-r', tM, yM/10, '-k' )
xlim( [25 160] )
ylim( [-0.1 0.2] )
ylabel( 'I_P (MA), n_e (10^{20}m^{-3}), Mirnov (a.u.)', 'fontsize', 16 )
% xlabel( 'time (ms)', 'fontsize', 16 )
% legend( 'I_P', 'n_e', 'Mirnov', 'fontsize', 14 )
title( sprintf( 'TCABR #%d', DISP ), 'fontsize', 16 )
set( gca, 'fontsize', 14, 'tickdir', 'out' )
XLIM_MASTER = xlim;
YLIM_MASTER = ylim;
hold on

handle_LINHA(4) = ...
    plot( handle_MASTER, [NaN NaN], YLIM_MASTER, [ '-m' ] );
handle_LINHA(5) = ...
    plot( handle_MASTER, [NaN NaN], YLIM_MASTER, [ '-b' ] );

VALENDO = 1;
while( VALENDO )
    [X,Y,BUTTON] = ginput(1);
    SUBPLOTATIVO = 0;
    if isempty( BUTTON ), BUTTON = ' '; end
    switch( BUTTON )
        case{ '1' }
            SUBPLOTATIVO = 4;
            CORATIVA = 'm';
        case{ '2' }
            SUBPLOTATIVO = 5;
            CORATIVA = 'b';
        case{ 'q' 'f' 27 }
            VALENDO = 0;
    end
    if SUBPLOTATIVO>0
        subplot( 5, 1, SUBPLOTATIVO )
        indSEL = find( tM>=X(1) & tM<=(X(1)+1) );
        plot( tM( indSEL ), yM( indSEL ), ['-' CORATIVA] )
        axis tight
        set( handle_LINHA(SUBPLOTATIVO), 'XData', [X(1) X(1)] )
        axes( handle_MASTER )
    end
end