Skip to content
Snippets Groups Projects
tsgqc.m 176 KiB
Newer Older
    tsg = getappdata( hMainFig, 'tsg_data');
    hdls = findobj( '-regexp', 'tag', 'TAG_UIMENU_MAP_RESOLUTION_');
    set(hdls, 'Checked', 'off');  % set all menu off
    set(src, 'Checked', 'on');   % set the menu on
    
    tsg.preference.map.resolution = resolution;
    % Save the modifications for use new setting
    % -----------------------------------------
    setappdata( hMainFig, 'tsg_data', tsg);
    
    % redraw map
    % ------------------------------------------
    erase_Line( hPlotAxes, 4 );
    plot_map(hMainFig, hPlotAxes);
  end

  function mapPatchMenuCallback(src, evnt, patch)
    
    % get the tsg structure
    % ---------------------
    tsg = getappdata( hMainFig, 'tsg_data');
    
    if patch
      % find map with patch submenu and set 'checked' property to 'off'
      % ------------------------------------------------------------
      hdl = findobj( '-regexp', 'tag', 'TAG_UIMENU_MAP_WITHOUT_PATCH');
      set(hdl, 'checked', 'off');
      
      % set current climato submenu checked
      % -----------------------------------
      set(src, 'checked', 'on');
      
      % set tsg.preference for next use
      % ------------------------------------------------
      tsg.preference.map.patch_value = 2;
      
    else
      hdl = findobj( '-regexp', 'tag', 'TAG_UIMENU_MAP_WITH_PATCH');
      set(hdl, 'checked', 'off');
      
      % set current climato submenu checked
      % -----------------------------------
      set(src, 'checked', 'on');
      
      % set tsg.preference for next use
      % ------------------------------------------------
      tsg.preference.map.patch_value = 1;
    
    % Save the modifications for use new setting
    % -----------------------------------------
    setappdata( hMainFig, 'tsg_data', tsg);
    
    % redraw map
    % ------------------------------------------
    erase_Line( hPlotAxes, 4 );
    plot_map(hMainFig, hPlotAxes);
    
  end

% --------------------------------
  function mapDynaBorderCallback(src, evnt, border)
    
    % get the tsg structure
    % ---------------------
    tsg = getappdata( hMainFig, 'tsg_data');
    
    hdls = findobj( '-regexp', 'tag', 'TAG_UIMENU_MAP_BORDER_');
    set(hdls, 'Checked', 'off');
    set(src, 'Checked', 'on');
    
    % change the map border from menu on map
    % --------------------------------------
    tsg.preference.map.border = border;
    setappdata( hMainFig, 'tsg_data', tsg);
    
    % redraw map
    % ------------------------------------------
    erase_Line( hPlotAxes, 4 );
    plot_map(hMainFig, hPlotAxes);
  end

% display 2D climatology on map if pushbutton climato is on
% ---------------------------------------------------------
  function mapClimatologyCallback(src, evnt, climato)
    
    % get the tsg structure
    % ---------------------
    tsg = getappdata( hMainFig, 'tsg_data');
    
    % check only the selected menu
    % -----------------------------
    hdls = findobj( '-regexp', 'tag', 'TAG_UIMENU_MAP_CLIMATOLOGY_WITH_');
    set(hdls, 'Checked', 'off');
    set(src, 'Checked', 'on');
    % change the map 2D climatology from menu on map
    % ----------------------------------------------
    tsg.preference.map.climatology = climato;
    
    % save tsg.preference.map.climatology
    % -----------------------------------
    setappdata( hMainFig, 'tsg_data', tsg);
    
    % redraw map
    % ------------------------------------------
    erase_Line( hPlotAxes, 4 );
    plot_map(hMainFig, hPlotAxes);
    
  end

% call when user change range value for contourf climato on map
% -------------------------------------------------------------
  function mapContourfCallback(src, evnt, type)
    
    % get the tsg structure
    % ---------------------
    tsg = getappdata( hMainFig, 'tsg_data');
    
    
    % change the map 2D climatology from menu on map
    % ----------------------------------------------
    tsg.preference.map.climato.(tsg.plot.sample).(type) = str2double(get(src,'string'));
    
    % save tsg.preference.map.climatology
    % -----------------------------------
    setappdata( hMainFig, 'tsg_data', tsg);
    
    % redraw map
    % ------------------------------------------
    erase_Line( hPlotAxes, 4 );
    plot_map(hMainFig, hPlotAxes);
% Generic function for all customization menus.
% The tag argument (second) is used to extract the handle of the selected
% We use a regular expression to access and modify the preference structure
% -------------------------------------------------------------------------
  function mapCustomizeCallback(src, evnt, val, tag)
    
    % get the tsg structure
    % ---------------------
    tsg = getappdata( hMainFig, 'tsg_data');
    
    hdls = findobj( '-regexp', 'tag', ['TAG_UIMENU_MAP_' tag]);
    set(hdls, 'Checked', 'off');
    set(src, 'Checked', 'on');
    
    % change the value from customize menu on map, dynamically transform
    % tag to structure member
    % -------------------------------------------------------------
    member =  regexp(lower(tag),'(\w+)_(\w+)','tokens');
    tsg.preference.map.(member{1}{1}).(member{1}{2}) = val;
    
    % save the selected position menu (index) for next use
    index = [member{1}{2} '_index'];
    tsg.preference.map.(member{1}{1}).(index) = get(src, 'position');
    
    % save tsg struct
    setappdata( hMainFig, 'tsg_data', tsg);
    
    % redraw map
    % ------------------------------------------
    erase_Line( hPlotAxes, 4 );
    plot_map(hMainFig, hPlotAxes);
  end

% color picker callback
% ---------------------
  function mapColorPickerCallback(src, evnt, tag)
    
    % open color picker
    theColor = uisetcolor;
    % if user select cancel, abort
    if length(theColor) == 1 && theColor == 0
      return
    end
    % call generic function
    mapCustomizeCallback(src, evnt, theColor, tag)
% print map figure from menu
% ----------------------------
  function mapPrintCallback(src, evnt)
    % cache the contourf setting panel before printing
    set(findobj('tag', 'TAG_MAP_CLIMATO_UIPANEL'),'Visible', 'off');
    % resize map panel for full figure
    if strcmp(tsg.preference.map.climatology, 'contourf')
      set(findobj('tag', 'TAG_MAP_CLIMATO_AXES'),'Position',[0, 0, 1, 1])
    end
    % call the print dialog box to the figure handle
    printdlg(get(get(hPlotAxes(4),'parent'),'parent'));
    if strcmp(tsg.preference.map.climatology, 'contourf')
      set(findobj('tag', 'TAG_MAP_CLIMATO_AXES'),'Position',[0, 0, .8, 1])
      % enable the contourf setting panel
      set(findobj('tag', 'TAG_MAP_CLIMATO_UIPANEL'),'Visible', 'on');
    end
% print preview of the map figure, parent of panel and axe
% --------------------------------------------------------
  function mapPrintPreviewCallback(src, evnt)
  
    % cache the contourf setting panel before printing
    set(findobj('tag', 'TAG_MAP_CLIMATO_UIPANEL'),'Visible', 'off');
    % resize map panel for full figure
    if strcmp(tsg.preference.map.climatology, 'contourf')
      set(findobj('tag', 'TAG_MAP_CLIMATO_AXES'),'Position',[0, 0, 1, 1])
    end
    printpreview(get(get(hPlotAxes(4),'parent'),'parent'));
    % resize map panel for axes only when contourf is selected
    if strcmp(tsg.preference.map.climatology, 'contourf')
      set(findobj('tag', 'TAG_MAP_CLIMATO_AXES'),'Position',[0, 0, .8, 1])
      % enable the contourf setting panel
      set(findobj('tag', 'TAG_MAP_CLIMATO_UIPANEL'),'Visible', 'on');
    end
% enable toolbar on map figure from menu
% see Reverting axes controls in figure toolbar under:
% https://undocumentedmatlab.com/blog/reverting-axes-controls-in-figure-toolbar
% and Matlab Central:
% https://fr.mathworks.com/matlabcentral/answers/419036-what-happened-to-the-figure-toolbar-in-r2018b-why-is-it-an-axes-toolbar-how-can-i-put-the-buttons
% --------------------------------------------------------------------
  function mapToolBarCallback(src, evnt)
    if( strcmp(get(hMapFig, 'toolbar'), 'none'))
      set(hMapFig, 'toolbar', 'figure');
        addToolbarExplorationButtons(hMapFig);
      set(hMapFig, 'toolbar', 'none');
  function mapSaveAsCallback(src, evnt)
    [file, path] = uiputfile({'*.jpeg';'*.png';'*.pdf';'*.tiff';'*.bmp';'*.eps'});
    fprintf(1, 'Save figure map as %s\n', fullfile(path,file));
    % cache the contourf setting panel before printing
    set(findobj('tag', 'TAG_MAP_CLIMATO_UIPANEL'),'Visible', 'off');
    % resize map panel for full figure
    if strcmp(tsg.preference.map.climatology, 'contourf')
      set(findobj('tag', 'TAG_MAP_CLIMATO_AXES'),'Position',[0, 0, 1, 1])
    end
    % save figure to specific file format
    map_hdl = get(get(hPlotAxes(4), 'parent'), 'parent');
    
    % bug with saveas for version < R2014b when figure as panel children
    % -------------------------------------------------------------------
    if verLessThan('matlab','8.4')
      screencapture(map_hdl, fullfile(path,file));
    else
      saveas(map_hdl, fullfile(path,file));
    end
    % resize map panel for axes only when contourf is selected
    if strcmp(tsg.preference.map.climatology, 'contourf')
      set(findobj('tag', 'TAG_MAP_CLIMATO_AXES'),'Position',[0, 0, .8, 1])
      % enable the contourf setting panel
      set(findobj('tag', 'TAG_MAP_CLIMATO_UIPANEL'),'Visible', 'on');
    end
%% HeaderMenuCallback
% -------------------------------------------------------------------
% Callback function run when the headerForm tool bar item is selected
% -------------------------------------------------------------------
  function HeaderMenuCallback(src, evnt)
    % call header form function
    % -------------------------
    headerForm(hMainFig);
  end

%% PrintFigMenuCallback
% -------------------------------------------------------------------
% Callback function run when the Report tool bar item is selected
% -------------------------------------------------------------------
  function PrintFigMenuCallback(src, evnt)
    % disable ButtonMotion on main fig during select
    % ----------------------------------------------
    set( hMainFig, 'WindowButtonMotionFcn', []);
    % Get the tsg structure
    % ---------------------
    tsg = getappdata( hMainFig, 'tsg_data');
    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, 'interpreter', 'none');
    if strcmp(get(hBottleToggletool,'state'), 'on')
      % Default parameter
      % -----------------------------------------------------
      PARA = getParaCorModule( hMainFig );
      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 );
      plot_Validation( hMainFig, hPlot, 1, tsg.plot.parameter{1} );
      plot_Validation( hMainFig, hPlot, 2, tsg.plot.parameter{2} );
      plot_Validation( hMainFig, hPlot, 3, tsg.plot.parameter{3} );
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
      %       I = getframe(hPlotAxes(1));
      %       imwrite(I.cdata, 'myplot.png');
    linkaxes([hPlot(1),hPlot(2),hPlot(3)], 'x');
    for i = 1 : length( hPlot )
      %      set(hPlot(i), 'box', 'on','XTickMode','auto');
      set(hPlot(i), 'box', 'on','XTickMode','auto');
      datetick(hPlot(i),'x','mm/dd','keepticks' );
    % 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(src, evnt)
    % call report function
    % --------------------
    saveReport(hMainFig);
%% GoogleEarthMenuCallback
% -------------------------------------------------------------------
% Callback function run when the Google Earth tool bar item is selected
% -------------------------------------------------------------------
  function GoogleEarthMenuCallback(src, evnt)
    % ------------------------
    tsg = getappdata( hMainFig, 'tsg_data' );
    
    % call KML Toolbox functions
    % --------------------------
    k = kml(tsg.CYCLE_MESURE);
    
    if verLessThan('matlab','8.4')
      XLim = get(hPlotAxes(1), 'XLim');
      ind = find(tsg.DAYD > XLim(1) & tsg.DAYD < XLim(2));
    else
      ind = find(tsg.DAYD > hPlotAxes(1).XLim(1) & ...
    % Color value format must be passed as a character array according
    % to the format string 'AABBGGRR', eg: red = 'FF0000FF'
    k.plot(tsg.LONX(ind),tsg.LATX(ind), 'linewidth', 2, 'linecolor', 'FF0000FF');
%% SaveMenuCallback
% -------------------------------------------------------------------
% Callback function run when the Save menu item is selected
% -------------------------------------------------------------------
  function SaveMenuCallback(src, evnt)
    % 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 evnt 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);
      % update the display
      % ------------------
      set( hInfoFileText, 'String', fileName);
      % enable Quality Control mode
      % ---------------------------
      hdl_pushtool = findobj('Tag', 'QC');
      set(hdl_pushtool, 'Enable', 'on');
    end
%% ExportTsgCallback
% -------------------------------------------------------------------
% Callback function run when the Export menu item is selected
% -------------------------------------------------------------------
  function ExportTsgCallback(src, evnt)
    % 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' );
    % Write a .TSG (ascii)  file
    % --------------------------
    error = writeAsciiTsg(hMainFig, strcat(pathName, fileName));
    % Pointer reset to arrow
    % ----------------------
    set( hMainFig, 'Pointer', 'arrow' );
    % enable Quality Control mode
    % ---------------------------
    hdl_pushtool = findobj('Tag', 'QC');
    set(hdl_pushtool, 'Enable', 'on');
    % Set MouseMotion 'on'
    % --------------------
    set( hMainFig, 'WindowButtonMotionFcn', @MouseMotion);
    % Check for .TSG writing error - must to be rewriting
    % Because of the 'return' - These line must be at the end
    % --------------------------------------------------------
    if error == -1
      warning('tsgqc:SaveMenuCallback', ...
        'TSG no ouput: %s %s', pathName, fileName);
      return;
    end
  end
%% ExportSampleCallback
% -------------------------------------------------------------------
% Callback function run when the Export menu item is selected
% -------------------------------------------------------------------
  function ExportSampleCallback(src, evnt)
    % 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('*.spl', ...
      'Save file name', strcat(tsg.file.name, '.spl'));
    % if user press cancel button, all var are set to zero
    % ----------------------------------------------------
    if filterindex == 0
    % Pointer set to watch during reading and plotting
    % ------------------------------------------------
    set( hMainFig, 'Pointer', 'watch' );
    % --------------------------
    error = writeAsciiSample(hMainFig, strcat(pathName, fileName));
    % Pointer reset to arrow
    % ----------------------
    set( hMainFig, 'Pointer', 'arrow' );
    % enable Quality Control mode
    % ---------------------------
    hdl_pushtool = findobj('Tag', 'QC');
    set(hdl_pushtool, 'Enable', 'on');
    % Set MouseMotion 'on'
    % --------------------
    set( hMainFig, 'WindowButtonMotionFcn', @MouseMotion);
    % Check for .TSG writing error - must to be rewriting
    % Because of the 'return' - These line must be at the end
    % --------------------------------------------------------
    if error == -1
      warning('tsgqc:SaveMenuCallback', ...
        'TSG no ouput: %s %s', pathName, fileName);
      return;
    end
  end

%% UndoMenuCallback
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
% -----------------------------------------------------------------------
% Callback function run when the Edit/Undo menu item is selected (Ctrl+Z)
% -----------------------------------------------------------------------
  function UndoMenuCallback(src, evnt)
    
    if (tsg.qc_history_pointer > 0)
      
      % Get the parameter (SSPS, SSJT or SSTP)
      % --------------------------------------
      PARA = getParaCorModule( hMainFig );
      
      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
      
      % Back to previous QC flags
      % -------------------------
      tsg.qc_history_state(:,tsg.qc_history_pointer+1) = tsg.([PARA{1} '_QC']);
      tsg.([PARA{1} '_QC']) = tsg.qc_history_state(:,tsg.qc_history_pointer);
      tsg.qc_history_pointer = tsg.qc_history_pointer - 1;
      
      % Allow redo
      % ----------
      tsg.qc_redo_possible = tsg.qc_redo_possible + 1;
      set(findobj('tag','UIMENU_REDO'),'enable','on');
      
      % 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);
      end
      
      % As soon as a modification took place the data should be saved
      % -------------------------------------------------------------
      set( hSaveMenu, 'UserData', 'on' );
      
    else
      
      msgbox('Undo not possible', 'modal');
      
%% RedoMenuCallback
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
% -----------------------------------------------------------------------
% Callback function run when the Edit/Redo menu item is selected (Ctrl+R)
% -----------------------------------------------------------------------
  function RedoMenuCallback(src, evnt)
      
      % Get the parameter (SSPS, SSJT or SSTP)
      % --------------------------------------
      PARA = getParaCorModule( hMainFig );
      
      % Forward to undone QC flags
      % --------------------------
      tsg.qc_history_pointer = tsg.qc_history_pointer + 1;
      tsg.([PARA{1} '_QC']) = tsg.qc_history_state(:,tsg.qc_history_pointer+1);
      
      % Reduce number of possible redo
      % ------------------------------
      tsg.qc_redo_possible = tsg.qc_redo_possible-1;
      if tsg.qc_redo_possible == 0
        set(findobj('tag','UIMENU_REDO'),'enable','off');
      end
          
      % 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);
      end
      
      % As soon as a modification took place the data should be saved
      % -------------------------------------------------------------
      set( hSaveMenu, 'UserData', 'on' );
      
    else
      
      msgbox('Redo not possible', 'modal');   
%% This function is called after to duplicate QC from upper plot to middle
% plot
% -----------------------------------------------------------------------
  function duplicateQc(src, evnt)
    
    % Get the data from the application GUI
    % -------------------------------------
    tsg = getappdata( hMainFig, 'tsg_data');
    
    source = tsg.plot.parameter{1};
    dest = tsg.plot.parameter{2};
    valid = {'SSPS', 'SSTP'};
    
    if any(strcmp(valid, source)) && any(strcmp(valid, dest))
      
      % duplicate QC from parameter 1 to 2
      % ----------------------------------
      tsg.([dest '_QC']) = tsg.([source '_QC']);
      
      % Save tsg structure first for plot_Validation
      % ---------------------------------------------
      setappdata( hMainFig, 'tsg_data', tsg);
      
      % plot with QC on middle plot
      % ---------------------------
      plot_Validation( hMainFig, hPlotAxes, 2, tsg.plot.parameter{2} );
      
    else
      msgbox( 'Duplication QC code only on SSPS or SSTP variable',...
        'Choose the correct variable first', 'warn', 'modal');
    end
    
  end

%% KeyPressFcnCallback
% -----------------------------------------------------------------------
% Callback function run when key is pressed
% -----------------------------------------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
  function keyPressFcnCallback(src, evnt)
    % MATLAB generates repeated KeyPressFcn events, desactivate callback
    % ------------------------------------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    set(src, 'KeyPressFcn', []);
    % check if key is pressed
    % -----------------------
    if ~isempty(evnt.Key)
      % test key, shift or control
      % --------------------------
      switch evnt.Key
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
          pointerShape = get(src, 'pointer');
          % save current cursor shape
          % -------------------------
          setappdata( hMainFig, 'tsg_pointer', pointerShape);
          % set cursor to fullcross or reset to normal arrow
          % ------------------------------------------------
          % Get current position of cusor and return its coordinates in
          % axes
          % -----------------------------------------------------------
          a = get(hPlotAxes(1), 'CurrentPoint');
          x = a(2,1);
          % Test if cursor is inside data interval
          % -------------------------------------
          if x > tsg.DAYD(1) && x < tsg.DAYD(end)
            % loop over 3 subplot and draw vertical lines
            % -------------------------------------------
            for iplot = 1:3
              % axes( hPlotAxes(iplot));
              line(hPlotAxes(iplot), [x x], limy,...
                'color', 'k', 'tag', 'VERTICAL_TAG_LINE');
              % to immediately display changes to object data, call
              % the drawnow function instead of setting EraseMode to 'xor'.
              % since R2014
              drawnow;

%% KeyReleaseFcnCallback
% -----------------------------------------------------------------------
% Callback function run when key is release
% -----------------------------------------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
  function keyReleaseFcnCallback(src, evnt)
    % check if key is pressed
    % -----------------------
    if ~isempty(evnt.Key)
      % test key, shift or control
      % --------------------------
      switch evnt.Key
          % get stored cursor shape
          % -----------------------
          pointerShape = getappdata( hMainFig, 'tsg_pointer');
          % if pointer equal to fullcrosshair, oups, error, reset to arrow
          % ---------------------------------------------------------------
          if strcmp(pointerShape, 'fullcrosshair')
            pointerShape = 'arrow';
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
            set(src, 'pointer', pointerShape);
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
          set(src, 'pointer', pointerShape);
          % find vertical lines and delete them
          % -----------------------------------
          hdl_lines = findobj( 'Tag', 'VERTICAL_TAG_LINE' );
          delete(hdl_lines);
          
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    set(src, 'KeyPressFcn', @keyPressFcnCallback);
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
% -----------------------------------------------------------------
% Callback function run when the Quit Map Figure item is selected
% -----------------------------------------------------------------
  function QuitMapCallback(src, evnt)
    % Make the earth map invisible
    % ----------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    set(hMapFig, 'Visible', 'off' );
    set(hMapToggletool, 'state',  'off' );
%% QuitMenuCallback
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
% -----------------------------------------------------------------
% Callback function run when the Quit menu item is selected
% -----------------------------------------------------------------
  function QuitMenuCallback(src, evnt)
    
    % Get the data from the application GUI
    % -------------------------------------
    tsg = getappdata(hMainFig, 'tsg_data');
    % in case of bad initialisation, the user could close the windows
    % ---------------------------------------------------------------
    try
      % save config mat file in prefdir
      % -------------------------------
      config_file = [prefdir, filesep, tsgqcname, '.mat'];
      % save preference mat file
      % ------------------------
      if exist('tsg', 'var') && isfield( tsg, 'preference')
        preference = tsg.preference;
        save( config_file, 'preference');
      end
      % leave program directly in debug mode
      % ------------------------------------
        quitProgram(DEFAULT_PATH_FILE, hMainFig, hMapFig);
        return
      end
      % If the data have been modified and not save, the program
      % ask to save the data
      % --------------------------------------------------------
      if  strcmp( get( hSaveMenu, 'UserData' ), 'on')
        selection = ...
          questdlg('The file has been modified.  Do you want to save it ?',...
          'Save before Quit?',...
          'Yes', 'No', 'Yes');
          % call File/Save Menu Callback before before quit
          % -----------------------------------------------
          SaveMenuCallback;
        % quit program
        % ------------
        quitProgram(DEFAULT_PATH_FILE, hMainFig, hMapFig);
        selection = ...
          questdlg(['Quit ' get(hMainFig, 'Name') '?'],...
          ['Quit ' get(hMainFig, 'Name') '?'],...
          'Yes', 'No', 'Yes');
        if strcmp(selection, 'No')
          return;
        else
          quitProgram(DEFAULT_PATH_FILE, hMainFig, hMapFig);
        end
      % catch error during close windows
      % --------------------------------
    catch
      % display message to console and quit program
      % -------------------------------------------
      fprintf('abnormal program termination during close user request\n');
      quitProgram(DEFAULT_PATH_FILE, hMainFig);
  function quitProgram(DEFAULT_PATH_FILE, varargin)
    
    % close all windows
    % -----------------
    for ii=1:size(varargin,2)
      delete(varargin{ii});
    end
    
    % reset userdata property of root Matalab object (0) for next use
    % ---------------------------------------------------------------
    set(0, 'userdata', []);
    
    % reset Matlab search path to default
    % addpath isn't mandatory and failed with compiled applications
    % --------------------------------------------------------------
    if (~isdeployed)
      rmpath( [DEFAULT_PATH_FILE filesep 'tsg_util'] );
      rmpath( [DEFAULT_PATH_FILE filesep 'tsg_util/ScreenCapture'] ); 
      rmpath( [DEFAULT_PATH_FILE filesep 'tsg_util/gsw'] );       
      rmpath( [DEFAULT_PATH_FILE filesep 'tsg_data'] );
      rmpath( [DEFAULT_PATH_FILE filesep 'tsg_io'] );
      rmpath( [DEFAULT_PATH_FILE filesep 'tsg_icon'] );
      rmpath( [DEFAULT_PATH_FILE filesep 'tsg_map'] );
    end
    
    % Refresh file system caches
    % --------------------------
    rehash;
    
    % clear base workspace just before quit, this is the only method
    % to clear NetCDF variables assign to base workspace with assignin
    % ----------------------------------------------------------------
    evalin('base','clear all');
    
  end % end of quitProgram