Skip to content

Commit

Permalink
Moving custom units out of powermeter class
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaushik Malapati authored and Kaushik Malapati committed Oct 22, 2024
1 parent f88880a commit 1f47c75
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
7 changes: 7 additions & 0 deletions pcdsdevices/custom_units.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sympy.physics.units as units

millijoule = mJ = units.quantities.Quantity("millijoule", abbrev='mJ')
millijoule.set_global_relative_scale_factor(units.prefixes.milli, units.joule)

microjoule = uJ = units.quantities.Quantity("microjoule", abbrev='uJ')
microjoule.set_global_relative_scale_factor(units.prefixes.micro, units.joule)
9 changes: 0 additions & 9 deletions pcdsdevices/pim.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,6 @@ class PPMPowerMeter(BaseInterface, Device):
`calibrated_mj` = (Signal - Background) / (Responsivity * Beam_Rate)
"""
def __init__(self, prefix, *, name, **kwargs):
import sympy.physics.units as units
from sympy.physics.units.prefixes import micro, milli
from sympy.physics.units.quantities import Quantity
millijoule = mJ = Quantity("millijoule", abbrev='mJ')
millijoule.set_global_relative_scale_factor(milli, units.joule)
microjoule = uJ = Quantity("microjoule", abbrev='uJ')
microjoule.set_global_relative_scale_factor(micro, units.joule)
setattr(units, 'mJ', mJ)
setattr(units, 'uJ', uJ)
super().__init__(prefix, name=name, **kwargs)

tab_component_names = True
Expand Down
12 changes: 10 additions & 2 deletions pcdsdevices/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from ophyd.device import Device
from ophyd.ophydobj import Kind

from . import custom_units
from ._html import collapse_list_head, collapse_list_tail
from .type_hints import Number, OphydDataType

Expand Down Expand Up @@ -155,8 +156,15 @@ def convert_unit(value: float, unit: str, new_unit: str):
new_value : float
The starting value, but converted to the new unit.
"""
unit = getattr(units, unit)
new_unit = getattr(units, new_unit)
try:
unit = getattr(units, unit)
except Exception:
unit = getattr(custom_units, unit)

try:
new_unit = getattr(units, new_unit)
except Exception:
new_unit = getattr(custom_units, new_unit)

if unit == new_unit:
return value
Expand Down

0 comments on commit 1f47c75

Please sign in to comment.