source: sasview/src/sas/plottools/unitConverter.py @ 36c5910

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 36c5910 was 79492222, checked in by krzywon, 9 years ago

Changed the file and folder names to remove all SANS references.

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