1 __author__ = "Roberto De Almeida <rob@pydap.org>"
2
3 import dap.lib
4 from dap.util.http import openurl
5 from dap.exceptions import ClientError
6
7
8 -def open(url, cache=None, username=None, password=None, verbose=False):
9 """Connect to a remote dataset.
10
11 This function opens a dataset stored in a DAP server:
12
13 >>> dataset = open(url, cache=None, username=None, password=None, verbose=False):
14
15 You can specify a cache location (a directory), so that repeated
16 accesses to the same URL avoid the network.
17
18 The username and password may be necessary if the DAP server requires
19 authentication. The 'verbose' option will make pydap print all the
20 URLs that are acessed.
21 """
22
23 dap.lib.VERBOSE = verbose
24
25 if url.startswith('http'):
26 for response in [_ddx, _ddsdas]:
27 dataset = response(url, cache, username, password)
28 if dataset: return dataset
29 else:
30 raise ClientError("Unable to open dataset.")
31 else:
32 from dap.plugins.lib import loadhandler
33
34 handler = loadhandler(url)
35 dataset = handler._parseconstraints()
36
37 return dataset
38
39
40 -def _ddsdas(baseurl, cache, username, password):
41 ddsurl, dasurl = '%s.dds' % baseurl, '%s.das' % baseurl
42
43
44 respdds, dds = openurl(ddsurl)
45 respdas, das = openurl(dasurl)
46
47 if respdds['status'] == '200' and respdas['status'] == '200':
48 from dap.parsers.dds import DDSParser
49 from dap.parsers.das import DASParser
50
51
52 dataset = DDSParser(dds, ddsurl, cache, username, password).parse()
53
54
55 dataset = DASParser(das, dasurl, dataset).parse()
56 return dataset
57
58
59 -def _ddx(baseurl, cache, username, password):
60 pass
61