示例#1
0
 def test_bad_arguments(self):
     with self.assertRaises(TypeError):
         calculate_abs_humidity(pressure='a',
                                temperature=25,
                                rel_humidity=110)
     with self.assertRaises(TypeError):
         calculate_abs_humidity(pressure=975,
                                temperature='a',
                                rel_humidity=110)
     with self.assertRaises(TypeError):
         calculate_abs_humidity(pressure=975,
                                temperature=25,
                                rel_humidity='a')
示例#2
0
    def test(self):
        test_pressure = [100, 800, 850, 900, 950, 1000, 1050, 1100]
        test_temp = [-10, 0, 10, 15, 20, 25, 40, 60]
        test_rel = [80, 10, 20, 90, 40, 50, 43.25, 12.34]

        correct_values = [0.0018930155819513275,
                          0.00048681001461818693,
                          0.0018843546921809028,
                          0.011566932891098143,
                          0.006927846890531939,
                          0.011536889704637605,
                          0.022155411985739303,
                          0.01612715205125449]

        for i_test, correct_value in enumerate(correct_values):
            result = calculate_abs_humidity(pressure=test_pressure[i_test],
                                            temperature=test_temp[i_test],
                                            rel_humidity=test_rel[i_test])
            self.assertLess(abs(result - correct_value), 1e-6)
示例#3
0
    def get_humidity(self, relative=True):
        """Get a humidity reading.

        Fetches a humidity reading from the sensor. The value can be the
        relative humidity (`relative=True`), with values between 0 and 100.
        Alternatively, it returns the absolute humidity in kg / m^3.

        Args:
            relative (bool): indicate relative (instead of absolute) humidity

        Returns:
            float: the current relative/absolute humidity
        """
        data = self.get_data()

        if relative:
            return data['humidity']

        return calculate_abs_humidity(pressure=data['pressure'],
                                      temperature=data['temperature'],
                                      rel_humidity=data['humidity'])
示例#4
0
    def print_data(self, temp_unit='C', relative_humidity=True,
                   pressure_unit='hPa', n_significant_digits=4):
        """Print sensor data.

        Prints the temperature, humidity, and pressure in a easy readable
        format. The user can specify the temperature unit (e.g. "C") via
        `temp_unit`, the pressure unit (e.g. "hPa") via `pressure_unit`,
        and whether to use absolute or relative humidity via
        `relative_humidity`.

        Args:
            temp_unit (str): the unit the temperature should be in (C/F/K)
            relative_humidity (bool): relative instead of absolute humidity
            pressure_unit (str): pressure unit (Pa/hPa/kPa/atm/mmHg)
            n_significant_digits (int): number of significant digits for values

        Returns:
            None: values are printed, not returned.
        """
        data = self.get_data()
        temperature = convert_temperature(data['temperature'], temp_unit)
        pressure = convert_pressure(data['pressure'], pressure_unit)
        humidity = data['humidity']
        humidity_unit = '%'
        if not relative_humidity:
            humidity = calculate_abs_humidity(pressure=data['pressure'],
                                              temperature=data['temperature'],
                                              rel_humidity=data['humidity'])
            humidity_unit = "kg / m^3"

        # round to n significant digits
        temperature = round_to_n_significant_digits(temperature,
                                                    n_significant_digits)
        humidity = round_to_n_significant_digits(humidity,
                                                 n_significant_digits)
        pressure = round_to_n_significant_digits(pressure,
                                                 n_significant_digits)
        print("Temperature: ", temperature, temp_unit)
        print("Humidity:    ", humidity, humidity_unit)
        print("Pressure:    ", pressure, pressure_unit)
示例#5
0
 def test_valid_range(self):
     with self.assertRaises(ValueError):
         # pressure too low
         calculate_abs_humidity(pressure=0,
                                temperature=25,
                                rel_humidity=50)
     with self.assertRaises(ValueError):
         # pressure too high
         calculate_abs_humidity(pressure=1111,
                                temperature=25,
                                rel_humidity=50)
     with self.assertRaises(ValueError):
         # rel humidity too low
         calculate_abs_humidity(pressure=975,
                                temperature=25,
                                rel_humidity=-1)
     with self.assertRaises(ValueError):
         # rel humidity too high
         calculate_abs_humidity(pressure=975,
                                temperature=25,
                                rel_humidity=110)
     with self.assertRaises(ValueError):
         # temp too low
         calculate_abs_humidity(pressure=975,
                                temperature=-101,
                                rel_humidity=50)
     with self.assertRaises(ValueError):
         # temp too high
         calculate_abs_humidity(pressure=975,
                                temperature=101,
                                rel_humidity=50)