function [trace,error,varargout] = gdat(shot,data_type,varargin)
%
% list of data_type currently available:
%
% All machines
% '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}!)
%
% specific to TCV (see help loadTCVdata for more information)
% 'MPX'  =  wire chambers with varargout{1} option (requires varargin{5}!)
% 'xx_2 or xx_3' for Liuqe2 or 3: same as above for xx related to equilibrium
%
% JET
% Special case compatible with old gdat.m allows (JET related): gdat(51994,'ppf','efit/xip',1)
%
% INPUT:
%
% shot: shot number
% data_type: type of the required data.( see above)
%
% optional arguments valid for all values of data_type (not passed on to loadMACHINEdata function):
%
% varargin{1}:  0 => no plot (default), 1 => plot 
% varargin{2}:  machine name 'JET', 'TCV' (default)
%
% Additional input arguments for specific traces (passed on to loadMACHINEdata function)
%
% data_type=sxr or ece:
%                  varargin{3}:  channel status 1= unread yet, 0= read
%                                (for traces with many channel, enables to load additional channels,
%                                 like SXR, ECE, etc.)
%                  varargin{4}:  [i1 i2] : if not empty, assumes need many chords from i1 to i2
%                  varargin{5}:  zmag for varargout{1} computation
%
% OUTPUT:
%
% trace: structure containing data and meshes (compatible with tdi main structure)
% trace.data:   data
% trace.t:      time of reference 
% trace.x:      space of reference 
% trace.dim:    cell array of grids, trace.dim{1}, {2}, ...
% trace.dimunits: units of dimensions
%
% error:        error in loading signal (0=> OK, 1=> error)
%
% Additional Output arguments depending on data_type
%
% data_type=sxR:
%                varargout{1}: intersection of the view lines with magnetic axis
% data_type=MPX: (specific to TCV) 
%                varargout{1}: see help loadTCVdata
%
%
% functions needed: loadTCVdata, loadJETdata
%                  
%
% Examples:
%         [zmag,error]=gdat(shot,'zmag',1);  % gets zmag from TCV and plot
%         [zmag,error]=gdat(shot,'zmag',1,'JET'); % idem but from JET
%         [zmag,error]=gdat(shot,'ppf','efit/zmag',1); as above for JET 
%
%

gdatpaths

nargineff=nargin;
if nargineff>=3 & ischar(varargin{1})
  data_type={data_type ; varargin{1}};
  if nargineff>=4; 
    varargin{1}=varargin{2};
  else
    varargin{1}=0;
    nargineff=4;
  end
  varargin{2}='JET';
end  

% SETTING MACHINE
doplot=0;
if (nargineff>=3 & ~isempty(varargin{1})); doplot=varargin{1}; end
machine='TCV';
if (nargineff>=4 & ~isempty(varargin{2})); machine=varargin{2}; end

% load data from specified machine
if nargineff<=4
  eval(['[trace,error,varargout] = load' machine 'data(shot,data_type);']);
else
  eval(['[trace,error,varargout] = load' machine 'data(shot,data_type,varargin{3:end});']);
end

% PLOT DATA (if required)
if doplot==1
  figure;zoom on
  if length(size(trace.data))<=2
    plot(trace.t,trace.data);
    ylabel(data_type)
  else
    plot(trace.t,trace.data(:,:,1));
    ylabel([data_type '(:,:,1)'])
  end
  xlabel('time [s]')
  title([machine ' '  num2str(shot)])
  grid
elseif doplot==-1
  hold on
  if length(size(trace.data))<=2
    plot(trace.t,trace.data,'r');
  else
    plot(trace.t,trace.data(:,:,1),'--');
  end
end
