Repository: mhe/pynrrd

Issue:
Need to account for both 'datafile' and 'data file'
We should account for both 'data file' and 'datafile' in [Ln 190](https://github.com/mhe/pynrrd/blob/master/nrrd/writer.py#L190) and [Ln 211](https://github.com/mhe/pynrrd/blob/master/nrrd/writer.py#L211) just like:
https://github.com/mhe/pynrrd/blob/master/nrrd/reader.py#L354

Comments:

Patch:
diff --git a/nrrd/writer.py b/nrrd/writer.py
index b9187f7..24d78b0 100644
--- a/nrrd/writer.py
+++ b/nrrd/writer.py
@@ -180,6 +180,11 @@ def write(filename, data, header=None, detached_header=False, relative_data_path
     if 'encoding' not in header:
         header['encoding'] = 'gzip'
 
+    # If 'datafile' is specified, then we rename to 'data file'
+    # The standard seems to advocate for 'data file' OVER 'datafile'
+    if 'datafile' in header:
+        header['data file'] = header.pop('datafile')
+
     # A bit of magic in handling options here.
     # If *.nhdr filename provided, this overrides `detached_header=False`
     # If *.nrrd filename provided AND detached_header=True, separate header and data files written.
@@ -188,7 +193,9 @@ def write(filename, data, header=None, detached_header=False, relative_data_path
     if filename.endswith('.nhdr'):
         detached_header = True
 
-        if 'data file' not in header:
+        # TODO This will cause issues for relative data files because it will not save in the correct spot
+        data_filename = header.get('datafile', None)
+        if not data_filename:
             # Get the base filename without the extension
             base_filename = os.path.splitext(filename)[0]
 
@@ -207,9 +214,6 @@ def write(filename, data, header=None, detached_header=False, relative_data_path
 
             header['data file'] = os.path.basename(data_filename) \
                 if relative_data_path else os.path.abspath(data_filename)
-        else:
-            # TODO This will cause issues for relative data files because it will not save in the correct spot
-            data_filename = header['data file']
     elif filename.endswith('.nrrd') and detached_header:
         data_filename = filename
         header['data file'] = os.path.basename(data_filename) \


Test patch:
diff --git a/nrrd/tests/test_writing.py b/nrrd/tests/test_writing.py
index 535b75d..7c0c1c7 100644
--- a/nrrd/tests/test_writing.py
+++ b/nrrd/tests/test_writing.py
@@ -310,6 +310,26 @@ class TestWritingFunctions(object):
             self.assertTrue('space units: "mm" "cm" "in"' in lines)
             self.assertTrue('labels: "X" "Y" "f(log(X, 10), Y)"' in lines)
 
+    def test_write_detached_datafile_check(self):
+        output_filename = os.path.join(self.temp_write_dir, 'testfile_detached.nhdr')
+
+        nrrd.write(output_filename, self.data_input, {'datafile': 'testfile_detached.gz'}, detached_header=True,
+                   index_order=self.index_order)
+
+        # Read back the same file
+        data, header = nrrd.read(output_filename, index_order=self.index_order)
+        self.assertEqual(header['data file'], 'testfile_detached.raw.gz')
+
+    def test_write_detached_datafile_check2(self):
+        output_filename = os.path.join(self.temp_write_dir, 'testfile_detached.nhdr')
+
+        nrrd.write(output_filename, self.data_input, {'data file': 'testfile_detached.gz'}, detached_header=True,
+                   index_order=self.index_order)
+
+        # Read back the same file
+        data, header = nrrd.read(output_filename, index_order=self.index_order)
+        self.assertEqual(header['data file'], 'testfile_detached.raw.gz')
+
 
 class TestWritingFunctionsFortran(TestWritingFunctions, unittest.TestCase):
     index_order = 'F'