From 40a9b3e6cfaea601aa20bd82c2dd7dc84818f0fd Mon Sep 17 00:00:00 2001
From: Jacques Grelet <Jacques.grelet@ird.fr>
Date: Fri, 7 Dec 2018 14:37:55 +0100
Subject: [PATCH] an error msg was displayed on console when the user select
 cancel button

---
 tsg_util/corTsgBias.m | 256 ++++++++++++++++++++++--------------------
 tsgqc.m               |   9 +-
 2 files changed, 140 insertions(+), 125 deletions(-)

diff --git a/tsg_util/corTsgBias.m b/tsg_util/corTsgBias.m
index f9a37c7..2592882 100644
--- a/tsg_util/corTsgBias.m
+++ b/tsg_util/corTsgBias.m
@@ -1,6 +1,6 @@
 function [error] = corTsgBias(hMainFig, PARA, dateMin, dateMax)
 % Correct the TSG time series with constant value, a bias.
-% 
+%
 % Input
 % hMainFig ..... Handle to the main GUI
 % PARA ..........Cell array
@@ -12,7 +12,8 @@ function [error] = corTsgBias(hMainFig, PARA, dateMin, dateMax)
 %
 % Output
 % Error ........  1 everything OK
-%       ........ -1 dateMax <= date Min
+........  0 canceled by user
+  %       ........ -1 dateMax <= date Min
 %
 % $Id$
 
@@ -40,15 +41,15 @@ keptCode = [];
 
 nKeptCode = 0;
 for key = qc_list
-    
-    % get handle of checkbox
-    % ----------------------
-    hCb = findobj(hMainFig, 'tag', ['TAG_CHECK_CORRECTION_' char(key)]);
-    
-    if get( hCb, 'value' )
-        nKeptCode = nKeptCode + 1;
-        keptCode(nKeptCode) = tsg.qc.hash.(key).code;
-    end
+  
+  % get handle of checkbox
+  % ----------------------
+  hCb = findobj(hMainFig, 'tag', ['TAG_CHECK_CORRECTION_' char(key)]);
+  
+  if get( hCb, 'value' )
+    nKeptCode = nKeptCode + 1;
+    keptCode(nKeptCode) = tsg.qc.hash.(key).code;
+  end
 end
 
 % Get PROBABLY_GOOD, PROBABLY_BAD and VALUE_CHANGED codes
@@ -63,142 +64,151 @@ VALUE_CHANGED = tsg.qc.hash.VALUE_CHANGED.code;
 % netcdf toolbox failed with assertion when we write NaN to ncbyte variable
 % -------------------------------------------------------------------------
 if isempty( tsg.([PARA{1} '_ADJUSTED_ERROR']) )
-    tsg.([PARA{1} '_ADJUSTED'])       = NaN*ones(size(tsg.(PARA{1})));
-    tsg.([PARA{1} '_ADJUSTED_QC'])    = zeros(size(tsg.([PARA{1} '_QC'])));
-    tsg.([PARA{1} '_ADJUSTED_ERROR']) = NaN*ones(size(tsg.(PARA{1})));
+  tsg.([PARA{1} '_ADJUSTED'])       = NaN*ones(size(tsg.(PARA{1})));
+  tsg.([PARA{1} '_ADJUSTED_QC'])    = zeros(size(tsg.([PARA{1} '_QC'])));
+  tsg.([PARA{1} '_ADJUSTED_ERROR']) = NaN*ones(size(tsg.(PARA{1})));
 end
 
 if dateMax > dateMin
+  
+  
+  % Find samples within TIME_WINDOWS with Good, probably Good, QC
+  % -------------------------------------------------------------
+  ind = find( tsg.DAYD_EXT    >= dateMin &  tsg.DAYD_EXT    <= dateMax &...
+    tsg.([SAMPLE '_EXT_QC']) <= PROBABLY_GOOD);
+  
+  if ~isempty(ind)
     
-    
-    % Find samples within TIME_WINDOWS with Good, probably Good, QC
+    % detect NaN in sample.SSPS_DIF due to bad QC code for tsg.SSPS
+    % or no TSG measurement at sampling time
     % -------------------------------------------------------------
-    ind = find( tsg.DAYD_EXT    >= dateMin &  tsg.DAYD_EXT    <= dateMax &...
-        tsg.([SAMPLE '_EXT_QC']) <= PROBABLY_GOOD);
+    ind2 = find(~isnan(tsg.EXT_DIF(ind)));
     
-    if ~isempty(ind)
+    if ~isempty(ind2)
+      
+      % Compute mean and standard deviation of the TSG/SAMPLE difference
+      % that are suggested as default value for bias and error
+      % ----------------------------------------------------------------
+      if length(ind2) > 2
         
-        % detect NaN in sample.SSPS_DIF due to bad QC code for tsg.SSPS
-        % or no TSG measurement at sampling time
-        % -------------------------------------------------------------
-        ind2 = find(~isnan(tsg.EXT_DIF(ind)));
+        meanDif = mean(tsg.EXT_DIF(ind(ind2)));
+        stdDif = std(tsg.EXT_DIF(ind(ind2)));
         
-        if ~isempty(ind2)
-            
-            % Compute mean and standard deviation of the TSG/SAMPLE difference
-            % that are suggested as default value for bias and error
-            % ----------------------------------------------------------------
-            if length(ind2) > 2
-                
-                meanDif = mean(tsg.EXT_DIF(ind(ind2)));
-                stdDif = std(tsg.EXT_DIF(ind(ind2)));
-                
-                % Case with 2 samples only: the suggested bias is the
-                % mean TSG/SAMPLE difference and the suggested error is the standard
-                % deviation of the TSG/SAMPLE difference, the latter negative to warn that
-                % the correction is not done with a significant number of samples
-                % --------------------------------------------------------------------------
-            elseif length(ind2) == 2
-                
-                meanDif = mean( tsg.EXT_DIF(ind(ind2)) );
-                stdDif = -std( tsg.EXT_DIF(ind(ind2)) );
-                
-                % Case with 1 sample only: the suggested bias is the
-                % TSG/SAMPLE difference and the suggested error is -1
-                % --------------------------------------------------------------------------
-            elseif length(ind2) == 1
-                
-                meanDif = tsg.EXT_DIF(ind(ind2));
-                stdDif = -1;
-                
-            end
-            
-            defaultValue = {num2str(meanDif),num2str(stdDif)};
-            
-        else
-            
-            defaultValue = {'0','0'};
-
-        end
+        % Case with 2 samples only: the suggested bias is the
+        % mean TSG/SAMPLE difference and the suggested error is the standard
+        % deviation of the TSG/SAMPLE difference, the latter negative to warn that
+        % the correction is not done with a significant number of samples
+        % --------------------------------------------------------------------------
+      elseif length(ind2) == 2
         
-    else
+        meanDif = mean( tsg.EXT_DIF(ind(ind2)) );
+        stdDif = -std( tsg.EXT_DIF(ind(ind2)) );
+        
+        % Case with 1 sample only: the suggested bias is the
+        % TSG/SAMPLE difference and the suggested error is -1
+        % --------------------------------------------------------------------------
+      elseif length(ind2) == 1
         
-        defaultValue = {'0','0'};
+        meanDif = tsg.EXT_DIF(ind(ind2));
+        stdDif = -1;
         
+      end
+      
+      defaultValue = {num2str(meanDif),num2str(stdDif)};
+      
+    else
+      
+      defaultValue = {'0','0'};
+      
     end
     
+  else
     
-    % Enter the bias that will be applied to PARA{1}
-    % ----------------------------------------------
-    prompt = {['Constant value to be applied to the ' PARA{1} ' time series:'],...
-        ['Error value to be applied to the ' PARA{1} ' time series:']};
-    answer = inputdlg(prompt,'Bias adjustment',1,defaultValue);
-    a = answer(1);
-    b = answer(2);
+    defaultValue = {'0','0'};
+    
+  end
+  
+  
+  % Enter the bias that will be applied to PARA{1}
+  % ----------------------------------------------
+  prompt = {['Constant value to be applied to the ' PARA{1} ' time series:'],...
+    ['Error value to be applied to the ' PARA{1} ' time series:']};
+  answer = inputdlg(prompt,'Bias adjustment',1,defaultValue);
+  
+  % if the user clic on cancel, answer is empty, do nothing
+  % --------------------------------------------------------
+  if ~isempty( answer )
     
     % everything OK
     % -------------
     error = 1;
-    if ~isempty( a )
-        
-        % If necessary replace a comma by a point
-        % ---------------------------------------
-        bias = regexprep(a, ',', '.');
-        biasError = regexprep(b, ',', '.');
-        
-        % If bias not a numeric, str2doublereturn a NaN
-        % ------------------------------------
-        bias = str2double( bias );
-        biasError = str2double( biasError );
+    a = answer(1);
+    b = answer(2);
+    
+    % If necessary replace a comma by a point
+    % ---------------------------------------
+    bias = regexprep(a, ',', '.');
+    biasError = regexprep(b, ',', '.');
+    
+    % If bias not a numeric, str2doublereturn a NaN
+    % ------------------------------------
+    bias = str2double( bias );
+    biasError = str2double( biasError );
+    
+    if isnumeric( bias ) && ~isnan( bias)
+      
+      
+      %       if bias ~= 0
+      
+      % The correction is applied to the TSG between dateMin and dateMax
+      % only to measurements with keptCode Quality Codes
+      % ------------------------------------------------------------------------
+      dtTsgQCkept=[];
+      for icode = 1 : length( keptCode )
+        dtTsg = find( tsg.DAYD    >= dateMin  & tsg.DAYD <= dateMax &...
+          tsg.([PARA{1} '_QC']) == keptCode( icode ));
         
-        if isnumeric( bias ) && ~isnan( bias)
-            
-            
-            %       if bias ~= 0
-            
-            % The correction is applied to the TSG between dateMin and dateMax
-            % only to measurements with keptCode Quality Codes
-            % ------------------------------------------------------------------------
-            dtTsgQCkept=[];
-            for icode = 1 : length( keptCode )
-                dtTsg = find( tsg.DAYD    >= dateMin  & tsg.DAYD <= dateMax &...
-                    tsg.([PARA{1} '_QC']) == keptCode( icode ));
-                
-                if ~isempty( dtTsg )
-                    
-                    dtTsgQCkept=[dtTsgQCkept; dtTsg];
-                    
-                    % Compute the corrected value : orignal value + correction
-                    % --------------------------------------------------------
-                    tsg.([PARA{1} '_ADJUSTED'])(dtTsg) = tsg.(PARA{2})(dtTsg) + bias;
-                    
-                    % Attribute an error
-                    % ------------------
-                    tsg.([PARA{1} '_ADJUSTED_ERROR'])(dtTsg) = biasError;
-                    
-                    % Transfer the QC
-                    % ---------------
-                    tsg.([PARA{1} '_ADJUSTED_QC'])(dtTsg) = tsg.([PARA{1} '_QC'])(dtTsg);
-                end
-                
-            end
-            
-            % Update tsg application data
-            % ---------------------------
-            setappdata( hMainFig, 'tsg_data', tsg);
-            
-            % everything OK
-            % -------------
-            error = 1;
+        if ~isempty( dtTsg )
+          
+          dtTsgQCkept = [dtTsgQCkept; dtTsg];
+          
+          % Compute the corrected value : orignal value + correction
+          % --------------------------------------------------------
+          tsg.([PARA{1} '_ADJUSTED'])(dtTsg) = tsg.(PARA{2})(dtTsg) + bias;
+          
+          % Attribute an error
+          % ------------------
+          tsg.([PARA{1} '_ADJUSTED_ERROR'])(dtTsg) = biasError;
+          
+          % Transfer the QC
+          % ---------------
+          tsg.([PARA{1} '_ADJUSTED_QC'])(dtTsg) = tsg.([PARA{1} '_QC'])(dtTsg);
         end
+        
+      end
+      
+      % Update tsg application data
+      % ---------------------------
+      setappdata( hMainFig, 'tsg_data', tsg);
+      
+      % everything OK
+      % -------------
+      error = 1;
     end
     
-else
+  else
     
-    % DateMax <= DateMin
+    % canceled by user
     % ------------------
-    error = -1;
-
+    error = 0;
+  end
+  
+else
+  
+  % DateMax <= DateMin
+  % ------------------
+  error = -1;
+  
 end
 
 
diff --git a/tsgqc.m b/tsgqc.m
index 0518d58..96a6429 100644
--- a/tsgqc.m
+++ b/tsgqc.m
@@ -2698,7 +2698,6 @@ end
     % set active code and color from selected context menu
     % ----------------------------------------------------
     tsg.qc.active.Code  = code;
-    tsg.qc.active.Code
     tsg.qc.active.Color = color;
     
     % Modify and plot the last selected tsg data
@@ -3365,6 +3364,10 @@ end
     
     switch error
       
+      case 0
+      
+        % canceled by user, do nothing
+        
       case 1
         
         % Plot in the 3 axes
@@ -3373,13 +3376,15 @@ end
         
       case -1
         
+        % date mismatch
+        % --------------
         if DateMax <= DateMin
           msgbox( 'Date limits are not correct',...
             'Correction module', 'warn', 'modal');
         end
         
       otherwise
-        msgbox( 'CorBiasCallback : Error different   or -1',' ', 'error');
+        msgbox( {'CorBiasCallback : unknow error', sprintf('error = %d', error)});
         
     end
   end
-- 
GitLab