comparison python/utils.py @ 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 0954aaf28231
children 784298512b60
comparison
equal deleted inserted replaced
636:3058e00887bc 637:c9a0b72979fd
20 for i,x in enumerate(l): 20 for i,x in enumerate(l):
21 result[x] = i 21 result[x] = i
22 return result 22 return result
23 23
24 ######################### 24 #########################
25 # simple statistics 25 # Simple statistics
26 ######################### 26 #########################
27 27
28 def sampleSize(stdev, tolerance, percentConfidence, printLatex = False): 28 def sampleSize(stdev, tolerance, percentConfidence, printLatex = False):
29 from scipy.stats.distributions import norm 29 from scipy.stats.distributions import norm
30 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200. 30 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200.
480 pcolor(X, Y, ma.masked_where(C==defaultValue,C)) 480 pcolor(X, Y, ma.masked_where(C==defaultValue,C))
481 else: 481 else:
482 pcolor(X, Y, C) 482 pcolor(X, Y, C)
483 483
484 ######################### 484 #########################
485 # file I/O section 485 # Data download
486 #########################
487
488 def downloadECWeather(stationID, years, months = [], outputDirectoryname = '.', english = True):
489 '''Downloads monthly weather data from Environment Canada
490 If month is provided (number 1 to 12), it means hourly data for the whole month
491 Otherwise, means the data for each day, for the whole year
492
493 Example: MONTREAL MCTAVISH 10761
494 MONTREALPIERRE ELLIOTT TRUDEAU INTL A 5415
495
496 To get daily data for 2010 and 2011, downloadECWeather(10761, [2010,2011], [], '/tmp')
497 To get hourly data for 2009 and 2012, January, March and October, downloadECWeather(10761, [2009,2012], [1,3,10], '/tmp')'''
498 import urllib2
499 if english:
500 language = 'e'
501 else:
502 language = 'f'
503 if len(months) == 0:
504 timeFrame = 2
505 months = [1]
506 else:
507 timeFrame = 1
508
509 for year in years:
510 for month in months:
511 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))
512 data = url.read()
513 outFilename = '{}/{}-{}'.format(outputDirectoryname, stationID, year)
514 if timeFrame == 1:
515 outFilename += '-{}-hourly'.format(month)
516 else:
517 outFilename += '-daily'
518 outFilename += '.csv'
519 out = open(outFilename, 'w')
520 out.write(data)
521 out.close()
522
523 #########################
524 # File I/O
486 ######################### 525 #########################
487 526
488 def removeExtension(filename, delimiter = '.'): 527 def removeExtension(filename, delimiter = '.'):
489 '''Returns the filename minus the extension (all characters after last .)''' 528 '''Returns the filename minus the extension (all characters after last .)'''
490 i = filename.rfind(delimiter) 529 i = filename.rfind(delimiter)