Skip to content

Commit 4120e12

Browse files
gbrammerbsipocz
authored andcommitted
Allow for requesting datasets from an earlier ESO archive retrieval without rerunning the query
1 parent e8489d3 commit 4120e12

File tree

1 file changed

+59
-30
lines changed

1 file changed

+59
-30
lines changed

astroquery/eso/core.py

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,8 @@ def _download_file(self, url, local_filepath, **kwargs):
629629
return resp
630630

631631
def retrieve_data(self, datasets, continuation=False, destination=None,
632-
with_calib='none', request_all_objects=False, unzip=True):
632+
with_calib='none', request_all_objects=False, unzip=True,
633+
request_id=None):
633634
"""
634635
Retrieve a list of datasets form the ESO archive.
635636
@@ -657,6 +658,12 @@ def retrieve_data(self, datasets, continuation=False, destination=None,
657658
unzip : bool
658659
Unzip compressed files from the archive after download. `True` by
659660
default.
661+
request_id : str, int
662+
Retrieve from an existing request number rather than sending a new
663+
query, with the `request_id` from the URL in the email sent from
664+
the archive from the earlier request as in:
665+
666+
https://dataportal.eso.org/rh/requests/[USERNAME]/[request_id]
660667
661668
Returns
662669
-------
@@ -694,9 +701,14 @@ def retrieve_data(self, datasets, continuation=False, destination=None,
694701
datasets, continuation=continuation, destination=destination)
695702

696703
# Second: Check that the datasets to download are in the archive
697-
log.info("Checking availability of datasets to download...")
698-
valid_datasets = [self.verify_data_exists(ds)
704+
if request_id is None:
705+
log.info("Checking availability of datasets to download...")
706+
valid_datasets = [self.verify_data_exists(ds)
699707
for ds in datasets_to_download]
708+
else:
709+
# Assume all valid if a request_id was provided
710+
valid_datasets = [(ds, True) for ds in datasets_to_download]
711+
700712
if not all(valid_datasets):
701713
invalid_datasets = [ds for ds, v in zip(datasets_to_download,
702714
valid_datasets) if not v]
@@ -710,33 +722,42 @@ def retrieve_data(self, datasets, continuation=False, destination=None,
710722
self.login()
711723
url = "http://archive.eso.org/cms/eso-data/eso-data-direct-retrieval.html"
712724
with suspend_cache(self): # Never cache staging operations
713-
log.info("Contacting retrieval server...")
714-
retrieve_data_form = self._request("GET", url, cache=False)
715-
retrieve_data_form.raise_for_status()
716-
log.info("Staging request...")
717-
inputs = {"list_of_datasets": "\n".join(datasets_to_download)}
718-
data_confirmation_form = self._activate_form(
719-
retrieve_data_form, form_index=-1, inputs=inputs,
720-
cache=False)
721-
722-
data_confirmation_form.raise_for_status()
723-
724-
root = BeautifulSoup(data_confirmation_form.content,
725-
'html5lib')
726-
login_button = root.select('input[value=LOGIN]')
727-
if login_button:
728-
raise LoginError("Not logged in. "
729-
"You must be logged in to download data.")
730-
inputs = {}
731-
if with_calib != 'none':
732-
inputs['requestCommand'] = calib_options[with_calib]
733-
734-
# TODO: There may be another screen for Not Authorized; that
735-
# should be included too
736-
# form name is "retrieve"; no id
737-
data_download_form = self._activate_form(
738-
data_confirmation_form, form_index=-1, inputs=inputs,
739-
cache=False)
725+
if request_id is None:
726+
log.info("Contacting retrieval server...")
727+
retrieve_data_form = self._request("GET", url,
728+
cache=False)
729+
retrieve_data_form.raise_for_status()
730+
log.info("Staging request...")
731+
inputs = {"list_of_datasets": "\n".join(datasets_to_download)}
732+
data_confirmation_form = self._activate_form(
733+
retrieve_data_form, form_index=-1, inputs=inputs,
734+
cache=False)
735+
736+
data_confirmation_form.raise_for_status()
737+
738+
root = BeautifulSoup(data_confirmation_form.content,
739+
'html5lib')
740+
login_button = root.select('input[value=LOGIN]')
741+
if login_button:
742+
raise LoginError("Not logged in. "
743+
"You must be logged in to download data.")
744+
inputs = {}
745+
if with_calib != 'none':
746+
inputs['requestCommand'] = calib_options[with_calib]
747+
748+
# TODO: There may be another screen for Not Authorized;
749+
# that should be included too
750+
# form name is "retrieve"; no id
751+
data_download_form = self._activate_form(
752+
data_confirmation_form, form_index=-1, inputs=inputs,
753+
cache=False)
754+
else:
755+
# Build URL by hand
756+
request_url = 'https://dataportal.eso.org/rh/requests/'
757+
request_url += f'{self.USERNAME}/{request_id}'
758+
data_download_form = self._request("GET", request_url,
759+
cache=False)
760+
740761
log.info("Staging form is at {0}"
741762
.format(data_download_form.url))
742763
root = BeautifulSoup(data_download_form.content, 'html5lib')
@@ -809,6 +830,14 @@ def retrieve_data(self, datasets, continuation=False, destination=None,
809830
log.debug("Files:\n{}".format('\n'.join(fileLinks)))
810831
for i, fileLink in enumerate(fileLinks, 1):
811832
fileId = fileLink.rsplit('/', maxsplit=1)[1]
833+
834+
if request_id is not None:
835+
# Since we fetched the script directly without sending
836+
# a new request, check here that the file in the list
837+
# is among those requested in the input list
838+
if fileId.split('.fits')[0] not in datasets_to_download:
839+
continue
840+
812841
log.info("Downloading file {}/{}: {}..."
813842
.format(i, nfiles, fileId))
814843
filename = self._request("GET", fileLink, save=True,

0 commit comments

Comments
 (0)