Quantcast
Channel: Earthdata Forum
Viewing all articles
Browse latest Browse all 1307

Questions/Comments • Re: Download L3SMI data using R

$
0
0
Hi Sean,

Thank you for your prompt response and for addressing the issue regarding the login redirect. I believe that it might be a problem with the credentials. After several attempts, I have been unable to resolve the issue with the .netrc file while using Catalio Cuadrado's script. I am considering the possibility that it could be related to the IT network configuration within my organization.

However, I have had success in converting the Python script available on the EarthData User website (https://wiki.earthdata.nasa.gov/display/EL/How+To+Access+Data+With+Python) into an R script, and it works perfectly! :-) In case others encounter a similar problem, I am sharing my script here for reference.

library(httr)

# Function to create a custom session for authentication
session <- function(username, password) {
httr::authenticate(username, password)
}

# Define username and password
username <- "xxxx"
password <- "xxxx"

# Create the session
my_session <- session(username, password)

# URL of the file to retrieve
url <- "https://oceandata.sci.gsfc.nasa.gov/getfile/L2023201.L3m_DAY_CYAN_CI_cyano_CYAN_CONUS_300m_2_2.tif"

# Extract the filename from the URL
filename <- sub(".*/", "", url)

tryCatch({
# Submit the request using the configured session
response <- httr::GET(
url,
httr::progress(),
httr::add_headers(`User-Agent` = "Mozilla/5.0"),
httr::authenticate(username, password)
)

# Check for successful response
if (httr::status_code(response) == 200) {
# Save the file
content <- httr::content(response, as = "raw")
writeBin(content, filename)
print("File downloaded successfully")
} else {
stop("Failed to download the file")
}
}, error = function(e) {
# Handle any errors here
print(paste("Error:", e$message))
})

Thank you again for your assistance.
Yuan

Statistics: Posted by ygrund — Tue Jan 09, 2024 1:07 pm America/New_York



Viewing all articles
Browse latest Browse all 1307

Trending Articles