Skip to content

Instantly share code, notes, and snippets.

@just-digital
Created April 1, 2012 23:50
Show Gist options
  • Save just-digital/2279541 to your computer and use it in GitHub Desktop.
Save just-digital/2279541 to your computer and use it in GitHub Desktop.
BOM (Bureau of Meteorology) in Australia have data feeds available for weather. This is a quick convenient python class for fetching that data.
import urllib2
import datetime
"""
Module for implementing BOM services. The BOMForcast class will fetch 7 days
of weather temperature and precis data. It also implements a handy iterator for
looping through forecast days.
See http://www.bom.gov.au/catalogue/data-feeds.shtml for more information on
data feeds available.
"""
SERVICE_URL = "ftp://ftp2.bom.gov.au/anon/gen/fwo/IDA00002.dat" # 7 day Precis
SITE_NO = "066062"
SITE_NAME = "Sydney"
class BOMForcast:
"""
Retrieve and store 7 days of weather forecast from Bureau of Meteorology (Australian Government).
"""
data = []
service = ""
prefix = "%s#%s#" % (SITE_NO, SITE_NAME)
error = None
def __init__(self):
self.__fetch()
self.itercurrent = 0
def __fetch(self):
try:
response = urllib2.urlopen(SERVICE_URL)
raw = response.read()
except:
return #broken
for line in raw.split("\n"):
# Find the record we want "Eg. Sydney"
if line[0:len(self.prefix)] == self.prefix:
cols = line.split("#")
fdate = datetime.datetime.strptime(cols[3], "%Y%m%d")
idate = datetime.datetime.strptime(cols[4]+cols[5], "%Y%m%d%H%M%S")
for i in range(7):
# build it data set
self.data.append({
"forecast_date": datetime.date(fdate.year, fdate.month, fdate.day + i),
"issue_date":idate,
"temperature_min":cols[6 + i],
"temperature_max":cols[7 + i],
"precis":cols[22 + i],
})
return
def __iter__(self):
return self
def next(self):
if not self.data or self.itercurrent > (len(self.data)-1):
raise StopIteration
self.itercurrent += 1
return self.data[(self.itercurrent -1)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment