def set_quantity(self, element, name, value, default_unit=None):
# Import Astropy here to avoid import errors for this module for users without an Astropy installation
from astropy.units import Unit
try:
# If this works, assume it is a Quantity (or Angle)
unit = value.unit
# Works for Angles as well (str(angle) gives something that is not 'value + unit'
to_string = str(value.to(value.unit).value) + " " + str(unit)
except AttributeError:
if default_unit is not None:
to_string = str(value) + " " + str(Unit(default_unit))
else:
to_string = str(value) # dimensionless quantity
# Set the value in the tree element
element.set(name, to_string)
# -----------------------------------------------------------------
评论列表
文章目录