Advertisement
Guest User

weatherhistory.py

a guest
Jan 2nd, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. import urllib2
  2. import sys
  3.  
  4.  
  5. if len(sys.argv) < 5:
  6.     print "Usage: weatherhistory.py [Station Code] [Start Year] [End Year] [Month]\nie. weatherhistory.py KSMF 1980 2000 12"
  7.     sys.exit()
  8.  
  9. airportC = sys.argv[1]
  10. startY = sys.argv[2]
  11. endY = sys.argv[3]
  12. m = sys.argv[4]
  13. # Create/open a file called wunder.txt (which will be a comma-delimited file)
  14. f = open('wunder-data.txt', 'w')
  15.  
  16. # Iterate through year, month, and day
  17. for y in range(int(startY), int(endY)):
  18.   month_avg = 0
  19.   ValidYear = True
  20.   c = 0
  21.   temp_avg = 0
  22.   # Open wunderground.com url
  23.   url = "http://www.wunderground.com/history/airport/"+airportC+"/"+str(y)+ "/" + str(m) + "/1/MonthlyHistory.html?format=1"
  24.   page = urllib2.urlopen(url)
  25.   for line in urllib2.urlopen(url):
  26.     data = line.strip().split(",")
  27.     if data[0].startswith("19") or data[0].startswith("20"):
  28.       date = data[0]
  29.       mean = data[2]
  30.       # print data
  31.       if data[2] != '':
  32.         temp_avg += int(mean)
  33.         if int(data[2]) < -140:
  34.           ValidYear = False
  35.       else:
  36.         ValidYear = False
  37.       c+=1
  38.   month_avg += temp_avg / c
  39.   #print str(y)+"-"+str(m)+" average mean: "+str(temp_avg / c)
  40.   if ValidYear:
  41.     print str(y)+" average mean: "+str(month_avg)
  42.     f.write(str(y)+','+str(month_avg)+"\n")
  43.   else:
  44.     print "Year "+str(y)+" contained bad data and will be discarded"
  45.   # Done getting data! Close file.
  46. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement