
% the Artinis oxy3 files come with an optodetemplates.xml
% this has to be in the same directory as the oxy3 file and it cannot be a subdirectory

cd('original')

%%

% this data is from http://www.fieldtriptoolbox.org/tutorial/nirs_multichannel/

filelist = {
  'LR-01-2015-06-01-0002.oxy3'
  'LR-02-2015-06-08-0001.oxy3'
  'LR-03-2015-06-15-0001.oxy3'
  'LR-04-2015-06-17-0001.oxy3'
  'LR-05-2015-06-23-0001.oxy3'
  };


%%

for i=1:length(filelist)
  filename = filelist{i};
  
  cfg = [];
  cfg.dataset = filename;
  cfg.method = 'convert';
  cfg.writejson = 'replace';
  cfg.writetsv = 'replace';
  
  % the following settings relate to the directory structure and file names
  cfg.bidsroot = '../bids'; % we are running this code in the "original" directory
  cfg.sub = filename([1 2 4 5]); % the first characters minus the "-"
  cfg.ses = [];
  cfg.run = [];
  cfg.task = 'auditoryoddball';
  cfg.datatype = 'nirs';
  
  % the following settings relate to the dataset_description.json
  cfg.dataset_description.Name                = '48-channel NIRS measured during auditory oddball task';
  cfg.dataset_description.Authors             = 'Marc van Wanrooij';
  cfg.dataset_description.ReferencesAndLinks  = {'http://www.fieldtriptoolbox.org/tutorial/nirs_multichannel/'}; % this can be a list
  cfg.dataset_description.BIDSVersion         = 'BEP030'; % this does not correspnd to an official version, but a BIDS Extension Proposal. See http://bids.neuroimaging.io/bep030
  
  data2bids(cfg)
end

%%

% now that the data is converted to BIDS, it is also in SNIRF format
% note however, that FieldTrip uses the BIDS json and tsv sidecar files to overrule the header and event details

hdr1   = ft_read_header('../bids/sub-LR01/nirs/sub-LR01_task-auditoryoddball_nirs.snirf', 'readbids', true)
event1 = ft_read_event('../bids/sub-LR01/nirs/sub-LR01_task-auditoryoddball_nirs.snirf', 'readbids', true)

% by specifying readbids=false, you can skip the BIDS json and tsv
% in that case the channel names correspond to the snirf defaults, with Sx-Dx instead of Artinis Rx-Tx

hdr2   = ft_read_header('../bids/sub-LR01/nirs/sub-LR01_task-auditoryoddball_nirs.snirf', 'readbids', false)
event2 = ft_read_event('../bids/sub-LR01/nirs/sub-LR01_task-auditoryoddball_nirs.snirf', 'readbids', false)

