import os
import logging
import envoy
from .ants_error import ANTSError

def create_template(metric, files):
    """
    Builds a template from the given image files.
    
    :param metric: the similarity metric
    :param files: the image files
    :return: the template file name
    """
    CMD = "buildtemplateparallel.sh -d 2 -c 2 -j 4 -d 2 -s {metric} -o {output} {files}"
    PREFIX = 'reg_'
    SUFFIX = 'template.nii.gz'

    tmpl = PREFIX + SUFFIX
    if os.path.exists(tmpl):
        logging.info("Registration template already exists: %s" % tmpl)
        return tmpl
    cmd = CMD.format(metric=metric.name, output=PREFIX, files=' '.join(files))
    logging.info("Building the %s registration template with the following command:" % tmpl)
    logging.info(cmd[:80])
    r = envoy.run(cmd)
    if r.status_code:
        logging.error("Build registration template failed with error code %d" % r.status_code)
        logging.error(r.std_err)
        raise ANTSError("Build registration template unsuccessful; see the log for details")
    if not os.path.exists(tmpl):
        logging.error("Build registration template was not created.")
        raise ANTSError("Build registration template unsuccessful; see the log for details")
        
    logging.info("Built the registration template %s." % tmpl)
    return tmpl

@@ -31,16 +31,50 @@
 t envoy%0A
+from .similarity_metrics import *%0A
 from .an
@@ -124,21 +124,48 @@
 ate(
-metric, files
+files, dimension=2, cores=4, metric=PR()
 ):%0A 
@@ -241,71 +241,220 @@
 ram 
-metric
+files
 : the 
-s
 im
-ilarity metric%0A    :param files
+age files%0A    :param dimension: the image dimension - 2 (default), 3, or 4%0A    :param cores: the number of CPU cores (default 4)%0A    :param metric
 : the 
+s
 im
-age files
+ilarity metric (default probability mapping)
 %0A   
@@ -535,37 +535,17 @@
 sh -
-d 2 -c 2 -j 4 -d 2 -s %7Bmetric
+c 2 %7Bopts
 %7D -o
@@ -615,16 +615,46 @@
 ii.gz'%0A%0A
+    # The template file name.%0A
     tmpl
@@ -801,45 +801,166 @@
     
-cmd = CMD.format(metric=metric.name, 
+%0A    # Build the command line.%0A    opts = %22-d %25(dim)s -j %25(cores)s -m %25(metric)s%22 %25 %7B'dim': dimension, 'cores': cores, 'metric': metric%7D%0A    cmd = CMD.format(
 outp
@@ -969,16 +969,27 @@
 =PREFIX,
+ opts=opts,
  files='
@@ -1124,16 +1124,44 @@
 d%5B:80%5D)%0A
+    %0A    # Run the command.%0A
     r = 

Add options.