From a8e76d4716b56cc42c6e7de022a8342fc32ec6b5 Mon Sep 17 00:00:00 2001
From: Olivier Sauter <olivier.sauter@epfl.ch>
Date: Wed, 24 Apr 2019 14:20:15 +0000
Subject: [PATCH] add cxrs related files from svn export
 https://spcsvn.epfl.ch/repos/TCV/CXRS/analysis/trunk/crpptbx/xxx.m

git-svn-id: https://spcsvn.epfl.ch/repos/TCV/gdat/trunk@11791 d63d8f72-b253-0410-a779-e742ad2e26cf
---
 crpptbx/TCV/AK_MDS_node_info.m     |  103 ++
 crpptbx/TCV/CXRS_fit_profies.m     |   87 ++
 crpptbx/TCV/CXRS_get_profiles.m    |  355 +++++++
 crpptbx/TCV/CXRS_load_MDS.m        | 1466 ++++++++++++++++++++++++++++
 crpptbx/TCV/CXRS_plot_definition.m |   27 +
 crpptbx/TCV/gdat_tcv.m             |   10 +-
 6 files changed, 2043 insertions(+), 5 deletions(-)
 create mode 100644 crpptbx/TCV/AK_MDS_node_info.m
 create mode 100644 crpptbx/TCV/CXRS_fit_profies.m
 create mode 100644 crpptbx/TCV/CXRS_get_profiles.m
 create mode 100644 crpptbx/TCV/CXRS_load_MDS.m
 create mode 100644 crpptbx/TCV/CXRS_plot_definition.m

diff --git a/crpptbx/TCV/AK_MDS_node_info.m b/crpptbx/TCV/AK_MDS_node_info.m
new file mode 100644
index 00000000..2aeb622a
--- /dev/null
+++ b/crpptbx/TCV/AK_MDS_node_info.m
@@ -0,0 +1,103 @@
+function Info=AK_MDS_node_info(node,shot,k_debug,shot_time)
+% Get information about node in MDS
+% Call: Info=AK_MDS_node_info(node,shot,k_debug)
+%       node - MDS node name, for example '\RESULTS::CXRS_001:COMMENT'
+%       shot - TCV shot number (default [], opened shot)
+%       k_debug - debug key (default false)
+
+s=sprintf('%s',mfilename);
+Info.OK  = 1;
+Info.msg = 'OK';
+
+if nargin < 1, node=[]; end
+if nargin < 2, shot=[]; end
+if nargin < 3, k_debug = false; end
+if nargin < 4, shot_time=[]; end
+if ~isempty(shot),
+    Info.shot=mdsopen(shot);
+    if isempty(Info.shot), 
+      Info.msg=sprintf('ERROR in %s - MDS Open shot #%d - probably not exist',s,shot); disp(Info.msg);
+      Info.OK=-100; mdsclose; return
+    elseif (shot~=Info.shot)&&(shot~=0), 
+      Info.msg=sprintf('ERROR in %s - MDS Open shot #%d <> data.shot #%d',s,shot,Info.shot); disp(Info.msg);
+      Info.OK=-101; mdsclose; return
+    end
+else
+    [Info.shot,stat]=mdsvalue('$SHOT');
+    if ~mod(stat,2),
+      Info.msg=sprintf('ERROR in %s - MDS shot is nor opened',s); disp(Info.msg);
+      Info.OK=-102; mdsclose; return
+    elseif isempty(Info.shot);
+      Info.msg=sprintf('ERROR in %s - MDS shot is unknown',s); disp(Info.msg);
+      Info.OK=-103; mdsclose; return
+    end    
+end
+
+if isempty(shot_time),
+    % Info.shot_time = mdsvalue('shot_date(nint($1))',Info.shot);
+    [Info.shot_time,stat] = mdsdata(['tcv_shot_time(',int2str(Info.shot),')']);
+else
+    Info.shot_time=shot_time; stat=1;
+end
+chk_tim=true;
+if ~mod(stat,2),
+    [shot_time,stat] = mdsvalue('shot_date(nint($1))',Info.shot);
+    lf= findstr(shot_time,sprintf('\n'));
+    if ~isempty(lf), shot_time=shot_time(1:lf(1)-1); end
+    Info.shot_time = shot_time; 
+    chk_tim=false;
+end
+
+if (k_debug~=0), disp(sprintf('          ----- TCV shot %d cycle start date and time: %s -----',Info.shot,Info.shot_time));  end 
+Info.shot_time_vec=datevec(Info.shot_time,'dd-mmm-yyyy HH:MM:SS.FFF'); timk=[10000 100 1 0.01 0.0001 0.000001];
+Info.shot_run= sum(Info.shot_time_vec.*timk);
+
+if ~isempty(node),
+    Info.node=node;
+    tdis=sprintf('getnci("\\%s","FULLPATH")'            ,node);  [node_fullpath,stat_fullpath] =mdsdata(tdis);
+    tdis=sprintf('owner_ascii("\\%s")'                  ,node);  [node_ownerid, stat_ownerid ] =mdsdata(tdis);
+    tdis=sprintf('times(getnci("\\%s","TIME_INSERTED"))',node);  [node_time,    stat_time    ] =mdsdata(tdis);
+    tdis=sprintf('uid_gid("\\%s")'                      ,node);  [node_ownerID, stat_ownerID ] =mdsdata(tdis);
+    if ~mod(stat_fullpath,2), node_fullpath = 'unknown'; Info.OK  = -201; end
+    if ~mod(stat_ownerid ,2), node_ownerid  = 0        ; Info.OK  = -202; end
+    if ~mod(stat_time    ,2), node_time     = 'unknown'; Info.OK  = -203; end
+    if ~mod(stat_ownerID ,2), node_ownerID  = []       ; Info.OK  = -204; end
+    Info.node_fullpath= deblank(node_fullpath);
+    Info.node_ownerid = node_ownerid;
+    Info.node_time    = node_time;
+    Info.node_ownerID = node_ownerID;
+    %if ~isempty(node_ownerID),
+    %    cmd=sprintf('getent passwd %d  | sed -e ''s/\\:.*//''',node_ownerID(1));  [stat,owner] = system(cmd);
+    %    if (stat~=0), owner=[]; end
+    %    cmd=sprintf('getent group  %d  | sed -e ''s/\\:.*//''',node_ownerID(2));  [stat,group] = system(cmd);
+    %    if (stat~=0), group=[]; end
+    %    if isempty(group), group=sprintf('%d',node_ownerID(2)); end
+    %    if isempty(owner), owner=sprintf('%d',node_ownerID(1)); end
+    %    if ~isempty(findstr(group,'invalid option')), group='unknown'; end
+    %    if ~isempty(findstr(owner,'invalid option')), owner='unknown'; end
+    %    node_owner=sprintf('[%s,%s]',deblank(group),deblank(owner));
+    %else
+    %    node_owner=sprintf('[unknown,unknown]');
+    %end
+    Info.node_owner   = sprintf(' user %s',node_ownerid);
+    if (k_debug~=0), disp(sprintf('Node %s (%s) created %s by %s',Info.node,Info.node_fullpath,Info.node_time,Info.node_owner)); end
+    if mod(stat_time    ,2),
+        Info.node_time_vec=datevec(Info.node_time,'dd-mmm-yyyy HH:MM:SS.FFF'); timk=[10000 100 1 0.01 0.0001 0.000001];
+        Info.node_run= sum(Info.node_time_vec.*timk);        
+    else
+        Info.node_time_vec=[1970 1 1 0 0 0]; timk=[10000 100 1 0.01 0.0001 0.000001];
+        Info.node_run= sum(Info.node_time_vec.*timk);                
+    end
+    if (Info.node_run-Info.shot_run)<-0.005,
+        if (Info.OK==1)&&chk_tim, 
+            Info.OK=-301;
+            Info.msg=sprintf('WARNING node %s is craeated before shot #%d created',Info.node,Info.shot); 
+            if (k_debug~=0), disp(Info.msg); end
+        end
+    end
+    
+end
+
+if ~isempty(shot), mdsclose; end
+
+return
\ No newline at end of file
diff --git a/crpptbx/TCV/CXRS_fit_profies.m b/crpptbx/TCV/CXRS_fit_profies.m
new file mode 100644
index 00000000..26c1c32e
--- /dev/null
+++ b/crpptbx/TCV/CXRS_fit_profies.m
@@ -0,0 +1,87 @@
+function profiles=CXRS_fit_profies(V,Times,param_prof,param_plot);
+% Fit profiles with "interpos" function
+% Call profiles=CXRS_fit_profies(V,Times,param_prof,param_plot);
+
+%sel=find((V.t+V.dt/2>=Times(1))&(V.t-V.dt/2<=Times(2)));
+sel=find((V.t>=Times(1))&(V.t<=Times(2)));
+x    = V.x(sel); dx   = V.dx(sel); y    = V.y(sel); dy   = V.dy(sel); t    = V.t(sel); dt   = V.dt(sel); sys  = V.sys(sel);
+
+% shift of duplicated points in rho 
+Xb=x;
+for ip=1:length(Xb)
+      sss=1:length(Xb); sss=find(sss~=ip);
+      iff=(find(Xb(ip)==Xb(sss)));
+      if length(iff)>0, Xb(iff(1))=Xb(iff(1))+1.e-8; end
+end 
+% sort points in rho
+[x,iii]   = sort(Xb); 
+if length(iii)==0 % No data
+   profiles.t_lim=Times;
+   profiles.x_prof=[];
+   %profiles.x_prof= NaN(1,101)
+   profiles.y_prof=[];
+   profiles.dy_prof=[];
+   profiles.use.x=[]; profiles.use.dx=[]; profiles.use.y=[]; profiles.use.dy=[]; profiles.use.t=[]; profiles.use.dt=[];profiles.use.sys=[];
+   profiles.param_prof = param_prof;
+   profiles.param_plot = param_plot;
+   if param_plot.ON,
+      if param_plot.clear, clf; end
+      hve(1)=plot(0,NaN,'ok'); hold on
+      set(hve(1),'Color',param_plot.color,'LineStyle','none','LineWidth',param_plot.lnwidth,'Marker',param_plot.typemk,'MarkerSize',param_plot.sizemk);
+      profiles.plot_handle=hve(1);
+   end
+   return;
+end
+dx=dx(iii); y=y(iii); dy=dy(iii); t=t(iii); dt=dt(iii);sys=sys(iii); 
+if isempty(param_prof.xout), param_prof.xout=linspace(min(x)*0.98,max(x)*1.02,101); end 
+if ((min(x)-min(param_prof.xout))>0.45), 
+   param_prof.xout=linspace(min(x)*0.85,max(param_prof.xout),length(param_prof.xout));
+   %disp(sprintf('Redefine rho for %4.4f-%4.4fsec',Times)); 
+end 
+x_prof    = param_prof.xout;
+XXmax=max([max(x)+0.01 1.3]);
+if length(iii)>=10,
+  %y_prof    = interpos(param_prof.kopt,x,y,x_prof,param_prof.taus,param_prof.nbc,param_prof.ybc,abs(dy)+param_prof.err_min);
+  %disp('...before')
+  y_prof    = interpos(param_prof.kopt,[0; x; XXmax],[y(1); y; 0],x_prof,param_prof.taus,param_prof.nbc,param_prof.ybc,[abs(y(1))+abs(dy(1))*10.; abs(dy); 0]+param_prof.err_min );
+  %disp('...after')
+else
+  y_prof    = x_prof'*NaN;
+end
+y_prof=y_prof';
+for ip=1:length(x_prof)
+    W=1./dy.*1./(1.+((x-x_prof(ip))./dx).^2); W=W/sum(W);
+    Wn=sum(1./(1.+((x-x_prof(ip))./dx).^2));
+    dy_prof(ip) = sqrt(sum(W.*dy.^2))./sqrt(Wn);
+end
+
+
+profiles.t_lim=[min(t-dt/2.) max(t+dt/2.)];
+profiles.x_prof=x_prof;
+profiles.y_prof=y_prof;
+profiles.dy_prof=dy_prof;
+profiles.use.x=x; profiles.use.dx=dx; profiles.use.y=y; profiles.use.dy=dy; profiles.use.t=t; profiles.use.dt=dt;profiles.use.sys=sys;
+profiles.param_prof = param_prof;
+profiles.param_plot = param_plot;
+
+if param_plot.ON,
+   if param_plot.clear, clf; end
+   % matlab version
+   [v, ~] = version;
+   mat_ver = str2double(v(1:3));
+   hve=CXRS_errorbar(mat_ver, x,y,dx,dy); set(hve,'Color',param_plot.color); C=get(hve(1),'Color'); hold on
+   set(hve(1),'Color',max(C-0.2,[0 0 0]),'LineStyle','none','LineWidth',1.0,'Marker',param_plot.typemk,'MarkerSize',param_plot.sizemk);
+   set(hve(2),'Color',C); set(hve(1),'MarkerFaceColor',min(C+0.5,[1 1 1]),'MarkerEdgeColor',max(C-0.3,[0 0 0]));
+   if param_plot.clear, grid on; if ~isnan(max(x_prof)), set(gca,'Xlim',[min(x_prof) max(x_prof)]); else, set(gca,'Xlim',[0 1.1]); end, end
+   hh=plot(x_prof,y_prof,'-r');
+   set(hh,'Color',max(C-0.1,[0 0 0]),'LineStyle','-','LineWidth',param_plot.lnwidth);
+   if (size(y_prof,2)~=size(dy_prof,2)), y_prof=y_prof'; end
+   % whos x_prof y_prof dy_prof
+   hh=plot(x_prof,y_prof+dy_prof,'-r');
+   set(hh,'Color',min(C+0.2,[1 1 1]),'LineStyle','--','LineWidth',1.);
+   hh=plot(x_prof,y_prof-dy_prof,'-r');
+   set(hh,'Color',min(C+0.2,[1 1 1]),'LineStyle','--','LineWidth',1.);
+   profiles.plot_handle=hve(1);
+end
+
+return
diff --git a/crpptbx/TCV/CXRS_get_profiles.m b/crpptbx/TCV/CXRS_get_profiles.m
new file mode 100644
index 00000000..28d637c1
--- /dev/null
+++ b/crpptbx/TCV/CXRS_get_profiles.m
@@ -0,0 +1,355 @@
+function cxrs_profiles = CXRS_get_profiles(varargin)
+% Gets (reads from MDS) Ti,Vi,nC,Zeff profiles from TCV CXRS diagnostic, interpolates and plots
+% Call: cxrs_profiles = CXRS_get_profiles(varargin);
+%
+% Few Shots where it works: 42422 42404 42398 42310 42388 42357 42353 42340 42329 42308 42254 42231
+% Old Shots where it works: 41874 41757 41396 40268 40070 39360 38930 37108 36424 36401 36354 36203 36201 36196 (07.11.2008)
+%
+% Inputs:   {1} shot  - TCV shot number
+%           or  data  - structure from CXRS data analysis
+%           {2} sys   - cxrs system(s) LFS, HFS, ALL or 1,2,0 (default ALL)
+%           {3} time  - time, {a} [tmin tmax], 
+%                             {b} [tmin:dt:tmax],   [Tmin(i) Tmax(i)]=[t(i) t(i+1)];
+%                             {c} -[tmin(1) tmax(1) ... tmin(end) tmax(end)]) - direct specification of [Tmin(i) Tmax(i)]=abs([t(i*2-1) t(i*2)]);  
+%                                  default [] - all times for first system in sys
+%           {4} param - parameters strucrure, useful fields:
+%                      param.k_plot - 1 (default) plot results, 0 disable graphics, -1 plot in new figures, <=-2 plot on the top of existing figure (hold on)
+%                      param.prof.all - structire with parameters for interpos (see help interpos) used if corresponding filelds in param.prof.(Ti,vi,nc,zeff) are empty
+%                      param.prof.all.xout - rho for proffit, if empty variable on time according to rho_min rho_max of CXRS measurement 
+% Output:
+%         OK:  1 -OK, or error index
+%      argin:  cell of input arguments
+%       shot:  shot number
+%     system:  CXRS systems used
+%      param:  used parameters strucrure
+%      Times:  used times
+%       data:  cxrs data from CXRS_load_MDS(shot,isys,'result',[],...)
+%        exp:  experimental data for Ti, vTor, vPol, nC, Zeff structures with x,dx,y,dy,t,dt,sys
+%         Ti, vTor, vPol, nC, Zeff - cells with proffit data from CXRS_fit_profies.m function
+%    proffit: proffit data Ti(eV), vTor(km/sec), vPol(km/sec), nC(m^{-3}), Zeff all (time x rho) with rho(time x rho) - (normalized poloidal flux)^0.5, 
+%
+%
+%   See also: CXRS_save_profiles  - save cxrs_profiles in MDS
+%             CXRS_calc_automatic - automatic CXRS data analysis
+%             CXRS_save_MDS       - save CXRS data
+% Examples: shot=42422; cxrs_profiles = CXRS_get_profiles(shot);                                                      % standard run
+%           cxrs_profiles = CXRS_get_profiles; param=cxrs_profiles.param; param.prof.all.xout=linspace(0,1.1,101); 
+%                                              cxrs_profiles = CXRS_get_profiles(shot,[],[],param);                   % define rho grid
+%           mdsopen(shot); tdiTS=tdi('\results::thomson.profiles.auto:te'); mdsclose;                                 % get TS times
+%                                              dTimes=0.05;                                                           % max TS/CXRS time difference (50/2 ms) 
+%                                              Times(1:2:length(tdiTS.dim{1})*2)=tdiTS.dim{1}'-dTimes/2;
+%                                              Times(2:2:length(tdiTS.dim{1})*2)=tdiTS.dim{1}'+dTimes/2; 
+%                                              cxrs_profiles = CXRS_get_profiles; param=cxrs_profiles.param; param.prof.all.xout=linspace(0,1.1,101); 
+%                                              cxrs_profiles = CXRS_get_profiles(shot,[],-Times,param);               % gives CXRS profiles at TS times 101 rho points in [0 1.1]
+%                                              cxrs_profiles = CXRS_get_profiles; param=cxrs_profiles.param; param.prof.all.xout=tdiTS.dim{2}';
+%                                              param.k_plot=0;                                                        % without plot for profiles
+%                                              cxrs_profiles = CXRS_get_profiles(shot,[],-Times,param);               % gives CXRS profiles at TS times and rho
+%           param.prof.all.xout=[]; param.prof.Ti.taus=1.e-5; param.prof.vi.taus=5.e-5; 
+%                                              cxrs_profiles = CXRS_get_profiles(shot,[],[],param);                   % define taus for interpos for Ti and vi
+%           plot profiles from LFS,VER&HFS systems on the same plots by different colors
+%           cxrs_profiles = CXRS_get_profiles; param=cxrs_profiles.param; param.prof.all.xout=linspace(0,1.1,101);
+%           param.prof.Ti.taus=1.e-5; param.prof.vi.taus=2.e-5; param.prof.nc.taus=5.e-5;
+%           shot=45262; times=[0.41 1.01];
+%           param.k_plot= 1;  cxrs_profiles = CXRS_get_profiles(shot,1,times,param);
+%           param.k_plot=-2;  cxrs_profiles = CXRS_get_profiles(shot,2,times,param);
+%           param.k_plot=-3;  cxrs_profiles = CXRS_get_profiles(shot,3,times,param);
+%
+%
+% Created: A.Karpushov, CRPP-EPFL, 01.04.2011
+
+s=mfilename;
+cxrs_profiles.OK=1;
+%if nargin==0, cmd=sprintf('help %s',s); eval(cmd); cxrs_profiles.OK=0; return; end
+
+% input arguments
+ninparg = 4; inparg=varargin; for iinparg=nargin+1:ninparg, inparg{iinparg}=[]; end
+if isa(inparg{1},'numeric'), % shot number
+    shot=inparg{1}; k_load=1;
+elseif isa(inparg{1},'struct'), % data structure from CXRS data analysis
+    data=inparg{1};
+    if ~isfield(data,'shot'), data_OK=0; elseif ~isfield(data,'calc'), data_OK=0; elseif ~isfield(data.calc,'Ti'), data_OK=0; else data_OK=1; end
+    if data_OK, k_load=0; shot=data.shot; inparg{2}=data.system; else, cmd=sprintf('help %s',s); eval(cmd); cxrs_profiles.OK=0; return; end
+else, 
+    cmd=sprintf('help %s',s); eval(cmd); cxrs_profiles.OK=-9998; return; 
+end
+if isempty(inparg{2}), 
+   sys=0;
+elseif isa(inparg{2},'char'), 
+   switch upper(inparg{2});
+   case ('ALL'),
+      sys=0;
+   case ('LFS'),
+      sys=1;
+   case ('VER'),
+      sys=2;
+   case ('HFS'),
+      sys=3;
+   case ('EDG'),
+      sys=4;
+   otherwise,
+      cmd=sprintf('help %s',s); eval(cmd); cxrs_profiles.OK=-9999; return;
+   end
+elseif isa(inparg{2},'numeric'), 
+   if ((inparg{2} >= -1)&(inparg{2} <= 4)), 
+      sys=inparg{2};
+   else
+      cmd=sprintf('help %s',s); eval(cmd); cxrs_profiles.OK=0; return; 
+   end  
+else, 
+   cmd=sprintf('help %s',s); eval(cmd); cxrs_profiles.OK=0; return; 
+end
+if (sys(1)==0), sys=[1 2]; end % LFS & VER systems
+if (sys(1)==-1), sys=[1 2 3]; end % LFS & VER & HFS systems
+
+if ~isempty(inparg{3}), 
+  if (max(inparg{3}(1))<=0)&(length(inparg{3})>=2), 
+     Time_min=abs(inparg{3}(1:2:end)); Time_max=abs(inparg{3}(2:2:end)); % [tmin(1) tmax(1) tmin(2) tmax(2) ... tmin(end) tmax(end)]
+  elseif (length(inparg{3})==1), 
+     Time_min=abs(inparg{3})-0.05; Time_max=abs(inparg{3})+0.05;         % single time point
+  else
+     Time_min=abs(inparg{3}(1:end-1)); Time_max=abs(inparg{3}(2:end));   % tmax(i)=tmin(i-1)
+  end
+else, 
+  Time_min=[]; Time_max=[];
+end
+
+% Default paramaters
+param.name     = 'Default parameters';
+param.k_plot   = 1;
+param.k_debug  = 1;
+[colos,lnstyle,typemk]=CXRS_plot_definition;
+param.plot.colos   = colos;
+param.plot.lnstyle = lnstyle;
+param.plot.typemk  = typemk;
+a.kopt = 23;                   % option for interpolation and extrapolation method, 23 - cubic spline interpolation, with tension=taus
+%a.xout = linspace(0,1.10,101); % output grid for interpos
+a.xout = []; 
+%a.taus = [];               
+a.taus = 2.e-3 ;                % tension value for cubic spline interpolation
+%a.nbc  = [1  2 ];              % 0/1/2 - VALUE OF SECOND DERIVATIVE; 1-ST; FUNCTION AT XBCLFT
+a.nbc  = [1  0 ];               % 0/1/2 - VALUE OF SECOND DERIVATIVE; 1-ST; FUNCTION AT XBCLFT
+a.ybc  = [0. 0. 0. 1.];         % 1-ST  DERIVATIVE 0 (left); 2-MD  DERIVATIVE 0. (right) at rho 0.0 and 1.0
+% for sigma taus(i)=taus .* sigma(i) ./ min(sigma)
+param.prof.all  = a;
+param.prof.Ti   = a;
+param.prof.vi   = a;
+param.prof.nc   = a;
+param.prof.zeff = a;
+
+param.prof.Ti.err_min = 10.0;
+param.prof.vi.err_min = 0.5;
+param.prof.nc.err_min = 1.e+17;
+param.prof.zeff.err_min = 0.5;
+
+% add input parameters
+if ~isempty(inparg{4}),
+   if  ~isa(inparg{4},'struct'), cmd=sprintf('help %s',s); eval(cmd); cxrs_profiles.OK=0; return; end
+   parinp=inparg{4};
+   parnames = fieldnames(parinp);
+   for ipar=1:length(parnames)
+      cmd=sprintf('param.%s = parinp.%s;',parnames{ipar},parnames{ipar});
+      eval(cmd);
+   end
+end
+if ~isempty(param.prof.all.xout),
+  param.prof.Ti.xout    = param.prof.all.xout;
+  param.prof.vi.xout    = param.prof.all.xout;
+  param.prof.nc.xout    = param.prof.all.xout;
+  param.prof.zeff.xout  = param.prof.all.xout;
+end 
+
+cxrs_profiles.argin=inparg;
+cxrs_profiles.shot   = shot;
+cxrs_profiles.system = sys;
+cxrs_profiles.param  = param;
+cxrs_profiles.Times=(Time_min+Time_max)/2.;
+if nargin==0, cxrs_profiles.OK=0; return; end
+
+V.y = []; V.dy = []; V.x=[]; V.dx=[]; V.t=[]; V.dt=[]; V.sys=[]; 
+Ti=V; vTor=V; vPol=V; nC=V; Zeff=V;
+jsis=0; 
+for isys=sys
+  %data=CXRS_load_MDS(shot,isys,'result',[],param.k_debug,param.k_plot);
+  if k_load, data=CXRS_load_MDS(shot,isys,'result',[],param.k_debug,0); end
+  % if ~isfield(data.calc,'Ti'), data.OK=0; end
+  if (data.OK==1)
+     jsis=jsis+1; cxrs_profiles.data{jsis}=data;
+     nr=size(data.calc.Ti,1); nt=length(data.calc.time);
+     for it=1:nt, 
+        time(1:nr,it)=data.calc.time(it); 
+	if (length(data.calc.dtime)==1), dtime(1:nr,it)=data.calc.dtime; else, dtime(1:nr,it)=data.calc.dtime(it); end
+     end
+     if isempty(Time_min), Time_min=time(1,:)-dtime(1,:)/2; Time_max=time(1,:)+dtime(1,:)/2; end
+     Times=(Time_min+Time_max)/2.;
+     if isfield(data.calc,'Ti'),
+        Vold=Ti; V.y=data.calc.Ti; V.dy=data.calc.err_Ti; 
+	sel=find(~isnan(V.y)&~isnan(V.dy));
+        V.y=data.calc.Ti(sel); V.dy=data.calc.err_Ti(sel); V.x=data.calc.rho(sel); V.dx=data.calc.rho_err(sel); V.t=time(sel); V.dt=dtime(sel); V.sys=ones(size(V.y))*data.system;
+	V.y   =  [Vold.y; V.y]; V.dy   =  [Vold.dy; V.dy]; V.x   =  [Vold.x; V.x]; V.dx   =  [Vold.dx; V.dx]; V.t   =  [Vold.t; V.t]; V.dt   =  [Vold.dt; V.dt]; V.sys   =  [Vold.sys; V.sys]; 
+	Ti=V;
+     end
+     if isfield(data.calc,'vi')&((((data.system==2)&(data.shot<47823))||((data.system==3)&(data.shot>=47823)))||(data.system==4)), % VER CXRS system
+        Vold=vPol; V.y=data.calc.vi; V.dy=data.calc.err_vi; 
+	    sel=find(~isnan(V.y)&~isnan(V.dy));
+        V.y=data.calc.vi(sel); V.dy=data.calc.err_vi(sel); V.x=data.calc.rho(sel); V.dx=data.calc.rho_err(sel); V.t=time(sel); V.dt=dtime(sel); V.sys=ones(size(V.y))*data.system;
+	    V.y   =  [Vold.y; V.y]; V.dy   =  [Vold.dy; V.dy]; V.x   =  [Vold.x; V.x]; V.dx   =  [Vold.dx; V.dx]; V.t   =  [Vold.t; V.t]; V.dt   =  [Vold.dt; V.dt]; V.sys   =  [Vold.sys; V.sys]; 
+	    vPol=V;
+     else                                         % LFS CXRS system
+        Vold=vTor; V.y=data.calc.vi; V.dy=data.calc.err_vi; 
+	    sel=find(~isnan(V.y)&~isnan(V.dy));
+        V.y=data.calc.vi(sel); V.dy=data.calc.err_vi(sel); V.x=data.calc.rho(sel); V.dx=data.calc.rho_err(sel); V.t=time(sel); V.dt=dtime(sel); V.sys=ones(size(V.y))*data.system;
+	V.y   =  [Vold.y; V.y]; V.dy   =  [Vold.dy; V.dy]; V.x   =  [Vold.x; V.x]; V.dx   =  [Vold.dx; V.dx]; V.t   =  [Vold.t; V.t]; V.dt   =  [Vold.dt; V.dt]; V.sys   =  [Vold.sys; V.sys]; 
+	vTor=V;
+     end
+     if isfield(data.calc,'ni'),
+        Vold=nC; V.y=data.calc.ni; V.dy=data.calc.err_ni; 
+	sel=find(~isnan(V.y)&~isnan(V.dy));
+        V.y=data.calc.ni(sel); V.dy=data.calc.err_ni(sel); V.x=data.calc.rho(sel); V.dx=data.calc.rho_err(sel); V.t=time(sel); V.dt=dtime(sel); V.sys=ones(size(V.y))*data.system;
+	V.y   =  [Vold.y; V.y]; V.dy   =  [Vold.dy; V.dy]; V.x   =  [Vold.x; V.x]; V.dx   =  [Vold.dx; V.dx]; V.t   =  [Vold.t; V.t]; V.dt   =  [Vold.dt; V.dt]; V.sys   =  [Vold.sys; V.sys]; 
+	nC=V;
+     end
+     if isfield(data.calc,'zeff'),
+        Vold=Zeff; V.y=data.calc.zeff; V.dy=data.calc.err_zeff; 
+	sel=find(~isnan(V.y)&~isnan(V.dy));
+        V.y=data.calc.zeff(sel); V.dy=data.calc.err_zeff(sel); V.x=data.calc.rho(sel); V.dx=data.calc.rho_err(sel); V.t=time(sel); V.dt=dtime(sel); V.sys=ones(size(V.y))*data.system;
+	V.y   =  [Vold.y; V.y]; V.dy   =  [Vold.dy; V.dy]; V.x   =  [Vold.x; V.x]; V.dx   =  [Vold.dx; V.dx]; V.t   =  [Vold.t; V.t]; V.dt   =  [Vold.dt; V.dt]; V.sys   =  [Vold.sys; V.sys]; 
+	Zeff=V;
+     end
+  end
+end
+
+if isempty(Ti.y), msg=sprintf('%s - No CXRS Data for TCV shot:%d',s,cxrs_profiles.shot); disp(msg); cxrs_profiles.OK=-100; return; end
+
+cxrs_profiles.Times=Times;
+cxrs_profiles.Time_min=Time_min;
+cxrs_profiles.Time_max=Time_max;
+cxrs_profiles.exp.Ti=Ti;
+cxrs_profiles.exp.vTor=vTor;
+cxrs_profiles.exp.vPol=vPol;
+cxrs_profiles.exp.nC=nC;
+cxrs_profiles.exp.Zeff=Zeff;
+
+figures=[1:5]+9900;
+names_exp{1} = 'Ti'; names_exp{2} = 'vTor'; names_exp{3} = 'vPol'; names_exp{4} = 'nC'; names_exp{5} = 'Zeff'; 
+names_par{1} = 'Ti'; names_par{2} = 'vi';   names_par{3} = 'vi';   names_par{4} = 'nc'; names_par{5} = 'zeff'; 
+names_uni{1} = 'eV'; names_uni{2} = 'km/sec';   names_uni{3} = 'km/sec';   names_uni{4} = 'm^{-3}'; names_uni{5} = ' '; 
+%if cxrs_profiles.param.k_plot==1 || cxrs_profiles.param.k_plot==-1 || cxrs_profiles.param.k_plot<=-2
+if cxrs_profiles.param.k_plot~=0
+   param_plot.ON      = 1; param_plot.lnwidth = 2.0; param_plot.sizemk  = 6.0;
+else
+   param_plot.ON      = 0;
+end
+
+iaddcolor=0;
+for iexp=1:length(names_exp),
+ if isfield(cxrs_profiles.exp,names_exp{iexp})
+   if cxrs_profiles.param.k_plot==1
+     figure(figures(iexp))
+   elseif cxrs_profiles.param.k_plot>=2
+     figure(figures(iexp))
+     iaddcolor=cxrs_profiles.param.k_plot+1;
+   elseif cxrs_profiles.param.k_plot==-1
+     figure
+   elseif cxrs_profiles.param.k_plot<=-2
+     figure(figures(iexp)); hold on
+     iaddcolor=-cxrs_profiles.param.k_plot+1;
+   end
+   clear profiles
+   cmd=sprintf('param_prof = cxrs_profiles.param.prof.%s;',names_par{iexp}); eval(cmd);
+   cmd=sprintf('exp_data   = cxrs_profiles.exp.%s;',names_exp{iexp}); eval(cmd);
+   if ~isempty(exp_data.y),
+    for it=1:length(cxrs_profiles.Time_min)
+      %if (it==1 && iaddcolor==0), param_plot.clear   = 1; else, param_plot.clear   = 0; end
+      if (it==1 && cxrs_profiles.param.k_plot>0), param_plot.clear   = 1; else, param_plot.clear   = 0; end
+      param_plot.color   = cxrs_profiles.param.plot.colos(it+iaddcolor,:);
+      param_plot.typemk  = cxrs_profiles.param.plot.typemk{it+iaddcolor};
+      param_plot.lnstyle = cxrs_profiles.param.plot.lnstyle{1};
+      profiles{it}=CXRS_fit_profies(exp_data,[cxrs_profiles.Time_min(it) cxrs_profiles.Time_max(it)],param_prof,param_plot);
+    end
+    cmd=sprintf('cxrs_profiles.%s=profiles;',names_exp{iexp}); eval(cmd);
+    %if (cxrs_profiles.param.k_plot>0), 
+    if (cxrs_profiles.param.k_plot~=0), 
+      title(sprintf('CXRS %s for TCV shot: %5.5d sys.No.:[%s]',names_exp{iexp},cxrs_profiles.shot,num2str(cxrs_profiles.system)));
+      xlabel('\rho_{TCV}')
+      ylabel(sprintf('%s, %s',names_exp{iexp},names_uni{iexp}));
+      clear hh leg
+      for it=1:length(profiles), 
+         hh(it)=profiles{it}.plot_handle;
+         if isempty(profiles{it}.y_prof),
+             leg{it}=sprintf('%3.3f-%3.3fs - No Data',profiles{it}.t_lim);
+         else
+             %leg{it}=sprintf('%2.2f-%2.2f->%2.2f-%2.2fs',cxrs_profiles.Time_min(it),cxrs_profiles.Time_max(it),profiles{it}.t_lim);
+             %leg{it}=sprintf('%3.3f-%3.3fs',profiles{it}.t_lim);
+             if length(cxrs_profiles.system)==1
+                 SysP=sprintf('(S%d)',cxrs_profiles.system);
+             else
+                SysP='';
+             end
+             leg{it}=sprintf('#%d@%3.3f-%3.3fs%s',shot,profiles{it}.t_lim,SysP);
+             set(hh(it),'Tag','CXRS data','DisplayName',leg{it});
+         end 
+      end
+      hE = findobj(gcf,'-regexp','Tag','CXRS data');
+      if(length(hE)>0),
+          hE=hE(end:-1:1);
+          leg=get(hE,'DisplayName');
+          legend(hE,leg,0);
+      end
+      %legend(hh,leg,0); grid on
+    end
+   else
+    if (cxrs_profiles.param.k_plot~=0), clf; tit=sprintf('No CXRS %s for TCV shot:%d sys.No.:[%s]',names_exp{iexp},shot,num2str(sys)); title(tit); text(0.5,0.5,tit,'HorizontalAlignment','center'); end
+   end
+ end
+end
+
+cxrs_profiles.proffit.shot=cxrs_profiles.shot;
+cxrs_profiles.proffit.comment=sprintf('CXRS proffit data, function:%s, executed:%s',mfilename,datestr(now));
+
+for it=1:length(cxrs_profiles.Ti)
+   D=cxrs_profiles.Ti{it};
+   cxrs_profiles.proffit.time(1,it)  = mean(D.t_lim);
+   cxrs_profiles.proffit.dtime(1,it) = (D.t_lim(2)-D.t_lim(1))/2.;
+   if ~isempty(D.y_prof), rho=D.x_prof; else rho=cxrs_profiles.param.prof.Ti.xout'; end
+   if isempty(rho); rho=NaN(1,101); end
+   if isempty(D.y_prof),   V=rho*NaN;    D.dy_prof=[]; elseif (length(find(isnan(D.y_prof)))>0),   V=rho*NaN;  else,  V=D.y_prof'; end 
+   if isempty(D.dy_prof), dV=rho*NaN;  elseif (length(find(isnan(D.dy_prof)))>0), dV=rho*NaN;  else, dV=D.dy_prof'; end 
+   V(find(V<0))=0; 
+   cxrs_profiles.proffit.Ti(:,it)   =V'; cxrs_profiles.proffit.dTi(:,it)   =dV'; cxrs_profiles.proffit.Ti_rho(:,it)  = rho';
+   cxrs_profiles.proffit.rho(:,it)  = rho';
+   if isfield(cxrs_profiles,'vTor')
+      D=cxrs_profiles.vTor{it};
+      if ~isempty(D.y_prof), rho=D.x_prof; else, rho=cxrs_profiles.proffit.rho(:,it); end
+      if isempty(D.y_prof),  V=rho*NaN;    D.dy_prof=[]; elseif (length(find(isnan(D.y_prof)))>0), V=rho*NaN;  else, V=D.y_prof'; end  
+      if isempty(D.dy_prof), dV=rho*NaN;  elseif (length(find(isnan(D.dy_prof)))>0), dV=rho*NaN;  else, dV=D.dy_prof'; end 
+      cxrs_profiles.proffit.vTor(:,it)   =V'; cxrs_profiles.proffit.dvTor(:,it)   =dV'; cxrs_profiles.proffit.vTor_rho(:,it)  = rho';
+   end  
+   if isfield(cxrs_profiles,'vPol')
+      D=cxrs_profiles.vPol{it};
+      if ~isempty(D.y_prof), rho=D.x_prof; else, rho=cxrs_profiles.proffit.rho(:,it); end
+      if isempty(D.y_prof),  V=rho*NaN;    D.dy_prof=[]; elseif (length(find(isnan(D.y_prof)))>0), V=rho*NaN;  else, V=D.y_prof'; end  
+      if isempty(D.dy_prof), dV=rho*NaN;  elseif (length(find(isnan(D.dy_prof)))>0), dV=rho*NaN;  else, dV=D.dy_prof'; end 
+      cxrs_profiles.proffit.vPol(:,it)   =V'; cxrs_profiles.proffit.dvPol(:,it)   =dV'; cxrs_profiles.proffit.vPol_rho(:,it)  = rho';
+   end  
+   if isfield(cxrs_profiles,'nC')
+      D=cxrs_profiles.nC{it};
+      if ~isempty(D.y_prof), rho=D.x_prof; else, rho=cxrs_profiles.proffit.rho(:,it); end
+      if isempty(D.y_prof),  V=rho*NaN;  D.dy_prof=[]; elseif (length(find(isnan(D.y_prof)))>0), V=rho*NaN;  else, V=D.y_prof'; end  
+      if isempty(D.dy_prof), dV=rho*NaN;  elseif (length(find(isnan(D.dy_prof)))>0), dV=rho*NaN;  else, dV=D.dy_prof'; end 
+      V(find(V<0))=0; 
+      cxrs_profiles.proffit.nC(:,it)   =V'; cxrs_profiles.proffit.dnC(:,it)   =dV'; cxrs_profiles.proffit.nC_rho(:,it)  = rho';
+   end  
+   if isfield(cxrs_profiles,'Zeff')
+      D=cxrs_profiles.Zeff{it};
+      if ~isempty(D.y_prof), rho=D.x_prof; else, rho=cxrs_profiles.proffit.rho(:,it); end
+      if isempty(D.y_prof),  V=rho*NaN;    D.dy_prof=[]; elseif (length(find(isnan(D.y_prof)))>0), V=rho*NaN;  else, V=D.y_prof'; end  
+      if isempty(D.dy_prof), dV=rho*NaN;  elseif (length(find(isnan(D.dy_prof)))>0), dV=rho*NaN;  else, dV=D.dy_prof'; end 
+      V(find(V<1))=1; 
+      cxrs_profiles.proffit.Zeff(:,it)   =V'; cxrs_profiles.proffit.dZeff(:,it)   =dV'; cxrs_profiles.proffit.Zeff_rho(:,it)  = rho';
+   end  
+
+end
+
+if (length(find(~isnan(cxrs_profiles.proffit.Ti)))==0), msg=sprintf('%s - Proffit not possible for TCV shot:%d',s,cxrs_profiles.shot); disp(msg); cxrs_profiles.OK=-101; return; end
+
+return
diff --git a/crpptbx/TCV/CXRS_load_MDS.m b/crpptbx/TCV/CXRS_load_MDS.m
new file mode 100644
index 00000000..83fecbc6
--- /dev/null
+++ b/crpptbx/TCV/CXRS_load_MDS.m
@@ -0,0 +1,1466 @@
+function data=CXRS_load_MDS(shot,sys,what2read,data,k_debug,k_plot)
+% Read CXRS data from MDS
+% Call: data=CXRS_load_MDS(shot,[sys,what2read,data,k_debug,k_plot])
+% Inputs: shot       - TCV shot number
+%         sys        - CXRS system 1 (default) - LFS HOR, 2 - VER
+%         what2read  - what to load from MDS  
+%                 cxrsdsi           - Displays CXRS Data Storage Info, information on nodes in MDS impotrant for CXRS data analysis
+%                 atlas (default)   - CXRS experimental data from MDS (ATLAS tree) from CCD camera
+%                 result            - Ti,Vi,nC,Zeff from CXRS
+%                 time              - CXRS timing data from MDS (vista)
+%                 analysis
+%                 calibration
+%                 plasma_ts; plasma_zeff; plasma_ip; psitbxtcv - External data for CXRS data analysis
+%         data       - adds data to this structure, default [] - empty (new structure)
+%         k_debug    - debug messages: 0 (default)-OFF; 1-ON, 
+%         k_plot     - plot option: 0 (default)-OFF; 1-ON, 
+%                     Created: AK, CRPP-EPFL, 2010
+%                     Update:  AK, 27.04.2011
+% Examples:  data=CXRS_load_MDS(shot,[],'cxrsdsi');
+%            data=CXRS_load_MDS(shot,sys,'atlas',[],1,1);
+%            data=CXRS_load_MDS(data.shot,data.system,'time',data,1,1);
+%            data=CXRS_load_MDS(shot,sys,'result',[],1,1);
+
+%% 0 Initialization
+% mdsserver='tcvdata';
+% mdsdefaultserver(mdsserver);
+% mdsconnect(mdsserver)
+
+% default shot - last
+if (nargin<1),        shot = [];  end
+if (length(shot)==0), shot = 0;   end
+if (shot==0),         shot = mdsopen(shot);end
+
+%default system No.1
+if (nargin<2),       sys=[]; end
+if (length(sys)==0), sys=1;  end
+
+%default action read measurement from ATLAS
+if (nargin<3),             what2read=[];      end
+if (length(what2read)==0), what2read='atlas';  end
+
+%redefine data structure
+if (nargin<4), data=[]; end 
+if (length(data)==0), data.shot=shot; data.system=sys; data.confidence      = -4; end
+
+if (nargin<5), k_debug=0; elseif (length(k_debug)==0), k_debug=0; elseif (k_debug==0), k_debug=0; else, k_debug=1; end 
+if (nargin<6), k_plot =0; elseif (length(k_plot )==0), k_plot =0; elseif (k_plot ==0), k_plot =0; else, k_plot =1; end 
+
+s=sprintf('%s',upper(mfilename));
+data.OK=1; data.msg=sprintf('%s OK',s);
+
+if ~strcmp(what2read,'file')
+    try
+        c_shot = shot;
+        shot = mdsopen(shot); 
+    catch
+        mdsdisconnect;
+        pause(0.1);
+        shot = mdsopen(c_shot); 
+    end  
+   if isempty(shot), 
+      data.msg=sprintf('ERROR in %s - MDS Open shot #%d - probably not exist',s,data.shot); disp(data.msg);
+      data.OK=-100; return
+   elseif (shot~=data.shot)&&(data.shot~=45640), 
+      data.msg=sprintf('ERROR in %s - MDS Open shot #%d <> data.shot #%d',s,shot,data.shot); disp(data.msg);
+      data.OK=-100; return
+   end
+end
+
+%% Different options
+switch (what2read)
+case 'cxrsdsi', 
+%% 1. CXRS Data Storage Info - Displays information on nodes in MDS impotrant for CXRS data analysis
+  shot = mdsopen(shot);
+  
+  data.last_action=sprintf('Check CXRS nodes in MDS for TCV shot %d',shot);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  Info=AK_MDS_node_info;
+  shot_time=Info.shot_time;
+  disp(sprintf('          ----- TCV shot %d cycle start date and time:%s -----',shot,shot_time));
+  shot_time_vec = Info.shot_time_vec;
+  shot_run      = Info.shot_run;
+  dsi.shot=shot; dsi.shot_time=shot_time;
+  id=0;
+  disp('          ------------------------------- CXRS data -------------------------------')
+  if(shot>=59000);
+      Sysmax=5;
+  elseif(shot>=50200);
+      Sysmax=4;
+  elseif (shot>=45100);
+      Sysmax=3; 
+  else
+      Sysmax=2;
+  end
+  Sys_range = intersect(1:Sysmax,sys);
+  for System=Sys_range
+    if shot>50100,
+      shot = mdsopen(shot);
+      TrEE='\ATLAS::CXRS';
+    elseif   (shot>=48900)&&(System~=3) || shot>=49900 ,
+      shot = mdsopen('cxrs',shot);
+      TrEE='\CXRS';
+    else
+      shot = mdsopen(shot);
+      TrEE='\ATLAS::CXRS';
+    end
+    if   (shot>=41216) && ((System~=3) || (shot>=45458))  
+      node           = sprintf('%s_%3.3d:COMMENT',TrEE,System);
+    else
+      node           = sprintf('\\ATLAS::CXRS_DATA:P_%3.3d',System);
+    end
+      if (System==1), SysT='LFS'; elseif (System==2), SysT='HFS'; elseif (System==3), SysT='VER'; elseif (System==4), SysT='EDV'; else, SysT='unknown'; end
+      disp(sprintf('                                     ---- System No.%2.2d (%s) ----',System,SysT))
+      label          = sprintf('CXRS experimental data from System No.%2.2d (%s)',System,SysT);
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+      if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+      disp(msg); id=id+1; 
+      dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+      dsi.CXRS_atlas_EXP_OK(System) = OK;
+      
+    if ((shot>=41216)&&(System~=3))||((shot>=45390)&&(System==3))  
+      label          = sprintf('CXRS ACQ Info for System No.%2.2d (%s)',System,SysT);
+      node           = sprintf('%s_%3.3d.INFO:CAMERA',TrEE,System);
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+      if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+      disp(msg); id=id+1; 
+      dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+      dsi.CXRS_atlas_INFO_OK(System) = OK;
+     
+      node           = sprintf('%s_%3.3d.DCD:RD',TrEE,System);
+      label          = sprintf('CXRS Diagnostic Chords for System No.%2.2d (%s)',System,SysT);
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+      if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+      disp(msg); id=id+1; 
+      dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+      dsi.CXRS_atlas_DCD_OK(System) = OK;
+    else
+      dsi.CXRS_atlas_DCD_OK(System) = 0;
+     dsi.CXRS_atlas_INFO_OK(System) = 0;
+    end
+    
+    if ((shot>=41216)&&(System~=3))||((shot>=45390)&&(System==3))
+      node           = sprintf('%s_%3.3d.ACQ:SPECTRA',TrEE,System);
+    else
+      node           = sprintf('\\ATLAS::CXRS_DATA:P_%3.3d',System);
+    end
+      label          = sprintf('CXRS Spectra for System No.%2.2d (%s)',System,SysT);
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+      if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+      disp(msg); id=id+1; 
+      dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+      dsi.CXRS_atlas_ACQ_OK(System) = OK;
+      
+    if ((shot>=41216)&&(System~=3))||((shot>=45390)&&(System==3))
+      node           = sprintf('%s_%3.3d.CALIBRATION.ABS:TRANSMISSION',TrEE,System); 
+      label          = sprintf('CXRS Absolute Calibration for System No.%2.2d (%s)',System,SysT);
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+      if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+      disp(msg); id=id+1; 
+      dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+      dsi.CXRS_atlas_CAL_ABS_OK(System) = OK;
+    else
+      dsi.CXRS_atlas_CAL_ABS_OK(System) = 0;
+    end
+      
+    if ((shot>=41216)&&(System~=3))||((shot>=45390)&&(System==3))
+      node           = sprintf('%s_%3.3d.CALIBRATION.CAMERA:PARAM',TrEE,System);
+      label          = sprintf('CXRS Camera Calibration for System No.%2.2d (%s)',System,SysT);
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+      if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+      disp(msg); id=id+1; 
+      dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+      dsi.CXRS_atlas_CAL_Camera_OK(System) = OK;
+    else
+      dsi.CXRS_atlas_CAL_Camera_OK(System) = 0;
+    end
+      
+    if ((shot>=41216)&&(System~=3))||((shot>=45390)&&(System==3))
+      node           = sprintf('%s_%3.3d.CALIBRATION.WL:SPECTRA',TrEE,System);
+      label          = sprintf('CXRS WaveLength Calibration for System No.%2.2d (%s)',System,SysT);
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+      if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+      disp(msg); id=id+1; 
+      dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+      dsi.CXRS_atlas_CAL_WL_OK(System) = OK;
+    else
+      dsi.CXRS_atlas_CAL_WL_OK(System) = 0;
+    end
+      
+      if ((shot>=41216)&&(System~=3))||((shot>=45390)&&(System==3))
+          node           = sprintf('%s_%3.3d.CALIBRATION.FULL_FRAME:SPECTRA',TrEE,System);
+      else
+          node           = sprintf('\\ATLAS::CXRS_CALIBRATION.SYS_%3.3d:SPECTRA',System);
+      end
+      label          = sprintf('CXRS Full Frame Calibration for System No.%2.2d (%s)',System,SysT);
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+      if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+      disp(msg); id=id+1; 
+      dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+      dsi.CXRS_atlas_CAL_FF_OK(System) = OK;
+      
+      shot = mdsopen(shot);
+
+      if (shot>=36390)||(System~=1),
+          node           = sprintf('\\RESULTS::CXRS_%3.3d:TI',System);
+      else
+          node           = sprintf('\\RESULTS::CXRS:TI');          
+      end
+      label          = sprintf('CXRS Ti Results for System No.%2.2d (%s)',System,SysT);
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      [node_data,stat]=mdsdata(node);
+      if (Info.OK==1)&&(mod(stat,2)), OK=true; else, OK=false; node_time='Not filled'; end      
+      if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+      disp(msg); id=id+1; 
+      dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+      dsi.CXRS_result_TI_OK(System) = OK;
+      
+      if (shot>=36390),
+          node           = sprintf('\\RESULTS::CXRS_%3.3d:VI',System);
+      elseif (System==1)
+          node           = sprintf('\\RESULTS::CXRS:VI_TOR');  
+      else
+          node           = sprintf('\\RESULTS::CXRS:VI_POL');  
+      end
+      label          = sprintf('CXRS Vi Results for System No.%2.2d (%s)',System,SysT);
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      [node_data,stat]=mdsdata(node);
+      if (Info.OK==1)&&(mod(stat,2)), OK=true; else, OK=false; node_time='Not filled'; end      
+      if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+      disp(msg); id=id+1; 
+      dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+      dsi.CXRS_result_VI_OK(System) = OK;
+
+      if (shot>=36390)||(System~=1),
+          node           = sprintf('\\RESULTS::CXRS_%3.3d:NI',System);
+      else
+          node           = sprintf('\\RESULTS::CXRS:NI');          
+      end
+      label          = sprintf('CXRS nC Results for System No.%2.2d (%s)',System,SysT);
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      [node_data,stat]=mdsdata(node);
+      if (Info.OK==1)&&(mod(stat,2)), OK=true; else, OK=false; node_time='Not filled'; end      
+      if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+      disp(msg); id=id+1; 
+      dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+      dsi.CXRS_result_NI_OK(System) = OK;
+
+    if (shot>=41216)  
+      node           = sprintf('\\RESULTS::CXRS_%3.3d:ZEFF',System);
+      label          = sprintf('CXRS Zeff Results for System No.%2.2d (%s)',System,SysT);
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+      if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+      disp(msg); id=id+1; 
+      dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+      dsi.CXRS_result_ZEFF_OK(System) = OK;
+    else
+      dsi.CXRS_result_ZEFF_OK(System) = 0; 
+    end
+    
+    if (shot>=40000) 
+      node           = sprintf('\\RESULTS::CXRS_%3.3d.WL_CAL:WAVELENGTH',System);  
+      label          = sprintf('CXRS Cali Results for System No.%2.2d (%s)',System,SysT);
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      if (Info.OK==1), OK=true; else OK=false; node_time='Not filled'; end      
+      if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else msg=sprintf('%50s: not filled yet',label); end
+      disp(msg); id=id+1; 
+      dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+      dsi.CXRS_result_WLCAB_OK(System) = OK;
+    else
+      dsi.CXRS_result_WLCAB_OK(System) = 0; 
+    end
+    
+    
+    
+  end
+
+  disp('          ----------------------------- CXRS profiles -----------------------------')
+
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:TI');
+  label          = sprintf('CXRS Ion Temperature Profile');
+  Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+  if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+  if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+  disp(msg); id=id+1; 
+  dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+  eval(sprintf('dsi.CXRS_proffit_Ti_OK = %d;',OK));
+
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:VI_TOR');
+  label          = sprintf('CXRS CVI Toroidal Rotation Profile');
+  Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+  if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+  if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+  disp(msg); id=id+1; 
+  dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+  eval(sprintf('dsi.CXRS_proffit_Vi_Tor_OK = %d;',OK));
+
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:VI_POL');
+  label          = sprintf('CXRS CVI Poloidal Rotation Profile');
+  Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+  if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+  if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+  disp(msg); id=id+1; 
+  dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+  eval(sprintf('dsi.CXRS_proffit_Vi_Pol_OK = %d;',OK));
+
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:NI');
+  label          = sprintf('CXRS CVI Ion Density Profile');
+  Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+  if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+  if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+  disp(msg); id=id+1; 
+  dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+  eval(sprintf('dsi.CXRS_proffit_Ni_OK = %d;',OK));
+
+  
+  disp('          --------------------------- External MDS data ---------------------------')
+  % Nodes required for CXRS data analysis
+  % < Feb 08, 2013
+  %    \TCV_SHOT::TOP.RESULTS.EQ_RECON.TRACES:I_P         == \I_P
+  %    \TCV_SHOT::TOP.RESULTS.EQ_RECON.PSITBX:RMAG        == \PSITBX:RMAG
+  %    \TCV_SHOT::TOP.RESULTS.THOMSON:NE                  == \RESULTS::THOMSON:NE == \THOMSON:NE == \NE_THOMSON
+  %    \TCV_SHOT::TOP.RESULTS.THOMSON:PSISCATVOL          == \THOMSON:PSISCATVOL
+  %    \TCV_SHOT::TOP.RESULTS.THOMSON.PROFILES.AUTO:NE    == \RESULTS::THOMSON.PROFILES.AUTO:NE == \THOMSON.PROFILES.AUTO:NE
+  % > Feb 08, 2013
+  %    \TCV_SHOT::TOP.RESULTS.EQ_RECON.TRACES:PSI == \RESULTS::PSI        == \PSI 
+  %    \TCV_SHOT::TOP.RESULTS.THOMSON:NE          == \RESULTS::THOMSON:NE == \THOMSON:NE == \NE_THOMSON
+  
+  % Plasma current
+  node = sprintf('\\I_P:FOO');         
+  label          = sprintf('Plasma Current (Ip)');
+  Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+  if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+  if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+  disp(msg); id=id+1; 
+  dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+  eval(sprintf('dsi.CXRS_plasma_IP_OK = %d;',OK));
+  
+  % LIUQE
+  node = sprintf('\\PSI:FOO');         
+  label          = sprintf('TCV Psi Toolbox (PSI, LIUQE)');
+  Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+  if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+  if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+  disp(msg); id=id+1; 
+  dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+  eval(sprintf('dsi.CXRS_plasma_PSI_OK = %d;',OK));
+
+  % Thomson Ne and Te
+  % node = sprintf('\\ne_thomson:foo');                         
+  node = sprintf('\\THOMSON:NE:FOO');
+  label          = sprintf('Thomson Ne and Te experimental data');
+  Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+  if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+  if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+  disp(msg); id=id+1; 
+  dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+  eval(sprintf('dsi.CXRS_plasma_TS_data_OK = %d;',OK));
+
+  % Thomson PsiScatVol
+  %node = sprintf('\\thomson:psiscatvol:foo'); 
+  node = sprintf('\\THOMSON:PSISCATVOL:FOO');
+  label          = sprintf('Thomson PsiScatVol');
+  Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+  if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+  if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: not filled yet',label); end
+  disp(msg); id=id+1; 
+  dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+  eval(sprintf('dsi.CXRS_plasma_PsiScatVol_OK = %d;',OK));
+
+  % Thomson auto fits
+  %node = sprintf('\\RESULTS::THOMSON.PROFILES.AUTO:NE:FOO');
+  node = sprintf('\\THOMSON.PROFILES.AUTO:NE:FOO');
+  label          = sprintf('Thomson AutoFit Ne and Te Profiles');
+  Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+  if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; end      
+  if ~OK, 
+      node      = sprintf('\\RESULTS::PROFFIT.LOCAL_TIME:NEFT'); 
+      label          = sprintf('Thomson Proffit Profiles');
+      Info=AK_MDS_node_info(node,[],[],shot_time); node_owner=Info.node_owner; node_time=Info.node_time;
+      if (Info.OK==1), OK=true; else, OK=false; node_time='Not filled'; fill_msg='not filled yet'; end      
+  end
+  if (Info.OK==1),
+      node_run=Info.node_run;
+       if (node_run>=shot_run),
+           OK=1;
+           fill_msg='filled';
+       else
+           OK=0;
+           fill_msg=sprintf('created at %s before shot',node_time);
+       end
+  end
+  if OK, msg=sprintf('%55s: filled at %s by %s',label,node_time,node_owner); else, msg=sprintf('%50s: %s',label,fill_msg); end  
+  disp(msg); id=id+1; 
+  dsi.node{id}.node=node; dsi.node{id}.OK=OK; dsi.node{id}.label=label; dsi.node{id}.time=node_time; dsi.node{id}.owner=node_owner;
+  eval(sprintf('dsi.CXRS_plasma_TS_profile_OK = %d;',OK));
+  
+  disp(sprintf('          ----- TCV shot %d cycle start date and time:%s RUN: %8.6f (YYYYMMDD.hhmmss) -----',shot,shot_time,shot_run));
+  data.dsi=dsi;
+  data.last_action=sprintf('Check CXRS nodes in MDS for TCV shot %d - DONE',shot);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  
+case 'atlas', 
+%% 2. Load CXRS experimental data from MDS (ATLAS tree) from CCD camera
+  data.last_action=sprintf('Read CXRS data from MDS (atlas) TCV shot:%d - system No.%d',shot,sys);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  if shot>50100,
+    [D,Stat]=CXRS_load_ATLAS(shot,sys);
+  elseif (shot>=48900)&(sys~=3) || shot>=49900,
+    [D,Stat]=CXRS_load_TCV1(shot,sys);  
+  elseif ((shot>=41216)&(sys~=3))||((shot>=45390)&(sys==3)),
+    [D,Stat]=CXRS_load_ATLAS(shot,sys);
+  elseif (shot>=45092)&(sys==3)
+    [D,Stat]=CXRS_load_ATLAS_2010(shot,sys); % Old cameras
+  elseif (shot>=36390)&(shot<=41215)
+    [D,Stat]=CXRS_load_ATLAS_2010(shot,sys); % Old cameras
+  else
+     data.msg=sprintf('%s - CXRS analysis not possible for TCV shot:%d < 36390',s,shot); disp(data.msg);
+     data.OK=-112; return,
+  end
+  if (D.OK<1), 
+     data.msg=sprintf('ERROR in %s - Data loading error for TCV shot:%d',s,shot); disp(data.msg);
+     data.OK=D.OK; return,
+  elseif (D.OK>1), 
+     data.msg=sprintf('Warning in %s - Data loading for TCV shot:%d',s,shot); disp(data.msg);
+     data.OK=D.OK;
+  end
+  if ((D.Param.Shot~=shot)||(D.Param.System~=sys))
+     data.msg=sprintf('Warning in %s - shot and/or system not correct %d->%d, %d->%d ',s,shot,D.Param.Shot,sys,D.Param.System); disp(data.msg);
+     data.shot=D.Param.Shot; data.system=D.Param.System;
+  end
+  data.load=rmfield(D,'OK');
+  
+  CXRS_FixTrig; % Fix Trig. problem for CXRS ACQ
+
+  data.ACQ.DateTime     = data.load.ACQ.Param.DateTime;
+  data.ACQ.Shot         = data.load.ACQ.Param.Shot;
+  data.ACQ.System       = data.load.ACQ.Param.System;
+  % Convert counts to photons
+  % active signals in photons to CCD
+  if isempty(data.load.CAL.Camera.QEFF.FunctionName),
+      disp('ATTENTION: no data.load.CAL.Camera.QEFF.FunctionName field')
+    if (data.system==1), QE=0.1; elseif (data.system==3), QE=0.1; else, QE=0.15; end
+  else
+    if ~isfield(data.load.Info.Spectrometer,'WaveLength'), data.load.Info.Spectrometer.WaveLength=5286; end
+    cmd=sprintf('QE=%s(%f);',data.load.CAL.Camera.QEFF.FunctionName,data.load.Info.Spectrometer.WaveLength);
+    eval(cmd);     % QE - Camera quantum efficiency (0.968) event/photon
+  end
+  if isempty(data.load.CAL.Camera.AMP.FunctionName),
+    AMP=1; eNoise=1;
+  else
+    cmd=sprintf('[AMP,P,eNoise]=%s(%f,%f,%f,%f,[],%f);',data.load.CAL.Camera.AMP.FunctionName,data.load.ACQ.Setup.PreAmpGain,data.load.ACQ.Setup.OutputAmplifier,data.load.ACQ.Setup.HSSpeed,data.load.ACQ.Setup.EMCCDGain,data.load.Info.Camera.CameraSerialNumber);
+    eval(cmd);     % AMP - ADCcounts/event
+  end
+  data.ACQ.QE      = QE;  % QE - Camera quantum efficiency (0.968) event/photon
+  data.ACQ.AMP     = AMP; % AMP - ADCcounts/event
+  data.ACQ.Spectra      = data.load.ACQ.Spectra/AMP/QE; % ADC counts /(event/photon)/(ADCcount/event) =>  [photons on ccd / pixel]
+  data.ACQ.Label_Spectra = 'data.ACQ.Spectra in [ph], for [counts] use data.load.ACQ.Spectra';
+  data.ACQ.phNoise      = eNoise*QE; % camera noise (in photons)
+  data.last_action=sprintf('Read CXRS data from MDS (atlas) TCV shot:%d - system No.%d - DONE',shot,sys);
+  if k_debug, 
+      disp('          --------------------------- Load Summary ---------------------------')
+      for id=1:length(Stat.OK)
+          if (Stat.OK(id)==1), Msg='Loaded'; else, Msg='NOT Loaded'; end
+          disp(sprintf('%60s - %s',Stat.node{id},Msg));
+      end
+      disp(sprintf('%s - %s',s,data.last_action)); 
+  end
+  if k_plot,
+      figure(1205); clf
+      plot(squeeze(max(data.load.ACQ.Spectra,[],2)));
+      grid on
+      set(gca,'Xlim',[0 size(data.load.ACQ.Spectra,1)+1]);
+      xlabel('WaveLength, pix'); ylabel('Max. counts');
+      title(sprintf('CXRS ACQ Spectra #%d SYS:%2.2d',data.shot,data.system));      
+  end
+  
+case 'file',
+%% 3. Load CXRS experimental data from file from CCD camera - test of CXRS data analysis
+  data.last_action=sprintf('Read CXRS data from file for TCV shot:%d - system No.%d',shot,sys);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  data_path='/macii/karpusho/Work/CXRS/';
+  ACQ_file=sprintf('%sACQ%2.2d_%5.5d.mat',data_path,sys,shot);
+  CAL_file=sprintf('%sCAL%2.2d_%5.5d.mat',data_path,sys,shot);
+  if (exist(ACQ_file,'file')~=2),
+     data.msg=sprintf('ERROR in %s - File %s not found',s,ACQ_file); disp(data.msg);
+     data.OK=-121; return,
+  elseif (exist(CAL_file,'file')~=2),
+     data.msg=sprintf('ERROR in %s - File %s not found',s,CAL_file); disp(data.msg);
+     data.OK=-122; return,
+  end
+  data.load=load(ACQ_file);
+  data.load.CAL=load(CAL_file);
+  data.ACQ.DateTime     = data.load.ACQ.Param.DateTime;
+  data.ACQ.Shot         = data.load.ACQ.Param.Shot;
+  data.ACQ.System       = data.load.ACQ.Param.System;
+  data.ACQ.Spectra      = double(data.load.ACQ.Spectra);
+  
+case 'time',
+%% 4. Load CXRS timing data from MDS (vista)
+  shot = mdsopen(shot); 
+  data.last_action=sprintf('Read CXRS timing for TCV shot %d - system No.%d',shot,sys);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  OK=1;
+  if ~isfield(data,'load'), OK=-1; elseif ~isfield(data.load,'ACQ'), OK=-2; elseif ~isfield(data.load.ACQ,'Timing'), OK=-3; elseif ~isfield(data.load.ACQ.Timing,'TriggerName'), OK=-4; end 
+  if (OK~=1), 
+           data.msg=sprintf('%s ERROR - NO field ''data.load.ACQ.Timing.TriggerName'' in input data structure',s); disp(data.msg);
+           data.OK=-129; return
+  end
+  if (data.load.ACQ.Timing.TriggerName(2)=='\'), icf=2; else, icf=1; end
+  node=data.load.ACQ.Timing.BurstsName(icf:end);
+  data.ACQ.Timing.timpulses=mdsdata(node);
+  data.ACQ.Timing.node_timpulses=node;
+  node=data.load.ACQ.Timing.PeriodName(icf:end);
+  data.ACQ.Timing.timperiode = mdsdata(node)./1e6;
+  data.ACQ.Timing.node_timperiode = node;
+  node=data.load.ACQ.Timing.TriggerName(icf:end);
+  data.ACQ.Timing.timtrig=mdsdata(node);
+  data.ACQ.Timing.node_timtrig=node;
+  if shot > 50100
+       dPeriod=-0.5;
+  elseif (shot>46558),
+      if (sys==3), dPeriod=0.5; else, dPeriod=-0.5; end
+  else
+  % Correction on Trig length and +- for shots <= 46558
+    if (sys==1), dPeriod=-0.5; elseif (sys==3), dPeriod=-0.5; elseif ((shot>41258)&(shot<41892)), dPeriod=-0.5; else, dPeriod=-0.0; end
+    if (shot<41218)&(shot>36396)&(data.system==1), 
+      dPeriod=dPeriod+1; 
+    elseif (shot<=36396)&(shot>0)&(data.system==1), 
+      dPeriod=dPeriod-1; 
+    elseif  (shot<41218)&(data.system==2), 
+      dPeriod=dPeriod-0.5; 
+    elseif (shot>45092)&(data.system==3), 
+      dPeriod=dPeriod+1;
+    end
+    % end of coorection
+  end
+    
+  % disp(dPeriod); 
+  data.ACQ.Time= data.ACQ.Timing.timtrig + ([0:data.ACQ.Timing.timpulses-1]+dPeriod).*data.ACQ.Timing.timperiode;
+  data.ACQ.Time=data.ACQ.Time(1:size(data.ACQ.Spectra,3));
+  data.ACQ.dTime=data.ACQ.Timing.timperiode;  
+  
+  % Fix. Trig problem
+  if (shot==47849)&&(data.system~=3), data.ACQ.Time=data.ACQ.Time-0.02; end 
+  if (shot==47851)&&(data.system==1), data.ACQ.Time=data.ACQ.Time-0.02; end
+  if (shot==47854)&&(data.system==1), data.ACQ.Time=data.ACQ.Time-0.02; end
+  if (shot==47875)&&(data.system==1), data.ACQ.Time=data.ACQ.Time-0.02; end
+  if (shot==47876)&&(data.system==1), data.ACQ.Time=data.ACQ.Time-0.02; end
+  if (shot==48166)&&(data.system==1), data.ACQ.Time=data.ACQ.Time-0.02; end
+  if (shot==47420)&&(data.system==1), data.ACQ.Time=data.ACQ.Time-0.02; end
+  if (shot==47337)&&(data.system==1), data.ACQ.Time=data.ACQ.Time-0.02; end
+  if (shot==48881)&&(data.system==1), data.ACQ.Time=data.ACQ.Time-0.02; end
+  if (shot==48959)&&(data.system==1), data.ACQ.Time=data.ACQ.Time-0.02; end
+
+  data.last_action=sprintf('Read CXRS timing for TCV shot %d - system No.%d - DONE',shot,sys);
+  if k_debug, 
+      disp(sprintf('          CXRS data acquired in %4.4f - %4.4f sec with integration time %2.2f msec',data.ACQ.Time([1 end]),data.ACQ.dTime*1000))
+      disp(sprintf('%s - %s',s,data.last_action)); 
+  end
+  if k_plot,
+      figure(1206); clf
+      plot(data.ACQ.Time(1:size(data.ACQ.Spectra,3)),squeeze(mean(data.ACQ.Spectra,2))*data.ACQ.AMP*data.ACQ.QE);
+      grid on
+      set(gca,'Xlim',data.ACQ.Time([1 end]));
+      xlabel('Time, sec'); ylabel('Mean counts');
+      title(sprintf('CXRS ACQ #%d SYS:%2.2d',data.shot,data.system));      
+  end
+
+case 'plasma_ip',
+%% 5. Read TCV plasma current
+  shot = mdsopen(shot); 
+  data.last_action=sprintf('Read TCV plasma current for TCV shot %d',shot);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  ip_limit=20000.; % minimal Ip 20kA
+  data.plasma.ip_lower_limit_check = ip_limit;
+  node=sprintf('\\magnetics::iplasma:trapeze');
+  %tdis=sprintf('times(getnci("\\%s","TIME_INSERTED"))',node); node_time=mdsvalue(tdis);
+  %tdis=sprintf('getnci("\\%s","OWNER")',node);  node_owner=mdsvalue(tdis);   node_owner=num2str(node_owner);
+  Info=AK_MDS_node_info(node,[],[]); node_owner=Info.node_owner; node_time=Info.node_time;
+  node=sprintf('\\magnetics::iplasma:trapeze[*:*:0.0001]');
+  node_tdi=tdi(node);
+  if strcmp(node_owner,'[0,0]')||strcmp(node_owner,'0'), node_owner='[unknown]'; OK=0; else, OK=1; end
+  if OK, 
+     if k_debug, disp(sprintf('    ---  Ip DATA filled on %s by %s',node_time,node_owner)); end
+  else
+     if k_debug, disp(sprintf('    ---  Ip DATA - EMPTY')); end
+     data.msg=sprintf('%s ERROR - NO data in MDS for %s (#%d)',s,node,shot); disp(data.msg);
+     data.OK=-131; return
+  end
+  data.plasma.ip_shot    = shot;
+  data.plasma.ip_tdi     = node_tdi;
+  data.plasma.ip_date    = node_time;
+  data.plasma.ip_owner   = node_owner;
+  data.plasma.ip         = node_tdi.data-mean(node_tdi.data(1:10));
+  data.plasma.ip_time    = node_tdi.dim{1};
+  max_ip=max(abs(data.plasma.ip));
+  if (max_ip<=ip_limit),
+   data.msg=sprintf('%s ERROR - low |Ip| - %2.2f <= %2.2f kA  (#%d)',s,max_ip/1000,ip_limit/1000,shot); disp(data.msg);
+   data.OK=-132; return
+  end
+  sel=find(abs(data.plasma.ip)>=0.1*max_ip);
+  data.plasma.ip_start=data.plasma.ip_time(sel(1));
+  data.plasma.ip_stop =data.plasma.ip_time(sel(end));
+  if (data.shot~=40539),
+   y = mdsvalue('abs(tcv_eq("BZERO","LIUQE"))');
+   data.plasma.BT=mean(y);
+  else
+   data.plasma.BT=1.46;
+  end
+  data.last_action=sprintf('Read TCV plasma current for TCV shot %d - DONE',shot);
+  if k_debug, 
+      disp(sprintf('    ---  TCV plasma in %2.2f-%2.2f (sec) max |Ip| %2.2f kA; mean BT %2.2f T',data.plasma.ip_start,data.plasma.ip_stop,max_ip/1000,data.plasma.BT));
+      disp(sprintf('%s - %s',s,data.last_action)); 
+  end
+  if k_plot&&(data.OK==1),
+      figure(1207); clf
+      plot(data.plasma.ip_time,data.plasma.ip/1000,'-r','LineWidth',2.);
+      grid on
+      set(gca,'Xlim',data.plasma.ip_time([1 end]),'Ylim',[min(data.plasma.ip)-10.e+3 max(data.plasma.ip)+10.e+3]/1000.);
+      xlabel('Time, sec'); ylabel('Ip, kA');
+      title(sprintf('Plasma current for TCV shot:%d',data.shot));      
+  end
+
+case 'psitbxtcv',
+%% 6. Read TCV psitbxtcv
+  data.last_action=sprintf('Read psitbxtcv for TCV shot %d',shot);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  try
+      source = 'LIUQE';
+      % using LIUQE 2 reconstruction
+      if data.shot== 47340 || data.shot== 47341 || data.shot== 47345  || data.shot== 47351 || data.shot== 47420 || data.shot== 47402 || data.shot== 47418  || data.shot== 47417  || data.shot== 47402 || data.shot== 47418  || data.shot== 47415  || data.shot== 47417  || data.shot== 47906 || data.shot== 47865 || data.shot== 47901
+          data.shot
+          data.shot
+          source = 'LIUQE2';
+      end
+      liuqe_times = mdsvalue('dim_of(tcv_eq("i_p",$1))',source);
+      input_times = liuqe_times(iround(liuqe_times,data.calc.time));
+      psi = psitbxtcv(shot,input_times,'01',source);
+      mask = iround(psi.t,input_times);% Handle duplicate times
+      psi = subs_time(psi,mask);       % Handle duplicate times
+      
+      data.plasma.psi_shot    = shot;
+      data.plasma.psi         = psi;
+  catch
+      data.plasma.psi_shot    = NaN;
+      data.plasma.psi=[];
+      data.msg=sprintf('%s ERROR - psitbxtcv not OK for TCV shhot: %d',s,shot); disp(data.msg);
+      data.OK=-151;
+  end
+  data.last_action=sprintf('Read psitbxtcv for TCV shot %d - DONE',shot);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  if k_plot&&(data.OK==1),
+      figure(1208); clf
+      plot(data.plasma.psi)
+      title(sprintf('PsiTbxTCV for shot No.%d',data.shot));      
+  end
+
+
+case 'plasma_zeff', % deafult value for Zeff
+%% 7. Read Zeff from alternative diagnostics - Not inplemented yet
+  data.last_action=sprintf('Read Zeff for TCV shot %d',shot);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  data.plasma.Zeff = 2.5;
+  data.last_action=sprintf('Read Zeff for TCV shot %d - DONE',shot);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+
+case 'plasma_ts',
+%% 8. Read TCV Thomson Te and ne profiles
+  shot = mdsopen(shot); 
+  data.last_action=sprintf('Read TCV TS ne&Te profiles for TCV shot %d',shot);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+   node_OK=1;
+   node_root      = sprintf('\\RESULTS::THOMSON.PROFILES.AUTO');
+   node_gen       = sprintf('%s:NE',node_root);
+   node_version   = sprintf('%s:VERSION_NUM',node_gen);
+   node_user      = sprintf('%s:USER_NAME',node_gen);
+   % Check version number
+   data_version   = tdi(node_version); data_version   = data_version.data;
+   data_user      = tdi(node_user);    data_user      = data_user.data;
+   if (isempty(data_version)),
+     data.msg=sprintf('%s ERROR - shot #%d node: %s is empty',s,shot,node_version); disp(data.msg);
+     data.OK=-141; return
+   end
+
+  % Read data
+  if (data_version==0),
+     data.msg=sprintf('%s WARNING - shot #%d tree: %s version is "0"',s,shot,node_gen); disp(data.msg);
+     data.msg=sprintf('%s WARNING - try to fill %s for shot #%d (anasrv)',s,node_gen,shot); disp(data.msg);
+  end
+  data_tdi=tdi(node_gen);
+  if (data_version==0),
+     data_version   = tdi(node_version); data_version   = data_version.data;
+     if (data_version==0),
+       data.msg=sprintf('%s ERROR - shot #%d node: %s not filled',s,shot,node_version); disp(data.msg);
+       data.OK=-142; return
+     end
+  end
+  if (isstr(data_tdi.data)),
+       data.msg=sprintf('%s ERROR - shot #%d node: %s not filled',s,shot,node_gen); disp(data.msg);
+       data.OK=-143; return
+  end 
+  tdis=sprintf('times(getnci("\\%s","TIME_INSERTED"))',node_gen);
+  node_time=mdsvalue(tdis);
+
+  d.node_root    = node_root;
+  d.node_gen     = node_gen;
+  d.data_version = data_version;
+  d.node_user    = data_user;
+  d.data_tdi     = data_tdi;
+  d.node_time    = node_time;
+  data.plasma.TS_ne=d;
+
+   node_gen       = sprintf('%s:TE',node_root);
+   node_version   = sprintf('%s:VERSION_NUM',node_gen);
+   node_user      = sprintf('%s:USER_NAME',node_gen);
+   % Check version number
+   data_version   = tdi(node_version); data_version   = data_version.data;
+   data_user      = tdi(node_user);    data_user      = data_user.data;
+   if (isempty(data_version)),
+     data.msg=sprintf('%s ERROR - shot #%d node: %s is empty',s,shot,node_version); disp(data.msg);
+     data.OK=-141; return
+   end
+
+  % Read data
+  if (data_version==0),
+     data.msg=sprintf('%s WARNING - shot #%d tree: %s version is "0"',s,shot,node_gen); disp(data.msg);
+     data.msg=sprintf('%s WARNING - try to fill %s for shot #%d (anasrv)',s,node_gen,shot); disp(data.msg);
+  end
+  data_tdi=tdi(node_gen);
+  if (data_version==0),
+     data_version   = tdi(node_version); data_version   = data_version.data;
+     if (data_version==0),
+       data.msg=sprintf('%s ERROR - shot #%d node: %s not filled',s,shot,node_version); disp(data.msg);
+       data.OK=-142; return
+     end
+  end
+  if (isstr(data_tdi.data)),
+       data.msg=sprintf('%s ERROR - shot #%d node: %s not filled',s,shot,node_gen); disp(data.msg);
+       data.OK=-143; return
+  end 
+  tdis=sprintf('times(getnci("\\%s","TIME_INSERTED"))',node_gen);
+  node_time=mdsvalue(tdis);
+
+  d.node_root    = node_root;
+  d.node_gen     = node_gen;
+  d.data_version = data_version;
+  d.node_user    = data_user;
+  d.data_tdi     = data_tdi;
+  d.node_time    = node_time;
+  data.plasma.TS_Te=d;
+  data.last_action=sprintf('Read TCV TS ne&Te profiles for TCV shot %d - DONE',shot);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  if k_plot&&(data.OK==1),
+      figure(1209); clf
+      subplot(2,1,1)
+      plot(data.plasma.TS_ne.data_tdi.dim{2},data.plasma.TS_ne.data_tdi.data)
+      grid on
+      xlabel('\rho_{\psi}'); ylabel('ne, m^{-3}');
+      title(sprintf('TS data for TCV shot:%d',data.shot));      
+      subplot(2,1,2)
+      plot(data.plasma.TS_Te.data_tdi.dim{2},data.plasma.TS_Te.data_tdi.data)
+      grid on
+      xlabel('\rho_{\psi}'); ylabel('Te, eV');
+  end
+
+case 'result',
+%% 9. Read CXRS Ti,Vi,nC,Zeff results
+  shot = mdsopen(shot); 
+  data.last_action=sprintf('Read CXRS results for TCV shot #%d - system No.%d',shot,sys);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  if ((shot>=20382)&(shot<36390)), CXRS_load_OLD_Results; return; end % Old version of data storage, CXRS not in DS (40 fibers) configuration
+
+  node           = sprintf('\\RESULTS::CXRS_%3.3d:VERSION',sys);
+  d=tdi(node);
+  data.calc.version=d.data;
+  if isempty(data.calc.version),
+       data.msg=sprintf('%s ERROR - shot #%d node: %s not filled',s,shot,node); disp(data.msg);
+       data.OK=-152; return
+  end
+  node           = sprintf('\\RESULTS::CXRS_%3.3d:CONFIDENCE',sys);
+  d=tdi(node);
+  data.calc.confidence=d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d:COMMENT',sys);
+  d=tdi(node);
+  if isempty(d.data), d.data=' '; end
+  data.calc.comment=d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.CX_LINE:NAME',sys);
+  d=tdi(node);
+  data.Info.CX_Line.Name=d.data; data.calc.cxrs_line_name = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.CX_LINE:WAVELENGTH',sys);
+  d=tdi(node);
+  data.Info.CX_Line.WaveLength=d.data; data.calc.cxrs_line = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.CX_LINE:MASS_NUMBER',sys);
+  d=tdi(node);
+  data.Info.CX_Line.Mass_Number=d.data; data.calc.atom_numb = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.CX_LINE:ION_CHARGE',sys);
+  d=tdi(node);
+  data.Info.CX_Line.Ion_Charge=d.data; data.calc.ion_charge = d.data;
+
+  % Read Ti
+  node           = sprintf('\\RESULTS::CXRS_%3.3d:TI:VERSION',sys);
+  d=tdi(node);
+  data.calc.Ti_version=d.data;
+  if mod(d.status,2),
+  % if ~isempty(data.calc.Ti_version)
+  % if 1
+    node           = sprintf('\\RESULTS::CXRS_%3.3d:TI:CONFIDENCE',sys);
+    d=tdi(node);
+    data.calc.Ti_confidence=d.data;
+    node           = sprintf('\\RESULTS::CXRS_%3.3d:TI:COMMENT',sys);
+    d=tdi(node);
+    if isempty(d.data), d.data=' '; end
+    data.calc.Ti_comment=d.data;
+    node           = sprintf('\\RESULTS::CXRS_%3.3d:TI',sys);  
+    d=tdi(node);  
+    data.calc.Ti=d.data;
+    data.calc.Ti_units = '[eV]';
+    data.calc.rho =d.dim{1};
+    if (length(d.dim)==1), 
+        nn=sprintf('\\RESULTS::CXRS_%3.3d:ANALYSIS.TIME',sys);dd=tdi(nn);
+        nn=sprintf('\\RESULTS::CXRS_%3.3d:ANALYSIS.TIME_INDEX:USE',sys);cc=tdi(nn);
+        d.dim{2}=dd.data(cc.data);
+    end
+    data.calc.time=d.dim{2};
+    data.calc.rho_type=d.dimunits{1};
+    if ~isempty(findstr(d.dimunits{1},'from DNB atten')), data.calc.rho_dnbi = data.calc.rho; end
+    %tdis=sprintf('times(getnci("\\%s","TIME_INSERTED"))',node); node_str_time = mdsvalue(tdis);
+    %tdis=sprintf('getnci("\\%s","OWNER")',node);                   node_owner    = mdsvalue(tdis);    node_owner=num2str(node_owner);
+    Info=AK_MDS_node_info(node,[],[]); node_owner=Info.node_owner; node_str_time=Info.node_time;
+    data.calc.Ti_node_time = node_str_time; data.calc.Ti_node_owner = node_owner;    
+    if k_debug, msg=sprintf('%35s: filled at %s by %s loaded',node,node_str_time,node_owner); disp(msg); end
+    node           = sprintf('\\RESULTS::CXRS_%3.3d:TI:ERR',sys);  
+    d=tdi(node);
+    data.calc.err_Ti=d.data;
+    if (length(d.dim)==1), 
+        nn=sprintf('\\RESULTS::CXRS_%3.3d:ANALYSIS.TIME',sys);dd=tdi(nn);
+        nn=sprintf('\\RESULTS::CXRS_%3.3d:ANALYSIS.TIME_INDEX:USE',sys);cc=tdi(nn);
+        d.dim{2}=dd.data(cc.data);
+    end    
+    if ~isempty(data.calc.Ti_version)
+      data.calc.rho_err =d.dim{1};
+      data.calc.dtime=mean(d.dim{2});
+    else
+      data.calc.rho_err =data.calc.rho/10.;
+      data.calc.dtime=min(diff(data.calc.time));
+    end
+    if ~isempty(findstr(d.dimunits{1},'from DNB atten')), data.calc.rho_dnbi_err = data.calc.rho_err; end    
+    if k_plot
+     figure(1211); clf
+     X_ALL=data.calc.rho; Y_ALL=data.calc.Ti; dX_ALL=data.calc.rho_err; dY_ALL=data.calc.err_Ti;
+     T_ALL=data.calc.time; dT_ALL=data.calc.dtime;
+     G=CXRS_plot_XYT(X_ALL,dX_ALL,Y_ALL,dY_ALL,T_ALL,dT_ALL);
+     if (data.system==1), vdir='toroidal'; elseif (data.system==2), vdir='poloidal'; else, vdir='??'; end
+     title(sprintf('CXRS #%d SYS:%3.3d (%s) ion temperature',data.shot,data.system,vdir));
+     ylabel(sprintf('T C, eV'));
+    end
+  end
+  % Read Vi
+  node           = sprintf('\\RESULTS::CXRS_%3.3d:VI:VERSION',sys);
+  d=tdi(node);
+  data.calc.vi_version=d.data;
+  if mod(d.status,2),
+  %if ~isempty(data.calc.vi_version)
+  %if 1
+    node           = sprintf('\\RESULTS::CXRS_%3.3d:VI:CONFIDENCE',sys);
+    d=tdi(node);
+    data.calc.vi_confidence=d.data;
+    node           = sprintf('\\RESULTS::CXRS_%3.3d:VI:COMMENT',sys);
+    d=tdi(node);
+    if isempty(d.data), d.data=' '; end
+    data.calc.vi_comment=d.data;
+    node           = sprintf('\\RESULTS::CXRS_%3.3d:VI',sys);  
+    d=tdi(node);
+    if (length(d.dim)==1)&(~isempty(d.data)), 
+        nn=sprintf('\\RESULTS::CXRS_%3.3d:ANALYSIS.TIME',sys);dd=tdi(nn);
+        nn=sprintf('\\RESULTS::CXRS_%3.3d:ANALYSIS.TIME_INDEX:USE',sys);cc=tdi(nn);
+        d.dim{2}=dd.data(cc.data);
+    end    
+    if isempty(d.data)
+       disp('!!!! Vi results are empty!!!!');
+    elseif (max(abs(data.calc.time-d.dim{2}))==0)
+      data.calc.vi=d.data;
+      data.calc.vi_units = '[km/s]';
+      %tdis=sprintf('times(getnci("\\%s","TIME_INSERTED"))',node); node_str_time = mdsvalue(tdis);
+      %tdis=sprintf('getnci("\\%s","OWNER")',node);                   node_owner    = mdsvalue(tdis);   node_owner=num2str(node_owner);
+      Info=AK_MDS_node_info(node,[],[]); node_owner=Info.node_owner; node_str_time=Info.node_time;
+      data.calc.vi_node_time = node_str_time; data.calc.vi_node_owner = node_owner;
+      if k_debug, msg=sprintf('%35s: filled at %s by %s loaded',node,node_str_time,node_owner); disp(msg); end
+      node           = sprintf('\\RESULTS::CXRS_%3.3d:VI:ERR',sys);  
+      d=tdi(node);
+      data.calc.err_vi=d.data;
+       if k_plot
+         figure(1212); clf
+         X_ALL=data.calc.rho; Y_ALL=data.calc.vi; dX_ALL=data.calc.rho_err; dY_ALL=data.calc.err_vi;
+         T_ALL=data.calc.time; dT_ALL=data.calc.dtime;
+         G=CXRS_plot_XYT(X_ALL,dX_ALL,Y_ALL,dY_ALL,T_ALL,dT_ALL);
+         if (data.system==1), vdir='toroidal'; elseif (data.system==2), vdir='poloidal'; else, vdir='??'; end
+         title(sprintf('CXRS #%d SYS:%3.3d (%s) velocity',data.shot,data.system,vdir));
+         ylabel(sprintf('V C, [km/s]'));
+       end
+    else
+       disp('!!!! Different time basis for Ti and Vi !!!!');
+    end
+  end
+  % Read Ni
+  node           = sprintf('\\RESULTS::CXRS_%3.3d:NI:VERSION',sys);
+  d=tdi(node);
+  data.calc.ni_version=d.data;
+  %if ~isempty(data.calc.ni_version)
+  if mod(d.status,2),
+ %if 1
+    node           = sprintf('\\RESULTS::CXRS_%3.3d:NI:CONFIDENCE',sys);
+    d=tdi(node);
+    data.calc.ni_confidence=d.data;
+    node           = sprintf('\\RESULTS::CXRS_%3.3d:NI:COMMENT',sys);
+    d=tdi(node);
+    if isempty(d.data), d.data=' '; end
+    data.calc.ni_comment=d.data;
+    node           = sprintf('\\RESULTS::CXRS_%3.3d:NI',sys);  
+    d=tdi(node);
+    if (length(d.dim)==1)&(~isempty(d.data)), 
+        nn=sprintf('\\RESULTS::CXRS_%3.3d:ANALYSIS.TIME',sys);dd=tdi(nn);
+        nn=sprintf('\\RESULTS::CXRS_%3.3d:ANALYSIS.TIME_INDEX:USE',sys);cc=tdi(nn);
+        d.dim{2}=dd.data(cc.data);
+    end    
+    if isempty(d.data)
+       disp('!!!! Ni results are empty!!!!');
+    elseif (max(abs(data.calc.time-d.dim{2}))==0)
+      data.calc.ni=d.data;
+      data.calc.ni_units = '[m^-^3]';
+      %tdis=sprintf('times(getnci("\\%s","TIME_INSERTED"))',node); node_str_time = mdsvalue(tdis);
+      %tdis=sprintf('getnci("\\%s","OWNER")',node);                   node_owner    = mdsvalue(tdis);  node_owner=num2str(node_owner);
+      Info=AK_MDS_node_info(node,[],[]); node_owner=Info.node_owner; node_str_time=Info.node_time;
+      data.calc.ni_node_time = node_str_time; data.calc.ni_node_owner = node_owner;
+      if k_debug, msg=sprintf('%35s: filled at %s by %s loaded',node,node_str_time,node_owner); disp(msg); end
+      node           = sprintf('\\RESULTS::CXRS_%3.3d:NI:ERR',sys);  
+      d=tdi(node);
+      data.calc.err_ni=d.data;
+      if k_plot
+        figure(1213); clf; subplot(2,1,1);
+        X_ALL=data.calc.rho; Y_ALL=data.calc.ni; dX_ALL=data.calc.rho_err; dY_ALL=data.calc.err_ni;
+        T_ALL=data.calc.time; dT_ALL=data.calc.dtime;
+        G=CXRS_plot_XYT(X_ALL,dX_ALL,Y_ALL,dY_ALL,T_ALL,dT_ALL);
+        title(sprintf('CXRS #%d SYS:%3.3d carbon density',data.shot,data.system));
+        ylabel(sprintf('nC, [m^{-3}]'));
+      end
+    else
+       disp('!!!! Different time basis for Ti and Ni !!!!');
+    end
+  end
+  % Read Zeff
+  node           = sprintf('\\RESULTS::CXRS_%3.3d:ZEFF:VERSION',sys);
+  d=tdi(node);
+  data.calc.zeff_version=d.data;
+  if mod(d.status,2),
+  %if ~isempty(data.calc.zeff_version)
+    node           = sprintf('\\RESULTS::CXRS_%3.3d:ZEFF:CONFIDENCE',sys);
+    d=tdi(node);
+    data.calc.zeff_confidence=d.data;
+    node           = sprintf('\\RESULTS::CXRS_%3.3d:ZEFF:COMMENT',sys);
+    d=tdi(node);
+    if isempty(d.data), d.data=' '; end
+    data.calc.zeff_comment=d.data;
+    node           = sprintf('\\RESULTS::CXRS_%3.3d:ZEFF',sys);  
+    d=tdi(node);
+    if (length(d.dim)==1)&(~isempty(d.data)), 
+        nn=sprintf('\\RESULTS::CXRS_%3.3d:ANALYSIS.TIME',sys);dd=tdi(nn);
+        nn=sprintf('\\RESULTS::CXRS_%3.3d:ANALYSIS.TIME_INDEX:USE',sys);cc=tdi(nn);
+        d.dim{2}=dd.data(cc.data);
+    end    
+    if (max(abs(data.calc.time-d.dim{2}))==0)
+      data.calc.zeff=d.data;
+      data.calc.zeff_units = '';
+      %tdis=sprintf('times(getnci("\\%s","TIME_INSERTED"))',node); node_str_time = mdsvalue(tdis);
+      %tdis=sprintf('getnci("\\%s","OWNER")',node);                   node_owner    = mdsvalue(tdis);  node_owner=num2str(node_owner);
+      Info=AK_MDS_node_info(node,[],[]); node_owner=Info.node_owner; node_str_time=Info.node_time;
+      data.calc.zeff_node_time = node_str_time; data.calc.zeff_node_owner = node_owner;
+      if k_debug, msg=sprintf('%35s: filled at %s by %s loaded',node,node_str_time,node_owner); disp(msg); end
+      node           = sprintf('\\RESULTS::CXRS_%3.3d:ZEFF:ERR',sys);  
+      d=tdi(node);
+      data.calc.err_zeff=d.data;
+      if k_plot
+        figure(1213); subplot(2,1,2);
+        X_ALL=data.calc.rho; Y_ALL=data.calc.zeff; dX_ALL=data.calc.rho_err; dY_ALL=data.calc.err_zeff;
+        T_ALL=data.calc.time; dT_ALL=data.calc.dtime;
+        G=CXRS_plot_XYT(X_ALL,dX_ALL,Y_ALL,dY_ALL,T_ALL,dT_ALL);
+        title(sprintf('CXRS #%d SYS:%3.3d Zeff',data.shot,data.system));
+        ylabel(sprintf('Zeff'));
+      end
+    else
+       disp('!!!! Different time basis for Ti and Zeff !!!!');
+    end
+  end
+  
+  % Read angle gamma
+  node = sprintf('\\RESULTS::CXRS_%3.3d:PRO_ANG',sys);
+  d=tdi(node);
+  if mod(d.status,2)
+      data.calc.gamma = d.data;
+      data.calc.gamma_label = 'Angles between the toroidal direction and the viewline directions, range [0:pi), [rad]' ;
+  else
+      data.calc.gamma = [];
+  end
+  
+  % Read angle delta
+  node = sprintf('\\RESULTS::CXRS_%3.3d:DELTA_ANG',sys);
+  d=tdi(node);
+  if mod(d.status,2)
+      data.calc.delta = d.data;
+      data.calc.delta_label =[ d.help ' ' d.units] ;
+  else
+      data.calc.delta = [];
+  end
+  
+ % Read Radiance
+  node = sprintf('\\RESULTS::CXRS_%3.3d:RADIANCE',sys);
+  d=tdi(node);
+  if mod(d.status,2)
+      data.calc.RAD = d.data;
+      data.calc.RAD_units = '[ph/s/sr/m^2]';
+      data.calc.RAD_label =[ d.help ' ' d.units] ;
+  else
+      data.calc.RAD = [];
+  end 
+  
+  % Read Radiance uncertainty
+  node = sprintf('\\RESULTS::CXRS_%3.3d:RADIANCE:ERR',sys);
+  d=tdi(node);
+  if mod(d.status,2)
+      data.calc.err_RAD = d.data;
+      %data.calc.RAD_label =[ d.help ' ' d.units] ;
+  else
+      data.calc.err_RAD = [];
+  end 
+  
+  % Read R
+  node = sprintf('\\RESULTS::CXRS_%3.3d:R',sys);
+  d=tdi(node);
+  if mod(d.status,2)
+      data.calc.R = d.data;
+  else
+      data.calc.R = [];
+  end
+  
+  % Read Z
+  node = sprintf('\\RESULTS::CXRS_%3.3d:Z',sys);
+  d=tdi(node);
+  if mod(d.status,2)
+      data.calc.Z = d.data;
+  else
+      data.calc.Z = [];
+  end
+  
+  % Read Residuals
+  node = sprintf('\\RESULTS::CXRS_%3.3d:RESIDUALS',sys);
+  d=tdi(node);
+  if mod(d.status,2)
+      data.calc.res_and_sat= d.data;
+      data.calc.res_and_sat_label =[ d.help ' ' d.units] ;
+  else
+      data.calc.res_and_sat = [];
+  end
+  
+  if isfield(data.calc,'Ti'),  
+      data.last_action=sprintf('Read CXRS results for TCV shot #%d - system No.%d - DONE',shot,sys);
+  else
+      data.last_action=sprintf('Read CXRS results for TCV shot #%d - system No.%d - ERROR',shot,sys);
+      data.OK=-9001;   
+  end
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+   
+case 'analysis',
+%% 10. Read CXRS analysis data
+  shot = mdsopen(shot); 
+  data.last_action=sprintf('Read CXRS analysis data for TCV shot #%d - system No.%d',shot,sys);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:COMMENT',sys);
+  d=tdi(node);
+  data.use.comment=d.data;
+  if isempty(data.use.comment),
+       data.msg=sprintf('%s ERROR - shot #%d node: %s not filled',s,shot,node); disp(data.msg);
+       data.OK=-162; return
+  end
+  if k_debug, disp(sprintf('      Analysis info: %s',data.use.comment)); end
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:PARAM',sys);
+  d=tdi(node);
+  V=String2Struct(d.data);
+  data.ACQ.DateTime = V.DateTime;
+  data.ACQ.Shot     = V.Shot;
+  data.ACQ.System   = V.System;
+  data.ACQ.dTime    = V.dTime;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:SOURCES',sys);
+  d=tdi(node);
+  V=String2Struct(d.data);  
+  %V.ACQ_From               = 'MDS, atlas';
+  data.load.ACQ.Param.DateTime       = V.ACQ_DateTime;
+  data.load.ACQ.Param.File           = V.ACQ_File;
+  data.load.ACQ.Param.NumberKinetics = V.ACQ_NumberKinetics;
+  data.load.ACQ.Param.Shot           = V.ACQ_Shot;
+  data.load.ACQ.Param.System         = V.ACQ_System;
+  data.calibr.Comment                = V.CAL_Comment;
+  data.calibr.Function               = V.CAL_Function;
+  data.calibr.DateTime               = V.CAL_DateTime;
+  data.calibr.Shot                   = V.CAL_Shot;
+  data.calibr.System                 = V.CAL_System;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:FIT',sys);
+  d=tdi(node);
+  V=String2Struct(d.data);  
+  data.fit.version  = V.version;
+  data.fit.fitfun   = V.fitfun;
+  data.fit.accept   = V.accept;
+  data.fit.max_its  = V.max_its;
+  data.fit.comment  = V.comment;
+  data.fit.pix_lim  = V.pix_lim;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:TIME',sys);
+  d=tdi(node);
+  data.use.time = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:TIME_INDEX:ACTIVE',sys);
+  d=tdi(node);
+  data.use.with_beam = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:TIME_INDEX:PASSIVE',sys);
+  d=tdi(node);
+  data.use.frames_OFF = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:TIME_INDEX:USE',sys);
+  d=tdi(node);
+  data.use.frames_ON = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:SPECTRA',sys);  
+  d=tdi(node);
+  data.use.spectra = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:SPECTRA:ERR',sys);  
+  d=tdi(node);
+  data.use.spectra_err = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:ACT_SPECTRA',sys);  
+  d=tdi(node);
+  data.use.spectra_active = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:ACT_SPECTRA:ERR',sys);  
+  d=tdi(node);
+  data.use.spectra_active_err = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:PASS_SPECTRA',sys);  
+  d=tdi(node);
+  data.use.spectra_passive = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:PASS_SPECTRA:ERR',sys);  
+  d=tdi(node);
+  data.use.spectra_passive_err = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:ACT_2_PASS',sys);
+  d=tdi(node);
+  data.use.act2pass = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.ANALYSIS:BAD_CHANNELS',sys);
+  d=tdi(node);
+  data.use.bad_channels = d.data;  
+  if k_plot
+        figure(1211); clf; subplot(3,1,1);
+        chan=1;
+        plot(squeeze(data.use.spectra_active(:,chan,:))); hold on
+        plot(squeeze(data.use.spectra_passive(:,chan,:)));
+        title(sprintf('CXRS ch.%2.2d active and passive spectra for #%d Sys.%2.2d',chan,data.shot,data.system));
+        ylabel(sprintf('photons'));
+        grid on
+        set(gca,'Xlim',[0 size(data.use.spectra_active,1)+1],'Ylim',[-0.02 1.05]*max(max(squeeze(data.use.spectra_passive(:,chan,:)))));
+        plot(data.fit.pix_lim,[0 0],'ok','LineWidth',2,'MarkerSize',10);
+        subplot(3,1,2);
+        chan=round(size(data.use.spectra_active,2)/2);
+        plot(squeeze(data.use.spectra_active(:,chan,:))); hold on
+        plot(squeeze(data.use.spectra_passive(:,chan,:)));
+        title(sprintf('CXRS ch.%2.2d active and passive spectra for #%d Sys.%2.2d',chan,data.shot,data.system));
+        ylabel(sprintf('photons'));
+        grid on
+        set(gca,'Xlim',[0 size(data.use.spectra_active,1)+1],'Ylim',[-0.02 1.05]*max(max(squeeze(data.use.spectra_passive(:,chan,:)))));
+        plot(data.fit.pix_lim,[0 0],'ok','LineWidth',2,'MarkerSize',10);
+        subplot(3,1,3);
+        chan=size(data.use.spectra_active,2);
+        plot(squeeze(data.use.spectra_active(:,chan,:))); hold on
+        plot(squeeze(data.use.spectra_passive(:,chan,:)));
+        title(sprintf('CXRS ch.%2.2d active and passive spectra for #%d Sys.%2.2d',chan,data.shot,data.system));
+        ylabel(sprintf('photons'));
+        grid on
+        set(gca,'Xlim',[0 size(data.use.spectra_active,1)+1],'Ylim',[-0.02 1.05]*max(max(squeeze(data.use.spectra_passive(:,chan,:)))));
+        plot(data.fit.pix_lim,[0 0],'ok','LineWidth',2,'MarkerSize',10);
+  end
+  data.last_action=sprintf('CXRS analysis data for TCV shot #%d - system No.%d - Loaded',shot,sys);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+   
+case 'calibration',
+%% 11. Read CXRS calibration data
+  shot = mdsopen(shot); 
+  data.last_action=sprintf('Read CXRS calibration for TCV shot #%d - system No.%d',shot,sys);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.WL_CAL:COMMENT',sys);
+  d=tdi(node);
+  if strcmpi(d.data,'t');
+      c_comment = d.units;
+  else
+      c_comment = d.data;
+  end
+  data.calibr.Comment=c_comment;
+  data.calibr.lamcalib.comment = c_comment;
+  if isempty(data.calibr.Comment),
+       data.msg=sprintf('%s ERROR - shot #%d node: %s not filled',s,shot,node); disp(data.msg);
+       data.OK=-172; return
+  end
+  if k_debug, disp(sprintf('      Calibration info: %s',data.calibr.Comment)); end
+  %data.calibr.lamcalib.version,data.load.Info.Spectrometer.WaveLength
+  
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.WL_CAL:PIX0',sys);  
+  d=tdi(node);
+  data.calibr.lamcalib.pix_0 = d.data;
+  
+  n_slit = size(data.calibr.lamcalib.pix_0,2);
+  
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.WL_CAL:PARAM',sys);
+  d=tdi(node);
+  if strcmpi(d.data,'t');
+      c_str = d.units;
+  else
+      c_str = d.data;
+  end
+  V=String2Struct(c_str);  
+  data.calibr.Shot                         = V.Shot;
+  data.calibr.System                       = V.System;
+  data.calibr.Function                     = V.Function;
+  data.calibr.lamcalib.shot                = V.Shot;
+  data.calibr.lamcalib.system              = V.System;
+  data.calibr.lamcalib.version             = V.Version;
+  data.calibr.lamcalib.CX_Line_WaveLength  = V.CX_Line_WaveLength;
+  data.load.Info.Spectrometer.WaveLength   = V.Spectrometer_WaveLength;
+  data.calibr.lamcalib.LampWLs             = V.LampWLs;
+  for i_sl = 1:n_slit;
+      c_com = sprintf('data.calibr.lamcalib.LampWLs_use{%d}      = V.LampWLs_use%d;',i_sl,i_sl);
+      eval(c_com);
+  end
+  data.calibr.lamcalib.FitFun              = V.FitFun;
+  data.calibr.lamcalib.pix_bg0             = V.pix_bg0;
+  
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.WL_CAL:PIX0:ERR',sys);  
+  d=tdi(node);
+  data.calibr.lamcalib.dpix_0 = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.WL_CAL:WAVELENGTH',sys);  
+  d=tdi(node);
+  data.calibr.lamcalib.lambda_all = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.WL_CAL:WAVELENGTH:ERR',sys);  
+  d=tdi(node);
+  data.calibr.lamcalib.dlamdpix = d.data./data.calibr.lamcalib.dpix_0;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.WL_CAL:TRACKS',sys);
+  d=tdi(node);
+  if strcmpi(d.data,'f');
+      c_val = d.units;
+  else
+      c_val = d.data;
+  end
+  data.load.ACQ.Tracks = c_val;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.IF_CAL:COMMENT',sys);
+  d=tdi(node);
+  if strcmpi(d.data,'t');
+      c_str = d.units;
+  else
+      c_str = d.data;
+  end
+  data.calibr.instrfun.comment = c_str;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.IF_CAL:PARAM',sys);
+  d=tdi(node);
+  if strcmpi(d.data,'t');
+      c_str = d.units;
+  else
+      c_str = d.data;
+  end
+  V=String2Struct(c_str);
+  data.calibr.instrfun.shot           = V.Shot;
+  data.calibr.instrfun.system         = V.System;
+  data.calibr.instrfun.version        = V.Version;
+  data.calibr.Function                = V.Function;
+  data.load.Info.Spectrometer.WaveLength = V.Spectrometer_WaveLength;
+  data.calibr.instrfun.LampWL_use_A      = V.LampWLs';
+  data.calibr.instrfun.LampWL_use        = V.LampWLs_use;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.IF_CAL:SPECTRA',sys);  
+  d=tdi(node);
+  data.calibr.instrfun.shape = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.IF_CAL:X0',sys);  
+  d=tdi(node);
+  data.calibr.instrfun.x0 = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.IF_CAL:X0:ERR',sys);  
+  d=tdi(node);
+  data.calibr.instrfun.dx0 = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.IF_CAL:FWHM',sys);  
+  d=tdi(node);
+  data.calibr.instrfun.FWHM = d.data;
+  node           = sprintf('\\RESULTS::CXRS_%3.3d.IF_CAL:FWHM:ERR',sys);  
+  d=tdi(node);
+  data.calibr.instrfun.dFWHM = d.data;    
+  C=data.calibr;
+  symb = {'-vr' '-^b' '-*m' '-oc'};
+  if k_plot
+    figure(1903); clf
+    subplot(2,1,1);
+    for i_sl = 1: n_slit;
+        c_com = sprintf('plot(C.lamcalib.pix_0(:,%d)-mean(C.lamcalib.pix_0(:,%d)),''%s'',''LineWidth'',2);',i_sl,i_sl,symb{i_sl});
+        eval(c_com);
+        hold on
+    end
+    %plot(C.lamcalib.pix_0(:,1)-mean(C.lamcalib.pix_0(:,1)),'-vr','LineWidth',2); hold on
+    %plot(C.lamcalib.pix_0(:,2)-mean(C.lamcalib.pix_0(:,2)),'-^b','LineWidth',2); hold on
+    grid on;
+    title(sprintf('CXRS calibration shift of %3.3f (A) line',C.lamcalib.CX_Line_WaveLength));
+    xlabel('channel'); ylabel('shift, pix');
+    subplot(2,1,2);
+    for i_sl = 1: n_slit;
+        c_com = sprintf('plot(C.lamcalib.dpix_0(:,%d),''%s'',''LineWidth'',2);',i_sl,symb{i_sl});
+        eval(c_com);
+        hold on
+    end
+    %plot(C.lamcalib.dpix_0(:,1),'-vr','LineWidth',2); hold on
+    %plot(C.lamcalib.dpix_0(:,2),'-^b','LineWidth',2); hold on
+    grid on;
+    title(sprintf('Error in position of %3.3f (A) line',C.lamcalib.CX_Line_WaveLength));
+    xlabel('channel'); ylabel('shift, pix');
+ 
+    figure(1904); clf
+    fwhm=squeeze(sum(C.instrfun.shape(:,:,:)))./squeeze(max(C.instrfun.shape(:,:,:)));
+    if size(fwhm,1)==1; fwhm = fwhm(:); end
+    for i_sl = 1: n_slit;
+        c_com = sprintf('plot(C.instrfun.FWHM(:,%d),''%s'',''LineWidth'',2);',i_sl,symb{i_sl});
+        eval(c_com);
+        hold on
+        c_com = sprintf('plot(fwhm(:,%d),''-%s'',''LineWidth'',2);',i_sl,symb{i_sl});
+        eval(c_com);
+        hold on
+    end
+    %plot(C.instrfun.FWHM(:,1),'-vr','LineWidth',2); hold on
+    %plot(C.instrfun.FWHM(:,2),'-^b','LineWidth',2); hold on
+    %plot(fwhm(:,1),'--<r','LineWidth',1); hold on
+    %plot(fwhm(:,2),'-->b','LineWidth',1); hold on
+    grid on;
+    title(sprintf('CXRS instrumental function FWHM for TCV shot:%d  system No.%2.2d',C.Shot,C.System));
+    xlabel('channel'); ylabel('FWHM, pix');  
+  end  
+  data.last_action=sprintf('CXRS calibration for TCV shot #%d - system No.%d - Loaded',shot,sys);
+  if k_debug, 
+      disp(sprintf('      Unsifted positions for 2 stits: %2.2f/%2.2f pix',mean(C.lamcalib.pix_0(:,:))));
+      disp(sprintf('%s - %s',s,data.last_action)); 
+  end
+
+case 'profiles',
+%% 12. Read CXRS profiles
+  shot = mdsopen(shot); 
+  data.last_action=sprintf('Read CXRS profiles for TCV shot #%d',shot);
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  
+  data.proffit.shot        =shot;
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:TI:COMMENT');
+  d=tdi(node);
+  data.proffit.comment = d.data;
+  if isempty(d.data),
+       data.msg=sprintf('%s ERROR - shot #%d node: %s not filled',s,shot,node); disp(data.msg);
+       data.OK=-192; return
+  end
+  data.proffit.comment = d.data(6:end);
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:TIME');
+  d=tdi(node); data.proffit.time = d.data;
+  if length(d.data)>1, data.proffit.dtime=min(diff(data.proffit.time))/6; else, data.proffit.dtime=NaN; end
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:RHO');
+  d=tdi(node); data.proffit.rho = d.data;
+
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:TI');  
+  d=tdi(node); data.proffit.Ti = d.data;
+  p.title  =d.help; p.xlabel =d.dimunits{1}; p.ylabel =d.units;
+  forplot.Ti=p;
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:TI:ERR');  
+  d=tdi(node); data.proffit.dTi = d.data;
+  data.proffit.Ti_rho = d.dim{1};
+
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:VI_TOR');  
+  d=tdi(node); data.proffit.vTor = d.data;
+  p.title  =d.help; p.xlabel =d.dimunits{1}; p.ylabel =d.units;
+  forplot.vTor=p;
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:VI_TOR:ERR');  
+  d=tdi(node); data.proffit.dvTor = d.data;
+  data.proffit.vTor_rho = d.dim{1};
+
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:VI_POL');  
+  d=tdi(node); data.proffit.vPol = d.data;
+  p.title  =d.help; p.xlabel =d.dimunits{1}; p.ylabel =d.units;
+  forplot.vPol=p;
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:VI_POL:ERR');  
+  d=tdi(node); data.proffit.dvPol = d.data;
+  data.proffit.vPol_rho = d.dim{1};
+
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:NI');  
+  d=tdi(node); data.proffit.nC = d.data;
+  p.title  =d.help; p.xlabel =d.dimunits{1}; p.ylabel =d.units;
+  forplot.nC=p;
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:NI:ERR');  
+  d=tdi(node); data.proffit.dnC = d.data;
+  data.proffit.nC_rho = d.dim{1};
+
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:TI:PARAM');
+  d=tdi(node); 
+  if ~isempty(d.data)
+      if strcmpi(d.data,'t')
+          pp=String2Struct(d.units);
+      else
+          pp=String2Struct(d.data);
+      end
+  else
+      pp=[];
+  end
+  data.proffit.param.Ti=pp;
+
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:VI_TOR:PARAM');
+  d=tdi(node); 
+  if ~isempty(d.data)
+      if strcmpi(d.data,'t')
+          pp=String2Struct(d.units);
+      else
+          pp=String2Struct(d.data);
+      end
+  else
+      pp=[];
+  end
+  data.proffit.param.vi=pp;
+ 
+  node           = sprintf('\\RESULTS::CXRS.PROFFIT:NI:PARAM');
+  d=tdi(node); 
+  if ~isempty(d.data)
+      if strcmpi(d.data,'t')
+          pp=String2Struct(d.units);
+      else
+          pp=String2Struct(d.data);
+      end
+  else
+      pp=[];
+  end
+  data.proffit.param.nc=pp;
+    
+  data.OK=1;
+
+  if k_plot
+      ifig=9900;
+      what2plot={'Ti' 'vTor' 'vPol' 'nC'};
+      for ii=1:length(what2plot),
+          ifig=ifig+1; figure(ifig); clf
+          eval(sprintf('X=data.proffit.%s_rho; Y=data.proffit.%s; dY=data.proffit.d%s; p=forplot.%s;',what2plot{ii},what2plot{ii},what2plot{ii},what2plot{ii}));
+          if length(Y)>0
+              dX=ones(size(X))*min(min(diff(X)))/2.;             
+              G=CXRS_plot_XYT(X,dX,Y,dY,data.proffit.time,min(diff(data.proffit.time))/6.);
+              title(p.title); xlabel(p.xlabel); ylabel(p.ylabel);
+              set(gca,'Xlim',[min(min(X)) max(max(X))],'Ylim',[min(min(Y-dY)) max(max(Y+dY))]);
+          end  
+      end
+
+  end
+  
+otherwise,
+  data.last_action=sprintf('Read CXRS unknown (%s) action: USE cxrsdsi,atlas, time, result, etc.',what2read); 
+  if k_debug, disp(sprintf('%s - %s',s,data.last_action)); end
+  data.OK=0;
+end 
+
+return
diff --git a/crpptbx/TCV/CXRS_plot_definition.m b/crpptbx/TCV/CXRS_plot_definition.m
new file mode 100644
index 00000000..23692283
--- /dev/null
+++ b/crpptbx/TCV/CXRS_plot_definition.m
@@ -0,0 +1,27 @@
+function [colos,lnstyle,typemk]=CXRS_plot_definition
+% [colos,lnstyle,typemk]=CXRS_plot_definition
+% define colors, linestyles and markers
+% taken from OS 
+col_1 =  [1.    0.    0.   ]; % red
+col_2 =  [0.    0.4   0.   ]; % dark green
+col_3 =  [0     0     1    ]; % blue
+col_4 =  [0.7   0.4   0.9  ]; % purple
+col_5 =  [0.    1.    0.9  ]; % cyan
+col_6 =  [1     0.85  0.3  ]; % yellow
+col_7 =  [0.    1.    0.   ]; % light green
+col_8 =  [1.0   0.1   0.7  ]; % reddish magenta
+col_9=   [1.    0.7   0    ];
+col_10 = [0     0.52  0.93 ];
+col_11=  [0.    0.    0.   ]; % black
+col_12=  [0.075 0.933 0.676];
+col_13 = [0.8   0.1   0.95 ];
+% using uisetcolor
+typemk={'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h', '*', 'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h', '*', 'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h', '*', 'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h', '*', 'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h', '*', 'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h', '*', 'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h', '*', 'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h', '*', 'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h', '*', 'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h', '*', 'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h', '*', 'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h', '*', 'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h', '*', 'o', 'p', 'd', 's', '^', 'v', '<', '>', 'h','*'};
+typemk={'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*', 'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*', 'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*', 'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*', 'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*', 'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*', 'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*', 'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*', 'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*', 'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*', 'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*', 'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*', 'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*', 'o', 's', '^', 'v', '<', '>', 'p', 'd', 'h', '*'};
+lnstyle={'-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':','-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':', '-', '--', '-.', ':'};
+colos=[col_1; col_2; col_3; col_4; col_5; col_6; col_7; col_8; col_9; col_10; col_11; col_12; col_13; col_1; col_2; col_3; col_4; col_5; col_6; col_7; col_8; col_9; col_10; col_11; col_12; col_13; col_1; col_2; col_3; col_4; col_5; col_6; col_7; col_8; col_9; col_10; col_11; col_12; col_13; col_1; col_2; col_3; col_4; col_5; col_6; col_7; col_8; col_9; col_10; col_11; col_12; col_13;col_1; col_2; col_3; col_4; col_5; col_6; col_7; col_8; col_9; col_10; col_11; col_12; col_13; col_1; col_2; col_3; col_4; col_5; col_6; col_7; col_8; col_9; col_10; col_11; col_12; col_13; col_1; col_2; col_3; col_4; col_5; col_6; col_7; col_8; col_9; col_10; col_11; col_12; col_13; col_1; col_2; col_3; col_4; col_5; col_6; col_7; col_8; col_9; col_10; col_11; col_12; col_13; col_1; col_2; col_3; col_4; col_5; col_6; col_7; col_8; col_9; col_10; col_11; col_12; col_13; col_1; col_2; col_3; col_4; col_5; col_6; col_7; col_8; col_9; col_10; col_11; col_12; col_13;col_1; col_2; col_3; col_4; col_5; col_6; col_7; col_8; col_9; col_10; col_11; col_12; col_13; col_1; col_2; col_3; col_4; col_5; col_6; col_7; col_8; col_9; col_10; col_11; col_12; col_13];
+
+typemk = [typemk typemk typemk];
+lnstyle = [lnstyle lnstyle lnstyle ];
+colos = [colos; colos; colos ];
+return
diff --git a/crpptbx/TCV/gdat_tcv.m b/crpptbx/TCV/gdat_tcv.m
index 97177bd4..c03c33ed 100644
--- a/crpptbx/TCV/gdat_tcv.m
+++ b/crpptbx/TCV/gdat_tcv.m
@@ -1228,7 +1228,7 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
         launchers_grid = [1:10]';
         % power deposition related:
         ec_data.p_abs_plasma.data = pabs_gyro.data * 1e6;
-        ec_data.p_abs_plasma.data(end+1,:) = nansum(ec_data.p_abs_plasma.data,1);
+        ec_data.p_abs_plasma.data(end+1,:) = sum(ec_data.p_abs_plasma.data,1,'omitnan');
         ec_data.p_abs_plasma.label = [strrep(pabs_gyro.comment,'MW','W') ' ; last index is total'];
         ec_data.p_abs_plasma.units = 'W';
         ec_data.p_abs_plasma.x = launchers_grid;
@@ -1237,7 +1237,7 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
         ec_data.p_abs_plasma.dimunits = {launchers_label, 's'};
         %
         ec_data.p_dens.data = pow_dens.data * 1e6;
-        ec_data.p_dens.data(:,end+1,:) = nansum(ec_data.p_dens.data,2);
+        ec_data.p_dens.data(:,end+1,:) = sum(ec_data.p_dens.data,2,'omitnan');
         ec_data.p_dens.label = [strrep(pow_dens.comment,'MW','W') ' ; last index is total'];
         ec_data.p_dens.units = 'W/m^3';
         ec_data.p_dens.x = pow_dens.rgrid';
@@ -1271,7 +1271,7 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
         ec_data.width_pow_dens.dimunits = {'s'};
         % current drive deposition related:
         ec_data.cd_tot.data = icdtot.data * 1e6;
-        ec_data.cd_tot.data(end+1,:) = nansum(ec_data.cd_tot.data,1);
+        ec_data.cd_tot.data(end+1,:) = sum(ec_data.cd_tot.data,1,'omitnan');
         ec_data.cd_tot.label = [strrep(icdtot.comment,'MA','A') ' ; last index is total'];
         ec_data.cd_tot.units = 'A';
         ec_data.cd_tot.x = launchers_grid;
@@ -1280,7 +1280,7 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
         ec_data.cd_tot.dimunits = {launchers_label, 's'};
         %
         ec_data.cd_dens.data = currentdrive_dens.data * 1e6;
-        ec_data.cd_dens.data(:,end+1,:) = nansum(ec_data.cd_dens.data,2);
+        ec_data.cd_dens.data(:,end+1,:) = sum(ec_data.cd_dens.data,2,'omitnan');
         ec_data.cd_dens.label = [strrep(currentdrive_dens.comment,'MA','A') ' ; last index is total'];
         ec_data.cd_dens.units = 'A/m^2';
         ec_data.cd_dens.x = currentdrive_dens.rgrid';
@@ -1468,7 +1468,7 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
     end
     %
     % add all to last index of .data(:,i)
-    gdat_data.data(end+1,:) = nansum(gdat_data.data,1);
+    gdat_data.data(end+1,:) = sum(gdat_data.data,1,'omitnan');
     gdat_data.x = [1:size(gdat_data.data,1)];
     gdat_data.label{end+1}='total';
     gdat_data.dim{1} = gdat_data.x;
-- 
GitLab