Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib2
- import sys
- if len(sys.argv) < 5:
- print "Usage: weatherhistory.py [Station Code] [Start Year] [End Year] [Month]\nie. weatherhistory.py KSMF 1980 2000 12"
- sys.exit()
- airportC = sys.argv[1]
- startY = sys.argv[2]
- endY = sys.argv[3]
- m = sys.argv[4]
- # Create/open a file called wunder.txt (which will be a comma-delimited file)
- f = open('wunder-data.txt', 'w')
- # Iterate through year, month, and day
- for y in range(int(startY), int(endY)):
- month_avg = 0
- ValidYear = True
- c = 0
- temp_avg = 0
- # Open wunderground.com url
- url = "http://www.wunderground.com/history/airport/"+airportC+"/"+str(y)+ "/" + str(m) + "/1/MonthlyHistory.html?format=1"
- page = urllib2.urlopen(url)
- for line in urllib2.urlopen(url):
- data = line.strip().split(",")
- if data[0].startswith("19") or data[0].startswith("20"):
- date = data[0]
- mean = data[2]
- # print data
- if data[2] != '':
- temp_avg += int(mean)
- if int(data[2]) < -140:
- ValidYear = False
- else:
- ValidYear = False
- c+=1
- month_avg += temp_avg / c
- #print str(y)+"-"+str(m)+" average mean: "+str(temp_avg / c)
- if ValidYear:
- print str(y)+" average mean: "+str(month_avg)
- f.write(str(y)+','+str(month_avg)+"\n")
- else:
- print "Year "+str(y)+" contained bad data and will be discarded"
- # Done getting data! Close file.
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement