From f92712bf13625d8706dbba6bb4e86b3e4efaa523 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Thu, 22 Dec 2016 14:02:22 +0000
Subject: [PATCH] Fix for images which have a qform but not an sform.

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

diff --git a/fsl/data/image.py b/fsl/data/image.py
index 8621ea60f..5f1ad22c4 100644
--- a/fsl/data/image.py
+++ b/fsl/data/image.py
@@ -240,12 +240,27 @@ class Nifti(object):
                     - :data:`~.constants.NIFTI_XFORM_MNI_152`
         """
 
-        if   code is None:     code = 'sform_code'
-        elif code == 'sform' : code = 'sform_code'
+        if   code == 'sform' : code = 'sform_code'
         elif code == 'qform' : code = 'qform_code'
-        else: raise ValueError('code must be None, sform, or qform')
+        elif code is not None:
+            raise ValueError('code must be None, sform, or qform')
+
+        if code is not None:
+            code = self.header[code]
+
+        # If a specific code is not
+        # specified, we check both.
+        # If the sform is present,
+        # we return it. Otherwise,
+        # if the qform is present,
+        # we return that.
+        else:
+            
+            sform_code = self.header['sform_code']
+            qform_code = self.header['qform_code']
 
-        code = self.header[code]
+            if   sform_code != constants.NIFTI_XFORM_UNKNOWN: code = sform_code
+            elif qform_code != constants.NIFTI_XFORM_UNKNOWN: code = qform_code
 
         # Invalid values
         if   code > 4: code = constants.NIFTI_XFORM_UNKNOWN
-- 
GitLab