Skip to content
Snippets Groups Projects
tsgqc.m 176 KiB
Newer Older
        msgbox( 'Date limits are not correct',...
          'Correction module', 'warn', 'modal');
    end
  end

%% InterpOtherCallback ...............................Interpolation OTher
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%----------------------------------------------------------------------
% Callback function run when
%----------------------------------------------------------------------
  function InterpPosOtherCallback( src, evnt)
    msgbox( 'Method not yet implemented', ...
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
      'Function InterpOtherCallback', ...
      'warn',...
      'modal' );
  end

%% InterpPosCancelCallback ..........................Cancel Interpolation
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%----------------------------------------------------------------------
  function InterpPosCancelCallback(src, evnt)
    % Callback function run when
    % Get tsg application data
    % ------------------------
    tsg = getappdata( hMainFig, 'tsg_data' );
    % Get NO_CONTROL and INTERPOLATED_VALUE codes
    % -------------------------------------------
    NO_CONTROL         = tsg.qc.hash.NO_CONTROL.code;
    INTERPOLATED_VALUE = tsg.qc.hash.INTERPOLATED_VALUE.code;
    if ~isempty( tsg.POSITION_QC )
      iINTERP = find(  tsg.POSITION_QC == INTERPOLATED_VALUE);
      tsg.LATX( iINTERP ) = NaN * ones( size( iINTERP), 1 );
      tsg.LONX( iINTERP ) = NaN * ones( size( iINTERP), 1 );
      tsg.POSITION_QC = [];
    end
    % Save tsg application data
    % --------------------------
    setappdata( hMainFig, 'tsg_data', tsg );
    % Refresh plots
    % -------------
    plot_Interpolation( hMainFig, hPlotAxes, 1 );
    plot_Interpolation( hMainFig, hPlotAxes, 2 );
    plot_Interpolation( hMainFig, hPlotAxes, 3 );
  end


%% Cal_OnMenuCallback ..................................... Calibration
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%----------------------------------------------------------------------
% Callback function run when
%----------------------------------------------------------------------
  function Cal_OnMenuCallback( src, evnt)
    % Activate or desactivate uipanels
    % --------------------------------
    set( hpCalCoef,         'Visible', 'on' );
    set( hbgParameter,      'Visible', 'off');
    set( hbgQc,             'Visible', 'off');
    % Pushbutton
    % ----------
    set( hQCToggletool,     'enable', 'off' );
    set( hClimToggletool,   'enable', 'off', 'state', 'off' );
    set( hBottleToggletool, 'enable', 'off' );
    set( hInterpToggletool, 'enable', 'off' );
    set( hHeaderPushtool,   'enable', 'off' );
    set( hPanToggletool,       'state', 'off', 'enable', 'on' );
    set( hTimelimitToggletool, 'state', 'off', 'enable', 'off' );
    set( hZoomInToggletool,    'state', 'off', 'enable', 'on' );
    set( hZoomOutToggletool,   'state', 'off', 'enable', 'on' );
    % Get tsg application data
    % ------------------------
    tsg = getappdata( hMainFig, 'tsg_data' );
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % Draw the 3 plots of the validation figure
    % -----------------------------------------
    plot_Calibration( hMainFig, hPlotAxes, 1, 'SSPS' );
    plot_Calibration( hMainFig, hPlotAxes, 2, 'SSJT' );
    plot_Calibration( hMainFig, hPlotAxes, 3, 'SSTP' );
    % find SLOPE (A) and OFFSET (B) indices in _LINCOEF_CONV array
    % ------------------------------------------------------------
    A_CNDC = strmatch('SLOPE', tsg.CNDC_LINCOEF_CONV);
    B_CNDC = strmatch('OFFSET',tsg.CNDC_LINCOEF_CONV);
    A_SSJT = strmatch('SLOPE', tsg.SSJT_LINCOEF_CONV);
    B_SSJT = strmatch('OFFSET',tsg.SSJT_LINCOEF_CONV);
    A_SSTP = strmatch('SLOPE', tsg.SSTP_LINCOEF_CONV);
    B_SSTP = strmatch('OFFSET',tsg.SSTP_LINCOEF_CONV);
    % Write the Cal Coef in the Editable uicontrol
    % --------------------------------------------
    set( hetCalCNDCSlope,  'String', num2str(tsg.CNDC_LINCOEF(A_CNDC)));
    set( hetCalCNDCOffset, 'String', num2str(tsg.CNDC_LINCOEF(B_CNDC)));
    set( hetCalSSJTSlope,  'String', num2str(tsg.SSJT_LINCOEF(A_SSJT)));
    set( hetCalSSJTOffset, 'String', num2str(tsg.SSJT_LINCOEF(B_SSJT)));
    set( hetCalSSTPSlope,  'String', num2str(tsg.SSTP_LINCOEF(A_SSTP)));
    set( hetCalSSTPOffset, 'String', num2str(tsg.SSTP_LINCOEF(B_SSTP)));
    % Intermediate variables
    % ----------------------
    tsg.CNDC_LINCOEF_NEW(A_CNDC) = str2num(get( hetCalCNDCSlope,  'String'));
    tsg.CNDC_LINCOEF_NEW(B_CNDC) = str2num(get( hetCalCNDCOffset, 'String'));
    tsg.SSJT_LINCOEF_NEW(A_SSJT) = str2num(get( hetCalSSJTSlope,  'String'));
    tsg.SSJT_LINCOEF_NEW(B_SSJT) = str2num(get( hetCalSSJTOffset, 'String'));
    tsg.SSTP_LINCOEF_NEW(A_SSTP) = str2num(get( hetCalSSTPSlope,  'String'));
    tsg.SSTP_LINCOEF_NEW(B_SSTP) = str2num(get( hetCalSSTPOffset, 'String'));
    % Save tsg application data
    % --------------------------
    setappdata( hMainFig, 'tsg_data', tsg );
  end


%% Cal_OffMenuCallback ..................................... Calibration
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%----------------------------------------------------------------------
% Callback function run when
%----------------------------------------------------------------------
  function Cal_OffMenuCallback( src, evnt)
    % Activate or desactivate uipanels
    % --------------------------------
    set( hpCalCoef,         'Visible', 'off' );
    set( hbgParameter,      'Visible', 'on');
    % Enable Pushbuttons
    % ------------------
    set( hClimToggletool,   'enable', 'on' );
    set( hBottleToggletool, 'enable', 'on' );
    set( hInterpToggletool, 'enable', 'on' );
    set( hHeaderPushtool,   'enable', 'on' );
    set( hQCToggletool,        'state', 'off', 'enable', 'on'  );
    set( hPanToggletool,       'state', 'off', 'enable', 'on' );
    set( hTimelimitToggletool, 'state', 'off', 'enable', 'on' );
    set( hZoomInToggletool,    'state', 'off', 'enable', 'on' );
    set( hZoomOutToggletool,   'state', 'off', 'enable', 'on' );
    % Get tsg application data
    % ------------------------
    tsg = getappdata( hMainFig, 'tsg_data' );
    % find SLOPE (A) and OFFSET (B) indices in _LINCOEF_CONV array
    % ------------------------------------------------------------
    A_CNDC = strmatch('SLOPE', tsg.CNDC_LINCOEF_CONV);
    B_CNDC = strmatch('OFFSET',tsg.CNDC_LINCOEF_CONV);
    A_SSJT = strmatch('SLOPE', tsg.SSJT_LINCOEF_CONV);
    B_SSJT = strmatch('OFFSET',tsg.SSJT_LINCOEF_CONV);
    A_SSTP = strmatch('SLOPE', tsg.SSTP_LINCOEF_CONV);
    B_SSTP = strmatch('OFFSET',tsg.SSTP_LINCOEF_CONV);
    % Save the calibration coefficient
    % --------------------------------
    tsg.CNDC_LINCOEF(A_CNDC) = tsg.CNDC_LINCOEF_NEW(A_CNDC);
    tsg.CNDC_LINCOEF(B_CNDC) = tsg.CNDC_LINCOEF_NEW(B_CNDC);
    tsg.SSJT_LINCOEF(A_SSJT) = tsg.SSJT_LINCOEF_NEW(A_SSJT);
    tsg.SSJT_LINCOEF(B_SSJT) = tsg.SSJT_LINCOEF_NEW(B_SSJT);
    tsg.SSTP_LINCOEF(A_SSTP) = tsg.SSTP_LINCOEF_NEW(A_SSTP);
    tsg.SSTP_LINCOEF(B_SSTP) = tsg.SSTP_LINCOEF_NEW(B_SSTP);
    tsg.CNDC_LINCOEF_NEW = [];
    tsg.SSJT_LINCOEF_NEW = [];
    tsg.SSTP_LINCOEF_NEW = [];
    % Save tsg application data
    % --------------------------
    setappdata( hMainFig, 'tsg_data', tsg );
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % Draw the 3 plots of the validation figure
    % -----------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    plot_Validation( hMainFig, hPlotAxes, 1, tsg.plot.parameter{1} );
    plot_Validation( hMainFig, hPlotAxes, 2, tsg.plot.parameter{2} );
    plot_Validation( hMainFig, hPlotAxes, 3, tsg.plot.parameter{3} );
  end

%% CalibrateCallback .......................................... Calibration
  function CalibrateCallback(src, evnt)
    % Callback function run when
    % Get tsg application data
    % ------------------------
    tsg = getappdata( hMainFig, 'tsg_data' );
    % find SLOPE (A) and OFFSET (B) indices in _LINCOEF_CONV array
    % ------------------------------------------------------------
    A_CNDC = strmatch('SLOPE', tsg.CNDC_LINCOEF_CONV);
    B_CNDC = strmatch('OFFSET',tsg.CNDC_LINCOEF_CONV);
    A_SSJT = strmatch('SLOPE', tsg.SSJT_LINCOEF_CONV);
    B_SSJT = strmatch('OFFSET',tsg.SSJT_LINCOEF_CONV);
    A_SSTP = strmatch('SLOPE', tsg.SSTP_LINCOEF_CONV);
    B_SSTP = strmatch('OFFSET',tsg.SSTP_LINCOEF_CONV);
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % Get the calibration coefficients.
    % They will be used in the function calibration
    % ---------------------------------------------
    tsg.CNDC_LINCOEF_NEW(A_CNDC) = str2num(get( hetCalCNDCSlope,  'String'));
    tsg.CNDC_LINCOEF_NEW(B_CNDC) = str2num(get( hetCalCNDCOffset, 'String'));
    tsg.SSJT_LINCOEF_NEW(A_SSJT) = str2num(get( hetCalSSJTSlope,  'String'));
    tsg.SSJT_LINCOEF_NEW(B_SSJT) = str2num(get( hetCalSSJTOffset, 'String'));
    tsg.SSTP_LINCOEF_NEW(A_SSTP) = str2num(get( hetCalSSTPSlope,  'String'));
    tsg.SSTP_LINCOEF_NEW(B_SSTP) = str2num(get( hetCalSSTPOffset, 'String'));
    % Save tsg application data
    % --------------------------
    setappdata( hMainFig, 'tsg_data', tsg );
    % Calibrate the sensors
    % ---------------------
    calibration( hMainFig );
    % Update the Adjusted variables (SSPS - SSJT) with calibrated records
    % -------------------------------------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
    % updateAdjustedVariable( hMainFig );
    % Refresh plot #1
    % ---------------
    plot_Calibration( hMainFig, hPlotAxes, 1, 'SSPS' );
    plot_Calibration( hMainFig, hPlotAxes, 2, 'SSJT' );
    plot_Calibration( hMainFig, hPlotAxes, 3, 'SSTP' );
    % As soon as a modification took place the data should be saved
    % -------------------------------------------------------------
    set( hSaveMenu, 'UserData', 'on' );
  end

%% CancelCalibrationCallback .................................. Calibration
  function CancelCalibrationCallback(src, evnt)
    % Callback function run when
    % Get tsg application data
    % ------------------------
    tsg = getappdata( hMainFig, 'tsg_data' );
    % Emptied the CAL variables
    % -------------------------
    tsg.CNDC_CAL = [];
    tsg.SSPS_CAL = [];
    tsg.SSJT_CAL = [];
    tsg.SSTP_CAL = [];
    % Save tsg application data
    % --------------------------
    setappdata( hMainFig, 'tsg_data', tsg );
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % Update the Adjusted variables
    % -----------------------------
Yves Gouriou's avatar
Yves Gouriou committed
    % updateAdjustedVariable( hMainFig );
    % find SLOPE (A) and OFFSET (B) indices in _LINCOEF_CONV array
    % ------------------------------------------------------------
    A_CNDC = strmatch('SLOPE', tsg.CNDC_LINCOEF_CONV);
    B_CNDC = strmatch('OFFSET',tsg.CNDC_LINCOEF_CONV);
    A_SSJT = strmatch('SLOPE', tsg.SSJT_LINCOEF_CONV);
    B_SSJT = strmatch('OFFSET',tsg.SSJT_LINCOEF_CONV);
    A_SSTP = strmatch('SLOPE', tsg.SSTP_LINCOEF_CONV);
    B_SSTP = strmatch('OFFSET',tsg.SSTP_LINCOEF_CONV);
    % Write the Cal Coef in the Editable uicontrol
    % --------------------------------------------
    set( hetCalCNDCSlope,  'String', num2str(tsg.CNDC_LINCOEF(A_CNDC)));
    set( hetCalCNDCOffset, 'String', num2str(tsg.CNDC_LINCOEF(B_CNDC)));
    set( hetCalSSJTSlope,  'String', num2str(tsg.SSJT_LINCOEF(A_SSJT)));
    set( hetCalSSJTOffset, 'String', num2str(tsg.SSJT_LINCOEF(B_SSJT)));
    set( hetCalSSTPSlope,  'String', num2str(tsg.SSTP_LINCOEF(A_SSTP)));
    set( hetCalSSTPOffset, 'String', num2str(tsg.SSTP_LINCOEF(B_SSTP)));
    % Refresh plot #1
    % ---------------
    plot_Calibration( hMainFig, hPlotAxes, 1, 'SSPS' );
    plot_Calibration( hMainFig, hPlotAxes, 2, 'SSJT' );
    plot_Calibration( hMainFig, hPlotAxes, 3, 'SSTP' );
    % As soon as a modification took place the data should be saved
    % -------------------------------------------------------------
    set( hSaveMenu, 'UserData', 'on' );
%% ZoomIn_OnMenuCallback
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%----------------------------------------------------------------------
% Callback function run when the toolbar zoom in (increase) push button
% is pressed
%----------------------------------------------------------------------
  function ZoomIn_OnMenuCallback(src, evnt)
    % Desactivate some toggle buttons, hZoomOutToggletool changed state
    % must be call before zoom function because the callback set zoom to
    % off
    % -------------------------------------------------------------------
    set( hZoomOutToggletool,   'state', 'off' );
    set( hQCToggletool,        'state', 'off' );
    set( hPanToggletool,       'state', 'off' );
    set( hTimelimitToggletool, 'state', 'off' );
    % returns a zoom mode object for the figure hMainFig handle
    % ---------------------------------------------------------
    hZoom = zoom(hMainFig);
    % Set the UI Context Menu to the custom menu
    % -----------------------------------------
    set(hZoom, 'UIContextMenu', zoom_context_menu)
    
    % Turns off the automatic adaptation of date ticks
    % ------------------------------------------------
    zoomAdaptiveDateTicks('off');
    % turns interactive zooming to in (increase)
    % ------------------------------------------
    set(hZoom, 'direction', 'in');
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % test zoom operation on the MAP axes objects
    % --------------------------------------------------
    %     hMapZoom = zoom(hMapFig);
    %     setAllowAxesZoom(hMapZoom, hPlotAxes(4), true);
    %      set(hMapZoom, 'enable', 'on');
    % turns on interactive zooming (same effect than zoom on) but prevent
    % side effect on another figure
    % -------------------------------------------------------------------
    set(hZoom, 'enable', 'on');
    % Set this callback to listen to when a zoom operation finishes
    % must be call after enable zoom (bug ?)
    % -------------------------------------------------------------
    set(hZoom, 'ActionPostCallback', @ZoomPan_PostCallback);
    
    % Set map with border and save tsg
    % --------------------------------
    setappdata( hMainFig, 'tsg_data', tsg );
    
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%----------------------------------------------------------------------
% Callback function run when the toolbar zoom in (increase) push button
% is pressed
%----------------------------------------------------------------------
  function ZoomIn_OffMenuCallback(src, evnt)
    % disable zoom mode
    % -----------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%------------------------------------------------------------------------
% Callback function run when the toolbar zoom out (descrease) push button
% is pressed
%------------------------------------------------------------------------
  function ZoomOut_OnMenuCallback(src, evnt)
    % Desactivate Zoom In Toggle toggle button
    % -----------------------------------------
    set( hZoomInToggletool,    'state', 'off' );
    set( hQCToggletool,        'state', 'off' );
    set( hPanToggletool,       'state', 'off' );
    set( hTimelimitToggletool, 'state', 'off' );
    % returns a zoom mode object for the figure hMainFig handle
    % ---------------------------------------------------------
    hZoom = zoom(hMainFig);
    % Set the UI Context Menu to the custom menu
    % -----------------------------------------
    set(hZoom, 'UIContextMenu', zoom_context_menu)
    
    % turns interactive zooming out (decrease)
    % ----------------------------------------
    set(hZoom, 'direction', 'out');
    % Disallows a zoom operation on the MAP axes objects
    % --------------------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    %     setAllowAxesZoom(hZoom, hPlotAxes(4), false);
    % turns on interactive zooming (same effect than zoom on) but prevent
    % side effect on another figure
    % -------------------------------------------------------------------
    set(hZoom, 'enable', 'on');
    % Set this callback to listen to when a zoom operation finishes
    % -------------------------------------------------------------
    set(hZoom, 'ActionPostCallback', @ZoomPan_PostCallback);
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%------------------------------------------------------------------------
% Callback function run when the toolbar zoom out (descrease) push button
% is pressed
%------------------------------------------------------------------------
  function ZoomOut_OffMenuCallback(src, evnt)
    % turns interactive zooming off
    % -----------------------------
    zoom off;
    % cursor back to normal, test if it's really necessery
    % -----------------------------------------------------
    % set(hMainFig, 'Pointer', 'arrow');
    
    % Desactivate Zoom Out Toggle toggle button
    % -----------------------------------------
    set( hZoomOutToggletool,    'state', 'off' );
% callback function run from context menu from right clic in zoom mode
% --------------------------------------------------------------------
  function zoom_reset_callback(src, evnt)
    % restore map border to its preference setting for next map plotting
    % call by ZoomPan_PostCallback
    % ------------------------------------------------------------------
    tsg.preference.map.border = ...
      str2double(tsg.preference.map.border_string(tsg.preference.map.border_value));
    setappdata( hMainFig, 'tsg_data', tsg );
    
    % returns the plot to its initial zoom setting and desactivate zoom
    % -----------------------------------------------------------------
    hZoom = zoom(hMainFig);
    % zoom off doesn't work
    set(hZoom, 'enable', 'off');
    
    % Desactivate Zoom In/Out Toggle toggle button
    % -----------------------------------------
    set( hZoomInToggletool,    'state', 'off' );
    set( hZoomOutToggletool,    'state', 'off' );
    
% callback function run from context menu from right clic in zoom mode
% --------------------------------------------------------------------
  function zoom_out_callback(src, evnt)
    
    % zoom out by the specified zoom factor 0.5
    % ------------------------------------------
    zoom(0.5);
%% Pan_OnMenuCallback
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%----------------------------------------------------------------------
% Callback function run when the pan toggle toolbar is selected
%----------------------------------------------------------------------
  function Pan_OnMenuCallback(src, evnt)
    % Desactivate some toggle buttons
    % -------------------------------
    set( hQCToggletool,        'state', 'off' );
    set( hTimelimitToggletool, 'state', 'off' );
    set( hZoomInToggletool,    'state', 'off' );
    set( hZoomOutToggletool,   'state', 'off' );
    % Hide the map. Otherwise it slows down the panning
    % -------------------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    %     set( hMapToggletool,       'state', 'off' );
    % Returns a pan mode object for the figure handle
    % -----------------------------------------------
    hPan = pan(hMainFig);
    % Disallows a pan operation on the MAP axes objects
    % --------------------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    %     setAllowAxesPan(hPan, hPlotAxes(4), false);
    % turns on interactive pan (same effect than pan on) but prevent
    % side effect on another figure
    % --------------------------------------------------------------
    set(hPan, 'enable', 'on');
    % Set this callback to listen to when a zoom operation finishes
    % must be call after enable zoom (bug ?)
    % -------------------------------------------------------------
    set(hPan, 'ActionPostCallback', @ZoomPan_PostCallback);
%% Pan_OffMenuCallback
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%----------------------------------------------------------------------
% Callback function run when the pan toggle toolbar is released
%----------------------------------------------------------------------
  function Pan_OffMenuCallback(src, evnt)
    % turns interactive pan off
    % -------------------------
    pan off;
    % cursor back to normal, test if it's really necessery
    % -----------------------------------------------------
    %set(hMainFig, 'Pointer', 'arrow');
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%------------------------------------------------------------------------
% Callback function run when zoom or pan action finishes: redraw axes
%------------------------------------------------------------------------
  function ZoomPan_PostCallback(src, evnt)
    % Set the right limit and interval to the 3 axes
    % ----------------------------------------------
    for iaxe = 1:3
      set(hPlotAxes(iaxe), 'XTickMode', 'auto')
      datetick(hPlotAxes(iaxe), 'x', 'keeplimits')
    end
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % Re-draw the map once the zoom/pan is off
    % ----------------------------------------
    if strcmp( get(hMapFig,'visible'), 'on') == 1
      erase_Line( hPlotAxes, 4 );
      plot_map( hMainFig, hPlotAxes);
    end
%% QCMenuCallback
%----------------------------------------------------------------------
% Callback function run when the Automatic QC menu item is selected
%
% Si vous modiifer ce CallBack verifier les modifications a apporter a
% 1 - preferenceForm.m
% 2 - tsg_preference.m
%----------------------------------------------------------------------
  function QCMenuCallback(src, evnt, test)
    
    switch test
      case 'Speed'
        minSpeedQC(hMainFig)
      case 'Flow'
        minFlowQC(hMainFig)
      case 'Press'
        minPressQC(hMainFig)
      case 'SSPSmin'
        minSSPSQC(hMainFig)
      case 'SSPSmax'
        maxSSPSQC(hMainFig)
      case 'SSJTmin'
        minSSJTQC(hMainFig)
      case 'SSJTmax'
        maxSSJTQC(hMainFig)
      case 'SSTPmin'
        minSSTPQC(hMainFig)
      case 'SSTPmax'
        maxSSTPQC(hMainFig)
      otherwise
        msgbox('QCMenuCallback : erreur', 'QCMenuCallback','error', 'modal');
    end
    % --------------------------
    % refresh QC statistic panel
    % --------------------------
    display_QC( hMainFig );
    % ------------------
    % Refresh the plots
    %-------------------
    
    % Get the parameter displayed on plot number 1 : figure at the top
    % -----------------------------------------------------------------
    PARA = getParaCorModule( hMainFig );
    % Draw plot 1
    % -----------
    plot_Validation( hMainFig, hPlotAxes, 1, PARA{1} );
    % Update the map if already displayed
    % -----------------------------------
    if strcmp( get(hMapFig,'visible'), 'on') == 1
      erase_Line( hPlotAxes, 4 );
      plot_map( hMainFig, hPlotAxes);
    end
    % enable Undo menu if QC flags applied
    % ------------------------------------
    if (tsg.qc_history_pointer > 0)
      set(findobj('tag','UIMENU_UNDO'),'enable','on');
    end
    
%% QC_OnMenuCallback ............................... Quality Control Module
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%----------------------------------------------------------------------
% Callback function run when the QC toggle tool is pressed
%----------------------------------------------------------------------
  function QC_OnMenuCallback(src, evnt)
    % Make the QC code uipanel visible
    % --------------------------------
    set( hbgQc,                'Visible' ,'on');
    set( hDuplicateQcPanel,  'Visible', 'on');
    set( hbgParameter,         'Visible', 'off');
    % Desactivate toggletools.
    % ------------------------
    set( hZoomInToggletool,    'state', 'off', 'enable', 'on' );
    set( hZoomOutToggletool,   'state', 'off', 'enable', 'on' );
    set( hPanToggletool,        'state', 'off' );
    set( hTimelimitToggletool,  'state', 'off' );
    set( hCalToggletool,        'state', 'off', 'enable', 'off'  );
    set( hInterpToggletool,     'state', 'off', 'enable', 'off' );
    % Retrieve named application data
    % -------------------------------
    tsg = getappdata( hMainFig, 'tsg_data');
    PARA = getParaCorModule( hMainFig );
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    SAMPLE = tsg.plot.sample;
    % refresh QC statistic panel
    % --------------------------
    display_QC( hMainFig );
    % Activate right clic context menu on first axes
    % ---------------------------------------------------------
    set(hPlotAxes(1),'UIContextMenu', hQcCmenu);
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % Activate clic mouse menu on first axes for next rbbox
    % ----------------------------------------------------------------
    set(hPlotAxes(1),'ButtonDownFcn', @QC_SelectCallback);
    % change cursor to crosshair aspect
    % ---------------------------------
    set( hMainFig, 'Pointer', 'crosshair');
    % -------------------------------------------------------------
    % nested function on mouse clic when QC toggle tool is selected
    % -------------------------------------------------------------
    function QC_SelectCallback(src, evnt)
      % disable zoom by default in QC mode
      try
        zoom(hMainFig, 'off');
      catch
      end
      
      % disable ButtonMotion on main fig during select
      % prevent drawing to map
      % ----------------------------------------------
      set( hMainFig, 'WindowButtonMotionFcn', []);
      % Selection of the data within the figure
      % ---------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
      point1    = get(src,'CurrentPoint');    % button down detected
      rbbox;                      % Create rubberband box for area selection
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
      point2    = get(src,'CurrentPoint');    % button up detected
      point1 = point1(1,1:2);                 % extract x and y
      point2 = point2(1,1:2);
      p1 = min(point1,point2);
      p2 = max(point1,point2);                % calculate locations
      % The following code is only executed if the left mouse button is clicked.
      % If the right mouse button is clicked, this code must not be
      % executed as the variable 'ind' will be emptied. And this variable
      % is used when the context menu (callback 'QC_ContextMenuCallback') is called
      % -----------------------------------------------------------------
      if ~strcmp( get(gcf, 'SelectionType'), 'alt')
        % if evnt.Button == 1 % this works only with Matlab version > R2014
        
        % The QC is applied either on TSG data either on Sample data.
        % it depends if the Botte Toggle button has been cliked on
        % -----------------------------------------------------------
        if strcmp( get(hBottleToggletool, 'state'), 'on')
          % get index on selected zone
          % --------------------------
          if ~isempty(tsg.DAYD_EXT)
            
            ind = find(tsg.DAYD_EXT > p1(1) &  tsg.DAYD_EXT < p2(1) & ...
              tsg.EXT_DIF  > p1(2) &  tsg.EXT_DIF  < p2(2));
            
            % Keep the information on the indices of the selected zone
            % --------------------------------------------------------
            tsg.rbboxind = ind .* ones(size(ind));
            
            % Modifiy the QC
            % --------------
            [tsg.qc.active.Code, tsg.qc.active.Color] = get_active_QC(hMainFig);
            tsg.([SAMPLE '_EXT_QC'])(ind) = tsg.qc.active.Code;
            
            % Save the modifications
            % ----------------------
            setappdata( hMainFig, 'tsg_data', tsg);
            
            % plot selected data with selected code
            % --------------------------------------
            plot_Correction( hMainFig, hPlotAxes, PARA);
            
          end
          
        else
          
          % store last QC in history for undo
          % ---------------------------------
          
          if (tsg.qc_history_pointer == 0)
            tsg.qc_history_state = int8(zeros(size(tsg.([PARA{1} '_QC']),1),tsg.qc_history_size));
          end
          
          if (tsg.qc_history_pointer == tsg.qc_history_size)
            tsg.qc_history_state = circshift(tsg.qc_history_state,[0 -1]);
            tsg.qc_history_pointer = tsg.qc_history_pointer + 1;
          end
          
          tsg.qc_history_state(:,tsg.qc_history_pointer) = tsg.([PARA{1} '_QC']);
          tsg.qc_redo_possible = 0;
          
          
          % get index on selected zone
          % ---------------------------
          ind = find(tsg.DAYD   > p1(1,1) & tsg.DAYD   < p2(1,1) & ...
            tsg.(PARA{1}) > p1(1,2) & tsg.(PARA{1}) < p2(1,2));
          
          % Keep the information on the indices of the selected zone
          % --------------------------------------------------------
          tsg.rbboxind = ind .* ones(size(ind));
          
          % Modify the QC
          % --------------
          [tsg.qc.active.Code, tsg.qc.active.Color] = get_active_QC(hMainFig);
          tsg.([PARA{1} '_QC'])(ind) = tsg.qc.active.Code;
          
          % Save the modifications
          % ----------------------
          setappdata( hMainFig, 'tsg_data', tsg);
          
          % Draw plot 1 of the validation figure
          % ------------------------------------
          plot_Validation( hMainFig, hPlotAxes, 1, PARA{1} );
          % refresh QC statistic panel
          % --------------------------
          display_QC( hMainFig );
          % Update the map if already displayed
          % -----------------------------------
          if strcmp( get(hMapFig,'visible'), 'on') == 1
            erase_Line( hPlotAxes, 4 );
            plot_map( hMainFig, hPlotAxes);
          % enable Undo menu if QC flags applied
          % ------------------------------------
          if (tsg.qc_history_pointer > 0)
            set(findobj('tag','UIMENU_UNDO'),'enable','on');
          end
          
          % enable Redo menu if QC flags applied
          % ------------------------------------
            set(findobj('tag','UIMENU_REDO'),'enable','on');
          end
        end
        
        % As soon as a modification took place the data should be saved
        % -------------------------------------------------------------
        set( hSaveMenu, 'UserData', 'on' );
        
      % enable ButtonMotion on main fig after select QC area
      % ----------------------------------------------------
      set( hMainFig, 'WindowButtonMotionFcn', @MouseMotion);
  end

%% QC_OffMenuCallback .............................. Quality Control Module
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%----------------------------------------------------------------------
% Callback function run when the QC toggle tool is released
%----------------------------------------------------------------------
  function QC_OffMenuCallback(src, evnt)
    % Desactive right clic menu on first axes (salinity)
    % ---------------------------------------------------
    set(hPlotAxes(1),'UIContextMenu', []);
    % Desactive clic mouse menu (zoom) on first axes (salinity)
    % ----------------------------------------------------------
    set(hPlotAxes(1),'ButtonDownFcn', []);
    % Uipanel visible or not
    % -----------------------
    set( hbgQc,        'Visible', 'off' );
    set( hDuplicateQcPanel,  'Visible', 'off');
    if strcmp( get(hBottleToggletool, 'state'), 'off' )
      set( hCalToggletool,        'state', 'off', 'enable', 'on' );
      set( hInterpToggletool,     'state', 'off', 'enable', 'on' );
    end
    % uibuttongroup uipanel used to choose a parameter is set to
    % on only if the Correction screen (bottle toggletool) is off
    % -----------------------------------------------------------
    if  strcmp( get( hBottleToggletool, 'state'), 'off')
      set( hbgParameter, 'Visible', 'on');
    end
    
    % set POSITION_QC from LATX_QC or LONX_QC after validation
    % give same value for  LATX_QC and LONX_QC
    % ---------------------------------------------------------
    tsg.POSITION_QC = max(tsg.LATX_QC,tsg.LONX_QC );
    tsg.LATX_QC = tsg.POSITION_QC;
    tsg.LONX_QC = tsg.POSITION_QC;
    % Save the modifications
    % ----------------------
    setappdata( hMainFig, 'tsg_data', tsg);
    
    % cursor back to normal
    % ---------------------
    set(hMainFig,'Pointer','arrow');
%% QC_ContextMenuCallback context menu selected ........................ Quality Control Module
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%-----------------------------------------------------------
% Context menu used to attribute a QC to a previously selected
% TSG time series
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%-----------------------------------------------------------
  function QC_ContextMenuCallback(src, evnt, key)
    % Retrieve Default Quality Code and Color
    % ---------------------------------------
    tsg = getappdata( hMainFig, 'tsg_data');
    % Get the parameter (SSPS, SSJT or SSTP)
    % --------------------------------------
    PARA = getParaCorModule( hMainFig );
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    SAMPLE = tsg.plot.sample;
    % get key and some values in hashtable
    % ------------------------------------
    code  = tsg.qc.hash.(key).code;
    color = tsg.qc.hash.(key).color;
    % set active code and color from selected context menu
    % ----------------------------------------------------
    tsg.qc.active.Code  = code;
    tsg.qc.active.Color = color;
    % Modify and plot the last selected tsg data
    % ------------------------------------------
    if isfield(tsg, 'rbboxind') && ~isempty( tsg.rbboxind )
      if strcmp( get(hBottleToggletool, 'state'), 'on')
        tsg.([SAMPLE '_EXT_QC'])(tsg.rbboxind) = tsg.qc.active.Code;
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
        % Save tsg.SSPS_QC and tsg.qc.active.Code
        % in the application data
        % ----------------------------------------
        setappdata( hMainFig, 'tsg_data', tsg );
        % Draw the 3 plots of the Correction figure
        % -----------------------------------------
        plot_Correction( hMainFig, hPlotAxes, PARA );
      else
        
        % store last QC in history for undo
        % ---------------------------------
        
        if (tsg.qc_history_pointer == 0)
          tsg.qc_history_state = int8(zeros(size(tsg.([PARA{1} '_QC']),1),tsg.qc_history_size));
        end
        
        if (tsg.qc_history_pointer == tsg.qc_history_size)
          tsg.qc_history_state = circshift(tsg.qc_history_state,[0 -1]);
        else
          tsg.qc_history_pointer = tsg.qc_history_pointer + 1;
        end
        
        tsg.qc_history_state(:,tsg.qc_history_pointer) = tsg.([PARA{1} '_QC']);
        tsg.qc_redo_possible = 0;
        set(findobj('tag','UIMENU_REDO'),'enable','off');    
        
        % Modify the QC
        % --------------
        tsg.([PARA{1} '_QC'])(tsg.rbboxind) = tsg.qc.active.Code;
        
        % Save tsg.SSPS_QC in the application data before use
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
        % inside plot_Validation
        % ---------------------------------------------------
        setappdata( hMainFig, 'tsg_data', tsg );
        % Draw plot 1 of the validation figure
        % ------------------------------------
        plot_Validation( hMainFig, hPlotAxes, 1, PARA{1} );
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
      % -------------------------------------------------------------
      if strcmp( get(hMapFig, 'visible'), 'on') == 1
        erase_Line( hPlotAxes, 4 );
        plot_map( hMainFig, hPlotAxes);
      end
      
    % Update the radio button corresponding to the selected
    % QC context menu (rbg : RadioButtonGroup)
    % -----------------------------------------------------
    radioTag     = ['TAG_QC_RADIO_' char(key)];        % Build the TAG of the CODE
    hbgChildren  = get(hbgQc,'Children');        % Get the handles of the rbg
    hbgTagCell   = get(hbgChildren, 'tag');      % get the TAG of the rbg
    ind          = strcmp(radioTag, hbgTagCell); % Compare the TAG
    hRadioButton = hbgChildren( ind == 1 );      % Get the handle of the button
    set(hbgQc, 'SelectedObject', hRadioButton);  % Make this button active
    % refresh QC statistic panel
    % ---------------------------
    display_QC( hMainFig);
    %setappdata( hMainFig, 'tsg_data', tsg );
  end

%% Radiobutton Quality Control ..................... Quality Control Module
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
% ---------------------------------------------------------------
% Callback to select CODE and COLOR QC from the RadioButton Group
% ---------------------------------------------------------------
  function radioButtonQc(source, evnt)
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % Retrieve Default Quality Code and Color
    % ---------------------------------------
    tsg = getappdata( hMainFig, 'tsg_data');
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % Retrieve the key from the active RadioButton
    % --------------------------------------------
    rbTag = get(evnt.NewValue,'Tag');
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    key = strrep(rbTag, 'TAG_QC_RADIO_', '');
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % store his handle to uibuttongroup userdata
    % ------------------------------------------
    set(hbgQc,'Userdata', evnt.NewValue);
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % set active code and color from selected context menu
    % ----------------------------------------------------
    tsg.qc.active.Code  = tsg.qc.hash.(key).code;
    tsg.qc.active.Color = tsg.qc.hash.(key).color;
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    setappdata( hMainFig, 'tsg_data', tsg );
  end

%% preQcPanCallback .................................Quality Control Module
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
% ---------------------------------------------------------------
% Callback function ... to be completed
% ---------------------------------------------------------------
  function preQcPanCallback(obj, evd)
    set(hQCToggletool, 'state', 'off' );
  end

%% postQcPanCallback ............................... Quality Control Module
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
% ---------------------------------------------------------------
% Callback function ... to be completed
% ---------------------------------------------------------------
  function postQcPanCallback(obj, evd)
    % Set the right limit and interval to the 3 axes
    % ----------------------------------------------
    for iaxe = 1:3
      set(hPlotAxes(iaxe),'XTickMode','auto')
      datetick(hPlotAxes(iaxe),'x','keeplimits')
    end
    % Re-draw the map once the pan is off -  only valide in QC mode
    % -------------------------------------------------------------
    if strcmp( get(hMapFig, 'visible'), 'on') == 1
      erase_Line( hPlotAxes, 4 );
      plot_map( hMainFig, hPlotAxes);
    end