Commit c0b04ffd authored by Pierre Paleo's avatar Pierre Paleo
Browse files

Add energy expressed in keV

parent ae09c2f1
...@@ -29,9 +29,50 @@ __date__ = "01/09/2016" ...@@ -29,9 +29,50 @@ __date__ = "01/09/2016"
from silx.utils.enum import Enum as _Enum from silx.utils.enum import Enum as _Enum
# Constants
_elementary_charge_coulomb = 1.602176634e-19
_meter = 1.0
_joule_si = 1.0
class EnergySI(_Enum):
"""Util enum for energy in SI units (Joules)"""
JOULE = _joule_si
ELEMCHARGE = _elementary_charge_coulomb
ELECTRONVOLT = _elementary_charge_coulomb
KILOELECTRONVOLT = _elementary_charge_coulomb * 1e3
KILOJOULE = 1e3 * _joule_si
@classmethod
def from_value(cls, value):
if isinstance(value, str):
return cls.from_str(value=value)
else:
_Enum.from_value(value=value)
@classmethod
def from_str(cls, value: str):
assert isinstance(value, str)
if value.lower() in ('e', 'qe'):
return EnergySI.ELEMCHARGE
elif value.lower() in ("j", "joule"):
return EnergySI.JOULE
elif value.lower() in ("kj", "kilojoule"):
return EnergySI.KILOJOULE
elif value.lower() in ("ev", "electronvolt"):
return EnergySI.ELECTRONVOLT
elif value.lower() in ("kev", "kiloelectronvolt"):
return EnergySI.KILOELECTRONVOLT
else:
raise ValueError("Cannot convert: %s" % value)
# Default units:
# - lenght: meter (m)
# - energy: kilo Electronvolt (keV)
_meter = 1.0
_kev = 1.0
_joule_kev = 1./EnergySI.KILOELECTRONVOLT.value
class MetricSystem(_Enum): class MetricSystem(_Enum):
"""Util enum to retrieve metric""" """Util enum to retrieve metric"""
...@@ -42,6 +83,12 @@ class MetricSystem(_Enum): ...@@ -42,6 +83,12 @@ class MetricSystem(_Enum):
MICROMETER = _meter * 1e-6 MICROMETER = _meter * 1e-6
NANOMETER = _meter * 1e-9 NANOMETER = _meter * 1e-9
KILOELECTRONVOLT = _kev
ELECTRONVOLT = _kev * 1e-3
JOULE = _kev / EnergySI.KILOELECTRONVOLT.value
KILOJOULE = _kev / EnergySI.KILOELECTRONVOLT.value * 1e3
@classmethod @classmethod
def from_value(cls, value): def from_value(cls, value):
if isinstance(value, str): if isinstance(value, str):
...@@ -62,6 +109,14 @@ class MetricSystem(_Enum): ...@@ -62,6 +109,14 @@ class MetricSystem(_Enum):
return MetricSystem.MICROMETER return MetricSystem.MICROMETER
elif value.lower() in ('nm', 'nanometer'): elif value.lower() in ('nm', 'nanometer'):
return MetricSystem.NANOMETER return MetricSystem.NANOMETER
elif value.lower() in ('kev', 'kiloelectronvolt'):
return MetricSystem.KILOELECTRONVOLT
elif value.lower() in ('ev', 'electronvolt'):
return MetricSystem.ELECTRONVOLT
elif value.lower() in ('j', 'joule'):
return MetricSystem.JOULE
elif value.lower() in ('kj', 'kilojoule'):
return MetricSystem.KILOJOULE
else: else:
raise ValueError("Cannot convert: %s" % value) raise ValueError("Cannot convert: %s" % value)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment