From 278098eb642edc6485761c7bdc2b1d18ee9e732a Mon Sep 17 00:00:00 2001
From: Olivier Sauter <olivier.sauter@epfl.ch>
Date: Mon, 28 Apr 2014 15:49:43 +0000
Subject: [PATCH] function to run line command torbeam via tbdemo and to read
 output file

git-svn-id: https://spcsvn.epfl.ch/repos/TCV/gdat/trunk@4420 d63d8f72-b253-0410-a779-e742ad2e26cf
---
 crpptbx/AUG/run_torbeam_fullshot.m | 79 ++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)
 create mode 100644 crpptbx/AUG/run_torbeam_fullshot.m

diff --git a/crpptbx/AUG/run_torbeam_fullshot.m b/crpptbx/AUG/run_torbeam_fullshot.m
new file mode 100644
index 00000000..2b0c09b4
--- /dev/null
+++ b/crpptbx/AUG/run_torbeam_fullshot.m
@@ -0,0 +1,79 @@
+function [torbeam_out_struct, file_out_prefix]=run_torbeam_fullshot(shot,varargin);
+%
+% Run tbdemo, using line command execution of TORBEAM, to run torbeam acroos full shot on reduced time axis
+%
+% shot: shot number for which to run Torbeam. 
+%       If empty or not given, ask for it
+%       if negative, assume output file exists, do not re-run TORBEAM and just reads data into structure torbeam_out_struct
+%
+% varargin{1:2}: 'time',time_array: time points for which to run Torbeam (by default run every 50ms for each ECRH2 gyrotron having power>0)
+% varargin{3:4}: 'file_prefix', filename_prefix: results are saved on [filename '_gyroXX.dat'] (default: tbdemo_shot_gyroXX.dat with XX being the gyro nb)
+%
+% Examples:
+%      torbeam_out_struct = run_torbeam_fullshot(shot);   % standard call to run Torbeam
+%      torbeam_out_struct = run_torbeam_fullshot(-shot);  % standard call to just read the data from the local file from a previous run
+%
+%      torbeam_out_struct = run_torbeam_fullshot(shot,'time',linspace(tstart,tend,nbpoints)); % to run on given time array
+%      torbeam_out_struct = run_torbeam_fullshot(shot,'time',[t1 t2 t3...],'file_prefix','tbdemo_shot_fewtimes_gyro'); % to run on specific times
+
+if ~exist('shot')
+  shot=input('shot: ');
+end
+
+time_array=linspace(0,10,10/0.05);
+file_out_prefix = ['tbdemo_' num2str(abs(shot)) '_gyro'];
+
+if nargin>=3 
+  for i=1:2:length(varargin)-1
+    if ~isempty(varargin{i}) && ~isempty(varargin{i+1})
+      switch lower(varargin{i})
+       case {'time'}
+	time_array = varargin{i+1};
+       case {'file_prefix'}
+	file_out_prefix = varargin{i+1};
+       otherwise
+	disp(['case ' lower(varargin{i}) ' not yet implemented, ask Olivier.Sauter@epfl.ch'])
+	return
+      end
+    end
+  end
+end
+
+if shot>0
+  pgyro=gdat(shot,'pgyro',1);
+
+  for igyro=5:8
+    filename=[file_out_prefix num2str(igyro) '.dat'];
+    unix(['rm ' filename ' >& /dev/null']); % since cannot write over an existing file by default
+    unix(['rm ' '.' filename ' >& /dev/null']); % since cannot write over an existing file by default
+    unix(['touch ' '.' filename]);
+    itt=find(pgyro.data(:,igyro)>1e5);
+    if ~isempty(itt)
+      itt_tok=find(time_array>=pgyro.t(itt(1)) & time_array<=pgyro.t(itt(end)));
+      if ~isempty(itt_tok)
+	pgyro_tok=interp1(pgyro.data(itt,igyro),time_array(itt_tok));
+	disp(['running system ' num2str(igyro) '...']);
+	for j=1:length(itt_tok)
+	  if pgyro_tok(j)>1e5
+	    [a,b]=unix(['ssh sxaug21.aug.ipp.mpg.de ~rem/public/tbm_demo/tbmdemo -batch -silent -shot ' num2str(shot) ' -time ' num2str(time_array(itt_tok(j))) ' -system ' num2str(igyro) ' -usedcr -eqdiag EQI -betapol -usemirror -cdrive 1 >> ' ['.' filename]]);
+	  end
+	end
+	[a,b]=unix(['grep -v ''could not''' ' .' filename '| grep -v ''invalid'' > ' filename]);
+      end
+    end
+  end
+end
+
+for igyro=5:8
+  try
+    eval(['[gyro_tbnam,tbout{igyro}.gyro,shot_tbnam,tbout{igyro}.shot,time_tbnam,tbout{igyro}.time,pol_tbnam,tbout{igyro}.pol,tor_tbnam,tbout{igyro}.tor,' ...
+	  'pow_tbnam,tbout{igyro}.pow,r_tbnam,tbout{igyro}.r,z_tbnam,tbout{igyro}.z,pwr_dep_tbnam,tbout{igyro}.rhopol_pwr_dep,rhopol_cd_dep_tbnam,tbout{igyro}.rhopol_cd_dep,jcd_peak_tbnam,' ...
+	  'tbout{igyro}.jcd_peak,rhopol_c_wid_tbnam,tbout{igyro}.rhopol_c_wid,pni_tbnam,tbout{igyro}.pni,picr_tbnam,tbout{igyro}.picr,prad_tbnam,tbout{igyro}.prad,' ...
+	  'betan_tbnam,tbout{igyro}.betan,ipl_tbnam,tbout{igyro}.ipl,bt_tbnam,tbout{igyro}.bt,a1_tbnam,tbout{igyro}.a1,a2_tbnam,tbout{igyro}.a2,rhopolntm_tbnam,tbout{igyro}.rhopolntm]=' ...
+	  'textread(''' file_out_prefix  num2str(igyro) '.dat'',''%s%d%s%d%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f'');']);
+  catch
+    disp(['problems reading file ' file_out_prefix  num2str(igyro) '.dat: may be some errors with tbdemo, check file, skipped'])
+  end
+end
+
+torbeam_out_struct = tbout;
-- 
GitLab