Skip to content

Commit 0c4da2b

Browse files
committed
add a new help parser for the new surveys form
1 parent 3ccf01b commit 0c4da2b

File tree

2 files changed

+97
-42
lines changed

2 files changed

+97
-42
lines changed

astroquery/eso/core.py

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -230,30 +230,8 @@ def list_instruments(self, cache=True):
230230
self._instrument_list.append(u'harps')
231231
return self._instrument_list
232232

233-
def list_surveys(self, cache=True):
234-
""" List all the available surveys (phase 3) in the ESO archive.
235-
236-
Returns
237-
-------
238-
survey_list : list of strings
239-
cache : bool
240-
Cache the response for faster subsequent retrieval
241-
242-
"""
243-
if self._survey_list is None:
244-
survey_list_response = self._request(
245-
"GET", "http://archive.eso.org/wdb/wdb/adp/phase3_main/form",
246-
cache=cache)
247-
root = BeautifulSoup(survey_list_response .content, 'html5lib')
248-
self._survey_list = []
249-
for select in root.select("select[name=phase3_program]"):
250-
for element in select.select('option'):
251-
survey = element.text.strip()
252-
if survey not in self._survey_list and 'Any' not in survey:
253-
self._survey_list.append(survey)
254-
return self._survey_list
255-
256-
def query_survey(self, survey, cache=True, **kwargs):
233+
def query_surveys(self, surveys='any_collection_id', cache=True,
234+
help=False, **kwargs):
257235
"""
258236
Query survey Phase 3 data contained in the ESO archive.
259237
@@ -276,13 +254,11 @@ def query_survey(self, survey, cache=True, **kwargs):
276254
277255
"""
278256

279-
if survey not in self.list_surveys():
280-
raise ValueError("Survey %s is not in the survey list." % survey)
281257
url = "http://archive.eso.org/wdb/wdb/adp/phase3_main/form"
282258
survey_form = self._request("GET", url, cache=cache)
283259
query_dict = kwargs
284260
query_dict["wdbo"] = "csv/download"
285-
query_dict['phase3_program'] = survey
261+
query_dict['collection_name'] = surveys
286262
if self.ROW_LIMIT >= 0:
287263
query_dict["max_rows_returned"] = self.ROW_LIMIT
288264
else:
@@ -345,7 +321,7 @@ def query_instrument(self, instrument, column_filters={}, columns=[],
345321
if open_form:
346322
webbrowser.open(url)
347323
elif help:
348-
self._print_help(url, instrument)
324+
self._print_instrument_help(url, instrument)
349325
else:
350326
instrument_form = self._request("GET", url, cache=cache)
351327
query_dict = {}
@@ -618,7 +594,7 @@ def query_apex_quicklooks(self, project_id=None, help=False,
618594
if open_form:
619595
webbrowser.open(apex_query_url)
620596
elif help:
621-
return self._print_help(apex_query_url, 'apex')
597+
return self._print_instrument_help(apex_query_url, 'apex')
622598
else:
623599

624600
payload = {'wdbo': 'csv/download'}
@@ -643,7 +619,7 @@ def query_apex_quicklooks(self, project_id=None, help=False,
643619

644620
return table
645621

646-
def _print_help(self, url, instrument, cache=True):
622+
def _print_instrument_help(self, url, instrument, cache=True):
647623
"""
648624
Download a form and print it in a quasi-human-readable way
649625
"""
@@ -700,4 +676,81 @@ def _print_help(self, url, instrument, cache=True):
700676
print("\n".join(result_string))
701677
return result_string
702678

679+
def _print_surveys_help(self, url, cache=True):
680+
"""
681+
Download a form and print it in a quasi-human-readable way
682+
"""
683+
log.info("List of the parameters accepted by the "
684+
"surveys query.")
685+
log.info("The presence of a column in the result table can be "
686+
"controlled if prefixed with a [ ] checkbox.")
687+
log.info("The default columns in the result table are shown as "
688+
"already ticked: [x].")
689+
690+
result_string = []
691+
692+
resp = self._request("GET", url, cache=cache)
693+
doc = BeautifulSoup(resp.content, 'html5lib')
694+
form = doc.select("html body form")[0]
695+
696+
# hovertext from different labels are used to give more info on forms
697+
helptext_dict = {abbr['title'].split(":")[0].strip():
698+
":".join(abbr['title'].split(":")[1:])
699+
for abbr in form.findAll('abbr')
700+
if 'title' in abbr.attrs and ":" in abbr['title']}
701+
702+
for fieldset in form.select('fieldset'):
703+
legend = fieldset.select('legend')
704+
if len(legend) > 1:
705+
raise ValueError("Form parsing error: too many legends.")
706+
elif len(legend) == 0:
707+
continue
708+
section_title = "\n\n"+"".join(legend[0].stripped_strings)+"\n"
709+
710+
result_string.append(section_title)
711+
712+
for section in fieldset.select('table'):
713+
714+
checkbox_name = ""
715+
checkbox_value = ""
716+
for tag in section.next_elements:
717+
if tag.name == u"table":
718+
break
719+
elif tag.name == u"input":
720+
if tag.get(u'type') == u"checkbox":
721+
checkbox_name = tag['name']
722+
checkbox_value = (u"[x]"
723+
if ('checked' in tag.attrs)
724+
else u"[ ]")
725+
name = ""
726+
value = ""
727+
else:
728+
name = tag['name']
729+
value = ""
730+
elif tag.name == u"select":
731+
options = []
732+
for option in tag.select("option"):
733+
options += ["{0} ({1})"
734+
.format(option['value']
735+
if 'value' in option
736+
else "",
737+
"".join(option.stripped_strings))]
738+
name = tag[u"name"]
739+
value = ", ".join(options)
740+
else:
741+
name = ""
742+
value = ""
743+
if u"tab_" + name == checkbox_name:
744+
checkbox = checkbox_value
745+
else:
746+
checkbox = " "
747+
if name != u"":
748+
result_string.append("{0} {1}: {2}"
749+
.format(checkbox, name, value))
750+
if name.strip() in helptext_dict:
751+
result_string.append(helptext_dict[name.strip()])
752+
753+
print("\n".join(result_string))
754+
return result_string
755+
703756
Eso = EsoClass()

astroquery/eso/tests/test_eso_remote.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@ def test_SgrAstar(self, temp_dir):
3030
eso = Eso()
3131
eso.cache_location = temp_dir
3232

33-
instruments = eso.list_instruments()
33+
instruments = eso.list_instruments(cache=False)
3434
# in principle, we should run both of these tests
3535
# result_i = eso.query_instrument('midi', target='Sgr A*')
3636
# Equivalent, does not depend on SESAME:
3737
result_i = eso.query_instrument('midi', coord1=266.41681662,
38-
coord2=-29.00782497)
38+
coord2=-29.00782497, cache=False)
3939

40-
surveys = eso.list_surveys()
40+
surveys = eso.list_surveys(cache=False)
41+
assert len(surveys) > 0
4142
# result_s = eso.query_survey('VVV', target='Sgr A*')
4243
# Equivalent, does not depend on SESAME:
4344
result_s = eso.query_survey('VVV', coord1=266.41681662,
44-
coord2=-29.00782497)
45+
coord2=-29.00782497, cache=False)
4546

4647
assert 'midi' in instruments
4748
assert result_i is not None
@@ -65,35 +66,36 @@ def test_nologin(self):
6566
def test_empty_return(self):
6667
# test for empty return with an object from the North
6768
eso = Eso()
68-
surveys = eso.list_surveys()
69+
surveys = eso.list_surveys(cache=False)
70+
assert len(surveys) > 0
6971
# result_s = eso.query_survey(surveys[0], target='M51')
7072
# Avoid SESAME
71-
result_s = eso.query_survey(surveys[0],
72-
coord1=202.469575, coord2=47.195258)
73+
result_s = eso.query_survey(surveys[0], coord1=202.469575,
74+
coord2=47.195258, cache=False)
7375

7476
assert result_s is None
7577

7678
def test_SgrAstar_remotevslocal(self, temp_dir):
7779
eso = Eso()
7880
# Remote version
79-
instruments = eso.list_instruments()
81+
instruments = eso.list_instruments(cache=False)
8082
# result1 = eso.query_instrument(instruments[0], target='Sgr A*')
8183
result1 = eso.query_instrument(instruments[0], coord1=266.41681662,
82-
coord2=-29.00782497)
84+
coord2=-29.00782497, cache=False)
8385

8486
# Local version
8587
eso.cache_location = temp_dir
86-
instruments = eso.list_instruments()
88+
instruments = eso.list_instruments(cache=False)
8789
# result2 = eso.query_instrument(instruments[0], target='Sgr A*')
8890
result2 = eso.query_instrument(instruments[0], coord1=266.41681662,
89-
coord2=-29.00782497)
91+
coord2=-29.00782497, cache=False)
9092

9193
assert result1 == result2
9294

9395
def test_list_instruments(self):
9496
# If this test fails, we may simply need to update it
9597

96-
inst = set(Eso.list_instruments())
98+
inst = set(Eso.list_instruments(cache=False))
9799

98100
# we only care about the sets matching
99101
assert set(inst) == set(instrument_list)

0 commit comments

Comments
 (0)