source: sasview/src/sas/qtgui/Plotting/ConvertUnits.py @ 75906a1

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 75906a1 was 75906a1, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 5 years ago

More fixes from PK's CR

  • Property mode set to 100644
File size: 3.6 KB
Line 
1"""
2    Convert units to strings that can be displayed
3    This is a cleaned up version of unitConverter.py
4"""
5import re
6
7def convertUnit(power, unit):
8    """
9        Convert units to strings that can be displayed
10    """
11    if power != 0:
12        if unit.find("^") != -1:  # if the unit contains a power ^
13            toks = re.split(r"\^", unit)
14            if toks[0].find("/") != -1 or \
15                toks[0].find("-") != -1:
16                if power == 1:
17                    unit = unit
18                else:
19                    unit = "(" + unit + ")" + "^{" + str(power) + "}"
20            else:
21                if toks[1].find("{") != -1:  # if found a {
22                    find_power_toks = re.split("{", toks[1])
23                    if find_power_toks[1].find("}") != -1:  # found }
24                        unit_toks = re.split("}", find_power_toks[1])
25                        if unit_toks[0].find(".") != -1:
26                            powerer = float(unit_toks[0]) * power
27                        elif unit_toks[0].find("/") != -1:
28                            power_toks = re.split("/", unit_toks[0])
29                            powerer = power * int(power_toks[0])\
30                                            / int(power_toks[1])
31                        else:
32                            powerer = int(unit_toks[0]) * power
33
34                        if powerer == 1.0:
35                            unit = toks[0]
36                        elif powerer == 0.5:
37                            unit = toks[0] + "^{1/2}"
38                        elif powerer == -0.5:
39                            unit = toks[0] + "^{-1/2}"
40                        else:
41                            unit = toks[0] + "^{" + str(powerer) + "}"
42                else:
43                    raise ValueError("missing } in unit expression")
44        else:  # no powerer
45            if  power != 1:
46                unit = "(" + unit + ")" + "^{" + str(power) + "}"
47    else:
48        raise ValueError("empty unit ,enter a powerer different from zero")
49    return unit
50
51
52if __name__ == "__main__":
53    # pylint: disable=invalid-name
54    # Input   ->  new scale  ->  Output
55    unit1 = "A^{-1} "  #             x                    A^{-1}
56    unit2 = "A"  #                   x                     A
57    unit3 = "A"  #                   x^2                  A^{2}
58    unit4 = "A "  #                  1/x                  A^{-1}
59    unit5 = "A^{0.5} "  #        x^2                      A
60    unit9 = "m^{1/2}"  #         x^2               m
61
62    # If you don't recognize the pattern, give up
63    # and just put some parentheses around the unit and write the transoformation:
64
65    unit6 = "m/s"  #                x^2               (m/s)^{2}
66    unit7 = "m/s^{2}"  #         1/x                 (m/s^{2})^{-1}
67    unit8 = "m/s^{4}"  #         x^2               (m/s^{4})^{2}
68
69    print("this unit1 %s ,its powerer %s , and value %s" % (unit1, 1, convert_unit(1, unit1)))
70    print("this unit2 %s ,its powerer %s , and value %s" % (unit2, 1, convert_unit(1, unit2)))
71    print("this unit3 %s ,its powerer %s , and value %s" % (unit3, 2, convert_unit(2, unit3)))
72    print("this unit4 %s ,its powerer %s , and value %s" % (unit4, -1, convert_unit(-1, unit4)))
73    print("this unit5 %s ,its powerer %s , and value %s" % (unit5, 2, convert_unit(2, unit5)))
74    print("this unit6 %s ,its powerer %s , and value %s" % (unit6, 2, convert_unit(2, unit6)))
75    print("this unit7 %s ,its powerer %s , and value %s" % (unit7, -1, convert_unit(-1, unit7)))
76    print("this unit8 %s ,its powerer %s , and value %s" % (unit8, 2, convert_unit(2, unit8)))
77    print("this unit9 %s ,its powerer %s , and value %s" % (unit9, 2, convert_unit(2, unit9)))
78
79
Note: See TracBrowser for help on using the repository browser.