Last active
January 30, 2026 14:41
-
-
Save ajelenak/f95f94a61cc6fcaa7224fd2940a49636 to your computer and use it in GitHub Desktop.
A Python script for running a command-line tool on an HDF5 file available in the NASA Earthdata Cloud. An Earthdata Login is required while the script manages obtaining AWS temporary credentials. HDF5 library 2.0.0 or later is required.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /// script | |
| # requires-python = ">=3.11" | |
| # dependencies = [ | |
| # "earthaccess", | |
| # ] | |
| # /// | |
| # This script contains inline script metadata (above) with its package | |
| # dependencies. The package managers that understand these metadata and enable | |
| # automatic installation of the dependencies are: uv, pipx, hatch, pdm, and | |
| # pixi. For any other, use that tool's method of installing the listed | |
| # dependencies. | |
| # | |
| # User Earthdata login must be in the netrc file. See | |
| # https://nsidc.org/data/user-resources/help-center/creating-netrc-file-earthdata-login. | |
| # | |
| # Example: | |
| # | |
| # python earthdata-cmd.py <DAAC> h5dump -H s3://mybucket/myfile.h5 | |
| # | |
| # <DAAC> is required as it defines which URL to use for AWS temporary | |
| # credentials. Run with the --help option to see all the values for <DAAC>. | |
| import argparse | |
| import os | |
| import subprocess | |
| import earthaccess as ea | |
| DAACs = [_["short-name"] for _ in ea.daac.DAACS] | |
| DAACs.append("NISAR") # This is a fix for NISAR-specific credential endpoint | |
| def get_s3_credentials(daac: str) -> None: | |
| """Fetches temporary AWS credentials using earthaccess.""" | |
| auth = ea.login(strategy="netrc", persist=False) | |
| if daac == "NISAR": | |
| creds = auth.get_s3_credentials( | |
| endpoint="https://nisar.asf.earthdatacloud.nasa.gov/s3credentials" | |
| ) | |
| else: | |
| creds = auth.get_s3_credentials(daac=daac) | |
| os.environ["AWS_ACCESS_KEY_ID"] = creds["accessKeyId"] | |
| os.environ["AWS_SECRET_ACCESS_KEY"] = creds["secretAccessKey"] | |
| os.environ["AWS_SESSION_TOKEN"] = creds["sessionToken"] | |
| def run_external_command(command): | |
| """Executes the command.""" | |
| print(f"Executing: {' '.join(command)}\n") | |
| try: | |
| subprocess.run(command, check=True) | |
| except subprocess.CalledProcessError as e: | |
| print(f"\nError: {e}") | |
| except FileNotFoundError: | |
| print(f"\nError: The command '{command[0]}' was not found.") | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description="Run a command on a NASA Earthdata Cloud HDF5 file." | |
| ) | |
| parser.add_argument( | |
| "daac", | |
| help="The DAAC managing the HDF5 file", | |
| type=str.upper, | |
| choices=DAACs, | |
| ) | |
| parser.add_argument("command", help="Command to execute", nargs=argparse.REMAINDER) | |
| args = parser.parse_args() | |
| try: | |
| get_s3_credentials(args.daac) | |
| run_external_command(args.command) | |
| except Exception as error: | |
| print(f"Application Error: {error}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment