| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""This module defines the Metar class. |
| 5 |
|
| 6 |
A Metar object represents the weather report encoded by a single METAR code. |
| 7 |
""" |
| 8 |
import re |
| 9 |
import datetime |
| 10 |
import warnings |
| 11 |
import logging |
| 12 |
|
| 13 |
from metar import __version__, __author__, __email__, __LICENSE__ |
| 14 |
from metar.Datatypes import ( |
| 15 |
temperature, |
| 16 |
pressure, |
| 17 |
speed, |
| 18 |
distance, |
| 19 |
direction, |
| 20 |
precipitation, |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
_logger = logging.getLogger(__name__) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class ParserError(Exception): |
| 29 |
"""Exception raised when an unparseable group is found in body of the report.""" |
| 30 |
|
| 31 |
pass |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
MISSING_RE = re.compile(r"^[M/]+$") |
| 36 |
|
| 37 |
TYPE_RE = re.compile(r"^(?P<type>METAR|SPECI)\s+") |
| 38 |
COR_RE = re.compile(r"^(?P<cor>COR)\s+") |
| 39 |
STATION_RE = re.compile(r"^(?P<station>[A-Z][A-Z0-9]{3})\s+") |
| 40 |
TIME_RE = re.compile( |
| 41 |
r"""^(?P<day>\d\d) |
| 42 |
(?P<hour>\d\d) |
| 43 |
(?P<min>\d\d)Z?\s+""", |
| 44 |
re.VERBOSE, |
| 45 |
) |
| 46 |
MODIFIER_RE = re.compile(r"^(?P<mod>AUTO|COR AUTO|FINO|NIL|TEST|CORR?|RTD|CC[A-G])\s+") |
| 47 |
WIND_RE = re.compile( |
| 48 |
r"""^(?P<dir>[\dO]{3}|[0O]|///|MMM|VRB) |
| 49 |
(?P<speed>P?[\dO]{2,3}|[/M]{2,3}) |
| 50 |
(G(?P<gust>P?(\d{1,3}|[/M]{1,3})))? |
| 51 |
(?P<units>KTS?|LT|K|T|KMH|MPS)? |
| 52 |
(\s+(?P<varfrom>\d\d\d)V |
| 53 |
(?P<varto>\d\d\d))?\s+""", |
| 54 |
re.VERBOSE, |
| 55 |
) |
| 56 |
VISIBILITY_RE = re.compile( |
| 57 |
r"""^(?P<vis>(?P<dist>(M|P)?\d\d\d\d|////) |
| 58 |
(?P<dir>[NSEW][EW]? | NDV)? | |
| 59 |
(?P<distu>(M|P)?(\d+|\d\d?/\d\d?|\d+\s+\d/\d)) |
| 60 |
(?P<units>SM|KM|M|U) | |
| 61 |
CAVOK )\s+""", |
| 62 |
re.VERBOSE, |
| 63 |
) |
| 64 |
RUNWAY_RE = re.compile( |
| 65 |
r"""^(RVRNO | |
| 66 |
R(?P<name>\d\d(RR?|LL?|C)?)/ |
| 67 |
(?P<low>(M|P)?(\d\d\d\d|/{4})) |
| 68 |
(V(?P<high>(M|P)?\d\d\d\d))? |
| 69 |
(?P<unit>FT)?[/NDU]*)\s+""", |
| 70 |
re.VERBOSE, |
| 71 |
) |
| 72 |
WEATHER_RE = re.compile( |
| 73 |
r"""^(?P<int>(-|\+|VC)*) |
| 74 |
(?P<desc>(MI|PR|BC|DR|BL|SH|TS|FZ)+)? |
| 75 |
(?P<prec>(DZ|RA|SN|SG|IC|PL|GR|GS|UP|/)*) |
| 76 |
(?P<obsc>BR|FG|FU|VA|DU|SA|HZ|PY)? |
| 77 |
(?P<other>PO|SQ|FC|SS|DS|NSW|/+)? |
| 78 |
(?P<int2>[-+])?\s+""", |
| 79 |
re.VERBOSE, |
| 80 |
) |
| 81 |
SKY_RE = re.compile( |
| 82 |
r"""^(?P<cover>VV|CLR|SKC|SCK|NSC|NCD|BKN|SCT|FEW|[O0]VC|///) |
| 83 |
(?P<height>[\dO]{2,4}|///)? |
| 84 |
(?P<cloud>([A-Z][A-Z]+|///))?\s+""", |
| 85 |
re.VERBOSE, |
| 86 |
) |
| 87 |
TEMP_RE = re.compile( |
| 88 |
r"""^(?P<temp>(M|-)?\d{1,2}|//|XX|MM)/ |
| 89 |
(?P<dewpt>(M|-)?\d{1,2}|//|XX|MM)?\s+""", |
| 90 |
re.VERBOSE, |
| 91 |
) |
| 92 |
PRESS_RE = re.compile( |
| 93 |
r"""^(?P<unit>A|Q|QNH)? |
| 94 |
(?P<press>[\dO]{3,4}|////) |
| 95 |
(?P<unit2>INS)?\s+""", |
| 96 |
re.VERBOSE, |
| 97 |
) |
| 98 |
RECENT_RE = re.compile( |
| 99 |
r"""^RE(?P<desc>MI|PR|BC|DR|BL|SH|TS|FZ)? |
| 100 |
(?P<prec>(DZ|RA|SN|SG|IC|PL|GR|GS|UP)*)? |
| 101 |
(?P<obsc>BR|FG|FU|VA|DU|SA|HZ|PY)? |
| 102 |
(?P<other>PO|SQ|FC|SS|DS)?\s+""", |
| 103 |
re.VERBOSE, |
| 104 |
) |
| 105 |
WINDSHEAR_RE = re.compile(r"^(WS\s+)?(ALL\s+RWY|R(WY)?(?P<name>\d\d(RR?|L?|C)?))\s+") |
| 106 |
COLOR_RE = re.compile( |
| 107 |
r"""^(BLACK)?(BLU|GRN|WHT|RED)\+? |
| 108 |
(/?(BLACK)?(BLU|GRN|WHT|RED)\+?)*\s*""", |
| 109 |
re.VERBOSE, |
| 110 |
) |
| 111 |
RUNWAYSTATE_RE = re.compile( |
| 112 |
r"""((?P<snoclo>R/SNOCLO) | |
| 113 |
((?P<name>\d\d) | R(?P<namenew>\d\d)(RR?|LL?|C)?/?) |
| 114 |
((?P<special> SNOCLO|CLRD(\d\d|//)) | |
| 115 |
(?P<deposit>(\d|/)) |
| 116 |
(?P<extent>(\d|/)) |
| 117 |
(?P<depth>(\d\d|//)) |
| 118 |
(?P<friction>(\d\d|//))))\s+""", |
| 119 |
re.VERBOSE, |
| 120 |
) |
| 121 |
TREND_RE = re.compile(r"^(?P<trend>TEMPO|BECMG|FCST|NOSIG)\s+") |
| 122 |
|
| 123 |
TRENDTIME_RE = re.compile(r"(?P<when>(FM|TL|AT))(?P<hour>\d\d)(?P<min>\d\d)\s+") |
| 124 |
|
| 125 |
REMARK_RE = re.compile(r"^(RMKS?|NOSPECI|NOSIG)\s+") |
| 126 |
|
| 127 |
|
| 128 |
AUTO_RE = re.compile(r"^AO(?P<type>\d)\s+") |
| 129 |
SEALVL_PRESS_RE = re.compile(r"^SLP(?P<press>\d\d\d)\s+") |
| 130 |
PEAK_WIND_RE = re.compile( |
| 131 |
r"""^P[A-Z]\s+WND\s+ |
| 132 |
(?P<dir>\d\d\d) |
| 133 |
(?P<speed>P?\d\d\d?)/ |
| 134 |
(?P<hour>\d\d)? |
| 135 |
(?P<min>\d\d)\s+""", |
| 136 |
re.VERBOSE, |
| 137 |
) |
| 138 |
WIND_SHIFT_RE = re.compile( |
| 139 |
r"""^WSHFT\s+ |
| 140 |
(?P<hour>\d\d)? |
| 141 |
(?P<min>\d\d) |
| 142 |
(\s+(?P<front>FROPA))?\s+""", |
| 143 |
re.VERBOSE, |
| 144 |
) |
| 145 |
PRECIP_1HR_RE = re.compile(r"^P(?P<precip>\d\d\d\d)\s+") |
| 146 |
PRECIP_24HR_RE = re.compile( |
| 147 |
r"""^(?P<type>6|7) |
| 148 |
(?P<precip>\d\d\d\d)\s+""", |
| 149 |
re.VERBOSE, |
| 150 |
) |
| 151 |
PRESS_3HR_RE = re.compile( |
| 152 |
r"""^5(?P<tend>[0-8]) |
| 153 |
(?P<press>\d\d\d)\s+""", |
| 154 |
re.VERBOSE, |
| 155 |
) |
| 156 |
TEMP_1HR_RE = re.compile( |
| 157 |
r"""^T(?P<tsign>0|1) |
| 158 |
(?P<temp>\d\d\d) |
| 159 |
((?P<dsign>0|1) |
| 160 |
(?P<dewpt>\d\d\d))?\s+""", |
| 161 |
re.VERBOSE, |
| 162 |
) |
| 163 |
TEMP_6HR_RE = re.compile( |
| 164 |
r"""^(?P<type>1|2) |
| 165 |
(?P<sign>0|1) |
| 166 |
(?P<temp>\d\d\d)\s+""", |
| 167 |
re.VERBOSE, |
| 168 |
) |
| 169 |
TEMP_24HR_RE = re.compile( |
| 170 |
r"""^4(?P<smaxt>0|1) |
| 171 |
(?P<maxt>\d\d\d) |
| 172 |
(?P<smint>0|1) |
| 173 |
(?P<mint>\d\d\d)\s+""", |
| 174 |
re.VERBOSE, |
| 175 |
) |
| 176 |
UNPARSED_RE = re.compile(r"(?P<group>\S+)\s+") |
| 177 |
|
| 178 |
LIGHTNING_RE = re.compile( |
| 179 |
r"""^((?P<freq>OCNL|FRQ|CONS)\s+)? |
| 180 |
LTG(?P<type>(IC|CC|CG|CA)*) |
| 181 |
( \s+(?P<loc>( OHD | VC | DSNT\s+ | \s+AND\s+ | |
| 182 |
[NSEW][EW]? (-[NSEW][EW]?)* )+) )?\s+""", |
| 183 |
re.VERBOSE, |
| 184 |
) |
| 185 |
|
| 186 |
TS_LOC_RE = re.compile( |
| 187 |
r"""TS(\s+(?P<loc>( OHD | VC | DSNT\s+ | \s+AND\s+ | |
| 188 |
[NSEW][EW]? (-[NSEW][EW]?)* )+))? |
| 189 |
( \s+MOV\s+(?P<dir>[NSEW][EW]?) )?\s+""", |
| 190 |
re.VERBOSE, |
| 191 |
) |
| 192 |
SNOWDEPTH_RE = re.compile(r"""^4/(?P<snowdepth>\d\d\d)\s+""") |
| 193 |
ICE_ACCRETION_RE = re.compile( |
| 194 |
r"^I(?P<ice_accretion_hours>[136])(?P<ice_accretion_depth>\d\d\d)\s+" |
| 195 |
) |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
loc_terms = [("OHD", "overhead"), ("DSNT", "distant"), ("AND", "and"), ("VC", "nearby")] |
| 200 |
|
| 201 |
|
| 202 |
def xlate_loc(loc): |
| 203 |
"""Substitute English terms for the location codes in the given string.""" |
| 204 |
for code, english in loc_terms: |
| 205 |
loc = loc.replace(code, english) |
| 206 |
return loc |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
SKY_COVER = { |
| 211 |
"SKC": "clear", |
| 212 |
"CLR": "clear", |
| 213 |
"NSC": "clear", |
| 214 |
"NCD": "clear", |
| 215 |
"FEW": "a few ", |
| 216 |
"SCT": "scattered ", |
| 217 |
"BKN": "broken ", |
| 218 |
"OVC": "overcast", |
| 219 |
"///": "", |
| 220 |
"VV": "indefinite ceiling", |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
CLOUD_TYPE = { |
| 225 |
"AC": "altocumulus", |
| 226 |
"ACC": "altocumulus castellanus", |
| 227 |
"ACSL": "standing lenticular altocumulus", |
| 228 |
"AS": "altostratus", |
| 229 |
"CB": "cumulonimbus", |
| 230 |
"CBMAM": "cumulonimbus mammatus", |
| 231 |
"CCSL": "standing lenticular cirrocumulus", |
| 232 |
"CC": "cirrocumulus", |
| 233 |
"CI": "cirrus", |
| 234 |
"CS": "cirrostratus", |
| 235 |
"CU": "cumulus", |
| 236 |
"NS": "nimbostratus", |
| 237 |
"SC": "stratocumulus", |
| 238 |
"ST": "stratus", |
| 239 |
"SCSL": "standing lenticular stratocumulus", |
| 240 |
"TCU": "towering cumulus", |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
WEATHER_INT = { |
| 245 |
"-": "light", |
| 246 |
"+": "heavy", |
| 247 |
"-VC": "nearby light", |
| 248 |
"+VC": "nearby heavy", |
| 249 |
"VC": "nearby", |
| 250 |
} |
| 251 |
WEATHER_DESC = { |
| 252 |
"MI": "shallow", |
| 253 |
"PR": "partial", |
| 254 |
"BC": "patches of", |
| 255 |
"DR": "low drifting", |
| 256 |
"BL": "blowing", |
| 257 |
"SH": "showers", |
| 258 |
"TS": "thunderstorm", |
| 259 |
"FZ": "freezing", |
| 260 |
} |
| 261 |
WEATHER_PREC = { |
| 262 |
"DZ": "drizzle", |
| 263 |
"RA": "rain", |
| 264 |
"SN": "snow", |
| 265 |
"SG": "snow grains", |
| 266 |
"IC": "ice crystals", |
| 267 |
"PL": "ice pellets", |
| 268 |
"GR": "hail", |
| 269 |
"GS": "snow pellets", |
| 270 |
"UP": "unknown precipitation", |
| 271 |
"//": "", |
| 272 |
} |
| 273 |
WEATHER_OBSC = { |
| 274 |
"BR": "mist", |
| 275 |
"FG": "fog", |
| 276 |
"FU": "smoke", |
| 277 |
"VA": "volcanic ash", |
| 278 |
"DU": "dust", |
| 279 |
"SA": "sand", |
| 280 |
"HZ": "haze", |
| 281 |
"PY": "spray", |
| 282 |
} |
| 283 |
WEATHER_OTHER = { |
| 284 |
"PO": "sand whirls", |
| 285 |
"SQ": "squalls", |
| 286 |
"FC": "funnel cloud", |
| 287 |
"SS": "sandstorm", |
| 288 |
"DS": "dust storm", |
| 289 |
"NSW": "no significant weather", |
| 290 |
} |
| 291 |
|
| 292 |
WEATHER_SPECIAL = {"+FC": "tornado"} |
| 293 |
|
| 294 |
COLOR = {"BLU": "blue", "GRN": "green", "WHT": "white"} |
| 295 |
|
| 296 |
|
| 297 |
PRESSURE_TENDENCY = { |
| 298 |
"0": "increasing, then decreasing", |
| 299 |
"1": "increasing more slowly", |
| 300 |
"2": "increasing", |
| 301 |
"3": "increasing more quickly", |
| 302 |
"4": "steady", |
| 303 |
"5": "decreasing, then increasing", |
| 304 |
"6": "decreasing more slowly", |
| 305 |
"7": "decreasing", |
| 306 |
"8": "decreasing more quickly", |
| 307 |
} |
| 308 |
|
| 309 |
LIGHTNING_FREQUENCY = {"OCNL": "occasional", "FRQ": "frequent", "CONS": "constant"} |
| 310 |
LIGHTNING_TYPE = { |
| 311 |
"IC": "intracloud", |
| 312 |
"CC": "cloud-to-cloud", |
| 313 |
"CG": "cloud-to-ground", |
| 314 |
"CA": "cloud-to-air", |
| 315 |
} |
| 316 |
|
| 317 |
REPORT_TYPE = { |
| 318 |
"METAR": "routine report", |
| 319 |
"SPECI": "special report", |
| 320 |
"AUTO": "automatic report", |
| 321 |
"COR": "manually corrected report", |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
def _sanitize(code): |
| 327 |
"""Some string prep to improve parsing fidelity.""" |
| 328 |
|
| 329 |
|
| 330 |
return "%s " % (code.strip().rstrip("="),) |
| 331 |
|
| 332 |
|
| 333 |
def _report_match(handler, match): |
| 334 |
"""Report success or failure of the given handler function. (DEBUG)""" |
| 335 |
if match: |
| 336 |
_logger.debug("%s matched '%s'", handler.__name__, match) |
| 337 |
else: |
| 338 |
_logger.debug("%s didn't match...", handler.__name__) |
| 339 |
|
| 340 |
|
| 341 |
def _unparsedGroup(self, d): |
| 342 |
""" |
| 343 |
Handle otherwise unparseable main-body groups. |
| 344 |
""" |
| 345 |
self._unparsed_groups.append(d["group"]) |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
debug = False |
| 350 |
|
| 351 |
|
| 352 |
class Metar(object): |
| 353 |
"""METAR (aviation meteorology report)""" |
| 354 |
|
| 355 |
def __init__(self, metarcode, month=None, year=None, strict=True): |
| 356 |
""" |
| 357 |
Parse raw METAR code. |
| 358 |
|
| 359 |
Parameters |
| 360 |
---------- |
| 361 |
metarcode : str |
| 362 |
month, year : int, optional |
| 363 |
Date values to be used when parsing a non-current METAR code. If not |
| 364 |
provided, then the month and year are guessed from the current date. |
| 365 |
strict : bool (default is True) |
| 366 |
This option determines if a ``ParserError`` is raised when |
| 367 |
unparsable groups are found or an unexpected exception is encountered. |
| 368 |
Setting this to `False` will prevent exceptions from being raised and |
| 369 |
only generate warning messages. |
| 370 |
""" |
| 371 |
|
| 372 |
self.code = metarcode |
| 373 |
self.type = "METAR" |
| 374 |
self.correction = None |
| 375 |
self.mod = "AUTO" |
| 376 |
self.station_id = None |
| 377 |
self.time = None |
| 378 |
self.cycle = None |
| 379 |
self.wind_dir = None |
| 380 |
self.wind_speed = None |
| 381 |
self.wind_gust = None |
| 382 |
self.wind_dir_from = None |
| 383 |
self.wind_dir_to = None |
| 384 |
self.vis = None |
| 385 |
self.vis_dir = None |
| 386 |
self.max_vis = None |
| 387 |
self.max_vis_dir = None |
| 388 |
self.temp = None |
| 389 |
self.dewpt = None |
| 390 |
self.press = None |
| 391 |
self.runway = [] |
| 392 |
self.weather = [] |
| 393 |
self.recent = [] |
| 394 |
self.sky = [] |
| 395 |
self.windshear = [] |
| 396 |
self.wind_speed_peak = None |
| 397 |
self.wind_dir_peak = None |
| 398 |
self.peak_wind_time = None |
| 399 |
self.wind_shift_time = None |
| 400 |
self.max_temp_6hr = None |
| 401 |
self.min_temp_6hr = None |
| 402 |
self.max_temp_24hr = None |
| 403 |
self.min_temp_24hr = None |
| 404 |
self.press_sea_level = None |
| 405 |
self.precip_1hr = None |
| 406 |
self.precip_3hr = None |
| 407 |
self.precip_6hr = None |
| 408 |
self.precip_24hr = None |
| 409 |
self.snowdepth = None |
| 410 |
self.ice_accretion_1hr = None |
| 411 |
self.ice_accretion_3hr = None |
| 412 |
self.ice_accretion_6hr = None |
| 413 |
self._trend = False |
| 414 |
self._trend_groups = [] |
| 415 |
self._remarks = [] |
| 416 |
self._unparsed_groups = [] |
| 417 |
self._unparsed_remarks = [] |
| 418 |
|
| 419 |
self._now = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) |
| 420 |
|
| 421 |
self._month = month |
| 422 |
self._year = year |
| 423 |
|
| 424 |
|
| 425 |
code = _sanitize(self.code) |
| 426 |
try: |
| 427 |
ngroup = len(self.handlers) |
| 428 |
igroup = 0 |
| 429 |
ifailed = -1 |
| 430 |
while igroup < ngroup and code: |
| 431 |
pattern, handler, repeatable = self.handlers[igroup] |
| 432 |
if debug: |
| 433 |
_logger.debug("%s: %s", handler.__name__, code) |
| 434 |
m = pattern.match(code) |
| 435 |
while m: |
| 436 |
ifailed = -1 |
| 437 |
if debug: |
| 438 |
_report_match(handler, m.group()) |
| 439 |
handler(self, m.groupdict()) |
| 440 |
code = code[m.end():] |
| 441 |
if self._trend: |
| 442 |
code = self._do_trend_handlers(code) |
| 443 |
if not repeatable: |
| 444 |
break |
| 445 |
|
| 446 |
if debug: |
| 447 |
_logger.debug("%s: %s", handler.__name__, code) |
| 448 |
m = pattern.match(code) |
| 449 |
if not m and ifailed < 0: |
| 450 |
ifailed = igroup |
| 451 |
igroup += 1 |
| 452 |
if igroup == ngroup and not m: |
| 453 |
pattern, handler = (UNPARSED_RE, _unparsedGroup) |
| 454 |
if debug: |
| 455 |
_logger.debug("%s: %s", handler.__name__, code) |
| 456 |
m = pattern.match(code) |
| 457 |
if debug: |
| 458 |
_report_match(handler, m.group()) |
| 459 |
handler(self, m.groupdict()) |
| 460 |
code = code[m.end():] |
| 461 |
igroup = ifailed |
| 462 |
ifailed = -2 |
| 463 |
|
| 464 |
if pattern == REMARK_RE or self.press: |
| 465 |
while code: |
| 466 |
for pattern, handler in self.remark_handlers: |
| 467 |
if debug: |
| 468 |
_logger.debug("%s: %s", handler.__name__, code) |
| 469 |
m = pattern.match(code) |
| 470 |
if m: |
| 471 |
if debug: |
| 472 |
_report_match(handler, m.group()) |
| 473 |
handler(self, m.groupdict()) |
| 474 |
code = pattern.sub("", code, 1) |
| 475 |
break |
| 476 |
|
| 477 |
except Exception as err: |
| 478 |
message = ("%s failed while processing '%s'\n\t%s") % ( |
| 479 |
handler.__name__, |
| 480 |
code, |
| 481 |
"\n\t".join(err.args), |
| 482 |
) |
| 483 |
if strict: |
| 484 |
raise ParserError(message) |
| 485 |
else: |
| 486 |
warnings.warn(message, RuntimeWarning) |
| 487 |
|
| 488 |
if self._unparsed_groups: |
| 489 |
code = " ".join(self._unparsed_groups) |
| 490 |
message = "Unparsed groups in body '%s' while processing '%s'" % ( |
| 491 |
code, |
| 492 |
metarcode, |
| 493 |
) |
| 494 |
if strict: |
| 495 |
raise ParserError(message) |
| 496 |
else: |
| 497 |
warnings.warn(message, RuntimeWarning) |
| 498 |
|
| 499 |
@property |
| 500 |
def decode_completed(self): |
| 501 |
""" |
| 502 |
Indicate whether the decoding was complete for non-remark elements. |
| 503 |
""" |
| 504 |
return not self._unparsed_groups |
| 505 |
|
| 506 |
def _do_trend_handlers(self, code): |
| 507 |
for pattern, handler, repeatable in self.trend_handlers: |
| 508 |
if debug: |
| 509 |
print(handler.__name__, ":", code) |
| 510 |
m = pattern.match(code) |
| 511 |
while m: |
| 512 |
if debug: |
| 513 |
_report_match(handler, m.group()) |
| 514 |
self._trend_groups.append(m.group().strip()) |
| 515 |
handler(self, m.groupdict()) |
| 516 |
code = code[m.end():] |
| 517 |
if not repeatable: |
| 518 |
break |
| 519 |
m = pattern.match(code) |
| 520 |
return code |
| 521 |
|
| 522 |
def __str__(self): |
| 523 |
return self.string() |
| 524 |
|
| 525 |
def _handleType(self, d): |
| 526 |
""" |
| 527 |
Parse the report-type group. |
| 528 |
|
| 529 |
The following attributes are set: |
| 530 |
type [string] |
| 531 |
""" |
| 532 |
self.type = d["type"] |
| 533 |
|
| 534 |
def _handleCorrection(self, d): |
| 535 |
""" |
| 536 |
Parse the correction group. |
| 537 |
|
| 538 |
The following attributes are set: |
| 539 |
correction [string] |
| 540 |
""" |
| 541 |
self.correction = d["cor"] |
| 542 |
|
| 543 |
def _handleStation(self, d): |
| 544 |
""" |
| 545 |
Parse the station id group. |
| 546 |
|
| 547 |
The following attributes are set: |
| 548 |
station_id [string] |
| 549 |
""" |
| 550 |
self.station_id = d["station"] |
| 551 |
|
| 552 |
def _handleModifier(self, d): |
| 553 |
""" |
| 554 |
Parse the report-modifier group. |
| 555 |
|
| 556 |
The following attributes are set: |
| 557 |
mod [string] |
| 558 |
""" |
| 559 |
mod = d["mod"] |
| 560 |
if mod == "CORR": |
| 561 |
mod = "COR" |
| 562 |
if mod == "NIL" or mod == "FINO": |
| 563 |
mod = "NO DATA" |
| 564 |
self.mod = mod |
| 565 |
|
| 566 |
def _handleTime(self, d): |
| 567 |
""" |
| 568 |
Parse the observation-time group. |
| 569 |
|
| 570 |
The following attributes are set: |
| 571 |
time [datetime] |
| 572 |
cycle [int] |
| 573 |
_day [int] |
| 574 |
_hour [int] |
| 575 |
_min [int] |
| 576 |
""" |
| 577 |
self._day = int(d["day"]) |
| 578 |
if not self._month: |
| 579 |
self._month = self._now.month |
| 580 |
if self._day > self._now.day: |
| 581 |
if self._month == 1: |
| 582 |
self._month = 12 |
| 583 |
else: |
| 584 |
self._month = self._month - 1 |
| 585 |
if not self._year: |
| 586 |
self._year = self._now.year |
| 587 |
if self._month > self._now.month: |
| 588 |
self._year = self._year - 1 |
| 589 |
elif self._month == self._now.month and self._day > self._now.day: |
| 590 |
self._year = self._year - 1 |
| 591 |
self._hour = int(d["hour"]) |
| 592 |
self._min = int(d["min"]) |
| 593 |
self.time = datetime.datetime( |
| 594 |
self._year, self._month, self._day, self._hour, self._min |
| 595 |
) |
| 596 |
if self._min < 45: |
| 597 |
self.cycle = self._hour |
| 598 |
else: |
| 599 |
self.cycle = self._hour + 1 |
| 600 |
|
| 601 |
def _handleWind(self, d): |
| 602 |
""" |
| 603 |
Parse the wind and variable-wind groups. |
| 604 |
|
| 605 |
The following attributes are set: |
| 606 |
wind_dir [direction] |
| 607 |
wind_speed [speed] |
| 608 |
wind_gust [speed] |
| 609 |
wind_dir_from [int] |
| 610 |
wind_dir_to [int] |
| 611 |
""" |
| 612 |
wind_dir = d["dir"].replace("O", "0") |
| 613 |
if wind_dir != "VRB" and wind_dir != "///" and wind_dir != "MMM": |
| 614 |
self.wind_dir = direction(wind_dir) |
| 615 |
wind_speed = d["speed"].replace("O", "0") |
| 616 |
units = d["units"] |
| 617 |
|
| 618 |
if units is None and self.station_id is not None: |
| 619 |
|
| 620 |
if len(self.station_id) == 3 or self.station_id.startswith("K"): |
| 621 |
units = "KT" |
| 622 |
|
| 623 |
if units is None: |
| 624 |
units = "MPS" |
| 625 |
if units == "KTS" or units == "K" or units == "T" or units == "LT": |
| 626 |
units = "KT" |
| 627 |
if wind_speed.startswith("P"): |
| 628 |
self.wind_speed = speed(wind_speed[1:], units, ">") |
| 629 |
elif not MISSING_RE.match(wind_speed): |
| 630 |
self.wind_speed = speed(wind_speed, units) |
| 631 |
if d["gust"]: |
| 632 |
wind_gust = d["gust"] |
| 633 |
if wind_gust.startswith("P"): |
| 634 |
self.wind_gust = speed(wind_gust[1:], units, ">") |
| 635 |
elif not MISSING_RE.match(wind_gust): |
| 636 |
self.wind_gust = speed(wind_gust, units) |
| 637 |
if d["varfrom"]: |
| 638 |
self.wind_dir_from = direction(d["varfrom"]) |
| 639 |
self.wind_dir_to = direction(d["varto"]) |
| 640 |
|
| 641 |
def _handleVisibility(self, d): |
| 642 |
""" |
| 643 |
Parse the minimum and maximum visibility groups. |
| 644 |
|
| 645 |
The following attributes are set: |
| 646 |
vis [distance] |
| 647 |
vis_dir [direction] |
| 648 |
max_vis [distance] |
| 649 |
max_vis_dir [direction] |
| 650 |
""" |
| 651 |
vis = d["vis"] |
| 652 |
vis_less = None |
| 653 |
vis_dir = None |
| 654 |
vis_units = "M" |
| 655 |
vis_dist = "10000" |
| 656 |
if d["dist"] and d["dist"] != "////": |
| 657 |
vis_dist = d["dist"] |
| 658 |
if d["dir"] and d["dir"] != "NDV": |
| 659 |
vis_dir = d["dir"] |
| 660 |
elif d["distu"]: |
| 661 |
vis_dist = d["distu"] |
| 662 |
if d["units"] and d["units"] != "U": |
| 663 |
vis_units = d["units"] |
| 664 |
if vis_dist == "9999": |
| 665 |
vis_dist = "10000" |
| 666 |
vis_less = ">" |
| 667 |
if self.vis: |
| 668 |
if vis_dir: |
| 669 |
self.max_vis_dir = direction(vis_dir) |
| 670 |
self.max_vis = distance(vis_dist, vis_units, vis_less) |
| 671 |
else: |
| 672 |
if vis_dir: |
| 673 |
self.vis_dir = direction(vis_dir) |
| 674 |
self.vis = distance(vis_dist, vis_units, vis_less) |
| 675 |
|
| 676 |
def _handleRunway(self, d): |
| 677 |
""" |
| 678 |
Parse a runway visual range group. |
| 679 |
|
| 680 |
The following attributes are set: |
| 681 |
range [list of tuples] |
| 682 |
. name [string] |
| 683 |
. low [distance] |
| 684 |
. high [distance] |
| 685 |
. unit [string] |
| 686 |
""" |
| 687 |
if d["name"] is None: |
| 688 |
return |
| 689 |
unit = d["unit"] if d["unit"] is not None else "M" |
| 690 |
if d["low"] == "////": |
| 691 |
return |
| 692 |
else: |
| 693 |
low = distance(d["low"], unit) |
| 694 |
if d["high"] is None: |
| 695 |
high = low |
| 696 |
else: |
| 697 |
high = distance(d["high"], unit) |
| 698 |
self.runway.append([d["name"], low, high, unit]) |
| 699 |
|
| 700 |
def _handleWeather(self, d): |
| 701 |
""" |
| 702 |
Parse a present-weather group. |
| 703 |
|
| 704 |
The following attributes are set: |
| 705 |
weather [list of tuples] |
| 706 |
. intensity [string] |
| 707 |
. description [string] |
| 708 |
. precipitation [string] |
| 709 |
. obscuration [string] |
| 710 |
. other [string] |
| 711 |
""" |
| 712 |
inteni = d["int"] |
| 713 |
if not inteni and d["int2"]: |
| 714 |
inteni = d["int2"] |
| 715 |
desci = d["desc"] |
| 716 |
preci = d["prec"] |
| 717 |
obsci = d["obsc"] |
| 718 |
otheri = d["other"] |
| 719 |
self.weather.append((inteni, desci, preci, obsci, otheri)) |
| 720 |
|
| 721 |
def _handleSky(self, d): |
| 722 |
""" |
| 723 |
Parse a sky-conditions group. |
| 724 |
|
| 725 |
The following attributes are set: |
| 726 |
sky [list of tuples] |
| 727 |
. cover [string] |
| 728 |
. height [distance] |
| 729 |
. cloud [string] |
| 730 |
""" |
| 731 |
height = d["height"] |
| 732 |
if not height or height == "///": |
| 733 |
height = None |
| 734 |
else: |
| 735 |
height = height.replace("O", "0") |
| 736 |
height = distance(int(height) * 100, "FT") |
| 737 |
cover = d["cover"] |
| 738 |
if cover == "SCK" or cover == "SKC" or cover == "CL": |
| 739 |
cover = "CLR" |
| 740 |
if cover == "0VC": |
| 741 |
cover = "OVC" |
| 742 |
cloud = d["cloud"] |
| 743 |
if cloud == "///": |
| 744 |
cloud = "" |
| 745 |
self.sky.append((cover, height, cloud)) |
| 746 |
|
| 747 |
def _handleTemp(self, d): |
| 748 |
""" |
| 749 |
Parse a temperature-dewpoint group. |
| 750 |
|
| 751 |
The following attributes are set: |
| 752 |
temp temperature (Celsius) [float] |
| 753 |
dewpt dew point (Celsius) [float] |
| 754 |
""" |
| 755 |
temp = d["temp"] |
| 756 |
dewpt = d["dewpt"] |
| 757 |
if temp and temp != "//" and temp != "XX" and temp != "MM": |
| 758 |
self.temp = temperature(temp) |
| 759 |
if dewpt and dewpt != "//" and dewpt != "XX" and dewpt != "MM": |
| 760 |
self.dewpt = temperature(dewpt) |
| 761 |
|
| 762 |
def _handlePressure(self, d): |
| 763 |
""" |
| 764 |
Parse an altimeter-pressure group. |
| 765 |
|
| 766 |
The following attributes are set: |
| 767 |
press [int] |
| 768 |
""" |
| 769 |
press = d["press"] |
| 770 |
if press != "////": |
| 771 |
press = float(press.replace("O", "0")) |
| 772 |
if d["unit"]: |
| 773 |
if d["unit"] == "A" or (d["unit2"] and d["unit2"] == "INS"): |
| 774 |
self.press = pressure(press / 100, "IN") |
| 775 |
elif d["unit"] == "SLP": |
| 776 |
if press < 500: |
| 777 |
press = press / 10 + 1000 |
| 778 |
else: |
| 779 |
press = press / 10 + 900 |
| 780 |
self.press = pressure(press, "HPA") |
| 781 |
self._remarks.append("sea-level pressure %.1fhPa" % press) |
| 782 |
else: |
| 783 |
self.press = pressure(press, "HPA") |
| 784 |
elif press > 2500: |
| 785 |
self.press = pressure(press / 100, "IN") |
| 786 |
else: |
| 787 |
self.press = pressure(press, "HPA") |
| 788 |
|
| 789 |
def _handleRecent(self, d): |
| 790 |
""" |
| 791 |
Parse a recent-weather group. |
| 792 |
|
| 793 |
The following attributes are set: |
| 794 |
weather [list of tuples] |
| 795 |
. intensity [string] |
| 796 |
. description [string] |
| 797 |
. precipitation [string] |
| 798 |
. obscuration [string] |
| 799 |
. other [string] |
| 800 |
""" |
| 801 |
desci = d["desc"] |
| 802 |
preci = d["prec"] |
| 803 |
obsci = d["obsc"] |
| 804 |
otheri = d["other"] |
| 805 |
self.recent.append(("", desci, preci, obsci, otheri)) |
| 806 |
|
| 807 |
def _handleWindShear(self, d): |
| 808 |
""" |
| 809 |
Parse wind-shear groups. |
| 810 |
|
| 811 |
The following attributes are set: |
| 812 |
windshear [list of strings] |
| 813 |
""" |
| 814 |
if d["name"]: |
| 815 |
self.windshear.append(d["name"]) |
| 816 |
else: |
| 817 |
self.windshear.append("ALL") |
| 818 |
|
| 819 |
def _handleColor(self, d): |
| 820 |
""" |
| 821 |
Parse (and ignore) the color groups. |
| 822 |
|
| 823 |
The following attributes are set: |
| 824 |
trend [list of strings] |
| 825 |
""" |
| 826 |
pass |
| 827 |
|
| 828 |
def _handleRunwayState(self, d): |
| 829 |
""" |
| 830 |
Parse (and ignore) the runway state. |
| 831 |
|
| 832 |
The following attributes are set: |
| 833 |
""" |
| 834 |
pass |
| 835 |
|
| 836 |
def _handleTrend(self, d): |
| 837 |
""" |
| 838 |
Parse (and ignore) the trend groups. |
| 839 |
""" |
| 840 |
if "trend" in d: |
| 841 |
self._trend_groups.append(d["trend"]) |
| 842 |
self._trend = True |
| 843 |
|
| 844 |
def _startRemarks(self, d): |
| 845 |
""" |
| 846 |
Found the start of the remarks section. |
| 847 |
""" |
| 848 |
self._remarks = [] |
| 849 |
|
| 850 |
def _handleSealvlPressRemark(self, d): |
| 851 |
""" |
| 852 |
Parse the sea-level pressure remark group. |
| 853 |
""" |
| 854 |
value = float(d["press"]) / 10.0 |
| 855 |
if value < 50: |
| 856 |
value += 1000 |
| 857 |
else: |
| 858 |
value += 900 |
| 859 |
self.press_sea_level = pressure(value, "HPA") |
| 860 |
|
| 861 |
def _handlePrecip24hrRemark(self, d): |
| 862 |
""" |
| 863 |
Parse a 3-, 6- or 24-hour cumulative preciptation remark group. |
| 864 |
""" |
| 865 |
value = float(d["precip"]) / 100.0 |
| 866 |
if d["type"] == "6": |
| 867 |
if self.cycle in [3, 9, 15, 21]: |
| 868 |
self.precip_3hr = precipitation(value, "IN") |
| 869 |
else: |
| 870 |
self.precip_6hr = precipitation(value, "IN") |
| 871 |
else: |
| 872 |
self.precip_24hr = precipitation(value, "IN") |
| 873 |
|
| 874 |
def _handlePrecip1hrRemark(self, d): |
| 875 |
"""Parse an hourly precipitation remark group.""" |
| 876 |
value = float(d["precip"]) / 100.0 |
| 877 |
self.precip_1hr = precipitation(value, "IN") |
| 878 |
|
| 879 |
def _handleTemp1hrRemark(self, d): |
| 880 |
""" |
| 881 |
Parse a temperature & dewpoint remark group. |
| 882 |
|
| 883 |
These values replace the temp and dewpt from the body of the report. |
| 884 |
""" |
| 885 |
value = float(d["temp"]) / 10.0 |
| 886 |
if d["tsign"] == "1": |
| 887 |
value = -value |
| 888 |
self.temp = temperature(value) |
| 889 |
if d["dewpt"]: |
| 890 |
value2 = float(d["dewpt"]) / 10.0 |
| 891 |
if d["dsign"] == "1": |
| 892 |
value2 = -value2 |
| 893 |
self.dewpt = temperature(value2) |
| 894 |
|
| 895 |
def _handleTemp6hrRemark(self, d): |
| 896 |
""" |
| 897 |
Parse a 6-hour maximum or minimum temperature remark group. |
| 898 |
""" |
| 899 |
value = float(d["temp"]) / 10.0 |
| 900 |
if d["sign"] == "1": |
| 901 |
value = -value |
| 902 |
if d["type"] == "1": |
| 903 |
self.max_temp_6hr = temperature(value, "C") |
| 904 |
else: |
| 905 |
self.min_temp_6hr = temperature(value, "C") |
| 906 |
|
| 907 |
def _handleTemp24hrRemark(self, d): |
| 908 |
""" |
| 909 |
Parse a 24-hour maximum/minimum temperature remark group. |
| 910 |
""" |
| 911 |
value = float(d["maxt"]) / 10.0 |
| 912 |
if d["smaxt"] == "1": |
| 913 |
value = -value |
| 914 |
value2 = float(d["mint"]) / 10.0 |
| 915 |
if d["smint"] == "1": |
| 916 |
value2 = -value2 |
| 917 |
self.max_temp_24hr = temperature(value, "C") |
| 918 |
self.min_temp_24hr = temperature(value2, "C") |
| 919 |
|
| 920 |
def _handlePress3hrRemark(self, d): |
| 921 |
""" |
| 922 |
Parse a pressure-tendency remark group. |
| 923 |
""" |
| 924 |
value = float(d["press"]) / 10.0 |
| 925 |
descrip = PRESSURE_TENDENCY[d["tend"]] |
| 926 |
self._remarks.append("3-hr pressure change %.1fhPa, %s" % (value, descrip)) |
| 927 |
|
| 928 |
def _handlePeakWindRemark(self, d): |
| 929 |
""" |
| 930 |
Parse a peak wind remark group. |
| 931 |
""" |
| 932 |
peak_dir = int(d["dir"]) |
| 933 |
peak_speed = int(d["speed"]) |
| 934 |
self.wind_speed_peak = speed(peak_speed, "KT") |
| 935 |
self.wind_dir_peak = direction(peak_dir) |
| 936 |
peak_min = int(d["min"]) |
| 937 |
if d["hour"]: |
| 938 |
peak_hour = int(d["hour"]) |
| 939 |
else: |
| 940 |
peak_hour = self._hour |
| 941 |
self.peak_wind_time = datetime.datetime( |
| 942 |
self._year, self._month, self._day, peak_hour, peak_min |
| 943 |
) |
| 944 |
if self.peak_wind_time > self.time: |
| 945 |
if peak_hour > self._hour: |
| 946 |
self.peak_wind_time -= datetime.timedelta(hours=24) |
| 947 |
else: |
| 948 |
self.peak_wind_time -= datetime.timedelta(hours=1) |
| 949 |
self._remarks.append( |
| 950 |
"peak wind %dkt from %d degrees at %d:%02d" |
| 951 |
% (peak_speed, peak_dir, peak_hour, peak_min) |
| 952 |
) |
| 953 |
|
| 954 |
def _handleWindShiftRemark(self, d): |
| 955 |
""" |
| 956 |
Parse a wind shift remark group. |
| 957 |
""" |
| 958 |
if d["hour"]: |
| 959 |
wshft_hour = int(d["hour"]) |
| 960 |
else: |
| 961 |
wshft_hour = self._hour |
| 962 |
wshft_min = int(d["min"]) |
| 963 |
self.wind_shift_time = datetime.datetime( |
| 964 |
self._year, self._month, self._day, wshft_hour, wshft_min |
| 965 |
) |
| 966 |
if self.wind_shift_time > self.time: |
| 967 |
if wshft_hour > self._hour: |
| 968 |
self.wind_shift_time -= datetime.timedelta(hours=24) |
| 969 |
else: |
| 970 |
self.wind_shift_time -= datetime.timedelta(hours=1) |
| 971 |
text = "wind shift at %d:%02d" % (wshft_hour, wshft_min) |
| 972 |
if d["front"]: |
| 973 |
text += " (front)" |
| 974 |
self._remarks.append(text) |
| 975 |
|
| 976 |
def _handleLightningRemark(self, d): |
| 977 |
""" |
| 978 |
Parse a lightning observation remark group. |
| 979 |
""" |
| 980 |
parts = [] |
| 981 |
if d["freq"]: |
| 982 |
parts.append(LIGHTNING_FREQUENCY[d["freq"]]) |
| 983 |
parts.append("lightning") |
| 984 |
if d["type"]: |
| 985 |
ltg_types = [] |
| 986 |
group = d["type"] |
| 987 |
while group: |
| 988 |
ltg_types.append(LIGHTNING_TYPE[group[:2]]) |
| 989 |
group = group[2:] |
| 990 |
parts.append("(" + ",".join(ltg_types) + ")") |
| 991 |
if d["loc"]: |
| 992 |
parts.append(xlate_loc(d["loc"])) |
| 993 |
self._remarks.append(" ".join(parts)) |
| 994 |
|
| 995 |
def _handleTSLocRemark(self, d): |
| 996 |
""" |
| 997 |
Parse a thunderstorm location remark group. |
| 998 |
""" |
| 999 |
text = "thunderstorm" |
| 1000 |
if d["loc"]: |
| 1001 |
text += " " + xlate_loc(d["loc"]) |
| 1002 |
if d["dir"]: |
| 1003 |
text += " moving %s" % d["dir"] |
| 1004 |
self._remarks.append(text) |
| 1005 |
|
| 1006 |
def _handleAutoRemark(self, d): |
| 1007 |
""" |
| 1008 |
Parse an automatic station remark group. |
| 1009 |
""" |
| 1010 |
if d["type"] == "1": |
| 1011 |
self._remarks.append("Automated station") |
| 1012 |
elif d["type"] == "2": |
| 1013 |
self._remarks.append("Automated station (type 2)") |
| 1014 |
|
| 1015 |
def _handleSnowDepthRemark(self, d): |
| 1016 |
""" |
| 1017 |
Parse the 4/ group snowdepth report |
| 1018 |
""" |
| 1019 |
self.snowdepth = distance(float(d["snowdepth"]), "IN") |
| 1020 |
self._remarks.append(" snowdepth %s" % (self.snowdepth,)) |
| 1021 |
|
| 1022 |
def _handleIceAccretionRemark(self, d): |
| 1023 |
""" |
| 1024 |
Parse the I/ group ice accretion report. |
| 1025 |
""" |
| 1026 |
myattr = "ice_accretion_%shr" % (d["ice_accretion_hours"],) |
| 1027 |
value = precipitation(float(d["ice_accretion_depth"]) / 100.0, "IN") |
| 1028 |
setattr(self, myattr, value) |
| 1029 |
|
| 1030 |
def _unparsedRemark(self, d): |
| 1031 |
""" |
| 1032 |
Handle otherwise unparseable remark groups. |
| 1033 |
""" |
| 1034 |
self._unparsed_remarks.append(d["group"]) |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
handlers = [ |
| 1039 |
(TYPE_RE, _handleType, False), |
| 1040 |
(COR_RE, _handleCorrection, False), |
| 1041 |
(STATION_RE, _handleStation, False), |
| 1042 |
(TIME_RE, _handleTime, False), |
| 1043 |
(MODIFIER_RE, _handleModifier, False), |
| 1044 |
(WIND_RE, _handleWind, False), |
| 1045 |
(VISIBILITY_RE, _handleVisibility, True), |
| 1046 |
(RUNWAY_RE, _handleRunway, True), |
| 1047 |
(WEATHER_RE, _handleWeather, True), |
| 1048 |
(SKY_RE, _handleSky, True), |
| 1049 |
(WIND_RE, _handleWind, False), |
| 1050 |
(VISIBILITY_RE, _handleVisibility, True), |
| 1051 |
(TEMP_RE, _handleTemp, False), |
| 1052 |
(PRESS_RE, _handlePressure, True), |
| 1053 |
(SEALVL_PRESS_RE, _handleSealvlPressRemark, False), |
| 1054 |
(RECENT_RE, _handleRecent, True), |
| 1055 |
(WINDSHEAR_RE, _handleWindShear, True), |
| 1056 |
(COLOR_RE, _handleColor, True), |
| 1057 |
(RUNWAYSTATE_RE, _handleRunwayState, True), |
| 1058 |
(TREND_RE, _handleTrend, True), |
| 1059 |
(REMARK_RE, _startRemarks, False), |
| 1060 |
] |
| 1061 |
|
| 1062 |
trend_handlers = [ |
| 1063 |
(TRENDTIME_RE, _handleTrend, True), |
| 1064 |
(WIND_RE, _handleTrend, True), |
| 1065 |
(VISIBILITY_RE, _handleTrend, True), |
| 1066 |
(WEATHER_RE, _handleTrend, True), |
| 1067 |
(SKY_RE, _handleTrend, True), |
| 1068 |
(COLOR_RE, _handleTrend, True), |
| 1069 |
] |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
remark_handlers = [ |
| 1075 |
(AUTO_RE, _handleAutoRemark), |
| 1076 |
(SEALVL_PRESS_RE, _handleSealvlPressRemark), |
| 1077 |
(PEAK_WIND_RE, _handlePeakWindRemark), |
| 1078 |
(WIND_SHIFT_RE, _handleWindShiftRemark), |
| 1079 |
(LIGHTNING_RE, _handleLightningRemark), |
| 1080 |
(TS_LOC_RE, _handleTSLocRemark), |
| 1081 |
(TEMP_1HR_RE, _handleTemp1hrRemark), |
| 1082 |
(PRECIP_1HR_RE, _handlePrecip1hrRemark), |
| 1083 |
(PRECIP_24HR_RE, _handlePrecip24hrRemark), |
| 1084 |
(PRESS_3HR_RE, _handlePress3hrRemark), |
| 1085 |
(TEMP_6HR_RE, _handleTemp6hrRemark), |
| 1086 |
(TEMP_24HR_RE, _handleTemp24hrRemark), |
| 1087 |
(SNOWDEPTH_RE, _handleSnowDepthRemark), |
| 1088 |
(ICE_ACCRETION_RE, _handleIceAccretionRemark), |
| 1089 |
(UNPARSED_RE, _unparsedRemark), |
| 1090 |
] |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
def string(self): |
| 1095 |
""" |
| 1096 |
Return a human-readable version of the decoded report. |
| 1097 |
""" |
| 1098 |
lines = [] |
| 1099 |
lines.append("station: %s" % self.station_id) |
| 1100 |
if self.type: |
| 1101 |
lines.append("type: %s" % self.report_type()) |
| 1102 |
if self.time: |
| 1103 |
lines.append("time: %s" % self.time.ctime()) |
| 1104 |
if self.temp: |
| 1105 |
lines.append("temperature: %s" % self.temp.string("C")) |
| 1106 |
if self.dewpt: |
| 1107 |
lines.append("dew point: %s" % self.dewpt.string("C")) |
| 1108 |
if self.wind_speed: |
| 1109 |
lines.append("wind: %s" % self.wind()) |
| 1110 |
if self.wind_speed_peak: |
| 1111 |
lines.append("peak wind: %s" % self.peak_wind()) |
| 1112 |
if self.wind_shift_time: |
| 1113 |
lines.append("wind shift: %s" % self.wind_shift()) |
| 1114 |
if self.vis: |
| 1115 |
lines.append("visibility: %s" % self.visibility()) |
| 1116 |
if self.runway: |
| 1117 |
lines.append("visual range: %s" % self.runway_visual_range()) |
| 1118 |
if self.press: |
| 1119 |
lines.append("pressure: %s" % self.press.string("hPa")) |
| 1120 |
if self.weather: |
| 1121 |
lines.append("weather: %s" % self.present_weather()) |
| 1122 |
if self.sky: |
| 1123 |
lines.append("sky: %s" % self.sky_conditions("\n ")) |
| 1124 |
if self.press_sea_level: |
| 1125 |
lines.append("sea-level pressure: %s" % self.press_sea_level.string("hPa")) |
| 1126 |
if self.max_temp_6hr: |
| 1127 |
lines.append("6-hour max temp: %s" % str(self.max_temp_6hr)) |
| 1128 |
if self.max_temp_6hr: |
| 1129 |
lines.append("6-hour min temp: %s" % str(self.min_temp_6hr)) |
| 1130 |
if self.max_temp_24hr: |
| 1131 |
lines.append("24-hour max temp: %s" % str(self.max_temp_24hr)) |
| 1132 |
if self.max_temp_24hr: |
| 1133 |
lines.append("24-hour min temp: %s" % str(self.min_temp_24hr)) |
| 1134 |
if self.precip_1hr: |
| 1135 |
lines.append("1-hour precipitation: %s" % str(self.precip_1hr)) |
| 1136 |
if self.precip_3hr: |
| 1137 |
lines.append("3-hour precipitation: %s" % str(self.precip_3hr)) |
| 1138 |
if self.precip_6hr: |
| 1139 |
lines.append("6-hour precipitation: %s" % str(self.precip_6hr)) |
| 1140 |
if self.precip_24hr: |
| 1141 |
lines.append("24-hour precipitation: %s" % str(self.precip_24hr)) |
| 1142 |
if self.ice_accretion_1hr: |
| 1143 |
lines.append("1-hour Ice Accretion: %s" % str(self.ice_accretion_1hr)) |
| 1144 |
if self.ice_accretion_3hr: |
| 1145 |
lines.append("3-hour Ice Accretion: %s" % str(self.ice_accretion_3hr)) |
| 1146 |
if self.ice_accretion_6hr: |
| 1147 |
lines.append("6-hour Ice Accretion: %s" % str(self.ice_accretion_6hr)) |
| 1148 |
if self._remarks: |
| 1149 |
lines.append("remarks:") |
| 1150 |
lines.append("- " + self.remarks("\n- ")) |
| 1151 |
if self._unparsed_remarks: |
| 1152 |
lines.append("- " + " ".join(self._unparsed_remarks)) |
| 1153 |
lines.append("METAR: " + self.code) |
| 1154 |
return "\n".join(lines) |
| 1155 |
|
| 1156 |
def report_type(self): |
| 1157 |
""" |
| 1158 |
Return a textual description of the report type. |
| 1159 |
""" |
| 1160 |
if self.type is None: |
| 1161 |
text = "unknown report type" |
| 1162 |
elif self.type in REPORT_TYPE: |
| 1163 |
text = REPORT_TYPE[self.type] |
| 1164 |
else: |
| 1165 |
text = self.type + " report" |
| 1166 |
if self.cycle: |
| 1167 |
text += ", cycle %d" % self.cycle |
| 1168 |
if self.mod: |
| 1169 |
if self.mod in REPORT_TYPE: |
| 1170 |
text += " (%s)" % REPORT_TYPE[self.mod] |
| 1171 |
else: |
| 1172 |
text += " (%s)" % self.mod |
| 1173 |
if self.correction: |
| 1174 |
text += " (%s)" % REPORT_TYPE[self.correction] |
| 1175 |
return text |
| 1176 |
|
| 1177 |
def wind(self, units="KT"): |
| 1178 |
""" |
| 1179 |
Return a textual description of the wind conditions. |
| 1180 |
|
| 1181 |
Units may be specified as "MPS", "KT", "KMH", or "MPH". |
| 1182 |
""" |
| 1183 |
if self.wind_speed is None: |
| 1184 |
return "missing" |
| 1185 |
elif self.wind_speed.value() == 0.0: |
| 1186 |
text = "calm" |
| 1187 |
else: |
| 1188 |
wind_speed = self.wind_speed.string(units) |
| 1189 |
if not self.wind_dir: |
| 1190 |
text = "variable at %s" % wind_speed |
| 1191 |
elif self.wind_dir_from: |
| 1192 |
text = "%s to %s at %s" % ( |
| 1193 |
self.wind_dir_from.compass(), |
| 1194 |
self.wind_dir_to.compass(), |
| 1195 |
wind_speed, |
| 1196 |
) |
| 1197 |
else: |
| 1198 |
text = "%s at %s" % (self.wind_dir.compass(), wind_speed) |
| 1199 |
if self.wind_gust: |
| 1200 |
text += ", gusting to %s" % self.wind_gust.string(units) |
| 1201 |
return text |
| 1202 |
|
| 1203 |
def peak_wind(self, units="KT"): |
| 1204 |
""" |
| 1205 |
Return a textual description of the peak wind conditions. |
| 1206 |
|
| 1207 |
Units may be specified as "MPS", "KT", "KMH", or "MPH". |
| 1208 |
""" |
| 1209 |
if self.wind_speed_peak is None: |
| 1210 |
return "missing" |
| 1211 |
elif self.wind_speed_peak.value() == 0.0: |
| 1212 |
text = "calm" |
| 1213 |
else: |
| 1214 |
wind_speed = self.wind_speed_peak.string(units) |
| 1215 |
if not self.wind_dir_peak: |
| 1216 |
text = wind_speed |
| 1217 |
else: |
| 1218 |
text = "%s at %s" % (self.wind_dir_peak.compass(), wind_speed) |
| 1219 |
if self.peak_wind_time is not None: |
| 1220 |
text += " at %s" % self.peak_wind_time.strftime("%H:%M") |
| 1221 |
return text |
| 1222 |
|
| 1223 |
def wind_shift(self, units="KT"): |
| 1224 |
""" |
| 1225 |
Return a textual description of the wind shift time |
| 1226 |
|
| 1227 |
Units may be specified as "MPS", "KT", "KMH", or "MPH". |
| 1228 |
""" |
| 1229 |
if self.wind_shift_time is None: |
| 1230 |
return "missing" |
| 1231 |
else: |
| 1232 |
return self.wind_shift_time.strftime("%H:%M") |
| 1233 |
|
| 1234 |
def visibility(self, units=None): |
| 1235 |
""" |
| 1236 |
Return a textual description of the visibility. |
| 1237 |
|
| 1238 |
Units may be statute miles ("SM") or meters ("M"). |
| 1239 |
""" |
| 1240 |
if self.vis is None: |
| 1241 |
return "missing" |
| 1242 |
if self.vis_dir: |
| 1243 |
text = "%s to %s" % (self.vis.string(units), self.vis_dir.compass()) |
| 1244 |
else: |
| 1245 |
text = self.vis.string(units) |
| 1246 |
if self.max_vis: |
| 1247 |
if self.max_vis_dir: |
| 1248 |
text += "; %s to %s" % ( |
| 1249 |
self.max_vis.string(units), |
| 1250 |
self.max_vis_dir.compass(), |
| 1251 |
) |
| 1252 |
else: |
| 1253 |
text += "; %s" % self.max_vis.string(units) |
| 1254 |
return text |
| 1255 |
|
| 1256 |
def runway_visual_range(self, units=None): |
| 1257 |
""" |
| 1258 |
Return a textual description of the runway visual range. |
| 1259 |
""" |
| 1260 |
lines = [] |
| 1261 |
for name, low, high, unit in self.runway: |
| 1262 |
reportunits = unit if units is None else units |
| 1263 |
if low != high: |
| 1264 |
lines.append( |
| 1265 |
("on runway %s, from %d to %s") |
| 1266 |
% (name, low.value(reportunits), high.string(reportunits)) |
| 1267 |
) |
| 1268 |
else: |
| 1269 |
lines.append("on runway %s, %s" % (name, low.string(reportunits))) |
| 1270 |
return "; ".join(lines) |
| 1271 |
|
| 1272 |
def present_weather(self): |
| 1273 |
""" |
| 1274 |
Return a textual description of the present weather. |
| 1275 |
""" |
| 1276 |
return self._weather(self.weather) |
| 1277 |
|
| 1278 |
def recent_weather(self): |
| 1279 |
""" |
| 1280 |
Return a textual description of the recent weather. |
| 1281 |
""" |
| 1282 |
return self._weather(self.recent) |
| 1283 |
|
| 1284 |
def _weather(self, weather): |
| 1285 |
""" |
| 1286 |
Return a textual description of weather. |
| 1287 |
""" |
| 1288 |
text_list = [] |
| 1289 |
for weatheri in weather: |
| 1290 |
(inteni, desci, preci, obsci, otheri) = weatheri |
| 1291 |
text_parts = [] |
| 1292 |
code_parts = [] |
| 1293 |
if inteni: |
| 1294 |
code_parts.append(inteni) |
| 1295 |
text_parts.append(WEATHER_INT[inteni]) |
| 1296 |
if desci: |
| 1297 |
code_parts.append(desci) |
| 1298 |
if desci != "SH" or not preci: |
| 1299 |
text_parts.append(WEATHER_DESC[desci[0:2]]) |
| 1300 |
if len(desci) == 4: |
| 1301 |
text_parts.append(WEATHER_DESC[desci[2:]]) |
| 1302 |
if preci: |
| 1303 |
code_parts.append(preci) |
| 1304 |
if len(preci) == 2: |
| 1305 |
precip_text = WEATHER_PREC[preci] |
| 1306 |
elif len(preci) == 4: |
| 1307 |
precip_text = WEATHER_PREC[preci[:2]] + " and " |
| 1308 |
precip_text += WEATHER_PREC[preci[2:]] |
| 1309 |
elif len(preci) == 6: |
| 1310 |
precip_text = WEATHER_PREC[preci[:2]] + ", " |
| 1311 |
precip_text += WEATHER_PREC[preci[2:4]] + " and " |
| 1312 |
precip_text += WEATHER_PREC[preci[4:]] |
| 1313 |
else: |
| 1314 |
precip_text = preci |
| 1315 |
if desci == "TS": |
| 1316 |
text_parts.append("with") |
| 1317 |
text_parts.append(precip_text) |
| 1318 |
if desci == "SH": |
| 1319 |
text_parts.append(WEATHER_DESC[desci]) |
| 1320 |
if obsci: |
| 1321 |
code_parts.append(obsci) |
| 1322 |
text_parts.append(WEATHER_OBSC[obsci]) |
| 1323 |
|
| 1324 |
if otheri: |
| 1325 |
code_parts.append(otheri) |
| 1326 |
text_parts.append(WEATHER_OTHER[otheri]) |
| 1327 |
code = " ".join(code_parts) |
| 1328 |
if code in WEATHER_SPECIAL: |
| 1329 |
text_list.append(WEATHER_SPECIAL[code]) |
| 1330 |
else: |
| 1331 |
text_list.append(" ".join(text_parts)) |
| 1332 |
return "; ".join(text_list) |
| 1333 |
|
| 1334 |
def sky_conditions(self, sep="; "): |
| 1335 |
""" |
| 1336 |
Return a textual description of the sky conditions. |
| 1337 |
""" |
| 1338 |
text_list = [] |
| 1339 |
for skyi in self.sky: |
| 1340 |
(cover, height, cloud) = skyi |
| 1341 |
if cover in ["SKC", "CLR", "NSC"]: |
| 1342 |
text_list.append(SKY_COVER[cover]) |
| 1343 |
else: |
| 1344 |
if cloud: |
| 1345 |
what = CLOUD_TYPE.get(cloud, "unknown CLOUD_TYPE of %s" % (cloud,)) |
| 1346 |
elif SKY_COVER[cover].endswith(" "): |
| 1347 |
what = "clouds" |
| 1348 |
else: |
| 1349 |
what = "" |
| 1350 |
label = "%s %s" % (SKY_COVER[cover], what) |
| 1351 |
|
| 1352 |
label = " ".join(label.strip().split()) |
| 1353 |
if cover == "VV": |
| 1354 |
label += ", vertical visibility to %s" % (str(height),) |
| 1355 |
else: |
| 1356 |
label += " at %s" % (str(height),) |
| 1357 |
text_list.append(label) |
| 1358 |
return sep.join(text_list) |
| 1359 |
|
| 1360 |
def trend(self): |
| 1361 |
""" |
| 1362 |
Return the trend forecast groups |
| 1363 |
""" |
| 1364 |
return " ".join(self._trend_groups) |
| 1365 |
|
| 1366 |
def remarks(self, sep="; "): |
| 1367 |
""" |
| 1368 |
Return the decoded remarks. |
| 1369 |
""" |
| 1370 |
return sep.join(self._remarks) |