[c476457] | 1 | from sas.sascalc.file_converter.nxcansas_writer import NXcanSASWriter |
---|
[248ff73] | 2 | from sas.sascalc.dataloader.loader import Loader |
---|
[c476457] | 3 | |
---|
| 4 | import os |
---|
[f53d684] | 5 | import os.path |
---|
[c476457] | 6 | import unittest |
---|
| 7 | import warnings |
---|
| 8 | |
---|
| 9 | warnings.simplefilter("ignore") |
---|
| 10 | |
---|
[f53d684] | 11 | |
---|
| 12 | def find(filename): |
---|
| 13 | return os.path.join(os.path.dirname(__file__), filename) |
---|
| 14 | |
---|
| 15 | |
---|
[c476457] | 16 | class nxcansas_writer(unittest.TestCase): |
---|
| 17 | |
---|
| 18 | def setUp(self): |
---|
[248ff73] | 19 | self.loader = Loader() |
---|
[c476457] | 20 | self.writer = NXcanSASWriter() |
---|
[f53d684] | 21 | self.read_file_1d = find("cansas1d.xml") |
---|
| 22 | self.write_file_1d = find("export1d.h5") |
---|
| 23 | self.read_file_2d = find("exp18_14_igor_2dqxqy.dat") |
---|
| 24 | self.write_file_2d = find("export2d.h5") |
---|
[c476457] | 25 | |
---|
[248ff73] | 26 | self.data_1d = self.loader.load(self.read_file_1d)[0] |
---|
[c476457] | 27 | |
---|
[248ff73] | 28 | self.data_2d = self.loader.load(self.read_file_2d)[0] |
---|
[c476457] | 29 | self.data_2d.detector[0].name = '' |
---|
| 30 | self.data_2d.source.radiation = 'neutron' |
---|
| 31 | |
---|
| 32 | def test_write_1d(self): |
---|
| 33 | self.writer.write([self.data_1d], self.write_file_1d) |
---|
[248ff73] | 34 | data = self.loader.load(self.write_file_1d) |
---|
[c476457] | 35 | self.assertTrue(len(data) == 1) |
---|
| 36 | data = data[0] |
---|
| 37 | self.assertTrue(len(data.x) == len(self.data_1d.x)) |
---|
| 38 | self.assertTrue(len(data.y) == len(self.data_1d.y)) |
---|
| 39 | self.assertTrue(len(data.dy) == len(self.data_1d.dy)) |
---|
| 40 | self._check_metadata(data, self.data_1d) |
---|
| 41 | |
---|
| 42 | def test_write_2d(self): |
---|
| 43 | self.writer.write([self.data_2d], self.write_file_2d) |
---|
[248ff73] | 44 | data = self.loader.load(self.write_file_2d) |
---|
[c476457] | 45 | self.assertTrue(len(data) == 1) |
---|
| 46 | data = data[0] |
---|
| 47 | self.assertTrue(len(data.data) == len(self.data_2d.data)) |
---|
| 48 | self.assertTrue(len(data.qx_data) == len(self.data_2d.qx_data)) |
---|
| 49 | self.assertTrue(len(data.qy_data) == len(self.data_2d.qy_data)) |
---|
| 50 | self._check_metadata(data, self.data_2d) |
---|
| 51 | |
---|
| 52 | def _check_metadata(self, written, correct): |
---|
| 53 | self.assertTrue(written.title == correct.title) |
---|
| 54 | self.assertTrue(written.sample.name == correct.sample.name) |
---|
| 55 | self.assertAlmostEqual(written.sample.thickness, correct.sample.thickness) |
---|
| 56 | self.assertAlmostEqual(written.sample.temperature, correct.sample.temperature) |
---|
| 57 | self.assertTrue(written.instrument == correct.instrument) |
---|
| 58 | self.assertTrue(len(written.detector) == len(correct.detector)) |
---|
| 59 | for i in range(len(written.detector)): |
---|
| 60 | written_det = written.detector[i] |
---|
| 61 | correct_det = correct.detector[i] |
---|
| 62 | self.assertAlmostEqual(written_det.distance, correct_det.distance) |
---|
| 63 | self.assertTrue(written_det.name == correct_det.name) |
---|
| 64 | self.assertTrue(written.source.radiation == correct.source.radiation) |
---|
| 65 | |
---|
| 66 | def tearDown(self): |
---|
| 67 | if os.path.isfile(self.write_file_1d): |
---|
| 68 | os.remove(self.write_file_1d) |
---|
[17c9436] | 69 | if os.path.isfile(self.write_file_2d): |
---|
| 70 | os.remove(self.write_file_2d) |
---|