Update sys.path example in INSTALL.
[weather.git] / weather.py
index 0e0d436..47870ec 100644 (file)
@@ -79,7 +79,8 @@ class Selections:
                     cache_search=(
                         self.get("cache") and self.get("cache_search")
                     ),
-                    cachedir=self.get("cachedir")
+                    cachedir=self.get("cachedir"),
+                    quiet=self.get_bool("quiet")
                 )
                 self.config.add_section(argument)
                 for item in guessed.items():
@@ -608,6 +609,7 @@ def get_config():
     import os
     rcfiles = [
         "/etc/weatherrc",
+        "/etc/weather/weatherrc",
         os.path.expanduser("~/.weather/weatherrc"),
         os.path.expanduser("~/.weatherrc"),
         "weatherrc"
@@ -699,7 +701,8 @@ def guess(
     info=False,
     cache_search=False,
     cacheage=900,
-    cachedir="."
+    cachedir=".",
+    quiet=False
 ):
     """Find URIs using airport, gecos, placename, station, ZCTA/ZIP, zone."""
     import codecs, datetime, time, os, re, sys
@@ -732,7 +735,7 @@ def guess(
             (0.995, "excellent"),
             (1.000, "ideal"),
         ]
-    print("Searching via %s..."%searchtype)
+    if not quiet: print("Searching via %s..."%searchtype)
     stations = configparser.ConfigParser()
     dataname = "stations"
     if dataname in datafiles:
@@ -796,7 +799,8 @@ def guess(
             if stations.has_option(station[0], "zone"):
                 zone = eval( stations.get(station[0], "zone") )
                 dataset = stations
-            if not info and stations.has_option( station[0], "description" ):
+            if not ( info or quiet ) \
+                and stations.has_option( station[0], "description" ):
                 print(
                     "[%s result %s]" % (
                         action,
@@ -819,7 +823,8 @@ def guess(
             if stations.has_option(expression, "zone"):
                 zone = eval( stations.get(expression, "zone") )
                 dataset = stations
-            if not info and stations.has_option(expression, "description"):
+            if not ( info or quiet ) \
+                and stations.has_option(expression, "description"):
                 print(
                     "[%s result %s]" % (
                         action,
@@ -841,7 +846,8 @@ def guess(
             station = eval( zones.get(expression, "station") )
             dataset = zones
             search = (expression, "NWS/NOAA weather zone %s" % expression)
-            if not info and zones.has_option(expression, "description"):
+            if not ( info or quiet ) \
+                and zones.has_option(expression, "description"):
                 print(
                     "[%s result %s]" % (
                         action,
@@ -939,7 +945,8 @@ def guess(
                 )
             if places.has_option(place, "zone"):
                 zone = eval( places.get(place, "zone") )
-            if not info and places.has_option(place, "description"):
+            if not ( info or quiet ) \
+                and places.has_option(place, "description"):
                 print(
                     "[%s result %s]" % (
                         action,
@@ -992,7 +999,8 @@ def guess(
                     description = zones.get(place, "description")
                     zone = (place, 0.0)
                     search = ( expression, "NWS/NOAA weather zone %s" % place )
-                if not info: print( "[%s result %s]" % (action, description) )
+                if not ( info or quiet ):
+                    print( "[%s result %s]" % (action, description) )
             if not possibilities and not station[0]:
                 message = "No FIPS code/census area match in the %s file.\n" % (
                     datafiles["places"][0]
@@ -1320,15 +1328,21 @@ def correlate():
     sys.stdout.flush()
     count = 0
     gcounties = zipfile.ZipFile(gcounties_an).open(gcounties_fn, "rU")
+    columns = gcounties.readline().decode("latin1").strip().split("\t")
     for line in gcounties:
         fields = line.decode("latin1").strip().split("\t")
-        if len(fields) == 10 and fields[0] != "STUSPS":
-            fips = "fips%s" % fields[1]
-            description = "%s, %s" % ( fields[3], fields[0] )
-            centroid = gecos( ",".join( fields[8:10] ) )
+        f_geoid = fields[ columns.index("GEOID") ].strip()
+        f_name = fields[ columns.index("NAME") ].strip()
+        f_usps = fields[ columns.index("USPS") ].strip()
+        f_intptlat = fields[ columns.index("INTPTLAT") ].strip()
+        f_intptlong = fields[ columns.index("INTPTLONG") ].strip()
+        if f_geoid and f_name and f_usps and f_intptlat and f_intptlong:
+            fips = "fips%s" % f_geoid
             if fips not in places: places[fips] = {}
-            places[fips]["centroid"] = centroid
-            places[fips]["description"] = description
+            places[fips]["centroid"] = gecos(
+                "%s,%s" % (f_intptlat, f_intptlong)
+            )
+            places[fips]["description"] = "%s, %s" % (f_name, f_usps)
             count += 1
     gcounties.close()
     print("done (%s lines)." % count)
@@ -1337,15 +1351,21 @@ def correlate():
     sys.stdout.flush()
     count = 0
     gcousubs = zipfile.ZipFile(gcousubs_an).open(gcousubs_fn, "rU")
+    columns = gcousubs.readline().decode("latin1").strip().split("\t")
     for line in gcousubs:
         fields = line.decode("latin1").strip().split("\t")
-        if len(fields) == 10 and fields[0] != "STUSPS":
-            fips = "fips%s" % fields[1]
-            description = "%s, %s" % ( fields[3], fields[0] )
-            centroid = gecos( ",".join( fields[8:10] ) )
+        f_geoid = fields[ columns.index("GEOID") ].strip()
+        f_name = fields[ columns.index("NAME") ].strip()
+        f_usps = fields[ columns.index("USPS") ].strip()
+        f_intptlat = fields[ columns.index("INTPTLAT") ].strip()
+        f_intptlong = fields[ columns.index("INTPTLONG") ].strip()
+        if f_geoid and f_name and f_usps and f_intptlat and f_intptlong:
+            fips = "fips%s" % f_geoid
             if fips not in places: places[fips] = {}
-            places[fips]["centroid"] = centroid
-            places[fips]["description"] = description
+            places[fips]["centroid"] = gecos(
+                "%s,%s" % (f_intptlat, f_intptlong)
+            )
+            places[fips]["description"] = "%s, %s" % (f_name, f_usps)
             count += 1
     gcousubs.close()
     print("done (%s lines)." % count)
@@ -1354,15 +1374,21 @@ def correlate():
     sys.stdout.flush()
     count = 0
     gplaces = zipfile.ZipFile(gplaces_an).open(gplaces_fn, "rU")
+    columns = gplaces.readline().decode("latin1").strip().split("\t")
     for line in gplaces:
         fields = line.decode("latin1").strip().split("\t")
-        if len(fields) == 10 and fields[0] != "STUSPS":
-            fips = "fips%s" % fields[1]
-            description = "%s, %s" % ( fields[3], fields[0] )
-            centroid = gecos( ",".join( fields[8:10] ) )
+        f_geoid = fields[ columns.index("GEOID") ].strip()
+        f_name = fields[ columns.index("NAME") ].strip()
+        f_usps = fields[ columns.index("USPS") ].strip()
+        f_intptlat = fields[ columns.index("INTPTLAT") ].strip()
+        f_intptlong = fields[ columns.index("INTPTLONG") ].strip()
+        if f_geoid and f_name and f_usps and f_intptlat and f_intptlong:
+            fips = "fips%s" % f_geoid
             if fips not in places: places[fips] = {}
-            places[fips]["centroid"] = centroid
-            places[fips]["description"] = description
+            places[fips]["centroid"] = gecos(
+                "%s,%s" % (f_intptlat, f_intptlong)
+            )
+            places[fips]["description"] = "%s, %s" % (f_name, f_usps)
             count += 1
     gplaces.close()
     print("done (%s lines)." % count)
@@ -1554,13 +1580,16 @@ def correlate():
     sys.stdout.flush()
     count = 0
     gzcta = zipfile.ZipFile(gzcta_an).open(gzcta_fn, "rU")
+    columns = gzcta.readline().decode("latin1").strip().split("\t")
     for line in gzcta:
         fields = line.decode("latin1").strip().split("\t")
-        if len(fields) == 7 and fields[0] != "GEOID":
-            zcta = fields[0]
-            if zcta not in zctas: zctas[zcta] = {}
-            zctas[zcta]["centroid"] = gecos(
-                ",".join( ( fields[6], fields[5] ) )
+        f_geoid = fields[ columns.index("GEOID") ].strip()
+        f_intptlat = fields[ columns.index("INTPTLAT") ].strip()
+        f_intptlong = fields[ columns.index("INTPTLONG") ].strip()
+        if f_geoid and f_intptlat and f_intptlong:
+            if f_geoid not in zctas: zctas[f_geoid] = {}
+            zctas[f_geoid]["centroid"] = gecos(
+                "%s,%s" % (f_intptlat, f_intptlong)
             )
             count += 1
     gzcta.close()
@@ -1882,6 +1911,7 @@ def correlate():
         for key, value in sorted( airports[airport].items() ):
             airports_fd.write( "\n%s = %s" % (key, value) )
         count += 1
+    airports_fd.write("\n")
     airports_fd.close()
     print("done (%s sections)." % count)
     message = "Writing %s..." % places_fn
@@ -1897,6 +1927,7 @@ def correlate():
         for key, value in sorted( places[fips].items() ):
             places_fd.write( "\n%s = %s" % (key, value) )
         count += 1
+    places_fd.write("\n")
     places_fd.close()
     print("done (%s sections)." % count)
     message = "Writing %s..." % stations_fn
@@ -1912,6 +1943,7 @@ def correlate():
         for key, value in sorted( stations[station].items() ):
             stations_fd.write( "\n%s = %s" % (key, value) )
         count += 1
+    stations_fd.write("\n")
     stations_fd.close()
     print("done (%s sections)." % count)
     message = "Writing %s..." % zctas_fn
@@ -1927,6 +1959,7 @@ def correlate():
         for key, value in sorted( zctas[zcta].items() ):
             zctas_fd.write( "\n%s = %s" % (key, value) )
         count += 1
+    zctas_fd.write("\n")
     zctas_fd.close()
     print("done (%s sections)." % count)
     message = "Writing %s..." % zones_fn
@@ -1942,6 +1975,7 @@ def correlate():
         for key, value in sorted( zones[zone].items() ):
             zones_fd.write( "\n%s = %s" % (key, value) )
         count += 1
+    zones_fd.write("\n")
     zones_fd.close()
     print("done (%s sections)." % count)
     message = "Starting QA check..."