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