コード例 #1
0
ファイル: model.py プロジェクト: stuartcampbell/mantid
    def _output_sample_logs(instrument, run_number, van_run_number, workspace,
                            rb_num):
        def write_to_file():
            with open(output_path, "w", newline="") as logfile:
                writer = csv.writer(logfile, ["Sample Log", "Avg Value"])
                for log in output_dict:
                    writer.writerow([log, output_dict[log]])

        output_dict = {}
        sample_run = workspace.getRun()
        log_names = sample_run.keys()
        # Collect numerical sample logs.
        for name in log_names:
            try:
                output_dict[name] = sample_run.getPropertyAsSingleValue(name)
            except ValueError:
                logger.information(
                    f"Could not convert {name} to a numerical value. It will not be included in the "
                    f"sample logs output file.")
        focus_dir = path.join(output_settings.get_output_path(), "Focus")
        if not path.exists(focus_dir):
            makedirs(focus_dir)
        output_path = path.join(focus_dir,
                                (instrument + "_" + run_number + "_" +
                                 van_run_number + "_sample_logs.csv"))
        write_to_file()
        if rb_num:
            focus_user_dir = path.join(output_settings.get_output_path(),
                                       "User", rb_num, "Focus")
            if not path.exists(focus_user_dir):
                makedirs(focus_user_dir)
            output_path = path.join(focus_user_dir,
                                    (instrument + "_" + run_number + "_" +
                                     van_run_number + "_sample_logs.csv"))
            write_to_file()
コード例 #2
0
ファイル: model.py プロジェクト: stuartcampbell/mantid
 def _save_output(self,
                  instrument,
                  sample_run,
                  van_run,
                  bank,
                  sample_workspace,
                  rb_num,
                  unit="TOF"):
     """
     Save a focused workspace to the file system. Saves separate copies to a User directory if an rb number has
     been set.
     :param instrument: The instrument the data is from.
     :param sample_run: The sample run number that was focussed
     :param bank: The name of the bank being saved.
     :param sample_workspace: The name of the workspace to be saved.
     :param rb_num: Usually an experiment id, defines the name of the user directory.
     """
     self._save_focused_output_files_as_nexus(instrument, sample_run,
                                              van_run, bank,
                                              sample_workspace, rb_num,
                                              unit)
     self._save_focused_output_files_as_gss(instrument, sample_run, van_run,
                                            bank, sample_workspace, rb_num,
                                            unit)
     self._save_focused_output_files_as_topas_xye(instrument, sample_run,
                                                  van_run, bank,
                                                  sample_workspace, rb_num,
                                                  unit)
     output_path = path.join(output_settings.get_output_path(), 'Focus')
     logger.notice(f"\n\nFocus files saved to: \"{output_path}\"\n\n")
     if rb_num:
         output_path = path.join(output_settings.get_output_path(), 'User',
                                 rb_num, 'Focus')
         logger.notice(
             f"\n\nFocus files also saved to: \"{output_path}\"\n\n")
コード例 #3
0
ファイル: model.py プロジェクト: stuartcampbell/mantid
 def _save_focused_output_files_as_gss(self, instrument, sample_run,
                                       van_run, bank, sample_workspace,
                                       rb_num, unit):
     gss_output_path = path.join(
         output_settings.get_output_path(), "Focus",
         self._generate_output_file_name(instrument, sample_run, van_run,
                                         bank, unit, ".gss"))
     SaveGSS(InputWorkspace=sample_workspace, Filename=gss_output_path)
     if rb_num:
         gss_output_path = path.join(
             output_settings.get_output_path(), "User", rb_num, "Focus",
             self._generate_output_file_name(instrument, sample_run,
                                             van_run, bank, unit, ".gss"))
         SaveGSS(InputWorkspace=sample_workspace, Filename=gss_output_path)
コード例 #4
0
    def test_last_path_updates_with_no_RB_number(self, addlog, nexus, gss, xye):
        mocked_workspace = "mocked-workspace"
        output_file = path.join(output_settings.get_output_path(), "Focus",
                                "ENGINX_123_456_North_TOF.nxs")

        self.model._save_output("ENGINX", "123", "456", "North",
                                mocked_workspace, None)

        self.assertEqual(self.model._last_focused_files[0], output_file)
コード例 #5
0
ファイル: model.py プロジェクト: stuartcampbell/mantid
 def _save_focused_output_files_as_nexus(self, instrument, sample_run,
                                         van_run, bank, sample_workspace,
                                         rb_num, unit):
     file_name = self._generate_output_file_name(instrument, sample_run,
                                                 van_run, bank, unit,
                                                 ".nxs")
     nexus_output_path = path.join(output_settings.get_output_path(),
                                   "Focus", file_name)
     AddSampleLog(Workspace=sample_workspace,
                  LogName="Vanadium Run",
                  LogText=van_run)
     SaveNexus(InputWorkspace=sample_workspace, Filename=nexus_output_path)
     if rb_num:
         nexus_output_path = path.join(output_settings.get_output_path(),
                                       "User", rb_num, "Focus", file_name)
         SaveNexus(InputWorkspace=sample_workspace,
                   Filename=nexus_output_path)
     if unit == "TOF":
         self._last_focused_files.append(nexus_output_path)
コード例 #6
0
    def test_save_output_files_with_no_RB_number(self, addlog, nexus, gss, xye):
        mocked_workspace = "mocked-workspace"
        output_file = path.join(output_settings.get_output_path(), "Focus",
                                "ENGINX_123_456_North_TOF.nxs")

        self.model._save_output("ENGINX", "123", "456", "North",
                                mocked_workspace, None)

        self.assertEqual(1, nexus.call_count)
        self.assertEqual(1, gss.call_count)
        self.assertEqual(1, xye.call_count)
        nexus.assert_called_with(Filename=output_file, InputWorkspace=mocked_workspace)
コード例 #7
0
ファイル: model.py プロジェクト: stuartcampbell/mantid
 def _save_focused_output_files_as_topas_xye(self, instrument, sample_run,
                                             van_run, bank,
                                             sample_workspace, rb_num,
                                             unit):
     xye_output_path = path.join(
         output_settings.get_output_path(), "Focus",
         self._generate_output_file_name(instrument, sample_run, van_run,
                                         bank, unit, ".abc"))
     SaveFocusedXYE(InputWorkspace=sample_workspace,
                    Filename=xye_output_path,
                    SplitFiles=False,
                    Format="TOPAS")
     if rb_num:
         xye_output_path = path.join(
             output_settings.get_output_path(), "User", rb_num, "Focus",
             self._generate_output_file_name(instrument, sample_run,
                                             van_run, bank, unit, ".abc"))
         SaveFocusedXYE(InputWorkspace=sample_workspace,
                        Filename=xye_output_path,
                        SplitFiles=False,
                        Format="TOPAS")
コード例 #8
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)