%-Working directory
wd = '/home/gflandin/data/auditory-bids';

%-Create file structure hierarchy
spm_mkdir(wd,'MoAEpilot-bids','sub-01',{'anat','func'});

%-README
rdm = {
' ___  ____  __  __'
'/ __)(  _ \(  \/  )  Statistical Parametric Mapping'
'\__ \ )___/ )    (   Wellcome Centre for Human Neuroimaging'
'(___/(__)  (_/\/\_)  https://www.fil.ion.ucl.ac.uk/spm/'

''
'               MoAEpilot example epoch (block) fMRI dataset'
'________________________________________________________________________'
''
'This experiment was conducted by Geraint Rees under the direction of'
'Karl Friston and the FIL methods group. The purpose was to explore new'
'equipment and techniques.  As such it has not been formally written up,'
'and is freely available for personal education and evaluation purposes.'
''
'These whole brain BOLD/EPI images were acquired on a modified 2T'
'Siemens MAGNETOM Vision system.  Each acquisition consisted of 64'
'contiguous slices (64x64x64 3mm x 3mm x 3mm voxels).  Acquisition took'
'6.05s, with the scan to scan repeat time (RT) set arbitrarily to 7s.'
''
'96 acquisitions were made (RT=7s), in blocks of 6, giving 16 42s'
'blocks. The condition for successive blocks alternated between rest and'
'auditory stimulation, starting with rest. Auditory stimulation was'
'bi-syllabic words presented binaurally at a rate of 60 per minute. Due'
'to T1 effects it is advisable to discard the first few scans (there were'
'no "dummy" lead-in scans).'
''
'A structural image was also acquired.'};
fid = fopen(fullfile(wd,'MoAEpilot-bids','README'),'wt');
for i=1:numel(rdm), fprintf(fid,'%s\n',rdm{i}); end
fclose(fid);

%-CHANGES
cg = {...
    '1.0.1 2019-03-20', ' - BIDS version.',...
    '1.0.0 1999-05-13', ' - Initial release.'};
fid = fopen(fullfile(wd,'MoAEpilot-bids','CHANGES'),'wt');
for i=1:numel(cg), fprintf(fid,'%s\n',cg{i}); end
fclose(fid);

%-Dataset description
descr = struct(...
    'BIDSVersion','1.2.0',...
    'Name','Mother of All Experiments',...
    'Authors',{{'Geraint Rees','Karl Friston'}},...
    'ReferencesAndLinks', {{'https://www.fil.ion.ucl.ac.uk/spm/data/auditory/'}});
spm_save(fullfile(wd,'MoAEpilot-bids','dataset_description.json'),descr,'indent','  ');

%-Structural MRI
VA = spm_vol(fullfile(wd,'MoAEpilot','sM00223','sM00223_002.img'));
A  = spm_read_vols(VA);
VA.fname = fullfile(wd,'MoAEpilot-bids','sub-01','anat','sub-01_T1w.nii');
spm_write_vol(VA,A);

%-Functional MRI
VF = spm_select('FPList',fullfile(wd,'MoAEpilot','fM00223'),'^fM.*\.img$');
VF(1:12,:) = []; % remove first 12 scans as dummy
spm_file_merge(VF,fullfile(wd,'MoAEpilot-bids','sub-01','func','sub-01_task-auditory_bold.nii'),0,7);

%-fMRI task information
task = struct(...
    'RepetitionTime', 7, ...
    'NumberOfVolumesDiscardedByScanner', 0,...
    'NumberOfVolumesDiscardedByUser', 12,...
    'TaskName', 'Auditory',...
    'TaskDescription', 'The condition for successive blocks alternated between rest and auditory stimulation, starting with rest. Auditory stimulation was with bi-syllabic words presented binaurally at a rate of 60 per minute.',...
    'Manufacturer', 'Siemens',...
    'ManufacturersModelName', 'MAGNETOM Vision',...
    'MagneticFieldStrength', 2);
spm_save(fullfile(wd,'MoAEpilot-bids','task-auditory_bold.json'),task,'indent','  ');

%-Task events
events = struct(...
    'onset',(6:12:84)*7,...
    'duration',repmat(6*7,1,7),...
    'trial_type',{repmat({'listening'},1,7)});
spm_save(fullfile(wd,'MoAEpilot-bids','sub-01','func','sub-01_task-auditory_events.tsv'),events);

%touch -d '1996-03-22 18:53:13.000000000 +0000' MoAEpilot-bids/
