Skip to content

Commit 6cf2bc0

Browse files
committed
start revising form parsing to handle new surveys form
1 parent 0c4da2b commit 6cf2bc0

File tree

1 file changed

+62
-31
lines changed

1 file changed

+62
-31
lines changed

astroquery/eso/core.py

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,31 @@ def _activate_form(self, response, form_index=0, inputs={}, cache=True,
9999
elif tag_name == 'select':
100100
if form_elem.get('multiple') is not None:
101101
value = []
102-
for option in form_elem.select('option[value]'):
103-
if option.get('selected') is not None:
104-
value.append(option.get('value'))
102+
if form_elem.select('option[value]'):
103+
for option in form_elem.select('option[value]'):
104+
if option.get('selected') is not None:
105+
value.append(option.get('value'))
106+
else:
107+
for option in form_elem.select('option'):
108+
if option.get('selected') is not None:
109+
value.append(option.string)
105110
else:
106-
for option in form_elem.select('option[value]'):
107-
if option.get('selected') is not None:
108-
value = option.get('value')
109-
# select the first option field if none is selected
110-
if value is None:
111-
value = form_elem.select(
112-
'option[value]')[0].get('value')
111+
if form_elem.select('option[value]'):
112+
for option in form_elem.select('option[value]'):
113+
if option.get('selected') is not None:
114+
value = option.get('value')
115+
# select the first option field if none is selected
116+
if value is None:
117+
value = form_elem.select(
118+
'option[value]')[0].get('value')
119+
else:
120+
# survey form just uses text, not value
121+
for option in form_elem.select('option'):
122+
if option.get('selected') is not None:
123+
value = option.string
124+
# select the first option field if none is selected
125+
if value is None:
126+
value = form_elem.select('option')[0].string
113127

114128
if key in inputs:
115129
value = str(inputs[key])
@@ -231,7 +245,7 @@ def list_instruments(self, cache=True):
231245
return self._instrument_list
232246

233247
def query_surveys(self, surveys='any_collection_id', cache=True,
234-
help=False, **kwargs):
248+
help=False, open_form=False, **kwargs):
235249
"""
236250
Query survey Phase 3 data contained in the ESO archive.
237251
@@ -255,27 +269,44 @@ def query_surveys(self, surveys='any_collection_id', cache=True,
255269
"""
256270

257271
url = "http://archive.eso.org/wdb/wdb/adp/phase3_main/form"
258-
survey_form = self._request("GET", url, cache=cache)
259-
query_dict = kwargs
260-
query_dict["wdbo"] = "csv/download"
261-
query_dict['collection_name'] = surveys
262-
if self.ROW_LIMIT >= 0:
263-
query_dict["max_rows_returned"] = self.ROW_LIMIT
264-
else:
265-
query_dict["max_rows_returned"] = 10000
266-
survey_response = self._activate_form(survey_form, form_index=0,
267-
inputs=query_dict, cache=cache)
268-
269-
content = survey_response.content
270-
# First line is always garbage
271-
content = content.split(b'\n', 1)[1]
272-
log.debug("Response content:\n{0}".format(content))
273-
if _check_response(content):
274-
table = Table.read(BytesIO(content), format="ascii.csv",
275-
comment="^#")
276-
return table
272+
if open_form:
273+
webbrowser.open(url)
274+
elif help:
275+
self._print_surveys_help(url, cache=cache)
277276
else:
278-
warnings.warn("Query returned no results", NoResultsWarning)
277+
survey_form = self._request("GET", url, cache=cache)
278+
query_dict = kwargs
279+
query_dict["wdbo"] = "csv/download"
280+
query_dict['collection_name'] = surveys
281+
if self.ROW_LIMIT >= 0:
282+
query_dict["max_rows_returned"] = self.ROW_LIMIT
283+
else:
284+
query_dict["max_rows_returned"] = 10000
285+
286+
# missing entries:
287+
# filter: Any
288+
# tel_id: Any
289+
# ins_id: Any
290+
# obstech: Any
291+
# date_obs:
292+
# mjd_obs:
293+
# exptime:
294+
#
295+
# collection_name should be blank (?)
296+
297+
survey_response = self._activate_form(survey_form, form_index=0,
298+
inputs=query_dict, cache=cache)
299+
300+
content = survey_response.content
301+
# First line is always garbage
302+
content = content.split(b'\n', 1)[1]
303+
log.debug("Response content:\n{0}".format(content))
304+
if _check_response(content):
305+
table = Table.read(BytesIO(content), format="ascii.csv",
306+
comment="^#")
307+
return table
308+
else:
309+
warnings.warn("Query returned no results", NoResultsWarning)
279310

280311
def query_instrument(self, instrument, column_filters={}, columns=[],
281312
open_form=False, help=False, cache=True, **kwargs):

0 commit comments

Comments
 (0)