From be685349426b18502e16bde31d59a0b4e4df4a5d Mon Sep 17 00:00:00 2001
From: Olivier Sauter <Olivier.Sauter@epfl.ch>
Date: Fri, 16 Apr 2021 14:52:27 +0200
Subject: [PATCH] add h89p gas_valve and add automatic shots in legend with
 gdat_plot

---
 matlab/TCV/gdat_tcv.m             | 44 ++++++++++++++++++++++++++++---
 matlab/TCV/tcv_requests_mapping.m | 12 ++++++++-
 matlab/gdat_plot.m                | 21 ++++++++++-----
 3 files changed, 67 insertions(+), 10 deletions(-)

diff --git a/matlab/TCV/gdat_tcv.m b/matlab/TCV/gdat_tcv.m
index 70fb1011..afc34429 100644
--- a/matlab/TCV/gdat_tcv.m
+++ b/matlab/TCV/gdat_tcv.m
@@ -1287,7 +1287,7 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
                     'plot_eqdsk, write_eqdsk, read_eqdsk can be used'];
 
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-   case {'gas', 'gas_flux', 'gas_request', 'gas_feedforward'}
+   case {'gas', 'gas_flux', 'gas_request', 'gas_feedforward','gas_valve'}
     params_eff = gdat_data.gdat_params;
     params_eff.data_request = '\diagz::flux_gaz:piezo_1:flux';
     gasflux = gdat_tcv(gdat_data.shot,params_eff);
@@ -1318,11 +1318,49 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
        gdat_data.data = gdat_data.gas_feedforward_volt.data;
        gdat_data.units = gdat_data.gas_feedforward_volt.units;
        gdat_data.t = gdat_data.gas_feedforward_volt.t;
+     case {'gas_valve'}
+      if ~isfield(params_eff,'source') || isempty(params_eff.source)
+        params_eff.source = {{1,'D2'}, {3,'N2'}};
+      end
+      if iscell(params_eff.source) && numel(params_eff.source)>0 && numel(params_eff.source{1})>1
+        gdat_data.units = '';
+        for i=1:numel(params_eff.source)
+          try
+            [aat,aad] = get_gas_flux(shot,params_eff.source{i}{1},params_eff.source{i}{2});
+            gdat_data.gas_valve(i).t = aat;
+            gdat_data.gas_valve(i).data = aad;
+            gdat_data.gas_valve(i).valve_nb = params_eff.source{i}{1};
+            gdat_data.gas_valve(i).gas_type = params_eff.source{i}{2};
+            gdat_data.gas_valve(i).units = 'Molecules/s';
+            if ~strcmp(gdat_data.gas_valve(i).gas_type(end),'2'); gdat_data.gas_valve(i).units = 'atoms/s'; end
+            gdat_data.data(:,i) = gdat_data.gas_valve(i).data;
+            gdat_data.t = gdat_data.gas_valve(i).t;
+            gdat_data.dim{1}(i) = gdat_data.gas_valve(i).valve_nb;
+            gdat_data.units{i} = gdat_data.gas_valve(i).units;
+            gdat_data.label = [gdat_data.label ',' gdat_data.gas_valve(i).gas_type];
+            if i==1
+              gdat_data.dim{2} = gdat_data.t;
+              gdat_data.dimunits{1} = 'valve nb';
+              gdat_data.dimunits{2} = 's';
+              gdat_data.mapping_for.tcv.gdat_timedim = 2;
+              gdat_data.mapping_for.tcv.timedim = 2;
+            end
+          catch ME
+            gdat_data.gas_valve(i).t = [];
+            gdat_data.gas_valve(i).data = [];
+            gdat_data.gas_valve(i).valve_nb = params_eff.source{i}{1};
+            gdat_data.gas_valve(i).gas_type = params_eff.source{i}{2};
+            gdat_data.gas_valve(i).units = '';
+          end
+        end
+      end
      otherwise
       error('gas option not defined')
     end
-    gdat_data.dim{1} = gdat_data.t;
-    gdat_data.dimunits{1} = 's';
+    if ~strcmp(data_request_eff,'gas_valve')
+      gdat_data.dim{1} = gdat_data.t;
+      gdat_data.dimunits{1} = 's';
+    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')
diff --git a/matlab/TCV/tcv_requests_mapping.m b/matlab/TCV/tcv_requests_mapping.m
index 354eac28..c850da91 100644
--- a/matlab/TCV/tcv_requests_mapping.m
+++ b/matlab/TCV/tcv_requests_mapping.m
@@ -136,7 +136,7 @@ switch lower(data_request)
   mapping.timedim = 0;
   mapping.method = 'tdi';
   mapping.expression = '\rtc::node03.params.cfg_file';
- case {'gas', 'gas_flux', 'gas_request', 'gas_feedforward'}
+ case {'gas', 'gas_flux', 'gas_request', 'gas_feedforward','gas_valve'}
   mapping.timedim = 1;
   mapping.label = 'gas flux';
   mapping.method = 'switchcase';
@@ -151,6 +151,16 @@ switch lower(data_request)
   mapping.label = 'Halpha';
   mapping.method = 'switchcase';
   mapping.expression = '';
+ case 'h89p'
+  mapping.timedim = 1;
+  mapping.label = 'H89P';
+  mapping.method = 'expression';
+  mapping.expression = ['params_eff = gdat_data.gdat_params;params_eff.data_request=''\tcv_shot::top.results.conf:tau'';' ...
+                    'gdat_tmp=gdat_tcv(shot,params_eff);params_eff.data_request=''\tcv_shot::top.results.conf:taue'';' ...
+                    'gdat_tmp2=gdat_tcv(shot,params_eff);'...
+                    'params_eff.data_request=''\tcv_shot::top.results.conf:h_scal'';' ...
+                    'gdat_tmp3=gdat_tcv(shot,params_eff);ih89p=4;' ...
+                    'ih89p=5;if size(gdat_tmp3.data,1)>=ih89p;gdat_tmp.data = gdat_tmp.data./gdat_tmp2.data .* gdat_tmp3.data(ih89p,:)'';end;'];
  case 'h98y2'
   mapping.timedim = 1;
   mapping.label = 'H98y2';
diff --git a/matlab/gdat_plot.m b/matlab/gdat_plot.m
index 866c2411..91712773 100644
--- a/matlab/gdat_plot.m
+++ b/matlab/gdat_plot.m
@@ -1,4 +1,4 @@
-function [fighandle]=gdat_plot(gdat_data,varargin);
+function [fighandle,linehandles]=gdat_plot(gdat_data,varargin);
 %
 % choices from doplot in gdat_data.gdat_params.doplot:
 %     doplot = 0: no plot
@@ -9,6 +9,7 @@ function [fighandle]=gdat_plot(gdat_data,varargin);
 %
 
 fighandle = NaN;
+linehandles = [];
 gdat_plot_params.dummy=NaN;
 if mod(nargin-1,2)==0
   for i=1:2:nargin-1
@@ -57,17 +58,17 @@ if all(isfield(gdat_data,{'data','t'})) && ~isempty(gdat_data.data) && ~isempty(
     if iscell(gdat_data.eqdsk); endstr = '{1}'; end
     eval(['contour(gdat_data.eqdsk' endstr '.rmesh,gdat_data.eqdsk' endstr '.zmesh,gdat_data.eqdsk' endstr '.psi'',100);'])
     hold on
-    eval(['plot(gdat_data.eqdsk' endstr '.rplas,gdat_data.eqdsk' endstr '.zplas,''k'');'])
-    eval(['plot(gdat_data.eqdsk' endstr '.rlim,gdat_data.eqdsk' endstr '.zlim,''k'');'])
+    eval(['linehandles(end+1) = plot(gdat_data.eqdsk' endstr '.rplas,gdat_data.eqdsk' endstr '.zplas,''k'');'])
+    eval(['linehandles(end+1) = plot(gdat_data.eqdsk' endstr '.rlim,gdat_data.eqdsk' endstr '.zlim,''k'');'])
     axis equal;
     title(eval(['gdat_data.eqdsk' endstr '.stitle']));
   elseif any(find(size(gdat_data.data)==length(gdat_data.t)))
     try
       if length(size(gdat_data.data))<=2
         if isnumeric(gdat_data.t)
-          plot(gdat_data.t,gdat_data.data);
+          linehandles(end+1) = plot(gdat_data.t,gdat_data.data);
         elseif iscell(gdat_data.t)
-          plot(str2num(cell2mat(gdat_data.t)),gdat_data.data);
+          linehandles(end+1) = plot(str2num(cell2mat(gdat_data.t)),gdat_data.data);
         end
       else
         disp('WARNING')
@@ -83,7 +84,11 @@ if all(isfield(gdat_data,{'data','t'})) && ~isempty(gdat_data.data) && ~isempty(
       return
     end
     if ~isfield(gdat_data,'shot'); return; end % allows to plot if just .t and .data exist
-    title([gdat_data.gdat_params.machine ' #' num2str(gdat_data.shot)]);
+    if strcmp(get(gcf,'nextplot'),'add')
+      title([gdat_data.gdat_params.machine]);
+    else
+      title([gdat_data.gdat_params.machine ' #' num2str(gdat_data.shot)]);
+    end
     if isfield(gdat_data,'mapping_for')
       dimunits_x = gdat_data.dimunits{gdat_data.mapping_for.(gdat_data.gdat_params.machine).gdat_timedim};
       if ischar(dimunits_x)
@@ -118,6 +123,10 @@ if all(isfield(gdat_data,{'data','t'})) && ~isempty(gdat_data.data) && ~isempty(
     end
     zoom on;
   end
+  for i=1:numel(linehandles)
+    set(linehandles(i),'DisplayName',num2str(gdat_data.shot));
+  end
+  legend show
   if strcmp(gdat_data.gdat_request,'mhd')
     % special case, add legend instead of ylabel
     delete(hylabel);
-- 
GitLab