From e748e8a1938b4edc9bda7f08a3bca306f9c00330 Mon Sep 17 00:00:00 2001
From: Federico Felici <federico.felici@epfl.ch>
Date: Wed, 16 Jan 2019 12:35:40 +0000
Subject: [PATCH] Extended standard test Clarified help examples

git-svn-id: https://spcsvn.epfl.ch/repos/TCV/gdat/trunk@11293 d63d8f72-b253-0410-a779-e742ad2e26cf
---
 crpptbx/gdat.m                  |  20 ++--
 crpptbx/test_all_requestnames.m | 190 +++++++++++++++++++++++++++++---
 2 files changed, 187 insertions(+), 23 deletions(-)

diff --git a/crpptbx/gdat.m b/crpptbx/gdat.m
index dc235198..ed5f95a8 100644
--- a/crpptbx/gdat.m
+++ b/crpptbx/gdat.m
@@ -19,7 +19,6 @@ function [gdat_data,gdat_params,error_status,varargout] = gdat(shot,data_request
 %                               'plot',1 (plot is set by default to 0)
 %                               'machine','TCV' (the default machine is the local machine)
 %
-%
 % Outputs:
 %
 % gdat_data: structure containing the data, the time trace if known and other useful information
@@ -41,16 +40,19 @@ function [gdat_data,gdat_params,error_status,varargout] = gdat(shot,data_request
 % eg.: param1 in gdat_params.param1 and help in gdat_params.help.param1
 %
 % Examples:
-% (should add working examples for various machines (provides working shot numbers for each machine...))
 % 
-%    [a1,a2]=gdat;
-%    a2.data_request = 'Ip';
-%    a3=gdat(48836,a2);  % gives input parameters as a structure, allows to call the same for many shots
-%    a4=gdat('opt1',123,'opt2',[1 2 3],'shot',48832,'data_request','Ip','opt3','aAdB'); % all in pairs
-%    a5=gdat(48836,'ip'); % standard call
-%    a6=gdat(48836,'ip','Opt1',123,'Doplot',1,'opt2','Abc'); % standard call with a few options (all lowercase in output)
+%    [gd,gp] = gdat; display(gd.gdat_request); % lists all available keywords
+%    gp.data_request = 'Ip'; gd = gdat(48836,gp);  % give input parameters as a structure
+%    gd = gdat('opt1',123,'opt2',[1 2 3],'shot',48832,'data_request','Ip','opt3','aAdB'); % give all inputs in pairs
+%
+%    gd = gdat(shot,'ip'); % standard call
+%    gd = gdat(shot,'ip','Opt1',123,'Doplot',1,'opt2','Abc'); % standard call with a few options (all lowercase in output)
+%    
+%    gd = gdat(48836,'ip','doplot',1,'machine','TCV'); % working call for TCV
+%    gd = gdat(35345,'ip','doplot',1,'machine','AUG'); % working call for AUG
 %
-% helps for specific machine: help gdat_tcv; help gdat_aug, help gdat_jet, etc
+%    more detailed examples for specific machine: 
+%        help gdat_tcv; help gdat_aug; help gdat_jet; etc
 %
 
 %
diff --git a/crpptbx/test_all_requestnames.m b/crpptbx/test_all_requestnames.m
index 70ebcc95..cb36376d 100644
--- a/crpptbx/test_all_requestnames.m
+++ b/crpptbx/test_all_requestnames.m
@@ -1,16 +1,178 @@
+function [pass,request_list,err,telaps,skipped] = test_all_requestnames(varargin)
+% test function to run all requestnames
+%  pass = test_gdat_AUG; % default test
+%  pass = test_gdat_AUG('param1',value1,'param2',value2,...); %
+%    INPUT PARAMETERS (optional)
+%       machine: string of machine name (e.g. 'aug'). (default: gdat internal default)
+%       testmode: depth of test: 'reduced' or 'full' (default: 'reduced')
+%       nverbose: 0 for silent, 1 for text, 2 for plots (default: 1)
+%    OUTPUTS
+%       pass: single boolean indicating pass/fail of test
+%                    pass means all error codes were zero
+%       request_list: list of requests
+%       err:     error code of tested requests
+%       telaps:  elapsed time for tested requests
+%       skipped: boolean flag indicating whether request was skipped
 
-% $$$ machine='AUG';
-% $$$ shot=30594;
-% $$$ machine='TCV';
-% $$$ shot=48836;
-% aa=gdat('machine',machine);
-aa=gdat;
-machine = aa.gdat_params.machine;
-all_request_names = aa.gdat_request
-%break
-istart=1;
-for irequest=istart:length(all_request_names)
-  request=all_request_names{irequest}
-  ab_test_all{irequest} = gdat(shot,request,'machine',machine,'doplot',1);
-  pause(0.3)
+[machine,testmode,nverbose] = parse_inputs(varargin{:});
+
+%
+if nverbose>=2
+    doplot = true;
+else
+    doplot = false;
+end
+
+%% get request list
+[gd0,gp0] = gdat;
+if isempty(machine)
+    machine = gp0.machine;
+end
+request_list = gd0.gdat_request;
+
+%% get default test shot for each machine
+switch upper(machine)
+    case 'AUG'
+        shot = 30594;
+    case 'TCV'
+        shot = 48836;
+end
+
+%% possibly skip some since they take too long for a reduced test
+switch testmode
+    case 'reduced'
+        skip = skip_list(machine);
+    case 'full'
+        skip = ''; % skip none
+    otherwise
+        error('invalid testmode, should be ''reduced'' or ''full''');
+end
+
+%% init
+Nreq = numel(gd0.gdat_request);
+err = zeros(Nreq,1);
+telaps = zeros(Nreq,1);
+skipped = false(Nreq,1);
+gdat_call = cell(Nreq,1);
+
+%% call gdat requests in a loop
+for ireq = 1:Nreq
+    myrequest = request_list{ireq};
+    
+    if ~ismember(myrequest,skip)
+        % build request string
+        gdat_call{ireq} = sprintf('gdat_aug(%d,''%s'',''doplot'',%d,''machine'',''%s'')',shot,myrequest,doplot,machine);
+        
+        % eval call
+        [err(ireq),telaps(ireq)] = do_gdat_call(gdat_call{ireq},nverbose);
+        skipped(ireq) = false;
+        pause(0.1)
+    else
+        % skip
+        err(ireq) = 0;
+        telaps(ireq) = 0;
+        skipped(ireq) = true;
+        fprintf('\n skipping gdat request ''%s''\n',myrequest)
+    end
 end
+
+pass = all(~err);
+
+%% Summary display
+if nverbose
+    summary_display(request_list,telaps,err,gdat_call,skipped)
+
+    if pass
+        fprintf('passed %s tests\n',mfilename());
+    else
+        fprintf('test FAILED: %s.m\n',mfilename());
+    end
+end
+
+end
+
+function [machine,testmode,verbose] = parse_inputs(varargin)
+p = inputParser;
+p.addOptional('machine','');
+p.addOptional('testmode','reduced');
+p.addOptional('nverbose',1);
+p.parse(varargin{:});
+
+machine  = p.Results.machine;
+testmode = p.Results.testmode;
+verbose  = p.Results.nverbose;
+end
+
+function skip = skip_list(machine)
+switch upper(machine)
+    case 'AUG'
+        skip = {'cxrs','transp','te_rho','ne_rho','nete_rho','ece_rho','eced_rho','cxrs_rho','eqdsk','equil'};
+    otherwise
+        warning('no list to skip defined for machine ''%s''',machine);
+        skip = '';
+end
+end
+
+function [err,telaps] = do_gdat_call(gdat_call,nverbose)
+if nverbose
+    fprintf('\n calling %s...\n',gdat_call);
+end
+
+tic
+try
+    [~,~,err] = eval(gdat_call);
+catch ME
+    warning('Caught matlab error. Report:\n\n%s',getReport(ME,'extended'));
+    err = -1;
+end
+telaps = toc; % elapsed time
+end
+
+function summary_display(request_list,telaps,err,gdat_call,skipped)
+%%
+Nreq = numel(request_list);
+
+if any(err==0)
+fprintf('\n\n  PASSED:\n')
+print_header();
+for ireq=1:Nreq
+    if ~skipped(ireq) && ~err(ireq)
+        print_result(request_list{ireq},telaps(ireq),err(ireq),gdat_call{ireq})
+    end
+end
+end
+
+if any(skipped) 
+fprintf('\n\n  SKIPPED:\n')
+print_header();
+for ireq=1:Nreq
+    if skipped(ireq)
+        fprintf('   %-20s%-20s\n',request_list{ireq},'skipped');
+    end
+end
+end
+
+if any(err~=0)
+fprintf('\n\n  FAILED:\n')
+print_header();
+for ireq=1:Nreq
+    if ~skipped(ireq) && (err(ireq)~=0)
+        print_result(request_list{ireq},telaps(ireq),err(ireq),gdat_call{ireq})
+    end
+end
+end
+
+fprintf('\n\n   total test time: %2.2f[s]\n\n',sum(telaps));
+
+end
+
+function print_header()
+fprintf('   %-60s\n',char(repmat(int8('-'),1,60)))
+fprintf('   %-20s%-20s%-10s%-100s\n','request','time_elapsed[''s'']','errorcode','gdat call')
+fprintf('   %-60s\n',char(repmat(int8('-'),1,60)))
+end
+
+function print_result(req,telaps,err,callstr)
+fprintf('   %-20s%-20.2f%-10d%-100s\n',req,telaps,err,callstr);
+end
+
-- 
GitLab