function layout=cosmo_meeg_find_layout(ds, varargin)
% finds an MEEG channel layout associated with a dataset
%
% layout=cosmo_meeg_find_layout(ds, varargin)
%
% Inputs:
% 'chantype', ct string indicating the channel type (if the dataset
% has channel labels that allow for different types of
% channels. Depending on the dataset, possible options
% are:
% - 'meg_planar' pairs of planar MEG
% - 'meg_axial' axial MEG
% - 'meg_planar_combined' combined planar MEG
% - 'meg_combined_from_planar' pairs of planar MEG [*]
% - 'eeg' eeg channels
%
% Output:
% layout MEG channel layout with fields
% .pos Nx2 x and y coordinates (for N channels)
% .width Nx1 channel widths
% .height Nx1 channel heights
% .label Nx1 cell with channel labels
% .name string with name of layout
% [*] when chantype is set to
% 'meg_combined_from_planar', layout also contains a
% field .parent which is a layout in itself for the
% 'meg_planar_combined channels'. In that case,
% layout.parent has the .pos, .width, .height, .label
% and .name fields (all of size Mx1 or Mx2 for M
% planar-combined channels, and in addition a cell
% .child_label (of size Mx1) which contains the
% channel labels in layout.name.
%
% Examples:
% % (This example requires FieldTrip)
% cosmo_skip_test_if_no_external('fieldtrip');
% %
% % generate neuromag306 dataset
% ds=cosmo_synthetic_dataset('type','meeg','sens','neuromag306_all');
% % get layout for the planar channels
% pl_layout=cosmo_meeg_find_layout(ds,'chantype','meg_planar');
% cosmo_disp(pl_layout.label)
% %|| { 'MEG0113'
% %|| 'MEG0112'
% %|| 'MEG0122'
% %|| :
% %|| 'MEG2643'
% %|| 'COMNT'
% %|| 'SCALE' }@206x1
% cosmo_disp([pl_layout.pos pl_layout.width pl_layout.height])
% %|| [ -0.408 0.253 0.0358 0.0369
% %|| -0.408 0.284 0.0358 0.0369
% %|| -0.328 0.285 0.0358 0.0369
% %|| : : : :
% %|| 0.373 -0.0821 0.0358 0.0369
% %|| -0.547 -0.5 0.0358 0.0369
% %|| 0.547 -0.5 0.0358 0.0369 ]@206x4
% pl_layout.name
% %|| 'neuromag306planar.lay'
% %
% % get layout for axial (magnetometer) channels
% mag_layout=cosmo_meeg_find_layout(ds,'chantype','meg_axial');
% cosmo_disp(mag_layout.label);
% %|| { 'MEG0111'
% %|| 'MEG0121'
% %|| 'MEG0131'
% %|| :
% %|| 'MEG2641'
% %|| 'COMNT'
% %|| 'SCALE' }@104x1
% %
% % get layout for planar channels, but add a 'parent' layout which has the
% % combined_planar channels
% combined_from_planar_layout=cosmo_meeg_find_layout(ds,'chantype',...
% 'meg_combined_from_planar');
% cosmo_disp(combined_from_planar_layout.label);
% %|| { 'MEG0113'
% %|| 'MEG0112'
% %|| 'MEG0122'
% %|| :
% %|| 'MEG2643'
% %|| 'COMNT'
% %|| 'SCALE' }@206x1
% cosmo_disp(combined_from_planar_layout.parent.label);
% %|| { 'MEG0112+0113'
% %|| 'MEG0122+0123'
% %|| 'MEG0132+0133'
% %|| :
% %|| 'MEG2642+2643'
% %|| 'COMNT'
% %|| 'SCALE' }@104x1
% cosmo_disp(combined_from_planar_layout.parent.child_label);
% %|| { { 'MEG0112'
% %|| 'MEG0113' }
% %|| { 'MEG0122'
% %|| 'MEG0123' }
% %|| { 'MEG0132'
% %|| 'MEG0133' }
% %|| :
% %|| { 'MEG2642'
% %|| 'MEG2643' }
% %|| { }
% %|| { } }@104x1
%
% See also: ft_prepare_neighbors, cosmo_meeg_chan_neighbors,
% cosmo_meeg_chan_neighborhood
%
% # For CoSMoMVPA's copyright information and license terms, #
% # see the COPYING file distributed with CoSMoMVPA. #