Skip to content

Commit

Permalink
More informative errors for vrt
Browse files Browse the repository at this point in the history
  • Loading branch information
dramanica committed Sep 5, 2024
1 parent f364021 commit 2c72b29
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions R/vrt_get_meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
#' @param vrt_path path to the XML file defining the vrt dataset
#' @returns list of three elements: vector `description` and `time_bp` defining
#' each band, and a boolean `time_bp` show determining whether times should be given
#' as time_bp when labelling bands by terra
#' as time_bp when labeling bands by terra
#'
#' @keywords internal
vrt_get_meta <- function(vrt_path) {
x<- xml2::read_xml(vrt_path)
# check that we have metadata information needed for pastclim
has_time_node <- xml2::xml_find_first(x, "./Metadata/MDI[@key = 'pastclim_time_bp']")
if (inherits(has_time_node,"xml_missing")){
stop ("metadata element 'pastclim_time_bp' missing; time information not available for this raster")
stop ("metadata element 'pastclim_time_bp' missing for ",vrt_path,"; time information not available for this raster")
}
# new determine whether time is in bp (with the pastclim conventions) or CE
time_bp <- as.logical(xml2::xml_text(has_time_node))
Expand All @@ -31,7 +31,7 @@ vrt_get_meta <- function(vrt_path) {
time_band <- as.numeric(xml2::xml_text(xml2::xml_find_all(x, "./VRTRasterBand/Metadata/MDI[@key = 'time']")))
# make sure that time metadata was unique (i.e. not duplicated)
if (length(time_band)!=length(xml2::xml_find_all(x, "./VRTRasterBand"))){
stop("duplicated time elements in at least one band")
stop("duplicated time elements in at least one band in ", vrt_path)
}
if (!time_bp){
time_band <- time_band-1950
Expand Down
4 changes: 2 additions & 2 deletions R/vrt_set_meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ vrt_set_meta <- function (vrt_path, description, time_vector, time_bp=TRUE){
# check that we don't alreayd have metadata information needed for pastclim
has_time_node <- xml2::xml_find_first(x, "./Metadata/MDI[@key = 'pastclim_time_bp']")
if (!inherits(has_time_node,"xml_missing")){
warning ("metadata for pastclim is already present")
warning ("metadata for pastclim is already present in ", vrt_path)
return(FALSE)
}
# add metadata to indicate that we have a time axis
Expand All @@ -29,7 +29,7 @@ vrt_set_meta <- function (vrt_path, description, time_vector, time_bp=TRUE){
# add band description and times
band_nodes <- xml2::xml_find_all(x, xpath="VRTRasterBand")
if (length(band_nodes)!=length(time_vector)){
warning("the vrt has a different number of bands from the length of the time vector")
warning("the vrt ",vrt_path," has a different number of bands from the length of the time vector")
return(FALSE)
}
for (i_node in seq_len(length(band_nodes))){
Expand Down

0 comments on commit 2c72b29

Please sign in to comment.