Repository: spectrochempy/spectrochempy

Issue:
read_opus() shifts the xaxis
Author: @atravert (Arnaud TRAVERT)

Redmine Issue: 75, https://redmine.spectrochempy.fr/issues/75

---

A bug in brukeropusreader module leads to a shift of the xaxis.
It has been corrected on the spectrochempy/brukeropusreader fork (PR FIX wrong setting of wavenumbers axis #1) 
but a change in read_opus() is also needed.




Discussion:
From: @fernandezc (Christian Fernandez )
Date: 2020-05-30T10:54:32Z

---

Is this still a bug? should we close this issue?


I will add a test and then close it.

Resolving patch:
diff --git a/spectrochempy/core/readers/readopus.py b/spectrochempy/core/readers/readopus.py
--- a/spectrochempy/core/readers/readopus.py
+++ b/spectrochempy/core/readers/readopus.py
@@ -65,7 +65,10 @@ def read_opus(dataset=None, **kwargs):
     >>> A = NDDataset.read_opus('irdata\\spectrum.0001')
     >>> print(A)
     NDDataset: [float64] a.u. (shape: (y:1, x:2568))
+
+
     """
+
     debug_("reading bruker opus files")
 
     # filename will be given by a keyword parameter except if the first parameters is already
@@ -114,7 +117,7 @@ def read_opus(dataset=None, **kwargs):
         xdata = linspace(fxv, lxv, npt)
 
         if not xaxis:
-            xaxis = Coord(x=xdata, title='Wavenumbers', units='cm^-1')
+            xaxis = Coord(xdata, title='Wavenumbers', units='cm^-1')
 
         elif (xdata != xaxis.data).any():
             raise ValueError("spectra have incompatible dimensions (xaxis)")
@@ -150,16 +153,13 @@ def read_opus(dataset=None, **kwargs):
 
     # Set origin, description and history
     dataset.origin = "opus"
-    dataset.description = ('Dataset from opus files. \n')
+    dataset.description = 'Dataset from opus files. \n'
 
     if sortbydate:
         dataset.sort(dim='y', inplace=True)
 
-    dataset.history = str(datetime.now()) + ':import from opus files \n'
-
-    # Set the NDDataset date
+    dataset.history = str(datetime.now()) + ': import from opus files \n'
     dataset._date = datetime.now()
     dataset._modified = dataset.date
-    # debug_("end of reading")
 
     return dataset


Test patch:
diff --git a/tests/test_readers/test_read_opus.py b/tests/test_readers/test_read_opus.py
new file mode 100644
--- /dev/null
+++ b/tests/test_readers/test_read_opus.py
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+#
+# ======================================================================================================================
+# Copyright (©) 2015-2020 LCS
+# Laboratoire Catalyse et Spectrochimie, Caen, France.
+# CeCILL-B FREE SOFTWARE LICENSE AGREEMENT
+# See full LICENSE agreement in the root directory
+# ======================================================================================================================
+
+import os
+from spectrochempy.core.dataset.nddataset import NDDataset
+import pytest
+
+
+def test_read_with_filename():
+    A = NDDataset.read_opus(os.path.join('irdata', 'OPUS', 'test.0000'))
+    assert A[0, 2303.8694].data == pytest.approx(2.72740, 0.00001)
+    B = NDDataset.read_opus(['test.0000', 'test.0001', 'test.0002'],
+                            directory=os.path.join('irdata', 'OPUS', ))
+    assert len(B) == 3
+
+
+