def __init__(self, name, mass=1.0, mass_unit=None, height=None, diameter=None, length_unit=None): """ :param name: str - name of the sample. :param mass: float - mass of the sample. If not kg then please specify the mass_unit. It is stored in kg. :param mass_unit: str - has to be specified in order to calculate the sample mass properly. :param height: float - sample height - stored in 'm' :param diameter: float - sample diameter - stored in 'm' :param length_unit: str - if not 'm' please specify """ self.name = name self.log = logging.getLogger('RockPy.SAMPLE') self.log.info('CREATING\t new sample << %s >>' % self.name) self.measurements = [] if mass_unit: mass_factor = convert.convert2(mass_unit, 'kg', 'mass') else: self.log.info(' MISSING\t length_unit: assuming << mm >>') mass_factor = convert.convert2('mg', 'kg', 'mass') if mass: self.mass_kg = mass * mass_factor self.log.debug(' ADDING\t << mass >> input: %.1f [%s] stored: %1f [kg]' % (mass, mass_unit, self.mass_kg)) else: self.log.debug('MISSING\t << mass >>') self.mass_kg = None # get length _unit for conversion if length_unit: length_factor = convert.convert2(length_unit, 'm', 'length') else: self.log.info(' MISSING\t mass_unit: assuming << mg >>') length_factor = convert.convert2('mm', 'm', 'length') if height: self.height_m = float(height) * length_factor self.log.debug( ' ADDING\t << height >> input: %.1f [%s] stored: %1f [m]' % (height, length_unit, self.height_m)) else: self.log.debug('MISSING\t << height >>') self.height_m = None if diameter: self.diameter_m = float(diameter) * length_factor self.log.debug( ' ADDING\t << diameter >> input: %.1f [%s] stored: %1f [m]' % (diameter, length_unit, self.diameter_m)) else: self.log.debug('MISSING\t << diameter >>') self.diameter_m = None
def mass(self, mass_unit='mg'): ''' Returns mass in specified mass unit :param mass_unit: str - unit to be output :return: float - mass in mass_unit ''' OUT = self.mass_kg * convert.convert2('kg', mass_unit, 'mass') return OUT
def add_height(self, height, length_unit='mm'): ''' Adds height in m to sample :param height: float :param length_unit: str changes sample.height_m from its initial value to a new value. Length unit should be given, if other than 'mm'. See convert2 helper function #todo point to helper ''' self.height_m = float(height) * convert.convert2(length_unit, 'm', 'length') self.log.debug(' ADDING\t << height >> input: %.1f [%s] stored: %1f [m]' % (height, length_unit, self.height_m))
def add_mass(self, mass, mass_unit='mg'): ''' Adds mass to the sample object :param mass: float :param mass_unit: str changes sample.mass_kg from its initial value to a new value. mass unit should be given, if other than 'mg'. See convert2 helper function #todo point to helper ''' self.mass_kg = float(mass) * convert.convert2(mass_unit, 'kg', 'mass') self.log.debug(' ADDING\t << mass >> input: %.1f [%s] stored: %1f [kg]' % (mass, mass_unit, self.mass_kg))
def add_diameter(self, diameter, length_unit='mm'): ''' Adds diameter in m to sample :param diameter: float :param length_unit: str changes sample.diameter_m from its initial value to a new value. Length unit should be given, if other than 'mm'. See convert2 helper function #todo point to helper ''' self.diameter_m = float(diameter) * convert.convert2(length_unit, 'm', 'length') self.log.debug( ' ADDING\t << diameter >> input: %.1f [%s] stored: %1f [m]' % (diameter, length_unit, self.diameter_m))
def diameter(self, length_unit='mm'): OUT = self.diameter_m * convert.convert2('m', length_unit, 'length') return OUT
def height(self, length_unit='mm'): OUT = self.height_m * convert.convert2('m', length_unit, 'length') return OUT