function [trace,error,varargout]=loadJETdata(shot,data_type,varargin)
%
%
% INPUT:
% shot: shot number
% data_type: type of the required data. Ex.: 'Ip', 'zmag', 'rmag','sxr', 'ece', 'sxR' 
% optional:
% varargin{1}:  [i1 i2] : if not empty, assumes need many chords from i1 to i2 
% varargin{2}:  channel staus 1= unread yet, 0= read
% 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 magnetics axis
%
% function needed: mds functions-xtomo_geometry-get_xtomo (Furno's routines)
%                  VsxrTCVradius-jetreaddata
% 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{5}!)
% Example:
%         [zmag,error]=loadJETdata(shot,'zmag');
%
JETsigkeywrd=[{'Ip'} ; {'zmag'} ; {'rmag'}];
JETsig.iip=1;
JETsig.izmag=2;
JETsig.irmag=3;
JETsiglocation=[{'ppf'; 'efit/xip'} {'ppf'; 'efit/zmag'} {'ppf'; 'efit/rmag'}];
% setting location of the required data
index_sig=strmatch(data_type,JETsigkeywrd);
if ~isempty(index_sig);
  location=JETsiglocation(:,index_sig);
elseif size(data_type,1)==2
  location=data_type;
end


%  LOAD MULTI CHANNEL DATA    
%  load JET soft x-ray data 
if (strcmp(data_type,'sxr') | strcmp(data_type,'sxR'))
% parameters needed for correct convertion of JET Sxr data
  vconvert=   [1.379 1.311 1.249 1.191 1.139 1.093 1.049 ...
	       1.011 0.975 0.945 0.917 0.893 0.873 0.856 ...
	       0.842 0.829 0.821 0.815 0.821 0.829 0.842 ...
	       0.856 0.873 0.894 0.918 0.946 0.976 1.012 ...
	       1.050 1.094 1.141 1.193 1.251 1.313 1.382];
		 
  starti=varargin{1}(1);
  endi=varargin{1}(2);
  status=varargin{2};
  for i=starti:endi
% Read channels from lowchannel to upchannel if necessary
    if status(i)==1
    % Status=1 => Not Read Yet
    % vertical SXR chords
      ppftype='jpf';
      tracename=['db/j3-sxr<v' num2str(i) '''''/1'];
      [a,t,x,d,e]=jetreaddata(['http://data.jet.uk/' ppftype '/' num2str(shot) '/' tracename]);
      trace.data(:,i)=a;
      trace.t(:,i)=t;
      trace.x(:,i)=x; 
      error=e;
      if error~=0
        [aaa bbb]=jetgetlasterrordetail;
        disp(bbb)
      end
% Convert from raw sxr data to W/m^2
      trace.data(i,:) = trace.data(i,:) * vconvert(i);
% calculating intersection of the view lines with magnetics axis 
      if strcmp(data_type,'sxR')
        zmag=varargin{3};
        radius(:,i)=2.848 + (2.172-zmag.data) .* ...
        tan(-4.5/180.*3.14159 - atan2(0.99.*(i-18),35.31));
        varargout={radius}; 
      end   
    end  
  end
elseif strcmp(data_type,'ece')  
  starti=varargin{1}(1);
  endi=varargin{1}(2);
  status=varargin{2};
% Read channels from lowchannel to upchannel if necessary     
  for i=starti:endi
    if status(i)==1
      if i<10
% ECE, te0
% Status=1 => Not Read Yet
        ppftype='ppf';
        tracename=['kk3/te0'num2str(i)];
        [a,t,x,d,e]=jetreaddata(['http://data.jet.uk/' ppftype '/' num2str(shot) '/' tracename]);
        trace.data(:,i)=a;
        trace.t(:,i)=t;
        trace.x(:,i)=x; 
        error=e;
        if error~=0
          [aaa bbb]=jetgetlasterrordetail;
          disp(bbb)
        end
        ppftype='ppf';
        tracename=['kk3/rc0'num2str(i)];
        [a,t,x,d,e]=jetreaddata(['http://data.jet.uk/' ppftype '/' num2str(shot) '/' tracename]);
        radius.data(:,i)=a;
        radius.t(:,i)=t;
        radius.x(:,i)=x; 
        error=e;	
      else
        ppftype='ppf';
        tracename=['kk3/te'num2str(i)];
        [a,t,x,d,e]=jetreaddata(['http://data.jet.uk/' ppftype '/' num2str(shot) '/' tracename]);
        trace.data(:,i)=a;
        trace.t(:,i)=t;
        trace.x(:,i)=x; 
        error=e;
        if error~=0
          [aaa bbb]=jetgetlasterrordetail;
          disp(bbb)
        end
        ppftype='ppf';
        tracename=['kk3/rc'num2str(i)];
        [a,t,x,d,e]=jetreaddata(['http://data.jet.uk/' ppftype '/' num2str(shot) '/' tracename]);
        radius.data(:,i)=a;
        radius.t(:,i)=t;
        radius.x(:,i)=x;
      end
    end
  end 
  varargout={radius}; 
else
%  load other JET data 
  varargout={[]}; 
  ppftype=location{1};
  tracename=location{2};
  [a,t,x,d,e]=jetreaddata(['http://data.jet.uk/' ppftype '/' num2str(shot) '/' tracename]);
  trace.data=a;
  trace.t=t;
  trace.x=x;
  clear error
  error=e;
  if error~=0
    [aaa bbb]=jetgetlasterrordetail;
    disp(bbb)
  end
end