Authentication

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.

We recommend authentication using earthaccess Python package, which you can also call from R. Please check the how do I find data using code on how to utilize the earthaccess package.

Authentication is also possible without the earthaccess package. 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. The following code creates a hidden .netrc file (_netrc for Windows OS) with Earthdata login credentials in your home directory. This file is needed to access NASA Earthdata assets from scripting environments like Python and R. Below is code describing this process.

Please see this 2021 Cloud Hackathon tutorial for Python code.

For authentication in R, please run the code below:

[R code]
# Required packages for this script
packages = c('sys', 'getPass')

# Identify missing (not installed) packages
new.packages = packages[!(packages %in% installed.packages()[,"Package"])]

# Install missing packages
if(length(new.packages)) install.packages(new.packages, repos='http://cran.rstudio.com/')

# Load packages into R
library(sys)
library(getPass)

# Determine OS and associated netrc file 
netrc_type <- if(.Platform$OS.type == "windows") "_netrc" else ".netrc"    # Windows OS uses _netrc file

# Specify path to user profile 
up <- file.path(Sys.getenv("USERPROFILE"))                            # Retrieve user directory (for netrc file)

# Below, HOME and Userprofile directories are set.  

if (up == "") {
    up <- Sys.getenv("HOME") 
    Sys.setenv("userprofile" = up)
    if (up == "") {
        cat('USERPROFILE/HOME directories need to be set up. Please type sys.setenv("HOME" = "YOURDIRECTORY") or  sys.setenv("USERPROFILE" = "YOURDIRECTORY") in your console and type your USERPROFILE/HOME direcory instead of "YOURDIRECTORY". Next, run the code chunk again.')
    }
} else {Sys.setenv("HOME" = up)}        

netrc_path <- file.path(up, netrc_type, fsep = .Platform$file.sep)    # Path to netrc file

# Create a netrc file if one does not exist already
if (file.exists(netrc_path) == FALSE || grepl("urs.earthdata.nasa.gov", readLines(netrc_path)) == FALSE) {
    netrc_conn <- file(netrc_path)
    
    # User will be prompted for NASA Earthdata Login Username and Password below
    writeLines(c("machine urs.earthdata.nasa.gov",
                 sprintf("login %s", getPass(msg = "Enter NASA Earthdata Login Username \n (An account can be Created at urs.earthdata.nasa.gov):")),
                 sprintf("password %s", getPass(msg = "Enter NASA Earthdata Login Password:"))), netrc_conn)
    close(netrc_conn)
}else{
    i <- 0 
    for (f in readLines(netrc_path)){
        i <- i + 1
        if (f =="machine urs.earthdata.nasa.gov"){
            username <- strsplit(readLines(netrc_path)[i+1], " ")[[1]][2]
            un <- getPass(msg = paste0("Is your NASA Earthdata Login Username: ", username, "\n\n Type yes or no."))
            if (tolower(un) == 'yes'){
                tx <- gsub(readLines(netrc_path)[i+2], sprintf("password %s", getPass(msg = "Enter NASA Earthdata Login Password:")), readLines(netrc_path))
                writeLines(tx, netrc_path)
                rm(username, un, tx, f, i)
            }else{
                user <- gsub(readLines(netrc_path)[i+1], sprintf("login %s", getPass(msg = "Enter NASA Earthdata Login Username:")), readLines(netrc_path))
                tx <- gsub(readLines(netrc_path)[i+2], sprintf("password %s", getPass(msg = "Enter NASA Earthdata Login Password:")), readLines(netrc_path))
                writeLines(tx, netrc_path)
                rm(username, un, user, tx, f, i)
            
            }
            break
        }
    }
}

AWS Credentials

coming soon