From 726e1aed4f688d6c4df348dc8a8eaa29a16ec7f2 Mon Sep 17 00:00:00 2001
From: Olivier Sauter <olivier.sauter@epfl.ch>
Date: Sun, 2 Jun 2024 13:53:22 +0200
Subject: [PATCH] add radcam 4 cameras top upper euatorial and bottom or 1 to 4

fix subcall and cell2str
---
 matlab/TCV/gdat_tcv.m            | 121 ++++++++++++++++++++++++++++---
 matlab/TCV/tcv_help_parameters.m |  10 ++-
 matlab/subcall_all2str.m         |   6 +-
 3 files changed, 116 insertions(+), 21 deletions(-)

diff --git a/matlab/TCV/gdat_tcv.m b/matlab/TCV/gdat_tcv.m
index deae6b52..92660f92 100644
--- a/matlab/TCV/gdat_tcv.m
+++ b/matlab/TCV/gdat_tcv.m
@@ -1740,7 +1740,8 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
           gdat_data.ec.ec_data = ec_data;
           filled_successfully = true; %set flag to true
         catch ME
-          warning(ME.identifier,'Problem retrieving TORAY data. \nError message: %s',ME.message);
+          warning('Problem retrieving TORAY data. \nError message: %s',ME.message);
+          getReport(ME) % without ; to get output
           % try to identify cause of the error
           if ~check_nodes_filled(shot,'toray')
             if ~any(ec_inputs.launchers_active.data,1)
@@ -3308,17 +3309,20 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
 
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-   case {'sxr', 'mpx'}
+   case {'sxr', 'mpx', 'radcam'}
 
-    if strcmp(data_request_eff,'mpx')
-      data_request_eff = 'mpx'; % mpx chosen through parameter 'source' within 'sxr'
-      gdat_data.data_request = data_request_eff;
-      gdat_data.gdat_params.source = 'mpx';
+    if any(contains(data_request_eff,{'mpx','radcam'}))
+      % effective source chosen through parameter 'source' within 'sxr'
+      gdat_data.gdat_params.source = data_request_eff;
     end
     % sxr from dmpx by default or xtomo if 'camera','xtomo' is provided
     if ~isfield(gdat_data.gdat_params,'source') || isempty(gdat_data.gdat_params.source)
-      gdat_data.gdat_params.source = 'mpx';
-    elseif ~strcmp(lower(gdat_data.gdat_params.source),'xtomo') && ~strcmp(lower(gdat_data.gdat_params.source),'mpx')
+      if shot < 70511
+        gdat_data.gdat_params.source = 'mpx';
+      else
+        gdat_data.gdat_params.source = 'radcam';
+      end
+    elseif ~any(contains(lower(gdat_data.gdat_params.source),{'xtomo','mpx','radcam'}))
       if gdat_data.gdat_params.nverbose>=1
         warning(['source = ' gdat_data.gdat_params.source ' not expected with data_request= ' data_request_eff])
       end
@@ -3339,10 +3343,101 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
                  num2str(time_interval(1)) ',' num2str(time_interval(2)) ']' char(10)])
       end
     end
-    if ~isfield(gdat_data.gdat_params,'freq')
-      gdat_data.gdat_params.freq = 'slow';
-    end
     switch lower(gdat_data.gdat_params.source)
+     case {'radcam'}
+      gdat_data.gdat_params.source = 'radcam';
+      gdat_data.camera_list  = {'top', 'upper','equatorial','bottom'};
+      gdat_data.channel_list = {[1:20],[21:40],[41:80],     [81:100]};
+      if ~isfield(gdat_data.gdat_params,'camera') || isempty(gdat_data.gdat_params.camera)
+        gdat_data.gdat_params.camera = {'equatorial'}; % default equatorial
+        disp(['loads all radcam cameras, each data also in subfield ' gdat_data.gdat_params.camera])
+      else
+        if ischar(gdat_data.gdat_params.camera) || (isstring(gdat_data.gdat_params.camera) && numel(gdat_data.gdat_params.camera)==1)
+          gdat_data.gdat_params.camera = {char(gdat_data.gdat_params.camera)};
+        end
+        if isnumeric(gdat_data.gdat_params.camera) && (min(gdat_data.gdat_params.camera)<5 && max(gdat_data.gdat_params.camera)>0)
+          ij = gdat_data.gdat_params.camera([gdat_data.gdat_params.camera>0 & gdat_data.gdat_params.camera<5]);
+          gdat_data.gdat_params.camera = gdat_data.camera_list(ij);
+        elseif any(startsWith(gdat_data.gdat_params.camera,gdat_data.camera_list).*endsWith(gdat_data.gdat_params.camera,gdat_data.camera_list))
+          ij = startsWith(gdat_data.gdat_params.camera,gdat_data.camera_list).*endsWith(gdat_data.gdat_params.camera,gdat_data.camera_list);
+          gdat_data.gdat_params.camera = lower(gdat_data.gdat_params.camera(ij>0));
+          if any(ij==0)
+            warning(['camera not defined: ' gdat_data.gdat_params.camera(ij==0)]);
+            disp(['list of cameras: ' gdat_data.camera_list])
+          end
+        else
+          error(['camera: ' gdat_data.gdat_params.camera ' not in list: ' gdat_data.camera_list])
+        end
+      end
+      if ~isfield(gdat_data.gdat_params,'channel') || isempty(gdat_data.gdat_params.channel)
+        for i=1:numel(gdat_data.gdat_params.camera)
+          gdat_data.gdat_params.(gdat_data.gdat_params.camera{i}).channel = ...
+              gdat_data.channel_list{[contains(gdat_data.camera_list,gdat_data.gdat_params.camera{i})]};
+          gdat_data.gdat_params.channel{i} = gdat_data.gdat_params.(gdat_data.gdat_params.camera{i}).channel;
+        end
+      else
+        if isnumeric(gdat_data.gdat_params.channel)
+          gdat_data.gdat_params.channel{1} = gdat_data.gdat_params.channel;
+        end
+        if numel(gdat_data.gdat_params.camera) ~= numel(gdat_data.gdat_params.channel)
+          gdat_data.gdat_params
+          error('expects same number of camera as number of channel group in each cell list')
+        end
+        all_channels = [];
+        for i=1:numel(gdat_data.gdat_params.camera)
+          all_channels(end+1:end+numel(gdat_data.gdat_params.channel{i})) = gdat_data.gdat_params.channel{i};
+        end
+        % check camera and channel chosen are consistent within camera interval, just redistribute
+        icount = 0;
+        for i=1:numel(gdat_data.camera_list)
+          ij = intersect(gdat_data.channel_list{i},all_channels);
+          if ~isempty(ij)
+            icount = icount + 1;
+            camera{icount} = gdat_data.camera_list{i};
+            channel{icount} = ij;
+          end
+        end
+        if ~isequal(sort(camera),sort(gdat_data.gdat_params.camera))
+          disp('***************************************************************************')
+          warning(sprintf('channel nbs and camera did not match, camera chosen adapted: %s %s %s %s',camera{:}));
+          disp('***************************************************************************')
+          gdat_data.gdat_params.camera = camera;
+        end
+        if ~isequal(sort(cell2mat(channel)),sort(cell2mat(gdat_data.gdat_params.channel)))
+          warning(sprintf('channel nbs and channel did not match, channel chosen adapted'));
+          gdat_data.gdat_params.channel = channel;
+        end
+        for i=1:numel(gdat_data.gdat_params.camera)
+          gdat_data.gdat_params.(gdat_data.gdat_params.camera{i}).channel = gdat_data.gdat_params.channel{i};
+        end
+      end
+      gdat_data.x = [];
+      for i=1:numel(gdat_data.gdat_params.camera)
+        gdat_data.(gdat_data.gdat_params.camera{i}).x = gdat_data.gdat_params.(gdat_data.gdat_params.camera{i}).channel;
+        gdat_data.x(end+1:end+numel(gdat_data.(gdat_data.gdat_params.camera{i}).x)) = gdat_data.(gdat_data.gdat_params.camera{i}).x;
+      end
+      sxr = rc_load_diodes(shot,'diag_name',"sxr",'channels',gdat_data.x); % since all cameras with different channel number
+      gdat_data.data = sxr.data(:,[sxr.channels==gdat_data.x])';
+      gdat_data.t = sxr.time;
+      gdat_data.r_x = sxr.geometry.xchord([sxr.channels==gdat_data.x],:);
+      gdat_data.z_x = sxr.geometry.ychord([sxr.channels==gdat_data.x],:);
+      gdat_data.good_channels = sxr.good_channels;
+      gdat_data.data_fullpath = ['using  rc_load_diodes(shot,''diag_name'',"sxr",...) with params in gdat_data.gdat_params'];
+      gdat_data.units = 'au';
+      gdat_data.dim = {gdat_data.x, gdat_data.t};
+      gdat_data.dimunits = {'channel', 's'};
+      gdat_data.label = strtrim(sprintf('radcam %s %s %s %s %s',gdat_data.gdat_params.camera{:}));
+      for i=1:numel(gdat_data.gdat_params.camera)
+        ij = iround_os(sxr.channels,gdat_data.(gdat_data.gdat_params.camera{i}).x);
+        gdat_data.(gdat_data.gdat_params.camera{i}).data = sxr.data(:,ij)';
+        gdat_data.(gdat_data.gdat_params.camera{i}).t = sxr.time;
+        gdat_data.(gdat_data.gdat_params.camera{i}).r_x = sxr.geometry.xchord(ij,:);
+        gdat_data.(gdat_data.gdat_params.camera{i}).z_x = sxr.geometry.ychord(ij,:);
+        gdat_data.(gdat_data.gdat_params.camera{i}).good_channels = intersect(gdat_data.(gdat_data.gdat_params.camera{i}).x,sxr.good_channels);
+        gdat_data.(gdat_data.gdat_params.camera{i}).label = sprintf('radcam %s nb chords: %d', ...
+          gdat_data.gdat_params.camera{i},numel(gdat_data.(gdat_data.gdat_params.camera{i}).x));
+      end
+
      case {'mpx', 'dmpx'}
       gdat_data.gdat_params.source = 'mpx';
       if ~isfield(gdat_data.gdat_params,'camera') || isempty(gdat_data.gdat_params.camera)
@@ -3357,6 +3452,9 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
         return
       end
       freq_opt = 0;
+      if ~isfield(gdat_data.gdat_params,'freq')
+        gdat_data.gdat_params.freq = 'slow';
+      end
       if strcmp(gdat_data.gdat_params.freq,'fast'); freq_opt = 1; end
       t_int = [0 10]; % starts from 0 otherwise mpxdata gives data from t<0
       if ~isempty(time_interval); t_int = time_interval; end
@@ -3474,7 +3572,6 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
 
     end
 
-
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    case {'transp'}
     % read transp netcdf output file provided in source parameter and generate substructure with all variables
diff --git a/matlab/TCV/tcv_help_parameters.m b/matlab/TCV/tcv_help_parameters.m
index 2c6c74c0..79d5de58 100644
--- a/matlab/TCV/tcv_help_parameters.m
+++ b/matlab/TCV/tcv_help_parameters.m
@@ -56,8 +56,12 @@ help_struct_all.error_bar = sprintf('%s\n','for ids: choice of nodes fill in and
           '''delta'' (default): only upper fill in  with the abs(value) to add or subtract to data to get upper and lower values (symmetric)', ...
           '''delta_with_lower'': same as delta but fill in lower node as well (with delta as well, same as upper)', ...
           '''added'': add the delta values (old cpo style), so upper=data+error_bar and lower=data+error_bar');
-help_struct_all.camera = ['sxr: for MPX: ''central'', ''top'' (default), ''bottom'' or ''both'' ; ' ...
-                    ' for XTOMO: ''central'' (a central chord only), defaults if empty, [1 3 5] if only camera 1, 3 and 5 are desired'];
+help_struct_all.camera = sprintf('%s\n%s\n%s', ...
+          'sxr: for radcam: 1 to 4 or ''top'' (default), ''upper'', ''equatorial'', ''bottom'' array or cell array ok as well;', ...
+          ' for MPX: ''central'', ''top'' (default), ''bottom'' or ''both'' ;', ...
+          ' for XTOMO: ''central'' (a central chord only), defaults if empty, [1 3 5] if only camera 1, 3 and 5 are desired');
+help_struct_all.channel = sprintf('%s\n%s\n%s', ...
+          'radcam: chord to choose within camera interval, or simply chords, then it is re-distributed to correct camera');
 help_struct_all.freq = '''slow'', default, lower sampling; ''fast'' full samples for both mpx and xtomo';
 help_struct_all.max_adcs = 'rtc: source=''adcs'' maximum nb of adc channels loaded for each board in each active node';
 help_struct_all.nfft = '512 (default) changes time resolution in spectrogram in gdat_plot for ''mhd'' request';
@@ -65,8 +69,6 @@ help_struct_all.map_eqdsk_psirz = 'eqdsk: if time array provided, maps all psi(R
 help_struct_all.write = 'eqdsk: write eqdsk while loading data (1, default) or not (0)';
 %help_struct_all. = '';
 
-
-
 if ~exist('parameter_list') || isempty(parameter_list)
   help_struct = help_struct_all;
   return
diff --git a/matlab/subcall_all2str.m b/matlab/subcall_all2str.m
index 6e5d1ee1..8966d184 100644
--- a/matlab/subcall_all2str.m
+++ b/matlab/subcall_all2str.m
@@ -56,11 +56,7 @@ for i_in=1:length(varargin)
   elseif ischar(var_to_treat)
     subcall = [subcall ',''' var_to_treat ''''];
   elseif iscell(var_to_treat)
-    subcall = [subcall ',{''' var_to_treat{1} ''''];
-    for i=2:length(var_to_treat)
-      subcall = [subcall ','''  var_to_treat{i} ''''];
-    end
-    subcall = [subcall '}'];
+    subcall = [subcall ',' cell2str(var_to_treat,3) ''];
   else
     warning('in subcall_all2str: case not foreseen');
   end
-- 
GitLab