# -*- coding: utf-8 -*-
#  This software and supporting documentation are distributed by
#      Institut Federatif de Recherche 49
#      CEA/NeuroSpin, Batiment 145,
#      91191 Gif-sur-Yvette cedex
#      France
#
# This software is governed by the CeCILL license version 2 under
# French law and abiding by the rules of distribution of free software.
# You can  use, modify and/or redistribute the software under the 
# terms of the CeCILL license version 2 as circulated by CEA, CNRS
# and INRIA at the following URL "http://www.cecill.info". 
#
# As a counterpart to the access to the source code and  rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty  and the software's author,  the holder of the
# economic rights,  and the successive licensors  have only  limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading,  using,  modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean  that it is complicated to manipulate,  and  that  also
# therefore means  that it is reserved for developers  and  experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or 
# data to be ensured and,  more generally, to use and operate it in the 
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license version 2 and that you accept its terms.



from brainvisa.processes import *
from brainvisa import shelltools
import os

name = 'RicCorticalThicknessByNormal'
roles = ('misc',)
userLevel = 0

signature = Signature(
  'Gray_matter_outer_mesh', ReadDiskItem( 'Mesh', ['MESH mesh', 'GIFTI file'] ),
  'White_matter_inner_mesh', ReadDiskItem( 'Mesh', ['MESH mesh', 'GIFTI file']),
  'Gray_matter_curvature_file', ReadDiskItem( 'Texture' ,['GIFTI file','Texture',
      'Z compressed Texture',
      'gz compressed texture',
      'Z compressed GIFTI file',
      'gz compressed GIFTI file']),
  'White_matter_curvature_file',  ReadDiskItem( 'Texture', ['GIFTI file','Texture',
      'Z compressed Texture',
      'gz compressed texture',
      'Z compressed GIFTI file',
      'gz compressed GIFTI file']),
  'Surface_to_map_on_Gray', Boolean(),
  'Surface_to_map_on_White_Default', Boolean(),
  'Base_output_file_name', String(),
    'Transformation_matrix_file_name', ReadDiskItem( 'Text file', []),
    'minc', Float(),
     'maxc', Float(),
       'mind', Float(),
     'maxd', Float(),
      'Fill_nodes' , Boolean(),
      'Max_fill_dist', Float(),
    'verbose' , Boolean(),
     'brainvisa_output', Boolean(),
     'PeterK_output', Boolean(),
      'GPU_mode', Boolean(),
    

)


def initialization( self ):
  self.setOptional(  'Gray_matter_curvature_file', 
  'White_matter_curvature_file',  
  'Surface_to_map_on_Gray', 
  'Surface_to_map_on_White_Default', 
  'Base_output_file_name', 
    'Transformation_matrix_file_name', 
    'minc', 
     'maxc', 
       'mind', 
     'maxd', 
      'Fill_nodes' , 
      'Max_fill_dist', 
    'verbose' , 
     'brainvisa_output', 
     'PeterK_output',
      'GPU_mode',)
  self.Gray_matter_curvature_file = None
  self.White_matter_curvature_file = None
  self.Surface_to_map_on_Gray = False
  self.Surface_to_map_on_White_Default = False
  self.Base_output_file_name = None 
  self.Transformation_matrix_file_name = None
  self.minc = None
  self.maxc = None 
  self.mind = None
  self.maxd = None
  self.Fill_nodes = False
  self.Max_fill_dist = None
  self.verbose = False
  self.brainvisa_output = False
  self.PeterK_output = False
  self.GPU_mode = False

def execution( self, context ):
  command = ['RicCorticalThicknessByNormal' , '--gm', self.Gray_matter_outer_mesh, '--wm',  self.White_matter_inner_mesh]
  
  
  if self.Gray_matter_curvature_file != None:
	  command += ['--gc',self.Gray_matter_curvature_file]
  
  if self.White_matter_curvature_file != None:
	  ccommand += ['--wc',self.White_matter_curvature_file]
  
  if self.Surface_to_map_on_Gray != False and self.Surface_to_map_on_White_Default != False:
	  command += ['--sm', 'both']
  elif self.Surface_to_map_on_Gray != False:
	  command += ['--sm', 'gray']
  
  if self.brainvisa_output != False:
	  command += ['--bv']
	  
  if self.GPU_mode != False:
	  command += ['--gpu']
	  
  if self.PeterK_output != False:
	  command += ['--pk']
	  
  if self.verbose != False:
	  command += ['-v']
	  
  if self.Max_fill_dist != None:
	  command += ['--fd', self.Max_fill_dist]
	  
  if self.Fill_nodes:
	  command += ['--ft']
  
  if self.minc:
	  command += ['--minc', self.minc]
	  
  if self.maxc:
	  command += ['--maxc', self.maxc]
	  
  if self.maxd:
	  command += ['--maxd', self.maxd]
	  
  if self.mind:
	  command += ['--mind', self.mind]
	  
  if self.Transformation_matrix_file_name != None:
	  command += ['--tm', self.Transformation_matrix_file_name]	
  
  if self.Base_output_file_name != None:
	  command += ['--o', self.Base_output_file_name]
   
	  
	  
    
      
  if apply( context.system, command ):
    raise Exception( _t_('Error while reading <em>%s</em> to <em>%s</em>') % \
                         ( command[ 2 ], command[ 4 ])  )

	
