Downloading ASDC Data with Python 3

Please enter here to ask a question about any NASA Science related topics!
Post Reply
njester
Posts: 4
Joined: Sat Mar 06, 2021 9:03 am America/New_York

Downloading ASDC Data with Python 3

by njester » Mon May 24, 2021 11:49 am America/New_York

The following allows you to download data from https://asdc.larc.nasa.gov/data/ using linux, a mac, or Windows. This guide uses the requests package, you can read more about the requests module here: https://pypi.org/project/requests/

Please note, for the following examples to work, you'll need to assign values to the following variables:
  • username
  • password
  • url
  • file_name

Code: Select all

from getpass import getpass
from pathlib import Path
username=input("Earthdata username: ")
password=getpass("Earthdata password: ")
url="https://asdc.larc.nasa.gov/data/ACEPOL/MetNav_AircraftInSitu_Data_1/ACEPOL-HSKPING_ER2_20171019_R0.ict"
file_name = Path(url).name
To download a single file:

Code: Select all

from requests import Session 
session = Session() 
session.auth = (username, password) 
_redirect = session.get(url) 
_response = session.get(_redirect.url) 
with open(file_name, 'wb') as file: 
    file.write(_response._content) 
To stream a file:

Code: Select all

from requests import Session 
session = Session() 
session.auth = (username, password) 
_redirect = session.get(url) 
_response = session.get(_redirect.url) 
with open(file_name, 'wb') as file: 
    for chunk in _response.iter_content(chunk_size=1024 ** 2): 
        file.write(chunk)

Tags:

Post Reply