#!/usr/bin/env python
"""Create 3-layer BEM model from Flash MRI images, with CFIN edits.

NB! This is a slightly modified and "clusterised" version of that found in
mne-python/mne/commands, all credits go there.

This program assumes that FreeSurfer and MNE are installed and
sourced properly.

This function extracts the BEM surfaces (outer skull, inner skull, and
outer skin) from multiecho FLASH MRI data with spin angles of 5 and 30
degrees. The multiecho FLASH data are inputted in DICOM format.
This function assumes that the Freesurfer segmentation of the subject
has been completed. In particular, the T1.mgz and brain.mgz MRI volumes
should be, as usual, in the subject's mri directory.

Before running this script do the following:
(unless the --noconvert option is specified)

    1. Copy all of your FLASH images in a single directory <source> and
       create a directory <dest> to hold the output of mne_organize_dicom
    2. cd to <dest> and run
       $ mne_organize_dicom <source>
       to create an appropriate directory structure
    3. Create symbolic links to make flash05 and flash30 point to the
       appropriate series:
       $ ln -s <FLASH 5 series dir> flash05
       $ ln -s <FLASH 30 series dir> flash30
       Some partition formats (e.g. FAT32) do not support symbolic links.
       In this case, copy the file to the appropriate series:
       $ cp <FLASH 5 series dir> flash05
       $ cp <FLASH 30 series dir> flash30
    4. cd to the directory where flash05 and flash30 links are
    5. Set SUBJECTS_DIR and SUBJECT environment variables appropriately
    6. Run this script

Example usage:

$ cfin_flash_bem --subject sample
"""
from __future__ import print_function

# Authors: Lorenzo De Santis

import os
import os.path as op
from mne.bem import make_flash_bem
from stormdb.process import convert_flash_mris_cfin
from argparse import ArgumentParser

parser = ArgumentParser()

parser.add_argument("-s", "--subject", dest="subject",
                  help="Subject name", default=None)
parser.add_argument("-d", "--subjects-dir", dest="subjects_dir",
                  help="Subjects directory", default=None)
parser.add_argument("-3", "--noflash30", dest="noflash30",
                  action="store_true", default=False,
                  help=("Skip the 30-degree flip angle data"),)
parser.add_argument("-u", "--unwarp", dest="unwarp",
                  action="store_true", default=False,
                  help=("Run grad_unwarp with -unwarp <type> option on "
                        "each of the converted data sets"))
parser.add_argument("-o", "--overwrite", dest="overwrite",
                  action="store_true", default=False,
                  help="Write over existing .surf files in bem folder")

options = parser.parse_args()

subject = options.subject
subjects_dir = options.subjects_dir
flash30 = not options.noflash30
unwarp = options.unwarp
overwrite = options.overwrite

if options.subject is None:
    parser.print_help()
    raise RuntimeError('The subject argument must be set')

# NB assumes this is run in the flash-dir (like mne_flash_bem)
flash_dir = op.join(os.getcwd(), 'flash05/')  # NB final slash is important!
if not op.isdir(flash_dir):
    raise RuntimeError('No sorted flash-folders found in {}'
                       .format(flash_dir))

n_echos = len([name for name in os.listdir(flash_dir) if
              op.isdir(op.join(flash_dir, name))])

convert_flash_mris_cfin(subject=subject, subjects_dir=subjects_dir,
                        flash30=flash30, n_echos=n_echos, unwarp=unwarp)
make_flash_bem(subject=subject, subjects_dir=subjects_dir,
               overwrite=overwrite, show=False, flash_path=None)
