|
2 | 2 |
|
3 | 3 | import os
|
4 | 4 | import logging
|
| 5 | +from collections.abc import Iterable |
5 | 6 | import cdsapi
|
6 | 7 |
|
7 | 8 | from .downloader import BaseDownloader
|
|
12 | 13 | class CDSDownloader(BaseDownloader):
|
13 | 14 |
|
14 | 15 | def __init__(self, product_name, config, request_dictionary, dataset,
|
15 |
| - overwrite): |
| 16 | + overwrite, extra_name=''): |
16 | 17 | super().__init__(config, dataset, overwrite)
|
17 | 18 | self._client = cdsapi.Client()
|
18 | 19 | self._product_name = product_name
|
19 | 20 | self._request_dict = request_dictionary
|
| 21 | + self.extra_name = extra_name |
20 | 22 |
|
21 | 23 | def download(self, year, month, day=None):
|
22 | 24 | request_dict = self._request_dict.copy()
|
23 | 25 | request_dict['year'] = f'{year}'
|
24 | 26 | request_dict['month'] = f"{month:02d}"
|
25 | 27 | if day:
|
26 |
| - request_dict['day'] = f"{day:02d}" |
| 28 | + if isinstance(day, Iterable): |
| 29 | + request_dict['day'] = day |
| 30 | + else: |
| 31 | + request_dict['day'] = f"{day:02d}" |
27 | 32 |
|
28 |
| - date_str = f"{self.dataset}_{year}{month:02d}" |
| 33 | + date_str = f"{self.dataset}{self.extra_name}_{year}{month:02d}" |
29 | 34 | if day:
|
30 |
| - date_str += f"{day:02d}" |
| 35 | + if not isinstance(day, Iterable): |
| 36 | + date_str += f"{day:02d}" |
31 | 37 |
|
32 | 38 | os.makedirs(self.local_folder, exist_ok=True)
|
33 |
| - file_path = os.path.join( |
34 |
| - self.local_folder, |
35 |
| - f"{self.dataset}_{date_str}.tar" |
36 |
| - ) |
37 |
| - if os.path.exists(file_path): |
| 39 | + file_path = f"{self.dataset}_{date_str}.tar" |
| 40 | + self.download_request(file_path, request_dict) |
| 41 | + |
| 42 | + def download_request(self, filename, request=None): |
| 43 | + if request is None: |
| 44 | + request = self._request_dict.copy() |
| 45 | + |
| 46 | + os.makedirs(self.local_folder, exist_ok=True) |
| 47 | + filename = os.path.join(self.local_folder, filename) |
| 48 | + if os.path.exists(filename): |
38 | 49 | if self.overwrite:
|
39 |
| - os.remove(file_path) |
| 50 | + os.remove(filename) |
40 | 51 | else:
|
41 | 52 | logger.info(
|
42 |
| - 'File %s already downloaded. Skipping...', file_path) |
| 53 | + 'File %s already downloaded. Skipping...', filename) |
43 | 54 | return
|
44 | 55 | self._client.retrieve(
|
45 | 56 | self._product_name,
|
46 |
| - request_dict, |
47 |
| - file_path, |
| 57 | + request, |
| 58 | + filename, |
48 | 59 | )
|
0 commit comments