from __future__ import division import quantities as pq import numpy as np import inspect # This IPython magic generates a table with version information #https://github.com/jrjohansson/version_information %load_ext version_information %version_information numpy, quantities x = 3 * pq.m y = 5 * pq.foot print x + y x = 3.5 * pq.newton y = 0.7 * pq.joule try: x + y # we get an error if the operation is invalid with this units except Exception as e: print u'*** Attention! ***: ',e x = 0.3 * pq.km # length in Km t = 250 * pq.ms # time in milliseconds accel = x/t**2 mass = 7 * pq.g # mass in grams force = mass * accel print force print force.simplified print force.rescale(pq.newton) print force.rescale(pq.dyne) # Time and distance units print pq.year # time in years print pq.au, pq.au.simplified # distance in astronomical units print pq.light_year, pq.light_year.simplified # distance in light-years print pq.pc, pq.pc.simplified # distance in parsecs # Temperature units print pq.K print pq.celsius # In this example we see that the conversion is not performed temp = 345 * pq.celsius print temp.rescale(pq.K) # Neither is correct the conversion between Farhrenheit and Celsius scales # However, if the value is considered as an increase in temperature, # then, the result is the correct answer temp = 170 * pq.fahrenheit print temp.rescale(pq.celsius) # Measurement of angles print pq.deg print pq.rad print pq.arcmin print pq.arcsec print (np.pi/2 * pq.rad).rescale(pq.deg) print (1 * pq.deg).rescale(pq.arcmin) # Definition of new units Mly = pq.UnitQuantity('megalight_year', 1e6*pq.ly, symbol = 'Mly') Mpc = pq.UnitQuantity('megaparsec', 1e6*pq.pc, symbol = 'Mpc') print (1 * Mpc).rescale(pq.ly) print pq.c, pq.c.simplified # Speed of light print pq.constants.G, pq.constants.G.simplified # Newtonian constant of gravitation # Definition of new constants: earth_radius = 6378160.0 * pq.m moon_radius = 1740000.0 * pq.m sun_radius = 695000000.0 * pq.m earth_mass = 5.97219e24 * pq.kg sun_mass = 332946 * earth_mass Hubble_constant = 67.80 * (pq.km/pq.s)/(1e6*pq.pc) xa = np.arange(10) * pq.N print xa[5] print (xa / (3.0 * pq.kg)).simplified # A single numerical value f = 30 * pq.N print f.item(), type(f.item()) # An array of values print xa.magnitude, type(f.magnitude) for nombre, datos in inspect.getmembers(pq.units, inspect.ismodule): print nombre, ',', for u in dir(pq.mass): print u,',', for nombre, datos in inspect.getmembers(pq.constants, inspect.ismodule): print nombre, ',', dir(pq.constants.astronomy)