From 4dcba47bf422c751fe6a863b464ee78a729101e5 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Thu, 17 Nov 2016 13:31:06 +0000
Subject: [PATCH] New function defaultExt, which checks FSLOUTPUTTYPE, and
 returns an appropriate extension

---
 fsl/data/image.py | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/fsl/data/image.py b/fsl/data/image.py
index cff154493..5e2a49195 100644
--- a/fsl/data/image.py
+++ b/fsl/data/image.py
@@ -27,12 +27,14 @@ and file names:
    looksLikeImage
    removeExt
    addExt
+   defaultExt
    loadIndexedImageFile
 """
 
 
-import               logging
+import               os
 import os.path    as op
+import               logging
 
 import               six 
 import numpy      as np
@@ -758,10 +760,6 @@ ambiguities - see :func:`fsl.utils.path.addExt`.
 """
 
 
-DEFAULT_EXTENSION  = '.nii.gz'
-"""The default file extension (TODO read this from ``$FSLOUTPUTTYPE``)."""
-
-
 PathError = fslpath.PathError
 """Error raised by :mod:`fsl.utils.path` functions when an error occurs.
 Made available in this module for convenience.
@@ -809,10 +807,29 @@ def addExt(prefix, mustExist=True):
     return fslpath.addExt(prefix,
                           ALLOWED_EXTENSIONS,
                           mustExist,
-                          DEFAULT_EXTENSION,
+                          defaultExt(),
                           fileGroups=FILE_GROUPS)
 
 
+def defaultExt():
+    """Returns the default NIFTI file extension that should be used.
+
+    If the ``$FSLOUTPUTTYPE`` variable is set, its value is used.
+    Otherwise, ``.nii.gz`` is returned.
+    """
+
+    # TODO: Add analyze support.
+    options = {
+        'NIFTI'      : '.nii',
+        'NIFTI_PAIR' : '.img',
+        'NIFTI_GZ'   : '.nii.gz',
+    }
+    
+    outputType = os.environ.get('FSLOUTPUTTYPE', 'NIFTI_GZ')
+
+    return options.get(outputType, '.nii.gz')
+
+
 def loadIndexedImageFile(filename):
     """Loads the given image file using ``nibabel`` and ``indexed_gzip``.
 
-- 
GitLab