Skip to content

Commit

Permalink
incomplete/corrupt file detection; temporary remove problematic calc_…
Browse files Browse the repository at this point in the history
…lag tests
  • Loading branch information
mitchellmanware committed Jul 22, 2024
1 parent 72cc9e4 commit 5b5be70
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 135 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export(calc_terraclimate)
export(calc_time)
export(calc_tri)
export(calc_worker)
export(check_file_size)
export(check_for_null_parameters)
export(check_mysf)
export(check_mysftime)
Expand Down Expand Up @@ -155,9 +156,12 @@ importFrom(future.apply,future_Map)
importFrom(future.apply,future_lapply)
importFrom(httr,GET)
importFrom(httr,HEAD)
importFrom(httr2,req_perform)
importFrom(httr2,request)
importFrom(methods,is)
importFrom(nhdplusTools,get_huc)
importFrom(parallelly,availableWorkers)
importFrom(purrr,map_dbl)
importFrom(rlang,inject)
importFrom(rlang,sym)
importFrom(rstac,assets_url)
Expand Down
8 changes: 4 additions & 4 deletions R/calculate_covariates.R
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ calc_hms <- function(
#### from == character indicates no wildfire smoke plumes are present
#### return 0 for all locs and dates
if (is.character(from)) {
cat(paste0(
message(paste0(
"Inherited list of dates due to absent smoke plume polygons.\n"
))
skip_extraction <- NULL
Expand Down Expand Up @@ -1511,7 +1511,7 @@ calc_hms <- function(
skip_extraction_date
)
}
cat(paste0(
message(paste0(
"Returning ",
tolower(skip_variable),
" smoke plume covariates.\n"
Expand Down Expand Up @@ -2035,7 +2035,7 @@ calc_sedac_population <- function(
names(from),
"_"
)[[1]]
cat(
message(
paste0(
"Calculating population covariates for ",
name_split[4],
Expand Down Expand Up @@ -2532,7 +2532,7 @@ calc_lagged <- function(
stopifnot(!is.null(time_id))
#### return from if lag == 0
if (lag == 0) {
cat("`lag` set to 0. Returning `from`.\n")
message("`lag` set to 0. Returning `from`.\n")
return(from)
}
#### extract times
Expand Down
4 changes: 2 additions & 2 deletions R/calculate_covariates_auxiliary.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ calc_message <- function(
)
}
}
cat(return_message)
message(return_message)
}

#' Prepare extraction locations
Expand Down Expand Up @@ -483,7 +483,7 @@ calc_worker <- function(
)
}
#### finish message
cat(
message(
paste0(
"Returning extracted covariates.\n"
)
Expand Down
77 changes: 43 additions & 34 deletions R/download.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ download_aqs <-
}
#### 5. build download file name
download_names <- sprintf(
paste(directory_to_download,
paste(
directory_to_download,
"aqs_",
resolution_temporal,
"_",
Expand All @@ -229,7 +230,7 @@ download_aqs <-
#### filter commands to non-existing files
download_commands <- download_commands[
which(
!file.exists(download_names)
!check_file_size(download_urls, download_names)
)
]
#### 7. initiate "..._curl_commands.txt"
Expand Down Expand Up @@ -385,11 +386,13 @@ download_ecoregion <- function(
Sys.Date(),
"_wget_command.txt"
)
#### 9. concatenate
#### 9. concatenateƒ
download_sink(commands_txt)
# if (!check_file_size(download_url, download_name)) {
if (!file.exists(download_name)) {
#### 10. concatenate and print download commands to "..._wget_commands.txt"
#### cat command only if file does not already exist
#### cat command only file does not already exist or
#### if size does not match URL size
cat(download_command)
}
#### 11. finish "...curl_commands.txt" file
Expand Down Expand Up @@ -563,7 +566,7 @@ download_geos <- function(
download_folder_name,
"\n"
)
if (!file.exists(download_folder_name)) {
if (!check_file_size(download_url, download_folder_name)) {
#### cat command only if file does not already exist
cat(download_command)
}
Expand Down Expand Up @@ -715,7 +718,7 @@ download_gmted <- function(
)
download_sink(commands_txt)
#### 13. concatenate and print download command to "..._curl_commands.txt"
if (!file.exists(download_name)) {
if (!check_file_size(download_url, download_name)) {
#### cat command only if file does not already exist
cat(download_command)
}
Expand Down Expand Up @@ -1097,7 +1100,7 @@ download_merra2 <- function(
download_name,
"\n"
)
if (!file.exists(download_name)) {
if (!check_file_size(download_url, download_name)) {
#### cat command only if file does not already exist
cat(download_command)
}
Expand Down Expand Up @@ -1130,7 +1133,7 @@ download_merra2 <- function(
download_name_metadata,
"\n"
)
if (!file.exists(download_name)) {
if (!file.exists(download_name_metadata)) {
#### cat command only if file does not already exist
cat(download_command_metadata)
}
Expand Down Expand Up @@ -1269,8 +1272,9 @@ download_narr <- function(
url,
"\n"
)
if (!file.exists(destfile)) {
#### cat command only if file does not already exist
if (!check_file_size(url = url, file = destfile)) {
#### cat command if file does not already exist or if local file size
#### and the HTTP length (url file size) do not match
cat(command)
}
}
Expand Down Expand Up @@ -1424,7 +1428,7 @@ download_nlcd <- function(
)
download_sink(commands_txt)
#### 12. concatenate and print download command to "..._curl_commands.txt"
if (!file.exists(download_name)) {
if (!check_file_size(download_url, download_name)) {
#### cat command only if file does not already exist
cat(download_command)
}
Expand Down Expand Up @@ -1577,9 +1581,9 @@ download_sedac_groads <- function(
"_curl_command.txt"
)
download_sink(commands_txt)
if (!file.exists(download_name)) {
if (!check_file_size(download_url, download_name)) {
#### 12. concatenate and print download command to "..._curl_commands.txt"
#### cat command only if file does not already exist
#### cat command if file does not already exist or is incomplete
cat(download_command)
}
#### 13. finish "..._curl_commands.txt" file
Expand Down Expand Up @@ -1689,7 +1693,7 @@ download_sedac_population <- function(
#### 7. 30 second resolution not available for all years
if (year == "totpop" && resolution == "30_sec") {
resolution <- "2pt5_min"
cat(paste0(
message(paste0(
"30 second resolution not available for all years. Returning",
" highest (2.5 minute) resolution.\n"
))
Expand All @@ -1702,7 +1706,7 @@ download_sedac_population <- function(
format <- "tif"
} else {
format <- "nc"
cat(paste0(
message(paste0(
"Data for all years is only available in netCDF format. ",
"Data will be downloaded as netCDF.\n"
))
Expand All @@ -1713,7 +1717,7 @@ download_sedac_population <- function(
format <- "asc"
} else {
format <- "nc"
cat(paste0(
message(paste0(
"Data for all years is only available in netCDF format. ",
"Data will be downloaded as netCDF.\n"
))
Expand Down Expand Up @@ -1769,9 +1773,9 @@ download_sedac_population <- function(
"_curl_commands.txt"
)
download_sink(commands_txt)
if (!file.exists(download_name)) {
if (!check_file_size(download_url, download_name)) {
#### 13. concatenate and print download command to "..._curl_commands.txt"
#### cat command only if file does not already exist
#### cat command if file does not already exist or is incomplete
cat(download_command)
}
#### 14. finish "..._curl_commands.txt" file
Expand Down Expand Up @@ -1948,7 +1952,7 @@ download_hms <- function(
url,
"\n"
)
if (!file.exists(destfile)) {
if (!check_file_size(url, destfile)) {
#### cat command only if file does not already exist
cat(command)
}
Expand All @@ -1974,7 +1978,7 @@ download_hms <- function(
#### 13. end if data_format == "KML"
if (data_format == "KML") {
unlink(directory_to_download, recursive = TRUE)
cat(paste0("KML files cannot be unzipped.\n"))
message(paste0("KML files cannot be unzipped.\n"))
return(TRUE)
}
#### 14. unzip downloaded zip files
Expand Down Expand Up @@ -2101,9 +2105,9 @@ download_koppen_geiger <- function(
"_wget_command.txt"
)
download_sink(commands_txt)
if (!file.exists(download_name)) {
if (!check_file_size(download_url, download_name)) {
#### 12. concatenate and print download command to "..._wget_commands.txt"
#### cat command only if file does not already exist
#### cat command if file does not already exist or is incomplete
cat(download_command)
}
sink()
Expand Down Expand Up @@ -2379,7 +2383,10 @@ download_modis <- function(
#### filter commands to non-existing files
download_command <- download_command[
which(
!file.exists(download_name)
!check_file_size(
download_url,
paste0(directory_to_save, download_name)
)
)
]

Expand Down Expand Up @@ -2507,7 +2514,10 @@ download_modis <- function(
#### filter commands to non-existing files
download_command <- download_command[
which(
!file.exists(download_name)
!check_file_size(
download_url,
paste0(directory_to_save, download_name)
)
)
]

Expand Down Expand Up @@ -2605,7 +2615,7 @@ download_tri <- function(
#### filter commands to non-existing files
download_commands <- download_commands[
which(
!file.exists(download_names)
!check_file_size(download_urls, download_names)
)
]
#### 5. initiate "..._curl_commands.txt"
Expand Down Expand Up @@ -2732,12 +2742,6 @@ download_nei <- function(
c("2017neiApr_onroad_byregions.zip",
"2020nei_onroad_byregion.zip")
download_names <- paste0(directory_to_download, download_names_file)
#### filter commands to non-existing files
download_urls <- download_urls[
which(
!file.exists(download_names)
)
]
#### 4. build download command
download_commands <-
paste0("wget --ca-certificate=",
Expand All @@ -2747,7 +2751,12 @@ download_nei <- function(
" -O ",
download_names,
"\n")

#### filter commands to non-existing files
download_commands <- download_commands[
which(
!check_file_size(download_urls, download_names)
)
]
#### 5. initiate "..._curl_commands.txt"
commands_txt <- paste0(
directory_original,
Expand Down Expand Up @@ -3527,7 +3536,7 @@ download_gridmet <- function(
url,
"\n"
)
if (!file.exists(destfile)) {
if (!check_file_size(url, destfile)) {
#### cat command only if file does not already exist
cat(command)
}
Expand Down Expand Up @@ -3667,7 +3676,7 @@ download_terraclimate <- function(
url,
"\n"
)
if (!file.exists(destfile)) {
if (!check_file_size(url, destfile)) {
#### cat command only if file does not already exist
cat(command)
}
Expand Down
Loading

0 comments on commit 5b5be70

Please sign in to comment.