from netrc import netrc
from subprocess import Popen
from platform import system
from getpass import getpass
import os
Authentication for NASA Earthdata
Written by NASA Openscapes mentors
Summary
This notebook creates a hidden .netrc
file (_netrc
for Window OS) with Earthdata login credentials in your home directory. This file is needed to access NASA Earthdata assets from a scripting environment like Python.
Earthdata Login
An Earthdata Login account is required to access data, as well as discover restricted data, from the NASA Earthdata system. Thus, to access NASA data, you need Earthdata Login. Please visit https://urs.earthdata.nasa.gov to register and manage your Earthdata Login account. This account is free to create and only takes a moment to set up.
Authentication via netrc File
You will need a netrc file containing your NASA Earthdata Login credentials in order to execute the notebooks. A netrc file can be created manually within text editor and saved to your home directory. An example of the required content is below.
machine urs.earthdata.nasa.gov
login <USERNAME>
password <PASSWORD>
<USERNAME>
and <PASSWORD>
would be replaced by your actual Earthdata Login username and password respectively.
Import Required Packages
The code below will:
- check what operating system (OS) is being used to determine which netrc file to check for/create (.netrc or _netrc)
- check if you have an netrc file, and if so, varify if those credentials are for the Earthdata endpoint
- create a netrc file if a netrc file is not present.
= 'urs.earthdata.nasa.gov' # Earthdata URL endpoint for authentication
urs = ['Enter NASA Earthdata Login Username: ',
prompts 'Enter NASA Earthdata Login Password: ']
# Determine the OS (Windows machines usually use an '_netrc' file)
= "_netrc" if system()=="Windows" else ".netrc"
netrc_name
# Determine if netrc file exists, and if so, if it includes NASA Earthdata Login Credentials
try:
= os.path.expanduser(f"~/{netrc_name}")
netrcDir 0]
netrc(netrcDir).authenticators(urs)[
# Below, create a netrc file and prompt user for NASA Earthdata Login Username and Password
except FileNotFoundError:
= os.path.expanduser("~")
homeDir 'touch {0}{2} | echo machine {1} >> {0}{2}'.format(homeDir + os.sep, urs, netrc_name), shell=True)
Popen('echo login {} >> {}{}'.format(getpass(prompt=prompts[0]), homeDir + os.sep, netrc_name), shell=True)
Popen('echo \'password {} \'>> {}{}'.format(getpass(prompt=prompts[1]), homeDir + os.sep, netrc_name), shell=True)
Popen(# Set restrictive permissions
'chmod 0600 {0}{1}'.format(homeDir + os.sep, netrc_name), shell=True)
Popen(
# Determine OS and edit netrc file if it exists but is not set up for NASA Earthdata Login
except TypeError:
= os.path.expanduser("~")
homeDir 'echo machine {1} >> {0}{2}'.format(homeDir + os.sep, urs, netrc_name), shell=True)
Popen('echo login {} >> {}{}'.format(getpass(prompt=prompts[0]), homeDir + os.sep, netrc_name), shell=True)
Popen('echo \'password {} \'>> {}{}'.format(getpass(prompt=prompts[1]), homeDir + os.sep, netrc_name), shell=True) Popen(
See if the file was created
If the file was created, we’ll see a .netrc
file (_netrc
for Window OS) in the list printed below. To view the contents from a Jupyter environment, click File on the top toolbar, select Open from Path…, type .netrc, and click Open. The .netrc
file will open within the text editor.
!!! Beware, your password will be visible if the
.netrc
file is opened in the text editor.
!