 function [trace,error,varargout]=loadTCVdata(shot,data_type,varargin)
%
%
% INPUT:
% shot: shot number
% data_type: type of the required data. Ex.: 'Ip', 'zmag', 'rmag','sxr', 'ece', 'sxR' 
%                                            'MPX'   
% optional:
% varargin{1}:  channel status 1= unread yet, 0= read
% varargin{2}:  [i1 i2] : if not empty, assumes need many chords from i1 to i2 
% varargin{3}:  zmag for varargout{1} computation
%
% OUTPUT:
% trace.data:   data structure
% trace.t:      time of reference 
% trace.x:      space of reference 
% varargout{1}: intersection of the view lines with magnetic axis
%
% function needed: mds functions-xtomo_geometry-get_xtomo_data (Furno's routines)
%                  VsxrTCVradius
% list of data_type currently available:
% 'Ip'   =  current
% 'zmag' =  vertical position of the center of the plasma (magnetic axis)
% 'rmag' =  radial position of the center of the plasma 
% 'ece'  =  electron cyclotron emission  
% 'sxr'  =  soft x-ray emission
% 'sxR'  =  soft x-ray emission with varargout{1} option (requires varargin{4}!)
% 'MPX'  =  soft x-ray from wire chambers
% Example:
%         [zmag,error]=loadTCVdata(shot,'zmag');
%

TCVsigkeywrd=[{'Ip'} ; {'zmag'} ; {'rmag'}];
TCVsigtimeindx=['1'   ;    '1'   ;   '1'];
TCVsig.iip=1;
TCVsig.izmag=2;
TCVsig.irmag=3;
TCVsiglocation=[{'\results::i_p'} ; {'\results::z_axis'} ; {'\results::r_axis'}];
 % setting location of the required data
index_sig=strmatch(data_type,TCVsigkeywrd);
if ~isempty(index_sig);
  location={TCVsiglocation{index_sig} TCVsigtimeindx(index_sig)}; 
elseif strcmp(data_type(1:1),'\')
  index_sig=strmatch(data_type,TCVsiglocation);
  if ~isempty(index_sig)
   fprintf(['Note: you can also get this data simply with gdat(shot,' ...
   char(TCVsigkeywrd(index_sig)) ') instead of the full node name' ' ' data_type]);
    location={data_type ; '1'};
  else
%    eval(['!mailto_Andrea ''from loadTCVdata, data_type= ' data_type ''''])
  end  
else
  disp(['this data_type' ' ' data_type ' ' 'not yet programmed in loadTCVdata, ask Andrea.Scarabosio@epfl.ch']);
end
disp(['loading' ' ' data_type ' from TCV shot #' num2str(shot)]); 


% LOAD DATA
if (strcmp(data_type,'sxr') | strcmp(data_type,'sxR'))
%  load TCV soft x-ray data  
 % camera selection: 1-10. each camera has 20 channels
  icamera=[0 1 0 0 0 0 0 0 0 0];  %index of the camera to use
  status=varargin{1};
  if ~isempty(find(status(1:20*icamera*ones(10,1)) == 1))
    [fans,vangle,xchord,ychord,aomega,angfact]=xtomo_geometry(1,icamera);
% calculating intersection of the view lines with magnetic axis 
    if strcmp(data_type,'sxR')
      zmag=varargin{3};
      varargout={VsxrTCVradius(zmag.data,xchord,ychord)};
      t_1=zmag.t(1);
      t_2=zmag.t(end);
    else
      t_1=0.001;
      t_2=3;
      varargout={};
    end
    [xtomo_signal,t]=get_xtomo_data(shot,t_1,t_2,13e-6*16, ...
    icamera,angfact);
  end  
  for i=1:(20*icamera*ones(10,1))
    trace.t(:,i)=t';
  end  
  trace.data=xtomo_signal';          
  error=0;
%  load TCV ECE data
elseif (strcmp(data_type,'ece'))
  status=varargin{2};
% Status=1 => Not Read Yet  
  if ~isempty(find(status == 1))
    [TE_ECE,TE_ECE_ERR,RHO,R,T,TE_THOM,TE_THOM_ERR,Fcentral,CAL]=ece_te ...
    (shot,[0.1 0.29],10,10);
  end  
  a=min(find(R(:,1)>=0));
  b=max(find(R(:,1)>=0));
  for i=1:size(TE_ECE,2)
    trace.t(:,i)=T(a:b);
  end  
  trace.data=TE_ECE(a:b,:);
  radius.t=trace.t; 
  radius.data=R(a:b,:);
  varargout={radius};
  error=0;
%  load TCV MPX data
elseif (strcmp(data_type,'MPX'))
  status=varargin{1};
% Status=1 => Not Read Yet 
  zmag=varargin{3};
  t_1=zmag.t(1);
  t_2=zmag.t(end);
  if ~isempty(find(status == 1))
    mdsopen(shot);
    signal=get_mds_mio('MPX',[t_1 t_2]); 
    mdsclose(shot)
    trace.data=signal.data;
    for i=1:size(signal.dim{2},2)
      trace.t(:,i)=signal.dim{1};
    end  
  end  
  [xchord,ychord]=mpx_geometry;
  varargout={VsxrTCVradius(zmag.data,xchord,ychord)};
  
  
  error=0;  
else 
%  load TCV other data 
  varargout={[]};
  mdsopen(shot);
  tracetdi=tdi(location{1});
  mdsclose(shot)
  trace.data=tracetdi.data;
  time_pos=str2num(location{2});
  trace.t=tracetdi.dim{time_pos};
  if (length(tracetdi.dim)>1)
    ii=2;
    if time_pos==2; ii=1; end;
    trace.x=tracetdi.dim{ii};
  else
    trace.x=[]; 
  end 
  error=0;
end
