@@ -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 ,
633+ unzip = True , 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 identifier 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,52 @@ 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+
761+ _content = data_download_form .content .decode ('utf-8' )
762+ if ('Request Handler - Error' in _content ):
763+ # Likely a problem with the request_url
764+ msg = (f"The form at { request_url } returned an error."
765+ " See your recent requests at "
766+ "https://dataportal.eso.org/rh/requests/"
767+ f"{ self .USERNAME } /recentRequests" )
768+
769+ raise RemoteServiceError (msg )
770+
740771 log .info ("Staging form is at {0}"
741772 .format (data_download_form .url ))
742773 root = BeautifulSoup (data_download_form .content , 'html5lib' )
@@ -809,6 +840,14 @@ def retrieve_data(self, datasets, continuation=False, destination=None,
809840 log .debug ("Files:\n {}" .format ('\n ' .join (fileLinks )))
810841 for i , fileLink in enumerate (fileLinks , 1 ):
811842 fileId = fileLink .rsplit ('/' , maxsplit = 1 )[1 ]
843+
844+ if request_id is not None :
845+ # Since we fetched the script directly without sending
846+ # a new request, check here that the file in the list
847+ # is among those requested in the input list
848+ if fileId .split ('.fits' )[0 ] not in datasets_to_download :
849+ continue
850+
812851 log .info ("Downloading file {}/{}: {}..."
813852 .format (i , nfiles , fileId ))
814853 filename = self ._request ("GET" , fileLink , save = True ,
0 commit comments