@@ -22,34 +22,34 @@ class ERDDAP(object):
22
22
The user must pass the endpoint explicitly!!
23
23
24
24
Examples:
25
- >>> e = ERDDAP(server_url='https://data.ioos.us/gliders/erddap')
25
+ >>> e = ERDDAP(
26
+ ... server_url='https://data.ioos.us/gliders/erddap',
27
+ ... server_type='tabledap'
28
+ ... )
26
29
27
30
"""
28
- def __init__ (self , server_url ):
31
+ def __init__ (self , server_url , server_type = 'tabledap' ):
29
32
self .server_url = server_url
30
- # Caching the last resquest for multiple access,
31
- # will be overriden when a new variable is requested.
33
+ self .server_type = server_type
34
+ # Caching the last request for quicker multiple access,
35
+ # will be overridden when making a new requested.
32
36
self ._nc = None
33
37
self ._dataset_id = None
34
- self .search_url = None
35
- self ._search_options = {}
36
- self .info_url = None
37
- self ._info_options = {}
38
38
self ._variables = {}
39
39
40
- def search (self , items_per_page = 1000 , page = 1 , response = 'csv' , ** kwargs ):
41
- """Compose the search URL for `server_url` endpoint.
40
+ def search_url (self , items_per_page = 1000 , page = 1 , response = 'csv' , ** kwargs ):
41
+ """Compose the search URL for the `server_url` endpoint.
42
42
43
43
Args:
44
44
items_per_page (int): how many items per page in the return,
45
45
default is 1000.
46
46
page (int): which page to display, defatul is the first page (1).
47
- response (str): check https://data.ioos.us/gliders/erddap/tabledap/documentation.html#fileType
48
- for all the options, default is a Comma Separated Value ('csv').
47
+ response (str): default is a Comma Separated Value ('csv').
48
+ See ERDDAP docs for all the options,
49
+ tabledap: http://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html
50
+ griddap: http://coastwatch.pfeg.noaa.gov/erddap/griddap/documentation.html
49
51
Returns:
50
52
search_url (str): the search URL for the `response` chosen.
51
- The `search_url` is cached as an instance property,
52
- but will be overriden when performing a new search.
53
53
"""
54
54
base = (
55
55
'{server_url}/search/advanced.{response}'
@@ -94,22 +94,19 @@ def search(self, items_per_page=1000, page=1, response='csv', **kwargs):
94
94
'minTime' : kwargs .get ('min_time' , default ),
95
95
'maxTime' : kwargs .get ('max_time' , default ),
96
96
}
97
- if not self .search_url or (self ._search_options .items () != search_options ):
98
- self .search_url = base (** search_options )
99
- self ._search_options = search_options
100
- return self .search_url
97
+ return base (** search_options )
101
98
102
- def info (self , dataset_id , response = 'csv' ):
103
- """Compose the info URL for `server_url` endpoint.
99
+ def info_url (self , dataset_id , response = 'csv' ):
100
+ """Compose the info URL for the `server_url` endpoint.
104
101
105
102
Args:
106
- dataset_id (str): Use the data id found using the the search.
107
- response (str): check https://data.ioos.us/gliders/erddap/tabledap/documentation.html#fileType
108
- for all the options, default is a Comma Separated Value ('csv').
103
+ dataset_id (str): a dataset unique id.
104
+ response (str): default is a Comma Separated Value ('csv').
105
+ See ERDDAP docs for all the options,
106
+ tabledap: http://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html
107
+ griddap: http://coastwatch.pfeg.noaa.gov/erddap/griddap/documentation.html
109
108
Returns:
110
109
info_url (str): the info URL for the `response` chosen.
111
- The `info_url` is cached as an instance property,
112
- but will be overriden when performing a new info request.
113
110
"""
114
111
response = _clean_response (response )
115
112
base = '{server_url}/info/{dataset_id}/index.{response}' .format
@@ -118,15 +115,47 @@ def info(self, dataset_id, response='csv'):
118
115
'dataset_id' : dataset_id ,
119
116
'response' : response
120
117
}
121
- if not self .info_url or (self ._info_options .items () != info_options ):
122
- self .info_url = base (** info_options )
123
- self ._info_options = info_options
124
- return self .info_url
118
+ return base (** info_options )
125
119
126
- def opendap (self , dataset_id ):
127
- base = '{server_url}/tabledap/{dataset_id}' .format
128
- self .opendap_url = base (server_url = self .server_url , dataset_id = dataset_id )
129
- return self .opendap_url
120
+ def download_url (self , dataset_id , variables , response = 'csv' , ** kwargs ):
121
+ """Compose the download URL for the `server_url` endpoint.
122
+
123
+ Args:
124
+ dataset_id (str): a dataset unique id.
125
+ variables (list/tuple): a list of the variables to download.
126
+ response (str): default is a Comma Separated Value ('csv').
127
+ See ERDDAP docs for all the options,
128
+ tabledap: http://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html
129
+ griddap: http://coastwatch.pfeg.noaa.gov/erddap/griddap/documentation.html
130
+ Returns:
131
+ download_url (str): the download URL for the `response` chosen.
132
+ """
133
+ variables = ',' .join (variables )
134
+ base = (
135
+ '{server_url}/{server_type}/{dataset_id}.{response}'
136
+ '?{variables}'
137
+ '{kwargs}'
138
+ ).format
139
+
140
+ kwargs = '' .join (['&{}{}' .format (k , v ) for k , v in kwargs .items ()])
141
+ return base (server_url = self .server_url , server_type = self .server_type ,
142
+ dataset_id = dataset_id , response = response , variables = variables ,
143
+ kwargs = kwargs )
144
+
145
+ def opendap_url (self , dataset_id ):
146
+ """Compose the opendap URL for the `server_url` the endpoint.
147
+
148
+ Args:
149
+ dataset_id (str): a dataset unique id.
150
+ Returns:
151
+ download_url (str): the download URL for the `response` chosen.
152
+ """
153
+ base = '{server_url}/{server_type}/{dataset_id}' .format
154
+ return base (
155
+ server_url = self .server_url ,
156
+ server_type = self .server_type ,
157
+ dataset_id = dataset_id
158
+ )
130
159
131
160
def get_var_by_attr (self , dataset_id , ** kwargs ):
132
161
"""Similar to netCDF4-python `get_variables_by_attributes` for a ERDDAP
@@ -151,20 +180,21 @@ def get_var_by_attr(self, dataset_id, **kwargs):
151
180
>>> # Get variables that have a "grid_mapping" attribute
152
181
>>> vs = nc.get_variables_by_attributes(grid_mapping=lambda v: v is not None)
153
182
"""
154
- info_url = self .info (dataset_id , response = 'csv' )
155
- df = pd .read_csv (info_url )
183
+ info_url = self .info_url (dataset_id , response = 'csv' )
156
184
157
185
# Creates the variables dictionary for the `get_var_by_attr` lookup.
158
186
if not self ._variables or self ._dataset_id != dataset_id :
187
+ _df = pd .read_csv (info_url )
188
+ self ._dataset_id = dataset_id
159
189
variables = {}
160
- # Virtually the same code as the netCDF4 counterpart.
161
- for variable in set (df ['Variable Name' ]):
162
- attributes = df .loc [
163
- df ['Variable Name' ] == variable ,
190
+ for variable in set (_df ['Variable Name' ]):
191
+ attributes = _df .loc [
192
+ _df ['Variable Name' ] == variable ,
164
193
['Attribute Name' , 'Value' ]
165
194
].set_index ('Attribute Name' ).to_dict ()['Value' ]
166
195
variables .update ({variable : attributes })
167
196
self ._variables = variables
197
+ # Virtually the same code as the netCDF4 counterpart.
168
198
vs = []
169
199
has_value_flag = False
170
200
for vname in self ._variables :
0 commit comments