File: instrument_checker.py

package info (click to toggle)
python-mcstasscript 0.0.46%2Bgit20250402111921.bfa5a26-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,440 kB
  • sloc: python: 13,421; makefile: 14
file content (74 lines) | stat: -rw-r--r-- 2,136 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def has_component(instrument, component_name=None, component_type=None):
    """
    Checks if given instrument has a component with given name and type

    instrument : McStasScript instrument object

    component_name : (str) name of component

    component_type : (str) type of component, for example Arm
    """
    if component_name is None and component_type is None:
        raise ValueError("Specify component_name, component_type or both.")

    # Run through components in instrument
    for comp in instrument.component_list:
        if component_name is not None:
            if comp.name != component_name:
                continue

        if component_type is not None:
            if comp.component_name != component_type:
                continue

        # Found case where both component name and type matches
        return True

    # Did not find any such component
    return False


def has_parameter(instrument, parameter_name, parameter_type=None):
    """
    Checks if instrument have parameter with given name and type

    instrument : McStasScript instrument object

    parameter_name : (str) Name of parameter

    parameter_type : (str) Type of parameter, "" treated as double
    """
    if parameter_name is None and parameter_type is None:
        raise ValueError("Specify parameter_name, parameter_type or both.")

    parameter_dict = instrument.parameters.parameters

    if parameter_name not in parameter_dict:
        return False

    if parameter_type is not None:
        found_type = parameter_dict[parameter_name].type

        if parameter_type == "double" and found_type == "":
            # Default McStas type is double
            return True

        if found_type != parameter_type:
            return False

    return True


def all_parameters_set(instrument):
    """
    Checks if all parameters of given instrument have default values

    instrument : McStasScript instrument object
    """
    parameter_dict = instrument.parameters.parameters

    for par_object in parameter_dict.values():
        if par_object.value is None:
            return False

    return True