def convert_quantity(q, unit_system=None, to_unit=None, family_name=None):
    if family_name == None:
        family_name = q.family_name

    if to_unit == None:
        unit_system = _get_unit_system(unit_system)
        try:
            to_unit = unit_system.units(family_name)

        except KeyError:
            logger.exception("Could not convert quantity: %s to system: %s" %
                             (q, unit_system))
            return q.clone()

    if q.units == to_unit:
        q = q.clone()

    else:
        data = units_convert(q.data, q.units, to_unit)
        # create a new object of the same type as the input Quantity object
        # could be a subclass, so use __class__
        q = q.__class__(
            data,
            units=to_unit,
            name=q.name or family_name,
            family_name=family_name)
    return q
Example #2
0
def convert_quantity(q, unit_system=None, to_unit=None, family_name=None):
    if family_name is None:
        family_name = q.family_name

    if to_unit is None:
        unit_system = _get_unit_system(unit_system)
        try:
            to_unit = unit_system.units(family_name)

        except KeyError:
            logger.exception(
                "Could not convert quantity: %s to system: %s" %
                (q, unit_system))
            return q.clone()

    if q.units == to_unit:
        q = q.clone()

    else:
        data = units_convert(q.data, q.units, to_unit)
        # create a new object of the same type as the input Quantity object
        # could be a subclass, so use __class__
        q = q.__class__(
            data,
            units=to_unit,
            name=q.name or family_name,
            family_name=family_name)
    return q
Example #3
0
def convert_quantity_old(q, unit_system):
    # I think importing within the function is needed to avoid circularity
    from scimath.units.quantity import Quantity

    try:
        units = unit_system.units(q.family_name)
    except KeyError:
        logger.exception("Could not convert quantity: %s to system: %s" % (q,unit_system))
        return q.clone()

    if q.units == units:
        q = q.clone()

    else:
        data = units_convert(q.data, q.units, units)
        q = Quantity(data, units=units, name=q.name or q.family_name, family_name=q.family_name)
    return q
def convert_unit_array(unit_array,
                       unit_system=None,
                       to_unit=None,
                       family_name=None):
    """ Function to convert the units of a unit_array

        Parameters:
        -----------
        unit_array
            Unit_array to be converted
        unit_system
            the unit system of the current unit_array units, defaults to the
            unit_managers default system (current unit_system):
        to_units
            new_units to convert to
        family_name
            provided in cases where the family_name attribute is not present
            in the provided unit_array object

    """
    from scimath.units.unit_array import UnitArray

    if family_name == None and unit_array.units is not None:
        family_name = _get_family_name_for_array(unit_array.units)

    if unit_array.units is None:
        family_name = None

    if to_unit == None:
        unit_system = _get_unit_system()
        try:
            to_unit = unit_system.units(family_name)
        except KeyError:
            logger.exception("Could not convert UnitArray: %s to system: %s" %\
                             (unit_array, unit_system))
            return unit_array.copy()

    if unit_array.units == to_unit:
        new_array = UnitArray(unit_array, units=unit_array.units)
    else:
        data = units_convert(
            unit_array.view(numpy.ndarray), unit_array.units, to_unit)
        new_array = UnitArray(data, units=to_unit)

    return new_array
def convert_log_index(log_index,
                      unit_system=None,
                      to_unit=None,
                      family_name=None):
    """ Function to convert the units of a LogIndex

        Parameters
        ----------
        log_index
            index to be converted
        unit_system
            the unit system of the current log_index units, defaults to the
            unit_managers default system (current unit_system):
        to_units
            new_units to convert to
        family_name
            provided in cases where the family_name attribute is not present
            in the provided log_index object
        """

    if family_name == None:
        family_name = log_index.family_name or log_index.name

    if to_unit == None:
        unit_system = _get_unit_system(unit_system)
        try:
            to_unit = unit_system.units(family_name)

        except KeyError:
            logger.exception("Could not convert LogIndex: %s to system: %s" % \
                              (log_index, unit_system))
            return log_index.clone()

    if log_index.units == to_unit:
        new_log_index = log_index.clone()
        new_log_index.family_name = family_name  # TODO: not sure if I need this
    else:
        #        print "To_units", to_unit, log_index.units, family_name, unit_system
        data = units_convert(log_index.data, log_index.units, to_unit)
        new_log_index = log_index.clone(data=data)
        new_log_index.family_name = family_name
        new_log_index.units = to_unit

    return new_log_index
Example #6
0
def convert_unit_array(unit_array, unit_system=None, to_unit=None,
                       family_name=None ):
    """ Function to convert the units of a unit_array

        Parameters:
        -----------
        unit_array
            Unit_array to be converted
        unit_system
            the unit system of the current unit_array units, defaults to the
            unit_managers default system (current unit_system):
        to_units
            new_units to convert to
        family_name
            provided in cases where the family_name attribute is not present
            in the provided unit_array object

    """
    from scimath.units.unit_array import UnitArray

    if family_name==None and unit_array.units is not None:
        family_name=_get_family_name_for_array(unit_array.units)

    if unit_array.units is None:
        family_name= None

    if to_unit==None:
        unit_system = _get_unit_system()
        try:
            to_unit = unit_system.units(family_name)
        except KeyError:
            logger.exception("Could not convert UnitArray: %s to system: %s" %\
                             (unit_array, unit_system))
            return unit_array.copy()

    if unit_array.units == to_unit:
        new_array = UnitArray(unit_array, units=unit_array.units)
    else:
        data = units_convert(unit_array.view(numpy.ndarray),
                             unit_array.units, to_unit)
        new_array = UnitArray(data, units=to_unit)

    return new_array
Example #7
0
def convert_log_index(log_index, unit_system=None, to_unit=None, family_name=None):
    """ Function to convert the units of a LogIndex

        Parameters
        ----------
        log_index
            index to be converted
        unit_system
            the unit system of the current log_index units, defaults to the
            unit_managers default system (current unit_system):
        to_units
            new_units to convert to
        family_name
            provided in cases where the family_name attribute is not present
            in the provided log_index object
        """


    if family_name==None:
        family_name=log_index.family_name or log_index.name

    if to_unit==None:
        unit_system = _get_unit_system(unit_system)
        try:
            to_unit = unit_system.units(family_name)

        except KeyError:
            logger.exception("Could not convert LogIndex: %s to system: %s" % \
                              (log_index, unit_system))
            return log_index.clone()

    if log_index.units == to_unit:
        new_log_index = log_index.clone()
        new_log_index.family_name = family_name # TODO: not sure if I need this
    else:
#        print "To_units", to_unit, log_index.units, family_name, unit_system
        data = units_convert(log_index.data, log_index.units, to_unit)
        new_log_index = log_index.clone(data=data)
        new_log_index.family_name = family_name
        new_log_index.units = to_unit

    return new_log_index