load_run_model.py 3.05 KB
Newer Older
Matthew Andrew's avatar
Matthew Andrew committed
1
2
3
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
#   NScD Oak Ridge National Laboratory, European Spallation Source,
#   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
Matthew Andrew's avatar
Matthew Andrew committed
6
# SPDX - License - Identifier: GPL - 3.0 +
T Jubb's avatar
T Jubb committed
7
from Muon.GUI.Common.muon_load_data import MuonLoadData
Matthew Andrew's avatar
Matthew Andrew committed
8
import Muon.GUI.Common.utilities.load_utils as load_utils
T Jubb's avatar
T Jubb committed
9
10
11
12
13


class LoadRunWidgetModel(object):
    """Stores info on all currently loaded workspaces"""

14
    def __init__(self, loaded_data_store=MuonLoadData(), context=None):
T Jubb's avatar
T Jubb committed
15
16
17
18
        # Used with load thread
        self._filenames = []

        self._loaded_data_store = loaded_data_store
19
        self._data_context = context.data_context
T Jubb's avatar
T Jubb committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
        self._current_run = None

    def remove_previous_data(self):
        self._loaded_data_store.remove_last_added_data()

    # Used with load thread
    def loadData(self, filenames):
        self._filenames = filenames

    # Used with load thread
    def execute(self):
        failed_files = []
        for filename in self._filenames:
            try:
34
35
                ws, run, filename, _, period_info = load_utils.load_workspace_from_filename(filename)
                self._data_context.periods_info = period_info
36
            except ValueError as error:
37
                failed_files += [(filename, error)]
T Jubb's avatar
T Jubb committed
38
                continue
39
            self._loaded_data_store.remove_data(run=[run])
40
41
            self._loaded_data_store.add_data(run=[run], workspace=ws, filename=filename,
                                             instrument=self._data_context.instrument)
T Jubb's avatar
T Jubb committed
42
        if failed_files:
43
44
            message = "The requested run could not be found. This could be due to: \n - The run does not yet exist." \
                      "\n - The file was not found locally (please check the user directories)."
T Jubb's avatar
T Jubb committed
45
46
            raise ValueError(message)

47
    # This is needed to work with thread model
T Jubb's avatar
T Jubb committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    def output(self):
        pass

    def cancel(self):
        pass

    def clear_loaded_data(self):
        self._loaded_data_store.clear()

    @property
    def current_run(self):
        return self._current_run

    @current_run.setter
    def current_run(self, run):
        self._current_run = run

    @property
    def loaded_filenames(self):
        return self._loaded_data_store.get_parameter("filename")

    @property
    def loaded_workspaces(self):
        return self._loaded_data_store.get_parameter("workspace")

    @property
    def loaded_runs(self):
        return self._loaded_data_store.get_parameter("run")
Matthew Andrew's avatar
Matthew Andrew committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

    @property
    def instrument(self):
        return self._data_context.instrument

    @property
    def current_runs(self):
        return self._data_context.current_runs

    @current_runs.setter
    def current_runs(self, value):
        self._data_context.current_runs = value

    def get_latest_loaded_run(self):
        return self._loaded_data_store.get_latest_data()['run']

    def get_data(self, run):
        return self._loaded_data_store.get_data(run=run, instrument=self._data_context.instrument)