-
Notifications
You must be signed in to change notification settings - Fork 1
Optimization for Large Files #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,9 @@ | |||||||||||||||||||||||||||||||||
| #' @export | ||||||||||||||||||||||||||||||||||
| convert_2d_longitude_gridded <- function(data.in, write.out = FALSE, output.files = NA) { | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Boost terra memory limit for in-memory processing fallback | ||||||||||||||||||||||||||||||||||
| terra::terraOptions(memfrac = 0.8) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| # 1. Standardized input coercion block | ||||||||||||||||||||||||||||||||||
| data.ls = EDABUtilities:::import_data(data.in) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Comment on lines
18
to
19
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
@@ -23,18 +26,59 @@ convert_2d_longitude_gridded <- function(data.in, write.out = FALSE, output.file | |||||||||||||||||||||||||||||||||
| out.ls <- lapply(seq_along(data.ls), function(i) { | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| current_data <- data.ls[[i]] | ||||||||||||||||||||||||||||||||||
| is_file_input <- is.character(current_data) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| # Fast path: File-to-file transformation via GDAL (only for rasters written to disk) | ||||||||||||||||||||||||||||||||||
| if (is_file_input && write.out) { | ||||||||||||||||||||||||||||||||||
| if (!file.exists(current_data)) stop(paste("File does not exist:", current_data)) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| # Quickly read extent without loading data into memory | ||||||||||||||||||||||||||||||||||
| temp_rast <- terra::rast(current_data) | ||||||||||||||||||||||||||||||||||
| dat.ext <- terra::ext(temp_rast) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| out_dir <- dirname(output.files[i]) | ||||||||||||||||||||||||||||||||||
| if (!dir.exists(out_dir)) dir.create(out_dir, recursive = TRUE) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (dat.ext[1] >= -0.001 && dat.ext[2] <= 360.001 && dat.ext[2] > 180.001) { | ||||||||||||||||||||||||||||||||||
| message("Detected longitude range approx 0-360. Fast processing via GDAL warp...") | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Comment on lines
+40
to
+44
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| # Perform fast GDAL extent shift | ||||||||||||||||||||||||||||||||||
| sf::gdal_utils( | ||||||||||||||||||||||||||||||||||
| util = "warp", | ||||||||||||||||||||||||||||||||||
| source = current_data, | ||||||||||||||||||||||||||||||||||
| destination = output.files[i], | ||||||||||||||||||||||||||||||||||
| options = c( | ||||||||||||||||||||||||||||||||||
| "-t_srs", "EPSG:4326", | ||||||||||||||||||||||||||||||||||
| "-te", "-180", "-90", "180", "90", | ||||||||||||||||||||||||||||||||||
| "-wo", "SOURCE_EXTRA=1000", | ||||||||||||||||||||||||||||||||||
| "--config", "CENTER_LONG", "0" | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+51
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| return(terra::rast(output.files[i])) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| } else if (!(dat.ext[1] >= -180.001 && dat.ext[2] <= 180.001)) { | ||||||||||||||||||||||||||||||||||
| stop("Longitude out of range. Extent is outside expected boundaries.") | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| message("Already standard format (-180:180). Copying file.") | ||||||||||||||||||||||||||||||||||
| file.copy(current_data, output.files[i], overwrite = TRUE) | ||||||||||||||||||||||||||||||||||
| return(terra::rast(output.files[i])) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| # --------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| # Fallback path: In-memory terra processing | ||||||||||||||||||||||||||||||||||
| # (used if input is already an object, or write.out is FALSE) | ||||||||||||||||||||||||||||||||||
| # --------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| # Safe coercion with file validation | ||||||||||||||||||||||||||||||||||
| if (is.character(current_data)) { | ||||||||||||||||||||||||||||||||||
| if (is_file_input) { | ||||||||||||||||||||||||||||||||||
| if (!file.exists(current_data)) stop(paste("File does not exist:", current_data)) | ||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| current_data <- current_data |> terra::rast() | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| dat.ext <- current_data |> terra::ext() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| # Spatial logic checks without redundant variables | ||||||||||||||||||||||||||||||||||
| if (dat.ext[1] >= -0.001 && dat.ext[2] <= 360.001 && dat.ext[2] > 180.001) { | ||||||||||||||||||||||||||||||||||
| message("Detected longitude range approximately 0-360. Converting to -180 to +180.") | ||||||||||||||||||||||||||||||||||
| message("Detected longitude range approx 0-360. Converting to -180 to +180 via terra::rotate.") | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Comment on lines
+81
to
82
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
| if (!terra::is.lonlat(current_data)) { | ||||||||||||||||||||||||||||||||||
| warning("Object does not have a standard geographic (lat/lon) CRS. Rotation may fail or shift bounds unexpectedly.") | ||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,82 +15,114 @@ | |||||||||||||||||||||||||||||||||||||||||||||
| crop_nc_2d <- function(data.in, shp.file, var.name, area.names = NA, write.out = FALSE, output.files = NULL) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
15
to
16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| # Data Input Standardization | ||||||||||||||||||||||||||||||||||||||||||||||
| data.ls = EDABUtilities:::import_data(data.in) | ||||||||||||||||||||||||||||||||||||||||||||||
| data.ls = EDABUtilities:::import_data(data.in) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+18
to
19
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| # Spatial Input Standardization | ||||||||||||||||||||||||||||||||||||||||||||||
| shp.vect = EDABUtilities:::import_shp(shp.file) | ||||||||||||||||||||||||||||||||||||||||||||||
| use.shp = ifelse(class(shp.vect) == 'SpatVector',T,F) | ||||||||||||||||||||||||||||||||||||||||||||||
| shp.vect = EDABUtilities:::import_shp(shp.file) | ||||||||||||||||||||||||||||||||||||||||||||||
| use.shp = ifelse(class(shp.vect) == 'SpatVector',T,F) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (!use.shp) stop("A valid shp.file must be provided.") | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!use.shp) stop("A valid shp.file must be provided.") | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+21
to
25
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| # Optimized Area Names Filtering | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!is.null(area.names) && !all(is.na(area.names))) { | ||||||||||||||||||||||||||||||||||||||||||||||
| shp.str <- as.data.frame(shp.vect) | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!is.null(area.names) && !all(is.na(area.names))) { | ||||||||||||||||||||||||||||||||||||||||||||||
| shp.str <- as.data.frame(shp.vect) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # Safely find the first column that contains the requested area.names | ||||||||||||||||||||||||||||||||||||||||||||||
| target_col <- NULL | ||||||||||||||||||||||||||||||||||||||||||||||
| for (col in names(shp.str)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| target_col <- NULL | ||||||||||||||||||||||||||||||||||||||||||||||
| for (col in names(shp.str)) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+27
to
+31
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (all(area.names %in% shp.str[[col]])) { | ||||||||||||||||||||||||||||||||||||||||||||||
| target_col <- col | ||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||
| target_col <- col | ||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (is.null(target_col)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| stop("None of the attributes in shp.file contain all specified area.names.") | ||||||||||||||||||||||||||||||||||||||||||||||
| if (is.null(target_col)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| stop("None of the attributes in shp.file contain all specified area.names.") | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
37
to
+39
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # Subset vector directly using terra logic | ||||||||||||||||||||||||||||||||||||||||||||||
| shp.vect <- shp.vect[shp.vect[[target_col]] %in% area.names, ] | ||||||||||||||||||||||||||||||||||||||||||||||
| shp.vect <- shp.vect[shp.vect[[target_col]][,1] %in% area.names , ] | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
41
to
+42
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| out.ls <- list() | ||||||||||||||||||||||||||||||||||||||||||||||
| out.ls <- list() | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| for (i in seq_along(data.ls)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| for (i in seq_along(data.ls)) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (is.character(data.ls[[i]])) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!file.exists(data.ls[[i]])) stop(sprintf("File does not exist: %s", data.ls[[i]])) | ||||||||||||||||||||||||||||||||||||||||||||||
| data.orig <- terra::rast(data.ls[[i]]) | ||||||||||||||||||||||||||||||||||||||||||||||
| if (is.character(data.ls[[i]])) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!file.exists(data.ls[[i]])) stop(sprintf("File does not exist: %s", data.ls[[i]])) | ||||||||||||||||||||||||||||||||||||||||||||||
| data.orig <- terra::rast(data.ls[[i]]) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
44
to
+51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| data.orig <- data.ls[[i]] | ||||||||||||||||||||||||||||||||||||||||||||||
| data.orig <- data.ls[[i]] | ||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| #Check CRS | ||||||||||||||||||||||||||||||||||||||||||||||
| data.crs = terra::crs(data.orig) | ||||||||||||||||||||||||||||||||||||||||||||||
| shp.crs = terra::crs(shp.vect) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| data.orig <- EDABUtilities::convert_2d_longitude_gridded(data.orig)[[1]] | ||||||||||||||||||||||||||||||||||||||||||||||
| if(!identical(data.crs,shp.crs)){ | ||||||||||||||||||||||||||||||||||||||||||||||
| shp.vect = terra::project(shp.vect,data.crs) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+56
to
+60
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| # --------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| # 1. Fast Vector Shift (Align Shapefile to Raster) | ||||||||||||||||||||||||||||||||||||||||||||||
| # --------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| e_rast <- as.vector(terra::ext(data.orig)) | ||||||||||||||||||||||||||||||||||||||||||||||
| shp_crop <- shp.vect | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+66
to
68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| # Edge Case Handling: Numeric Extent intersection check (avoids SpatExtent class mismatch) | ||||||||||||||||||||||||||||||||||||||||||||||
| e1 <- as.vector(terra::ext(data.orig)) | ||||||||||||||||||||||||||||||||||||||||||||||
| e2 <- as.vector(terra::ext(shp.vect)) | ||||||||||||||||||||||||||||||||||||||||||||||
| # If raster is 0-360 but shapefile has negative longitudes | ||||||||||||||||||||||||||||||||||||||||||||||
| if (e_rast["xmax"] > 180.001 && any(terra::ext(shp_crop)[1:2] < 0)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| # Shift the shapefile geometry 360 degrees East so it overlays on the 0-360 raster | ||||||||||||||||||||||||||||||||||||||||||||||
| shp_crop <- terra::rotate(shp_crop, long = 0,split = T, left = F) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| #If raster is -180 to 180 but shapefile is 0-360 | ||||||||||||||||||||||||||||||||||||||||||||||
| } else if (e_rast["xmax"] <= 180.001 && any(terra::ext(shp_crop)[1:2] > 180)) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| # Shift the shapefile geometry 360 degrees West | ||||||||||||||||||||||||||||||||||||||||||||||
| shp_crop <- terra::rotate(shp_crop) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| # Bounding boxes intersect if they overlap on both axes | ||||||||||||||||||||||||||||||||||||||||||||||
| intersects <- (e1["xmin"] <= e2["xmax"]) && (e1["xmax"] >= e2["xmin"]) && | ||||||||||||||||||||||||||||||||||||||||||||||
| (e1["ymin"] <= e2["ymax"]) && (e1["ymax"] >= e2["ymin"]) | ||||||||||||||||||||||||||||||||||||||||||||||
| # --------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| # 2. Intersection Check & Cropping | ||||||||||||||||||||||||||||||||||||||||||||||
| # --------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| # Edge Case Handling: Numeric Extent intersection check (avoids SpatExtent class mismatch)[cite: 2] | ||||||||||||||||||||||||||||||||||||||||||||||
| e1 <- as.vector(terra::ext(data.orig)) | ||||||||||||||||||||||||||||||||||||||||||||||
| e2 <- as.vector(terra::ext(shp_crop)) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+83
to
85
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (!intersects) { | ||||||||||||||||||||||||||||||||||||||||||||||
| warning(sprintf("Data extent and shapefile extent do not intersect for item %s. Skipping.", i)) | ||||||||||||||||||||||||||||||||||||||||||||||
| next | ||||||||||||||||||||||||||||||||||||||||||||||
| # Bounding boxes intersect if they overlap on both axes[cite: 2] | ||||||||||||||||||||||||||||||||||||||||||||||
| intersects <- (e1["xmin"] <= e2["xmax"]) && (e1["xmax"] >= e2["xmin"]) && | ||||||||||||||||||||||||||||||||||||||||||||||
| (e1["ymin"] <= e2["ymax"]) && (e1["ymax"] >= e2["ymin"]) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (!intersects) { | ||||||||||||||||||||||||||||||||||||||||||||||
| warning(sprintf("Data extent and shapefile extent do not intersect for item %s. Skipping.", i)) | ||||||||||||||||||||||||||||||||||||||||||||||
| next | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+87
to
+92
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| data.crop <- terra::crop(data.orig, shp.vect) | ||||||||||||||||||||||||||||||||||||||||||||||
| # Perform the blazing fast crop on the un-rotated raster | ||||||||||||||||||||||||||||||||||||||||||||||
| data.crop <- terra::crop(data.orig, shp_crop) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+96
to
+97
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| # --------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| # 3. Post-Crop Standardize | ||||||||||||||||||||||||||||||||||||||||||||||
| # --------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| # Convert longitude on the tiny cropped raster instead of the massive global one | ||||||||||||||||||||||||||||||||||||||||||||||
| data.crop <- EDABUtilities::convert_2d_longitude_gridded(data.crop)[[1]] | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (write.out) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (is.null(output.files) || length(output.files) != length(data.ls)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| stop("output.files must be provided and match the length of data.in when write.out is TRUE.") | ||||||||||||||||||||||||||||||||||||||||||||||
| # --------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| # 4. Write Output | ||||||||||||||||||||||||||||||||||||||||||||||
| # --------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| if (write.out) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (is.null(output.files) || length(output.files) != length(data.ls)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| stop("output.files must be provided and match the length of data.in when write.out is TRUE.") | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+107
to
+109
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| out_dir <- dirname(output.files[i]) | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!dir.exists(out_dir)) dir.create(out_dir, recursive = TRUE) | ||||||||||||||||||||||||||||||||||||||||||||||
| out_dir <- dirname(output.files[i]) | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!dir.exists(out_dir)) dir.create(out_dir, recursive = TRUE) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| terra::writeCDF(data.crop, output.files[i], varname = var.name, overwrite = TRUE) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
111
to
115
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| out.ls[[i]] <- data.crop | ||||||||||||||||||||||||||||||||||||||||||||||
| out.ls[[i]] <- data.crop | ||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (write.out == FALSE) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (is.character(data.in)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| names(out.ls) <- basename(data.in) | ||||||||||||||||||||||||||||||||||||||||||||||
| if (write.out == FALSE) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (is.character(data.in)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| names(out.ls) <- basename(data.in) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
120
to
+123
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| names(out.ls) <- paste0("layer_", seq_along(out.ls)) | ||||||||||||||||||||||||||||||||||||||||||||||
| names(out.ls) <- paste0("layer_", seq_along(out.ls)) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| return(out.ls) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,11 +3,12 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' This function extracts spatial raster data across specified shapefile regions and aggregates it temporally to produce timeseries summary statistics. It processes inputs by grouping them (e.g., aggregating daily layers into annual time series) and outputs either a list of summarized data frames or writes RDS files directly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' @param data.in character vector, list, or SpatRaster. Single file path, vector of file paths, single SpatRaster, or list of SpatRasters representing the spatial data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' @param var.name character. Name of variable to be subseted from dataset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' @return list of spatRasters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import_data = function(data.in){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import_data = function(data.in, var.name = NULL){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (inherits(data.in, "SpatRaster")|inherits(data.in, "SpatRasterDataset")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+11
to
13
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data.ls <- list(data.in) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -23,5 +24,25 @@ import_data = function(data.in){ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stop("data.in must be a file path, a vector of file paths, a single SpatRaster, or a list of SpatRasters.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(!is.null(var.name)){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data.ls = lapply(data.ls,function(x){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data.varname = terra::varnames(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(length(data.varname > 1)){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data = x[[terra::varnames(x) == var.name]] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }else{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data.names = terra::names(data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data.names = data.names[grepl(var.name,data.names)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(length(data.names) ==0){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| warning('NetCDF file does not contain any fields with var.name=',var.name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }else{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data = subset(x,data.names) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
26
to
+41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return(data.ls) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -49,7 +49,7 @@ make_2d_anomaly_gridded <- function(data.in, climatology, var.name, shp.file = N | |||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
| # Pre-mask climatology once to avoid doing it N times inside the loop | ||||||||||||||||||
| climatology <- terra::mask(climatology[[1]], shp.vect) | ||||||||||||||||||
| climatology <- EDABUtilities::crop_nc_2d(climatology[[1]], shp.file = shp.vect,area.names = area.names,var.name = var.name )[[1]] | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
| out.ls <- list() | ||||||||||||||||||
|
|
@@ -70,7 +70,7 @@ make_2d_anomaly_gridded <- function(data.in, climatology, var.name, shp.file = N | |||||||||||||||||
|
|
||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
| # Align extents and resolutions if mismatched | ||||||||||||||||||
| if (!(all(terra::res(data) == terra::res(climatology)) && all(terra::ext(data) == terra::ext(climatology)))) { | ||||||||||||||||||
| climatology <- terra::crop(climatology,data) | ||||||||||||||||||
| climatology <- terra::crop(climatology[[1]],data) | ||||||||||||||||||
|
Comment on lines
72
to
+73
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
| data <- terra::crop(terra::mask(data, climatology), climatology) | ||||||||||||||||||
| data <- terra::resample(data, climatology) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -63,7 +63,8 @@ make_2d_climatology_gridded <- function(data.in, var.name, agg.time, statistic, | |||
|
|
||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||
| # 3. OPTIMIZATION: Mask significantly fewer aggregated subset layers | ||||
| if (use.shp) { | ||||
| data.subset <- terra::mask(data.subset, shp.vect) | ||||
|
|
||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||
| data.subset <- EDABUtilities::crop_nc_2d(data.subset, shp.vect)[[1]] | ||||
| } | ||||
|
|
||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||
| data.time.agg.ls[[i]] <- data.subset | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -97,7 +97,7 @@ make_2d_deg_day_gridded <- function(data.in, var.name, metric, ref.value, type, | |||
|
|
||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||
| # OPTIMIZATION: Apply spatial mask on the final single aggregated layer | ||||
| if (use.shp) { | ||||
| data.out <- terra::mask(data.out, shp.vect) | ||||
| data.out <- EDABUtilities::crop_nc_2d(data.out, shp.vect)[[1]] | ||||
| } | ||||
|
|
||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||
| if (write.out) { | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -72,7 +72,8 @@ make_2d_summary_gridded <- function(data.in, var.name, statistics, agg.time, fil | |||||||||||||||
|
|
||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||
| # OPTIMIZATION: Pre-crop to bounding box once to reduce spatial memory footprint immediately | ||||||||||||||||
| if (use.shp) { | ||||||||||||||||
| data <- terra::crop(data, shp.vect) | ||||||||||||||||
| data <- EDABUtilities::crop_nc_2d(data, shp.file = shp.vect,area.names = area.names,var.name = var.name )[[1]] | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||
| # data <- terra::crop(data, shp.vect) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||
| data.stat.ls <- list() | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -94,6 +94,7 @@ make_2d_summary_ts <- function(data.in, var.name, statistics, agg.time, file.tim | |||||||||
| stop('monthly files not yet implemented') | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||
|
|
||||||||||
| if(terra::crs(data) != terra::crs(shp.vect)){ | ||||||||||
| data = terra::project(data, terra::crs(shp.vect)) | ||||||||||
|
Comment on lines
98
to
99
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||
| } | ||||||||||
|
|
@@ -121,7 +122,7 @@ make_2d_summary_ts <- function(data.in, var.name, statistics, agg.time, file.tim | |||||||||
| } | ||||||||||
|
|
||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||
| # OPTIMIZATION: Crop immediately to shapefile bounding box before ANY iterations | ||||||||||
| data <- terra::crop(data, shp.vect) | ||||||||||
| data <- EDABUtilities::crop_nc_2d(data, shp.vect)[[1]] | ||||||||||
|
|
||||||||||
| # OPTIMIZATION: Pull terra::tapp entirely out of the area loop. | ||||||||||
|
Comment on lines
126
to
127
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||
| # Execute once per statistics across the master clipped extent. | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -62,7 +62,8 @@ mask_nc_2d <- function(data.in, var.name, min.value, max.value, write.out = FALS | |||||||||||||||
| data <- if (is.character(item)) terra::rast(item) else item | ||||||||||||||||
|
|
||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||
| if (use.shp) { | ||||||||||||||||
| data <- terra::crop(data, shp.vect) | ||||||||||||||||
| data <- EDABUtilities::crop_nc_2d(data, shp.file = shp.vect,area.names = area.names,var.name = var.name )[[1]] | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||
| # data <- terra::crop(data, shp.vect) | ||||||||||||||||
| data <- terra::mask(data, shp.vect) | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[air] reported by reviewdog 🐶