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

Discussion:
codecov-io: # [Codecov](https://codecov.io/gh/mhe/pynrrd/pull/103?src=pr&el=h1) Report
> Merging [#103](https://codecov.io/gh/mhe/pynrrd/pull/103?src=pr&el=desc) into [master](https://codecov.io/gh/mhe/pynrrd/commit/b500fbce20b4c7126587714b4a3bcefe9fa1d645?src=pr&el=desc) will **increase** coverage by `0.26%`.
> The diff coverage is `75%`.

[![Impacted file tree graph](https://codecov.io/gh/mhe/pynrrd/pull/103/graphs/tree.svg?width=650&token=ktfNONePUI&height=150&src=pr)](https://codecov.io/gh/mhe/pynrrd/pull/103?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #103      +/-   ##
==========================================
+ Coverage   99.47%   99.73%   +0.26%     
==========================================
  Files           6        6              
  Lines         381      383       +2     
  Branches      123      124       +1     
==========================================
+ Hits          379      382       +3     
+ Misses          1        0       -1     
  Partials        1        1
```


| [Impacted Files](https://codecov.io/gh/mhe/pynrrd/pull/103?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [nrrd/writer.py](https://codecov.io/gh/mhe/pynrrd/pull/103/diff?src=pr&el=tree#diff-bnJyZC93cml0ZXIucHk=) | `99.19% <75%> (+0.83%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/mhe/pynrrd/pull/103?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/mhe/pynrrd/pull/103?src=pr&el=footer). Last update [b500fbc...abaad66](https://codecov.io/gh/mhe/pynrrd/pull/103?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-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'