From 8144c8924db9c1c2c8af978a086992e22e98835c Mon Sep 17 00:00:00 2001
From: Olivier Sauter <Olivier.Sauter@epfl.ch>
Date: Fri, 5 Mar 2021 17:31:57 +0100
Subject: [PATCH] fix time_out for simple request, fix grids_1d

---
 matlab/TCV/gdat_tcv.m | 63 ++++++++++++++++++++++++++++++++++++++++---
 matlab/get_grids_1d.m | 15 ++++++++---
 2 files changed, 72 insertions(+), 6 deletions(-)

diff --git a/matlab/TCV/gdat_tcv.m b/matlab/TCV/gdat_tcv.m
index b6bd9e2f..70fb1011 100644
--- a/matlab/TCV/gdat_tcv.m
+++ b/matlab/TCV/gdat_tcv.m
@@ -574,6 +574,8 @@ if strcmp(mapping_for_tcv.method(1:3),'tdi')
   end
   if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
     [gdat_data] = gdat2time_out(gdat_data,1);
+  elseif ~isfield(gdat_data.gdat_params,'time_out')
+    gdat_data.gdat_params.time_out = [];
   end
   gdat_data.units = aatmp.units;
   gdat_data.dimunits = aatmp.dimunits;
@@ -664,6 +666,8 @@ elseif strcmp(mapping_for_tcv.method,'expression')
     end
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
   end
   % end of method "expression"
@@ -774,6 +778,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     end
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -811,6 +817,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     end
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -867,6 +875,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     % Should implement at server level at some point, could exend tdi_os
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -1102,6 +1112,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
         others_as_data
       end
       [gdat_data] = gdat2time_out(gdat_data,1,others_as_data);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -1313,6 +1325,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     gdat_data.dimunits{1} = 's';
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -1345,6 +1359,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     gdat_data.mapping_for.tcv.gdat_timedim = 2;
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -1852,6 +1868,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
       end
 
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -1954,6 +1972,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     [gdat_data] = get_thomson_raw_data(shot,data_request_eff,gdat_data,gdat_data.gdat_params.systems,gdat_params.nverbose,0);
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1,{'error_bar'});
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -2248,6 +2268,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
           gdat_data.(extra_sub{i}) = rmfield(ab_tmp_rho,{'gdat_params','mapping_for','t'});
         end
       end
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -2448,6 +2470,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
         gdat_data.(sources_avail{i}) = rmfield(ab_tmp_rho,{'gdat_params','mapping_for'});
       end
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -2485,6 +2509,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     gdat_data = get_grids_1d(gdat_data,1,1,gdat_params.nverbose);
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,21);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -2525,6 +2551,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       % add extra fields then correct
       [gdat_data] = gdat2time_out(gdat_data,21);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
    case {'phi_tor', 'phitor', 'toroidal_flux'}
     % Phi(LCFS) = int(Bphi dSphi), can use Eq.(11) of "Tokamak coordinate conventions: COCOS" paper:
@@ -2626,6 +2654,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       % add extra fields then correct
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -2653,6 +2683,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     gdat_data.request_description = 'pprime=dp/dpsi';
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
    case {'pressure', 'pressure_rho', 'p_rho'}
     if liuqe_matlab==0
@@ -2678,6 +2710,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     gdat_data.request_description = 'pressure';
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -2713,6 +2747,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     gdat_data.vsurf_description = ['time derivative from surface_psi, obtained from \results::surface_flux'];
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -2735,6 +2771,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     gdat_data.units = tracetdi.units;
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -2769,6 +2807,18 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
         return
       end
       rhoequal = linspace(0,1,length(q_rho.dim{1}));
+      if isfield(params_eff,'rhopol') && ~isempty(params_eff.rhopol)
+        if numel(params_eff.rhopol) ~= 1
+          rhoequal = params_eff.rhopol;
+        elseif params_eff.rhopol > 0
+          rhoequal = linspace(0,1,params_eff.rhopol); % equidistant rhopol with given nb points
+        elseif params_eff.rhopol < 0
+          rhoequal = sqrt(linspace(0,1,abs(params_eff.rhopol))); % equidistant psinorm with given nb points
+        end
+        rhoequal = reshape(rhoequal,1,numel(rhoequal));
+      else
+        params_eff.rhopol = []; % means keep default rho mesh
+      end
       if strcmp(data_request_eff,'rhotor_edge')
         gdat_data.data = psi_axis.data; % to have the dimensions correct
         gdat_data.dim = psi_axis.dim;
@@ -2777,10 +2827,10 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
         gdat_data.units = 'T m^2';
         gdat_data.dimunits{1} = 's';
       elseif strcmp(data_request_eff,'rhotor_norm') || strcmp(data_request_eff,'rhotor')
-        gdat_data.data = q_rho.data; % to have the dimensions correct
-        gdat_data.dim{1} = ones(size(q_rho.dim{1}));
-        gdat_data.dim{1}(:) = rhoequal;
+        % keep orientation as original
+        gdat_data.dim{1} = shiftdim(rhoequal,abs(find(size(q_rho.dim{1})==1)-find(size(rhoequal)==1)));
         gdat_data.dim{2} = q_rho.dim{2};
+        gdat_data.data = NaN(numel(gdat_data.dim{1}),numel(gdat_data.dim{2})); % to have the dimensions correct
         gdat_data.t = gdat_data.dim{2};
         if strcmp(data_request_eff,'rhotor_norm')
           gdat_data.data_fullpath='sqrt(phitor/phitor_edge), rhotor_edge=sqrt(phitor(1)/B0/pi)';
@@ -2934,6 +2984,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     gdat_data.t = gdat_data.dim{mapping_for_tcv.gdat_timedim};
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1,{'volume_edge'});
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -3190,6 +3242,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     gdat_data.request_description = 'ttprime=t*dt/dpsi';
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -3246,6 +3300,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     gdat_data.mapping_for.tcv.gdat_timedim = 2;
     if isfield(gdat_data.gdat_params,'time_out') && ~isempty(gdat_data.gdat_params.time_out)
       [gdat_data] = gdat2time_out(gdat_data,1);
+    elseif ~isfield(gdat_data.gdat_params,'time_out')
+      gdat_data.gdat_params.time_out = [];
     end
 
    otherwise
@@ -3397,6 +3453,7 @@ for is = 1:nsys
     data_tmp(is).error_bar = [];
     data_tmp(is).data_fullpath = '';
     data_tmp(is).dim = {[],[]};
+    data_tmp(is).system = {};
     continue
   end
 
diff --git a/matlab/get_grids_1d.m b/matlab/get_grids_1d.m
index f8e00e4b..b5822569 100644
--- a/matlab/get_grids_1d.m
+++ b/matlab/get_grids_1d.m
@@ -39,12 +39,21 @@ rhotor_norm = gdat(gdat_data.shot,params_eff);
 ndim_x_rhotor = length(find(size(rhotor_norm.x)>1));
 params_eff.data_request='rhovol';
 rhovol = gdat(gdat_data.shot,params_eff);
-ndim_x_rhovol = length(find(size(rhotor_norm.x)>1));
+ndim_x_rhovol = length(find(size(rhovol.x)>1));
 % check that rhotor and rhovol come from same equil psi
 if sum(abs(size(rhotor_norm.x)-size(rhovol.x)))~=0 || sum(sum(abs(rhotor_norm.x-rhovol.x)))>1e-10 ...
       || sum(abs(rhotor_norm.t-rhovol.t))>1e-10;
-  error(['get_grids_1d: x and t arrays for rhotor_norm and rhovol are different, although should refer to same equilibrium' char(10) ...
-         'size(rhotor_norm.x) = ' num2str(size(rhotor_norm.x)) '; size(rhovol.x) = ' num2str(size(rhovol.x))]);
+  % try imposing same rhopol as rhovol on rhotor (since latter is usually integrating/interpolating anyway from q most of the time)
+  params_eff.data_request='rhotor_norm';
+  params_eff.rhopol = rhovol.x;
+  rhotor_norm = gdat(gdat_data.shot,params_eff);
+  ndim_x_rhotor = length(find(size(rhotor_norm.x)>1));
+  if sum(abs(size(rhotor_norm.x)-size(rhovol.x)))~=0 || sum(sum(abs(rhotor_norm.x-rhovol.x)))>1e-10 ...
+        || sum(abs(rhotor_norm.t-rhovol.t))>1e-10;
+    warning(['get_grids_1d: x and t arrays for rhotor_norm and rhovol are different, although should refer to same equilibrium' char(10) ...
+             'size(rhotor_norm.x) = ' num2str(size(rhotor_norm.x)) '; size(rhovol.x) = ' num2str(size(rhovol.x))]);
+    return
+  end
 end
 params_eff.data_request='psi_axis';
 psi_axis = gdat(gdat_data.shot,params_eff);
-- 
GitLab