/usr/share/pyshared/freevo/util/pymetar.py is in python-freevo 1.9.2b2-4.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 | # -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------
# This is the Freevo module for weather data
# -----------------------------------------------------------------------
# $Id: pymetar.py 11656 2010-02-11 16:15:04Z duncan $
#
# Modified by Jerome Alet
#
# Code contributed by:
# Jerome Alet
# Davide Di Blasi
# Adrian Holovaty
#
# Notes:
# Todo:
#
# -----------------------------------------------------------------------
# Copyright (C) 2002 Tobias Klausmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# -----------------------------------------------------------------------
#
# By reading this code you agree not to ridicule the author =)
#
# When reading this code you'll noctice that not everything is parsed
# from the raw METAR info as supplied with the report. Before you start
# flaming the authors, please read the FHM-1 spec and guess how hairy a
# parser for that format would be. As a side note, the reference
# implementation of the parser is written in... FORTRAN. Now stop crying.
import fpformat
import math
import re
import string
import urllib2
import sys
if float(sys.version[0:3]) >= 2.3:
import socket
socket.setdefaulttimeout(10)
else:
try:
import timeoutsocket # http://www.timo-tasi.org/python/timeoutsocket.py
timeoutsocket.setDefaultSocketTimeout(10)
except ImportError:
pass
__author__ = "klausman-pymetar@schwarzvogel.de"
__version__ = "0.7"
__doc__ = """Pymetar v%s (c) 2002-2004 Tobias Klausman
Pymetar is a python module and command line tool designed to fetch Metar
reports from the NOAA (http://www.noaa.gov) and allow access to the
included weather information.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
Please e-mail bugs to: %s""" % (__version__, __author__)
CLOUD_RE_STR= r"^(CLR|SKC|BKN|SCT|FEW|OVC)([0-9]{3})?$"
COND_RE_STR = r"^(-|\\+)?(VC|MI|BC|PR|TS|BL|SH|DR|FZ)?(DZ|RA|SN|SG|IC|PE|GR|GS|UP|BR|FG|FU|VA|SA|HZ|PY|DU|SQ|SS|DS|PO|\\+?FC)$"
class NetworkException(Exception):
def __init__(self, args=None):
self.args=args
class EmptyReportException(Exception):
def __init__(self, args=None):
self.args=args
class EmptyIDException(Exception):
def __init__(self, args=None):
self.args=args
# What a boring list to type !
#
# It seems the NOAA doesn't want to return plain text, but considering the
# format of their response, this is not to save bandwidth :-)
#
_WeatherConditions = {
"DZ" : ("Drizzle", "rain", {
"" : "Moderate drizzle",
"-" : "Light drizzle",
"+" : "Heavy drizzle",
"VC" : "Drizzle in the vicinity",
"MI" : "Shallow drizzle",
"BC" : "Patches of drizzle",
"PR" : "Partial drizzle",
"TS" : ("Thunderstorm", "storm"),
"BL" : "Windy drizzle",
"SH" : "Showers",
"DR" : "Drifting drizzle",
"FZ" : "Freezing drizzle",
}),
"RA" : ("Rain", "rain", {
"" : "Moderate rain",
"-" : "Light rain",
"+" : "Heavy rain",
"VC" : "Rain in the vicinity",
"MI" : "Shallow rain",
"BC" : "Patches of rain",
"PR" : "Partial rainfall",
"TS" : ("Thunderstorm", "storm"),
"BL" : "Blowing rainfall",
"SH" : "Rain showers",
"DR" : "Drifting rain",
"FZ" : "Freezing rain",
}),
"SN" : ("Snow", "snow", {
"" : "Moderate snow",
"-" : "Light snow",
"+" : "Heavy snow",
"VC" : "Snow in the vicinity",
"MI" : "Shallow snow",
"BC" : "Patches of snow",
"PR" : "Partial snowfall",
"TS" : ("Snowstorm", "storm"),
"BL" : "Blowing snowfall",
"SH" : "Snowfall showers",
"DR" : "Drifting snow",
"FZ" : "Freezing snow",
}),
"SG" : ("Snow grains", "snow", {
"" : "Moderate snow grains",
"-" : "Light snow grains",
"+" : "Heavy snow grains",
"VC" : "Snow grains in the vicinity",
"MI" : "Shallow snow grains",
"BC" : "Patches of snow grains",
"PR" : "Partial snow grains",
"TS" : ("Snowstorm", "storm"),
"BL" : "Blowing snow grains",
"SH" : "Snow grain showers",
"DR" : "Drifting snow grains",
"FZ" : "Freezing snow grains",
}),
"IC" : ("Ice crystals", "snow", {
"" : "Moderate ice crystals",
"-" : "Few ice crystals",
"+" : "Heavy ice crystals",
"VC" : "Ice crystals in the vicinity",
"BC" : "Patches of ice crystals",
"PR" : "Partial ice crystals",
"TS" : ("Ice crystal storm", "storm"),
"BL" : "Blowing ice crystals",
"SH" : "Showers of ice crystals",
"DR" : "Drifting ice crystals",
"FZ" : "Freezing ice crystals",
}),
"PE" : ("Ice pellets", "snow", {
"" : "Moderate ice pellets",
"-" : "Few ice pellets",
"+" : "Heavy ice pellets",
"VC" : "Ice pellets in the vicinity",
"MI" : "Shallow ice pellets",
"BC" : "Patches of ice pellets",
"PR" : "Partial ice pellets",
"TS" : ("Ice pellets storm", "storm"),
"BL" : "Blowing ice pellets",
"SH" : "Showers of ice pellets",
"DR" : "Drifting ice pellets",
"FZ" : "Freezing ice pellets",
}),
"GR" : ("Hail", "rain", {
"" : "Moderate hail",
"-" : "Light hail",
"+" : "Heavy hail",
"VC" : "Hail in the vicinity",
"MI" : "Shallow hail",
"BC" : "Patches of hail",
"PR" : "Partial hail",
"TS" : ("Hailstorm", "storm"),
"BL" : "Blowing hail",
"SH" : "Hail showers",
"DR" : "Drifting hail",
"FZ" : "Freezing hail",
}),
"GS" : ("Small hail", "rain", {
"" : "Moderate small hail",
"-" : "Light small hail",
"+" : "Heavy small hail",
"VC" : "Small hail in the vicinity",
"MI" : "Shallow small hail",
"BC" : "Patches of small hail",
"PR" : "Partial small hail",
"TS" : ("Small hailstorm", "storm"),
"BL" : "Blowing small hail",
"SH" : "Showers of small hail",
"DR" : "Drifting small hail",
"FZ" : "Freezing small hail",
}),
"UP" : ("Precipitation", "rain", {
"" : "Moderate precipitation",
"-" : "Light precipitation",
"+" : "Heavy precipitation",
"VC" : "Precipitation in the vicinity",
"MI" : "Shallow precipitation",
"BC" : "Patches of precipitation",
"PR" : "Partial precipitation",
"TS" : ("Unknown thunderstorm", "storm"),
"BL" : "Blowing precipitation",
"SH" : "Showers, type unknown",
"DR" : "Drifting precipitation",
"FZ" : "Freezing precipitation",
}),
"BR" : ("Mist", "fog", {
"" : "Moderate mist",
"-" : "Light mist",
"+" : "Thick mist",
"VC" : "Mist in the vicinity",
"MI" : "Shallow mist",
"BC" : "Patches of mist",
"PR" : "Partial mist",
"BL" : "Mist with wind",
"DR" : "Drifting mist",
"FZ" : "Freezing mist",
}),
"FG" : ("Fog", "fog", {
"" : "Moderate fog",
"-" : "Light fog",
"+" : "Thick fog",
"VC" : "Fog in the vicinity",
"MI" : "Shallow fog",
"BC" : "Patches of fog",
"PR" : "Partial fog",
"BL" : "Fog with wind",
"DR" : "Drifting fog",
"FZ" : "Freezing fog",
}),
"FU" : ("Smoke", "fog", {
"" : "Moderate smoke",
"-" : "Thin smoke",
"+" : "Thick smoke",
"VC" : "Smoke in the vicinity",
"MI" : "Shallow smoke",
"BC" : "Patches of smoke",
"PR" : "Partial smoke",
"TS" : ("Smoke w/ thunders", "storm"),
"BL" : "Smoke with wind",
"DR" : "Drifting smoke",
}),
"VA" : ("Volcanic ash", "fog", {
"" : "Moderate volcanic ash",
"+" : "Thick volcanic ash",
"VC" : "Volcanic ash in the vicinity",
"MI" : "Shallow volcanic ash",
"BC" : "Patches of volcanic ash",
"PR" : "Partial volcanic ash",
"TS" : ("Volcanic ash w/ thunders", "storm"),
"BL" : "Blowing volcanic ash",
"SH" : "Showers of volcanic ash",
"DR" : "Drifting volcanic ash",
"FZ" : "Freezing volcanic ash",
}),
"SA" : ("Sand", "fog", {
"" : "Moderate sand",
"-" : "Light sand",
"+" : "Heavy sand",
"VC" : "Sand in the vicinity",
"BC" : "Patches of sand",
"PR" : "Partial sand",
"BL" : "Blowing sand",
"DR" : "Drifting sand",
}),
"HZ" : ("Haze", "fog", {
"" : "Moderate haze",
"-" : "Light haze",
"+" : "Thick haze",
"VC" : "Haze in the vicinity",
"MI" : "Shallow haze",
"BC" : "Patches of haze",
"PR" : "Partial haze",
"BL" : "Haze with wind",
"DR" : "Drifting haze",
"FZ" : "Freezing haze",
}),
"PY" : ("Sprays", "fog", {
"" : "Moderate sprays",
"-" : "Light sprays",
"+" : "Heavy sprays",
"VC" : "Sprays in the vicinity",
"MI" : "Shallow sprays",
"BC" : "Patches of sprays",
"PR" : "Partial sprays",
"BL" : "Blowing sprays",
"DR" : "Drifting sprays",
"FZ" : "Freezing sprays",
}),
"DU" : ("Dust", "fog", {
"" : "Moderate dust",
"-" : "Light dust",
"+" : "Heavy dust",
"VC" : "Dust in the vicinity",
"BC" : "Patches of dust",
"PR" : "Partial dust",
"BL" : "Blowing dust",
"DR" : "Drifting dust",
}),
"SQ" : ("Squall", "storm", {
"" : "Moderate squall",
"-" : "Light squall",
"+" : "Heavy squall",
"VC" : "Squall in the vicinity",
"PR" : "Partial squall",
"TS" : "Thunderous squall",
"BL" : "Blowing squall",
"DR" : "Drifting squall",
"FZ" : "Freezing squall",
}),
"SS" : ("Sandstorm", "fog", {
"" : "Moderate sandstorm",
"-" : "Light sandstorm",
"+" : "Heavy sandstorm",
"VC" : "Sandstorm in the vicinity",
"MI" : "Shallow sandstorm",
"PR" : "Partial sandstorm",
"TS" : ("Thunderous sandstorm", "storm"),
"BL" : "Blowing sandstorm",
"DR" : "Drifting sandstorm",
"FZ" : "Freezing sandstorm",
}),
"DS" : ("Duststorm", "fog", {
"" : "Moderate duststorm",
"-" : "Light duststorm",
"+" : "Heavy duststorm",
"VC" : "Duststorm in the vicinity",
"MI" : "Shallow duststorm",
"PR" : "Partial duststorm",
"TS" : ("Thunderous duststorm", "storm"),
"BL" : "Blowing duststorm",
"DR" : "Drifting duststorm",
"FZ" : "Freezing duststorm",
}),
"PO" : ("Dustwhirls", "fog", {
"" : "Moderate dustwhirls",
"-" : "Light dustwhirls",
"+" : "Heavy dustwhirls",
"VC" : "Dustwhirls in the vicinity",
"MI" : "Shallow dustwhirls",
"BC" : "Patches of dustwhirls",
"PR" : "Partial dustwhirls",
"BL" : "Blowing dustwhirls",
"DR" : "Drifting dustwhirls",
}),
"+FC" : ("Tornado", "storm", {
"" : "Moderate tornado",
"+" : "Raging tornado",
"VC" : "Tornado in the vicinity",
"PR" : "Partial tornado",
"TS" : "Thunderous tornado",
"BL" : "Tornado",
"DR" : "Drifting tornado",
"FZ" : "Freezing tornado",
}),
"FC" : ("Funnel cloud", "fog", {
"" : "Moderate funnel cloud",
"-" : "Light funnel cloud",
"+" : "Thick funnel cloud",
"VC" : "Funnel cloud in the vicinity",
"MI" : "Shallow funnel cloud",
"BC" : "Patches of funnel cloud",
"PR" : "Partial funnel cloud",
"BL" : "Funnel cloud w/ wind",
"DR" : "Drifting funnel cloud",
}),
}
class WeatherReport:
"""Incorporates both the unparsed textual representation of the
weather report and the parsed values as soon as they are filled
in by ReportParser."""
def _ClearAllFields(self):
"""Clear all fields values."""
# until finished, report is invalid
self.valid = 0
# Clear all
self.givenstationid=None
self.fullreport=None
self.temp=None
self.tempf=None
self.windspeed=None
self.winddir=None
self.vis=None
self.dewp=None
self.dewpf=None
self.humid=None
self.press=None
self.code=None
self.weather=None
self.sky=None
self.fulln=None
self.cycle=None
self.windcomp=None
self.rtime=None
self.pixmap=None
self.latitude=None
self.longitude=None
self.altitude=None
self.stat_city=None
self.stat_country=None
self.reporturl=None
self.latf=None
self.longf=None
def __init__(self, MetarStationCode=None):
"""Clear all fields and fill in wanted station id."""
self._ClearAllFields()
self.givenstationid=MetarStationCode
def metar_to_iso8601(self, metardate) :
"""Convert a metar date to an ISO8601 date."""
if metardate is not None:
(date, hour, tz) = metardate.split()
(year, month, day) = date.split('.')
# assuming tz is always 'UTC', aka 'Z'
return "%s-%s-%s %s:%s:00Z"%(year, month, day, hour[:2], hour[2:4])
def getFullReport(self):
""" Return the complete weather report. """
return self.fullreport
def getTemperatureCelsius(self):
"""
Return the temperature in degrees Celsius.
"""
return self.temp
def getTemperatureFahrenheit(self):
"""
Return the temperature in degrees Fahrenheit.
"""
return self.tempf
def getDewPointCelsius(self):
"""
Return dewpoint in degrees Celsius.
"""
return self.dewp
def getDewPointFahrenheit(self):
"""
Return dewpoint in degrees Fahrenheit.
"""
return self.dewpf
def getWindSpeed(self):
"""
Return the wind speed in meters per second.
"""
return self.windspeed
def getWindSpeedMilesPerHour(self):
"""
Return the wind speed in miles per hour.
"""
if windspeed is not None:
return self.windspeed * 2.237
def getWindDirection(self):
"""
Return wind direction in degrees.
"""
return self.winddir
def getWindCompass(self):
"""
Return wind direction as compass direction
(e.g. NE or SSE)
"""
return self.windcomp
def getVisibilityKilometers(self):
"""
Return visibility in km.
"""
return self.vis
def getVisibilityMiles(self):
"""
Return visibility in miles.
"""
return self.vis / 1.609344
def getHumidity(self):
"""
Return relative humidity in percent.
"""
return self.humid
def getPressure(self):
"""
Return pressure in hPa.
"""
return self.press
def getRawMetarCode(self):
"""
Return the encoded weather report.
"""
return self.code
def getWeather(self):
"""
Return short weather conditions
"""
return self.weather
def getSkyConditions(self):
"""
Return sky conditions
"""
return self.sky
def getStationName(self):
"""
Return full station name
"""
return self.fulln
def getStationCity(self):
"""
Return city-part of station name
"""
return self.stat_city
def getStationCountry(self):
"""
Return country-part of station name
"""
return self.stat_country
def getCycle(self):
"""
Return cycle value.
The cycle value is not the frequency or delay between
observations but the "time slot" in which the observation was made.
There are 24 cycle slots every day which usually last from N:45 to
N+1:45. The cycle from 23:45 to 0:45 is cycle 0.
"""
return self.cycle
def getStationPosition(self):
"""
Return latitude, longitude and altitude above sea level of station
as a tuple. Some stations don't deliver altitude, for those, None
is returned as altitude. The lat/longs are expressed as follows:
xx-yyd
where xx is degrees, yy minutes and d the direction.
Thus 51-14N means 51 degrees, 14 minutes north. d may take the
values N, S for latitues and W, E for longitudes. Latitude and
Longitude may include seconds. Altitude is always given as meters
above sea level, including a trailing M.
Schipohl Int. Airport Amsterdam has, for example:
('52-18N', '004-46E', '-2M')
Moenchengladbach (where I live):
('51-14N', '063-03E', None)
If you need lat and long as float values, look at
getStationPositionFloat() instead
"""
# convert self.altitude to string for consistency
return (self.latitude, self.longitude, "%s"%self.altitude)
def getStationPositionFloat(self):
"""
Return latitude and longitude as float values in a
tuple (lat,long,alt).
"""
return (self.latf,self.longf,self.altitude)
def getStationLatitude(self) :
"""
Return the station's latitude in dd-mm[-ss]D format :
dd : degrees
mm : minutes
ss : seconds
D : direction (N, S, E, W)
"""
return self.latitude
def getStationLatitudeFloat(self):
"""
Return latitude as a float
"""
return self.latf
def getStationLongitude(self) :
"""
Return the station's longitude in dd-mm[-ss]D format :
dd : degrees
mm : minutes
ss : seconds
D : direction (N, S, E, W)
"""
return self.longitude
def getStationLongitudeFloat(self):
"""
Return Longitude as a float
"""
return self.longf
def getStationAltitude(self) :
"""
Return the station's altitude above the sea in meters.
"""
return self.altitude
def getReportURL(self):
"""
Return the URL from which the report was fetched.
"""
return self.reporturl
def getTime(self):
"""
Return the time when the observation was made. Note that this
is *not* the time when the report was fetched by us
Format: YYYY.MM.DD HHMM UTC
Example: 2002.04.01 1020 UTC
"""
return self.rtime
def getISOTime(self):
"""
Return the time when the observation was made in ISO 8601 format
(e.g. 2002-07-25 15:12:00Z)
"""
return(self.metar_to_iso8601(self.rtime))
def getPixmap(self):
"""
Return a suggested pixmap name, without extension, depending on
current weather.
"""
return self.pixmap
class ReportParser:
"""Parse raw METAR data from a WeatherReport object into actual
values and return the object with the values filled in."""
def __init__(self, MetarReport=None):
"""Set attribute Report as specified on instantation."""
self.Report=MetarReport
def strreverse(self, str):
"""Reverse a string"""
listr=list(str)
listr.reverse()
return "".join(listr)
def extractCloudInformation(self) :
"""
Extract cloud information. Return None or a tuple (sky type as a
string of text and suggested pixmap name)
"""
wcloud = self.match_WeatherPart(CLOUD_RE_STR)
if wcloud is not None :
stype = wcloud[:3]
if stype == "CLR" or stype == "SKC":
return ("Clear sky", "sun")
elif stype == "BKN" :
return ("Broken clouds", "suncloud")
elif stype == "SCT" :
return ("Scattered clouds", "suncloud")
elif stype == "FEW" :
return ("Few clouds", "suncloud")
elif stype == "OVC" :
return ("Overcast", "cloud")
else:
return None # Not strictly necessary
def extractSkyConditions(self) :
"""
Extract sky condition information from the encoded report. Return
a tuple containing the description of the sky conditions as a
string and a suggested pixmap name for an icon representing said
sky condition.
"""
wcond = self.match_WeatherPart(COND_RE_STR)
if wcond is not None :
if len(wcond) > 3 and (wcond.startswith('+') or wcond.startswith('-')) :
wcond = wcond[1:]
if wcond.startswith('+') or wcond.startswith('-') :
pphen = 1
elif len(wcond) < 4 :
pphen = 0
else :
pphen = 2
squal = wcond[:pphen]
sphen = wcond[pphen : pphen + 4]
phenomenon = _WeatherConditions.get(sphen, None)
if phenomenon is not None :
(name, pixmap, phenomenon) = phenomenon
pheninfo = phenomenon.get(squal, name)
if type(pheninfo) != type(()) :
return (pheninfo, pixmap)
else :
# contains pixmap info
return pheninfo
def match_WeatherPart(self, regexp) :
"""
Return the matching part of the encoded Metar report.
regexp: the regexp needed to extract this part.
Return the first matching string or None.
WARNING: Some Metar reports may contain several matching
strings, only the first one is taken into account!
"""
if self.Report.code is not None :
rg = re.compile(regexp)
for wpart in self.Report.getRawMetarCode().split() :
match = rg.match(wpart)
if match:
return match.string[match.start(0) : match.end(0)]
def parseLatLong(self, latlong):
"""
Parse Lat or Long in METAR notation into float values. N and E
are +, S and W are -. Expects one positional string and returns
one float value.
"""
# I know, I could invert this if and put
# the rest of the function into its block,
# but I find it to be more readable this way
if latlong is None: return None
s=latlong.upper().strip()
elms = s.split('-')
ud = elms[-1][-1]
elms[-1] = elms[-1][:-1]
elms = map(string.atoi, elms)
coords = 0.0
elen = len(elms)
if elen > 2:
coords = coords + float(elms[2])/3600.0
if elen > 1:
coords = coords + float(elms[1])/60.0
coords = coords + float(elms[0])
if ud in ('W','S'):
coords = -1.0*coords
f,i = math.modf(coords)
if elen > 2:
f = float(fpformat.sci(f,4))
elif elen > 1:
f = float(fpformat.sci(f,2))
else:
f = 0.0
return f+i
def ParseReport(self, MetarReport=None):
"""Take report with raw info only and return it with in
parsed values filled in. Note: This function edits the
WeatherReport object you supply!"""
if self.Report is None and MetarReport is None:
raise EmptyReportException('No report given on init and ParseReport')
elif MetarReport is not None:
self.Report=MetarReport
lines = self.Report.fullreport.split("\n")
for line in lines:
try:
header, data=line.split(":",1)
except ValueError:
header=data=line
header = header.strip()
data = data.strip()
# The station id inside the report
if header.find("("+self.Report.givenstationid+")") != -1:
try:
loc,p=data.split("(",1)
loc=loc.strip()
rloc=self.strreverse(loc)
rcoun,rcity=rloc.split(",",1)
except ValueError:
city=""
coun=""
p=data
try:
id,lat,long,ht=p.split()
ht=int(ht[:-1]) # cut off 'M' for meters
except ValueError:
id,lat,long=p.split()
ht=None
self.Report.stat_city=self.strreverse(rcity.strip())
self.Report.stat_country=self.strreverse(rcoun.strip())
self.Report.fulln=loc
self.Report.latitude=lat
self.Report.longitude=long
self.Report.latf=self.parseLatLong(lat)
self.Report.longf=self.parseLatLong(long)
self.Report.altitude=ht
# The line containing date and timr of the report
elif data.find("UTC") != -1:
local,rt=data.split("/")
self.Report.rtime=rt.strip()
# temperature
elif header == "Temperature":
# data is something like:
# '62 F (17 C)' and may have decimal points
m = re.search('(\d+).* F.*\((\d+).* C\)', data)
self.Report.tempf = int(m.group(1)) if m and len(m.groups()) >= 1 else 999
self.Report.temp = int(m.group(2)) if m and len(m.groups()) >= 2 else 99
# wind dir and speed
elif header == "Wind":
if data.find("Calm") != -1:
self.Report.windspeed=0.0
self.Report.winddir=None
self.Report.windcomp=None
elif data.find("Variable") != -1:
v,a,speed,r=data.split(" ",3)
self.Report.windspeed=(float(speed)*0.44704)
self.Report.winddir=None
self.Report.windcomp=None
else:
f,t,comp,deg,r,d,speed,r=data.split(" ",7)
self.Report.winddir=int(deg[1:])
self.Report.windcomp=comp.strip()
self.Report.windspeed=(float(speed)*0.44704)
# visibility
elif header == "Visibility":
for d in data.split():
try:
self.Report.vis=float(d)*1.609344
break
except ValueError:
pass
# dew point
elif header == "Dew Point":
# data is something like:
# '41 F (5 C)' and may have decimal points
m = re.search('(\d+).* F.*\((\d+).* C\)', data)
self.Report.dewpf = int(m.group(1)) if m and len(m.groups()) >= 1 else 999
self.Report.dewp = int(m.group(2)) if m and len(m.groups()) >= 2 else 99
# humidity
elif header == "Relative Humidity":
h,i=data.split("%",1)
self.Report.humid=int(h)
# pressure
elif header == "Pressure (altimeter)":
p,r=data.split(" ",1)
self.Report.press=(float(p)*33.863886)
# shot weather desc. ("rain", "mist", ...)
elif header == "Weather":
self.Report.weather=data
# short desc. of sky conditions
elif header == "Sky conditions":
self.Report.sky=data
# the encoded report itself
elif header == "ob":
self.Report.code=data.strip()
# the cycle value ("time slot")
elif header == "cycle":
self.Report.cycle=int(data)
# cloud info
cloudinfo = self.extractCloudInformation()
if cloudinfo is not None :
(cloudinfo, cloudpixmap) = cloudinfo
else :
(cloudinfo, cloudpixmap) = (None, None)
conditions = self.extractSkyConditions()
if conditions is not None :
(conditions, condpixmap) = conditions
else :
(conditions, condpixmap) = (None, None)
# fill the weather information
self.Report.weather = self.Report.weather or conditions or cloudinfo
# Pixmap guessed from general conditions has priority
# over pixmap guessed from clouds
self.Report.pixmap = condpixmap or cloudpixmap
# report is complete
self.Report.valid = 1
return self.Report
class ReportFetcher:
"""
Fetches a report from a given METAR id, optionally taking into account a
different baseurl and using environment var-specified proxies.
"""
def __init__(self, MetarStationCode=None, baseurl="http://weather.noaa.gov/pub/data/observations/metar/decoded/"):
"""Set stationid attribute"""
self.stationid=MetarStationCode
self.baseurl=baseurl
def FetchReport(self, StationCode=None):
"""
Fetch a report for a given station ID from the baseurl given upon
creation of the ReportFetcher instance.
"""
if self.stationid is None and StationCode is None:
raise EmptyIDException('No ID given on init and FetchReport')
elif StationCode is not None:
self.stationid=StationCode
self.stationid=self.stationid.upper()
self.reporturl="%s%s.TXT" % (self.baseurl, self.stationid)
urllib2.install_opener(
urllib2.build_opener(urllib2.ProxyHandler, urllib2.HTTPHandler))
try:
fn=urllib2.urlopen(self.reporturl)
except urllib2.HTTPError, why:
raise NetworkException(why)
except urllib2.URLError, why:
raise NetworkException(why)
# Dump entire report in a variable
self.fullreport=fn.read()
if fn.info().status:
raise NetworkException('Could not fetch METAR report')
report=WeatherReport(self.stationid)
report.reporturl=self.reporturl
report.fullreport=self.fullreport
return report
|