source: sasview/guitools/unitConverter.py @ c87d55a

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 c87d55a was 43bf807, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Fixed labels in PlotPanel? and minor mod for sqrt units in unitConverter.

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