def _unit_conversion_factor(self, diff=True, resample='min', target='default'): """ Return a conversion factor to convert the obtained data The method starts from the unit of the sensor, and takes into account sampling, differentiation (if any) and target unit. For gas, a default calorific value of 10 kWh/liter is used. For some units, unit conversion does not apply, and 1.0 is returned. Parameters ---------- diff : True (default) or False If True, the original data has been differentiated resample : str (default='min') Sampling rate, if any. Use 'raw' if no resampling. target : str , default='default' String representation of the target unit, eg m3/h, kW, ... If None, 1.0 is returned Returns ------- cf : float Multiplication factor for the original data to the target unit """ # get the target if target == 'default': target = self._get_default_unit(diff=diff, resample=resample) if target is None: return 1.0 if resample == 'raw': if diff: raise NotImplementedError("Differentiation always needs a sampled dataframe") # get the source if not self.type == 'gas': if not diff: source = self.unit else: # differentiation. Careful, this is a hack of the unit system. # we have to take care manually of some corner cases if self.unit: source = self.unit + '/' + resample else: source = self.unit return misc.unit_conversion_factor(source, target) else: # for gas, we need to take into account the calorific value # as of now, we use 10 kWh/l by default CALORIFICVALUE = 10 q_src = 1 * ureg(self.unit) q_int = q_src * ureg('Wh/liter') if not diff: source = str(q_int.units) # string representing the unit, mostly kWh else: source = str(q_int.units) + '/' + resample return CALORIFICVALUE * misc.unit_conversion_factor(source, target)
def unit_conversion_factor(source, target): """ Shorthand function to get a conversion factor for unit conversion. Parameters ---------- source, target : str Unit as string, should be parsable by pint Returns ------- cf : float Conversion factor. Multiply the source value with this factor to get the target value. Works only for factorial conversion! """ return 1 * ureg(source).to(target).magnitude