Skip to content
Snippets Groups Projects
tsgqc_GUI.m 96.9 KiB
Newer Older
%% postQcPanCallback ............................... Quality Control Module
  % ---------------------------------------------------------------
  % Callback function ... to be completed
  % ---------------------------------------------------------------
  function postQcPanCallback(obj, evd)

    set(hQCToggletool, 'state', 'on' );

    % 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

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

Yves Gouriou's avatar
Yves Gouriou committed
      % Get current position of cusor and return its coordinates in
      % axes with handle h_axes
      % -----------------------------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
      
      % Get the Limits of axes 1
      % ------------------------
      limx = get(hPlotAxes(1), 'XLim');
      limy = get(hPlotAxes(1), 'YLim');
Yves Gouriou's avatar
Yves Gouriou committed
      % Code to Activate the PAN function when QC mode is active
      % A PAN zone is defined in the bottom (10%) of PlotAxes(1)
Yves Gouriou's avatar
Yves Gouriou 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;
Yves Gouriou's avatar
Yves Gouriou committed

        if  x > limx(1) && x < limx(2) &&  y <= limy2 && y >= limy(1)
Yves Gouriou's avatar
Yves Gouriou committed
          hPan = pan(hMainFig);
          set(hPan,'ActionPreCallback',  @preQcPanCallback);
          set(hPan,'ActionPostCallback', @postQcPanCallback);
Yves Gouriou's avatar
Yves Gouriou committed
          set(hPan,'Enable','on');
        else
          pan off
        end
      end
      
      % Dynamically display data in uicontrol
      % -------------------------------------
      if x > tsg.DAYD(1) && x < tsg.DAYD(end)
      %if  x > limx(1) && x < limx(2)
Yves Gouriou's avatar
Yves Gouriou committed
        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(tsg.LONX(indCursor(1)),1));
        set( hInfoSSPSText, 'String', tsg.SSPS(indCursor(1)));
        set( hInfoSSJTText, 'String', tsg.SSJT(indCursor(1)));
        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(hMapPanel, 'Visible'), 'on')

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

          if isempty( get(hMapPanel, 'UserData'))
Yves Gouriou's avatar
Yves Gouriou committed
            hMarker = m_line( tsg.LONX(indCursor(1)), tsg.LATX(indCursor(1)),...
                              'Marker','o','MarkerSize',5, ...
                              'Color','r', 'MarkerFaceColor','r');
Yves Gouriou's avatar
Yves Gouriou committed
            set(hMapPanel, 'UserData', hMarker)
          else
            delete(get(hMapPanel, 'UserData'));
            hMarker = m_line( ...
Yves Gouriou's avatar
Yves Gouriou committed
                             tsg.LONX(indCursor(1)), tsg.LATX(indCursor(1)),...
                             'Marker','o','MarkerSize',5, ...
                             'Color','r', 'MarkerFaceColor','r');
Yves Gouriou's avatar
Yves Gouriou committed
            set(hMapPanel, 'UserData', hMarker);
  %---------------------------------------------------------------------
  % Callback function run when the Map tool bar item is unselected
  %---------------------------------------------------------------------
  function Map_OffMenuCallback(hObject, eventdata)
    
   % Make the earth map invisible
    % ----------------------------
    set(hMapPanel, 'Visible', 'off' ); 

  %---------------------------------------------------------------------
  % Callback function run when the Map tool bar item is selected
  %---------------------------------------------------------------------
  function Map_OnMenuCallback(hObject, eventdata)
     
    % Desactivate Zoom and Pan toggle buttons
    % may be change in the futur ...
    % ---------------------------------------
    set(hZoomToggletool,  'state', 'off' );
    set(hPanToggletool,  'state', 'off' );
    % Make the earth map visible
    % --------------------------
Yves Gouriou's avatar
Yves Gouriou committed
    erase_Line( hPlotAxes, 4 );
%% Bottle_OnMenuCallback ................................ Correction Module
  %---------------------------------------------------------------------
  % 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)
    % ------------------------------------------------------
    PARA = tsg.preference.parameter;
    
    % Switch somme buttons
    % --------------------
    set( hZoomToggletool,       'state',  'off' );
    set( hQCToggletool,         'state',  'off' );
    set( hPanToggletool,        'state',  'off' );
Yves Gouriou's avatar
Yves Gouriou committed
    set( hMapToggletool,        'state',  'off' ); 
    set( hClimToggletool,       'Enable', 'off');
    set( hCalToggletool,        'enable', 'off' );
    set( hInterpToggletool,     'enable', 'off' );
    set( hTimelimitToggletool,  'enable', 'on' );
    % Activate ord desactivate uipanels
    % ---------------------------------
    set( hpDateLimit,           'Visible', 'on' );
    set( hbgCorMethod,          'Visible', 'on' );
    set( hbgParameter,          'Visible', 'off');
    % Test if tsg and sample data have been loaded
    % --------------------------------------------
    if ~isempty( tsg.([PARA '_SPL']) )
        
%       % Running average of TSG time series
%       % ----------------------------------
%       tsg_moveaverage(hMainFig, PARA);
      % Compute the sample-TSG differences
      % ----------------------------------
      diffTsgSample( hMainFig, PARA );
      % 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
      % -----------------------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
      noNaN = tsg.DAYD(~isnan( tsg.DAYD ));
      set( hetDateMin, 'String', datestr(noNaN(1), 31));
      set( hetDateMax, 'String', datestr(noNaN(end), 31)); 

      % Desactivate the corretion module
      % --------------------------------
      set( hBottleToggletool, 'state', 'off' );
      
      msgbox('Sample data not loaded in the program', 'modal');
%% Bottle_OffMenuCallback ............................... Correction module
  %---------------------------------------------------------------------
  % Callback function run when the bootle push tool is selected
  %---------------------------------------------------------------------
  function Bottle_OffMenuCallback(hObject, eventdata)
    % If necessary toggle off some buttons
    % ------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
    set( hZoomToggletool,      'state',  'off' );
    set( hQCToggletool,        'state',  'off' );
    set( hPanToggletool,       'state',  'off' );
    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( 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)
    % ------------------------------------
    PARA = tsg.preference.parameter;
     
    % Drawing only necessary if the Correction moduel has been activated
    % ------------------------------------------------------------------
    if ~isempty( tsg.([PARA '_SPL']) )

      % Draw the 3 plots of the validation figure
      % -----------------------------------------
      plot_Validation( hMainFig, hPlotAxes, 1,  PARA );
      plot_Validation( hMainFig, hPlotAxes, 2, 'SSJT' );
      plot_Validation( hMainFig, hPlotAxes, 3, 'SPDC' );
    
    % Set the pointer
    % ---------------
    set( hMainFig, 'Pointer', 'arrow');
%% CorCancelCallback .................................... Correction Module
  function CorCancelCallback(hObject, eventdata)
    % Callback function run when ...
    % Desactivate somme Toggle button
    % -------------------------------
    set( hZoomToggletool,       '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)
    % --------------------------------------------------
    PARA = tsg.preference.parameter;

    % To cancel the correction set the ERROR to NaN then
    % call updateAdjustedVariable.
    % --------------------------------------------------
    tsg.([PARA '_ADJUSTED_ERROR']) = ...
            NaN * ones( size(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
    % ----------------------------------------------------------
    updateAdjustedVariable( hMainFig );
    % Plot in the 3 axes
    % ------------------
    plot_Correction( hMainFig, hPlotAxes, PARA );

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

    % Desactivate somme Toggle button
    % -------------------------------
    set( hZoomToggletool,       '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');
    PARA = tsg.preference.parameter;
    % 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

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

    % Desactivate somme Toggle button
    % -------------------------------
    set( hZoomToggletool,       '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');
    PARA = tsg.preference.parameter;
    % Compute the sample-TSG differences
    % ----------------------------------
    diffTsgSample( hMainFig, PARA );
    error = corTsgMedian(hMainFig, PARA, dateMin, dateMax);
        % Plot in the 3 axes
        % ------------------
        plot_Correction( hMainFig, hPlotAxes, PARA );
        msgbox( 'Date limits are not correct',...
Yves Gouriou's avatar
Yves Gouriou committed
                'Correction module', 'warn', 'modal');
%% SelectTime_OnMenuCallback
%---------------------------
  function SelectTime_OnMenuCallback(hObject, eventdata)
    % Callback function run when the ....
            
    % Desactivate Zoom and Pan functions.
    % ----------------------------------
    set( hZoomToggletool, '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)
  % Callback function run when the ....
  
  % Desactivate time limit buttons
  % ------------------------------
  set( hTimelimitToggletool, 'state', 'off');

  set( hMainFig, 'WindowButtonDownFcn', []);

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

  end

%% Clim_OffMenuCallback
  %------------------------------------------------------------------------
  % 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(hLines);
  %------------------------------------------------------------------------
  % 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' )
      
    end
  end

%% ClimatoSelectMenuCallback
  % -------------------------------------------------------------------
  % 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);
%% PreferencesMenuCallback
  % -------------------------------------------------------------------
  % Callback function run when Option/Preference is selected
  % -------------------------------------------------------------------
  function PreferencesMenuCallback(hObject, eventdata)
    
    % Test if the preference menu is allready checked
    % -----------------------------------------------
    if strcmp( get(hObject, 'checked'), 'on' )
      
      % set preference autoload off (unchecked)
      % ---------------------------------------
      
    else
      
      % set preference autoload on (checked)
      % ------------------------------------
      
    end

     % set current option/preference submenu state
     % -------------------------------------------
     set(hObject, 'checked', tsg.preference.autoload);
% -------------------------------------------------------------------
% Callback function run when the headerForm tool bar item is selected
% -------------------------------------------------------------------
    % call header form function
    % -------------------------
    headerForm(hMainFig);
% -------------------------------------------------------------------
% Callback function run when the Save menu item is selected
% -------------------------------------------------------------------
    % 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' );

      % Check for NetCDF writing error 
      % ------------------------------
        warning('tsgqc_GUI:SaveMenuCallback', ...
                'NetCDF writing error: %s %s', pathName, fileName);
      set( hInfoFileText, 'String', strcat(tsg.file.name, tsg.file.ext));
      % enable Quality Control mode
      % ---------------------------
      hdl_pushtool = findobj('Tag', 'QC');
      set(hdl_pushtool, 'Enable', 'on');
%% ExportMenuCallback
% -------------------------------------------------------------------
% Callback function run when the Export menu item is selected
% -------------------------------------------------------------------
  function ExportMenuCallback(hObject, eventdata)
    
    % Export module not yet implemented
    % -------------------------------
    msgbox('Export module not yet implemented', 'modal');
    
  end

%% UndoMenuCallback
  % -----------------------------------------------------------------------
  % Callback function run when the Edit/Undo menu item is selected (Ctrl+Z)
  % -----------------------------------------------------------------------
  function UndoMenuCallback(hObject, eventdata)  
 
    % Undo module not yet implemented
    % -------------------------------
    % msgbox('Undo module not yet implemented', 'modal');
    
    tsg.queue = undo(tsg.queue);
    tsg.SSPS_QC = get(tsg.queue);
   
    % Make the Salinity, temperature and velocity plot
    % ------------------------------------------------
    plot_SalTempVel( hMainFig, hPlotAxes );

  end

%% RedoMenuCallback
  % -----------------------------------------------------------------------
  % Callback function run when the Edit/Redo menu item is selected (Ctrl+R)
  % -----------------------------------------------------------------------
  function RedoMenuCallback(hObject, eventdata)  
 
    % Redo module not yet implemented
    % -------------------------------
    msgbox('Redo module not yet implemented', 'modal');
    
  end

  % -----------------------------------------------------------------
  % Callback function run when the Quit menu item is selected
  % -----------------------------------------------------------------
  function QuitMenuCallback(hObject, eventdata)  
    % save config mat file in prefdir
    % -------------------------------
    config_file = [prefdir, filesep, tsgqcname, '.mat'];

    % set tsg.levitus empty before save preferences in mat file
    % ---------------------------------------------------------
    tsg.levitus = [];

    % save preference mat file
    % ------------------------
    % If the data have been modified and not save, the program
    % --------------------------------------------------------
    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
      % ------------
    else
      selection = ...
        questdlg(['Quit ' get(hMainFig, 'Name') '?'],...
        ['Quit ' get(hMainFig, 'Name') '?'],...
        'Yes', 'No', 'Yes');
      if strcmp(selection, 'No')
        return;
      else
% end of tsgqc_GUI
% ----------------
end