def plot_raw_tof(self, bank_id): """ Plot raw vanadium spectrum data in d-spacing :param bank_id: :return: """ datatypeutility.check_int_variable('Bank ID', bank_id, (1, 100)) # Note: there is no need to plot runs in the complicated logic as its parent class # remove the previously plot line for vanadium TOF if self._raw_van_tof_line: self.ui.graphicsView_main.remove_line(self._raw_van_tof_line) # Set up class variables vec_x, vec_y = self._myController.project.vanadium_processing_manager.get_raw_data( bank_id, UNIT['tof']) self._raw_van_tof_line = self.ui.graphicsView_main.add_plot_1d( vec_x, vec_y, color='black', x_label='TOF', label='Raw Vanadium Bank {}'.format(bank_id)) # set unit self._curr_unit = UNIT['tof'] self._curr_bank = bank_id self.ui.checkBox_vpeakShowRaw.setChecked(True) return vec_x, vec_y
def locate_process_vanadium(vanadium_run): """ locate processed vanadium GSAS file and thus also the GSAS prm file related :except OSError: if directory cannot be found :except RuntimeError: if file cannot be found :param vanadium_run: :return: """ datatypeutility.check_int_variable('Vanadium run number', vanadium_run, (1, None)) prm_dir = '/SNS/VULCAN/shared/Calibrationfiles/Instrument/PRM' if not os.path.exists(prm_dir): raise OSError( 'VULCAN PRM directory {} cannot be located.'.format(prm_dir)) # Note: 's' for smoothed gsas_name = os.path.join(prm_dir, '{}-s.gda'.format(vanadium_run)) prm_name = os.path.join(prm_dir, 'Vulcan-{}-s.prm'.format(vanadium_run)) if not (os.path.exists(gsas_name) and os.path.exists(prm_name)): raise RuntimeError( 'Either smoothed GSAS vanadium file {} or PRM file {} cannot be found.' ''.format(gsas_name, prm_name)) return gsas_name, prm_name
def get_archived_vanadium_gsas_name(ipts_number, run_number, check_write_permission): """ Get/generate the vanadium name save to /SNS/VULCAN/shared :param ipts_number: :param run_number: :param check_write_permission: flag to check whether the user can have write permission to the file or directory :return: tuple (boolean: state, string: error message) """ # check inputs datatypeutility.check_int_variable('IPTS number', ipts_number, (1, 999999)) datatypeutility.check_int_variable('Run number', run_number, (1, 99999999)) # write to archive's instrument specific calibration directory's instrument specific calibration directory base_name = '{0}-s.gda'.format(run_number) van_dir = '/SNS/VULCAN/shared/Calibrationfiles/Instrument/Standard/Vanadium' # check directory existence and access if os.path.exists(van_dir) is False: return False, 'Vanadium directory {0} does not exist.'.format( van_dir) elif os.access(van_dir, os.W_OK) is False: return False, 'User has no privilege to write to directory {0}'.format( van_dir) gsas_file_name = os.path.join(van_dir, base_name) if os.path.exists(gsas_file_name) and os.access( gsas_file_name, os.W_OK) is False: return False, 'Smoothed vanadium GSAS file {0} exists and user does not have privilege to over write.' \ ''.format(gsas_file_name) return True, None
def get_gsas_file(self, ipts_number, run_number, check_exist): """ get reduced data written in GSAS. :param ipts_number: :param run_number: :param check_exist: if specified as True and file does not exist, a RuntimeError will be raised :return: """ # check datatypeutility.check_int_variable('IPTS number', ipts_number, (1, 999999)) datatypeutility.check_int_variable('Run number', run_number, (1, 99999999)) # build file name base_name = '{0}.gda'.format(run_number) sub_path = os.path.join( 'IPTS-{0}/shared/binned_data/'.format(ipts_number), base_name) gsas_full_path = os.path.join(self._archiveRootDirectory, sub_path) # check if check_exist and not os.path.exists(gsas_full_path): raise RuntimeError( 'Reduced GSAS file {0} is not found.'.format(gsas_full_path)) return gsas_full_path
def sns_archive_nexus_path(ipts_number, run_number): """ get the SNS archived nexus file path for VULCAN :param ipts_number: :param run_number: :return: name """ datatypeutility.check_int_variable('IPTS number', ipts_number, (1, None)) datatypeutility.check_int_variable('Run number', run_number, (1, None)) ned_nexus_name = '/SNS/VULCAN/IPTS-{0}/nexus/VULCAN_{1}.nxs.h5' \ ''.format(ipts_number, run_number) if os.path.exists(ned_nexus_name): r_file_name = ned_nexus_name else: # pre-Ned case pre_ned_nexus_name = '/SNS/VULCAN/IPTS-{0}/0/{1}/NeXus/VULCAN_{1}_event.nxs'.format( ipts_number, run_number) if os.path.exists(pre_ned_nexus_name): r_file_name = pre_ned_nexus_name else: raise RuntimeError( 'For IPTS-{0} Run {1}: Either nED {2} or pre-nED {3} exists' ''.format(ipts_number, run_number, ned_nexus_name, pre_ned_nexus_name)) # END-IF-ELSE return r_file_name
def strip_v_peaks(self, bank_id, peak_fwhm, pos_tolerance, background_type, is_high_background): """ Strip vanadium peaks Note: result is stored in _striped_peaks_ws_dict :param bank_id: :param peak_fwhm: :param pos_tolerance: :param background_type: :param is_high_background: :return: """ datatypeutility.check_int_variable('Bank ID', bank_id, (1, 99)) datatypeutility.check_int_variable('FWHM (number of pixels)', peak_fwhm, (1, 100)) datatypeutility.check_float_variable('Peak position tolerance', pos_tolerance, (0, None)) raw_van_ws = mantid_helper.retrieve_workspace(self._van_workspace_name) if mantid_helper.is_workspace_group(self._van_workspace_name): input_ws_name = raw_van_ws[bank_id-1].name() bank_list = [1] else: input_ws_name = self._van_workspace_name bank_list = [bank_id] output_ws_name = input_ws_name + '_NoPeak' mantid_helper.strip_vanadium_peaks(input_ws_name=input_ws_name, output_ws_name=output_ws_name, bank_list=bank_list, binning_parameter=None, # PEAK FWHM must be integer (legacy) fwhm=peak_fwhm, peak_pos_tol=pos_tolerance, background_type=background_type, is_high_background=is_high_background) self._striped_peaks_ws_dict[bank_id] = output_ws_name return output_ws_name
def get_chopped_sequence_info(self, run_number, chop_sequence): """ get the information of one chopped workspace (gsas and etc) from a series of GSAS file from a chopped run :param run_number: :param chop_sequence: :return: 3-tuple (workspace name, GSAS file, h5 log file) It is one element from the dictionary """ datatypeutility.check_int_variable('Run number (to chop from)', run_number, (1, 99999999)) datatypeutility.check_int_variable('Chopped (out) sequence', chop_sequence, (0, None)) if run_number not in self._chopped_gsas_dict: raise RuntimeError( 'Run {} is not in "Chopped GSAS Dict". Existing runs are {}' ''.format(run_number, self._chopped_gsas_dict.keys())) if chop_sequence not in self._chopped_gsas_dict[run_number]: raise RuntimeError( 'Chopped (out) sequence {} is not in loaded chapped run {}. Existing sequences are {}.' 'Chopped runs are {}' ''.format(chop_sequence, run_number, self._chopped_gsas_dict[run_number].keys(), self._chopped_gsas_dict.keys())) return self._chopped_gsas_dict[run_number][chop_sequence]
def load_setting_integer(qsettings, param_name, default_value): """ load setting as an integer :param qsettings: :param param_name: :param default_value: :return: """ # check assert isinstance(qsettings, QtCore.QSettings), 'Input settings must be a QSetting instance but not {0}.' \ ''.format(type(qsettings)) datatypeutility.check_int_variable('Default integer setting', default_value, (None, None)) int_value_str = qsettings.value(param_name, default_value) if isinstance(int_value_str, QVariant): int_value = int_value_str.toInt() else: # case as Unicode print('[DB...BAT] QSetting {}: {} is of type {}'.format( param_name, int_value_str, type(int_value_str))) try: int_value = int(str(int_value_str)) except (TypeError, ValueError): int_value = None # use default if int_value is None: int_value = default_value return int_value
def locate_chopped_nexus(ipts_number, run_number, chop_child_list): """ get the default directory for chopped NeXus file from SNS archive :exception: RuntimeError if unable to find the directory :param ipts_number: :param run_number: a list of string (i.e., files) :param chop_child_list: a list of chopped child :return: """ # TODO/ISSUE/NOWNOW - Apply chop_child_list to this method! - NEED A SOLID USE CASE! datatypeutility.check_int_variable('Run number', run_number, (1, None)) datatypeutility.check_int_variable('IPTS number', ipts_number, (1, None)) # form the directory name chop_dir = '/SNS/VULCAN/IPTS-{0}/shared/ChoppedData/{1}'.format( ipts_number, run_number) # scan the directory if os.path.exists(chop_dir) is False: raise RuntimeError( 'Directory for chopped NeXus file {0} does not exist.'.format( chop_dir)) # from os import listdir # from os.path import isfile, join nexus_file_list = [ f for f in os.listdir(chop_dir) if f.endswith('.nxs') and os.path.isfile(os.path.join(chop_dir, f)) ] return nexus_file_list
def __init__(self, run_number, nxs_file_name): """ Initialization :return: """ # Check input's validity datatypeutility.check_int_variable('Run number', run_number, (0, None)) datatypeutility.check_file_name(nxs_file_name, check_exist=True, check_writable=False, is_dir=False, note='NeXus file name') # Data structure for log data that is worked on now self._myNeXusFileName = nxs_file_name self._myRunNumber = run_number # workspace name (might be sample log only) self._meta_ws_name = None # name of (meta) workspace self._logNameList = None self._runStartTime = None # splitters self._currSplitterWorkspace = None self._currInfoTableWorkspace = None self._chopSetupDict = dict( ) # key: user-specified tag value: dictionary including everything! # initialization operation self.load_data_file() return
def save_vanadium(self, diff_ws_name, gsas_file_name, ipts_number, van_run_number, sample_log_ws_name): """ Save a WorkspaceGroup which comes from original GSAS workspace :param diff_ws_name: diffraction workspace (group) name :param gsas_file_name: output GSAS file name :param ipts_number: ITPS :param van_run_number: (van) run number :param sample_log_ws_name: workspace containing sample logs (proton charges) :return: """ datatypeutility.check_string_variable( 'Diffraction workspace (group) name', diff_ws_name) datatypeutility.check_file_name(gsas_file_name, False, True, False, 'Smoothed vanadium GSAS file') datatypeutility.check_int_variable('IPTS', ipts_number, (1, None)) datatypeutility.check_string_variable('Sample log workspace name', sample_log_ws_name) # rebin and then write output gsas_bank_buffer_dict = dict() van_ws = mantid_helper.retrieve_workspace(diff_ws_name) num_banks = mantid_helper.get_number_spectra(van_ws) datatypeutility.check_file_name(gsas_file_name, check_exist=False, check_writable=True, is_dir=False, note='Output GSAS file') # TODO - TONIGHT 5 - This will break if input is a Workspace but not GroupingWorkspace!!! for ws_index in range(num_banks): # get value bank_id = ws_index + 1 # write GSAS head considering vanadium tof_vector = None ws_name_i = van_ws[ws_index].name() gsas_section_i = self._write_slog_bank_gsas( ws_name_i, 1, tof_vector, None) gsas_bank_buffer_dict[bank_id] = gsas_section_i # END-FOR # header log_ws = mantid_helper.retrieve_workspace(sample_log_ws_name) gsas_header = self._generate_vulcan_gda_header(log_ws, gsas_file_name, ipts_number, van_run_number, gsas_file_name, False) # form to a big string gsas_buffer = gsas_header for bank_id in sorted(gsas_bank_buffer_dict.keys()): gsas_buffer += gsas_bank_buffer_dict[bank_id] # write to HDD g_file = open(gsas_file_name, 'w') g_file.write(gsas_buffer) g_file.close() return
def show_n_th_boundary(self, n): """ Show n-th boundary by adding an indicator :return: """ boundary_points = self.ui.graphicsView_main.get_cycle_boundaries() datatypeutility.check_int_variable('N-th boundaries points', n, (0, len(boundary_points))) n_th_time = boundary_points[n] return n_th_time
def get_vulcan_chopped_gsas_dir(ipts_number, run_number): """ get the directory where the chopped GSAS files are :param ipts_number: :param run_number: :return: directory under ..../shared/ """ datatypeutility.check_int_variable('IPTS number', ipts_number, (1, 999999)) datatypeutility.check_int_variable('Run number', run_number, (1, 99999999)) return '/SNS/VULCAN/IPTS-{0}/shared/binned_data/{1}/'.format( ipts_number, run_number)
def __init__(self, chopped_data_dir, run_number): """ initialization :param run_number: self._reductionSetup.get_run_number() """ datatypeutility.check_file_name(chopped_data_dir, check_writable=True, is_dir=True, note='Directory to store sliced log records') datatypeutility.check_int_variable('Run number', run_number, (1, None)) self._choppedDataDirectory = chopped_data_dir self._run_number = run_number return
def __init__(self, ipts_number, run_number, chop_index, norm_pc, van_run): """ constructor for plotting information :param ipts_number: IPTS number :param run_number: run number :param chop_index: chop sequence index :param norm_pc: flag to set the current proton charge normalization state :param van_run: flag to set the vanadium run normalization state """ # check datatypeutility.check_int_variable('IPTS number', ipts_number, (1, 9999999)) datatypeutility.check_int_variable('Run number', run_number, (1, 999999999)) if chop_index is not None: datatypeutility.check_int_variable('Chop index', chop_index, (0, None)) datatypeutility.check_bool_variable( 'Flag for proton charge normalization', norm_pc) if van_run is not None: datatypeutility.check_int_variable('Vanadium run number', van_run, (1, 99999999)) self._ipts_number = ipts_number self._run_number = run_number self._chop_index = chop_index self._norm_by_pc = norm_pc # normalized by proton charge self._vanadium_run_number = van_run return
def locate_sliced_h5_logs(ipts_number, run_number): """ get the file names of the sliced logs in HDF5 format Example: [wzz@analysis-node09 172282]$ cat summary.txt 0 /SNS/VULCAN/IPTS-22862/shared/pyvdrive_only/172282/1.hdf5 1.gda 1 /SNS/VULCAN/IPTS-22862/shared/pyvdrive_only/172282/2.hdf5 2.gda ... ... :param ipts_number: :param run_number: :return: list of 2-tuple (log h5 name, gsas file name) """ datatypeutility.check_int_variable('Run number', run_number, (1, None)) datatypeutility.check_int_variable('IPTS number', ipts_number, (1, None)) archived_h5_dir = os.path.join( '/SNS/VULCAN/', 'IPTS-{}/shared/pyvdrive_only/{}/' ''.format(ipts_number, run_number)) if not os.path.exists(archived_h5_dir): raise RuntimeError( 'Unable to locate hdf5 logs directory {}'.format( archived_h5_dir)) summary_name = os.path.join(archived_h5_dir, 'summary.txt') if not os.path.exists(summary_name): raise RuntimeError('Unable to locate {} for sliced logs\' summary') # parse sliced log file log_file_names_tuple = list() summary_file = open(summary_name, 'r') raw_lines = summary_file.readlines() summary_file.close() for line in raw_lines: line = line.strip() if line == '' or line[0] == '#': continue # ignore empty line and comment line items = line.split() if len(items) < 3: continue # invalid line else: log_h5_i = items[1] gsas_i = items[2] log_file_names_tuple.append((log_h5_i, gsas_i)) # END-FOR return log_file_names_tuple
def set_vanadium_info(self, ipts_number, van_run_number): """ Set the information about vanadium run :param ipts_number: :param van_run_number: :return: """ datatypeutility.check_int_variable('IPTS number', ipts_number, (1, 99999)) datatypeutility.check_int_variable('Vanadium run number', van_run_number, (1, 9999999)) self._ipts_number = ipts_number self._run_number = van_run_number return
def plot_strip_peak_vanadium(self, bank_id): """ Plot the vanadium spectrum with peak striped :param bank_id: :return: """ datatypeutility.check_int_variable('Bank ID', bank_id, (1, 100)) # get vector X and Y vec_x, vec_y = self._myController.project.vanadium_processing_manager.get_peak_striped_data( bank_id) self._remove_smoothed_line() self._remove_striped_peaks_line() self._remove_raw_tof_line() # for resizing vec_x_list = [vec_x] vec_y_list = [vec_y] # original dspacing if self._raw_van_dspace_line is None: # plot original van raw_vec_x, raw_vec_y = self.plot_raw_dspace(bank_id) vec_x_list.append(raw_vec_x) vec_y_list.append(raw_vec_y) # plot v-line self._no_peak_van_line = self.ui.graphicsView_main.add_plot_1d( vec_x, vec_y, color='red', label='Bank {} Peak Striped'.format(bank_id), x_label='dSpacing') # reset X Y limit self._reset_figure_range(vec_x_list, vec_y_list) # set unit self._curr_unit = UNIT['d'] self._curr_bank = bank_id return
def locate_event_nexus(self, ipts_number, run_number): """ get the raw event NEXUS file from archive :param ipts_number: :param run_number: :return: File name or None (if proposed path does not exist) """ datatypeutility.check_int_variable('Run number', run_number, (1, None)) datatypeutility.check_int_variable('IPTS number', ipts_number, (1, None)) # by default, it shall be nED data: build the path for the nED NeXus file base_name = 'VULCAN_{0}.nxs.h5'.format(run_number) sns_path = os.path.join(self._archiveRootDirectory, 'IPTS-{0}/nexus'.format(ipts_number)) raw_event_file_name = os.path.join(sns_path, base_name) if not os.path.exists(raw_event_file_name): # might be a pre-nED base_name = 'VULCAN_{0}_event.nxs'.format(run_number) sub_path = os.path.join( 'IPTS-{0}/0/{1}/NeXus'.format(ipts_number, run_number), base_name) raw_event_file_name_h5 = raw_event_file_name raw_event_file_name = os.path.join(self._archiveRootDirectory, sub_path) else: raw_event_file_name_h5 = '<Logic Error>' # END-IF # early return if os.path.exists(raw_event_file_name): # add the dictionary self._runIptsDict[run_number] = ipts_number else: # return for nothing raw_event_file_name = None print('[INFO] For IPTS-{} Run-{}, neither {} nor {} exists.' ''.format(ipts_number, run_number, raw_event_file_name_h5, raw_event_file_name)) # END-IF-ELSE return raw_event_file_name
def plot_smoothed_peak_vanadium(self, bank_id, with_raw=True, label=''): """ Plot vanadium spectrum after vanadium peak removed and smoothed :param bank_id: :return: """ datatypeutility.check_int_variable('Bank ID', bank_id, (1, 100)) # remove the previously plot smoothed vanadium line self._remove_smoothed_line() self._remove_striped_peaks_line() self._remove_raw_dspace_line() self.show_hide_v_peaks( show_v_peaks=False) # hide vanadium peaks indicators # get vector X and Y vec_x, vec_y = self._myController.project.vanadium_processing_manager.get_peak_smoothed_data( bank_id) vec_x_list = [vec_x] vec_y_list = [vec_y] if with_raw: vec_raw_x, vec_raw_y = self.plot_raw_tof(bank_id) vec_x_list.append(vec_raw_x) vec_y_list.append(vec_raw_y) self._smoothed_van_line = self.ui.graphicsView_main.add_plot_1d( vec_x, vec_y, color='red', x_label='TOF', label='Bank {} {}'.format(bank_id, label)) # # reset X Y limit self._reset_figure_range(vec_x_list, vec_y_list) # set unit self._curr_unit = UNIT['tof'] self._curr_bank = bank_id return
def load_auto_record(self, ipts_number, record_type): """ load auto record file :except RuntimeError if there is no IPTS in auto record :param ipts_number: :param record_type: None for AutoRecord.txt, 'data' for AutoRecordData.txt', 'align' for AutoRecordAlign.txt :return: """ # check input datatypeutility.check_int_variable('IPTS number', ipts_number, (1, None)) if record_type is not None: datatypeutility.check_string_variable( 'Log type', record_type, allowed_values=['data', 'align']) # locate IPTS folder and AutoRecord file ipts_shared_dir = '/SNS/VULCAN/IPTS-{}/shared'.format(ipts_number) if os.path.exists(ipts_shared_dir) is False: raise RuntimeError( 'IPTS {} has no directory {} in SNS archive'.format( ipts_number, ipts_shared_dir)) if record_type is None: base_name = 'AutoRecord.txt' elif record_type == 'data': base_name = 'AutoRecordData.txt' elif record_type == 'align': base_name = 'AutoRecordAlign.txt' else: raise NotImplementedError('Impossible to reach this point') auto_record_file_name = os.path.join(ipts_shared_dir, base_name) if not os.path.exists(auto_record_file_name): raise RuntimeError('Auto {} record file {} does not exist.'.format( record_type, auto_record_file_name)) # load and parse the file record_key = 'Auto{}-IPTS{}'.format(record_type, ipts_number) self._auto_record_dict[record_key] = vulcan_util.import_vulcan_log( auto_record_file_name) return record_key
def scan_detector_column(ipts, run_number): """ integrate the counts along column of (high angle) detector :param ipts: :param base_run: :param target_run: :return: """ try: datatypeutility.check_int_variable('Run Number', run_number, (1, None)) except AssertionError as ass_err: return False, 'Input arguments error: {}'.format(ass_err) try: collimator = Collimator() collimator.execute_calculate_2theta_intensity(ipts, run_number) except RuntimeError as run_err: return False, 'Execution error: {}'.format(run_err) return True, collimator
def locate_run(ipts, run_number, base_path='/SNS/VULCAN/'): """ check whether a run exists Note: this is renamed from locateRun() :param ipts: :param run_number: :param base_path: :return: boolean (found it or not), file name """ datatypeutility.check_int_variable('IPTS number', ipts, (1, None)) datatypeutility.check_int_variable('Run number', run_number, (1, None)) datatypeutility.check_file_name(base_path, check_exist=True, is_dir=True) err_msg = '' # build the name according to the convention # there could be 2 possibilities # new nED first rel_full_path = 'IPTS-{0}/nexus/VULCAN_{1}.nxs.h5'.format(ipts, run_number) full_path = os.path.join(base_path, rel_full_path) if os.path.exists(full_path): # nED file_name = full_path else: # pre-nED relpathname = "IPTS-%d/0/%d/NeXus/VULCAN_%d_event.nxs" % (ipts, run_number, run_number) nxsfilename = os.path.join(base_path, relpathname) if os.path.exists(nxsfilename): file_name = full_path else: file_name = None err_msg = 'IPTS {} Run {} does not exist. Neither {} nor {} is a valid path.' \ ''.format(full_path, nxsfilename, ipts, run_number) # END-IF status = file_name is not None if not status: return status, err_msg return True, file_name
def locate_gsas(ipts_number, run_number): """ get the path of GSAS file stored on archive server. If not found, just return None :param ipts_number: :param run_number: :return: list of data or None (cannot find) """ # check datatypeutility.check_int_variable('IPTS number', ipts_number, (1, 99999)) datatypeutility.check_int_variable('Run number', run_number, (1, 9999999)) # locate reduced GSAS file name gsas_file_name = os.path.join( '/SNS/VULCAN/IPTS-%d/shared/binned_data' % ipts_number, '%d.gda' % run_number) if not os.path.exists(gsas_file_name): return None return gsas_file_name
def init_session(self, workspace_name, ipts_number, van_run_number, out_gsas_name, sample_log_ws_name): """ Initialize vanadium processing session :param workspace_name: :param ipts_number: :param van_run_number: :param out_gsas_name: :param sample_log_ws_name: required for proton charge :return: """ datatypeutility.check_string_variable('Workspace name', workspace_name) datatypeutility.check_int_variable('IPTS number', ipts_number, (1, 99999)) datatypeutility.check_int_variable('Vanadium run number', van_run_number, (1, 999999)) datatypeutility.check_file_name(out_gsas_name, False, True, False, 'Output GSAS file name') datatypeutility.check_string_variable('Sample log workspace name', sample_log_ws_name) workspace = mantid_helper.retrieve_workspace(workspace_name) if workspace.id() == 'WorkspaceGroup': pass else: # create dictionary and etc raise NotImplementedError('Need to implement single workspace case to extract spectra') self._van_workspace_name = workspace_name self._ipts_number = ipts_number self._van_run_number = van_run_number self._output_gsas_name = out_gsas_name # parameter set up self._is_shift_case = False # convert to point data as a request mantid_helper.convert_to_point_data(self._van_workspace_name) mantid_helper.mtd_convert_units(self._van_workspace_name, 'dSpacig') self._sample_log_ws_name = sample_log_ws_name return
def locate_chopped_gsas(self, ipts_number, run_number, chop_seq): """ get chopped data from GSAS file stored in archive server :param ipts_number: :param run_number: :param chop_seq: :return: None or GSAS name """ datatypeutility.check_int_variable('IPTS number', ipts_number, (1, None)) datatypeutility.check_int_variable('Run number', run_number, (1, None)) datatypeutility.check_int_variable('Chop sequence number', chop_seq, (1, 100000)) # it could be in either .../ChoppedData/... or in .../binned_data/ chop_data_dir_1 = '/SNS/VULCAN/IPTS-{0}/shared/binned_data/'.format( ipts_number) chop_data_dir_2 = '/SNS/VULCAN/IPTS-%d/shared/ChoppedData/' % ipts_number # search GSAS file (seq.gda) under given directories chop_gsas_name = None for parent_dir in [chop_data_dir_1, chop_data_dir_2]: chop_dir = os.path.join(parent_dir, '%d' % run_number) chop_gsas_name = os.path.join(chop_dir, '%d.gda' % chop_seq) if os.path.exists(chop_gsas_name): break # END-FOR # TODO - TONIGHT - Create a data structure to record searched GSAS return chop_gsas_name
def __init__(self, number_banks, focus_instrument_dict, num_threads=24, output_dir=None): """ initialization :param number_banks: number of banks to focus to :param focus_instrument_dict: dictionary of parameter for instrument :param output_dir: :param num_threads: """ datatypeutility.check_int_variable('Number of banks', number_banks, [1, None]) datatypeutility.check_int_variable('Number of threads', num_threads, [1, 256]) datatypeutility.check_dict('Focused instrument dictionary', focus_instrument_dict) # other directories self._output_dir = '/tmp/' if output_dir is not None: datatypeutility.check_file_name( output_dir, True, True, True, 'Output directory for generated GSAS') self._output_dir = output_dir # run number (current or coming) self._run_number = 0 self._ws_name_dict = dict() self._last_loaded_event_ws = None self._last_loaded_ref_id = '' self._det_eff_ws_name = None self._number_banks = number_banks self._focus_instrument_dict = focus_instrument_dict # self._init_focused_instruments() # multiple threading variables self._number_threads = num_threads # dictionary for gsas content (multiple threading) self._gsas_buffer_dict = dict() return
def construct_workspace_name(file_name, file_type, prefix, max_int): """ Construct a standard workspace for loaded binned data (gsas/processed nexus) :param file_name: :param file_type: :param prefix: :param max_int: :return: """ # check inputs datatypeutility.check_string_variable('File name', file_name) datatypeutility.check_string_variable('File type', file_type) datatypeutility.check_string_variable('Workspace prefix', prefix) datatypeutility.check_int_variable( 'Maximum integer for file sequence number', max_int, (10, None)) base_ws_name = os.path.basename(file_name).split('.')[0] hash_part = hash(os.path.basename(file_name)) # add zeros for better sorting if base_ws_name.isdigit(): # add number of zeros in front num_zeros = int(math.log(max_int) / math.log(10)) + 1 if num_zeros < 1: num_zeros = 1 base_ws_name = '{0:0{1}}'.format(int(base_ws_name), num_zeros) if prefix != '': data_ws_name = '{}_{}'.format(prefix, base_ws_name) else: data_ws_name = base_ws_name if file_type == '': raise RuntimeError('File type cannot be empty string') else: data_ws_name = '{}_{}{}'.format(data_ws_name, file_type[0], hash_part) return data_ws_name
def get_raw_data(self, bank_id, unit): """ Get raw data :param bank_id: :param unit: :return: """ if self._van_workspace_name is None: raise RuntimeError('Vanadium workspace has not been set up yet!') datatypeutility.check_int_variable('Bank ID', bank_id, (1, 99)) datatypeutility.check_string_variable('Unit', unit, ['TOF', 'dSpacing']) if unit == 'TOF': if self._van_workspace_name_tof is None: self._van_workspace_name_tof = self._van_workspace_name + '_tof' mantid_helper.mtd_convert_units( self._van_workspace_name, 'TOF', self._van_workspace_name_tof) workspace = mantid_helper.retrieve_workspace(self._van_workspace_name_tof) else: workspace = mantid_helper.retrieve_workspace(self._van_workspace_name) return workspace[bank_id-1].readX(0), workspace[bank_id-1].readY(0)
def get_default_binned_directory(ipts_number, check_write_and_throw=False, merge_flag=False, run_number=None): """ get VDRIVE default directory for binned data :param ipts_number: IPTS number in integer :param check_write_and_throw: check whether the user has write permission and thus throw if not :return: """ # check assert isinstance(ipts_number, int), 'IPTS number {0} must be an integer but not a {1}.' \ ''.format(ipts_number, type(ipts_number)) # get directory binned_dir = '/SNS/VULCAN/IPTS-{0}/shared/binned_data/'.format(ipts_number) # create merged run directory if merge_flag: datatypeutility.check_int_variable('Run number', run_number, (1, 9999999)) binned_dir = os.path.join(binned_dir, '{}'.format(run_number)) if not os.path.exists(binned_dir): try: os.mkdir(binned_dir) # default mode is 777\ except OSError as os_err: raise RuntimeError('Default output directory {} cannot be made due to {}' ''.format(binned_dir, os_err)) # END-IF # END-IF # check write permission if check_write_and_throw: if os.path.exists(binned_dir) is False: raise RuntimeError( 'VULCAN binned data directory {0} does not exist.'.format(binned_dir)) if os.access(binned_dir, os.W_OK) is False: raise RuntimeError('User has no write permission to directory {0}.'.format(binned_dir)) return binned_dir