From 4b8bb689fee24b34fed829e8333cecf6883bd654 Mon Sep 17 00:00:00 2001 From: peterson-tim-j Date: Sat, 15 Feb 2025 18:54:19 +1100 Subject: [PATCH] Check added for non-NA catchment IDs and being unique --- DESCRIPTION | 2 +- R/extractCatchmentData.R | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 4495cd5..aef3407 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,5 +16,5 @@ URL: https://github.com/peterson-tim-j/AWAPer License: GPL-3 Encoding: UTF-8 LazyData: true -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.2 SystemRequirements: 7z (Windows only) diff --git a/R/extractCatchmentData.R b/R/extractCatchmentData.R index a30ef29..a8e40ad 100644 --- a/R/extractCatchmentData.R +++ b/R/extractCatchmentData.R @@ -48,7 +48,7 @@ #' @param DEM is either the full file name to a ESRI ASCII grid (as lat/long and using GDA94) or a raster class grid object. The DEM is used #' for the calculation of Morton's PET. The Australian 9 second DEM can be loaded using \code{getDEM()}. #' @param catchments is either the full file name to an ESRI shape file of points or polygons (latter assumed to be catchment boundaries) or a shape file -#' already imported using readShapeSpatial(). Either way the shape file must be in long/lat (i.e. not projected), use the ellipsoid GRS 80, and the first column should be a unique ID. +#' already imported using readShapeSpatial(). Either way the shape file must be in long/lat (i.e. not projected), use the ellipsoid GRS 80, and the first column must be a unique ID. #' @param temporal.timestep character string for the time step of the output data. The options are \code{daily}, \code{weekly}, \code{monthly}, \code{quarterly}, #' \code{annual} or a user-defined index for, say, water-years (see \code{xts::period.apply}). The default is \code{daily}. #' @param temporal.function.name character string for the function name applied to aggregate the daily data to \code{temporal.timestep}. @@ -311,6 +311,12 @@ extractCatchmentData <- function( catchments = sp::spTransform(catchments,sp::CRS('+proj=longlat +ellps=GRS80')) } + # Check each catchment or point has a unique (non-NA) ID. Note. + if (any(is.na(catchments[[1]]))) + stop('The list of catchments IDs (first column) contains NAs. Please remove these or rename') + if ( length(unique(catchments[[1]])) != length(catchments[[1]]) ) + stop('The list of catchments IDs (first column) is not unique. Please remove the duplicates') + # Check if catchments are points or a polygon. isCatchmentsPolygon=TRUE; if (methods::is(catchments,"SpatialPointsDataFrame")) { @@ -407,8 +413,6 @@ extractCatchmentData <- function( # Recalculate the time points to extract. timepoints2Extract = seq( as.Date(extractFrom,'%Y-%m-%d'), by="day", to=as.Date(extractTo,'%Y-%m-%d')) - - message(paste(' Data will be extracted from ',format.Date(extractFrom,'%Y-%m-%d'),' to ', format.Date(extractTo,'%Y-%m-%d'),' at ',length(catchments),' catchments ')); message('Starting data extraction:')