function [adata,error]=rdaAUG_eff(shot,diagname,sigtype,shotfile_exp,varargin);
%
% gets data using sf2sig or mdsplus (when mdsplus will be available)
% 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
% varargin{2}: extra args for sf2sig like '-raw'
%
% examples:
%          [data,error]=rdaAUG_eff(15133,'MAG','Ipi');
%          [data,error]=rdaAUG_eff(15133,'MAG','Ipi',[1 5]);
%
% set global variable: usemdsplus to decide if sf2sig or mdsplus is used:
%    >> global usemdsplus
%    >> usemdsplus=1 % means use mds to get data
%    >> usemdsplus=0 % means use sf2sig  (default if not defined)
%       if ~exist('usemdsplus'); usemdsplus=0; end
%

global usemdsplus
if ~exist('usemdsplus') || isempty(usemdsplus); usemdsplus=0; end
error=1;

time_int=[];
if nargin>=5 & ~isempty(varargin{1})
  time_int=varargin{1};
end
extra_arg_sf2sig=[];
if nargin>=6 & ~isempty(varargin{2})
  extra_arg_sf2sig=varargin{2};
end

if usemdsplus

  % use mdsplus
  
  if ~unix('test -d /home/duval/mdsplus')
    addpath('/home/duval/mdsplus')
  end
  
  if ~unix('test -d /home/osauter/gdat')
    mdsconnect('localhost:8001');
  else
    mdsconnect('localhost');
  end

  user=getenv('USER');
  if nargin>=5 & ~isempty(varargin{1})
    ['[data,error]=mdsvalue(''_rdaeff' user diagname '=augsignal(' num2str(shot) ',"' diagname '","' sigtype '",,,' ...
          num2str(varargin{1}(1),'%.14f') ',' num2str(varargin{1}(end),'%.14f') ')'');']
    eval(['[data,error]=mdsvalue(''_rdaeff' user diagname '=augsignal(' num2str(shot) ',"' diagname '","' sigtype '",,,' ...
          num2str(varargin{1}(1),'%.14f') ',' num2str(varargin{1}(end),'%.14f') ')'');']);
  else
    eval(['[data,error]=mdsvalue(''_rdaeff' user diagname '=augsignal(' num2str(shot) ',"' diagname '","' sigtype '")'');']);
  end
  adata.data=data;
  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 diagname ',0)'');']);
    x=[];

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

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

  otherwise
    disp([' more than 3 dimensions for ' num2str(shot) ' ; ' sigtype '/' diagname])
    error('in rdaAUG_eff')
    
  end
  adata.t=time;
  adata.x=x;
  adata.hsig=hsig;
  mdsdisconnect;
  if ~unix('test -d /home/duval/mdsplus')
    rmpath('/home/duval/mdsplus')
  end

else
  % use sf2sig
  if isempty(time_int)
    try
      if ~isempty(extra_arg_sf2sig)
	[adata,adata_time, adata_area]=sf2sig(diagname,shot,sigtype,'-exp',shotfile_exp,extra_arg_sf2sig);
      else
	[adata,adata_time, adata_area]=sf2sig(diagname,shot,sigtype,'-exp',shotfile_exp);
      end
    catch
      adata.value = [];
      adata.data = [];
      adata.dim = [];
      adata.t = [];
      adata_time.data = [];
      adata_area = [];
    end
  else
    try
      [adata,adata_time, adata_area]=sf2sig(diagname,shot,sigtype,[time_int(1);time_int(end)],'-exp',shotfile_exp);
    catch
      adata.value = [];
      adata.data = [];
      adata.dim = [];
      adata.t = [];
      adata_time.data = [];
      adata_area = [];
    end
  end

  if isempty(adata.value)
    return
  end

  % special checks
  if strcmp(upper(diagname),'SXB')
    % time missing one point
    if length(adata.value) == length(adata_time.value)+1
      adata_time.value=linspace(adata_time.range(1),adata_time.range(2),length(adata.value));
      adata_time.index(2) = length(adata.value);
    end
  end
  %
  if strcmp(upper(sigtype),'PNIQ')
    % transform 4x2 PINIs in 1:8 PINIs and total in index=9
    if (prod(size(adata.value))/length(adata_time.value) == 8)
      tmp(:,1:4) = adata.value(:,:,1);
      tmp(:,5:8) = adata.value(:,:,2);
      tmp(:,9) = sum(tmp,2);
      adata.value = tmp'; % transpose since will be transposed afterwards
      adata.dimunits = {'s','8 sources;total'};
    else
      disp('expects 8 sources in PNIQ');
      return
    end
  end

  adata.time_aug = adata_time;

  adata.area = adata_area;
 
  adata.exp = shotfile_exp;
  if (prod(size(adata.value))==length(adata.value))
    % only time signal
    adata.x = [];
    adata.value=reshape(adata.value,1,length(adata.value));
    if ~isempty(adata.time_aug)
      adata.t=adata.time_aug.value;
    else
      adata.t=[1:size(adata.value,2)];
    end
  else
    if length(size(adata.value))<=2; adata.value = adata.value'; end % cannot transpose Nd>2 matrix
    if ~isempty(adata.time_aug)
      if length(size(adata.value))<=2;
	adata.x=[1:prod(size(adata.value))/length(adata_time.value)];
      else
	adata.x = [];
      end
      adata.t=adata.time_aug.value;
    else
      adata.x=[1:size(adata.value,1)];
      adata.t=[1:size(adata.value,2)];
    end
  end
  adata.data=adata.value;
  adata.units = adata.unit;
  %  % 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

error=0;
