function [fighandle]=gdat_plot(gdat_data,varargin);
%
% choices from doplot in gdat_data.gdat_params.doplot:
%     doplot = 0: no plot
%            = 1: new figure created (default if not given)
%            = -1: add to current figure (with hold all)
%            > 1: create new figure with this number, adding clf
%            <-1: add to figure number abs(doplot) (with hold all)
%

fighandle = NaN;
gdat_plot_params.dummy=NaN;
if mod(nargin-1,2)==0
  for i=1:2:nargin-1
    if ischar(varargin{i})
      % enforce lower case for any character driven input
      gdat_plot_params.(lower(varargin{i})) = varargin{i+1};
    else
      warning(['input argument nb: ' num2str(i) ' is incorrect, expects a character string'])
      error_status=101;
      return
    end
  end
else
  warning('number of input arguments incorrect, cannot make pairs of parameters')
  error_status=102;
  return
end

doplot=1;
if isfield(gdat_plot_params,'doplot') && ~isempty(gdat_plot_params.doplot)
  doplot = gdat_plot_params.doplot;
elseif isfield(gdat_data,'gdat_params') && isfield(gdat_data.gdat_params,'doplot') && ~isempty(gdat_data.gdat_params.doplot)
  if gdat_data.gdat_params.doplot~=0 % assume one does not call gdat_plot not to plot
    doplot = gdat_data.gdat_params.doplot;
  end
end
if doplot==0; return; end

if all(isfield(gdat_data,{'data','t'})) && ~isempty(gdat_data.data) && ~isempty(gdat_data.t)
  fighandle = get(0,'CurrentFigure');
  if doplot == 1
    fighandle = figure;
  elseif doplot > 1
    fighandle = figure(doplot);
    clf;
  elseif doplot == -1
    hold all
  elseif doplot < -1
    fighandle = figure(abs(doplot));
    hold all
  end
  if isfield(gdat_data,'gdat_request') && ischar(gdat_data.gdat_request) ...
        && strcmp(gdat_data.gdat_request,'eqdsk')
    % special case, plot contours of first equil
    endstr = '';
    if iscell(gdat_data.eqdsk); endstr = '{1}'; end
    eval(['contour(gdat_data.eqdsk' endstr '.rmesh,gdat_data.eqdsk' endstr '.zmesh,gdat_data.eqdsk' endstr '.psi'',100);'])
    hold on
    eval(['plot(gdat_data.eqdsk' endstr '.rplas,gdat_data.eqdsk' endstr '.zplas,''k'');'])
    eval(['plot(gdat_data.eqdsk' endstr '.rlim,gdat_data.eqdsk' endstr '.zlim,''k'');'])
    axis equal;
    title(eval(['gdat_data.eqdsk' endstr '.stitle']));
  elseif any(find(size(gdat_data.data)==length(gdat_data.t)))
    try
      if length(size(gdat_data.data))<=2
        plot(gdat_data.t,gdat_data.data);
      else
        disp('WARNING')
        disp(['size(gdat_data.data) = ' num2str(size(gdat_data.data)) ', >2D thus do not try to plot'])
        disp(' ')
        return
      end
    catch ME
      if exist('ME','var')
        disp('Problem in gdat_plot')
        getReport(ME)
      end
      return
    end
    if ~isfield(gdat_data,'shot'); return; end % allows to plot if just .t and .data exist
    title([gdat_data.gdat_params.machine ' #' num2str(gdat_data.shot)]);
    if isfield(gdat_data,'mapping_for')
      dimunits_x = gdat_data.dimunits{gdat_data.mapping_for.(gdat_data.gdat_params.machine).gdat_timedim};
      if ischar(dimunits_x)
        xlabel(['time [' dimunits_x ']']);
      else
        xlabel(['time']);
      end
    else
      xlabel(['time']);
    end
    ylabel_eff = gdat_data.label;
    if iscell(gdat_data.label) && length(gdat_data.label)>=2; ylabel_eff = gdat_data.label{end}; end
    if ~isempty(gdat_data.units)
      hylabel=ylabel([ylabel_eff '[' gdat_data.units ']'],'interpreter','none');
    else
      hylabel=ylabel(ylabel_eff,'interpreter','none');
    end
    if iscell(gdat_data.label) && length(gdat_data.label)>=2;
      legend(gdat_data.label,'interpreter','none');
    end
    zoom on;
  end
  if strcmp(gdat_data.gdat_request,'mhd')
    % special case, add legend instead of ylabel
    delete(hylabel);
    legend(gdat_data.label);
    % add spectrogram per signal
    mhd_sum_data = 0.;
    if isfield(gdat_data.gdat_params,'nfft') && ~isempty(gdat_data.gdat_params.nfft)
      nfft = gdat_data.gdat_params.nfft;
    else
      nfft=512;
    end
    tmhdm=mean(reshape(gdat_data.t(1:nfft*fix(length(gdat_data.t)/nfft)),nfft,fix(length(gdat_data.t)/nfft)));
    for i=1:size(gdat_data.data,2)
      [B,F,T]=specgram(gdat_data.data(:,i),nfft,1/mean(diff(gdat_data.t)),hanning(nfft),nfft/2);
      figure;
      imagesc(T+tmhdm(1),F/1e3,20*log10(abs(B)));axis xy;colormap jet;
      ylabel('freq [kHz]')
      xlabel(gdat_data.dimunits{1})
      ylabel_eff = gdat_data.label;
      if iscell(gdat_data.label) && length(gdat_data.label)>=i; ylabel_eff = gdat_data.label{i}; end
      title([upper(gdat_data.gdat_params.machine) '#' num2str(gdat_data.shot) ' ' ylabel_eff])
      mhd_sum_data = mhd_sum_data + gdat_data.data(:,i);
    end
    [B,F,T]=specgram(mhd_sum_data./size(gdat_data.data,2),nfft,1/mean(diff(gdat_data.t)),hanning(nfft),nfft/2);
    figure;
    imagesc(T+tmhdm(1),F/1e3,20*log10(abs(B)));axis xy;colormap jet;
    ylabel('freq [kHz]')
    xlabel(gdat_data.dimunits{1})
    ylabel_eff = gdat_data.label;
    if iscell(gdat_data.label); ylabel_eff = sprintf('%s ',gdat_data.label{:}); end
    title([upper(gdat_data.gdat_params.machine) '#' num2str(gdat_data.shot) ' sum of ' ylabel_eff])
  end
else
  disp('cannot plot gdat_data, has empty data or t field')
end
