model.py 3.89 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
8
9
import os
from mantid.kernel import ConfigService
from Muon.GUI.Common.muon_load_data import MuonLoadData
Matthew Andrew's avatar
Matthew Andrew committed
10
import Muon.GUI.Common.utilities.load_utils as load_utils
T Jubb's avatar
T Jubb committed
11

Matthew Andrew's avatar
Matthew Andrew committed
12

T Jubb's avatar
T Jubb committed
13
14
class BrowseFileWidgetModel(object):

15
    def __init__(self, loaded_data_store=MuonLoadData(), context=None):
T Jubb's avatar
T Jubb committed
16
17
18
19
        # Temporary list of filenames used for load thread
        self._filenames = []

        self._loaded_data_store = loaded_data_store
20
        self._data_context = context.data_context
T Jubb's avatar
T Jubb committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

    @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")

    # Used with load thread
    def output(self):
        pass

    # Used with load thread
    def cancel(self):
        pass

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

    # Used with load thread
    def execute(self):
        failed_files = []
        for filename in self._filenames:
            try:
51
                ws, run, filename, psi_data, _ = load_utils.load_workspace_from_filename(filename)
52
53
            except Exception as error:
                failed_files += [(filename, error)]
T Jubb's avatar
T Jubb committed
54
                continue
55
            if not psi_data:
56
57
                instrument_from_workspace = ws['OutputWorkspace'][0].workspace.getInstrument().getName()
            else:
58
                # Load another instrument first
59
                instrument_from_workspace = "PSI"
60

61
            self._loaded_data_store.remove_data(run=[run])
62
            self._loaded_data_store.add_data(run=[run], workspace=ws, filename=filename, instrument=instrument_from_workspace)
T Jubb's avatar
T Jubb committed
63
        if failed_files:
64
            message = load_utils.exception_message_for_failed_files(failed_files)
T Jubb's avatar
T Jubb committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
            raise ValueError(message)

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

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

    def get_run_list(self):
        return self.loaded_runs

    def add_directories_to_config_service(self, file_list):
        """
        Parses file_list into the unique directories containing the files, and adds these
        to the global config service. These directories will then be automatically searched in
        all subsequent Load calls.
        """
        dirs = [os.path.dirname(filename) for filename in file_list]
Matthew Andrew's avatar
Matthew Andrew committed
83
        dirs = [path if os.path.isdir(path) else "" for path in dirs]
T Jubb's avatar
T Jubb committed
84
85
86
87
        dirs = list(set(dirs))
        if dirs:
            for directory in dirs:
                ConfigService.Instance().appendDataSearchDir(directory.encode('ascii', 'ignore'))
Matthew Andrew's avatar
Matthew Andrew committed
88
89
90
91
92

    def get_data(self, *args, **kwargs):
        return self._loaded_data_store.get_data(**kwargs)

    def get_instrument_from_latest_run(self):
93
94
95
96
97
        instrument = self._loaded_data_store.get_latest_data()['workspace']['OutputWorkspace'][0].workspace.getInstrument().getName()
        if instrument:
            return instrument
        else:
            return "PSI"
Matthew Andrew's avatar
Matthew Andrew committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

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

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

    @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
114
115
116
117

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