function [data, dimord] = data2raw(data);

% DATA2RAW is a helper function that converts various types of averages to
% raw data. This function is used to apply the analysis steps that were
% written for use on preprocessed data also on averaged data.
%
% This function is used in FREQANALYSIS, MEGREALIGN, MEGPLANAR, MEGREPAIR

% Copyright (C) 2005, Robert Oostenveld
%
% $Log: data2raw.m,v $
% Revision 1.3  2005/06/03 07:02:48  roboos
% fixed error for empty dimord in case of raw input data (it should return immediately)
%
% Revision 1.2  2005/06/02 16:00:33  roboos
% removed average from the output structure
%
% Revision 1.1  2005/06/02 12:14:10  roboos
% new implementation for consistent conversion of averaged data (with either keepsubject or keepindividual) to raw trials as they come out of preprocessing
% these two helper functions are from now on used in freqanalysis, megplanar, megrealign, megrepair and in combineplanar
%

% determine the type of input data
if ~isfield(data, 'avg') && isfield(data, 'trial') && iscell(data.trial)
  dimord = [];                  % output of PREPROCESSING
elseif isfield(data, 'avg') && ~isfield(data, 'trial') && ~isfield(data, 'individual')
  dimord = 'chan_time';         % output of TIMELOCKANALYSIS with keeptrials=no or TIMELOCKGRANDAVERAGE with keepindividual=no
elseif isfield(data, 'avg') &&  isfield(data, 'trial') && ~isfield(data, 'individual')
  dimord = 'trial_chan_time';   % output of TIMELOCKANALYSIS with keeptrials=yes
elseif isfield(data, 'avg') && ~isfield(data, 'trial') &&  isfield(data, 'individual')
  dimord = 'subj_chan_time';    % output of TIMELOCKGRANDAVERAGE with keepindividual=yes
else
  warning('unrecognized input data');
  dimord = [];
end

if isempty(dimord)
  % nothing to do
  return
end

% convert the input data into raw output, which resembles the output of PREPROCESSING
switch dimord
  case 'chan_time'
    fprintf('converting average to single trial\n');
    data.trial{1} = data.avg;
    data.time     = {data.time};
    data          = rmfield(data, 'avg');

  case 'trial_chan_time'
    fprintf('converting timelocked trials to raw trials\n');
    tmptrial = {};
    tmptime  = {};
    ntrial = size(data.trial,1);
    nchan  = size(data.trial,2);
    ntime  = size(data.trial,3);
    for i=1:ntrial
      tmptrial{i} = reshape(data.trial(i,:,:), [nchan, ntime]);
      tmptime{i}  = data.time;
    end
    data = rmfield(data, 'trial');
    data.trial = tmptrial;
    data.time  = tmptime;

  case 'subj_chan_time'
    fprintf('converting individual subject averages to raw trials\n');
    tmptrial = {};
    tmptime  = {};
    ntrial = size(data.individual,1);
    nchan  = size(data.individual,2);
    ntime  = size(data.individual,3);
    for i=1:ntrial
      tmptrial{i} = reshape(data.individual(i,:,:), [nchan, ntime]);
      tmptime{i}  = data.time;
    end
    data = rmfield(data, 'individual');
    data.trial = tmptrial;
    data.time  = tmptime;

  otherwise
    % nothing to do

end

% raw data is supposed to contain the sampling frequency
if ~isfield(data, 'fsample')
    % compute the sampling frequency from the first two timepoints
    data.fsample = 1/(data.time{1}(2)-data.time{1}(1));
end

% these fields cannot be used by the external functions that want the data in raw format
if isfield(data, 'avg'),   data = rmfield(data, 'avg');   end
if isfield(data, 'var'),   data = rmfield(data, 'var');   end
if isfield(data, 'cov'),   data = rmfield(data, 'cov');   end
if isfield(data, 'blcov'), data = rmfield(data, 'blcov'); end
