Units manipulation examples

In this example, we show how units can be used in SpectroChemPy

import spectrochempy as scp

SpectroChemPy can do calculations with units. It uses [pint](https://pint.readthedocs.io) to define and perform operations on data with units.

Create quantities

To create quantity, use for instance, one of the following expression:

scp.Quantity("10.0 cm^-1")

""
scp.Quantity(1.0, "cm^-1/hour")
1.0 cm-1⋅h-1


Or, more simply, using ur:

ur = scp.ur
10.0 * ur.meter / ur.gram / ur.volt
10.0 m⋅g-1⋅V-1


ur stands for unit registry, which handles many types of units and conversions between them.

Units for dataset

When loading experimental dataset using the read method, units are generally attributed to coordinates and data

ds = scp.read("wodger.spg")[0]
_ = ds.plot()
plot c units
  • wavenumbers (x ) coordinates are here expressed in $cm^{-1}$

  • and data are in absorbance ($a.u.$) units.

Convert between units

Here are some examples

x = 36 * ur("km/hr")
x.to("cm/s")
1000.0 cm⋅s-1


We can make the conversion inplace using ito instead of to

x.ito("m/s")
x
10.0 m⋅s-1


Obviously you cannot convert between incompatible units

try:
    x.to("hour")
except Exception as e:
    print(f"Expected incompatible-unit conversion error: {e}")
Expected incompatible-unit conversion error: Cannot convert from 'meter / second' ([length] / [time]) to 'hour' ([time])

This, of course, also applies to NDDataset. Let’s try for the x coordinate. It is a wavenumber in $cm^{-1}$ that can be transformed to $Hz$, for instance:

ds.x.ito("terahertz")
_ = ds.plot()
plot c units

We can also change wavenumber or frequency units to energy or wavelength, as SpectroChemPy, thanks to [pint](https://pint.readthedocs.io), knows how to make the transformation.

ds.x.ito("eV")
_ = ds.plot()
plot c units
try:
    ds.x.ito("nanometer")
except Exception as e:
    print(f"Expected incompatible coordinate conversion error: {e}")

ds.x = ds.x.to("nanometer")
ds.x
Coord [x:wavelength] — float64, size: 5549, nm
size
:
5549
title
:
wavelength
coordinates
:
[ 1667 1667 ... 1.536e+04 1.538e+04] nm


_ = ds.plot()
plot c units

absorbance units (the units of the data) can also be transformed into transmittance

ds.ito("transmittance")
_ = ds.plot()
plot c units

Back to absorbance.

ds.ito("absorbance")
ds.x.ito("cm^-1")
_ = ds.plot()
plot c units

This ends the example ! The following line can be uncommented if no plot shows when running the .py script with python

# scp.show()

Total running time of the script: (0 minutes 0.766 seconds)