Skip to content

Commit

Permalink
Add flat option for now subdirs in Observation.download_products
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavies-st committed Sep 7, 2022
1 parent 3a57b4c commit 521f829
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions astroquery/mast/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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/<obs_collection>/<obs_id>/".
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.
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 521f829

Please sign in to comment.