def getmetar(icao): try: icao = str(icao.upper()) metar = MetarSet(icao) metar.refresh() metar = metar.get_latest() metar = (str(metar.raw_text)) return str(metar) except Exception as e: return str(e)
class WeatherSummary(object): station = None def __init__(self, station_id): self.station = WeatherStation(station_id) self.metars = MetarSet(station_id) self.tafs = TafSet(station_id) self.refresh() def refresh(self): self.metars.refresh()
class MetarTests(unittest.TestCase): def setUp(self): with open('tests/mock_KBUF.xml', 'rU') as f: self.mock_response = f.read() self.metar_set = MetarSet('KBUF,KAPA') self.metar_set.refresh(mock_response=self.mock_response) def test_explicit_station_string(self): self.assertEqual(self.metar_set.station_string, "KBUF,KAPA") def test_implicit_station_string(self): metar_set = MetarSet('wrong') metar_set.refresh(mock_response=self.mock_response) for metar in metar_set.report_set: self.assertEqual(metar.station.station_id, "KBUF") def test_get_latest(self): """ Ensures algorithm is getting the Metar with the most recent timestamp. """ latest_metar = self.metar_set.get_latest() print latest_metar.raw_text expected_time = dateutil.parser.parse("2015-02-11T23:03:00Z") self.assertEqual(latest_metar.observation_time, expected_time) def _test_metar_attributes(self, metar): self.assertTrue(isinstance(metar.raw_text, (str, unicode))) self.assertTrue(isinstance(metar.flight_category, (str, unicode))) self.assertTrue(isinstance(metar.observation_time, datetime.datetime)) self.assertTrue(isinstance(metar.station, WeatherStation)) self.assertTrue(isinstance(metar.wind, Wind)) self.assertTrue(isinstance(metar.cloud_layers, CloudLayerSet)) # Floats for key in ['temp', 'dewpoint', 'visibility', 'altimeter']: val = getattr(metar, key) self.assertTrue( isinstance(val, float), "%s should be a float, not a %s" % (key, type(val))) def test_available_attributes(self): for metar in self.metar_set.report_set: self._test_metar_attributes(metar)
class MetarTests(unittest.TestCase): def setUp(self): with open('tests/mock_KBUF.xml', 'rU') as f: self.mock_response = f.read() self.metar_set = MetarSet('KBUF,KAPA') self.metar_set.refresh(mock_response=self.mock_response) def test_explicit_station_string(self): self.assertEqual(self.metar_set.station_string, "KBUF,KAPA") def test_implicit_station_string(self): metar_set = MetarSet('wrong') metar_set.refresh(mock_response=self.mock_response) for metar in metar_set.report_set: self.assertEqual(metar.station.station_id, "KBUF") def test_get_latest(self): """ Ensures algorithm is getting the Metar with the most recent timestamp. """ latest_metar = self.metar_set.get_latest() print latest_metar.raw_text expected_time = dateutil.parser.parse("2015-02-11T23:03:00Z") self.assertEqual(latest_metar.observation_time, expected_time) def _test_metar_attributes(self, metar): self.assertTrue(isinstance(metar.raw_text, (str, unicode))) self.assertTrue(isinstance(metar.flight_category, (str, unicode))) self.assertTrue(isinstance(metar.observation_time, datetime.datetime)) self.assertTrue(isinstance(metar.station, WeatherStation)) self.assertTrue(isinstance(metar.wind, Wind)) self.assertTrue(isinstance(metar.cloud_layers, CloudLayerSet)) # Floats for key in ['temp', 'dewpoint', 'visibility', 'altimeter']: val = getattr(metar, key) self.assertTrue(isinstance(val, float), "%s should be a float, not a %s" % (key, type(val))) def test_available_attributes(self): for metar in self.metar_set.report_set: self._test_metar_attributes(metar)
indicators.append( Indicator('yellow', "Wind Gusting: %s kts" % metar.wind.gust)) if metar.temp <= -4: indicators.append( Indicator( 'yellow', "It's chilly: %.0fC (%.0fF)" % (metar.temp, (metar.temp * 9 / 5 + 32)))) return indicators if len(sys.argv) != 2: print "Must enter a single airport identifier as an argument." sys.exit(1) metar_set = MetarSet("%s" % sys.argv[1]) metar_set.refresh() #metar = Metar('KAPA', fake='mock.xml') metar = metar_set.get_latest() if metar is None: print "No METARs found" sys.exit(1) bad_indicators = get_bad_indicators(metar) print metar.raw_text if len(bad_indicators) == 0: print Indicator('green') else: for indicator in bad_indicators: print indicator
def test_implicit_station_string(self): metar_set = MetarSet('wrong') metar_set.refresh(mock_response=self.mock_response) for metar in metar_set.report_set: self.assertEqual(metar.station.station_id, "KBUF")