Esempio n. 1
0
    def units(self,unt):
        # Setup unit and num unit
        if isinstance(unt,(u.Unit,u.CompositeUnit)):
            self._units = unt.to_string()
            self._num_unit = unt
        elif isinstance(unt,(str)):
            if unt in pint_units.keys():
                self._units = unt
                self._num_unit = pint_units[unt]
            else:
                self._units = unt
                self._num_unit = u.Unit(self._units)
        elif unt is None:
            self._units = unt
            self._num_unit = unt

        else:
            raise ValueError('Units can only take string, astropy units or None')

        # Check if this is the first time set units
        if hasattr(self,'value'):
            wmsg = 'Parameter '+self.name+'units has been reset to '+unt
            log.warning(wmsg)
            try:
                if hasattr(self.value,'unit'):
                    temp = self.value.to(self.num_unit)
            except:
                log.warning('The value unit is not compatable with'\
                                 ' parameter units,right now.')
Esempio n. 2
0
    def units(self, unt):
        # Check if this is the first time set units and check compatibility
        if hasattr(self, 'quantity'):
            if self.units is not None:
                if unt != self.units:
                    wmsg = 'Parameter ' + self.name + ' default units has been '
                    wmsg += ' reset to ' + str(unt) + ' from ' + str(
                        self._units)
                    log.warning(wmsg)
                try:
                    if hasattr(self.quantity, 'unit'):
                        _ = self.quantity.to(unt)
                except:
                    log.warning('The value unit is not compatible with'
                                ' parameter units right now.')

        if unt is None:
            self._units = None

        # Always compare a string to pint_units.keys()
        # If search an astropy unit object with a sting list
        # If the string does not match astropy unit, astropy will guess what
        # does the string mean. It will take a lot of time.
        elif isinstance(unt, str) and unt in pint_units.keys():
            # These are special-case unit strings in in PINT
            self._units = pint_units[unt]

        else:
            # Try to use it as an astopy unit.  If this fails,
            # ValueError will be raised.
            self._units = u.Unit(unt)

        if hasattr(self, 'quantity') and hasattr(self.quantity, 'unit'):
            # Change quantity unit to new unit
            self.quantity = self.quantity.to(self._units)
        if hasattr(self, 'uncertainty') and hasattr(self.uncertainty, 'unit'):
            # Change uncertainty unit to new unit
            self.uncertainty = self.uncertainty.to(self._units)
Esempio n. 3
0
    def units(self, unt):
        # Check if this is the first time set units and check compatibility
        if hasattr(self, 'quantity'):
            if self.units is not None:
                if unt != self.units:
                    wmsg = 'Parameter '+self.name+' default units has been '
                    wmsg += ' reset to ' + str(unt) + ' from '+ str(self._units)
                    log.warning(wmsg)
                try:
                    if hasattr(self.quantity, 'unit'):
                        _ = self.quantity.to(unt)
                except:
                    log.warning('The value unit is not compatible with'
                                ' parameter units right now.')


        if unt is None:
            self._units = None

        # Always compare a string to pint_units.keys()
        # If search an astropy unit object with a sting list
        # If the string does not match astropy unit, astropy will guess what
        # does the string mean. It will take a lot of time. 
        elif isinstance(unt, str) and unt in pint_units.keys():
            # These are special-case unit strings in in PINT
            self._units = pint_units[unt]

        else:
            # Try to use it as an astopy unit.  If this fails,
            # ValueError will be raised.
            self._units = u.Unit(unt)

        if hasattr(self, 'quantity') and hasattr(self.quantity, 'unit'):
            # Change quantity unit to new unit
            self.quantity = self.quantity.to(self._units)
        if hasattr(self, 'uncertainty') and hasattr(self.uncertainty, 'unit'):
            # Change uncertainty unit to new unit
            self.uncertainty = self.uncertainty.to(self._units)