1 """Contains various object definitions needed by the weather utility."""
3 weather_copyright = """\
4 # Copyright (c) 2006-2024 Jeremy Stanley <fungi@yuggoth.org>. Permission to
5 # use, copy, modify, and distribute this software is granted under terms
6 # provided in the LICENSE file distributed with this software.
9 weather_version = "2.4.4"
11 radian_to_km = 6372.795484
12 radian_to_mi = 3959.871528
15 """An object to contain selection data."""
17 """Store the config, options and arguments."""
18 self.config = get_config()
19 self.options, self.arguments = get_options(self.config)
20 if self.get_bool("cache") and self.get_bool("cache_search") \
21 and not self.get_bool("longlist"):
22 integrate_search_cache(
27 if not self.arguments:
28 if "id" in self.options.__dict__ \
29 and self.options.__dict__["id"]:
30 self.arguments.append( self.options.__dict__["id"] )
31 del( self.options.__dict__["id"] )
33 message = "WARNING: the --id option is deprecated and will eventually be removed\n"
34 sys.stderr.write(message)
35 elif "city" in self.options.__dict__ \
36 and self.options.__dict__["city"] \
37 and "st" in self.options.__dict__ \
38 and self.options.__dict__["st"]:
39 self.arguments.append(
41 self.options.__dict__["city"],
42 self.options.__dict__["st"]
45 del( self.options.__dict__["city"] )
46 del( self.options.__dict__["st"] )
48 message = "WARNING: the --city/--st options are deprecated and will eventually be removed\n"
49 sys.stderr.write(message)
50 def get(self, option, argument=None):
51 """Retrieve data from the config or options."""
53 if self.config.has_section(argument) and (
54 self.config.has_option(argument, "city") \
55 or self.config.has_option(argument, "id") \
56 or self.config.has_option(argument, "st")
58 self.config.remove_section(argument)
60 message = "WARNING: the city/id/st options are now unsupported in aliases\n"
61 sys.stderr.write(message)
62 if not self.config.has_section(argument):
65 path=self.get("setpath"),
66 info=self.get("info"),
68 self.get("cache") and self.get("cache_search")
70 cachedir=self.get("cachedir"),
71 quiet=self.get_bool("quiet")
73 self.config.add_section(argument)
74 for item in guessed.items():
75 self.config.set(argument, *item)
76 if self.config.has_option(argument, option):
77 return self.config.get(argument, option)
78 if option in self.options.__dict__:
79 return self.options.__dict__[option]
81 message = "WARNING: no URI defined for %s\n" % option
82 sys.stderr.write(message)
84 def get_bool(self, option, argument=None):
85 """Get data and coerce to a boolean if necessary."""
86 # Mimic configparser's getboolean() method by treating
87 # false/no/off/0 as False and true/yes/on/1 as True values,
89 value = self.get(option, argument)
90 if isinstance(value, bool):
92 if isinstance(value, str):
93 vlower = value.lower()
94 if vlower in ('false', 'no', 'off', '0'):
96 elif vlower in ('true', 'yes', 'on', '1'):
98 raise ValueError("Not a boolean: %s" % value)
99 def getint(self, option, argument=None):
100 """Get data and coerce to an integer if necessary."""
101 value = self.get(option, argument)
102 if value: return int(value)
106 """Average a list of coordinates."""
113 return (x/count, y/count)
115 def filter_units(line, units="imperial"):
116 """Filter or convert units in a line of text between US/UK and metric."""
118 # filter lines with both pressures in the form of "X inches (Y hPa)" or
121 r"(.* )(\d*(\.\d+)? (inches|in\. Hg)) \((\d*(\.\d+)? hPa)\)(.*)",
125 preamble, in_hg, i_fr, i_un, hpa, h_fr, trailer = dual_p.groups()
126 if units == "imperial": line = preamble + in_hg + trailer
127 elif units == "metric": line = preamble + hpa + trailer
128 # filter lines with both temperatures in the form of "X F (Y C)"
130 r"(.* )(-?\d*(\.\d+)? F) \((-?\d*(\.\d+)? C)\)(.*)",
134 preamble, fahrenheit, f_fr, celsius, c_fr, trailer = dual_t.groups()
135 if units == "imperial": line = preamble + fahrenheit + trailer
136 elif units == "metric": line = preamble + celsius + trailer
137 # if metric is desired, convert distances in the form of "X mile(s)" to
139 if units == "metric":
140 imperial_d = re.match(
141 r"(.* )(\d+)( mile\(s\))(.*)",
145 preamble, mi, m_u, trailer = imperial_d.groups()
146 line = preamble + str(int(round(int(mi)*1.609344))) \
147 + " kilometer(s)" + trailer
148 # filter speeds in the form of "X MPH (Y KT)" to just "X MPH"; if metric is
149 # desired, convert to "Z KPH"
150 imperial_s = re.match(
151 r"(.* )(\d+)( MPH)( \(\d+ KT\))(.*)",
155 preamble, mph, m_u, kt, trailer = imperial_s.groups()
156 if units == "imperial": line = preamble + mph + m_u + trailer
157 elif units == "metric":
158 line = preamble + str(int(round(int(mph)*1.609344))) + " KPH" + \
160 imperial_s = re.match(
161 r"(.* )(\d+)( MPH)( \(\d+ KT\))(.*)",
165 preamble, mph, m_u, kt, trailer = imperial_s.groups()
166 if units == "imperial": line = preamble + mph + m_u + trailer
167 elif units == "metric":
168 line = preamble + str(int(round(int(mph)*1.609344))) + " KPH" + \
170 # if imperial is desired, qualify given forcast temperatures like "X F"; if
171 # metric is desired, convert to "Y C"
172 imperial_t = re.match(
173 r"(.* )(High |high |Low |low )(\d+)(\.|,)(.*)",
177 preamble, parameter, fahrenheit, sep, trailer = imperial_t.groups()
178 if units == "imperial":
179 line = preamble + parameter + fahrenheit + " F" + sep + trailer
180 elif units == "metric":
181 line = preamble + parameter \
182 + str(int(round((int(fahrenheit)-32)*5/9))) + " C" + sep \
184 # hand off the resulting line
194 """Return a string containing the results of a URI GET."""
195 import os, time, urllib, urllib.error, urllib.request
197 dcachedir = os.path.join( os.path.expanduser(cachedir), "datacache" )
198 if not os.path.exists(dcachedir):
199 try: os.makedirs(dcachedir)
200 except (IOError, OSError): pass
201 dcache_fn = os.path.join(
203 uri.split(":",1)[1].replace("/","_")
206 if cache_data and os.access(dcache_fn, os.R_OK) \
207 and now-cacheage < os.stat(dcache_fn).st_mtime <= now:
208 dcache_fd = open(dcache_fn)
209 data = dcache_fd.read()
213 data = urllib.request.urlopen(uri).read().decode("utf-8")
214 except urllib.error.URLError:
215 if ignore_fail: return ""
217 sys.stderr.write("%s error: failed to retrieve\n %s\n\n" % (
218 os.path.basename( sys.argv[0] ), uri))
220 # Some data sources are HTML with the plain text wrapped in pre tags
222 data = data[data.find("<pre>")+5:data.find("</pre>")]
226 dcache_fd = codecs.open(dcache_fn, "w", "utf-8")
227 dcache_fd.write(data)
229 except (IOError, OSError): pass
243 """Return a summarized METAR for the specified station."""
246 message = "%s error: METAR URI required for conditions\n" % \
247 os.path.basename( sys.argv[0] )
248 sys.stderr.write(message)
252 cache_data=cache_data,
256 if type(metar) is bytes: metar = metar.decode("utf-8")
257 if verbose: return metar
260 lines = metar.split("\n")
263 "relative_humidity," \
264 + "precipitation_last_hour," \
265 + "sky conditions," \
271 headerlist = headers.lower().replace("_"," ").split(",")
274 title = "Current conditions at %s"
275 place = lines[0].split(", ")
277 place = "%s, %s" % ( place[0].title(), place[1] )
278 else: place = "<UNKNOWN>"
279 output.append(title%place)
280 output.append("Last updated " + lines[1])
282 for header in headerlist:
284 if line.lower().startswith(header + ":"):
285 if re.match(r".*:\d+$", line): line = line[:line.rfind(":")]
286 if imperial: line = filter_units(line, units="imperial")
287 elif metric: line = filter_units(line, units="metric")
288 if quiet: output.append(line)
289 else: output.append(" " + line)
293 "(no conditions matched your header list, try with --verbose)"
295 return "\n".join(output)
306 """Return alert notice for the specified URI."""
312 cache_data=cache_data,
316 if type(alert) is bytes: alert = alert.decode("utf-8")
318 if verbose: return alert
321 if re.search(r"\nNational Weather Service", alert):
325 expirycheck = re.search(r"Expires:([0-9]{12})", alert)
327 # only report alerts and forecasts that expired less than delay
329 import datetime, zoneinfo
330 expiration = datetime.datetime.fromisoformat(
331 "%sT%sZ" % (expirycheck[1][:8], expirycheck[1][-4:]))
332 now = datetime.datetime.now(tz=zoneinfo.ZoneInfo("UTC"))
333 if now - expiration > datetime.timedelta(hours=delay):
335 lines = alert.split("\n")
338 if muted and line.startswith("National Weather Service"):
345 if line and not muted:
346 if quiet: output.append(line)
347 else: output.append(" " + line)
348 return "\n".join(output)
350 def get_options(config):
351 """Parse the options passed on the command line."""
353 # for optparse's builtin -h/--help option
355 "usage: %prog [options] [alias1|search1 [alias2|search2 [...]]]"
357 # for optparse's builtin --version option
358 verstring = "%prog " + weather_version
362 option_parser = optparse.OptionParser(usage=usage, version=verstring)
363 # separate options object from list of arguments and return both
365 # the -a/--alert option
366 if config.has_option("default", "alert"):
367 default_alert = config.getboolean("default", "alert")
368 else: default_alert = False
369 option_parser.add_option("-a", "--alert",
372 default=default_alert,
373 help="include local alert notices")
375 # the --atypes option
376 if config.has_option("default", "atypes"):
377 default_atypes = config.get("default", "atypes")
380 "coastal_flood_statement," \
381 + "flash_flood_statement," \
382 + "flash_flood_warning," \
383 + "flash_flood_watch," \
385 + "severe_thunderstorm_warning," \
386 + "severe_weather_statement," \
387 + "special_weather_statement," \
389 + "urgent_weather_message"
390 option_parser.add_option("--atypes",
392 default=default_atypes,
393 help="list of alert notification types to display")
395 # the --build-sets option
396 option_parser.add_option("--build-sets",
400 help="(re)build location correlation sets")
402 # the --cacheage option
403 if config.has_option("default", "cacheage"):
404 default_cacheage = config.getint("default", "cacheage")
405 else: default_cacheage = 900
406 option_parser.add_option("--cacheage",
408 default=default_cacheage,
409 help="duration in seconds to refresh cached data")
411 # the --cachedir option
412 if config.has_option("default", "cachedir"):
413 default_cachedir = config.get("default", "cachedir")
414 else: default_cachedir = "~/.weather"
415 option_parser.add_option("--cachedir",
417 default=default_cachedir,
418 help="directory for storing cached searches and data")
421 if config.has_option("default", "delay"):
422 default_delay = config.getint("default", "delay")
423 else: default_delay = 1
424 option_parser.add_option("--delay",
426 default=default_delay,
427 help="hours to delay alert and forecast expiration")
429 # the -f/--forecast option
430 if config.has_option("default", "forecast"):
431 default_forecast = config.getboolean("default", "forecast")
432 else: default_forecast = False
433 option_parser.add_option("-f", "--forecast",
436 default=default_forecast,
437 help="include a local forecast")
439 # the --headers option
440 if config.has_option("default", "headers"):
441 default_headers = config.get("default", "headers")
445 + "relative_humidity," \
450 + "sky_conditions," \
451 + "precipitation_last_hour"
452 option_parser.add_option("--headers",
454 default=default_headers,
455 help="list of conditions headers to display")
457 # the --imperial option
458 if config.has_option("default", "imperial"):
459 default_imperial = config.getboolean("default", "imperial")
460 else: default_imperial = False
461 option_parser.add_option("--imperial",
464 default=default_imperial,
465 help="filter/convert conditions for US/UK units")
468 option_parser.add_option("--info",
472 help="output detailed information for your search")
474 # the -l/--list option
475 option_parser.add_option("-l", "--list",
479 help="list all configured aliases and cached searches")
481 # the --longlist option
482 option_parser.add_option("--longlist",
486 help="display details of all configured aliases")
488 # the -m/--metric option
489 if config.has_option("default", "metric"):
490 default_metric = config.getboolean("default", "metric")
491 else: default_metric = False
492 option_parser.add_option("-m", "--metric",
495 default=default_metric,
496 help="filter/convert conditions for metric units")
498 # the -n/--no-conditions option
499 if config.has_option("default", "conditions"):
500 default_conditions = config.getboolean("default", "conditions")
501 else: default_conditions = True
502 option_parser.add_option("-n", "--no-conditions",
504 action="store_false",
505 default=default_conditions,
506 help="disable output of current conditions")
508 # the --no-cache option
509 if config.has_option("default", "cache"):
510 default_cache = config.getboolean("default", "cache")
511 else: default_cache = True
512 option_parser.add_option("--no-cache",
514 action="store_false",
516 help="disable all caching (searches and data)")
518 # the --no-cache-data option
519 if config.has_option("default", "cache_data"):
520 default_cache_data = config.getboolean("default", "cache_data")
521 else: default_cache_data = True
522 option_parser.add_option("--no-cache-data",
524 action="store_false",
526 help="disable retrieved data caching")
528 # the --no-cache-search option
529 if config.has_option("default", "cache_search"):
530 default_cache_search = config.getboolean("default", "cache_search")
531 else: default_cache_search = True
532 option_parser.add_option("--no-cache-search",
534 action="store_false",
536 help="disable search result caching")
538 # the -q/--quiet option
539 if config.has_option("default", "quiet"):
540 default_quiet = config.getboolean("default", "quiet")
541 else: default_quiet = False
542 option_parser.add_option("-q", "--quiet",
545 default=default_quiet,
546 help="skip preambles and don't indent")
548 # the --setpath option
549 if config.has_option("default", "setpath"):
550 default_setpath = config.get("default", "setpath")
551 else: default_setpath = ".:~/.weather"
552 option_parser.add_option("--setpath",
554 default=default_setpath,
555 help="directory search path for correlation sets")
557 # the -v/--verbose option
558 if config.has_option("default", "verbose"):
559 default_verbose = config.getboolean("default", "verbose")
560 else: default_verbose = False
561 option_parser.add_option("-v", "--verbose",
564 default=default_verbose,
565 help="show full decoded feeds")
568 if config.has_option("default", "city"):
569 default_city = config.get("default", "city")
570 else: default_city = ""
571 option_parser.add_option("-c", "--city",
573 default=default_city,
574 help=optparse.SUPPRESS_HELP)
575 if config.has_option("default", "id"):
576 default_id = config.get("default", "id")
577 else: default_id = ""
578 option_parser.add_option("-i", "--id",
581 help=optparse.SUPPRESS_HELP)
582 if config.has_option("default", "st"):
583 default_st = config.get("default", "st")
584 else: default_st = ""
585 option_parser.add_option("-s", "--st",
588 help=optparse.SUPPRESS_HELP)
590 options, arguments = option_parser.parse_args()
591 return options, arguments
594 """Parse the aliases and configuration."""
595 import configparser, os
596 config = configparser.ConfigParser()
599 "/etc/weather/weatherrc",
600 os.path.expanduser("~/.weather/weatherrc"),
601 os.path.expanduser("~/.weatherrc"),
604 for rcfile in rcfiles:
605 if os.access(rcfile, os.R_OK):
606 config.read(rcfile, encoding="utf-8")
607 for section in config.sections():
608 if section != section.lower():
609 if config.has_section(section.lower()):
610 config.remove_section(section.lower())
611 config.add_section(section.lower())
612 for option,value in config.items(section):
613 config.set(section.lower(), option, value)
616 def integrate_search_cache(config, cachedir, setpath):
617 """Add cached search results into the configuration."""
618 import configparser, os, time
619 scache_fn = os.path.join( os.path.expanduser(cachedir), "searches" )
620 if not os.access(scache_fn, os.R_OK): return config
621 scache_fd = open(scache_fn)
622 created = float( scache_fd.readline().split(":")[1].strip().split()[0] )
625 datafiles = data_index(setpath)
627 data_freshness = sorted(
628 [ x[1] for x in datafiles.values() ],
631 else: data_freshness = now
632 if created < data_freshness <= now:
635 print( "[clearing outdated %s]" % scache_fn )
636 except (IOError, OSError):
639 scache = configparser.ConfigParser()
640 scache.read(scache_fn, encoding="utf-8")
641 for section in scache.sections():
642 if not config.has_section(section):
643 config.add_section(section)
644 for option,value in scache.items(section):
645 config.set(section, option, value)
648 def list_aliases(config, detail=False):
649 """Return a formatted list of aliases defined in the config."""
651 output = "\n# configured alias details..."
652 for section in sorted(config.sections()):
653 output += "\n\n[%s]" % section
654 for item in sorted(config.items(section)):
655 output += "\n%s = %s" % item
658 output = "configured aliases and cached searches..."
659 for section in sorted(config.sections()):
660 if config.has_option(section, "description"):
661 description = config.get(section, "description")
662 else: description = "(no description provided)"
663 output += "\n %s: %s" % (section, description)
666 def data_index(path):
669 for filename in ("airports", "places", "stations", "zctas", "zones"):
670 for dirname in path.split(":"):
671 for extension in ("", ".gz", ".txt"):
672 candidate = os.path.expanduser(
673 os.path.join( dirname, "".join( (filename, extension) ) )
675 if os.path.exists(candidate):
676 datafiles[filename] = (
678 os.stat(candidate).st_mtime
681 if filename in datafiles:
695 """Find URIs using airport, gecos, placename, station, ZCTA/ZIP, zone."""
696 import codecs, configparser, datetime, time, os, re, sys
697 datafiles = data_index(path)
698 if re.match("[A-Za-z]{3}$", expression): searchtype = "airport"
699 elif re.match("[A-Za-z0-9]{4}$", expression): searchtype = "station"
700 elif re.match("[A-Za-z]{2}[Zz][0-9]{3}$", expression): searchtype = "zone"
701 elif re.match("[0-9]{5}$", expression): searchtype = "ZCTA"
703 r"[\+-]?\d+(\.\d+)?(-\d+){,2}[ENSWensw]?, *[\+-]?\d+(\.\d+)?(-\d+){,2}[ENSWensw]?$",
706 searchtype = "coordinates"
707 elif re.match(r"(FIPS|fips)\d+$", expression): searchtype = "FIPS"
711 if cache_search: action = "caching"
712 else: action = "using"
721 (0.995, "excellent"),
724 if not quiet: print("Searching via %s..."%searchtype)
725 stations = configparser.ConfigParser()
726 dataname = "stations"
727 if dataname in datafiles:
728 datafile = datafiles[dataname][0]
729 if datafile.endswith(".gz"):
731 stations.read_string( gzip.open(datafile).read().decode("utf-8") )
733 stations.read(datafile, encoding="utf-8")
735 message = "%s error: can't find \"%s\" data file\n" % (
736 os.path.basename( sys.argv[0] ),
739 sys.stderr.write(message)
741 zones = configparser.ConfigParser()
743 if dataname in datafiles:
744 datafile = datafiles[dataname][0]
745 if datafile.endswith(".gz"):
747 zones.read_string( gzip.open(datafile).read().decode("utf-8") )
749 zones.read(datafile, encoding="utf-8")
751 message = "%s error: can't find \"%s\" data file\n" % (
752 os.path.basename( sys.argv[0] ),
755 sys.stderr.write(message)
763 if searchtype == "airport":
764 expression = expression.lower()
765 airports = configparser.ConfigParser()
766 dataname = "airports"
767 if dataname in datafiles:
768 datafile = datafiles[dataname][0]
769 if datafile.endswith(".gz"):
771 airports.read_string(
772 gzip.open(datafile).read().decode("utf-8") )
774 airports.read(datafile, encoding="utf-8")
776 message = "%s error: can't find \"%s\" data file\n" % (
777 os.path.basename( sys.argv[0] ),
780 sys.stderr.write(message)
782 if airports.has_section(expression) \
783 and airports.has_option(expression, "station"):
784 search = (expression, "IATA/FAA airport code %s" % expression)
785 station = ( airports.get(expression, "station"), 0 )
786 if stations.has_option(station[0], "zone"):
787 zone = eval( stations.get(station[0], "zone") )
789 if not ( info or quiet ) \
790 and stations.has_option( station[0], "description" ):
794 stations.get(station[0], "description")
798 message = "No IATA/FAA airport code \"%s\" in the %s file.\n" % (
800 datafiles["airports"][0]
802 sys.stderr.write(message)
804 elif searchtype == "station":
805 expression = expression.lower()
806 if stations.has_section(expression):
807 station = (expression, 0)
809 search = (expression, "ICAO station code %s" % expression)
810 if stations.has_option(expression, "zone"):
811 zone = eval( stations.get(expression, "zone") )
813 if not ( info or quiet ) \
814 and stations.has_option(expression, "description"):
818 stations.get(expression, "description")
822 message = "No ICAO weather station \"%s\" in the %s file.\n" % (
824 datafiles["stations"][0]
826 sys.stderr.write(message)
828 elif searchtype == "zone":
829 expression = expression.lower()
830 if zones.has_section(expression) \
831 and zones.has_option(expression, "station"):
832 zone = (expression, 0)
833 station = eval( zones.get(expression, "station") )
835 search = (expression, "NWS/NOAA weather zone %s" % expression)
836 if not ( info or quiet ) \
837 and zones.has_option(expression, "description"):
841 zones.get(expression, "description")
845 message = "No usable NWS weather zone \"%s\" in the %s file.\n" % (
847 datafiles["zones"][0]
849 sys.stderr.write(message)
851 elif searchtype == "ZCTA":
852 zctas = configparser.ConfigParser()
854 if dataname in datafiles:
855 datafile = datafiles[dataname][0]
856 if datafile.endswith(".gz"):
858 zctas.read_string( gzip.open(datafile).read().decode("utf-8") )
860 zctas.read(datafile, encoding="utf-8")
862 message = "%s error: can't find \"%s\" data file\n" % (
863 os.path.basename( sys.argv[0] ),
866 sys.stderr.write(message)
869 if zctas.has_section(expression) \
870 and zctas.has_option(expression, "station"):
871 station = eval( zctas.get(expression, "station") )
872 search = (expression, "Census ZCTA (ZIP code) %s" % expression)
873 if zctas.has_option(expression, "zone"):
874 zone = eval( zctas.get(expression, "zone") )
876 message = "No census ZCTA (ZIP code) \"%s\" in the %s file.\n" % (
878 datafiles["zctas"][0]
880 sys.stderr.write(message)
882 elif searchtype == "coordinates":
883 search = (expression, "Geographic coordinates %s" % expression)
885 for station in stations.sections():
886 if stations.has_option(station, "location"):
887 stationtable[station] = {
888 "location": eval( stations.get(station, "location") )
890 station = closest( gecos(expression), stationtable, "location", 0.1 )
892 message = "No ICAO weather station found near %s.\n" % expression
893 sys.stderr.write(message)
896 for zone in zones.sections():
897 if zones.has_option(zone, "centroid"):
899 "centroid": eval( zones.get(zone, "centroid") )
901 zone = closest( gecos(expression), zonetable, "centroid", 0.1 )
903 message = "No NWS weather zone near %s; forecasts unavailable.\n" \
905 sys.stderr.write(message)
906 elif searchtype in ("FIPS", "name"):
907 places = configparser.ConfigParser()
909 if dataname in datafiles:
910 datafile = datafiles[dataname][0]
911 if datafile.endswith(".gz"):
913 places.read_string( gzip.open(datafile).read().decode("utf-8") )
915 places.read(datafile, encoding="utf-8")
917 message = "%s error: can't find \"%s\" data file\n" % (
918 os.path.basename( sys.argv[0] ),
921 sys.stderr.write(message)
924 place = expression.lower()
925 if places.has_section(place) and places.has_option(place, "station"):
926 station = eval( places.get(place, "station") )
927 search = (expression, "Census Place %s" % expression)
928 if places.has_option(place, "description"):
931 search[1] + ", %s" % places.get(place, "description")
933 if places.has_option(place, "zone"):
934 zone = eval( places.get(place, "zone") )
935 if not ( info or quiet ) \
936 and places.has_option(place, "description"):
940 places.get(place, "description")
944 for place in places.sections():
945 if places.has_option(place, "description") \
946 and places.has_option(place, "station") \
949 places.get(place, "description"),
952 possibilities.append(place)
953 for place in stations.sections():
954 if stations.has_option(place, "description") \
957 stations.get(place, "description"),
960 possibilities.append(place)
961 for place in zones.sections():
962 if zones.has_option(place, "description") \
963 and zones.has_option(place, "station") \
966 zones.get(place, "description"),
969 possibilities.append(place)
970 if len(possibilities) == 1:
971 place = possibilities[0]
972 if places.has_section(place):
973 station = eval( places.get(place, "station") )
974 description = places.get(place, "description")
975 if places.has_option(place, "zone"):
976 zone = eval( places.get(place, "zone" ) )
977 search = ( expression, "%s: %s" % (place, description) )
978 elif stations.has_section(place):
979 station = (place, 0.0)
980 description = stations.get(place, "description")
981 if stations.has_option(place, "zone"):
982 zone = eval( stations.get(place, "zone" ) )
983 search = ( expression, "ICAO station code %s" % place )
984 elif zones.has_section(place):
985 station = eval( zones.get(place, "station") )
986 description = zones.get(place, "description")
988 search = ( expression, "NWS/NOAA weather zone %s" % place )
989 if not ( info or quiet ):
990 print( "[%s result %s]" % (action, description) )
991 if not possibilities and not station[0]:
992 message = "No FIPS code/census area match in the %s file.\n" % (
993 datafiles["places"][0]
995 sys.stderr.write(message)
998 uris["metar"] = stations.get( station[0], "metar" )
1000 for key,value in zones.items( zone[0] ):
1001 if key not in ("centroid", "description", "station"):
1004 count = len(possibilities)
1005 if count <= max_results:
1006 print( "Your search is ambiguous, returning %s matches:" % count )
1007 for place in sorted(possibilities):
1008 if places.has_section(place):
1012 places.get(place, "description")
1015 elif stations.has_section(place):
1019 stations.get(place, "description")
1022 elif zones.has_section(place):
1026 zones.get(place, "description")
1031 "Your search is too ambiguous, returning %s matches." % count
1038 for section in dataset.sections():
1039 if dataset.has_option(section, "station"):
1041 eval( dataset.get(section, "station") )[1]
1043 if dataset.has_option(section, "zone"):
1044 zonelist.append( eval( dataset.get(section, "zone") )[1] )
1047 scount = len(stationlist)
1048 zcount = len(zonelist)
1051 for score in scores:
1053 sranks.append( stationlist[ int( (1-score[0]) * scount ) ] )
1055 zranks.append( zonelist[ int( (1-score[0]) * zcount ) ] )
1056 description = search[1]
1057 uris["description"] = description
1059 "%s\n%s" % ( description, "-" * len(description) )
1064 stations.get( station[0], "description" )
1067 km = radian_to_km*station[1]
1068 mi = radian_to_mi*station[1]
1069 if sranks and not description.startswith("ICAO station code "):
1070 for index in range(0, len(scores)):
1071 if station[1] >= sranks[index]:
1072 score = scores[index][1]
1075 " (proximity %s, %.3gkm, %.3gmi)" % ( score, km, mi )
1077 elif searchtype == "coordinates":
1078 print( " (%.3gkm, %.3gmi)" % (km, mi) )
1081 "%s: %s" % ( zone[0], zones.get( zone[0], "description" ) )
1083 km = radian_to_km*zone[1]
1084 mi = radian_to_mi*zone[1]
1085 if zranks and not description.startswith("NWS/NOAA weather zone "):
1086 for index in range(0, len(scores)):
1087 if zone[1] >= zranks[index]:
1088 score = scores[index][1]
1091 " (proximity %s, %.3gkm, %.3gmi)" % ( score, km, mi )
1093 elif searchtype == "coordinates" and zone[0]:
1094 print( " (%.3gkm, %.3gmi)" % (km, mi) )
1097 nowstamp = "%s (%s)" % (
1099 datetime.datetime.isoformat(
1100 datetime.datetime.fromtimestamp(now),
1104 search_cache = ["\n"]
1105 search_cache.append( "[%s]\n" % search[0] )
1106 search_cache.append( "cached = %s\n" % nowstamp )
1107 for uriname in sorted(uris.keys()):
1108 search_cache.append( "%s = %s\n" % ( uriname, uris[uriname] ) )
1109 real_cachedir = os.path.expanduser(cachedir)
1110 if not os.path.exists(real_cachedir):
1111 try: os.makedirs(real_cachedir)
1112 except (IOError, OSError): pass
1113 scache_fn = os.path.join(real_cachedir, "searches")
1114 if not os.path.exists(scache_fn):
1116 [ x[1] for x in datafiles.values() ],
1119 thenstamp = "%s (%s)" % (
1121 datetime.datetime.isoformat(
1122 datetime.datetime.fromtimestamp(then),
1126 search_cache.insert(
1128 "# based on data files from: %s\n" % thenstamp
1131 scache_existing = configparser.ConfigParser()
1132 scache_existing.read(scache_fn, encoding="utf-8")
1133 if not scache_existing.has_section(search[0]):
1134 scache_fd = codecs.open(scache_fn, "a", "utf-8")
1135 scache_fd.writelines(search_cache)
1137 except (IOError, OSError): pass
1141 def closest(position, nodes, fieldname, angle=None):
1143 if not angle: angle = 2*math.pi
1146 if fieldname in nodes[name]:
1147 node = nodes[name][fieldname]
1148 if node and abs( position[0]-node[0] ) < angle:
1149 if abs( position[1]-node[1] ) < angle \
1150 or abs( abs( position[1]-node[1] ) - 2*math.pi ) < angle:
1151 if position == node:
1155 candidate = math.acos(
1156 math.sin( position[0] ) * math.sin( node[0] ) \
1157 + math.cos( position[0] ) \
1158 * math.cos( node[0] ) \
1159 * math.cos( position[1] - node[1] )
1161 if candidate < angle:
1164 if match: match = str(match)
1165 return (match, angle)
1167 def gecos(formatted):
1169 coordinates = formatted.split(",")
1170 for coordinate in range(0, 2):
1171 degrees, foo, minutes, bar, seconds, hemisphere = re.match(
1172 r"([\+-]?\d+\.?\d*)(-(\d+))?(-(\d+))?([ensw]?)$",
1173 coordinates[coordinate].strip().lower()
1175 value = float(degrees)
1176 if minutes: value += float(minutes)/60
1177 if seconds: value += float(seconds)/3600
1178 if hemisphere and hemisphere in "sw": value *= -1
1179 coordinates[coordinate] = math.radians(value)
1180 return tuple(coordinates)
1183 import codecs, configparser, csv, datetime, hashlib, os, re, sys, time
1184 import zipfile, zoneinfo
1185 for filename in os.listdir("."):
1186 if re.match("[0-9]{4}_Gaz_counties_national.zip$", filename):
1187 gcounties_an = filename
1188 gcounties_fn = filename[:-4] + ".txt"
1189 elif re.match("[0-9]{4}_Gaz_cousubs_national.zip$", filename):
1190 gcousubs_an = filename
1191 gcousubs_fn = filename[:-4] + ".txt"
1192 elif re.match("[0-9]{4}_Gaz_place_national.zip$", filename):
1193 gplace_an = filename
1194 gplace_fn = filename[:-4] + ".txt"
1195 elif re.match("[0-9]{4}_Gaz_zcta_national.zip$", filename):
1197 gzcta_fn = filename[:-4] + ".txt"
1198 elif re.match("bp[0-9]{2}[a-z]{2}[0-9]{2}.dbx$", filename):
1199 cpfzcf_fn = filename
1200 nsdcccc_fn = "nsd_cccc.txt"
1201 ourairports_fn = "airports.csv"
1202 overrides_fn = "overrides.conf"
1203 overrideslog_fn = "overrides.log"
1207 airports_fn = "airports"
1208 places_fn = "places"
1209 stations_fn = "stations"
1214 # generated by %s on %s from these public domain sources:
1216 # https://www.census.gov/geographies/reference-files/time-series/geo/gazetteer-files.html
1222 # https://www.weather.gov/gis/ZoneCounty/
1225 # https://tgftp.nws.noaa.gov/data/
1228 # https://ourairports.com/data/
1231 # ...and these manually-generated or hand-compiled adjustments:
1237 os.path.basename( sys.argv[0] ),
1238 datetime.date.isoformat(
1239 datetime.datetime.utcfromtimestamp( int(os.environ.get('SOURCE_DATE_EPOCH', time.time())) )
1241 hashlib.md5( open(gcounties_an, "rb").read() ).hexdigest(),
1242 datetime.date.isoformat(
1243 datetime.datetime.utcfromtimestamp( os.path.getmtime(gcounties_an) )
1246 hashlib.md5( open(gcousubs_an, "rb").read() ).hexdigest(),
1247 datetime.date.isoformat(
1248 datetime.datetime.utcfromtimestamp( os.path.getmtime(gcousubs_an) )
1251 hashlib.md5( open(gplace_an, "rb").read() ).hexdigest(),
1252 datetime.date.isoformat(
1253 datetime.datetime.utcfromtimestamp( os.path.getmtime(gplace_an) )
1256 hashlib.md5( open(gzcta_an, "rb").read() ).hexdigest(),
1257 datetime.date.isoformat(
1258 datetime.datetime.utcfromtimestamp( os.path.getmtime(gzcta_an) )
1261 hashlib.md5( open(cpfzcf_fn, "rb").read() ).hexdigest(),
1262 datetime.date.isoformat(
1263 datetime.datetime.utcfromtimestamp( os.path.getmtime(cpfzcf_fn) )
1266 hashlib.md5( open(nsdcccc_fn, "rb").read() ).hexdigest(),
1267 datetime.date.isoformat(
1268 datetime.datetime.utcfromtimestamp( os.path.getmtime(nsdcccc_fn) )
1271 hashlib.md5( open(ourairports_fn, "rb").read() ).hexdigest(),
1272 datetime.date.isoformat(
1273 datetime.datetime.utcfromtimestamp( os.path.getmtime(ourairports_fn) )
1276 hashlib.md5( open(overrides_fn, "rb").read() ).hexdigest(),
1277 datetime.date.isoformat(
1278 datetime.datetime.utcfromtimestamp( os.path.getmtime(overrides_fn) )
1281 hashlib.md5( open(slist_fn, "rb").read() ).hexdigest(),
1282 datetime.date.isoformat(
1283 datetime.datetime.utcfromtimestamp( os.path.getmtime(slist_fn) )
1286 hashlib.md5( open(zlist_fn, "rb").read() ).hexdigest(),
1287 datetime.date.isoformat(
1288 datetime.datetime.utcfromtimestamp( os.path.getmtime(zlist_fn) )
1297 message = "Reading %s:%s..." % (gcounties_an, gcounties_fn)
1298 sys.stdout.write(message)
1301 gcounties = zipfile.ZipFile(gcounties_an).open(gcounties_fn, "r")
1302 columns = gcounties.readline().decode("utf-8").strip().split("\t")
1303 for line in gcounties:
1304 fields = line.decode("utf-8").strip().split("\t")
1305 f_geoid = fields[ columns.index("GEOID") ].strip()
1306 f_name = fields[ columns.index("NAME") ].strip()
1307 f_usps = fields[ columns.index("USPS") ].strip()
1308 f_intptlat = fields[ columns.index("INTPTLAT") ].strip()
1309 f_intptlong = fields[ columns.index("INTPTLONG") ].strip()
1310 if f_geoid and f_name and f_usps and f_intptlat and f_intptlong:
1311 fips = "fips%s" % f_geoid
1312 if fips not in places: places[fips] = {}
1313 places[fips]["centroid"] = gecos(
1314 "%s,%s" % (f_intptlat, f_intptlong)
1316 places[fips]["description"] = "%s, %s" % (f_name, f_usps)
1319 print("done (%s lines)." % count)
1320 message = "Reading %s:%s..." % (gcousubs_an, gcousubs_fn)
1321 sys.stdout.write(message)
1324 gcousubs = zipfile.ZipFile(gcousubs_an).open(gcousubs_fn, "r")
1325 columns = gcousubs.readline().decode("utf-8").strip().split("\t")
1326 for line in gcousubs:
1327 fields = line.decode("utf-8").strip().split("\t")
1328 f_geoid = fields[ columns.index("GEOID") ].strip()
1329 f_name = fields[ columns.index("NAME") ].strip()
1330 f_usps = fields[ columns.index("USPS") ].strip()
1331 f_intptlat = fields[ columns.index("INTPTLAT") ].strip()
1332 f_intptlong = fields[ columns.index("INTPTLONG") ].strip()
1333 if f_geoid and f_name and f_usps and f_intptlat and f_intptlong:
1334 fips = "fips%s" % f_geoid
1335 if fips not in places: places[fips] = {}
1336 places[fips]["centroid"] = gecos(
1337 "%s,%s" % (f_intptlat, f_intptlong)
1339 places[fips]["description"] = "%s, %s" % (f_name, f_usps)
1342 print("done (%s lines)." % count)
1343 message = "Reading %s:%s..." % (gplace_an, gplace_fn)
1344 sys.stdout.write(message)
1347 gplace = zipfile.ZipFile(gplace_an).open(gplace_fn, "r")
1348 columns = gplace.readline().decode("utf-8").strip().split("\t")
1350 fields = line.decode("utf-8").strip().split("\t")
1351 f_geoid = fields[ columns.index("GEOID") ].strip()
1352 f_name = fields[ columns.index("NAME") ].strip()
1353 f_usps = fields[ columns.index("USPS") ].strip()
1354 f_intptlat = fields[ columns.index("INTPTLAT") ].strip()
1355 f_intptlong = fields[ columns.index("INTPTLONG") ].strip()
1356 if f_geoid and f_name and f_usps and f_intptlat and f_intptlong:
1357 fips = "fips%s" % f_geoid
1358 if fips not in places: places[fips] = {}
1359 places[fips]["centroid"] = gecos(
1360 "%s,%s" % (f_intptlat, f_intptlong)
1362 places[fips]["description"] = "%s, %s" % (f_name, f_usps)
1365 print("done (%s lines)." % count)
1366 message = "Reading %s..." % slist_fn
1367 sys.stdout.write(message)
1370 slist = codecs.open(slist_fn, "r", "utf-8")
1372 icao = line.split("#")[0].strip()
1375 "metar": "https://tgftp.nws.noaa.gov/data/observations/"\
1376 + "metar/decoded/%s.TXT" % icao.upper()
1380 print("done (%s lines)." % count)
1381 message = "Reading %s..." % nsdcccc_fn
1382 sys.stdout.write(message)
1385 nsdcccc = codecs.open(nsdcccc_fn, "r", "utf-8")
1386 for line in nsdcccc:
1388 fields = line.split(";")
1389 icao = fields[0].strip().lower()
1390 if icao in stations:
1392 name = " ".join( fields[3].strip().title().split() )
1393 if name: description.append(name)
1394 st = fields[4].strip()
1395 if st: description.append(st)
1396 country = " ".join( fields[5].strip().title().split() )
1397 if country: description.append(country)
1399 stations[icao]["description"] = ", ".join(description)
1400 lat, lon = fields[7:9]
1402 stations[icao]["location"] = gecos( "%s,%s" % (lat, lon) )
1403 elif "location" not in stations[icao]:
1404 lat, lon = fields[5:7]
1406 stations[icao]["location"] = gecos( "%s,%s" % (lat, lon) )
1409 print("done (%s lines)." % count)
1410 message = "Reading %s..." % ourairports_fn
1411 sys.stdout.write(message)
1414 ourairports = open(ourairports_fn, "r")
1415 for row in csv.reader(ourairports):
1416 icao = row[12].lower()
1417 if icao in stations:
1418 iata = row[13].lower()
1419 if len(iata) == 3: airports[iata] = { "station": icao }
1420 if "description" not in stations[icao]:
1423 if name: description.append(name)
1424 municipality = row[10]
1425 if municipality: description.append(municipality)
1430 c,r = region.split("-", 1)
1431 if c == country: region = r
1432 description.append(region)
1434 description.append(country)
1436 stations[icao]["description"] = ", ".join(description)
1437 if "location" not in stations[icao]:
1442 stations[icao]["location"] = gecos(
1443 "%s,%s" % (lat, lon)
1447 print("done (%s lines)." % count)
1448 message = "Reading %s..." % zlist_fn
1449 sys.stdout.write(message)
1452 zlist = codecs.open(zlist_fn, "r", "utf-8")
1454 line = line.split("#")[0].strip()
1459 print("done (%s lines)." % count)
1460 message = "Reading %s..." % cpfzcf_fn
1461 sys.stdout.write(message)
1465 cpfzcf = codecs.open(cpfzcf_fn, "r", "utf-8")
1467 fields = line.strip().split("|")
1468 if len(fields) == 11 \
1469 and fields[0] and fields[1] and fields[9] and fields[10]:
1470 zone = "z".join( fields[:2] ).lower()
1473 description = fields[3].strip()
1474 fips = "fips%s"%fields[6]
1475 countycode = "%sc%s" % (state.lower(), fips[-3:])
1477 zones[zone]["coastal_flood_statement"] = (
1478 "https://tgftp.nws.noaa.gov/data/watches_warnings/"
1479 "flood/coastal/%s/%s.txt" % (state.lower(), zone))
1480 zones[zone]["flash_flood_statement"] = (
1481 "https://tgftp.nws.noaa.gov/data/watches_warnings/"
1482 "flash_flood/statement/%s/%s.txt"
1483 % (state.lower(), countycode))
1484 zones[zone]["flash_flood_warning"] = (
1485 "https://tgftp.nws.noaa.gov/data/watches_warnings/"
1486 "flash_flood/warning/%s/%s.txt"
1487 % (state.lower(), countycode))
1488 zones[zone]["flash_flood_watch"] = (
1489 "https://tgftp.nws.noaa.gov/data/watches_warnings/"
1490 "flash_flood/watch/%s/%s.txt" % (state.lower(), zone))
1491 zones[zone]["flood_warning"] = (
1492 "https://tgftp.nws.noaa.gov/data/watches_warnings/"
1493 "flood/warning/%s/%s.txt"
1494 % (state.lower(), countycode))
1495 zones[zone]["severe_thunderstorm_warning"] = (
1496 "https://tgftp.nws.noaa.gov/data/watches_warnings/"
1497 "thunderstorm/%s/%s.txt" % (state.lower(), countycode))
1498 zones[zone]["severe_weather_statement"] = (
1499 "https://tgftp.nws.noaa.gov/data/watches_warnings/"
1500 "severe_weather_stmt/%s/%s.txt"
1501 % (state.lower(), countycode))
1502 zones[zone]["short_term_forecast"] = (
1503 "https://tgftp.nws.noaa.gov/data/forecasts/nowcast/"
1504 "%s/%s.txt" % (state.lower(), zone))
1505 zones[zone]["special_weather_statement"] = (
1506 "https://tgftp.nws.noaa.gov/data/watches_warnings/"
1507 "special_weather_stmt/%s/%s.txt"
1508 % (state.lower(), zone))
1509 zones[zone]["state_forecast"] = (
1510 "https://tgftp.nws.noaa.gov/data/forecasts/state/"
1511 "%s/%s.txt" % (state.lower(), zone))
1512 zones[zone]["tornado"] = (
1513 "https://tgftp.nws.noaa.gov/data/watches_warnings/"
1514 "tornado/%s/%s.txt" % (state.lower(), countycode))
1515 zones[zone]["urgent_weather_message"] = (
1516 "https://tgftp.nws.noaa.gov/data/watches_warnings/"
1517 "non_precip/%s/%s.txt" % (state.lower(), zone))
1518 zones[zone]["zone_forecast"] = (
1519 "https://tgftp.nws.noaa.gov/data/forecasts/zone/"
1520 "%s/%s.txt" % (state.lower(), zone))
1523 zones[zone]["tz"] = "US/Alaska"
1524 elif tzcode == "AH":
1525 zones[zone]["tz"] = "US/Aleutian"
1526 elif tzcode in ("C", "CE", "CM"):
1527 zones[zone]["tz"] = "US/Central"
1528 elif tzcode in ("E", "e"):
1529 zones[zone]["tz"] = "US/Eastern"
1531 zones[zone]["tz"] = "Pacific/Guadalcanal"
1533 zones[zone]["tz"] = "Pacific/Guam"
1535 zones[zone]["tz"] = "US/Hawaii"
1537 zones[zone]["tz"] = "Japan"
1539 zones[zone]["tz"] = "Pacific/Kwajalein"
1540 elif tzcode in ("M", "MC", "MP"):
1541 zones[zone]["tz"] = "US/Mountain"
1543 zones[zone]["tz"] = "US/Arizona"
1545 zones[zone]["tz"] = "US/Pacific"
1547 zones[zone]["tz"] = "US/Samoa"
1549 zones[zone]["tz"] = "America/Virgin"
1551 zones[zone]["tz"] = ""
1554 if description.endswith(county):
1555 description += " County"
1557 description += ", %s County" % county
1558 description += ", %s, US" % state
1559 zones[zone]["description"] = description
1560 zones[zone]["centroid"] = gecos( ",".join( fields[9:11] ) )
1561 if fips in places and not zones[zone]["centroid"]:
1562 zones[zone]["centroid"] = places[fips]["centroid"]
1565 print("done (%s lines)." % count)
1566 message = "Reading %s:%s..." % (gzcta_an, gzcta_fn)
1567 sys.stdout.write(message)
1570 gzcta = zipfile.ZipFile(gzcta_an).open(gzcta_fn, "r")
1571 columns = gzcta.readline().decode("utf-8").strip().split("\t")
1573 fields = line.decode("utf-8").strip().split("\t")
1574 f_geoid = fields[ columns.index("GEOID") ].strip()
1575 f_intptlat = fields[ columns.index("INTPTLAT") ].strip()
1576 f_intptlong = fields[ columns.index("INTPTLONG") ].strip()
1577 if f_geoid and f_intptlat and f_intptlong:
1578 if f_geoid not in zctas: zctas[f_geoid] = {}
1579 zctas[f_geoid]["centroid"] = gecos(
1580 "%s,%s" % (f_intptlat, f_intptlong)
1584 print("done (%s lines)." % count)
1585 message = "Reading %s..." % overrides_fn
1586 sys.stdout.write(message)
1592 overrides = configparser.ConfigParser()
1593 overrides.read_file( codecs.open(overrides_fn, "r", "utf8") )
1595 for section in overrides.sections():
1598 if section.startswith("-"):
1599 section = section[1:]
1601 else: delete = False
1602 if re.match("[A-Za-z]{3}$", section):
1604 if section in airports:
1605 del( airports[section] )
1606 logact = "removed airport %s" % section
1609 logact = "tried to remove nonexistent airport %s" % section
1611 if section in airports:
1612 logact = "changed airport %s" % section
1615 airports[section] = {}
1616 logact = "added airport %s" % section
1618 for key,value in overrides.items(section):
1619 if key in airports[section]: chgopt += 1
1621 if key in ("centroid", "location"):
1622 airports[section][key] = eval(value)
1624 airports[section][key] = value
1625 if addopt and chgopt:
1626 logact += " (+%s/!%s options)" % (addopt, chgopt)
1627 elif addopt: logact += " (+%s options)" % addopt
1628 elif chgopt: logact += " (!%s options)" % chgopt
1629 elif re.match("[A-Za-z0-9]{4}$", section):
1631 if section in stations:
1632 del( stations[section] )
1633 logact = "removed station %s" % section
1636 logact = "tried to remove nonexistent station %s" % section
1638 if section in stations:
1639 logact = "changed station %s" % section
1642 stations[section] = {}
1643 logact = "added station %s" % section
1645 for key,value in overrides.items(section):
1646 if key in stations[section]: chgopt += 1
1648 if key in ("centroid", "location"):
1649 stations[section][key] = eval(value)
1651 stations[section][key] = value
1652 if addopt and chgopt:
1653 logact += " (+%s/!%s options)" % (addopt, chgopt)
1654 elif addopt: logact += " (+%s options)" % addopt
1655 elif chgopt: logact += " (!%s options)" % chgopt
1656 elif re.match("[0-9]{5}$", section):
1658 if section in zctas:
1659 del( zctas[section] )
1660 logact = "removed zcta %s" % section
1663 logact = "tried to remove nonexistent zcta %s" % section
1665 if section in zctas:
1666 logact = "changed zcta %s" % section
1670 logact = "added zcta %s" % section
1672 for key,value in overrides.items(section):
1673 if key in zctas[section]: chgopt += 1
1675 if key in ("centroid", "location"):
1676 zctas[section][key] = eval(value)
1678 zctas[section][key] = value
1679 if addopt and chgopt:
1680 logact += " (+%s/!%s options)" % (addopt, chgopt)
1681 elif addopt: logact += " (+%s options)" % addopt
1682 elif chgopt: logact += " (!%s options)" % chgopt
1683 elif re.match("[A-Za-z]{2}[Zz][0-9]{3}$", section):
1685 if section in zones:
1686 del( zones[section] )
1687 logact = "removed zone %s" % section
1690 logact = "tried to remove nonexistent zone %s" % section
1692 if section in zones:
1693 logact = "changed zone %s" % section
1697 logact = "added zone %s" % section
1699 for key,value in overrides.items(section):
1700 if key in zones[section]: chgopt += 1
1702 if key in ("centroid", "location"):
1703 zones[section][key] = eval(value)
1705 zones[section][key] = value
1706 if addopt and chgopt:
1707 logact += " (+%s/!%s options)" % (addopt, chgopt)
1708 elif addopt: logact += " (+%s options)" % addopt
1709 elif chgopt: logact += " (!%s options)" % chgopt
1710 elif re.match("fips[0-9]+$", section):
1712 if section in places:
1713 del( places[section] )
1714 logact = "removed place %s" % section
1717 logact = "tried to remove nonexistent place %s" % section
1719 if section in places:
1720 logact = "changed place %s" % section
1723 places[section] = {}
1724 logact = "added place %s" % section
1726 for key,value in overrides.items(section):
1727 if key in places[section]: chgopt += 1
1729 if key in ("centroid", "location"):
1730 places[section][key] = eval(value)
1732 places[section][key] = value
1733 if addopt and chgopt:
1734 logact += " (+%s/!%s options)" % (addopt, chgopt)
1735 elif addopt: logact += " (+%s options)" % addopt
1736 elif chgopt: logact += " (!%s options)" % chgopt
1738 overrideslog.append("%s\n" % logact)
1740 if os.path.exists(overrideslog_fn):
1741 os.rename(overrideslog_fn, "%s_old"%overrideslog_fn)
1742 overrideslog_fd = codecs.open(overrideslog_fn, "w", "utf8")
1744 overrideslog_fd.write(
1745 '# Copyright (c) %s Jeremy Stanley <fungi@yuggoth.org>. Permission to\n'
1746 '# use, copy, modify, and distribute this software is granted under terms\n'
1747 '# provided in the LICENSE file distributed with this software.\n\n'
1748 % time.gmtime().tm_year)
1749 overrideslog_fd.writelines(overrideslog)
1750 overrideslog_fd.close()
1751 print("done (%s overridden sections: +%s/-%s/!%s)." % (
1757 estimate = 2*len(places) + len(stations) + 2*len(zctas) + len(zones)
1759 "Correlating places, stations, ZCTAs and zones (upper bound is %s):" % \
1763 milestones = list( range(51) )
1765 sys.stdout.write(message)
1768 centroid = places[fips]["centroid"]
1770 station = closest(centroid, stations, "location", 0.1)
1772 places[fips]["station"] = station
1775 level = int(50*count/estimate)
1776 if level in milestones:
1777 for remaining in milestones[:milestones.index(level)+1]:
1780 sys.stdout.write(message)
1783 message = "%s%%" % (remaining*2,)
1784 sys.stdout.write(message)
1786 milestones.remove(remaining)
1788 zone = closest(centroid, zones, "centroid", 0.1)
1790 places[fips]["zone"] = zone
1793 level = int(50*count/estimate)
1794 if level in milestones:
1795 for remaining in milestones[:milestones.index(level)+1]:
1798 sys.stdout.write(message)
1801 message = "%s%%" % (remaining*2,)
1802 sys.stdout.write(message)
1804 milestones.remove(remaining)
1805 for station in stations:
1806 if "location" in stations[station]:
1807 location = stations[station]["location"]
1809 zone = closest(location, zones, "centroid", 0.1)
1811 stations[station]["zone"] = zone
1814 level = int(50*count/estimate)
1815 if level in milestones:
1816 for remaining in milestones[:milestones.index(level)+1]:
1819 sys.stdout.write(message)
1822 message = "%s%%" % (remaining*2,)
1823 sys.stdout.write(message)
1825 milestones.remove(remaining)
1826 for zcta in zctas.keys():
1827 centroid = zctas[zcta]["centroid"]
1829 station = closest(centroid, stations, "location", 0.1)
1831 zctas[zcta]["station"] = station
1834 level = int(50*count/estimate)
1835 if level in milestones:
1836 for remaining in milestones[ : milestones.index(level)+1 ]:
1839 sys.stdout.write(message)
1842 message = "%s%%" % (remaining*2,)
1843 sys.stdout.write(message)
1845 milestones.remove(remaining)
1847 zone = closest(centroid, zones, "centroid", 0.1)
1849 zctas[zcta]["zone"] = zone
1852 level = int(50*count/estimate)
1853 if level in milestones:
1854 for remaining in milestones[:milestones.index(level)+1]:
1857 sys.stdout.write(message)
1860 message = "%s%%" % (remaining*2,)
1861 sys.stdout.write(message)
1863 milestones.remove(remaining)
1864 for zone in zones.keys():
1865 if "centroid" in zones[zone]:
1866 centroid = zones[zone]["centroid"]
1868 station = closest(centroid, stations, "location", 0.1)
1870 zones[zone]["station"] = station
1873 level = int(50*count/estimate)
1874 if level in milestones:
1875 for remaining in milestones[:milestones.index(level)+1]:
1878 sys.stdout.write(message)
1881 message = "%s%%" % (remaining*2,)
1882 sys.stdout.write(message)
1884 milestones.remove(remaining)
1885 for remaining in milestones:
1888 sys.stdout.write(message)
1891 message = "%s%%" % (remaining*2,)
1892 sys.stdout.write(message)
1894 print("\n done (%s correlations)." % count)
1895 message = "Writing %s..." % airports_fn
1896 sys.stdout.write(message)
1899 if os.path.exists(airports_fn):
1900 os.rename(airports_fn, "%s_old"%airports_fn)
1901 airports_fd = codecs.open(airports_fn, "w", "utf8")
1902 airports_fd.write(header)
1903 for airport in sorted( airports.keys() ):
1904 airports_fd.write("\n\n[%s]" % airport)
1905 for key, value in sorted( airports[airport].items() ):
1906 if type(value) is float: value = "%.7f"%value
1907 elif type(value) is tuple:
1909 for element in value:
1910 if type(element) is float: elements.append("%.7f"%element)
1911 else: elements.append( repr(element) )
1912 value = "(%s)"%", ".join(elements)
1913 airports_fd.write( "\n%s = %s" % (key, value) )
1915 airports_fd.write("\n")
1917 print("done (%s sections)." % count)
1918 message = "Writing %s..." % places_fn
1919 sys.stdout.write(message)
1922 if os.path.exists(places_fn):
1923 os.rename(places_fn, "%s_old"%places_fn)
1924 places_fd = codecs.open(places_fn, "w", "utf8")
1925 places_fd.write(header)
1926 for fips in sorted( places.keys() ):
1927 places_fd.write("\n\n[%s]" % fips)
1928 for key, value in sorted( places[fips].items() ):
1929 if type(value) is float: value = "%.7f"%value
1930 elif type(value) is tuple:
1932 for element in value:
1933 if type(element) is float: elements.append("%.7f"%element)
1934 else: elements.append( repr(element) )
1935 value = "(%s)"%", ".join(elements)
1936 places_fd.write( "\n%s = %s" % (key, value) )
1938 places_fd.write("\n")
1940 print("done (%s sections)." % count)
1941 message = "Writing %s..." % stations_fn
1942 sys.stdout.write(message)
1945 if os.path.exists(stations_fn):
1946 os.rename(stations_fn, "%s_old"%stations_fn)
1947 stations_fd = codecs.open(stations_fn, "w", "utf-8")
1948 stations_fd.write(header)
1949 for station in sorted( stations.keys() ):
1950 stations_fd.write("\n\n[%s]" % station)
1951 for key, value in sorted( stations[station].items() ):
1952 if type(value) is float: value = "%.7f"%value
1953 elif type(value) is tuple:
1955 for element in value:
1956 if type(element) is float: elements.append("%.7f"%element)
1957 else: elements.append( repr(element) )
1958 value = "(%s)"%", ".join(elements)
1959 if type(value) is bytes:
1960 value = value.decode("utf-8")
1961 stations_fd.write( "\n%s = %s" % (key, value) )
1963 stations_fd.write("\n")
1965 print("done (%s sections)." % count)
1966 message = "Writing %s..." % zctas_fn
1967 sys.stdout.write(message)
1970 if os.path.exists(zctas_fn):
1971 os.rename(zctas_fn, "%s_old"%zctas_fn)
1972 zctas_fd = codecs.open(zctas_fn, "w", "utf8")
1973 zctas_fd.write(header)
1974 for zcta in sorted( zctas.keys() ):
1975 zctas_fd.write("\n\n[%s]" % zcta)
1976 for key, value in sorted( zctas[zcta].items() ):
1977 if type(value) is float: value = "%.7f"%value
1978 elif type(value) is tuple:
1980 for element in value:
1981 if type(element) is float: elements.append("%.7f"%element)
1982 else: elements.append( repr(element) )
1983 value = "(%s)"%", ".join(elements)
1984 zctas_fd.write( "\n%s = %s" % (key, value) )
1986 zctas_fd.write("\n")
1988 print("done (%s sections)." % count)
1989 message = "Writing %s..." % zones_fn
1990 sys.stdout.write(message)
1993 if os.path.exists(zones_fn):
1994 os.rename(zones_fn, "%s_old"%zones_fn)
1995 zones_fd = codecs.open(zones_fn, "w", "utf8")
1996 zones_fd.write(header)
1997 for zone in sorted( zones.keys() ):
1998 zones_fd.write("\n\n[%s]" % zone)
1999 for key, value in sorted( zones[zone].items() ):
2000 if type(value) is float: value = "%.7f"%value
2001 elif type(value) is tuple:
2003 for element in value:
2004 if type(element) is float: elements.append("%.7f"%element)
2005 else: elements.append( repr(element) )
2006 value = "(%s)"%", ".join(elements)
2007 zones_fd.write( "\n%s = %s" % (key, value) )
2009 zones_fd.write("\n")
2011 print("done (%s sections)." % count)
2012 message = "Starting QA check..."
2013 sys.stdout.write(message)
2015 airports = configparser.ConfigParser()
2016 airports.read(airports_fn, encoding="utf-8")
2017 places = configparser.ConfigParser()
2018 places.read(places_fn, encoding="utf-8")
2019 stations = configparser.ConfigParser()
2020 stations.read(stations_fn, encoding="utf-8")
2021 zctas = configparser.ConfigParser()
2022 zctas.read(zctas_fn, encoding="utf-8")
2023 zones = configparser.ConfigParser()
2024 zones.read(zones_fn, encoding="utf-8")
2026 places_nocentroid = 0
2027 places_nodescription = 0
2028 for place in sorted( places.sections() ):
2029 if not places.has_option(place, "centroid"):
2030 qalog.append("%s: no centroid\n" % place)
2031 places_nocentroid += 1
2032 if not places.has_option(place, "description"):
2033 qalog.append("%s: no description\n" % place)
2034 places_nodescription += 1
2035 stations_nodescription = 0
2036 stations_nolocation = 0
2037 stations_nometar = 0
2038 for station in sorted( stations.sections() ):
2039 if not stations.has_option(station, "description"):
2040 qalog.append("%s: no description\n" % station)
2041 stations_nodescription += 1
2042 if not stations.has_option(station, "location"):
2043 qalog.append("%s: no location\n" % station)
2044 stations_nolocation += 1
2045 if not stations.has_option(station, "metar"):
2046 qalog.append("%s: no metar\n" % station)
2047 stations_nometar += 1
2048 airports_badstation = 0
2049 airports_nostation = 0
2050 for airport in sorted( airports.sections() ):
2051 if not airports.has_option(airport, "station"):
2052 qalog.append("%s: no station\n" % airport)
2053 airports_nostation += 1
2055 station = airports.get(airport, "station")
2056 if station not in stations.sections():
2057 qalog.append( "%s: bad station %s\n" % (airport, station) )
2058 airports_badstation += 1
2059 zctas_nocentroid = 0
2060 for zcta in sorted( zctas.sections() ):
2061 if not zctas.has_option(zcta, "centroid"):
2062 qalog.append("%s: no centroid\n" % zcta)
2063 zctas_nocentroid += 1
2064 zones_nocentroid = 0
2065 zones_nodescription = 0
2067 zones_noforecast = 0
2068 zones_overlapping = 0
2070 for zone in zones.sections():
2071 if zones.has_option(zone, "centroid"):
2073 "centroid": eval( zones.get(zone, "centroid") )
2075 for zone in sorted( zones.sections() ):
2076 if zones.has_option(zone, "centroid"):
2077 zonetable_local = zonetable.copy()
2078 del( zonetable_local[zone] )
2079 centroid = eval( zones.get(zone, "centroid") )
2081 nearest = closest(centroid, zonetable_local, "centroid", 0.1)
2082 if nearest[1]*radian_to_km < 1:
2083 qalog.append( "%s: within one km of %s\n" % (
2087 zones_overlapping += 1
2089 qalog.append("%s: no centroid\n" % zone)
2090 zones_nocentroid += 1
2091 if not zones.has_option(zone, "description"):
2092 qalog.append("%s: no description\n" % zone)
2093 zones_nodescription += 1
2094 if not zones.has_option(zone, "tz") or not zones.get(
2095 zone, "tz") in zoneinfo.available_timezones():
2096 qalog.append("%s: no time zone\n" % zone)
2098 if not zones.has_option(zone, "zone_forecast"):
2099 qalog.append("%s: no forecast\n" % zone)
2100 zones_noforecast += 1
2101 if os.path.exists(qalog_fn):
2102 os.rename(qalog_fn, "%s_old"%qalog_fn)
2103 qalog_fd = codecs.open(qalog_fn, "w", "utf8")
2106 '# Copyright (c) %s Jeremy Stanley <fungi@yuggoth.org>. Permission to\n'
2107 '# use, copy, modify, and distribute this software is granted under terms\n'
2108 '# provided in the LICENSE file distributed with this software.\n\n'
2109 % time.gmtime().tm_year)
2110 qalog_fd.writelines(qalog)
2113 print("issues found (see %s for details):"%qalog_fn)
2114 if airports_badstation:
2115 print(" %s airports with invalid station"%airports_badstation)
2116 if airports_nostation:
2117 print(" %s airports with no station"%airports_nostation)
2118 if places_nocentroid:
2119 print(" %s places with no centroid"%places_nocentroid)
2120 if places_nodescription:
2121 print(" %s places with no description"%places_nodescription)
2122 if stations_nodescription:
2123 print(" %s stations with no description"%stations_nodescription)
2124 if stations_nolocation:
2125 print(" %s stations with no location"%stations_nolocation)
2126 if stations_nometar:
2127 print(" %s stations with no METAR"%stations_nometar)
2128 if zctas_nocentroid:
2129 print(" %s ZCTAs with no centroid"%zctas_nocentroid)
2130 if zones_nocentroid:
2131 print(" %s zones with no centroid"%zones_nocentroid)
2132 if zones_nodescription:
2133 print(" %s zones with no description"%zones_nodescription)
2135 print(" %s zones with no time zone"%zones_notz)
2136 if zones_noforecast:
2137 print(" %s zones with no forecast"%zones_noforecast)
2138 if zones_overlapping:
2139 print(" %s zones within one km of another"%zones_overlapping)
2140 else: print("no issues found.")
2141 print("Indexing complete!")