Commit ecc5817b authored by Conor Anderson's avatar Conor Anderson

Better error detection in cropncdf

parent 4fab42b5
......@@ -419,10 +419,12 @@ function(req, res){
tdir <- tempdir()
shp_bak <- request$shp
errors <- NULL
for (row in 1:nrow(request$meta)) {
shp <- shp_bak
message("Working on: ", paste(unlist(dplyr::select(request$meta, Model:End)[row,]), collapse = " "))
working_on <- paste(unlist(dplyr::select(request$meta, Model:End)[row,]), collapse = " ")
message("Working on: ", working_on)
filenames <- request$meta$Files[[row]]
components <- tibble::as_tibble(t(sapply(filenames, get.split.filename.cmip)))
......@@ -434,8 +436,17 @@ function(req, res){
var_details <- nc_nc$var[[var]]
ncdf4::nc_close(nc_nc)
bricks <- lapply(filenames, function(x) raster::brick(file.path(file_dir, cmip, var, "verified", x)))
st <- do.call(raster::stack, bricks)
bricks <- try(lapply(filenames, function(x) raster::brick(file.path(file_dir, cmip, var, "verified", x))))
if (inherits(bricks, "try-error")) {
errors <- c(errors, paste("Error reading", working_on))
next
}
st <- try(do.call(raster::stack, bricks))
if (inherits(st, "try-error")) {
errors <- c(errors, paste("Error stacking", working_on))
next
}
rm(bricks); invisible(gc())
pstart <- as.character(request$period[1])
......@@ -450,9 +461,17 @@ function(req, res){
message("Working with a shapefile")
if (attr(raster::extent(st), "xmax") > 180) shp <- sp::recenter(shp)
if (!identical(raster::crs(shp), raster::crs(st))) shp <- sp::spTransform(shp, raster::crs(st))
st.crop <- raster::crop(st, shp)
st.crop <- try(raster::crop(st, shp))
if (inherits(st.crop, "try-error")) {
errors <- c(errors, paste("Error cropping", working_on))
next
}
rm(st); invisible(gc())
st.out = raster::mask(st.crop, shp)
st.out = try(raster::mask(st.crop, shp))
if (inherits(st.out, "try-error")) {
errors <- c(errors, paste("Error masking", working_on))
next
}
rm(st.crop); invisible(gc())
} else {
stop("The shp object is an unexpected type")
......@@ -482,36 +501,26 @@ function(req, res){
r <- httr::PUT(link,
body = httr::upload_file(writeout),
httr::authenticate(dav_user, dav_pass))
if (r$status_code == 201) {
message("File uploaded successfully")
unlink(writeout)
} else {
if (exists("errors")) {
errors <- c(errors, fileout)
} else {
errors <- fileout
}
errors <- c(errors, paste("Error uploading", working_on))
unlink(writeout)
}
} else {
message("Error writing ", writeout)
if (exists("errors")) {
errors <- c(errors, fileout)
} else {
errors <- fileout
}
errors <- c(errors, paste("Error writing", working_on))
}
}
if (exists("errors") && length(errors) == nrow(request$meta)) {
body <- "We are sorry to say that we encountered errors processing your <strong>Conjuntool</strong> data request. Please try again later, or contact the author for help."
if (!is.null(errors) && length(errors) == nrow(request$meta)) {
body <- "We are sorry to say that we encountered errors processing your <strong>Conjuntool</strong> data request. Please contact the author for help."
} else {
body <- paste("We did it! Your <strong>Conjuntool</strong> data request should be available at:<br>", file.path(dav_root, user_folder, request_time))
if (exists("errors")) {
body <- paste(body, "<br><br>Please note that we encountered errors processing the following files:<br><br>",
paste0(errors,"<br>"),
"<br>This seems to happen with the cropped area for a model is very small.")
body <- paste(body, "<br><br>Please note that we encountered the following errors:<br><br>",
paste0(errors,"<br>"))
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment