source: DPPP/kml/csml2kml/python/csml2kml/csml2kml/utils.py @ 3693

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DPPP/kml/csml2kml/python/csml2kml/csml2kml/utils.py@3693
Revision 3693, 1.5 KB checked in by mkochan, 12 years ago (diff)

Extended csmlGrapher to allow multiple datasets (that is, data served from multiple WFSs).

Line 
1'''
2Utility/convenience functions.
3'''
4
5import re
6import urllib2
7from matplotlib import dates
8
9class UrlRetrievalError(Exception):
10    def __init__(self, value):
11        self.value = value
12    def __repr__(self):
13        return repr(self.value)
14
15def wget(url):
16    '''
17    Read data from an URL.
18    @param url: The URL.
19    @type url: C{str}
20    @return: The returned data as a string, or an C{urllib2.HTTPError} on retrieval error.
21    @rtype: C{str}
22    '''
23    content = None
24    try:
25        f = urllib2.urlopen(url)         # open a file-like object with the data
26        content = f.read()               # read the text in
27    finally:
28        try:
29            f.close()
30        except NameError:
31            pass                         # in case socket has not even been opened
32    return content
33
34def parseTimestamp(timestamp):
35    '''
36    Wraps the function dates.dateutil.parser.parse, while interpreting timestamps
37    like "31-12-2006T24:00:00" correctly as "1-1-2007T00:00:00"
38    (as matplotviz.dates.dateutil.parser.parse cannot parse timestams with hour other than 0..23).
39    @param timestamp: The timestamp
40    @type timestamp: C{str}
41    @return: Corresponding datetime
42    @rtype: C{dates.datetime.datetime}
43    '''
44    mo = re.match('(.+)24\:00\:00(Z?)', timestamp)
45    if mo:
46        oneDayBackTimestamp = mo.group(1) + '00:00:00' + mo.group(2)
47        oneDay = dates.datetime.timedelta(1)
48        return dates.dateutil.parser.parse(oneDayBackTimestamp) + oneDay
49    else:
50        return dates.dateutil.parser.parse(timestamp)
Note: See TracBrowser for help on using the repository browser.