Correlation building fixes
[weather.git] / weather.py
index 60067f2..2bacc27 100644 (file)
@@ -1,7 +1,7 @@
 """Contains various object definitions needed by the weather utility."""
 
 weather_copyright = """\
-# Copyright (c) 2006-2012 Jeremy Stanley <fungi@yuggoth.org>. Permission to
+# Copyright (c) 2006-2014 Jeremy Stanley <fungi@yuggoth.org>. Permission to
 # use, copy, modify, and distribute this software is granted under terms
 # provided in the LICENSE file distributed with this software.
 #"""
@@ -1226,7 +1226,7 @@ def correlate():
 %s
 # generated by %s on %s from these public domain sources:
 #
-# http://www.census.gov/geo/www/gazetteer/gazetteer2010.html
+# http://www.census.gov/geo/maps-data/data/gazetteer2010.html
 # %s %s %s
 # %s %s %s
 # %s %s %s
@@ -1328,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)
@@ -1345,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)
@@ -1362,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)
@@ -1527,12 +1545,12 @@ def correlate():
     cpfz = {}
     cpfzcf = open(cpfzcf_fn)
     for line in cpfzcf:
-        fields = line.split("|")
+        fields = line.strip().split("|")
         if len(fields) == 11 \
             and fields[0] and fields[1] and fields[9] and fields[10]:
             zone = "z".join( fields[:2] ).lower()
             if zone in zones:
-                zones[zone]["centroid"] = gecos( ",".join( fields[9:] ) )
+                zones[zone]["centroid"] = gecos( ",".join( fields[9:11] ) )
             elif fields[6]:
                 state = fields[0]
                 description = fields[3]
@@ -1562,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()
@@ -1888,8 +1909,16 @@ def correlate():
     for airport in sorted( airports.keys() ):
         airports_fd.write("\n\n[%s]" % airport)
         for key, value in sorted( airports[airport].items() ):
+            if type(value) is float: value = "%.7f"%value
+            elif type(value) is tuple:
+                elements = []
+                for element in value:
+                    if type(element) is float: elements.append("%.7f"%element)
+                    else: elements.append( repr(element) )
+                value = "(%s)"%", ".join(elements)
             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
@@ -1903,8 +1932,16 @@ def correlate():
     for fips in sorted( places.keys() ):
         places_fd.write("\n\n[%s]" % fips)
         for key, value in sorted( places[fips].items() ):
+            if type(value) is float: value = "%.7f"%value
+            elif type(value) is tuple:
+                elements = []
+                for element in value:
+                    if type(element) is float: elements.append("%.7f"%element)
+                    else: elements.append( repr(element) )
+                value = "(%s)"%", ".join(elements)
             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
@@ -1918,8 +1955,16 @@ def correlate():
     for station in sorted( stations.keys() ):
         stations_fd.write("\n\n[%s]" % station)
         for key, value in sorted( stations[station].items() ):
+            if type(value) is float: value = "%.7f"%value
+            elif type(value) is tuple:
+                elements = []
+                for element in value:
+                    if type(element) is float: elements.append("%.7f"%element)
+                    else: elements.append( repr(element) )
+                value = "(%s)"%", ".join(elements)
             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
@@ -1933,8 +1978,16 @@ def correlate():
     for zcta in sorted( zctas.keys() ):
         zctas_fd.write("\n\n[%s]" % zcta)
         for key, value in sorted( zctas[zcta].items() ):
+            if type(value) is float: value = "%.7f"%value
+            elif type(value) is tuple:
+                elements = []
+                for element in value:
+                    if type(element) is float: elements.append("%.7f"%element)
+                    else: elements.append( repr(element) )
+                value = "(%s)"%", ".join(elements)
             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
@@ -1948,8 +2001,16 @@ def correlate():
     for zone in sorted( zones.keys() ):
         zones_fd.write("\n\n[%s]" % zone)
         for key, value in sorted( zones[zone].items() ):
+            if type(value) is float: value = "%.7f"%value
+            elif type(value) is tuple:
+                elements = []
+                for element in value:
+                    if type(element) is float: elements.append("%.7f"%element)
+                    else: elements.append( repr(element) )
+                value = "(%s)"%", ".join(elements)
             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..."