Fix Py3K compatibility for compressed correlation
[weather.git] / weather.py
index dbd7321..af43de4 100644 (file)
@@ -1,12 +1,12 @@
 """Contains various object definitions needed by the weather utility."""
 
 weather_copyright = """\
-# Copyright (c) 2006-2014 Jeremy Stanley <fungi@yuggoth.org>. Permission to
+# Copyright (c) 2006-2016 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.
 #"""
 
-weather_version = "2.0"
+weather_version = "2.3"
 
 radian_to_km = 6372.795484
 radian_to_mi = 3959.871528
@@ -692,6 +692,8 @@ def data_index(path):
                         os.stat(candidate).st_mtime
                     )
                     break
+            if filename in datafiles:
+                break
     return datafiles
 
 def guess(
@@ -742,7 +744,10 @@ def guess(
         datafile = datafiles[dataname][0]
         if datafile.endswith(".gz"):
             import gzip
-            stations.readfp( gzip.open(datafile) )
+            if pyversion("3"):
+                stations.read_string(
+                    gzip.open(datafile).read().decode("utf-8") )
+            else: stations.readfp( gzip.open(datafile) )
         else:
             stations.read(datafile)
     else:
@@ -758,7 +763,9 @@ def guess(
         datafile = datafiles[dataname][0]
         if datafile.endswith(".gz"):
             import gzip
-            zones.readfp( gzip.open(datafile) )
+            if pyversion("3"):
+                zones.read_string( gzip.open(datafile).read().decode("utf-8") )
+            else: zones.readfp( gzip.open(datafile) )
         else:
             zones.read(datafile)
     else:
@@ -782,7 +789,10 @@ def guess(
             datafile = datafiles[dataname][0]
             if datafile.endswith(".gz"):
                 import gzip
-                airports.readfp( gzip.open(datafile) )
+                if pyversion("3"):
+                    airports.read_string(
+                        gzip.open(datafile).read().decode("utf-8") )
+                else: airports.readfp( gzip.open(datafile) )
             else:
                 airports.read(datafile)
         else:
@@ -868,7 +878,10 @@ def guess(
             datafile = datafiles[dataname][0]
             if datafile.endswith(".gz"):
                 import gzip
-                zctas.readfp( gzip.open(datafile) )
+                if pyversion("3"):
+                    zctas.read_string(
+                        gzip.open(datafile).read().decode("utf-8") )
+                else: zctas.readfp( gzip.open(datafile) )
             else:
                 zctas.read(datafile)
         else:
@@ -923,7 +936,10 @@ def guess(
             datafile = datafiles[dataname][0]
             if datafile.endswith(".gz"):
                 import gzip
-                places.readfp( gzip.open(datafile) )
+                if pyversion("3"):
+                    places.read_string(
+                        gzip.open(datafile).read().decode("utf-8") )
+                else: places.readfp( gzip.open(datafile) )
             else:
                 places.read(datafile)
         else:
@@ -1196,14 +1212,14 @@ def correlate():
     import codecs, datetime, hashlib, os, re, sys, tarfile, time, zipfile
     if pyversion("3"): import configparser
     else: import ConfigParser as configparser
-    gcounties_an = "2014_Gaz_counties_national.zip"
-    gcounties_fn = "2014_Gaz_counties_national.txt"
-    gcousubs_an = "2014_Gaz_cousubs_national.zip"
-    gcousubs_fn = "2014_Gaz_cousubs_national.txt"
-    gplace_an = "2014_Gaz_place_national.zip"
-    gplace_fn = "2014_Gaz_place_national.txt"
-    gzcta_an = "2014_Gaz_zcta_national.zip"
-    gzcta_fn = "2014_Gaz_zcta_national.txt"
+    gcounties_an = "2015_Gaz_counties_national.zip"
+    gcounties_fn = "2015_Gaz_counties_national.txt"
+    gcousubs_an = "2015_Gaz_cousubs_national.zip"
+    gcousubs_fn = "2015_Gaz_cousubs_national.txt"
+    gplace_an = "2015_Gaz_place_national.zip"
+    gplace_fn = "2015_Gaz_place_national.txt"
+    gzcta_an = "2015_Gaz_zcta_national.zip"
+    gzcta_fn = "2015_Gaz_zcta_national.txt"
     for filename in os.listdir("."):
         if re.match("bp[0-9][0-9][a-z][a-z][0-9][0-9].dbx$", filename):
             cpfzcf_fn = filename
@@ -1226,7 +1242,7 @@ def correlate():
 %s
 # generated by %s on %s from these public domain sources:
 #
-# http://www.census.gov/geo/maps-data/data/gazetteer2014.html
+# http://www.census.gov/geo/maps-data/data/gazetteer2015.html
 # %s %s %s
 # %s %s %s
 # %s %s %s
@@ -1235,10 +1251,10 @@ def correlate():
 # http://www.weather.gov/geodata/catalog/wsom/html/cntyzone.htm
 # %s %s %s
 #
-# http://weather.noaa.gov/data/nsd_cccc.txt
+# http://tgftp.nws.noaa.gov/data/nsd_cccc.txt
 # %s %s %s
 #
-# http://weather.noaa.gov/pub/data/zonecatalog.curr.tar
+# http://tgftp.nws.noaa.gov/data/zonecatalog.curr.tar
 # %s %s %s
 #
 # http://www.nco.ncep.noaa.gov/pmb/codes/nwprod/dictionaries/metar.tbl
@@ -1401,7 +1417,7 @@ def correlate():
         icao = line.split("#")[0].strip()
         if icao:
             stations[icao] = {
-                "metar": "http://weather.noaa.gov/pub/data/observations/"\
+                "metar": "http://tgftp.nws.noaa.gov/data/observations/"\
                     + "metar/decoded/%s.TXT" % icao.upper()
             }
             count += 1
@@ -1529,12 +1545,13 @@ def correlate():
                     zones[zone]["description"] = description
                     for line in data[1:]:
                         line = line.decode("latin1").strip()
-                        urimatch = re.match("/webdocs/(.+):(.+) for ", line)
+                        urimatch = re.match("/webdocs/pub/(.+):(.+) for ",
+                                            line)
                         if urimatch:
                             uritype = urimatch.group(2).lower().replace(" ","_")
-                            zones[zone][uritype] \
-                                = "http://weather.noaa.gov/%s" \
-                                % urimatch.group(1)
+                            zones[zone][uritype]  = (
+                                "http://tgftp.nws.noaa.gov/%s"
+                                % urimatch.group(1))
         count += 1
     zcatalog.close()
     print("done (%s files)." % count)