Skip to content
Snippets Groups Projects
tsgqc.m 112 KiB
Newer Older
    % 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( hTimelimitToggletool,  'state', '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
    % cursor back to normal
    % ---------------------
    set(hMainFig,'Pointer','arrow');
  end

%% Qc context menu selected ........................ Quality Control Module
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%-----------------------------------------------------------
% 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)
    % --------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    PARA = tsg.plot.parameter{1};
    SAMPLE = tsg.plot.sample;
    % 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 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
        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 );
      end

    end

    % 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 );

  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, eventdata)

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(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

%% 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
    % restacks the figure to the top of the screen
    % --------------------------------------------
    figure( hMainFig);
    set(hQCToggletool, 'state', 'on' );

jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%---------------------------------------------------------------------
% Callback function run when mouse pointer is moving on temperature plot
% draw corresponding measurement position on map
%---------------------------------------------------------------------
  function MouseMotion(hObject, eventdata)

    % Test if the callback can be activated
    % -------------------------------------
    if strcmp( get( hMainFig, 'UserData'), 'ButtonMotionOn')

      % Retrieve named application data
      % -------------------------------
      tsg = getappdata( hMainFig, 'tsg_data');

      % Get current position of cusor and return its coordinates in
      % axes with handle h_axes
      % -----------------------------------------------------------
      a = get(hPlotAxes(1), 'CurrentPoint');
      x = a(2,1);
      y = a(2,2);
      % Get the Limits of axes 1
      % ------------------------
      limx = get(hPlotAxes(1), 'XLim');
      limy = get(hPlotAxes(1), 'YLim');

      % Code to Activate the PAN function when QC mode is active
      % A PAN zone is defined in the bottom (10%) of PlotAxes(1)
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
      % 2 callback are needed :
      %    1 - one to desactivate QC when Pan is set to on.
      %    2 - one to reactivate QC once the pan has been used.
      % ---------------------------------------------------------
      qcState = get(hQCToggletool,   'state' );
      if strcmp(qcState, 'on' );
        % Suppose that Y axes is increasing from the bottom to the top
        % ------------------------------------------------------------
        limy2 = limy(1) + (limy(2)-limy(1)) * 0.10;

        if  x > limx(1) && x < limx(2) &&  y <= limy2 && y >= limy(1)
          hPan = pan(hMainFig);
          set(hPan,'ActionPreCallback',  @preQcPanCallback);
          set(hPan,'ActionPostCallback', @postQcPanCallback);
          set(hPan,'Enable','on');
        else
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
          pan(hMainFig, 'off');
      % Dynamically display data in uicontrol
      % -------------------------------------
      if x > tsg.DAYD(1) && x < tsg.DAYD(end)
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
        %if  x > limx(1) && x < limx(2)

        indCursor = find( tsg.DAYD > x);
        set( hInfoDateText, 'String',...
          datestr(tsg.DAYD(indCursor(1)),'dd/mm/yyyy   HH:MM'));
        set( hInfoLatText,  'String', dd2dm(tsg.LATX(indCursor(1)),0));
        set( hInfoLongText, 'String', dd2dm(mod(tsg.LONX(indCursor(1))+180,360)-180,1));
        if ~isempty(tsg.SSPS)
          set( hInfoSSPSText, 'String', tsg.SSPS(indCursor(1)));
        end
        if ~isempty(tsg.SSJT)
          set( hInfoSSJTText, 'String', tsg.SSJT(indCursor(1)));
        end
        if ~isempty(tsg.SSTP)
          set( hInfoSSTPText, 'String', tsg.SSTP(indCursor(1)));
        end

        % Plot the position on the map if this one is active
        % --------------------------------------------------
        if strcmp( get(hMapFig, 'Visible'), 'on')

          % Select the map axes
          % -------------------
          axes( hPlotAxes(4));

          % Look for a MARKER (Red point) on the ship track
          % -----------------------------------------------
          h = findobj( hPlotAxes(4), '-regexp','Tag', 'TAG_PLOT4_LINE_MARKER');
          if isempty( h )   % if no Marker
            if ~isnan( tsg.LONX(indCursor(1)) )
              % Plot a Marker (Red point) on the ship track
              % -------------------------------------------
              hMarker = m_line( tsg.LONX(indCursor(1)), tsg.LATX(indCursor(1)),...
                'Marker','o','MarkerSize',5, ...
                'Color','r', 'MarkerFaceColor','r');
              % Put a tag on the Marker - This tag allows to get the handle
              % of the Marker
              % -----------------------------------------------------------
              set( hMarker, 'TAG', 'TAG_PLOT4_LINE_MARKER' );
          else         % a marker exists
            if ~isnan( tsg.LONX(indCursor(1)) )

              % Delete the Marker and redraw it
              % -------------------------------
              delete( h);
              hMarker = m_line( ...
                tsg.LONX(indCursor(1)), tsg.LATX(indCursor(1)),...
                'Marker','o','MarkerSize',5, ...
                'Color','r', 'MarkerFaceColor','r');
              % Put a tag on the Marker - This tag allows to get the handle
              % of the Marker
              % -----------------------------------------------------------
              set( hMarker, 'TAG', 'TAG_PLOT4_LINE_MARKER' );
        end
      end
    end
  end

%% Map_OnMenuCallback
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%---------------------------------------------------------------------
% Callback function run when the Map tool bar item is selected
%---------------------------------------------------------------------
  function Map_OnMenuCallback(hObject, eventdata)
    % Desactivate Zoom and Pan toggle buttons
    % ---------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    set(hZoomInToggletool,  'state', 'off' );
    set(hZoomOutToggletool,  'state', 'off' );
    set(hPanToggletool,   'state', 'off' );

    % Make the earth map visible
    % --------------------------
    set(hMapFig, 'Visible', 'on' );
    erase_Line( hPlotAxes, 4 );

    plot_map( hMainFig, hPlotAxes)
%% Map_OffMenuCallback
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%---------------------------------------------------------------------
% Callback function run when the Map tool bar item is unselected
%---------------------------------------------------------------------
  function Map_OffMenuCallback(hObject, eventdata)
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed

    % Make the earth map invisible
    % ----------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    set(hMapFig, 'Visible', 'off' );
%% Bottle_OnMenuCallback ................................ Correction Module
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%---------------------------------------------------------------------
% Callback function run when the bootle push tool is selected
%---------------------------------------------------------------------
  function Bottle_OnMenuCallback(hObject, eventdata)
    % Callback function run when the Bottle tool bar item is selected

    % Get the tsg structure
    % ---------------------
    tsg = getappdata(hMainFig, 'tsg_data');
    % Get the parameter we are working on (SSPS, SSJT, SSTP)
    % ------------------------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    PARA   = tsg.plot.parameter{1};
    SAMPLE = tsg.plot.sample;
    % Switch somme buttons
    % --------------------
    set( hQCToggletool,         'state',  'off' );
    set( hZoomInToggletool,     'state', 'off', 'enable', 'on' );
    set( hZoomOutToggletool,    'state', 'off', 'enable', 'on' );
    set( hPanToggletool,        'state',  'off' );
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    %     set( hMapToggletool,        'state',  'off' );
    set( hClimToggletool,       'state',  'off', 'enable', 'off');
    set( hCalToggletool,        'enable', 'off' );
    set( hInterpToggletool,     'enable', 'off' );
    set( hTimelimitToggletool,  'enable', 'on' );

    % Activate or desactivate uipanels
    % ---------------------------------
    set( hpDateLimit,           'Visible', 'on' );
    set( hbgCorMethod,          'Visible', 'on' );
    set( hCorPanel,             'Visible', 'on' );
    set( hbgParameter,          'Visible', 'off');
    set( hbgQc,                 'Visible', 'off');
    if ~isempty( tsg.([SAMPLE '_EXT']) )
      % Compute the sample-TSG differences
      % ----------------------------------
      diffTsgSample( hMainFig, PARA );
    else

      msgbox('No sample data file has been read', 'modal');
    % Plot in the 3 axes
    % ------------------
    plot_Correction( hMainFig, hPlotAxes, PARA );
    % 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));

  end

%% Bottle_OffMenuCallback ............................... Correction module
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%---------------------------------------------------------------------
% Callback function run when the bootle push tool is selected
%---------------------------------------------------------------------
  function Bottle_OffMenuCallback(hObject, eventdata)
    % If necessary toggle off some buttons
    % ------------------------------------
    set( hQCToggletool,        'state',  'off' );
    set( hZoomInToggletool,    'state',  'off' );
    set( hZoomOutToggletool,   'state',  'off' );
    set( hPanToggletool,       'state',  'off' );
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    %     set( hMapToggletool,       'state',  'off' );
    set( hClimToggletool,      'enable', 'on');
    set( hCalToggletool,       'enable', 'on'  );
    set( hInterpToggletool,    'enable', 'on' );
    set( hTimelimitToggletool, 'enable', 'off' );
    % Activate or Desactivate uipanel
    % --------------------------------
    set( hpDateLimit,          'Visible', 'off' );
    set( hCorPanel,            'Visible', 'off' );
    set( hbgCorMethod,         'Visible', 'off' );
    set( hbgParameter,         'Visible', 'on');

    % Desactivate Click Mouse on figure
    % ---------------------------------
    set( hMainFig, 'WindowButtonDownFcn', []);
    % Get tsg structure
    % -----------------
    tsg  = getappdata( hMainFig, 'tsg_data' );
    % Get the parameter (SSPS, SSJT, SSTP)
    % ------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % PARA = tsg.plot.parameter;

    % 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} );
    % Set the pointer
    % ---------------
    set( hMainFig, 'Pointer', 'arrow');

  end

%% CorCancelCallback .................................... Correction Module
  function CorCancelCallback(hObject, eventdata)
    % Callback function run when ...
    % Desactivate somme Toggle button
    % -------------------------------
    set( hZoomInToggletool,       'state', 'off' );
    set( hZoomOutToggletool,       'state', 'off' );
    set( hPanToggletool,        'state', 'off' );
    set( hQCToggletool,         'state', 'off' );
    set( hTimelimitToggletool,  'state', 'off' );

    % Get tsg application data
    % ------------------------
    tsg = getappdata(hMainFig, 'tsg_data');

    % Get parameter we are working on (SSPS, SSJT, SSTP)
    % --------------------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    PARA = tsg.plot.parameter{1};
Yves Gouriou's avatar
Yves Gouriou committed
    % To cancel the correction set the ADJUSTED variable
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % to []
    % --------------------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
    tsg.([PARA '_ADJUSTED']) = [];
    tsg.([PARA '_ADJUSTED_QC']) = [];
    tsg.([PARA '_ADJUSTED_ERROR']) = [];

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

    % Set the ADJUSTED variable either to the raw variable or to
    % the calibrated variable
    % ----------------------------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
    % updateAdjustedVariable( hMainFig );

    % Plot in the 3 axes
    % ------------------
    plot_Correction( hMainFig, hPlotAxes, PARA );

  end

%% CorLinearCallback .................................... Correction Module
  function CorLinearCallback(hObject, eventdata)
    % Callback function run when

    % Desactivate somme Toggle button
    % -------------------------------
    set( hZoomInToggletool,     'state', 'off' );
    set( hZoomOutToggletool,    'state', 'off' );
    set( hPanToggletool,        'state', 'off' );
    set( hQCToggletool,         'state', 'off' );
    set( hTimelimitToggletool,  'state', 'off' );

    % 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');
    % Get the parameter (SSPS, SSJT, or SSTP)
    % ---------------------------------------
    tsg    = getappdata(hMainFig, 'tsg_data');
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    PARA   = tsg.plot.parameter{1};
    SAMPLE = tsg.plot.sample;

    if ~isempty( tsg.([SAMPLE '_EXT']) )
      % Compute the sample-TSG differences
      % ----------------------------------
      diffTsgSample( hMainFig, PARA );
      % Correction
      % ----------
      error = corTsgLinear(hMainFig, PARA, dateMin, dateMax);
          % Plot in the 3 axes
          % ------------------
          plot_Correction( hMainFig, hPlotAxes, PARA );

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

  end

%% CorMedianCallback .................................... Correction Module
  function CorMedianCallback(hObject, eventdata)
    % Callback function run when

    % Desactivate somme Toggle button
    % -------------------------------
    set( hZoomInToggletool,       'state', 'off' );
    set( hZoomOutToggletool,       'state', 'off' );
    set( hPanToggletool,        'state', 'off' );
    set( hQCToggletool,         'state', 'off' );
    set( hTimelimitToggletool,  'state', 'off' );

    % 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');
    % Get the parameter (SSPS, SSJT, or SSTP)
    % ---------------------------------------
    tsg    = getappdata(hMainFig, 'tsg_data');
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    PARA   = tsg.plot.parameter{1};
    SAMPLE = tsg.plot.sample;

    if ~isempty( tsg.([SAMPLE '_EXT']) )
      % Compute the sample-TSG differences
      % ----------------------------------
      diffTsgSample( hMainFig, PARA );
      % Correction
      % ----------
      error = corTsgMedian(hMainFig, PARA, dateMin, dateMax);
          % Plot in the 3 axes
          % ------------------
          plot_Correction( hMainFig, hPlotAxes, PARA );

          % Nothing is done - Error msg within the corTsgMedian function
%% PopupMenu Select Parameter
% ---------------------------
  function SelectParameter(hObject, eventdata, nplot)
    % Callback function run when the ....

    % Get application data
    % --------------------
    tsg = getappdata( hMainFig, 'tsg_data');

    % Get the active string of the selected box
    % -----------------------------------------
    val             = get( hObject, 'Value' );
    string_list     = get( hObject, 'String' );
    selected_string = string_list{val};

    % Get the default parameter
    % -------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    tsg.plot.parameter{nplot} =  selected_string;
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
      tsg.plot.sample = selected_string;
      if strcmp( tsg.plot.sample, 'SSJT' )
        tsg.plot.sample = 'SSTP';

    % Save application data
    % --------------------
    setappdata( hMainFig, 'tsg_data', tsg);
    % Disable the climatology
    % -----------------------
    plotClim = 0;
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    if strcmp( get(hClimToggletool, 'state'), 'on' )
      set( hClimToggletool, 'state', 'off' );
      plotClim = 1;
    end
    % Disable the climatology
    % -----------------------
    set( hClimToggletool, 'state', 'off' );

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} );

    % Plot the climatology if it was already plotted
    % ----------------------------------------------
    if plotClim
      set( hClimToggletool, 'state', 'on' );
      plot_Climatology(hMainFig, hPlotAxes);
%% SelectTime_OnMenuCallback
%---------------------------
  function SelectTime_OnMenuCallback(hObject, eventdata)
    % Callback function run when the ....
    % Desactivate Zoom and Pan functions.
    % ----------------------------------
    set( hZoomInToggletool, 'state', 'off' );
    set( hZoomOutToggletool, 'state', 'off' );
    set( hQCToggletool,   'state', 'off' );
    set( hPanToggletool,  'state', 'off' );
    % Create a pointer to select the time limits
    % ------------------------------------------
    selTimePointer = ones(16)+1;
    selTimePointer(1,:)       = 1; selTimePointer(16,:)      = 1;
    selTimePointer(:,1)       = 1; selTimePointer(:,16)      = 1;
    selTimePointer(1:4,8:9)   = 1; selTimePointer(13:16,8:9) = 1;
    selTimePointer(8:9,1:4)   = 1; selTimePointer(8:9,13:16) = 1;
    selTimePointer(5:12,5:12) = NaN; % Create a transparent region in the center
    % Activate clic mouse menu on second axes (salinity) for next rbbox
    % ----------------------------------------------------------------
    set(hMainFig,'WindowButtonDownFcn', @Time_SelectCallback);

    % change cursor
    % ---------------
    set( hMainFig, 'Pointer', 'custom',...
      'PointerShapeCData', selTimePointer, 'PointerShapeHotSpot',[9 9]);

    % ----------------------------------------------------------------------
    % nested function on mouse clic when Select Time toggle tool is selected
    % ----------------------------------------------------------------------
    function Time_SelectCallback(gcbo, eventdata)

      % disable ButtonMotion on main fig during select
      % prevent drawing to map
      % ----------------------------------------------
      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
      point2 = point2(1,1:2);

      p1 = min(point1,point2);
      p2 = max(point1,point2);                % calculate locations

      % get index on selected zone - Only on X axes (time)
      % --------------------------------------------------
      ind = find(tsg.DAYD >= p1(1,1) & tsg.DAYD <= p2(1,1));

      % Write the date in the Editable uicontrol
      % ----------------------------------------
      set( hetDateMin, 'String', datestr(tsg.DAYD(ind(1)),   31));
      set( hetDateMax, 'String', datestr(tsg.DAYD(ind(end)), 31));

      % enable ButtonMotion on main fig after select QC area
      % ----------------------------------------------------
      set( hMainFig, 'WindowButtonMotionFcn', @MouseMotion);
    end
  end

%% SelectTime_OffMenuCallback
%----------------------------
  function SelectTime_OffMenuCallback(hObject, eventdata)
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % Callback function run when the ....
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % Desactivate time limit buttons
    % ------------------------------
    set( hTimelimitToggletool, 'state', 'off');
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    set( hMainFig, 'WindowButtonDownFcn', []);

    set( hMainFig, 'Pointer', 'arrow');

  end

%% Clim_OffMenuCallback
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%------------------------------------------------------------------------
% Callback function run when the Levitus climatology toolbar is unselected
%------------------------------------------------------------------------
  function Clim_OffMenuCallback(hObject, eventdata)

    % Get lines handles from tag
    % --------------------------
    hLines = findobj('-regexp', 'Tag', 'TAG_LINE_CLIMATO_');
    % Delete climatology lines on axes
    % ---------------------------------
    delete(hLines);
  end

%% Clim_OnMenuCallback
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%------------------------------------------------------------------------
% Callback function run when the Levitus climatology toolbar is unselected
%------------------------------------------------------------------------
  function Clim_OnMenuCallback(hObject, eventdata)

    % Test if the TSG and bucket files have been read
    % -----------------------------------------------
    if strcmp( get(hOpenMenu, 'UserData'), 'on' )

      % plot climatology
      % ----------------
      plot_Climatology(hMainFig, hPlotAxes);
    end
  end

%% ClimatoSelectMenuCallback
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
% -------------------------------------------------------------------
% Callback function run when climato submenu is selected
% -------------------------------------------------------------------
  function ClimatoSelectMenuCallback(hObject, eventdata, climato, time)
    % find all climato submenu and set 'checked' property to 'off'
    % ------------------------------------------------------------
    hdl = findobj( '-regexp', 'tag', 'TAG_UIMENU_CLIMATO');
    set(hdl, 'checked', 'off');
    % set current climato submenu checked
    % -----------------------------------
    set(hObject, 'checked', 'on');
    % memorize action on climatology menu for next use
    % ------------------------------------------------
    s.type = climato;
    s.time = time;
    set(hClimatoMenu, 'userdata', s);
    % check if climatology toggle button is set
    % -----------------------------------------
    if strcmp(get(hClimToggletool, 'state'), 'on')
      % clear last plotted climatology
      % ------------------------------
      Clim_OffMenuCallback;
      % plot and read (eventually) new climatology
      % ------------------------------------------
      plot_Climatology(hMainFig, hPlotAxes);
  end

%% PreferencesMenuCallback
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
% -------------------------------------------------------------------
% Callback function run when Option/Preference is selected
% -------------------------------------------------------------------
  function PreferencesMenuCallback(hObject, eventdata)
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed

    % call preferences form function
    % ------------------------------
    if( preferencesForm(hMainFig) )
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
      % if form is validate, update plots only if plot exist
      % ----------------------------------------------------
      if ~isempty(findobj( '-regexp', 'Tag', ('TAG_PLOT\d_LINE_')))
        SelectParameter(pmhPara(1),[],1);
      end
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%% HelpMenuCallback
% -------------------------------------------------------------------
% Callback function run when Help/Help is selected
% -------------------------------------------------------------------
  function HelpMenuCallback(hObject, eventdata)
    msgbox( 'Function Help not yet implemented', 'warn', 'modal');
  end

%% AboutMenuCallback
% -------------------------------------------------------------------
% Callback function run when Help/About is selected
% -------------------------------------------------------------------
  function AboutMenuCallback(hObject, eventdata)
    %splash('Thermo.jpg', 3000);
    aboutDialog(hMainFig, DEFAULT_PATH_FILE);
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
  end

%% HeaderMenuCallback
% -------------------------------------------------------------------
% Callback function run when the headerForm tool bar item is selected
% -------------------------------------------------------------------
  function HeaderMenuCallback(hObject, eventdata)

    % call header form function
    % -------------------------
    headerForm(hMainFig);

  end

%% PrintFigMenuCallback
% -------------------------------------------------------------------
% Callback function run when the Report tool bar item is selected
% -------------------------------------------------------------------
  function PrintFigMenuCallback(hObject, eventdata)
    % disable ButtonMotion on main fig during select
    % prevent drawing to map
    % ----------------------------------------------
    set( hMainFig, 'WindowButtonMotionFcn', []);
    % Get the tsg structure
    % ---------------------
    tsg = getappdata( hMainFig, 'tsg_data');

    % Default parameter
    % -----------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    PARA = tsg.plot.parameter{1};

    hPrintFig = figure( 'Name', 'Print','NumberTitle', 'off','Resize', 'on');
    hPlot(1) = subplot(3,1,1);
    hPlot(2) = subplot(3,1,2);
    hPlot(3) = subplot(3,1,3);
    title( hPlot(1), tsg.file.name);

    if strcmp(get(hBottleToggletool,'state'), 'on')
      plot_Correction( hMainFig, hPlot, PARA );
    elseif strcmp(get(hCalToggletool,'state'), 'on')
      plot_Calibration( hMainFig, hPlot, 1, 'SSPS' );
      plot_Calibration( hMainFig, hPlot, 2, 'SSJT' );
      plot_Calibration( hMainFig, hPlot, 3, 'SSTP' );
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
      %     elseif strcmp(get(hMapToggletool,'state'), 'on')
      %      plot_map( hMainFig, hPlot)
    elseif strcmp(get(hInterpToggletool,'state'), 'on')
      plot_Interpolation( hMainFig, hPlot, 1 );
      plot_Interpolation( hMainFig, hPlot, 2 );
      plot_Interpolation( hMainFig, hPlot, 3 );
    else
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} );

      %       I = getframe(hPlotAxes(1));
      %       imwrite(I.cdata, 'myplot.png');
    end
    for i = 1 : length( hPlot )
      set(hPlot(i), 'box', 'on','XTickMode','auto');
      datetick(hPlot(i),'x','keeplimits' );
    end
    % enable ButtonMotion on main fig after select QC area
    % ----------------------------------------------------
    set( hMainFig, 'WindowButtonMotionFcn', @MouseMotion);

  end

%% ReportMenuCallback
% -------------------------------------------------------------------
% Callback function run when the Report tool bar item is selected
% -------------------------------------------------------------------
  function ReportMenuCallback(hObject, eventdata)

    % call report function
    % --------------------
    saveReport(hMainFig);

  end

%% SaveMenuCallback
% -------------------------------------------------------------------
% Callback function run when the Save menu item is selected
% -------------------------------------------------------------------
  function SaveMenuCallback(hObject, eventdata)

    % Retrieve named application data
    % -------------------------------
    tsg = getappdata( hMainFig, 'tsg_data');

    % fill or append header form
    % -------------------------
    error = headerForm(hMainFig);

    % if user press continue button, ask for netcdf file
    % ------------------------------------------------
    if error ~= -1
      [fileName, pathName, filterindex] = uiputfile('*.nc', ...
        'Save file name', strcat(tsg.file.name, '.nc'));

      % if user press cancel button, all var are set to zero
      % ----------------------------------------------------
      if filterindex == 0
        return;
      end

      % Pointer set to watch during reading and plotting
      % ------------------------------------------------
      set( hMainFig, 'Pointer', 'watch' );

      % flushes the event queue and updates the closed uiputfile window
      % ---------------------------------------------------------------
      drawnow;

      % write netcdf file
      % -----------------
      error = writeTSGDataNetCDF(hMainFig, strcat(pathName, fileName));

      % Pointer reset to arrow
      % ----------------------
      set( hMainFig, 'Pointer', 'arrow' );

jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
      % Check for NetCDF writing error
      % must to be rewriting
      % ------------------------------
      if error == -1
        warning('tsgqc:SaveMenuCallback', ...
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
          'NetCDF writing error: %s %s', pathName, fileName);
        return;
      end

      % update the display
      % ------------------
      set( hInfoFileText, 'String', strcat(tsg.file.name, tsg.file.ext));
      % enable Quality Control mode
      % ---------------------------
      hdl_pushtool = findobj('Tag', 'QC');
      set(hdl_pushtool, 'Enable', 'on');
    end

  end

%% ExportTsgCallback
% -------------------------------------------------------------------
% Callback function run when the Export menu item is selected
% -------------------------------------------------------------------
  function ExportTsgCallback(hObject, eventdata)

    % Retrieve named application data
    % -------------------------------
    tsg = getappdata( hMainFig, 'tsg_data');
    % Desactivate MouseMotion 'off'
    % ----------------------------
    set( hMainFig, 'WindowButtonMotionFcn', []);
    % Open standard dialog box for saving files
    % -----------------------------------------
    [fileName, pathName, filterindex] = uiputfile('*.tsgqc', ...
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
      'Save file name', strcat(tsg.file.name, '.tsgqc'));

    % if user press cancel button, all var are set to zero
    % ----------------------------------------------------
    if filterindex == 0
      return;
    end

    % Pointer set to watch during reading and plotting
    % ------------------------------------------------
    set( hMainFig, 'Pointer', 'watch' );