source: sasview/plottools/src/danse/common/plottools/unitConverter.py @ 82a54b8

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 82a54b8 was 82a54b8, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

adding plottools Part 2

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