Commit 900ce538 authored by payno's avatar payno
Browse files

[unit system] create MetricSystem Enum to simplify and allow the from_value

parent a250e7e6
......@@ -38,7 +38,7 @@ import io
from typing import Union
from ..scanbase import TomoScanBase
from .utils import get_parameters_frm_par_or_info, extract_urls_from_edf
from ..unitsystem import metricsystem
from ..unitsystem.metricsystem import MetricSystem
from ..utils import docstring
import logging
......
......@@ -27,40 +27,54 @@ __license__ = "MIT"
__date__ = "01/09/2016"
from typing import Union
from silx.utils.enum import Enum as _Enum
meter = 1.0
m = meter
_meter = 1.0
centimeter = meter / 100.0
cm = centimeter
millimeter = 0.1 * centimeter
mm = millimeter
class MetricSystem(_Enum):
"""Util enum to retrieve metric"""
METER = _meter
m = _meter
CENTIMETER = _meter / 100.0
MILLIMETER = _meter / 1000.0
MICROMETER = _meter * 1e-6
NANOMETER = _meter * 1e-9
@classmethod
def from_value(cls, value):
if isinstance(value, str):
return cls.from_str(value=value)
else:
_Enum.from_value(value=value)
micrometer = 1e-6 * meter
@classmethod
def from_str(cls, value: str):
assert isinstance(value, str)
if value.lower() in ('m', 'meter'):
return MetricSystem.METER
elif value.lower() in ('cm', 'centimeter'):
return MetricSystem.CENTIMETER
elif value.lower() in ('mm', 'millimeter'):
return MetricSystem.MILLIMETER
elif value.lower() in ('micrometer'):
return MetricSystem.MICROMETER
elif value.lower() in ('nm', 'nanometer'):
return MetricSystem.NANOMETER
else:
raise ValueError("Cannot convert: %s" % value)
nanometer = 1e-9 * meter
nm = nanometer
mm2 = millimeter * millimeter
cm2 = centimeter * centimeter
m2 = meter * meter
m = MetricSystem.METER
meter = MetricSystem.METER
centimeter = MetricSystem.CENTIMETER
cm = centimeter
def getUnitName(value: Union[float, int]) -> str:
"""
:return: name in (None, nm, cm, m, mm) from the given unit
"""
if value == nanometer:
return 'nm'
if value == millimeter:
return 'mm'
if value == centimeter:
return 'cm'
if value == meter:
return 'm'
return None
millimeter = MetricSystem.MILLIMETER
mm = MetricSystem.MILLIMETER
micrometer = MetricSystem.MICROMETER
nanometer = MetricSystem.NANOMETER
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