1 | import os |
---|
2 | import wx |
---|
3 | from sas.sascalc.data_util.calcthread import CalcThread |
---|
4 | from sas.sascalc.dataloader.data_info import Data2D |
---|
5 | from sas.sascalc.file_converter.red2d_writer import Red2DWriter |
---|
6 | |
---|
7 | import sys |
---|
8 | |
---|
9 | if sys.platform.count("darwin") > 0: |
---|
10 | import time |
---|
11 | stime = time.time() |
---|
12 | |
---|
13 | def clock(): |
---|
14 | return time.time() - stime |
---|
15 | |
---|
16 | def sleep(t): |
---|
17 | return time.sleep(t) |
---|
18 | else: |
---|
19 | from time import clock |
---|
20 | from time import sleep |
---|
21 | |
---|
22 | class ConvertBSLThread(CalcThread): |
---|
23 | |
---|
24 | def __init__(self, xy, frame_data, out_file, frame_number=None, |
---|
25 | updatefn=None, completefn=None): |
---|
26 | CalcThread.__init__(self, updatefn=updatefn, completefn=completefn) |
---|
27 | (self.x_data, self.y_data) = xy |
---|
28 | self.frame_data = frame_data |
---|
29 | self.frame_number = frame_number |
---|
30 | self.frame_filename = '' |
---|
31 | self.out_file = out_file |
---|
32 | |
---|
33 | def compute(self): |
---|
34 | try: |
---|
35 | completed = self._convert_to_red2d() |
---|
36 | except Exception as e: |
---|
37 | self.ready() |
---|
38 | self.update(exception=e) |
---|
39 | self.complete(success=False) |
---|
40 | return |
---|
41 | |
---|
42 | self.complete(success=completed) |
---|
43 | |
---|
44 | def isquit(self): |
---|
45 | """Check for interrupts. Should be called frequently to |
---|
46 | provide user responsiveness. Also yields to other running |
---|
47 | threads, which is required for good performance on OS X.""" |
---|
48 | |
---|
49 | # # Only called from within the running thread so no need to lock |
---|
50 | # if self._running and self.yieldtime > 0 \ |
---|
51 | # and clock() > self._time_for_nap: |
---|
52 | # sleep(self.yieldtime) |
---|
53 | # self._time_for_nap = clock() + self.worktime |
---|
54 | return self._interrupting |
---|
55 | |
---|
56 | def _convert_to_red2d(self): |
---|
57 | """ |
---|
58 | Writes Data2D objects to Red2D .dat files. If more than one frame is |
---|
59 | provided, the frame number will be appended to the filename of each |
---|
60 | file written. |
---|
61 | |
---|
62 | :param filepath: The filepath to write to |
---|
63 | :param x: The x column of the data |
---|
64 | :param y: The y column of the data |
---|
65 | :param frame_data: A dictionary of the form frame_number: data, where |
---|
66 | data is a 2D numpy array containing the intensity data |
---|
67 | |
---|
68 | :return: True if export completed, False if export cancelled by user |
---|
69 | """ |
---|
70 | filename = os.path.split(self.out_file)[-1] |
---|
71 | filepath = os.path.split(self.out_file)[0] |
---|
72 | writer = Red2DWriter() |
---|
73 | |
---|
74 | if self.isquit(): |
---|
75 | return False |
---|
76 | |
---|
77 | if self.frame_number is not None: |
---|
78 | frame_filename = filename.split('.') |
---|
79 | frame_filename[0] += str(self.frame_number) |
---|
80 | frame_filename = '.'.join(frame_filename) |
---|
81 | else: |
---|
82 | frame_filename = filename |
---|
83 | |
---|
84 | self.ready() |
---|
85 | self.update(msg="Writing file: {}".format(frame_filename)) |
---|
86 | data_i = self.frame_data.reshape((len(self.x_data),1)) |
---|
87 | data_info = Data2D(data=data_i, qx_data=self.x_data, qy_data=self.y_data) |
---|
88 | success = writer.write(os.path.join(filepath, frame_filename), data_info, self) |
---|
89 | |
---|
90 | # Used by ConverterPanel.conversion_complete to notify user that file |
---|
91 | # has been written (or that there was an error) |
---|
92 | self.frame_filename = frame_filename |
---|
93 | |
---|
94 | return success |
---|