示例#1
0
    def create_new_calibration(self,
                               ceria_path,
                               plot_output,
                               instrument,
                               rb_num=None,
                               bank=None,
                               calfile=None,
                               spectrum_numbers=None):
        """
        Create a new calibration from a ceria run
        :param ceria_path: Path to ceria (CeO2) data file
        :param plot_output: Whether the output should be plotted.
        :param instrument: The instrument the data relates to.
        :param rb_num: The RB number for file creation.
        :param bank: Optional parameter to crop by bank
        :param calfile: Optional parameter to crop using a custom calfile
        :param spectrum_numbers: Optional parameter to crop using spectrum numbers.
        """

        ceria_workspace = path_handling.load_workspace(ceria_path)
        if Ads.doesExist("full_inst_calib"):
            full_calib = Ads.retrieve("full_inst_calib")
        else:
            full_calib_path = get_setting(
                output_settings.INTERFACES_SETTINGS_GROUP,
                output_settings.ENGINEERING_PREFIX, "full_calibration")
            try:
                full_calib = Load(full_calib_path,
                                  OutputWorkspace="full_inst_calib")
            except ValueError:
                logger.error(
                    "Error loading Full instrument calibration - this is set in the interface settings."
                )
                return

        cal_params, ceria_raw, grp_ws = self.run_calibration(
            ceria_workspace, bank, calfile, spectrum_numbers, full_calib)
        if plot_output:
            plot_dicts = list()
            if len(cal_params) == 1:
                if calfile:
                    bank_name = "Custom"
                elif spectrum_numbers:
                    bank_name = "Cropped"
                else:
                    bank_name = bank
                plot_dicts.append(
                    EnggUtils.generate_tof_fit_dictionary(bank_name))
                EnggUtils.plot_tof_fit(plot_dicts, [bank_name])
            else:
                plot_dicts.append(
                    EnggUtils.generate_tof_fit_dictionary("bank_1"))
                plot_dicts.append(
                    EnggUtils.generate_tof_fit_dictionary("bank_2"))
                EnggUtils.plot_tof_fit(plot_dicts, ["bank_1", "bank_2"])
        difa = [row['difa'] for row in cal_params]
        difc = [row['difc'] for row in cal_params]
        tzero = [row['tzero'] for row in cal_params]

        bk2bk_params = self.extract_b2b_params(ceria_raw)
        DeleteWorkspace(ceria_raw)

        params_table = []

        for i in range(len(difc)):
            params_table.append([i, difc[i], difa[i], tzero[i]])
        self.update_calibration_params_table(params_table)

        calib_dir = path.join(output_settings.get_output_path(), "Calibration",
                              "")
        if calfile:
            EnggUtils.save_grouping_workspace(grp_ws,
                                              calib_dir,
                                              ceria_path,
                                              instrument,
                                              calfile=calfile)
        elif spectrum_numbers:
            EnggUtils.save_grouping_workspace(grp_ws,
                                              calib_dir,
                                              ceria_path,
                                              instrument,
                                              spec_nos=spectrum_numbers)
        self.create_output_files(calib_dir, difa, difc, tzero, bk2bk_params,
                                 ceria_path, instrument, bank,
                                 spectrum_numbers, calfile)
        if rb_num:
            user_calib_dir = path.join(output_settings.get_output_path(),
                                       "User", rb_num, "Calibration", "")
            self.create_output_files(user_calib_dir, difa, difc, tzero,
                                     bk2bk_params, ceria_path, instrument,
                                     bank, spectrum_numbers, calfile)
示例#2
0
def create_calibration_files(ceria_run, van_run, full_inst_calib, int_van, van_curves_file, calibration_directory,
                             calibration_general, use_spectrum_number, crop_name, spec_nos):
    """
    create and save a calibration

    @param ceria_run :: run number for the ceria used
    @param van_run :: the run number of the vanadium to use
    @param int_van :: name of the integrated vanadium workspace
    @param full_inst_calib :: workspace containing the full instrument calibration
    @param van_curves_file :: path to save vanadium curves to
    @param calibration_directory :: the user specific calibration directory to save to
    @param calibration_general :: the general calibration directory
    @param use_spectrum_number :: whether or not to crop using spectrum numbers or banks
    @param crop_name :: name of the output workspace
    @param spec_nos :: the value to crop on, either a spectra number, or a bank
    """
    van_integrated_ws = load_van_integration_file(int_van)
    ceria_ws = simple.Load(Filename="ENGINX" + ceria_run, OutputWorkspace="engg_calib")
    van_file = _gen_filename(van_run)
    van_ws = simple.Load(Filename=van_file)
    bank_names = ["North", "South"]
    # check which cropping method to use

    if use_spectrum_number:
        spectrum_numbers = spec_nos
        bank = None
    else:
        if spec_nos.lower() == "north" or spec_nos == '1':
            spectrum_numbers = None
            bank = '1'
        elif spec_nos.lower() == "south" or spec_nos == '2':
            spectrum_numbers = None
            bank = '2'
        else:
            spectrum_numbers = None
            bank = None

    output, ceria_raw, curves = run_calibration(sample_ws=ceria_ws,
                                                vanadium_workspace=van_ws,
                                                van_integration=van_integrated_ws,
                                                bank=bank,
                                                spectrum_numbers=spectrum_numbers,
                                                full_inst_calib=full_inst_calib)
    handle_van_curves(curves, van_curves_file)
    bk2bk_params = extract_b2b_params(ceria_raw)
    if len(output) == 1:
        # get the values needed for saving out the .prm files
        difa = [output[0]['difa']]
        difc = [output[0]['difc']]
        tzero = [output[0]['tzero']]
        if bank is None and spectrum_numbers is None:
            save_calibration(ceria_run, calibration_directory, calibration_general, "all_banks", [crop_name],
                             tzero, difc, difa, bk2bk_params)
        if spectrum_numbers is not None:
            save_calibration(ceria_run, calibration_directory, calibration_general,
                             "bank_cropped", [crop_name], tzero, difc, difa, bk2bk_params)
        else:
            save_calibration(ceria_run, calibration_directory, calibration_general,
                             "bank_{}".format(spec_nos), [crop_name], tzero, difc, difa, bk2bk_params)
        # create the table workspace containing the parameters
        param_tbl_name = crop_name if crop_name is not None else "Cropped"
        create_params_table(difc, tzero, difa)
        plot_dict = Utils.generate_tof_fit_dictionary(param_tbl_name)
        Utils.plot_tof_fit([plot_dict], [param_tbl_name])
    else:
        difas = [row['difa'] for row in output]
        difcs = [row['difc'] for row in output]
        tzeros = [row['tzero'] for row in output]
        plot_dicts = list()
        for i in range(1, 3):
            save_calibration(ceria_run, calibration_directory, calibration_general,
                             f"bank_{i}", [bank_names[i - 1]],
                             [tzeros[i - 1]], [difcs[i - 1]], [difas[i - 1]], bk2bk_params)
            plot_dicts.append(Utils.generate_tof_fit_dictionary(f"bank_{i}"))
        save_calibration(ceria_run, calibration_directory, calibration_general, "all_banks", bank_names,
                         tzeros, difcs, difas, bk2bk_params)
        # create the table workspace containing the parameters
        create_params_table(difcs, tzeros, difas)
        Utils.plot_tof_fit(plot_dicts, ["bank_1", "bank_2"])