def _generate_output_file_name(vanadium_path, sample_path, instrument, bank): """ Generate an output filename in the form INSTRUMENT_VanadiumRunNo_SampleRunNo_BANKS :param vanadium_path: Path to vanadium data file :param sample_path: Path to sample data file :param instrument: The instrument in use. :param bank: The bank being saved. :return: The filename, the vanadium run number, and sample run number. """ vanadium_no = path_handling.get_run_number_from_path( vanadium_path, instrument) sample_no = path_handling.get_run_number_from_path( sample_path, instrument) filename = instrument + "_" + vanadium_no + "_" + sample_no + "_" if bank == "all": filename = filename + "all_banks.prm" elif bank == "north": filename = filename + "bank_North.prm" elif bank == "south": filename = filename + "bank_South.prm" elif bank == "cropped": filename = filename + "cropped.prm" else: raise ValueError("Invalid bank name entered") return filename
def create_output_files(self, calibration_dir, difa, difc, tzero, sample_path, vanadium_path, instrument, bank, spectrum_numbers): """ Create output files from the algorithms in the specified directory :param calibration_dir: The directory to save the files into. :param difa: DIFA values from calibration algorithm :param difc: DIFC values from the calibration algorithm. :param tzero: TZERO values from the calibration algorithm. :param sample_path: The path to the sample data file. :param vanadium_path: The path to the vanadium data file. :param instrument: The instrument (ENGINX or IMAT) :param bank: Optional parameter to crop by bank :param spectrum_numbers: Optional parameter to crop using spectrum numbers. """ kwargs = { "ceria_run": path_handling.get_run_number_from_path(sample_path, instrument), "vanadium_run": path_handling.get_run_number_from_path(vanadium_path, instrument) } def south_kwargs(): kwargs["template_file"] = SOUTH_BANK_TEMPLATE_FILE kwargs["bank_names"] = ["South"] def north_kwargs(): kwargs["template_file"] = NORTH_BANK_TEMPLATE_FILE kwargs["bank_names"] = ["North"] def generate_output_file(difa_list, difc_list, tzero_list, bank_name, kwargs_to_pass): file_path = calibration_dir + self._generate_output_file_name( vanadium_path, sample_path, instrument, bank=bank_name) write_ENGINX_GSAS_iparam_file(file_path, difa_list, difc_list, tzero_list, **kwargs_to_pass) if not path.exists(calibration_dir): makedirs(calibration_dir) if bank is None and spectrum_numbers is None: generate_output_file(difa, difc, tzero, "all", kwargs) north_kwargs() generate_output_file([difa[0]], [difc[0]], [tzero[0]], "north", kwargs) south_kwargs() generate_output_file([difa[1]], [difc[1]], [tzero[1]], "south", kwargs) elif bank == "1": north_kwargs() generate_output_file([difa[0]], [difc[0]], [tzero[0]], "north", kwargs) elif bank == "2": south_kwargs() generate_output_file([difa[0]], [difc[0]], [tzero[0]], "south", kwargs) elif bank is None: # Custom cropped files use the north bank template. north_kwargs() generate_output_file([difa[0]], [difc[0]], [tzero[0]], "cropped", kwargs)
def fetch_correction_workspaces(vanadium_path, instrument, rb_num=""): """ Fetch workspaces from the file system or create new ones. :param vanadium_path: The path to the requested vanadium run raw data. :param instrument: The instrument the data came from. :param rb_num: A user identifier, usually an experiment number. :return: The resultant integration and curves workspaces. """ vanadium_number = path_handling.get_run_number_from_path(vanadium_path, instrument) integ_path, curves_path = _generate_saved_workspace_file_paths(vanadium_number) force_recalc = get_setting(path_handling.INTERFACES_SETTINGS_GROUP, path_handling.ENGINEERING_PREFIX, "recalc_vanadium", return_type=bool) if path.exists(curves_path) and path.exists(integ_path) and not force_recalc: # Check if the cached files exist. try: integ_workspace = Load(Filename=integ_path, OutputWorkspace=INTEGRATED_WORKSPACE_NAME) curves_workspace = Load(Filename=curves_path, OutputWorkspace=CURVES_WORKSPACE_NAME) if rb_num: user_integ, user_curves = _generate_saved_workspace_file_paths(vanadium_number, rb_num=rb_num) if not path.exists(user_integ) and not path.exists(user_curves): _save_correction_files(integ_workspace, user_integ, curves_workspace, user_curves) return integ_workspace, curves_workspace except RuntimeError as e: logger.error( "Problem loading existing vanadium calculations. Creating new files. Description: " + str(e)) integ_workspace, curves_workspace = _calculate_vanadium_correction(vanadium_path) _save_correction_files(integ_workspace, integ_path, curves_workspace, curves_path) if rb_num: user_integ, user_curves = _generate_saved_workspace_file_paths(vanadium_number, rb_num=rb_num) _save_correction_files(integ_workspace, user_integ, curves_workspace, user_curves) return integ_workspace, curves_workspace
def focus_run(self, sample_paths, banks, plot_output, instrument, rb_num, spectrum_numbers): """ Focus some data using the current calibration. :param sample_paths: The paths to the data to be focused. :param banks: The banks that should be focused. :param plot_output: True if the output should be plotted. :param instrument: The instrument that the data came from. :param rb_num: The experiment number, used to create directories. Can be None :param spectrum_numbers: The specific spectra that should be focused. Used instead of banks. """ if not Ads.doesExist(vanadium_corrections.INTEGRATED_WORKSPACE_NAME ) and not Ads.doesExist( vanadium_corrections.CURVES_WORKSPACE_NAME): return integration_workspace = Ads.retrieve( vanadium_corrections.INTEGRATED_WORKSPACE_NAME) curves_workspace = Ads.retrieve( vanadium_corrections.CURVES_WORKSPACE_NAME) output_workspaces = [] # List of collated workspaces to plot. full_calib_path = get_setting(path_handling.INTERFACES_SETTINGS_GROUP, path_handling.ENGINEERING_PREFIX, "full_calibration") if full_calib_path is not None and path.exists(full_calib_path): full_calib_workspace = LoadAscii(full_calib_path, OutputWorkspace="det_pos", Separator="Tab") else: full_calib_workspace = None if spectrum_numbers is None: for sample_path in sample_paths: sample_workspace = path_handling.load_workspace(sample_path) run_no = path_handling.get_run_number_from_path( sample_path, instrument) workspaces_for_run = [] for name in banks: output_workspace_name = str( run_no) + "_" + FOCUSED_OUTPUT_WORKSPACE_NAME + str( name) self._run_focus(sample_workspace, output_workspace_name, integration_workspace, curves_workspace, name, full_calib_workspace) workspaces_for_run.append(output_workspace_name) # Save the output to the file system. self._save_output(instrument, sample_path, name, output_workspace_name, rb_num) output_workspaces.append(workspaces_for_run) else: for sample_path in sample_paths: sample_workspace = path_handling.load_workspace(sample_path) run_no = path_handling.get_run_number_from_path( sample_path, instrument) output_workspace_name = str( run_no) + "_" + FOCUSED_OUTPUT_WORKSPACE_NAME + "cropped" self._run_focus(sample_workspace, output_workspace_name, integration_workspace, curves_workspace, None, full_calib_workspace, spectrum_numbers) output_workspaces.append([output_workspace_name]) self._save_output(instrument, sample_path, "cropped", output_workspace_name, rb_num) # Plot the output if plot_output: for ws_names in output_workspaces: self._plot_focused_workspaces(ws_names)
def _generate_output_file_name(instrument, sample_path, bank, suffix): run_no = path_handling.get_run_number_from_path( sample_path, instrument) return instrument + "_" + run_no + "_bank_" + bank + suffix