@@ -94,12 +94,11 @@ def __call__(self, bytes_transferred):
9494 _sys .stdout .flush ()
9595
9696
97-
98- def get_earthdata_credentials (username : str = None , password : str = None ) -> Tuple [str , str ]:
97+ def get_earthdata_credentials (username : Optional [str ] = None , password : Optional [str ] = None ) -> Tuple [str , str ]:
9998 """
10099 Get NASA Earthdata credentials from .netrc file or direct parameters.
101- :param str username: Directly provided username (highest priority)
102- :param str password: Directly provided password (highest priority)
100+ :param Optional[ str] username: Directly provided username (highest priority)
101+ :param Optional[ str] password: Directly provided password (highest priority)
103102 :return Tuple[str, str]: Username and password tuple
104103 :raises ValueError: If no credentials can be obtained
105104 """
@@ -774,17 +773,17 @@ def ftp_tls(url: str, **kwargs) -> Generator[Any, Any, Any]:
774773
775774def download_file_from_cddis (
776775 filename : str ,
777- ftp_folder : Optional [str ] = None , # deprecated
778- url_folder : Optional [str ] = None , # preferred
776+ ftp_folder : Optional [str ] = None , # deprecated
777+ url_folder : Optional [str ] = None , # preferred
779778 output_folder : _Path = _Path ("." ),
780779 max_retries : int = 3 ,
781780 decompress : bool = True ,
782781 if_file_present : str = "prompt_user" ,
783- username : str = None ,
784- password : str = None ,
782+ username : Optional [ str ] = None ,
783+ password : Optional [ str ] = None ,
785784 note_filetype : Optional [str ] = None ,
786785) -> Union [_Path , None ]:
787- """ Download a single file from the CDDIS HTTPS archive using NASA Earthdata authentication
786+ """Download a single file from the CDDIS HTTPS archive using NASA Earthdata authentication
788787
789788 :param str filename: Name of the file to download
790789 :param str ftp_folder: (Deprecated) Legacy folder path on the CDDIS FTP server. Use url_folder instead
@@ -794,8 +793,8 @@ def download_file_from_cddis(
794793 :param bool decompress: If true, decompresses files on download, defaults to True
795794 :param str if_file_present: What to do if file already present: "replace", "dont_replace", defaults to "prompt_user"
796795 :param str note_filetype: How to label the file for STDOUT messages, defaults to None
797- :param str username: NASA Earthdata username (optional, will try .netrc if not provided).
798- :param str password: NASA Earthdata password (optional, will try .netrc if not provided).
796+ :param Optional[ str] username: NASA Earthdata username (optional, will try .netrc if not provided).
797+ :param Optional[ str] password: NASA Earthdata password (optional, will try .netrc if not provided).
799798 :raises ValueError: If no credentials can be obtained.
800799 :raises requests.RequestException: If the file cannot be downloaded after retries.
801800 :return _Path or None: The pathlib.Path of the downloaded file (or decompressed output of it).
@@ -823,14 +822,8 @@ def download_file_from_cddis(
823822 if download_filepath is None :
824823 return None # File exists and user chose not to replace
825824
826- # Get NASA Earthdata credentials
827- try :
828- earthdata_username , earthdata_password = get_earthdata_credentials (
829- username = username , password = password
830- )
831- except ValueError as e :
832- logging .error (f"Failed to obtain NASA Earthdata credentials: { e } " )
833- raise
825+ # Get NASA Earthdata credentials (raises ValueError on failure)
826+ earthdata_username , earthdata_password = get_earthdata_credentials (username = username , password = password )
834827
835828 retries = 0
836829 while retries <= max_retries :
@@ -862,25 +855,27 @@ def download_file_from_cddis(
862855 except _requests .exceptions .RequestException as e :
863856 retries += 1
864857 if retries > max_retries :
858+ # TODO consider wrapping the RequestException with this, and raising that, rather than logging an error
865859 logging .error (f"Failed to download { filename } after { max_retries } retries: { e } " )
866860 if download_filepath .is_file ():
867861 download_filepath .unlink ()
868862 raise
869863 backoff = _random .uniform (0.0 , 2.0 ** retries )
870- logging .warning (f"Error downloading { filename } : { e } "
871- f"(retry { retries } /{ max_retries } , backoff { backoff :.1f} s)" )
864+ _warnings .warn (
865+ f"Error downloading { filename } : { e } " f"(retry { retries } /{ max_retries } , backoff { backoff :.1f} s)"
866+ )
872867 _time .sleep (backoff )
873868
874869 raise Exception ("Unexpected fallthrough in download_file_from_cddis." )
875870
876871
877872def download_multiple_files_from_cddis (
878873 files : List [str ],
879- ftp_folder : Optional [str ] = None , # deprecated
880- url_folder : Optional [str ] = None , # preferred
874+ ftp_folder : Optional [str ] = None , # deprecated
875+ url_folder : Optional [str ] = None , # preferred
881876 output_folder : _Path = _Path ("." ),
882- username : str = None ,
883- password : str = None ,
877+ username : Optional [ str ] = None ,
878+ password : Optional [ str ] = None ,
884879) -> None :
885880 """
886881 Download multiple files from the CDDIS HTTPS archive concurrently, using a thread pool.
@@ -889,8 +884,8 @@ def download_multiple_files_from_cddis(
889884 :param str ftp_folder: (Deprecated) Legacy folder path on the CDDIS FTP server. Use url_folder instead.
890885 :param str url_folder: Folder path (relative to CDDIS HTTPS archive root).
891886 :param _Path output_folder: Local folder to store the output files.
892- :param str username: NASA Earthdata username (optional, will try .netrc if not provided).
893- :param str password: NASA Earthdata password (optional, will try .netrc if not provided).
887+ :param Optional[ str] username: NASA Earthdata username (optional, will try .netrc if not provided).
888+ :param Optional[ str] password: NASA Earthdata password (optional, will try .netrc if not provided).
894889 :raises ValueError: If both ftp_folder and url_folder are provided.
895890 :return None: Runs downloads in parallel, does not return values. Each file is handled independently.
896891 """
@@ -943,8 +938,8 @@ def download_product_from_cddis(
943938 project_type : str = "OPS" ,
944939 timespan : _datetime .timedelta = _datetime .timedelta (days = 2 ),
945940 if_file_present : str = "prompt_user" ,
946- username : str = None ,
947- password : str = None ,
941+ username : Optional [ str ] = None ,
942+ password : Optional [ str ] = None ,
948943) -> List [_Path ]:
949944 """Download the file/s from CDDIS based on start and end epoch, to the download directory (download_dir)
950945
@@ -961,8 +956,8 @@ def download_product_from_cddis(
961956 :param str project_type: Project type of file to download (e.g. ), defaults to "OPS"
962957 :param _datetime.timedelta timespan: Timespan of the file/s to download, defaults to _datetime.timedelta(days=2)
963958 :param str if_file_present: What to do if file already present: "replace", "dont_replace", defaults to "prompt_user"
964- :param str username: NASA Earthdata username (optional, will try .netrc if not provided).
965- :param str password: NASA Earthdata password (optional, will try .netrc if not provided).
959+ :param Optional[ str] username: NASA Earthdata username (optional, will try .netrc if not provided).
960+ :param Optional[ str] password: NASA Earthdata password (optional, will try .netrc if not provided).
966961 :raises FileNotFoundError: Raise error if the specified file cannot be found on CDDIS
967962 :raises Exception: If a file fails to download after all retries.
968963 :return List[_Path]: List of pathlib.Path objects to downloaded (or decompressed) files.
0 commit comments