function [data,x,time,hsig,error]=rda_eff(shot,pftype,tracename,varargin);
%
% gets data using RDA or mdsplus
% 1D arrays: assumes dimension is time
% 2D arrays: assumes data vs (x,time)
% 3D arrays: assumes data vs (x,time,hsig) (for mdsplus)
%
% varargin{1}: time interval or timevalue, will get data closest to that time or within that time interval
%              (DOES NOT WORK YET)
%
% examples:
%          [data,x,time,hsig,error]=rda_eff(51994,'ppf','efit/xip');
%          [data,x,time,hsig,error]=rda_eff(52206,'ppf','equi/rmji?uid=jetthg+seq=122');
%
% set global variable: usemdsplus to decide if RDA or mdsplus is used:
%    >> global usemdsplus
%    >> usemdsplus=1 % means use mds to get data (default if not defined)
%    >> usemdsplus=0 % means use jetreaddata routine (RDA)
%       if ~exist('usemdsplus'); usemdsplus=1; end
%

global usemdsplus
if isempty(usemdsplus); usemdsplus=1; end

time_int=[];
if nargin>=4 & ~isempty(varargin{1})
  time_int=varargin{1};
end

if usemdsplus
  % use mdsplus
  
  if ~unix('test -d /home/duval/mdsplus')
    addpath('/home/duval/mdsplus')
  end
  mdsconnect('mdsplus.jet.efda.org');

  % defines trace to fetch
  % after '?' specific details
  separator='+';
  mainseparator='?';
  imaintrace=findstr(mainseparator,tracename);
  if isempty(imaintrace)
    maintrace=tracename;
    uid=[];
    seq=[];
    diag=[];
  else
    maintrace=tracename(1:imaintrace-1);
    rest=tracename(imaintrace+1:end);
    % gets uid if any
    iuid=findstr('uid=',rest);
    if isempty(iuid)
      uid=[];
    else
      ii=findstr(separator,rest(iuid:end));
      if isempty(ii)
        uid=rest(iuid+4:end);
      else
        uid=rest(iuid+4:iuid+ii(1)-2);
      end
    end
    % gets seq if any
    iseq=findstr('seq=',rest);
    if isempty(iseq)
      seq=[];
    else
      ii=findstr(separator,rest(iseq:end));
      if isempty(ii)
        seq=rest(iseq+4:end);
      else
        seq=rest(iseq+4:iseq+ii(1)-2);
      end
    end
    % gets diag if any
    idiag=findstr('diag=',rest);
    if isempty(idiag)
      diag=[];
    else
      ii=findstr(separator,rest(idiag:end));
      if isempty(ii)
        diag=rest(idiag+5:end);
      else
        diag=rest(idiag+5:idiag+ii(1)-2);
      end
    end

  end
  
  % fetch value
  if ~isempty(uid)
    eval(['u=mdsvalue(''_sig=ppfuid("' uid '")'');'])
  end
  traceeff=[pftype '/' maintrace];
  if ~isempty(seq)
    traceeff=[traceeff '/' num2str(seq)];
  end
  user=getenv('USER');
  eval(['[data,error]=mdsvalue(''_rdaeff' user '=jet("' traceeff '",' num2str(shot) ')'');'])
  hsig=[];
  ss=size(data);
  nbofdim=length(ss);
  if ss(end)==1; nbofdim=nbofdim-1; end
  nbofdim=max(nbofdim,1);
  switch nbofdim
  case 1
    eval(['time=mdsvalue(''dim_of(_rdaeff' user ',0)'');']);
    x=[];

  case 2
    eval(['x=mdsvalue(''dim_of(_rdaeff' user ',0)'');']);
    eval(['time=mdsvalue(''dim_of(_rdaeff' user ',1)'');']);

  case 3
    eval(['x=mdsvalue(''dim_of(_rdaeff' user ',0)'');']);
    eval(['time=mdsvalue(''dim_of(_rdaeff' user ',1)'');']);
    disp('3rd dimension in hsig!!!!!!!!!!!!!!!!!!!!!!!!!')
    eval(['hsig=mdsvalue(''dim_of(_rdaeff' user ',2)'');']);

  otherwise
    disp([' more than 3 dimensions for ' num2str(shot) ' ; ' pftype '/' tracename])
    error('in rda_eff')
    
  end

  mdsdisconnect;
  if ~unix('test -d /home/duval/mdsplus')
    rmpath('/home/duval/mdsplus')
  end

else
  % use RDA
  [a,time,x,hsig,error]=jetreaddata(['http://data.jet.uk/' pftype '/' num2str(shot) '/' tracename]);
  % transpose data as output in C format, reversed from Fortran and matlab standard
  ss=size(a);
  nbofdim=length(ss);
  if ss(end)==1; nbofdim=nbofdim-1; end
  nbofdim=max(nbofdim,1);
  if nbofdim==1
    data=a;
  else
    data=a';
  end
end
