function output = fiff_make_multichannel(inputtable, outfname)
% fiff_make_multichannel(inputtable, outfname)
%
% Combine signals from multiple files to form a synthetic multichannel recording
%
% 2016-02-01 LP: Modified to use textscan instead of sscanf for parsing the
% config file.
%
% (C)opyright Lauri Parkkonen, 2010, 2016
%
% $Log$
%

if nargin < 1   
    error('Not enough arguments');
end

if ~exist(inputtable, 'file')
    error('Input table not found');
end

FIFF = fiff_define_constants();

output = [];
oresp = [];
ochs = [];
onames = [];
nchan = 0;

intable = fopen(inputtable, 'r');
oneline = fgetl(intable);
while (oneline > -1)
    if isempty(oneline)
        oneline = fgetl(intable);
        continue;
    end
    if oneline(1) == '#'
        oneline = fgetl(intable);
        continue;
    end
    words = textscan(oneline, '%s %s %f %f %f %s %f %d');
    if (length(words) ~= 8)
        fclose(intable);
        error('Syntax error in %s on line "%s": too few or incorrect fields\n', inputtable, oneline);
    end
    ifname = char(words{1});
    ichannel = char(words{2});
    ox = words{3};
    oy = words{4};
    oz = words{5};
    ochannel = char(words{6});
    ocal = words{7};
    ocoil = words{8};
    idata = fiff_read_evoked(ifname,1);
    if isempty(output)
        output.info = idata.info;
        output.evoked.aspect_kind = idata.evoked.aspect_kind;
        output.evoked.comment = idata.evoked.comment;
        output.evoked.first = idata.evoked.first;
        output.evoked.last = idata.evoked.last;
        output.evoked.is_smsh = idata.evoked.is_smsh;
        output.evoked.nave = idata.evoked.nave;
        output.evoked.times = idata.evoked.times;
    end
    for c = 1:length(idata.info.chs)
        if strcmpi(idata.info.chs(c).ch_name, ichannel)
            nchan = nchan + 1;
            fprintf('%s:%s (index %d) --> OUT:%s (index %d)\n', ifname, ichannel, c, ochannel, nchan);
            % Extract the response
            oresp(nchan,:) = idata.evoked.epochs(c,:) * ocal;
            % Construct the channel info structure
            ochs(nchan).scanno = nchan;
            if strcmpi(ochannel(1:3), 'MEG') 
                ochs(nchan).logno = 1000 + nchan;
                ochs(nchan).kind = FIFF.FIFFV_MEG_CH;
            elseif strcmpi(ochannel(1:3), 'MCG')
                ochs(nchan).logno = 200 + nchan;
                ochs(nchan).kind = FIFF.FIFFV_MCG_CH;
            elseif strcmpi(ochannel(1), 'E')
                ochs(nchan).logno = nchan;
                ochs(nchan).kind = FIFF.FIFFV_EEG_CH;
            end
            ochs(nchan).range = idata.info.chs(c).range;
            ochs(nchan).cal = idata.info.chs(c).cal;
            ochs(nchan).coil_type = ocoil;
            ochs(nchan).ch_name = ochannel;
            ochs(nchan).loc = [ ox oy oz 1 0 0 0 1 0 0 0 1 ]; % FIXME: Now assuming a planar system
            ochs(nchan).coil_trans = eye(4);   % FIXME: Now assuming a planar system
            if ochs(nchan).kind == FIFF.FIFFV_EEG_CH
                ochs(nchan).eeg_loc = [ ox oy oz ];
            else
                ochs(nchan).eeg_loc = [];
            end
            ochs(nchan).coord_frame = FIFF.FIFFV_COORD_DEVICE;
            switch ocoil
                case {4, 1000, 2000, 2001, 3021, 3022, 3023, 3024, 4001, 4002, 5001}
                    unit = 112; % Teslas
                case {2, 3, 3011, 3012, 3013, 3014}
                    unit = 201; % Teslas/meter
                case {1, 5}
                    unit = 107; % Volts
                otherwise
                    printf('Warning: Unknown coil type %d\n', ocoil);
                    unit = -1;  % FIFF_UNIT_NONE
            end
            ochs(nchan).unit = unit;
            ochs(nchan).unit_mul = 0;
            onames{nchan} = ochannel;
            break
        end
    end
    oneline = fgetl(intable);
end
fclose(intable);

output.info.nchan = nchan;
output.info.ch_names = onames;
output.info.chs = ochs;
output.evoked.epochs = oresp;

if nargin > 1
  fiff_write_evoked(outfname, output);
end