From e97ad9cd390686b9d1e7e7fafa9cd5869a420e12 Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Wed, 22 Nov 2023 15:55:24 +0100 Subject: [PATCH 1/3] Change default parameters and correct case when there is only 1 region --- R/ShapeToMask.R | 33 ++++++++++++++++++--------------- man/ShapeToMask.Rd | 25 ++++++++++++++----------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/R/ShapeToMask.R b/R/ShapeToMask.R index 248775e..3021d3c 100644 --- a/R/ShapeToMask.R +++ b/R/ShapeToMask.R @@ -22,27 +22,30 @@ #' data. Either (1) a netCDF file or (2) a list of lon and lat to provide the #' reference grid points. It is NULL by default. #'@param shp_system A character string containing the Shapefile System Database -#' name. The accepted systems are: 'NUTS', 'LAU', and 'GADM'. It is set to -#' 'NUTS' by default. +#' Name used to subset the shapefile into regions by using parameters 'reg_ids' +#' or 'reg_names'. The accepted systems are: 'NUTS', 'LAU', and 'GADM'. When it +#' is used, you must specify either 'reg_ids' or 'reg_names'; if you don't need +#' to subset different regions, set it to NULL. It is set to NULL by default +#' (optional). #'@param shp_col_name_ids A character string indicating the column name of the -#' column in where the specified 'reg_ids' will be taken. +#' column in where the specified 'reg_ids' will be taken (optional). #'@param reg_ids A character string indicating the unique ID in shapefile. -#' It is NULL by default. +#' It is NULL by default (optional). #'@param reg_names A named list of character string vectors indicating the #' country and the region name. The name of the list stands for the country #' name code and the vector character strings indicate the region name for -#' each country. It is NULL by default. +#' each country. It is NULL by default (optional). #'@param reg_level An integer number from 1 to 3 indicating the 'NUTS' dataset #' level. For other datasets this parameter is not used. One mask can only have -#' a unique level. It is set to 3 by default. +#' a unique level. It is set to 3 by default (optional). #'@param lat_dim A character string indicating the latitudinal dimension. If it #' is NULL, the latitudinal name will be searched using an internal function #' with the following possible names: 'lat', 'latitude', 'y', 'j' and -#' 'nav_lat'. It is NULL by default. +#' 'nav_lat'. It is set to 'latitude' by default. #'@param lon_dim A character string indicating the longitudinal dimension. If it #' is NULL, the longitudinal name will be searched using an internal function #' with the following possible names: 'lon', 'longitude', 'x', 'i' and -#' 'nav_lon'. +#' 'nav_lon'. It is set to 'longitude' by default. #'@param target_crs A character string indicating the target 'Coordinate #' Reference System'. #'@param region A logical value indicating if we want a dimension for the @@ -79,9 +82,9 @@ #'@importFrom doParallel registerDoParallel #'@export ShapeToMask <- function(shp_file, ref_grid, - shp_system = "NUTS", reg_names = NULL, - reg_ids = NULL, shp_col_name_ids = NULL, - reg_level = 3, lat_dim = NULL, lon_dim = NULL, + shp_system = NULL, reg_names = NULL, reg_ids = NULL, + shp_col_name_ids = NULL, reg_level = 3, + lat_dim = 'latitude', lon_dim = 'longitude', region = FALSE, target_crs = NULL, check_valid = FALSE, find_min_dist = FALSE, max_dist = 50, ncores = NULL, ...) { @@ -143,11 +146,10 @@ ShapeToMask <- function(shp_file, ref_grid, stop("Parameter 'shp_system' must be a character strinig.") } if (all(is.null(reg_ids), is.null(reg_names))) { - stop("Either provide parameter 'reg_ids' or 'reg_names'.") + stop("If 'shp_system' is used, you must provide either parameter ", + "'reg_ids' or 'reg_names'.") } - } else if (is.null(shp_col_name_ids)) { - stop("Either provide parameter 'shp_system' or 'shp_col_name_ids'.") - } else { + } else if (!is.null(shp_col_name_ids)) { if (is.null(reg_ids)) { stop("If 'shp_col_name_ids' is used, parameter 'reg_ids' must be provided.") } @@ -331,6 +333,7 @@ ShapeToMask <- function(shp_file, ref_grid, registerDoSEQ() } if (region) { + if (length(dim(mask)) == 2) dim(mask) <- c(dim(mask), 1) names(dim(mask)) <- c(lon_dim, lat_dim, 'region') } else { names(dim(mask)) <- c(lon_dim, lat_dim) diff --git a/man/ShapeToMask.Rd b/man/ShapeToMask.Rd index f5beede..4730817 100644 --- a/man/ShapeToMask.Rd +++ b/man/ShapeToMask.Rd @@ -7,13 +7,13 @@ ShapeToMask( shp_file, ref_grid, - shp_system = "NUTS", + shp_system = NULL, reg_names = NULL, reg_ids = NULL, shp_col_name_ids = NULL, reg_level = 3, - lat_dim = NULL, - lon_dim = NULL, + lat_dim = "latitude", + lon_dim = "longitude", region = FALSE, target_crs = NULL, check_valid = FALSE, @@ -31,33 +31,36 @@ data. Either (1) a netCDF file or (2) a list of lon and lat to provide the reference grid points. It is NULL by default.} \item{shp_system}{A character string containing the Shapefile System Database -name. The accepted systems are: 'NUTS', 'LAU', and 'GADM'. It is set to -'NUTS' by default.} +Name used to subset the shapefile into regions by using parameters 'reg_ids' +or 'reg_names'. The accepted systems are: 'NUTS', 'LAU', and 'GADM'. When it +is used, you must specify either 'reg_ids' or 'reg_names'; if you don't need +to subset different regions, set it to NULL. It is set to NULL by default +(optional).} \item{reg_names}{A named list of character string vectors indicating the country and the region name. The name of the list stands for the country name code and the vector character strings indicate the region name for -each country. It is NULL by default.} +each country. It is NULL by default (optional).} \item{reg_ids}{A character string indicating the unique ID in shapefile. -It is NULL by default.} +It is NULL by default (optional).} \item{shp_col_name_ids}{A character string indicating the column name of the -column in where the specified 'reg_ids' will be taken.} +column in where the specified 'reg_ids' will be taken (optional).} \item{reg_level}{An integer number from 1 to 3 indicating the 'NUTS' dataset level. For other datasets this parameter is not used. One mask can only have -a unique level. It is set to 3 by default.} +a unique level. It is set to 3 by default (optional).} \item{lat_dim}{A character string indicating the latitudinal dimension. If it is NULL, the latitudinal name will be searched using an internal function with the following possible names: 'lat', 'latitude', 'y', 'j' and -'nav_lat'. It is NULL by default.} +'nav_lat'. It is set to 'latitude' by default.} \item{lon_dim}{A character string indicating the longitudinal dimension. If it is NULL, the longitudinal name will be searched using an internal function with the following possible names: 'lon', 'longitude', 'x', 'i' and -'nav_lon'.} +'nav_lon'. It is set to 'longitude' by default.} \item{region}{A logical value indicating if we want a dimension for the regions in the resulting mask array. It is FALSE by default.} -- GitLab From 594e7e222843889b61881e317c68751b4c884a1a Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Wed, 22 Nov 2023 16:10:51 +0100 Subject: [PATCH 2/3] Set default values as before --- R/ShapeToMask.R | 10 +++++----- tests/testthat/test-ShapeToMask.R | 5 +++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/R/ShapeToMask.R b/R/ShapeToMask.R index 3021d3c..350e38b 100644 --- a/R/ShapeToMask.R +++ b/R/ShapeToMask.R @@ -25,7 +25,7 @@ #' Name used to subset the shapefile into regions by using parameters 'reg_ids' #' or 'reg_names'. The accepted systems are: 'NUTS', 'LAU', and 'GADM'. When it #' is used, you must specify either 'reg_ids' or 'reg_names'; if you don't need -#' to subset different regions, set it to NULL. It is set to NULL by default +#' to subset different regions, set it to NULL. It is set to "NUTS" by default #' (optional). #'@param shp_col_name_ids A character string indicating the column name of the #' column in where the specified 'reg_ids' will be taken (optional). @@ -41,11 +41,11 @@ #'@param lat_dim A character string indicating the latitudinal dimension. If it #' is NULL, the latitudinal name will be searched using an internal function #' with the following possible names: 'lat', 'latitude', 'y', 'j' and -#' 'nav_lat'. It is set to 'latitude' by default. +#' 'nav_lat'. It is set to NULL by default. #'@param lon_dim A character string indicating the longitudinal dimension. If it #' is NULL, the longitudinal name will be searched using an internal function #' with the following possible names: 'lon', 'longitude', 'x', 'i' and -#' 'nav_lon'. It is set to 'longitude' by default. +#' 'nav_lon'. It is set to NULL by default. #'@param target_crs A character string indicating the target 'Coordinate #' Reference System'. #'@param region A logical value indicating if we want a dimension for the @@ -82,9 +82,9 @@ #'@importFrom doParallel registerDoParallel #'@export ShapeToMask <- function(shp_file, ref_grid, - shp_system = NULL, reg_names = NULL, reg_ids = NULL, + shp_system = "NUTS", reg_names = NULL, reg_ids = NULL, shp_col_name_ids = NULL, reg_level = 3, - lat_dim = 'latitude', lon_dim = 'longitude', + lat_dim = NULL, lon_dim = NULL, region = FALSE, target_crs = NULL, check_valid = FALSE, find_min_dist = FALSE, max_dist = 50, ncores = NULL, ...) { diff --git a/tests/testthat/test-ShapeToMask.R b/tests/testthat/test-ShapeToMask.R index 0217da5..550f2ee 100644 --- a/tests/testthat/test-ShapeToMask.R +++ b/tests/testthat/test-ShapeToMask.R @@ -87,6 +87,11 @@ test_that("2. Output", { sum(mask3), 105 ) + # Test a single region + mask3_1 <- ShapeToMask(shp_file1, ref_grid = ref_grid1_1, + shp_col_name_ids = "NUTS_ID", lon_dim = 'lon', + lat_dim = 'lat', reg_ids = "DE149", region = TRUE) + # Test GADM mask4 <- ShapeToMask(shp_file = shp_file2, ref_grid = ref_grid2, reg_ids = GADM_id2, shp_system = "GADM") expect_equal( -- GitLab From 357711ea36ed25c5cde92acd0b3cd0c1bf36e02a Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Wed, 22 Nov 2023 16:12:42 +0100 Subject: [PATCH 3/3] Regernerate documentation --- man/ShapeToMask.Rd | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/man/ShapeToMask.Rd b/man/ShapeToMask.Rd index 4730817..5f4a660 100644 --- a/man/ShapeToMask.Rd +++ b/man/ShapeToMask.Rd @@ -7,13 +7,13 @@ ShapeToMask( shp_file, ref_grid, - shp_system = NULL, + shp_system = "NUTS", reg_names = NULL, reg_ids = NULL, shp_col_name_ids = NULL, reg_level = 3, - lat_dim = "latitude", - lon_dim = "longitude", + lat_dim = NULL, + lon_dim = NULL, region = FALSE, target_crs = NULL, check_valid = FALSE, @@ -34,7 +34,7 @@ reference grid points. It is NULL by default.} Name used to subset the shapefile into regions by using parameters 'reg_ids' or 'reg_names'. The accepted systems are: 'NUTS', 'LAU', and 'GADM'. When it is used, you must specify either 'reg_ids' or 'reg_names'; if you don't need -to subset different regions, set it to NULL. It is set to NULL by default +to subset different regions, set it to NULL. It is set to "NUTS" by default (optional).} \item{reg_names}{A named list of character string vectors indicating the @@ -55,12 +55,12 @@ a unique level. It is set to 3 by default (optional).} \item{lat_dim}{A character string indicating the latitudinal dimension. If it is NULL, the latitudinal name will be searched using an internal function with the following possible names: 'lat', 'latitude', 'y', 'j' and -'nav_lat'. It is set to 'latitude' by default.} +'nav_lat'. It is set to NULL by default.} \item{lon_dim}{A character string indicating the longitudinal dimension. If it is NULL, the longitudinal name will be searched using an internal function with the following possible names: 'lon', 'longitude', 'x', 'i' and -'nav_lon'. It is set to 'longitude' by default.} +'nav_lon'. It is set to NULL by default.} \item{region}{A logical value indicating if we want a dimension for the regions in the resulting mask array. It is FALSE by default.} -- GitLab