From f83ed4743a943261572e7e1ba74b5d2a0e441b09 Mon Sep 17 00:00:00 2001
From: parneshraniga <parnesh.raniga@gmail.com>
Date: Tue, 3 Jan 2017 17:34:16 +1000
Subject: [PATCH 1/3] Update get_csa_header to return none on CSAReadError

Some non-MRI dicom datasets have just strings in CSA Header tags. This causes the read on line 68 to throw an error because the string is not a CSA header. Unfortunately this throws out other packages such as dcmstack which use nibabel. The proposed fix returns none if a read error is encountered when such strings are parsed. It is possible such strings may pass the initial check for 0 < n_tags <=128 which throws the CSAReadError but fail later in which case the except block can be be modified to handle those exceptions as well.
---
 nibabel/nicom/csareader.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/nibabel/nicom/csareader.py b/nibabel/nicom/csareader.py
index cba9e39e34..ecf8833642 100644
--- a/nibabel/nicom/csareader.py
+++ b/nibabel/nicom/csareader.py
@@ -65,10 +65,11 @@ def get_csa_header(dcm_data, csa_type='image'):
     element_no = section_start + element_offset
     try:
         tag = dcm_data[(0x29, element_no)]
-    except KeyError:
+        return read(tag.value)
+    except (KeyError,CSAReadError):
         # The element could be missing due to anonymization
         return None
-    return read(tag.value)
+    
 
 
 def read(csa_str):

From 4a73b36fe2358d003adcc8437fb5ea3a5bd2e45e Mon Sep 17 00:00:00 2001
From: parneshraniga <parnesh.raniga@gmail.com>
Date: Wed, 4 Jan 2017 09:26:15 +1000
Subject: [PATCH 2/3] Update csareader.py to correct style errors

---
 nibabel/nicom/csareader.py | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/nibabel/nicom/csareader.py b/nibabel/nicom/csareader.py
index ecf8833642..2fae0205bd 100644
--- a/nibabel/nicom/csareader.py
+++ b/nibabel/nicom/csareader.py
@@ -66,10 +66,9 @@ def get_csa_header(dcm_data, csa_type='image'):
     try:
         tag = dcm_data[(0x29, element_no)]
         return read(tag.value)
-    except (KeyError,CSAReadError):
+    except (KeyError, CSAReadError):
         # The element could be missing due to anonymization
-        return None
-    
+        return None    
 
 
 def read(csa_str):

From 920cf492ab16d6b269aa50f62bf1921a4a009291 Mon Sep 17 00:00:00 2001
From: parneshraniga <parnesh.raniga@gmail.com>
Date: Wed, 4 Jan 2017 11:30:48 +1000
Subject: [PATCH 3/3] Update csareader.py to fix style and add comments

---
 nibabel/nicom/csareader.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/nibabel/nicom/csareader.py b/nibabel/nicom/csareader.py
index 2fae0205bd..1bfaf02a25 100644
--- a/nibabel/nicom/csareader.py
+++ b/nibabel/nicom/csareader.py
@@ -64,11 +64,14 @@ def get_csa_header(dcm_data, csa_type='image'):
         return None
     element_no = section_start + element_offset
     try:
+        # The element could be missing due to anonymization
         tag = dcm_data[(0x29, element_no)]
+        # Non-MRI Dicoms have string values in CSA tag
+        # This will cause read to throw a CSAReadError
+        # in most instances. If so, we return None
         return read(tag.value)
     except (KeyError, CSAReadError):
-        # The element could be missing due to anonymization
-        return None    
+        return None
 
 
 def read(csa_str):