Changeset 0b1a677 in sasview for src/sas/sascalc/file_converter
- Timestamp:
- Aug 10, 2016 5:38:37 AM (8 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 18544f8
- Parents:
- 35488b2
- Location:
- src/sas/sascalc/file_converter
- Files:
-
- 1 added
- 1 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/file_converter/convert_bsl_thread.py
r35488b2 r0b1a677 3 3 from sas.sascalc.data_util.calcthread import CalcThread 4 4 from sas.sascalc.dataloader.data_info import Data2D 5 from sas.sascalc.file_converter.bsl_loader import BSLLoader 6 from sas.sascalc.dataloader.readers.red2d_reader import Reader as Red2DWriter 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 7 21 8 22 class ConvertBSLThread(CalcThread): 9 23 10 def __init__(self, parent, in_file, out_file,24 def __init__(self, xy, frame_data, out_file, frame_number=None, 11 25 updatefn=None, completefn=None): 12 26 CalcThread.__init__(self, updatefn=updatefn, completefn=completefn) 13 self.parent = parent 14 self.in_file = in_file 27 (self.x_data, self.y_data) = xy 28 self.frame_data = frame_data 29 self.frame_number = frame_number 30 self.frame_filename = '' 15 31 self.out_file = out_file 16 32 17 33 def compute(self): 18 self.ready(delay=0.0)19 self.update(msg="Extracting data...")20 21 34 try: 22 x, y, frame_data = self._extract_bsl_data(self.in_file) 23 except Exception as e: 24 self.ready() 25 self.update(exception=e) 26 self.complete(success=False) 27 return 28 29 if x == None and y == None and frame_data == None: 30 # Cancelled by user 31 self.ready() 32 self.update(msg="Conversion cancelled") 33 self.complete(success=False) 34 return 35 36 if self.isquit(): 37 self.complete(success=False) 38 return 39 40 self.ready(delay=0.0) 41 self.update(msg="Exporting data...") 42 43 try: 44 completed = self._convert_to_red2d(self.out_file, x, y, frame_data) 35 completed = self._convert_to_red2d() 45 36 except Exception as e: 46 37 self.ready() … … 51 42 self.complete(success=completed) 52 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.""" 53 48 54 def _extract_bsl_data(self, filename): 55 """ 56 Extracts data from a 2D BSL file 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 57 55 58 :param filename: The header file to extract the data from 59 :return x_data: A 1D array containing all the x coordinates of the data 60 :return y_data: A 1D array containing all the y coordinates of the data 61 :return frame_data: A dictionary of the form frame_number: data, where 62 data is a 2D numpy array containing the intensity data 63 """ 64 loader = BSLLoader(filename) 65 frames = [0] 66 should_continue = True 67 68 if loader.n_frames > 1: 69 params = self.parent.ask_frame_range(loader.n_frames) 70 frames = params['frames'] 71 elif loader.n_rasters == 1 and loader.n_frames == 1: 72 message = ("The selected file is an OTOKO file. Please select the " 73 "'OTOKO 1D' option if you wish to convert it.") 74 dlg = wx.MessageDialog(self.parent, 75 message, 76 'Error!', 77 wx.OK | wx.ICON_WARNING) 78 dlg.ShowModal() 79 should_continue = False 80 dlg.Destroy() 81 else: 82 message = ("The selected data file only has 1 frame, it might be" 83 " a multi-frame OTOKO file.\nContinue conversion?") 84 dlg = wx.MessageDialog(self.parent, 85 message, 86 'Warning!', 87 wx.YES_NO | wx.ICON_WARNING) 88 should_continue = (dlg.ShowModal() == wx.ID_YES) 89 dlg.Destroy() 90 91 if not should_continue: 92 return None, None, None 93 94 frame_data = {} 95 96 for frame in frames: 97 loader.frame = frame 98 frame_data[frame] = loader.load_data() 99 100 # TODO: Tidy this up 101 # Prepare axes values (arbitrary scale) 102 x_data = [] 103 y_data = range(loader.n_pixels) * loader.n_rasters 104 for i in range(loader.n_rasters): 105 x_data += [i] * loader.n_pixels 106 107 return x_data, y_data, frame_data 108 109 def _convert_to_red2d(self, filepath, x, y, frame_data): 56 def _convert_to_red2d(self): 110 57 """ 111 58 Writes Data2D objects to Red2D .dat files. If more than one frame is … … 121 68 :return: True if export completed, False if export cancelled by user 122 69 """ 123 filename = os.path.split( filepath)[-1]124 filepath = os.path.split( filepath)[0]70 filename = os.path.split(self.out_file)[-1] 71 filepath = os.path.split(self.out_file)[0] 125 72 writer = Red2DWriter() 126 73 127 for i, frame in frame_data.iteritems(): 128 # If more than 1 frame is being exported, append the frame 129 # number to the filename 130 if self.isquit(): 131 return False 74 if self.isquit(): 75 return False 132 76 133 if len(frame_data) > 1:134 135 frame_filename[0] += str(i+1)136 137 138 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 139 83 140 data_i = frame.reshape((len(x),1))141 data_info = Data2D(data=data_i, qx_data=x, qy_data=y)142 writer.write(os.path.join(filepath, frame_filename), data_info)143 self.ready()144 self.update(msg="Written file: {}".format(frame_filename))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) 145 89 146 return True 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
Note: See TracChangeset
for help on using the changeset viewer.