function [trace,error,varargout] = gdat(shot,data_type,varargin)
%
% 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 
% 'rcont' =  R of plama boundary vs time
% 'zcont' =  Z of plama boundary vs time
% 'vol' =  volume of flux surfaces vs rho=sqrt(psi)
% 'qrho' =  q profile on rho mesh
% 'q95' =  q95 vs time
% 'kappa', 'elon' =  edge elongation vs time
% 'delta', 'triang' =  edge averaged triangularity vs time
% 'deltatop', 'triangtop' =  edge upper (top) triangularity vs time
% 'deltabot', 'triangbot' =  edge lower (bottom) triangularity vs time
% 'neint' =  line-integrated electron density [m/m^3]
% 'ne'= ne raw profile on (z,t). ADD error bars in .std
% 'te'= Te raw profile on (z,t). ADD error bars in .std
% 'nerho'= ne profile on (rho=sqrt(psi),time) mesh.Note rho is a 2D array as depends on time. ADD error bars in .std
% 'terho'= Te profile on (rho=sqrt(psi),time) mesh.Note rho is a 2D array as depends on time. ADD error bars in .std
% '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)
% 'xx_2 or xx_3' for Liuqe2 or 3: same as above for xx related to equilibrium
% 'vol' =  volume of flux surfaces
% 'profnerho' =  ne smoothed or fitted , vs (rho,t) (from Thomson fit)
% 'profterho' =  te smoothed or fitted , vs (rho,t) (from Thomson fit)
% 'neft' =  ne fitted from data on rho mesh (from proffit.local_time:neft)
% 'teft' =  te fitted from data on rho mesh (from proffit.local_time:teft)
% 'neftav' =  ne fitted from averaged over time data on rho mesh (from proffit.avg_time:neft)
% 'teftav' =  te fitted from averaged over time data on rho mesh (from proffit.avg_time:teft)
% '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
%
% 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 'AUG', 'JET' , 'TCV' (default: depending on where implemented)
%
% Additional input arguments for specific traces (passed on to loadMACHINEdata function)
%
% data_type=sxr or ece:
%                  varargin{3}:  [i1 i2] : if not empty, assumes need many chords from i1 to i2
%                  varargin{4}:  channel status 1= unread yet, 0= read
%                                (for traces with many channel, enables to load additional channels,
%                                 like SXR, ECE, etc.)
%                  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: loadAUGdata, loadJETdata, loadTCVdata
%                  
%
% 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
a=which('gdat');
if ~isempty(findstr('ipp',a)) | ~isempty(findstr('/u/osauter',a));
  machine='AUG';
  global usemdsplus; % so far from AUG, do not use mdsplus
  usemdsplus=0;
elseif ~isempty(findstr('/home/osauter',a));
  machine='JET';
elseif ~isempty(findstr('/home/sauter',a));
  machine='TCV';
elseif ~isempty(findstr('/u/sauter',a));
  machine='D3D';
end
if ~exist('machine')
 % disp('did not find machine, set it to TCV')
  machine='TCV';
end

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 & length(trace.data)>1 & ~ischar(trace.data)
  figure;zoom on
  if length(size(trace.data))<=2
    plot(trace.t,trace.data);
    ylabel(data_type)
  else
    for idim=1:length(trace.dim)
      if length(trace.t)==length(trace.dim{idim}); idim_t=idim; end
    end
    if idim_t<=2
      plot(trace.t,trace.data(:,:,floor(end/2)));
      ylabel([data_type '(:,:,floor(end/2))'])
    elseif idim_t==3;
      plot(trace.t,reshape(trace.data(:,floor(end/2),:),length(trace.dim{1}),length(trace.t)));
      ylabel([data_type '(:,floor(end/2),:)'])
    end
  end
  xlabel('time [s]')
  title([machine ' '  num2str(shot)])
  grid on
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
