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
