Skip to content
Snippets Groups Projects
tsgqc_GUI.m 98.1 KiB
Newer Older
  % Draw the 3 plots of the validation figure 
  % -----------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
  resetAxes( hMainFig, hPlotAxes );
  plot_Validation( hMainFig, hPlotAxes, 1, PARA );
  plot_Validation( hMainFig, hPlotAxes, 2, 'SSJT' );
  plot_Validation( hMainFig, hPlotAxes, 3, 'SPDC' );
 
  % Draw the map with the ship track
  % --------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
  plot_map( hMainFig, hPlotAxes);

  % The callback to detect the mouse motion can be set to on
  % --------------------------------------------------------
  set( hMainFig, 'UserData', 'ButtonMotionOn');

  % Hide handles marked as hidden with HandleVisibility property set to
  % callback or off
  % -------------------------------------------------------------------
  set(0, 'ShowHiddenHandles', 'off');
  
end


%% *************************** CALLBACKS **********************************
  %----------------------------------------------------------------------
  % Callback function run when the Open menu item is selected
  %----------------------------------------------------------------------
    % Activate or desactivate uipanels
    % --------------------------------
    set( hpCalCoef,            'Visible', 'off' );
    set( hpDateLimit,          'Visible', 'off' );
    set( hpInterpPos,          'Visible', 'off' );
%    set( hbgParameter,         'Visible', 'off');
Yves Gouriou's avatar
Yves Gouriou committed
    % Toggle button
    % -------------
    set( hBottleToggletool,    'state', 'off' );
    set( hCalToggletool,       'state', 'off' );
Yves Gouriou's avatar
Yves Gouriou committed
    set( hClimToggletool,      'state', 'off' );
    set( hInterpToggletool,    'state', 'off' );
Yves Gouriou's avatar
Yves Gouriou committed
    set( hPanToggletool,       'state', 'off' );
    set( hQCToggletool,        'state', 'off' );
    set( hTimelimitToggletool, 'state', 'off' );
    set( hZoomToggletool,      'state', 'off' );
    
    set( hHeaderPushtool,      'enable', 'off' );
    % Open standard dialog box for retrieving files
    % ---------------------------------------------
    [fileName, pathname, filterIndex] = uigetfile( ...
      {'*.txt';'*.xml';'*.nc';'*.lbv';'*.btl'; '*.arg'}, 'Pick a file');
    % flushes the event queue and updates the closed uigetfile window
    % ---------------------------------------------------------------
    
    % if the user clicks the Cancel button or closes the dialog window, 
    % FileName and PathName are set to 0.
    % -----------------------------------------------------------------
      % Pointer set to watch during reading and plotting
      % ------------------------------------------------
      set( hMainFig, 'Pointer', 'watch' );

      % construct valid and full file path
      % -----------------------------------
      fullFileName = strcat(pathname, fileName);
          tsg_initialisation(hMainFig);
          errTsg = readTsgDataTxt(     hMainFig, fullFileName );
          tsg_initialisation(hMainFig);
          errTsg = readTsgDataXML(     hMainFig, fullFileName );
          tsg_initialisation(hMainFig);
          errTsg = readTsgDataNetCDF(  hMainFig, fullFileName );
        case 4                      % read TSG labview file *.lbv          
          tsg_initialisation(hMainFig);
          errTsg = readTsgDataLabview( hMainFig, fullFileName );

        case 5                      % Read bucket file *.btl
          if ~isempty( tsg.SSPS )
            errSpl = readBucketData(     hMainFig, fullFileName );
          else
            msgbox( 'Load a TSG file before a Water sample file', 'Read Bucket');
          end

        case 6                       % Read Argo file *.arg (G. Reverdin format)
          if ~isempty( tsg.SSPS )
            errSpl = readCoriolisData(   hMainFig, fullFileName );
          else
            msgbox( 'Load a TSG file before a Water sample file', 'Read Bucket');
          end

      % Get the tsg structure
      % ---------------------
      tsg = getappdata( hMainFig, 'tsg_data');
      
      % Default parameter
      % -----------------
      PARA = tsg.preference.parameter;
      % A TSG file has been read
      % ------------------------
      if errTsg > 0
        
        % set WindowButtonMotionFcn on
        % ----------------------------
        set( hMainFig, 'WindowButtonMotionFcn', @MouseMotion);

        % A TSG file has been open and read
        % ---------------------------------
        set( hOpenMenu, 'UserData', 'on' );
        % enable toolbar menu pushtool
        % ----------------------------
        hdl_pushtool = findobj('-regexp','Tag', 'PUSHTOOL_');
        set(hdl_pushtool, 'Enable', 'on');
        set( hbgParameter, 'Visible', 'on');
        
        % enable Save menu
        % ----------------
        set(hSaveMenu, 'Enable', 'on');
        % update some fields in tsg structure and restore tsg
        % ---------------------------------------------------
        updateTsgStruct(hMainFig );

        % update the filename display
        % ---------------------------
        set( hInfoFileText, 'String', strcat(tsg.file.name, tsg.file.ext));
        % The smooth TSG time series (tsg_moveaverage) is
        % no more computed : too long
        % We now computed a smooth value only at the position of the sample
        % in diffTsgSample
        % -----------------------------------------------------------------
        %tsg_moveaverage(hMainFig, PARA);
        % The callback to detect the mouse motion can be set to on
        % --------------------------------------------------------
        set( hMainFig, 'UserData', 'ButtonMotionOn');
        
        % Update QC statistics
        % --------------------
        % Problem to read the file or
        % Choice of parameters for Labview file canceled by the user
        % TSG structure has been reinitialize       
        % Disable toolbar menu pushtool except the open file button
        % ----------------------------------------------------------
        hdl_pushtool = findobj('-regexp','Tag', 'PUSHTOOL_');
        set(hdl_pushtool, 'Enable', 'off');
        
        set( hOpenPushtool, 'Enable', 'on' );

      end    %if errTsg > 0
      
      % Merge sample from different origins : WS and EXT
      % ------------------------------------------------
      tsg_mergesample( hMainFig, PARA );
    % Draw the 3 plots of the validation figure 
    % -----------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
    resetAxes( hMainFig, hPlotAxes )
    plot_Validation( hMainFig, hPlotAxes, 1, PARA );
    plot_Validation( hMainFig, hPlotAxes, 2, 'SSJT' );
    plot_Validation( hMainFig, hPlotAxes, 3, 'SPDC' );
    % Pointer reset to arrow
    % ----------------------
    set( hMainFig, 'Pointer', 'arrow' );

  end

Yves Gouriou's avatar
Yves Gouriou committed
%% Inter_OnMenuCallback ................................... Interpolation
  %----------------------------------------------------------------------
  % Callback function run when 
  %
  function Inter_OnMenuCallback( hObject, eventdata)
Yves Gouriou's avatar
Yves Gouriou committed
    % Activate or desactivate uipanels
    % --------------------------------
    set( hpDateLimit,          'Visible', 'on' );
    set( hpInterpPos,          'Visible', 'on' );
    set( hpCalCoef,            'Visible', 'off');
    set( hbgQc,                'Visible', 'off');
    set( hbgParameter,         'Visible', 'off');
Yves Gouriou's avatar
Yves Gouriou committed
    % Toggle button
    % -------------
    set( hBottleToggletool,    'state', 'off', 'enable', 'off');
    set( hCalToggletool,       'state', 'off', 'enable', 'off');
    set( hClimToggletool,      'state', 'off', 'enable', 'off');
    set( hPanToggletool,       'state', 'off', 'enable', 'on' );
    set( hQCToggletool,        'enable', 'off');
Yves Gouriou's avatar
Yves Gouriou committed
    set( hTimelimitToggletool, 'state', 'off', 'enable', 'on' );
    set( hZoomToggletool,      'state', 'off', 'enable', 'on' );
Yves Gouriou's avatar
Yves Gouriou committed
    set( hHeaderPushtool,      'enable', 'off' );
    
    % Get application data TSG
    % ------------------------
    tsg = getappdata( hMainFig, 'tsg_data');
    
    % Get the information on time limits of the time series
    % Write them in the uipanel
    % -----------------------------------------------------
    noNaN = tsg.DAYD(~isnan( tsg.DAYD ));
    set( hetDateMin, 'String', datestr(noNaN(1), 31));
    set( hetDateMax, 'String', datestr(noNaN(end), 31));
Yves Gouriou's avatar
Yves Gouriou committed
   
    % Draw the 3 plots of the interpolation figure 
    % --------------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
     plot_Interpolation( hMainFig, hPlotAxes, 1 );
     plot_Interpolation( hMainFig, hPlotAxes, 2 );
     plot_Interpolation( hMainFig, hPlotAxes, 3 );
Yves Gouriou's avatar
Yves Gouriou committed
    
  end

%% Inter_OffMenuCallback .................................. Interpolation
  %----------------------------------------------------------------------
  % Callback function run when 
  %
  function Inter_OffMenuCallback( hObject, eventdata)
    
    % Activate or desactivate uipanels
    % --------------------------------
    set( hpCalCoef,            'Visible', 'off' );
    set( hpDateLimit,          'Visible', 'off' );
Yves Gouriou's avatar
Yves Gouriou committed
    set( hpInterpPos,          'Visible', 'off' );
    set( hbgParameter,         'Visible', 'on'  );
Yves Gouriou's avatar
Yves Gouriou committed
    
    % Enable Pushbuttons
    % ------------------
Yves Gouriou's avatar
Yves Gouriou committed
    set( hBottleToggletool,    'state', 'off', 'enable', 'on' );
    set( hCalToggletool,       'state', 'off', 'enable', 'on'  );
    set( hClimToggletool,      'state', 'off', 'enable', 'on'  );
    set( hPanToggletool,       'state', 'off', 'enable', 'on'  );
    set( hQCToggletool,        'state', 'off', 'enable', 'on'  );
    set( hTimelimitToggletool, 'state', 'off', 'enable', 'off' );
    set( hZoomToggletool,      'state', 'off', 'enable', 'on'  );

Yves Gouriou's avatar
Yves Gouriou committed
    set( hHeaderPushtool,      'enable', 'on' );

    % Get tsg structure
    % -----------------
    tsg = getappdata( hMainFig, 'tsg_data' );
    
Yves Gouriou's avatar
Yves Gouriou committed
    % Draw the 3 plots of the validation figure 
    % -----------------------------------------
    plot_Validation( hMainFig, hPlotAxes, 1, tsg.preference.parameter );
    plot_Validation( hMainFig, hPlotAxes, 2, 'SSJT' );
    plot_Validation( hMainFig, hPlotAxes, 3, 'SPDC' );
    % Set the pointer
    % ---------------
    set( hMainFig, 'Pointer', 'arrow');

Yves Gouriou's avatar
Yves Gouriou committed
  end

%% InterpLinearCallback ...............................Interpolation Linear
  %------------------------------------------------------------------------
  % Callback function run when 
  %----------------------------------------------------------------------
  function InterpPosLinearCallback( hObject, eventdata)

    % Get the time limits for the correction A TESTER
    % --------------------------------------
    dateMin = datenum(get( hetDateMin, 'String'), 'yyyy-mm-dd HH:MM:SS');
    dateMax = datenum(get( hetDateMax, 'String'), 'yyyy-mm-dd HH:MM:SS');
    
    error = interpPosLinear( hMainFig, dateMin, dateMax );
    
Yves Gouriou's avatar
Yves Gouriou committed
    switch error

      case 1

        % Plot in the 3 axes
        % ------------------
        plot_Interpolation( hMainFig, hPlotAxes, 2 );
        plot_Interpolation( hMainFig, hPlotAxes, 3 );
Yves Gouriou's avatar
Yves Gouriou committed

      case -1
        msgbox( 'Date limits are not correct',...
          'Correction module', 'warn', 'modal');
    end


  end

%% InterpOtherCallback ...............................Interpolation OTher
  %----------------------------------------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
  % Callback function run when 
  %----------------------------------------------------------------------
  function InterpPosOtherCallback( hObject, eventdata)

    msgbox( 'Method not yet implemented', ...
            'Function InterpOtherCallback', ...
            'warn',...
            'modal' );
  end
%% InterpPosCancelCallback ..........................Cancel Interpolation
  %----------------------------------------------------------------------
  function InterpPosCancelCallback(hObject, eventdata)
    % Callback function run when
    
    % Get tsg application data
    % ------------------------
    tsg = getappdata( hMainFig, 'tsg_data' );
    
    % Get NO_CONTROL and INTERPOLATED_VALUE codes
    % -------------------------------------------
    NO_CONTROL         = get(tsg.qc.hash, 'NO_CONTROL', 'code');
    INTERPOLATED_VALUE = get(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
  %----------------------------------------------------------------------
  % Callback function run when 
  %----------------------------------------------------------------------
  function Cal_OnMenuCallback( hObject, eventdata)
   
Yves Gouriou's avatar
Yves Gouriou committed
    % Activate or desactivate uipanels
    % --------------------------------
    set( hpCalCoef,         'Visible', 'on' );
    set( hbgParameter,      'Visible', 'off');
    set( hbgQc,             'Visible', 'off');
Yves Gouriou's avatar
Yves Gouriou committed
    % ----------
    set( hQCToggletool,     'enable', 'off' );
    set( hClimToggletool,   'enable', 'off' );
    set( hBottleToggletool, 'enable', 'off' );
Yves Gouriou's avatar
Yves Gouriou committed
    set( hInterpToggletool, 'enable', 'off' );
    set( hHeaderPushtool,   'enable', 'off' );

    set( hPanToggletool,       'state', 'off', 'enable', 'on' );
    set( hTimelimitToggletool, 'state', 'off', 'enable', 'off' );
    set( hZoomToggletool,      'state', 'off', 'enable', 'on' );
        
    % Get tsg application data
    % ------------------------
    tsg = getappdata( hMainFig, 'tsg_data' );
    
    % pour test
    tsg.SSTP = [];
    setappdata( hMainFig, 'tsg_data', tsg );
   
    % 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' );
    
    % Write the Cal Coef in the Editable uicontrol
    % --------------------------------------------
    set( hetCalCNDCSlope,  'String', num2str(tsg.CNDC_LINCOEF(1)));
    set( hetCalCNDCOffset, 'String', num2str(tsg.CNDC_LINCOEF(2)));
    set( hetCalSSJTSlope,  'String', num2str(tsg.SSJT_LINCOEF(1)));
    set( hetCalSSJTOffset, 'String', num2str(tsg.SSJT_LINCOEF(2)));
    set( hetCalSSTPSlope,  'String', num2str(tsg.SSTP_LINCOEF(1)));
    set( hetCalSSTPOffset, 'String', num2str(tsg.SSTP_LINCOEF(2)));
  end


%% Cal_OffMenuCallback ..................................... Calibration
  %----------------------------------------------------------------------
  % Callback function run when 
  %----------------------------------------------------------------------
  function Cal_OffMenuCallback( hObject, eventdata)
  
Yves Gouriou's avatar
Yves Gouriou committed
    % Activate or desactivate uipanels
    % --------------------------------
    set( hpCalCoef,         'Visible', 'off' );
    set( hbgParameter,      'Visible', 'on');
    
    % Enable Pushbuttons
    % ------------------
    set( hClimToggletool,   'enable', 'on' );
    set( hBottleToggletool, 'enable', 'on' );
Yves Gouriou's avatar
Yves Gouriou committed
    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( hZoomToggletool,      'state', 'off', 'enable', 'on' );
        
    % Get tsg application data
    % ------------------------
    tsg = getappdata( hMainFig, 'tsg_data' );
    
    % Save the calibration coefficient
    % --------------------------------
    tsg.CNDC_LINCOEF(1) = str2num(get( hetCalCNDCSlope,  'String'));
    tsg.CNDC_LINCOEF(2) = str2num(get( hetCalCNDCOffset, 'String'));
    tsg.SSJT_LINCOEF(1) = str2num(get( hetCalSSJTSlope,  'String'));
    tsg.SSJT_LINCOEF(2) = str2num(get( hetCalSSJTOffset, 'String'));
    tsg.SSTP_LINCOEF(1) = str2num(get( hetCalSSTPSlope,  'String'));
    tsg.SSTP_LINCOEF(2) = str2num(get( hetCalSSTPOffset, 'String'));
        
    % Save tsg application data
    % --------------------------
    setappdata( hMainFig, 'tsg_data', tsg );
    
    % Draw the 3 plots of the validation figure 
    % -----------------------------------------
    plot_Validation( hMainFig, hPlotAxes, 1, tsg.preference.parameter );
    plot_Validation( hMainFig, hPlotAxes, 2, 'SSJT' );
    plot_Validation( hMainFig, hPlotAxes, 3, 'SPDC' );
%% CalibrateCallback .......................................... Calibration
  function CalibrateCallback(hObject, eventdata)
    % Callback function run when
    
    % Get tsg application data
    % ------------------------
    tsg = getappdata( hMainFig, 'tsg_data' );
    
Yves Gouriou's avatar
Yves Gouriou committed
    % Get the calibration coefficients. 
    % They will be used in the function calibration
    % ---------------------------------------------
    tsg.CNDC_LINCOEF(1) = str2num(get( hetCalCNDCSlope,  'String'));
    tsg.CNDC_LINCOEF(2) = str2num(get( hetCalCNDCOffset, 'String'));
    tsg.SSJT_LINCOEF(1) = str2num(get( hetCalSSJTSlope,  'String'));
    tsg.SSJT_LINCOEF(2) = str2num(get( hetCalSSJTOffset, 'String'));
    tsg.SSTP_LINCOEF(1) = str2num(get( hetCalSSTPSlope,  'String'));
    tsg.SSTP_LINCOEF(2) = 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
    % -------------------------------------------------------------------
    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(hObject, eventdata)
    % Callback function run when
    
    % Get tsg application data
    % ------------------------
    tsg = getappdata( hMainFig, 'tsg_data' );
        
    tsg.SSPS_CAL = [];
    tsg.SSJT_CAL = [];
    tsg.SSTP_CAL = [];
    
    % Save tsg application data
    % --------------------------
    setappdata( hMainFig, 'tsg_data', tsg );
    
    % Update the Adjusted variables (SSPS - SSJT) with calibrated records
    % -------------------------------------------------------------------
    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
%% Zoom_OffMenuCallback
  %----------------------------------------------------------------------
  % Callback function run when the zoom toggle toolbar is released
  %----------------------------------------------------------------------
  function Zoom_OffMenuCallback(hObject, eventdata)
    
    % turns interactive zooming off
    % -----------------------------
    zoom off;
Yves Gouriou's avatar
Yves Gouriou committed
 
    % 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
    % Turns off the automatic adaptation of date ticks
    % ------------------------------------------------
    zoomAdaptiveDateTicks('off');
    
  %----------------------------------------------------------------------
  % Callback function run when the zoom toggle toolbar is released
  %----------------------------------------------------------------------
  function Zoom_OnMenuCallback(hObject, eventdata)
    
    % cursor back to normal
    % ---------------------
    set(hMainFig,'Pointer','arrow');
    
    % returns a zoom mode object for the figure hMainFig handle
    % --------------------------------------------------------
    % Disallows a zoom operation on the MAP axes objects
    % --------------------------------------------------
    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');
    % Turns on the automatic adaptation of date ticks
    % -----------------------------------------------
    zoomAdaptiveDateTicks('on');
 
    % Desactivate some toggle buttons
    % -------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
    set( hQCToggletool,        'state', 'off' );
    set( hPanToggletool,       'state', 'off' );
    set( hTimelimitToggletool, 'state', 'off' );
  %----------------------------------------------------------------------
  % Callback function run when the pan toggle toolbar is released
  %----------------------------------------------------------------------
  function Pan_OffMenuCallback(hObject, eventdata)
    
    % turns interactive pan off
    % -------------------------
    pan off;
    
Yves Gouriou's avatar
Yves Gouriou committed
    % 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
 
    % Turns off the automatic adaptation of date ticks
    % ------------------------------------------------
    panAdaptiveDateTicks('off');
  %----------------------------------------------------------------------
  % Callback function run when the pan toggle toolbar is selected
  %----------------------------------------------------------------------
  function Pan_OnMenuCallback(hObject, eventdata)
    
    % cursor back to normal
    % ---------------------
    set(hMainFig,'Pointer','arrow');
    
    % Returns a pan mode object for the figure handle
    % -----------------------------------------------
    % Disallows a pan operation on the MAP axes objects
    % --------------------------------------------------
    setAllowAxesPan(hPan, hPlotAxes(4), false);
Yves Gouriou's avatar
Yves Gouriou committed

    % turns on interactive pan (same effect than pan on) but prevent
    % side effect on another figure
Yves Gouriou's avatar
Yves Gouriou committed
    % --------------------------------------------------------------
    set(hPan,'enable', 'on');
    
    % Turns on the automatic adaptation of date ticks
    % -----------------------------------------------
    panAdaptiveDateTicks('on');
    % Desactivate some toggle buttons
    % -------------------------------
    set( hQCToggletool,   'state', 'off' );
    set( hZoomToggletool, 'state', 'off' );
    set( hTimelimitToggletool, 'state', 'off' );
%% QC_OnMenuCallback ............................... Quality Control Module
  %----------------------------------------------------------------------
  % Callback function run when the QC toggle tool is pressed
  %----------------------------------------------------------------------
  function QC_OnMenuCallback(gcbo, eventdata)
    
   PARA = tsg.preference.parameter;
 
    % Make the QC code uipanel visible
    % --------------------------------
    set( hbgQc,                'Visible' ,'on');
    set( hbgParameter,         'Visible', 'off');
    
Yves Gouriou's avatar
Yves Gouriou committed
    % Desactivate toggletools.
    % ------------------------
    set( hZoomToggletool,       'state', 'off' );
    set( hPanToggletool,        'state', 'off' );
    set( hTimelimitToggletool,  'state', 'off' );
    set( hCalToggletool,        'state', 'off' );
Yves Gouriou's avatar
Yves Gouriou committed
    set( hInterpToggletool,     'state', 'off' );
Yves Gouriou's avatar
Yves Gouriou committed

    % Activate right clic context menu on first axes (salinity)
    % ---------------------------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
    set(hPlotAxes(1),'UIContextMenu', hQcCmenu);
    % Activate clic mouse menu on first axes (salinity) for next rbbox
    % ----------------------------------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
    set(hPlotAxes(1),'ButtonDownFcn', @QC_SelectCallback);
    % change cursor to crosshair aspect
    % ---------------------------------
    % -------------------------------------------------------------
    % nested function on mouse clic when QC toggle tool is selected
    % -------------------------------------------------------------
    function QC_SelectCallback(gcbo, eventdata)
      % disable ButtonMotion on main fig during select
      % ----------------------------------------------
      set( hMainFig, 'WindowButtonMotionFcn', []);
      % Retrieve named application data
      % -------------------------------
      tsg = getappdata( hMainFig, 'tsg_data');
      % Selection of the data within the figure
      % ---------------------------------------
      point1    = get(gca,'CurrentPoint');    % button down detected
      finalRect = rbbox;                      % return figure units
      point2    = get(gca,'CurrentPoint');    % button up detected
      point1 = point1(1,1:2);                 % extract x and y
      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') is called
      % -----------------------------------------------------------------
      if ~strcmp( get(gcf, 'SelectionType'), 'alt')

        % 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
          % --------------------------
          ind = find(tsg.DAYD_SPL            > p1(1,1) &...
                     tsg.DAYD_SPL            < p2(1,1) & ...
                     tsg.([PARA '_SPL_DIF']) > p1(1,2) &...
                     tsg.([PARA '_SPL_DIF']) < p2(1,2));

          % Keep the information on the indices of the selected zone
          % --------------------------------------------------------
          tsg.rbboxind = ind .* ones(size(ind));
          
          % Modifiy the QC
          % --------------
          tsg.([PARA '_SPL_QC'])(ind) = tsg.qc.active.Code;

          % Save the modifications
          % ----------------------
          setappdata( hMainFig, 'tsg_data', tsg);
          
          % Update sample QC in the _WS and _EXT records
          % --------------------------------------------
          updateSampleQC( hMainFig, PARA );

          % plot selected data with selected code
          % --------------------------------------
          plot_Correction( hMainFig, hPlotAxes, PARA);

        else

          % get index on selected zone
          % ---------------------------
          ind = find(tsg.DAYD   > p1(1,1) & tsg.DAYD   < p2(1,1) & ...
                     tsg.(PARA) > p1(1,2) & tsg.(PARA) < p2(1,2));

          % Keep the information on the indices of the selected zone
          % --------------------------------------------------------
          tsg.rbboxind = ind .* ones(size(ind));
          
          % Modifiy the QC
          % --------------
          tsg.([PARA '_QC'])(ind) = tsg.qc.active.Code;

          % put last SSPS_QC in queue (Undo/Redo)
          % -------------------------------------
          tsg.queue = push( tsg.queue, tsg.([PARA '_QC']));

          % store QC in queue object, for undo
          % ----------------------------------
          tsg.queue = push(tsg.queue, tsg.([PARA '_QC']));

          % Save the modifications
          % ----------------------
          setappdata( hMainFig, 'tsg_data', tsg);

          % Draw plot 1 of the validation figure 
          % ------------------------------------
          plot_Validation( hMainFig, hPlotAxes, 1, PARA );

          % refresh QC statistic panel
          % --------------------------
          display_QC( hMainFig, hPlotAxes );

          % enable undo menu
          % ----------------
          set(findobj('tag','UIMENU_UNDO'),'enable','on');
        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);
%% QC_OffMenuCallback .............................. Quality Control Module
  %----------------------------------------------------------------------
  % Callback function run when the QC toggle tool is released
  %----------------------------------------------------------------------
  function QC_OffMenuCallback(gcbo, eventdata)

    % 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' );
    
    % 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
    % cursor back to normal
    % ---------------------
    set(hMainFig,'Pointer','arrow');
    
  end

%% Qc context menu selected ........................ Quality Control Module
  %-----------------------------------------------------------
  % Callback function run when the QC context menu is selected
  %
  % Context menu used to attribute a QC to the TSG time series
  %
  %-----------------------------------------------------------
  function Qc(hObject, eventdata, key)
    % Retrieve Default Quality Code and Color
    % ---------------------------------------
    tsg    = getappdata( hMainFig, 'tsg_data');
    
    % Get the parameter (SSPS, SSJT or SSTP)
    % --------------------------------------
    PARA = tsg.preference.parameter;
    % get key and some values in hashtable
    % ------------------------------------
    code  = get(tsg.qc.hash, key, 'code');
    color = get(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 ~isempty( tsg.rbboxind )

      if strcmp( get(hBottleToggletool, 'state'), 'on')
        tsg.([PARA '_SPL_QC'])(tsg.rbboxind) = tsg.qc.active.Code;
Yves Gouriou's avatar
Yves Gouriou committed
        % Save tsg.SSPS_QC and tsg.qc.active.Code 
        % in the application data
        % ----------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
        setappdata( hMainFig, 'tsg_data', tsg );
        % Draw the 3 plots of the Correction figure
        % -----------------------------------------
        plot_Correction( hMainFig, hPlotAxes, PARA );
        tsg.([PARA '_QC'])(tsg.rbboxind) = tsg.qc.active.Code;
        % Save tsg.SSPS_QC in the application data
        % ----------------------------------------
        setappdata( hMainFig, 'tsg_data', tsg );
     
        % Draw plot 1 of the validation figure
        % ------------------------------------
        plot_Validation( hMainFig, hPlotAxes, 1, PARA );
    % Update the radio button corresponding to the selected
    % QC context menu (rbg : RadioButtonGroup)
    % -----------------------------------------------------
    radioTag     = ['TAG_QC_RADIO_' 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

    setappdata( hMainFig, 'tsg_data', tsg );
%% Radiobutton Quality Control ..................... Quality Control Module
  % ---------------------------------------------------------------
  % Callback to select CODE and COLOR QC from the RadioButton Group
  % ---------------------------------------------------------------
  function RadiobuttonQc(source, eventdata)
        
      % Retrieve Default Quality Code and Color
      % ---------------------------------------
      tsg = getappdata( hMainFig, 'tsg_data');
      % Retrieve the key from the active RadioButton
      % --------------------------------------------
      rbTag = get(eventdata.NewValue,'Tag');
      key = strrep(rbTag, 'TAG_QC_RADIO_', '');
      % store his handle to uibuttongroup userdata
      % ------------------------------------------
      set(hbgQc,'Userdata', eventdata.NewValue);
      
      % get key and some values in hashtable
      % ------------------------------------
      code  = get(tsg.qc.hash, key, 'code');
      color = get(tsg.qc.hash, key, 'color');
       
      % set active code and color from selected context menu
      % ----------------------------------------------------
      tsg.qc.active.Code  = code;
      tsg.qc.active.Color = color;

      setappdata( hMainFig, 'tsg_data', tsg );

  end

%% Radiobutton Parameter choice ..................... Parameter choice
  % ---------------------------------------------------------------
  % Callback to select CODE and COLOR QC from the RadioButton Group
  % ---------------------------------------------------------------
  function RadiobuttonParameter(source, eventdata)

    % If the map is visible, hide it
    % ------------------------------
    set( hMapPanel, 'visible' ,'off' );
    set( hMapToggletool, 'state', 'off');
    
    % Retrieve Default Quality Code and Color
    % ---------------------------------------
    tsg = getappdata( hMainFig, 'tsg_data');

    % Retrieve the key from the active RadioButton
    % --------------------------------------------
    rbTag_Old = get(eventdata.OldValue,'Tag');
    rbTag     = get(eventdata.NewValue,'Tag');
    PARA = strrep(rbTag, 'TAG_PARA_RADIO_', '');

    % Test if PARA exists
    % -------------------
    ind = find( isnan(tsg.(PARA)) == 0 );
    if ~isempty( tsg.(PARA) ) & ~isempty (ind )

      % store his handle to uibuttongroup userdata
      % ------------------------------------------
      set(hbgParameter,'Userdata', eventdata.NewValue);

      tsg.preference.parameter = PARA;

      setappdata( hMainFig, 'tsg_data', tsg );

      % Get sample data
      % ---------------
      tsg_mergesample( hMainFig, PARA );

      % The smooth TSG time series (tsg_moveaverage) is
      % no more computed : too long
      % We now computed a smooth value only at the position of the sample
      % in diffTsgSample
      % -----------------------------------------------------------------
      %tsg_moveaverage(hMainFig, PARA);

      % Draw the 3 plots of the validation figure
      % -----------------------------------------
      resetAxes( hMainFig, hPlotAxes )
      plot_Validation( hMainFig, hPlotAxes, 1, PARA );
      plot_Validation( hMainFig, hPlotAxes, 2, 'SSJT' );
      plot_Validation( hMainFig, hPlotAxes, 3, 'SPDC' );

    else
      msgbox( ['No ' PARA ' data in the file'], 'Parameter choice', 'warn', 'modal');

      % Set the old radio-button active
      % -------------------------------
      hbgChildren  = get(hbgParameter,'Children');        % Get the handles of the rbg
      hbgTagCell   = get(hbgChildren, 'tag');             % get the TAG of the rbg
      ind          = strcmp(rbTag_Old, hbgTagCell);       % Compare the TAG
      hRadioButton = hbgChildren( ind == 1 );             % Get the handle of the button
      set(hbgParameter, 'SelectedObject', hRadioButton);  % Make this button active