diff --git a/astroquery/mast/observations.py b/astroquery/mast/observations.py index b00f915eb6..250b435853 100644 --- a/astroquery/mast/observations.py +++ b/astroquery/mast/observations.py @@ -584,7 +584,7 @@ def download_file(self, uri, *, local_path=None, base_url=None, cache=True, clou return status, msg, url - def _download_files(self, products, base_dir, *, cache=True, cloud_only=False,): + def _download_files(self, products, base_dir, flat=False, *, cache=True, cloud_only=False,): """ Takes an `~astropy.table.Table` of data products and downloads them into the directory given by base_dir. @@ -594,6 +594,9 @@ def _download_files(self, products, base_dir, *, cache=True, cloud_only=False,): Table containing products to be downloaded. base_dir : str Directory in which files will be downloaded. + flat : bool + Default is False. If set to True, no subdirectories will be made for the + downloaded files. cache : bool Default is True. If file is found on disk it will not be downloaded again. cloud_only : bool, optional @@ -610,9 +613,12 @@ def _download_files(self, products, base_dir, *, cache=True, cloud_only=False,): for data_product in products: # create the local file download path - local_path = os.path.join(base_dir, data_product['obs_collection'], data_product['obs_id']) - if not os.path.exists(local_path): - os.makedirs(local_path) + if not flat: + local_path = os.path.join(base_dir, data_product['obs_collection'], data_product['obs_id']) + if not os.path.exists(local_path): + os.makedirs(local_path) + else: + local_path = base_dir local_path = os.path.join(local_path, os.path.basename(data_product['productFilename'])) # download the files @@ -660,7 +666,7 @@ def _download_curl_script(self, products, out_dir): 'Message': [msg]}) return manifest - def download_products(self, products, *, download_dir=None, + def download_products(self, products, *, download_dir=None, flat=False, cache=True, curl_flag=False, mrp_only=False, cloud_only=False, **filters): """ Download data products. @@ -673,6 +679,12 @@ def download_products(self, products, *, download_dir=None, or a Table of products (as is returned by `get_product_list`) download_dir : str, optional Optional. Directory to download files to. Defaults to current directory. + flat : bool, optional + Default is False. If set to True, and download_dir is specified, it will put + all files into download_dir without subdirectories. Or if set to True and + download_dir is not specified, it will put files in the current directory, + again with no subdirs. The default of False puts files into the standard + directory structure of "mastDownload///". cache : bool, optional Default is True. If file is found on disc it will not be downloaded again. Note: has no affect when downloading curl script. @@ -735,9 +747,12 @@ def download_products(self, products, *, download_dir=None, download_dir) else: - base_dir = download_dir.rstrip('/') + "/mastDownload" + if flat: + base_dir = download_dir + else: + base_dir = os.path.join(download_dir, "mastDownload") manifest = self._download_files(products, - base_dir=base_dir, + base_dir=base_dir, flat=flat, cache=cache, cloud_only=cloud_only)