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)
%                  get_mdsdata (Reimerdes's routine)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');
%

varargout{1}=cell(1,1);

% all keywords and corresponding case to run below
TCVkeywrdall=[{'Ip'} {'zmag'} {'rmag'} {'sxr'} {'sxR'} {'ece'}];
TCVsig.iip=1;
TCVsig.izmag=2;
TCVsig.irmag=3;
TCVsig.isxr=4;
TCVsig.isxR=5;
TCVsig.iece=6;
% For each keyword, specify which case to use. As most common is 'simpletdi', fill in with this and change
% only indices needed. Usually use name of case same as keyword name
TCVkeywrdcase=cell(size(TCVkeywrdall));
TCVkeywrdcase(:)={'simpletdi'};
TCVkeywrdcase(TCVsig.isxr)=TCVkeywrdall(TCVsig.isxr);
TCVkeywrdcase(TCVsig.isxR)=TCVkeywrdall(TCVsig.isxR);
TCVkeywrdcase(TCVsig.iece)=TCVkeywrdall(TCVsig.iece);

% For the 'simpletdi' cases, we need the full node in case gdat was called with full location directly
% for the other cases, leave this location empty
TCVsiglocation=cell(size(TCVkeywrdall));
TCVsiglocation(:)={''};
TCVsiglocation(TCVsig.iip)={'\results::i_p'};
TCVsiglocation(TCVsig.izmag)={'\results::z_axis'};
TCVsiglocation(TCVsig.irmag)={'\results::r_axis'};

% Information about which dimension has time, always return 2D data as (x,t) array
% as most are 1D arrays with time as first index, fill in with ones and change only those needed
TCVsigtimeindx=ones(size(TCVkeywrdall));

% find index of signal called upon
if strcmp(data_type(1:1),'\')
  % in case full node name was given
  index=strmatch(data_type,TCVsiglocation,'exact');
  if ~strcmp(TCVkeywrdcase{index},'simpletdi')
    msgbox(['Problem in loadTCVdata with data_type = ' data_type ...
          '. Full paths of nodes should only be for case simpletdi'],'in loadTCVdata','error')
    error('in loadTCVdata')
  end
else
  index=strmatch(data_type,TCVkeywrdall,'exact');
end
disp(['loading' ' ' data_type ' from TCV shot #' num2str(shot)]); 
disp(['case ' TCVkeywrdcase{index}])
status=ones(1,100);
zmag=cell(0,0);
nargineff=nargin;
i1=1;
i2=20;
if nargineff>=3
  for i=1:length(varargin)
    if ~isempty(varargin{i})
      if isstruct(varargin{i})
         zmag=varargin{i};
      elseif size(varargin{i},2)>2
         status=varargin{i};
      else
         i1 =varargin{i}(1);
    	 i2 =varargin{i}(2);
      end
    end 
  end
end

switch TCVkeywrdcase{index}

      
     
  case 'simpletdi'

    %  load TCV other data 
    mdsopen(shot);
    tracetdi=tdi(TCVsiglocation{index});
    mdsclose(shot)
    trace.data=tracetdi.data;
    trace.t=tracetdi.dim{TCVsigtimeindx(index)};
    if (length(tracetdi.dim)>1)
      ix=2;
      if TCVsigtimeindx(index)==2; ix=1; end;
      trace.x=tracetdi.dim{ix};
      % make sure data is of (x,t)
      if ix==2
        % transpose data
        if length(size(trace.data))==2
          trace.data=trace.data';
        else
          msgbox(['data more than 2D (data_type=' data_type ...
                '), check how to save it, contact andrea.scarabosio@epfl.ch or olivier.sauter@epfl.ch'],...
              'in simpletdi','warning')
          warning('in simpletdi of loadTCVdata')
        end
      end
    else
      trace.x=[]; 
    end 
    error=0;
   
  case {'sxr','sxR'}
    %  load TCV soft x-ray data  
    if isempty(zmag)
      zmag=loadTCVdata(shot,'zmag');
    end

    % 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
    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') 
        varargout{1}={VsxrTCVradius(zmag.data,xchord,ychord)};
        t_1=zmag.t(1);
        t_2=zmag.t(end);
      else
        t_1=0.001;
        t_2=3;
      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;

  case 'ece'
    %  load TCV ECE data
    if isempty(zmag)
      zmag=loadTCVdata(shot,'zmag');
    end
    % 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{1}={radius};
    error=0;
 
  case 'MPX'
    %  load TCV MPX data
    % Status=1 => Not Read Yet 
    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{1}={VsxrTCVradius(zmag.data,xchord,ychord)};
    error=0;  
    
  otherwise
    % eval(['!mailto_Andrea ''from loadTCVdata, data_type= ' data_type ''''])
    disp(['this data_type' ' ' data_type ' ' 'not yet programmed in loadTCVdata, ask Andrea.Scarabosio@epfl.ch']);

end