#!/bin/bash -x

#
# This script tests the use of the fsaverage brain with MEG
# subjects lacking an individual MRI. In particular, I have
# been unable to change the number of vertices on the resulting
# surface using the --spacing option (mne_setup_source_space,
# mne_do_forward_solution).
#
# The steps here are mainly based on a document created by Matti Hamalainen,
# April 22, 2009:
# https://mail.nmr.mgh.harvard.edu/pipermail//mne_analysis/attachments/20090422/0ed7909e/attachment.pdf
#
# A couple of steps are also taken from the MNE 2.7.3 software manual,
# Figure 3.1, "Workflow of the MNE software"
#
# Per A. Lysne
# University of New Mexico
#

export FREESURFER_HOME=/home/lysne/freesurfer

export MNE_ROOT=/home/lysne/MNE

export SUBJECTS_DIR=/media/lysne/3TB1/FaceEmotion.FS

export SPACING=7


cd $SUBJECTS_DIR

#
# Step 1: make a private copy of fsaverage. I am calling this
# fsaverage2, because I already have an fsaverage set up. If all
# commands are uncommented, this script runs to completion from
# scratch.
#
cp -R $FREESURFER_HOME/subjects/fsaverage $SUBJECTS_DIR/fsaverage2

#
# Step 2: copy additional fsaverage data from MNE. I am copying
# the files individually to make this more explicit. Also note,
# the path '$MNE_ROOT/mne/setup/mne_analyze/fsaverage' given in
# Matti's document does not exist in my MNE tree. I think I have
# found the appropriate files, however.
#
mkdir $SUBJECTS_DIR/fsaverage2/bem
cp $MNE_ROOT/share/mne/mne_analyze/fsaverage/fsaverage-head.fif            $SUBJECTS_DIR/fsaverage2/bem/fsaverage2-head.fif
cp $MNE_ROOT/share/mne/mne_analyze/fsaverage/fsaverage-inner_skull-bem.fif $SUBJECTS_DIR/fsaverage2/bem/fsaverage2-inner_skull-bem.fif
cp $MNE_ROOT/share/mne/mne_analyze/fsaverage/fsaverage-fiducials.fif       $SUBJECTS_DIR/fsaverage2/bem/fsaverage2-fiducials.fif
cp $MNE_ROOT/share/mne/mne_analyze/fsaverage/fsaverage-trans.fif           $SUBJECTS_DIR/fsaverage2/bem/fsaverage2-trans.fif

#
# Note 2: output the scalp surface. This is needed in order to
# place digitizer points in mne_analyze. (may have to be in the
# fsaverage2/bem directory to run this)
#
mne_list_bem --bem $SUBJECTS_DIR/fsaverage2/bem/fsaverage2-head.fif                   --surf --out $SUBJECTS_DIR/fsaverage2/bem/outer_skin.surf
mne_list_bem --bem $SUBJECTS_DIR/fsaverage2/bem/fsaverage2-inner_skull-bem.fif --id 1 --surf --out $SUBJECTS_DIR/fsaverage2/bem/inner_skull.surf

#
# Step 3: compute BEM geometry data for the forward solution
#
mne_prepare_bem_model --bem $SUBJECTS_DIR/fsaverage2/bem/fsaverage2-inner_skull-bem.fif

#
# Step 4: setup the source space
#
mne_setup_source_space --subject fsaverage2 --spacing $SPACING

#
# Step 5: setup the MRI is not being done here
#
# Step 6: coordinate alignment has already been done for this subject using mne_analyze
#

#
# Additional processing steps from the MNE cookbook
#
mne_watershed_bem --overwrite --subject fsaverage2
#
mne_setup_forward_model --subject fsaverage2 --homog --surf

#
# Step 7: compute the forward solution
#
mne_do_forward_solution --subject fsaverage2 --spacing $SPACING --mricoord --fixed --megonly --overwrite --meas /media/lysne/3TB1/FaceEmotion.EMF/faces.filt.decim.cat.proj.mneave.fif --fwd fsaverage2.fwd.fif --mri /media/lysne/3TB1/FaceEmotion.FS/fsaverage/mri/T1-neuromag/sets/COR-lysne-150202-102520.fif
