function [data] = meginterpolate(cfg, data)

% MEGINTERPOLATE interpolates MEG data towards standard gradiometer locations
% by projecting the individual MEG data towards a coarse source
% reconstructed representation and computing the magnetic field 
% on the standard gradiometer locations. Besides realigning the MEG 
% recordings of different datasets, it can also compute planar gradients
% and "repair" bad channels.
%
% The mathematical operations to repair, realign and compute planar 
% gradients are all "linear" operations. That means that the result of
% applying each of these operations on the raw data, followed by averaging,
% is the same as applying these operations on the average. In general, you
% will want to apply the MEGINTERPOLATE function on averages if you are
% interested in evoked activity (ERFs), and on raw data if you are interested
% in induced activity (frequency analysis). 
%
% Use as
%   [interp] = meginterpolate(cfg, data)
% or
%   [interp] = meginterpolate(cfg)
%
% The input data is stored in a structure that can either be obtained from 
% the PREPROCESSING function, or from the TIMELOCKANALYSIS function.
%
% This function wraps around the individual computations in MEGREPAIR,
% MEGREALIGN and MEGPLANAR. You should specify what to do with
%   cfg.repair         = 'yes' or 'no' 
%   cfg.realign        = 'yes' or 'no' 
%   cfg.planar         = 'yes' or 'no' 
%
% The other configuration options are described in detail in MEGREPAIR,
% MEGREALIGN and MEGPLANAR. 

% Copyright (C) 2003, Robert Oostenveld, F.C. Donders Centre
%
% $Log: meginterpolate.m,v $
% Revision 1.20  2005/01/17 14:40:38  roboos
% small change in documentation
%
% Revision 1.19  2004/04/06 19:58:23  roberto
% moved all functionnality to three new functions: megrepair, megrealign and megplanar
%
% Revision 1.18  2004/04/06 18:28:09  roberto
% only change in spaces or tabs
%
% Revision 1.17  2004/02/24 17:24:25  roberto
% fixed bug that ocurred if the ordering of the channels in the data
% and in the gradiometer definition were inconsistent
%
% Revision 1.16  2004/02/05 10:07:20  roberto
% updated help
%
% Revision 1.15  2004/02/04 14:01:20  roberto
% fixed some problems with handling of average input data
%
% Revision 1.14  2004/01/27 10:36:34  roberto
% improved help and documentation
%
% Revision 1.13  2004/01/27 10:23:52  roberto
% added two fprintf statements to indicate what the function is not doing
%
% Revision 1.12  2004/01/27 08:32:13  roberto
% fixed 2 bugs in keeping non-MEG channels
%
% Revision 1.11  2004/01/26 17:31:41  roberto
% added piece of JM to keep non-MEG channels in the output data
%
% Revision 1.10  2004/01/20 12:29:37  roberto
% fixed typo in the comments which caused cvstrack to get confused
%
% Revision 1.9  2004/01/20 12:24:03  roberto
% fixed bug in distance computation between channels: in the previous
% version (1.8) this was incorrect for option "repair", causing
% incorrect neighbours to be used in the repairing
%
% Revision 1.8  2003/10/31 15:25:05  roberto
% fixed minor bug in collection of output data
% reversed order to repair/realign/planar
%
% Revision 1.7  2003/10/29 14:57:22  roberto
% added plot3d option which gives 2 figures with pre/post topography
%
% Revision 1.6  2003/10/27 17:04:49  roberto
% added datatype for nicer handling of average CTF files (keep trial 1 of 3)
%
% Revision 1.5  2003/10/27 15:57:42  roberto
% fixed typo in the documentation
%
% Revision 1.4  2003/10/17 15:53:26  roberto
% added nargin check for empty data
% added external cfg.vol for headmodel
%
% Revision 1.3  2003/10/14 14:16:57  roberto
% multiple improvements, fixed helmet averaging and general data handling
%
% Revision 1.2  2003/10/09 16:13:42  roberto
% changed helmet averagingi, still incomplete
%
% Revision 1.1  2003/10/08 14:36:04  roberto
% *** empty log message ***
%

% set the default configuration 
if ~isfield(cfg, 'realign'),       cfg.realign = 'no';            end
if ~isfield(cfg, 'repair'),        cfg.repair = 'no';             end
if ~isfield(cfg, 'planar'),        cfg.planar = 'no';             end

if strcmp(cfg.repair, 'yes')
  data = megrepair(cfg, data);
else
  fprintf('not repairing bad channels\n');
end

if strcmp(cfg.realign, 'yes')
  data = megrealign(cfg, data);
else
  fprintf('not realigning to template\n');
end

if strcmp(cfg.planar, 'yes')
  data = megplanar(cfg, data);
else
  fprintf('not computing planar gradient\n');
end
