source: sasview/src/sas/plottools/convert_units.py @ 3477478

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 3477478 was 3477478, checked in by Mathieu Doucet <doucetm@…>, 9 years ago

pylint fixes

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