changeset 637:c9a0b72979fd

added function to get canadian public weather data
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 02 Apr 2015 15:53:59 +0200
parents 3058e00887bc
children 852f5de42d01
files python/utils.py
diffstat 1 files changed, 41 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/python/utils.py	Tue Mar 24 18:11:28 2015 +0100
+++ b/python/utils.py	Thu Apr 02 15:53:59 2015 +0200
@@ -22,7 +22,7 @@
     return result
 
 #########################
-# simple statistics
+# Simple statistics
 #########################
 
 def sampleSize(stdev, tolerance, percentConfidence, printLatex = False):
@@ -482,7 +482,46 @@
         pcolor(X, Y, C)
 
 #########################
-# file I/O section
+# Data download
+#########################
+
+def downloadECWeather(stationID, years, months = [], outputDirectoryname = '.', english = True):
+    '''Downloads monthly weather data from Environment Canada
+    If month is provided (number 1 to 12), it means hourly data for the whole month
+    Otherwise, means the data for each day, for the whole year
+
+    Example: MONTREAL MCTAVISH	10761
+             MONTREALPIERRE ELLIOTT TRUDEAU INTL A	5415
+
+    To get daily data for 2010 and 2011, downloadECWeather(10761, [2010,2011], [], '/tmp')
+    To get hourly data for 2009 and 2012, January, March and October, downloadECWeather(10761, [2009,2012], [1,3,10], '/tmp')'''
+    import urllib2
+    if english:
+        language = 'e'
+    else:
+        language = 'f'
+    if len(months) == 0:
+        timeFrame = 2
+        months = [1]
+    else:
+        timeFrame = 1
+
+    for year in years:
+        for month in months:
+            url = urllib2.urlopen('http://climat.meteo.gc.ca/climateData/bulkdata_{}.html?format=csv&stationID={}&Year={}&Month={}&Day=1&timeframe={}&submit=++T%C3%A9l%C3%A9charger+%0D%0Ades+donn%C3%A9es'.format(language, stationID, year, month, timeFrame))
+            data = url.read()
+            outFilename = '{}/{}-{}'.format(outputDirectoryname, stationID, year)
+            if timeFrame == 1:
+                outFilename += '-{}-hourly'.format(month)
+            else:
+                outFilename += '-daily'
+            outFilename += '.csv'
+            out = open(outFilename, 'w')
+            out.write(data)
+            out.close()
+
+#########################
+# File I/O
 #########################
 
 def removeExtension(filename, delimiter = '.'):