diff --git a/CHANGELOG.md b/CHANGELOG.md index b8991cc..ab8af55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Fixed * Copernicus DEM for NISAR URLs to fix 404 errors. +* Support for EARTHDATA credentials passed as env vars. ## [0.5.3] diff --git a/src/multirtc/__main__.py b/src/multirtc/__main__.py index d0273fb..707c9b1 100644 --- a/src/multirtc/__main__.py +++ b/src/multirtc/__main__.py @@ -1,10 +1,17 @@ import argparse +import os from multirtc import dem, geocode, multirtc +from multirtc.fetch import write_credentials_to_netrc_file from multirtc.multimetric import ale, point_target, rle def main(): + username = os.getenv('EARTHDATA_USERNAME') + password = os.getenv('EARTHDATA_PASSWORD') + if username and password: + write_credentials_to_netrc_file(username, password, append=False) + global_parser = argparse.ArgumentParser( prog='multirtc', description='ISCE3-based multi-sensor RTC and cal/val tool', diff --git a/src/multirtc/fetch.py b/src/multirtc/fetch.py index ba3516a..f779632 100644 --- a/src/multirtc/fetch.py +++ b/src/multirtc/fetch.py @@ -11,6 +11,21 @@ from urllib3.util.retry import Retry +EARTHDATA_LOGIN_DOMAIN = 'urs.earthdata.nasa.gov' + + +def write_credentials_to_netrc_file( + username: str, password: str, domain: str = EARTHDATA_LOGIN_DOMAIN, append: bool = False +): + """Write credentials to .netrc file""" + netrc_file = Path.home() / '.netrc' + if netrc_file.exists() and not append: + logging.warning(f'Using existing .netrc file: {netrc_file}') + else: + with open(netrc_file, 'a') as f: + f.write(f'machine {domain} login {username} password {password}\n') + + def _get_download_path(url: str, content_disposition: str | None = None, directory: Path | str = '.'): filename = None if content_disposition is not None: