Esempio n. 1
0
    def _handleWind(self, d):
        """
        Parse the wind and variable-wind groups.

        The following attributes are set:
            wind_dir           [direction]
            wind_speed         [speed]
            wind_gust          [speed]
            wind_dir_from      [int]
            wind_dir_to        [int]
        """
        wind_dir = d['dir'].replace('O', '0')
        if wind_dir != "VRB" and wind_dir != "///" and wind_dir != "MMM":
            self.wind_dir = direction(wind_dir)
        wind_speed = d['speed'].replace('O', '0')
        units = d['units']
        if units == 'KTS' or units == 'K' or units == 'T' or units == 'LT':
            units = 'KT'
        if wind_speed.startswith("P"):
            self.wind_speed = speed(wind_speed[1:], units, ">")
        elif not MISSING_RE.match(wind_speed):
            self.wind_speed = speed(wind_speed, units)
        if d['gust']:
            wind_gust = d['gust']
            if wind_gust.startswith("P"):
                self.wind_gust = speed(wind_gust[1:], units, ">")
            elif not MISSING_RE.match(wind_gust):
                self.wind_gust = speed(wind_gust, units)
        if d['varfrom']:
            self.wind_dir_from = direction(d['varfrom'])
            self.wind_dir_to = direction(d['varto'])
Esempio n. 2
0
    def _handleWind( self, d ):
        """
        Parse the wind and variable-wind groups.

        The following attributes are set:
            wind_dir           [direction]
            wind_speed         [speed]
            wind_gust          [speed]
            wind_dir_from      [int]
            wind_dir_to        [int]
        """
        wind_dir = d['dir'].replace('O','0')
        if wind_dir != "VRB" and wind_dir != "///" and wind_dir != "MMM":
            self.wind_dir = direction(wind_dir)
        wind_speed = d['speed'].replace('O','0')
        units = d['units']
        if units == 'KTS' or units == 'K' or units == 'T' or units == 'LT':
            units = 'KT'
        if wind_speed.startswith("P"):
            self.wind_speed = speed(wind_speed[1:], units, ">")
        elif not MISSING_RE.match(wind_speed):
            self.wind_speed = speed(wind_speed, units)
        if d['gust']:
            wind_gust = d['gust']
            if wind_gust.startswith("P"):
                self.wind_gust = speed(wind_gust[1:], units, ">")
            elif not MISSING_RE.match(wind_gust):
                self.wind_gust = speed(wind_gust, units)
        if d['varfrom']:
            self.wind_dir_from = direction(d['varfrom'])
            self.wind_dir_to = direction(d['varto'])
Esempio n. 3
0
 def testErrorChecking(self):
   self.assertRaises( ValueError, speed, "10KT" )
   self.assertRaises( ValueError, speed, "10", "MPS", "=" )
   self.assertRaises( ValueError, speed, "60", "KT", "gt" )
   self.assertRaises( UnitsError, speed, "10", "NM" )
   self.assertRaises( UnitsError, speed("10").value, "furlongs per fortnight" )
   self.assertRaises( UnitsError, speed("5").string, "fps" )
Esempio n. 4
0
    def _handleWind(self, d):
        """
        Parse the wind and variable-wind groups.

        The following attributes are set:
            wind_dir           [direction]
            wind_speed         [speed]
            wind_gust          [speed]
            wind_dir_from      [int]
            wind_dir_to        [int]
        """
        wind_dir = d["dir"].replace("O", "0")
        if wind_dir != "VRB" and wind_dir != "///" and wind_dir != "MMM":
            self.wind_dir = direction(wind_dir)
        wind_speed = d["speed"].replace("O", "0")
        units = d["units"]
        if units == "KTS" or units == "K" or units == "T" or units == "LT":
            units = "KT"
        if wind_speed.startswith("P"):
            self.wind_speed = speed(wind_speed[1:], units, ">")
        elif not MISSING_RE.match(wind_speed):
            self.wind_speed = speed(wind_speed, units)
        if d["gust"]:
            wind_gust = d["gust"]
            if wind_gust.startswith("P"):
                self.wind_gust = speed(wind_gust[1:], units, ">")
            elif not MISSING_RE.match(wind_gust):
                self.wind_gust = speed(wind_gust, units)
        if d["varfrom"]:
            self.wind_dir_from = direction(d["varfrom"])
            self.wind_dir_to = direction(d["varto"])
Esempio n. 5
0
 def testErrorChecking(self):
     self.assertRaises(ValueError, speed, "10KT")
     self.assertRaises(ValueError, speed, "10", "MPS", "=")
     self.assertRaises(ValueError, speed, "60", "KT", "gt")
     self.assertRaises(UnitsError, speed, "10", "NM")
     self.assertRaises(UnitsError,
                       speed("10").value, "furlongs per fortnight")
     self.assertRaises(UnitsError, speed("5").string, "fps")
Esempio n. 6
0
 def _handlePeakWindRemark(self, d):
     """
     Parse a peak wind remark group.
     """
     peak_dir = int(d["dir"])
     peak_speed = int(d["speed"])
     self.wind_speed_peak = speed(peak_speed, "KT")
     self.wind_dir_peak = direction(peak_dir)
     peak_min = int(d["min"])
     if d["hour"]:
         peak_hour = int(d["hour"])
     else:
         peak_hour = self._hour
     self.peak_wind_time = datetime.datetime(
         self._year, self._month, self._day, peak_hour, peak_min
     )
     if self.peak_wind_time > self.time:
         if peak_hour > self._hour:
             self.peak_wind_time -= datetime.timedelta(hours=24)
         else:
             self.peak_wind_time -= datetime.timedelta(hours=1)
     self._remarks.append(
         "peak wind %dkt from %d degrees at %d:%02d"
         % (peak_speed, peak_dir, peak_hour, peak_min)
     )
Esempio n. 7
0
 def testDefaults(self):
     self.assertEqual(speed("10").value(), 10.0)
     self.assertEqual(speed("5").string(), "5 mps")
     self.assertEqual(speed("10", "KT").value(), 10.0)
     self.assertEqual(speed("5", "KT").string(), "5 knots")
     self.assertEqual(speed("5", "KMH").string(), "5 km/h")
     self.assertEqual(speed("5", "MPH").string(), "5 mph")
     self.assertEqual(speed("5", None).string(), "5 mps")
Esempio n. 8
0
 def testDefaults(self):
   self.assertEqual( speed("10").value(), 10.0 )
   self.assertEqual( speed("5").string(), "5 mps" )
   self.assertEqual( speed("10", "KT").value(), 10.0 )
   self.assertEqual( speed("5", "KT").string(), "5 knots" )
   self.assertEqual( speed("5", "KMH").string(), "5 km/h" )
   self.assertEqual( speed("5", "MPH").string(), "5 mph" )
   self.assertEqual( speed("5",None).string(), "5 mps" )
Esempio n. 9
0
def test_defaults():
    """Test basic usage."""
    assert speed("10").value() == 10.0
    assert speed("5").string() == "5 mps"
    assert speed("10", "KT").value() == 10.0
    assert speed("5", "KT").string() == "5 knots"
    assert speed("5", "KMH").string() == "5 km/h"
    assert speed("5", "MPH").string() == "5 mph"
    assert speed("5", None).string() == "5 mps"
Esempio n. 10
0
 def testInputs(self):
   self.assertEqual( speed("10").value(), 10.0 )
   self.assertEqual( speed(10).value(), 10.0 )
   self.assertEqual( speed(10.0).value(), 10.0 )
   self.assertEqual( speed(10.0, None).value(), 10.0 )
   self.assertEqual( speed("10", gtlt=">").value(), 10.0 )
   self.assertEqual( speed("10", None, "<").value(), 10.0 )
Esempio n. 11
0
 def testInputs(self):
     self.assertEqual(speed("10").value(), 10.0)
     self.assertEqual(speed(10).value(), 10.0)
     self.assertEqual(speed(10.0).value(), 10.0)
     self.assertEqual(speed(10.0, None).value(), 10.0)
     self.assertEqual(speed("10", gtlt=">").value(), 10.0)
     self.assertEqual(speed("10", None, "<").value(), 10.0)
Esempio n. 12
0
    def _handleWind(self, d):
        """
        Parse the wind and variable-wind groups.

        The following attributes are set:
            wind_dir           [direction]
            wind_speed         [speed]
            wind_gust          [speed]
            wind_dir_from      [int]
            wind_dir_to        [int]
        """
        wind_dir = d["dir"].replace("O", "0")
        if wind_dir != "VRB" and wind_dir != "///" and wind_dir != "MMM":
            self.wind_dir = direction(wind_dir)
        wind_speed = d["speed"].replace("O", "0")
        units = d["units"]
        # Ambiguous METAR when no wind speed units are provided
        if units is None and self.station_id is not None:
            # Assume US METAR sites are reporting in KT
            if len(self.station_id) == 3 or self.station_id.startswith("K"):
                units = "KT"
        # If units are still None, default to MPS
        if units is None:
            units = "MPS"
        if units == "KTS" or units == "K" or units == "T" or units == "LT":
            units = "KT"
        if wind_speed.startswith("P"):
            self.wind_speed = speed(wind_speed[1:], units, ">")
        elif not MISSING_RE.match(wind_speed):
            self.wind_speed = speed(wind_speed, units)
        if d["gust"]:
            wind_gust = d["gust"]
            if wind_gust.startswith("P"):
                self.wind_gust = speed(wind_gust[1:], units, ">")
            elif not MISSING_RE.match(wind_gust):
                self.wind_gust = speed(wind_gust, units)
        if d["varfrom"]:
            self.wind_dir_from = direction(d["varfrom"])
            self.wind_dir_to = direction(d["varto"])
Esempio n. 13
0
def test_inputs():
    """Test inputs."""
    assert speed("10").value() == 10.0
    assert speed(10).value() == 10.0
    assert speed(10.0).value() == 10.0
    assert speed(10.0, None).value() == 10.0
    assert speed("10", gtlt=">").value() == 10.0
    assert speed("10", None, "<").value() == 10.0
Esempio n. 14
0
 def _handlePeakWindRemark( self, d ):
     """
     Parse a peak wind remark group.
     """
     peak_dir = int(d['dir'])
     peak_speed = int(d['speed'])
     self.wind_speed_peak = speed(peak_speed, "KT")
     self.wind_dir_peak = direction(peak_dir)
     peak_min = int(d['min'])
     if d['hour']:
         peak_hour = int(d['hour'])
     else:
         peak_hour = self._hour
     self.peak_wind_time = datetime.datetime(self._year, self._month, self._day,
                                                                 peak_hour, peak_min)
     if self.peak_wind_time > self.time:
         if peak_hour > self._hour:
             self.peak_wind_time -= datetime.timedelta(hours=24)
         else:
             self.peak_wind_time -= datetime.timedelta(hours=1)
     self._remarks.append("peak wind %dkt from %d degrees at %d:%02d" % \
                                             (peak_speed, peak_dir, peak_hour, peak_min))
Esempio n. 15
0
 def testConversions(self):
   self.assertEqual( speed("10", "MPS").value("MPS"), 10.0 )
   self.assertEqual( speed("10", "MPS").value("KMH"), 36.0 )
   self.assertAlmostEqual( speed("10", "MPS").value("MPH"), 22.4, 1 )
   self.assertAlmostEqual( speed("10", "MPS").value("KT"), 19.4, 1 )
   
   self.assertEqual( speed("10", "KT").value("KT"), 10.0 )
   self.assertAlmostEqual( speed("10", "KT").value("MPH"), 11.5, 1 )
   self.assertAlmostEqual( speed("10", "KT").value("MPS"), 5.1, 1 )
   self.assertAlmostEqual( speed("10", "KT").value("KMH"), 18.5, 1 )
   
   self.assertEqual( speed("10", "MPH").value("MPH"), 10.0 )
   self.assertAlmostEqual( speed("10", "MPH").value("KT"), 8.7, 1 )
   self.assertAlmostEqual( speed("10", "MPH").value("MPS"), 4.5, 1 )
   self.assertAlmostEqual( speed("10", "MPH").value("KMH"), 16.1, 1 )
   
   self.assertEqual( speed("10", "KMH").value("KMH"), 10.0 )
   self.assertAlmostEqual( speed("10", "KMH").value("KT"), 5.4, 1 )
   self.assertAlmostEqual( speed("10", "KMH").value("MPS"), 2.8, 1 )
   self.assertAlmostEqual( speed("10", "KMH").value("MPH"), 6.2, 1 )
Esempio n. 16
0
def test_conversions():
    """Test unit conversions."""
    assert speed("10", "MPS").value("MPS") == 10.0
    assert speed("10", "MPS").value("KMH") == 36.0
    assert abs(speed("10", "MPS").value("MPH") - 22.4) < 0.1
    assert abs(speed("10", "MPS").value("KT") - 19.4) < 0.1

    assert speed("10", "KT").value("KT") == 10.0
    assert abs(speed("10", "KT").value("MPH") - 11.5) < 0.1
    assert abs(speed("10", "KT").value("MPS") - 5.1) < 0.1
    assert abs(speed("10", "KT").value("KMH") - 18.5) < 0.1

    assert speed("10", "MPH").value("MPH") == 10.0
    assert abs(speed("10", "MPH").value("KT") - 8.7) < 0.1
    assert abs(speed("10", "MPH").value("MPS") - 4.5) < 0.1
    assert abs(speed("10", "MPH").value("KMH") - 16.1) < 0.1

    assert speed("10", "KMH").value("KMH") == 10.0
    assert abs(speed("10", "KMH").value("KT") - 5.4) < 0.1
    assert abs(speed("10", "KMH").value("MPS") - 2.8) < 0.1
    assert abs(speed("10", "KMH").value("MPH") - 6.2) < 0.1
Esempio n. 17
0
def test_error_checking():
    """Test exception raising."""
    with pytest.raises(ValueError):
        speed("10KT")
    with pytest.raises(ValueError):
        speed("10", "MPS", "=")
    with pytest.raises(ValueError):
        speed("60", "KT", "gt")
    with pytest.raises(UnitsError):
        speed("10", "NM")
    with pytest.raises(UnitsError):
        speed(speed("10").value, "furlongs per fortnight")
    with pytest.raises(UnitsError):
        speed(speed("5").string, "fps")
Esempio n. 18
0
    def testConversions(self):
        self.assertEqual(speed("10", "MPS").value("MPS"), 10.0)
        self.assertEqual(speed("10", "MPS").value("KMH"), 36.0)
        self.assertAlmostEqual(speed("10", "MPS").value("MPH"), 22.4, 1)
        self.assertAlmostEqual(speed("10", "MPS").value("KT"), 19.4, 1)

        self.assertEqual(speed("10", "KT").value("KT"), 10.0)
        self.assertAlmostEqual(speed("10", "KT").value("MPH"), 11.5, 1)
        self.assertAlmostEqual(speed("10", "KT").value("MPS"), 5.1, 1)
        self.assertAlmostEqual(speed("10", "KT").value("KMH"), 18.5, 1)

        self.assertEqual(speed("10", "MPH").value("MPH"), 10.0)
        self.assertAlmostEqual(speed("10", "MPH").value("KT"), 8.7, 1)
        self.assertAlmostEqual(speed("10", "MPH").value("MPS"), 4.5, 1)
        self.assertAlmostEqual(speed("10", "MPH").value("KMH"), 16.1, 1)

        self.assertEqual(speed("10", "KMH").value("KMH"), 10.0)
        self.assertAlmostEqual(speed("10", "KMH").value("KT"), 5.4, 1)
        self.assertAlmostEqual(speed("10", "KMH").value("MPS"), 2.8, 1)
        self.assertAlmostEqual(speed("10", "KMH").value("MPH"), 6.2, 1)