DMelt:Units/1 Unit conversions

From HandWiki
Revision as of 11:17, 14 February 2021 by imported>Jworkorg
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Member


Unit conversion

DataMelt can be used to easily convert between different units of measurement. You can use it in a GUI mode or as a Java classes, which can be accessed via any scripting language supported by the program.

A GUI mode

Simply start DataMelt IDE and select [Tools]->[Unit conversion]. You will see a GUI windows where you can sent different conversion units. The program is based on the JConvert program. This window is shown below:

Jconvert.png

Conversions in Java/Jython

Alternatively, you can do conversions inside Java/Jython code. Use the UConverter UConverter class.

Here is an example:

from jhplot import *
u=UConverter()                    # initialize converter
print u.toString()                # check measure types
c=u.getConversionType("Distance") # conversion type
print c.getAllFromUnits()         # check all units for this measure
print "1 mile = ", c.convert(1.0, "mile", "meter") , " meters" 
print "1 mile = " + c.convertFraction("1", "mile", "meter"), " meters";

The code, among other things, will print the correct answer:

1 mile =  1609.344  meters
1 mile = 1609 43/125  meters

The conversion tables are rather complete. In total, there are 24 measures, such as Flow, Volume, Power etc. and more than 3,000 possible conversion combinations.

Let us consider more sophisticated example with some more explanation:

from jhplot import *
u=UConverter()     # initialize converter
print u.toString() # check measure types

The last line print all possible conversions:

Flow - 1156 conversions
Volume Dry - 36 conversions
Volume - 400 conversions
Power - 100 conversions
Distance - 398 conversions
Density - 64 conversions
Area - 100 conversions
Temperature - 11 conversions
Velocity - 324 conversions
Computer - 169 conversions
Concentration - 169 conversions
Acceleration - 49 conversions
Amt of Substance - 16 conversions
Force - 64 conversions
Angle - 81 conversions
Time - 289 conversions
Mass - 256 conversions
Energy - 841 conversions
Torque - 169 conversions
Light - 16 conversions
Pressure - 441 conversions

Now let us continue with this example:

from jhplot import *
u=UConverter()     # initialize converter
print u.getTypes() # this prints list of all available units 

# we selected "Distance" measure
c=u.getConversionType("Distance") # get conversion type
print c.getAllFromUnits()         # here we print all conversion units
print "1 mile = ", c.convert(1.0, "mile", "meter") , " meters" 
print "1 mile = " + c.convertFraction("1", "mile", "meter"), " meters";


You can also setup a custom conversion tables:

from  com.edsdev.jconvert.domain import Conversion
conversion = Conversion.createInstance("Celsius", "C", "Fahrenheit", "F", "9/5", 32);

print "Test1: Check: 17 Celsius is 62.6 Fahrenheit"
print conversion.convertValue(17, conversion.getFromUnit())

print "Test2: 62.6 Fahrenheit is 17.0 Celsius"
print conversion.convertValue(62.6, "Fahrenheit", "Celsius")

The answer is:

Test1: Check: 17 Celsius is 62.6 Fahrenheit
62.6
Test2: 62.6 Fahrenheit is 17.0 Celsius
17.0