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

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

Changed the utils.wget() function -- switched to quicker implementation (urllib2), corrected exception handling, updated testUtils.py.

Line 
1import urllib2
2
3def wget(url):
4    '''
5    Read data from an URL.
6    @param url: The URL.
7    @return:    The returned data as a string, or None on retrieval error.
8    '''
9    content = None
10    try:
11        try:
12            f = urllib2.urlopen(url)         # open a file-like object with the data
13            content = f.read()               # read the text in
14        except urllib2.HTTPError:
15            pass                             # error in retrieval, wget() will return None
16    finally:
17        try:
18            f.close()
19        except NameError:
20            pass                             # f uninitialised, so no need to close it
21    return content
22
23def toDegreesMinutesSeconds(val):
24    '''
25    Determine how many integer degrees, minutes, seconds, and float parts of seconds there are
26    in an angle value given in float degrees.
27    @param val: An angle in float degrees.
28    @return:    A tuple containing (int degrees, int minutes, int seconds, float remainder in seconds).
29                The float remainder is a value between 0. (inclusive) and 1. (exclusive).
30    '''
31    if val >= 0: 
32        sign = 1
33    else: 
34        sign = -1
35    val = abs(val)
36    degrees = int(val)
37    decim = val % 1.
38    minute = 1/60.
39    second = 1/3600.
40    minutes = int(decim / minute)
41    remainder = decim % minute
42    seconds = int(remainder / second)
43    remainder = remainder % second
44    remainderInSeconds = remainder / second
45    return (sign * degrees, minutes, seconds, remainderInSeconds)
46
47def loadConfigElement(configFilename, basePath):
48    tree = ElementTree()
49    root = tree.parse(configFilename)
50    return root.find(basePath)
Note: See TracBrowser for help on using the repository browser.