datapath = '/neuro/labs/meglab/research/Limin/ElektaPhantom/DemoForLearn/';
filename = 'HPI_SGM_170112_163723_Phantom_20nAm_dip3_raw.fif';
avgfilename = 'HPI_SGM_170112_163723_Phantom_20nAm_dip3';
subject = 'ID_4892539_temp';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Several commands can help you understand MNE
% mne_view_manual
% mne_show_fiff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
unix(['mne_show_fiff --in ' datapath filename ' --verbose --long']);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% You need to call mne_view_manual directly from linux terminal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cmd = 'mne_view_manual';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Before we start to do anything, we need to know there are two
% template scripts. If you can not find them, you need to define
% them by yourself.
% Those template samples can be found in
% $MNE_ROOT/share/mne/mne_browse_raw/templates
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% simulation.ave and simulation.cov
% type gedit or vi to edit these two template for your application.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The first step to analysis the data is to check if there are
% extra bad channels. We can do this from mne_browse_raw
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
unix(['mne_browse_raw --raw ' datapath filename ]);
% From this routine, you can mark the extra bad channels and re-estimate
% SSP projector.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% After the noise issue is solved by using SSP to remove the
% enviroment noise, we can start to average the event-related
% responses.
%
% We will start an off-line averaging.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
unix(['mne_process_raw --raw ' datapath filename ' --digtrig TRG001 --lowpass 40 --projoff --saveavetag -ave --ave ' datapath 'simulation.ave']);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Now you can view the average file from mne_browse_raw
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
unix(['mne_browse_raw &']);
%File/Open evoked
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Next, we need to compute the noise-covariance matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
unix(['mne_process_raw --raw ' datapath filename ' --digtrig TRG001 --lowpass 40 --projon --savecovtag -cov --cov ' datapath 'simulation.cov']);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The noise covariance matrix is for the inverse solution of brain
% imaging.
% To do the source imaging, we need to do the following steps.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1. Setup the structural MRIs
unix('mne_setup_mri --overwrite');
%2. Setup source space: the related files are in /bem
unix('mne_setup_source_space --overwrite --ico -6');
%3. Setup Boundary-element model (BEM) for the forward solution
unix('mne_setup_forward_model --homog --surf --ico 4');
%4. MEG-MRI coordinate system alignment
% At this step, we need to call mne_analyze
unix('mne_analyze');
% a. File/load digitizer data from the raw data file
% b. File/Load surface from an inflated surface
% c. Open Viewer (View/show viewer)
% d. Open coordinate alignment window from Adjust/Coordinate alignment
% e. Setup RAP, nasion, LAP.
% f. By pressing the button of "Align using fiducials", you can get
% the transfer matrix. Save it to your specified dir.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute the forward solution
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
unix(['mne_do_forward_solution --overwrite --mindist 5 --spacing oct-6 --bem ' subject '-5120-bem-sol.fif --meas ' ...
datapath avgfilename '-ave.fif']);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute the inverse operator
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
unix(['mne_do_inverse_operator --fwd ' datapath avgfilename '-ave-oct-6-fwd.fif --depth --loose 0.2 --meg' ]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Final step, go to mne_analyze to check your results.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
unix('mne_analyze');
% load evoke file and load surface from there.