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,'location','best');
      end
      %legend(hh,leg,'location','best'); 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
