Skip to content

Commit

Permalink
download routine
Browse files Browse the repository at this point in the history
  • Loading branch information
emmamendelsohn committed Sep 5, 2023
1 parent d0ad8b9 commit 135238e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
40 changes: 40 additions & 0 deletions R/download_modis_ndvi.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#' .. content for \description{} (no empty lines) ..
#'
#' .. content for \details{} ..
#'
#' @title
#' @param modis_ndvi_bundle_request
#' @param download_directory
#' @param overwrite
#' @return
#' @author Emma Mendelsohn
#' @export
download_modis_ndvi <- function(modis_ndvi_token,
modis_ndvi_task_request,
file,
download_directory,
overwrite = FALSE) {

existing_files <- list.files(download_directory)

# Get file ID and file name
file_id <- unlist(file$file_id)
filename <- basename(unlist(file$file_name))

message(paste0("Downloading ", filename))

if(filename %in% existing_files & !overwrite) {
message("file already exists, skipping download")
return(file.path(download_directory, filename)) # skip if file exists
}

# Get the task ID for downloading
modis_ndvi_request_task_id <- fromJSON(modis_ndvi_task_request)$task_id

# Write the file to disk
response <- GET(paste("https://appeears.earthdatacloud.nasa.gov/api/bundle/", modis_ndvi_request_task_id, '/', file_id, sep = ""),
write_disk(file.path(download_directory, filename), overwrite = TRUE), progress(), add_headers(Authorization = modis_ndvi_token))

return(file.path(file.path(download_directory, filename)))

}
10 changes: 1 addition & 9 deletions R/submit_modis_ndvi_bundle_request.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,9 @@ submit_modis_ndvi_bundle_request <- function(modis_ndvi_token, modis_ndvi_task_r
}
}

bundle_response <- GET(paste("https://appeears.earthdatacloud.nasa.gov/api/bundle/", task_id, sep = ""), add_headers(Authorization = modis_ndvi_token))
bundle_response <- GET(paste("https://appeears.earthdatacloud.nasa.gov/api/bundle/", modis_ndvi_request_task_id, sep = ""), add_headers(Authorization = modis_ndvi_token))
bundle_response <- fromJSON(toJSON(content(bundle_response)))

return(bundle_response)
# file_id <- bundle_response$files$file_id
# file_id <- file_id[[1]]
#
# filepath <- paste(modis_ndvi_directory_raw, file_id, sep = '/')
# # write the file to disk using the destination directory and file name
# response <- GET(paste("https://appeears.earthdatacloud.nasa.gov/api/bundle/", task_id, '/', file_id, sep = ""),
# write_disk(filepath, overwrite = TRUE), progress(), add_headers(Authorization = token))
#

}
10 changes: 6 additions & 4 deletions _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,12 @@ dynamic_targets <- tar_plan(
tar_target(modis_ndvi_bundle_request, submit_modis_ndvi_bundle_request(modis_ndvi_token, modis_ndvi_task_request, timeout = 1500)),

# download files from source (locally)
tar_target(modis_ndvi_downloaded, download_modis_ndvi(modis_ndvi_bundle_request,
download_directory = modis_ndvi_directory_raw,
overwrite = FALSE),
pattern = modis_ndvi_bundle_request,
tar_target(modis_ndvi_downloaded, download_modis_ndvi(modis_ndvi_token,
modis_ndvi_task_request,
file = modis_ndvi_bundle_request$files,
download_directory = modis_ndvi_directory_raw,
overwrite = FALSE),
pattern = modis_ndvi_bundle_request$files,
format = "file",
repository = "local",
cue = tar_cue("thorough")),
Expand Down

0 comments on commit 135238e

Please sign in to comment.