Data Download Script with an EDL Token (Python)
Script
Purpose
The following Python code example demonstrates how to configure a connection to download data from an Earthdata Login enabled server. Note that you will need to a secure way to configure the Earthdata user token.
You can obtain user token using the EDL API, /api/users/find_or_create_token.
Examples
#!/usr/bin/env python3
import requests
import os
from http.cookiejar import LWPCookieJar
# Variables that need to be set by the user
bearer_token = input("Enter your EDL Bearer Token: ")
urls = ["https://n5eil01u.ecs.nsidc.org/MOST/NSIDC-0791.001/2000.03.01/NSIDC-0791_CSS_0.01Deg_WY2001-2023_V01.0.nc.xml",
"https://n5eil01u.ecs.nsidc.org/MOST/NSIDC-0791.001/2000.03.01/NSIDC-0791_FSS_0.01Deg_WY2001-2023_V01.0.nc.xml"]
# Setup a MozillaCookieJar to persist cookies
cookie_file = os.path.expanduser("~/.cookies.txt")
cookie_jar = LWPCookieJar(cookie_file)
# Load existing cookies if available
try:
cookie_jar.load(ignore_discard=True)
except FileNotFoundError:
pass
session = requests.Session()
session.cookies = cookie_jar
headers = {"Authorization": f"Bearer {bearer_token}"}
for url in urls:
try:
response = session.get(url, headers=headers, stream=True)
print(f"{url} -> HTTP {response.status_code}")
response.raise_for_status()
filename = url.split('/')[-1] or "index.html"
with open(filename, 'wb') as fd:
for chunk in response.iter_content(chunk_size=1024*1024):
if chunk:
fd.write(chunk)
print(f"Downloaded: {filename}")
# Save cookies after each request
cookie_jar.save(ignore_discard=True)
except requests.exceptions.RequestException as e:
print(f"Error downloading {url}: {e}")
# If you are seeing 429 (rate limit), we suggest adding retry logic to the script.