Skip to content
Snippets Groups Projects
tsgqc_GUI.m 59.4 KiB
Newer Older
      plot(tsg.DAYD(ind), tsg.SSPS(ind), color );
      hold off

      % As soon as a modification took place the data should be saved
      % -------------------------------------------------------------
      set( hSaveMenu, 'UserData', 'on' );
      
      % refresh QC statistic panel
      % --------------------------
      display_QC( hMainFig, hPlotAxes );
      
      % store QC in queue object, for undo
      % ----------------------------------
      tsg.queue = push(tsg.queue, tsg.SSPS_QC);
      
      % Save the modifications
      % ----------------------
      setappdata( hMainFig, 'tsg_data', tsg);
      % enable undo menu
      % ----------------
      set(findobj('tag','UIMENU_UNDO'),'enable','on');
      % enable ButtonMotion on main fig after select QC area
      % ----------------------------------------------------
      set( hMainFig, 'WindowButtonMotionFcn', @MouseMotion);
%% Qc context menu selected ........................ Quality Control Module
  %-----------------------------------------------------------
  % Callback function run when the QC context menu is selected
  %-----------------------------------------------------------
    
      % Retrieve Default Quality Code and Color
      % ---------------------------------------
      tsg = getappdata( hMainFig, 'tsg_data');
      
      % 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 );
%% Radiobutton Quality Control ..................... Quality Control Module
  % ---------------------------------------------------------------
  % Callback to select CODE and COLOR QC from the RadioButton Group
  % ---------------------------------------------------------------
  function RadiobuttonQc(source, eventdata)
        
      % Retrieve Default Quality Code and Color
      % ---------------------------------------
      tsg = getappdata( hMainFig, 'tsg_data');
      
      % Retrieve the key from the active RadioButton
      % --------------------------------------------
      rbTag = get(eventdata.NewValue,'Tag');
      key = strrep(rbTag, 'TAG_QC_RADIO_', '');
      
      % store his handle to uibuttongroup userdata
      % ------------------------------------------
      set(hbgQc,'Userdata', eventdata.NewValue);
      
      % get key and some values in hashtable
      % ------------------------------------
      code  = get(tsg.qc.hash, key, 'code');
      color = get(tsg.qc.hash, key, 'color');
       
      % set active code and color from selected context menu
      % ----------------------------------------------------
      tsg.qc.active.Code  = code;
      tsg.qc.active.Color = color;

      setappdata( hMainFig, 'tsg_data', tsg );

  end

%% preQcPanCallback .................................Quality Control Module
function preQcPanCallback(obj, evd)
  
  set(hQCToggletool, 'state', 'off' );

end

%% postQcPanCallback ............................... Quality Control Module
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
  
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
      % Code to Activate the PAN function when QC mode is active
      % A PAN zone is defined in the bottom (5%) of PlotAxes(1)
      % 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' );
        
        limx = get(hPlotAxes(1), 'XLim');
        limy = get(hPlotAxes(1), 'YLim');
        
        % Suppose that Y axes is increasing from the bottom to the top
        % ------------------------------------------------------------
        limy2 = limy(1) + (limy(2)-limy(1)) * 0.05;

        if  x > limx(1) && x < limx(2) &&  y <= limy2
          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)
        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'))
            hMarker = m_line( ...
              tsg.LONX(indCursor(1)), tsg.LATX(indCursor(1)),...
              'Marker','o','MarkerSize',5, ...
              'Color','r', 'MarkerFaceColor','r');
            set(hMapPanel, 'UserData', hMarker)
          else
            delete(get(hMapPanel, 'UserData'));
            hMarker = m_line( ...
              tsg.LONX(indCursor(1)), tsg.LATX(indCursor(1)),...
              'Marker','o','MarkerSize',5, ...
              'Color','r', 'MarkerFaceColor','r');
            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
    % ----------------------------
  %---------------------------------------------------------------------
  % 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
    % --------------------------
%% 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');
    
    % Desactivate the Climatology button
    % ----------------------------------
    set( hClimToggletool, 'Enable', 'off');
    % Activate some toolbar buttons
    % -----------------------------
    hdl_Toggletool = findobj('-regexp','Tag', 'CORRECT_');
    set(hdl_Toggletool, 'Enable', 'on');
    
    % Test if tsg and sample data have been loaded
    % --------------------------------------------
    if ~isempty( tsg.SSPS_WS ) || ~isempty( tsg.SSPS_EXT )
      
      % Merge bucket and external samples
      % ---------------------------------
      tsg_mergesample( hMainFig );
                    
      % Compute the sample-TSG differences
      % ----------------------------------

      % plot Salinity Difference
      % ------------------------
      plot_Sample( hMainFig, hPlotAxes );
      
      % Get the information on time limits of the time series
      % Write them in the uipanel
      % -----------------------------------------------------
      set( hetDateMin, 'String', datestr(tsg.DAYD(1), 31));
      set( hetDateMax, 'String', datestr(tsg.DAYD(end), 31)); 

      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)
 
    % Activate the Climatology button
    % -------------------------------
    set( hClimToggletool, 'Enable', 'on');
    
    % Desactivate some toolbar buttons
    % --------------------------------
    hdl_Toggletool = findobj('-regexp','Tag', 'CORRECT_');
    %set(hdl_Toggletool, 'State', 'off');

    % Desactivate Click Mouse on figure
    % ---------------------------------
    set( hMainFig,'WindowButtonDownFcn', []);

    % Plot the SSS, SST and Velocity
    % ------------------------------
    plot_SalTempVel( hMainFig, hPlotAxes );
    plot_WS( hMainFig, hPlotAxes );
    plot_EXT( hMainFig, hPlotAxes );
 
  end
%% SelectTime_OnMenuCallback ............................ Correction module
%----------------------------------------------------------------------
  function SelectTime_OnMenuCallback(hObject, eventdata)
    % Callback function run when the ....
    
    % 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
        
    % Desactivate Zoom and Pan functions.
    % ----------------------------------
    set(hZoomToggletool, 'state', 'off' );
    set(hPanToggletool,  'state', 'off' );

    % Toggle the state of the Qc Togglebutton to 'on' or 'off'
    % ----------------------------------------------------
    if hObject == hEndlimitToggletool
      set( hStartlimitToggletool, 'State', 'off' );
    else
      set( hEndlimitToggletool,   'State', 'off' );
    end
    
    % Activate clic mouse menu on first axes (salinity) for next rbbox
    % DO NOT USE :
    % ButtonDownFcn -  the callback executes whenever you press a button
    % while the pointer is within the axes, but not over another graphics
    % object parented to the axes.
    % -------------------------------------------------------------------
    %set(hPlotAxes(1),'ButtonDownFcn', @Time_SelectCallback);
    set( hMainFig,'WindowButtonDownFcn', @Time_SelectCallback);  
 
    % change cursor to crosshair aspect
    % ---------------------------------
    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', []);

      % Get the current mouse position
      % ------------------------------
      a = get(hPlotAxes(1), 'CurrentPoint');

      % Write the date in the Editable uicontrol
      % ----------------------------------------
      if hObject == hEndlimitToggletool
        set( hetDateMax, 'String', datestr(x, 31));
      else
        set( hetDateMin, 'String', datestr(x, 31));
      end

      % Enable ButtonMotion on main fig 
      % -------------------------------
      %set( hMainFig, 'WindowButtonMotionFcn', @MouseMotion);
      
    end
  end

%% SelectTime_OffMenuCallback ........................... Correction module
%----------------------------------------------------------------------
  function SelectTime_OffMenuCallback(hObject, eventdata)
  % Callback function run when the ....
  
    if hObject == hEndlimitToggletool
      set( hEndlimitToggletool, 'State', 'off');
    else
      set( hStartlimitToggletool, 'State', 'off');
    end

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

  end

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

    msgbox('Method not yet implemented', 'modal' );

  end


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

    msgbox('Method not yet implemented', 'modal' );

  end

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

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

    % Correction
    % ----------
    corTsgMethod1(hMainFig, dateMin, dateMax);
    
    plot_TsgAdjusted(hMainFig, hPlotAxes)

  end

%% Clim_OffMenuCallback
  %------------------------------------------------------------------------
  % Callback function run when the Levitus climatology toolbar is unselected
  %------------------------------------------------------------------------
  function Clim_OffMenuCallback(hObject, eventdata)
    % Get line handles
    % ----------------------------------------------
    hLine1 = get( hPlotAxes(1), 'UserData');
    hLine2 = get( hPlotAxes(2), 'UserData');
    
    % -----------------------------------
    try
      delete(hLine1.stdClimMinus);
      delete(hLine1.stdClimPlus);
      delete(hLine1.meanClim);
      delete(hLine2.stdClimMinus);
      delete(hLine2.stdClimPlus);
      delete(hLine2.meanClim);
      
    % if handle is missing, prevent warning message on console
    % --------------------------------------------------------
  %------------------------------------------------------------------------
  % 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' )
      % Get data
      % -----------------------
      tsg = getappdata( hMainFig, 'tsg_data' );
      hLine1 = get( hPlotAxes(1), 'UserData');
      hLine2 = get( hPlotAxes(2), 'UserData');

      % Read Climatology
      % ----------------
      if isempty( tsg.levitus.data)
        levitus = read_file_woa01( 'woa01_annual_surf.nc' );
        if ~isstruct(levitus)
          errordlg({'NetCDF climatology file not present in you path',...
            'Check your matlab path or download it from',...
            'ftp://ftp.ifremer.fr/ifremer/ird/us191/oceano/lib/matlab/woa01_annual_surf.nc'},...
            'NetCDF climatology file access error');
          return;
        else
          tsg.levitus.data = levitus;
          tsg.levitus.type = 'woa01an';
        end
        setappdata( hMainFig, 'tsg_data', tsg );
      end

      % round positive latitude and Longitude toward zero
      % -------------------------------------------------
      ind = find(tsg.LATX > 0);
      lat(ind) = fix(tsg.LATX(ind)) + 0.5;

      ind = find(tsg.LONX > 0);
      lon(ind) = fix(tsg.LONX(ind)) + 0.5;

      % rounds negative latitude and Longitudeto the nearest lowest integers
      % ---------------------------------------------------------------------
      ind = find(tsg.LATX <= 0);
      lat(ind) = floor(tsg.LATX(ind)) + 0.5;

      ind = find(tsg.LONX <= 0);
      lon(ind) = floor(tsg.LONX(ind)) + 0.5;

      % Calculates differences between adjacent elements of X.
      % 0 if adajacent latitude or longitude are equal
      % - 1 or -1 otherwise
      % ------------------------------------------------------------
      lat_diff = [diff( lat )'; 0];
      lon_diff = [diff( lon )'; 0];

      % Select latitude and longitude
      % -----------------------------
      ind  = find(abs(lat_diff) == 1 | abs(lon_diff == 1));
      lat2 = lat( ind );
      lon2 = lon( ind );
      time = tsg.DAYD( ind );
      temp = tsg.SSJT( ind );
      psal = tsg.SSPS( ind );

      % Get Climatology
      %           LATX(80)  = -0.5 et LATX(81)  = 0.5
      %           LONX(180) = -0.5 et LONX(181) = 0.5
      % ----------------
      axes( hPlotAxes(1) );
      mean_temp = zeros(size(ind));
      mean_psal = zeros(size(ind));
      std_temp  = zeros(size(ind));
      std_psal  = zeros(size(ind));
      for ii=1:length(ind)
        ilat         = find(tsg.levitus.data.WOA01_LATX == lat2(ii));
        ilon         = find(tsg.levitus.data.WOA01_LONX == lon2(ii));
        mean_temp(ii) = tsg.levitus.data.WOA01_MEAN_TEMP(ilat,ilon,1);
        mean_psal(ii) = tsg.levitus.data.WOA01_MEAN_PSAL(ilat,ilon,1);
        std_temp(ii)  = tsg.levitus.data.WOA01_STD_TEMP(ilat,ilon,1);
        std_psal(ii)  = tsg.levitus.data.WOA01_STD_PSAL(ilat,ilon,1);

      % Plot mean salinity climatology
      % ------------------------------
      hLine1.meanClim = line( ...
        time, mean_psal,'Linestyle', '-', 'Color','k');
      
      % Plot with 3 standard deviation
      % ------------------------------
      hLine1.stdClimPlus = line( ...
        time,  mean_psal + 3 * std_psal ,'Linestyle', '-', 'Color','r');
      hLine1.stdClimMinus = line( ...
        time,  mean_psal - 3 * std_psal ,'Linestyle', '-', 'Color','r');
      
      % Plot mean temperature climatology
      % ---------------------------------
      axes( hPlotAxes(2));
      hLine2.meanClim = line( ...
        time, mean_temp,'Linestyle', '-', 'Color','k');
      hLine2.stdClimPlus = line( ...
        time,  mean_temp + 3 * std_temp ,'Linestyle', '-', 'Color','r');
      hLine2.stdClimMinus = line( ...
        time,  mean_temp - 3 * std_temp ,'Linestyle', '-', 'Color','r');

      % Store the handle of the bucketline
      % ----------------------------------
      set( hPlotAxes(1), 'UserData', hLine1 );
      set( hPlotAxes(2), 'UserData', hLine2 );
  % -------------------------------------------------------------------
  % 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');

    % get fileName without extension in cell
    % --------------------------------------
    file = textscan( tsg.file.name,'%s','delimiter','.');

    % 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', ...

      % if user press cancel button, all var set to zero
      % ------------------------------------------------
      if fileName == 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, [pathName fileName] );

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

      % Check for a writing pb to NetCDF
      % must to be rewriting
      % --------------------------------
      if error == -1
        warning('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');
  % -----------------------------------------------------------------
  % Callback function run when the Quit menu item is selected
  % -----------------------------------------------------------------
  function QuitMenuCallback(hObject, eventdata)  
    % 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');
      if strcmp(selection, 'Yes')
        return;
      else
        QuitProgram;
      end
    else
      selection = ...
        questdlg(['Quit ' get(hMainFig, 'Name') '?'],...
        ['Quit ' get(hMainFig, 'Name') '?'],...
        'Yes', 'No', 'Yes');
      if strcmp(selection, 'No')
        return;
      else
        QuitProgram;
      end
  % -----------------------------------------------------------------------
  % 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 );

%% 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
  % -----------------------------------------------------------------
  % call from: 
  %   QuitMenuCallback
  %   callback 'CloseRequestFcn', @QuitProgram
  % -----------------------------------------------------------------
  function QuitProgram(hObject, eventdata)
    rmpath( [DEFAULT_PATH_FILE filesep 'tsg_util'] );
    rmpath( [DEFAULT_PATH_FILE filesep 'tsg_data'] );
    rmpath( [DEFAULT_PATH_FILE filesep 'tsg_io'] );
% end of tsgqc_GUI
% ----------------
end