source: sasview/src/sas/sasgui/plottools/convert_units.py @ 5251ec6

magnetic_scattrelease-4.2.2ticket-1009ticket-1249
Last change on this file since 5251ec6 was 5251ec6, checked in by Paul Kienzle <pkienzle@…>, 5 years ago

improved support for py37 in sasgui

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