Skip to content

Commit

Permalink
Added debug environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
r-barnes committed Sep 14, 2016
1 parent 322cd10 commit 1dfd91d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export(dgsetres)
export(dgshptogrid)
export(dgtransform)
export(dgverify)
export(dg_env)
55 changes: 46 additions & 9 deletions R/dggridR.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ dg_exe_path <- function(){
file.path(system.file(package="dggridR"), "bin", exe_name)
}


dg_env <- new.env()
assign("dg_debug", FALSE, envir=dg_env)

#' @name dg_shpfname_south_africa
#'
Expand Down Expand Up @@ -236,8 +237,13 @@ dgtransform <- function(dggs, lat, lon){ #TODO: Make sure we're not modifying th
ret <- read.csv(outputfile, header=FALSE)$V1

#Clean up
file.remove(inputfile)
file.remove(outputfile)
if(!get("dg_debug", envir=dg_env)){
file.remove(inputfile)
file.remove(outputfile)
} else {
print(paste("Inputfile:", inputfile))
print(paste("Outputfile:", outputfile))
}

if(any(ret>=2^53)) #R stores large numbers as an IEEE754 double, so we get 53 bits of exact integer goodness.
message('dgtransform(): Length of cell ids overflowed R\'s numeric storage capacity. Use a lower resolution')
Expand Down Expand Up @@ -283,10 +289,18 @@ dgrun <- function(dggs, clean=TRUE, check=TRUE, has_output_file=TRUE){
write.table(as.data.frame(do.call(rbind, dggs)), metafile, col.names=FALSE, quote=FALSE)
com <- paste(dg_exe_path(),metafile)
ret <- system(com, intern = TRUE, ignore.stdout = FALSE, ignore.stderr = FALSE)
if(get("dg_debug", envir=dg_env)){
cat(ret, sep='\n')
}
if(check && length(grep("complete \\*\\*",ret))!=1){
cat(ret)
stop('dggridR: Error in processing!', call.=FALSE)
}
#TODO: Clean up metafile here
if(!get("dg_debug", envir=dg_env)){
print(paste("Metafile:",metafile))
file.remove(metafile)
}
ret
}

Expand Down Expand Up @@ -756,15 +770,23 @@ dgrectgrid <- function(dggs,minlat=-1,minlon=-1,maxlat=-1,maxlon=-1,frame=TRUE,w
cellfile <- paste(cellfile,".kml",sep="")

#Clean up
file.remove(inputfile)
if(!get("dg_debug", envir=dg_env)){
file.remove(inputfile)
} else {
print(paste("Inputfile:",inputfile))
}

if(savegrid)
return(cellfile)

ret <- dg_process_kml(cellfile,frame,wrapcells)

#Clean up more
file.remove(cellfile)
if(!get("dg_debug", envir=dg_env)){
file.remove(cellfile)
} else {
print(paste("Cellfile: ",cellfile))
}

ret
}
Expand Down Expand Up @@ -841,7 +863,11 @@ dgearthgrid <- function(dggs,frame=TRUE,wrapcells=TRUE,savegrid=FALSE){ #TODO: D
ret <- dg_process_kml(cellfile,frame,wrapcells)

#Clean up
file.remove(cellfile)
if(!get("dg_debug", envir=dg_env)){
file.remove(cellfile)
} else {
print(paste("Cellfile:",cellfile))
}

ret
}
Expand Down Expand Up @@ -935,15 +961,22 @@ dgcellstogrid <- function(dggs,cells,frame=TRUE,wrapcells=TRUE,savegrid=FALSE){
cellfile <- paste(cellfile,".kml",sep="")

#Clean up
file.remove(inputfile)
if(!get("dg_debug", envir=dg_env)){
file.remove(inputfile)
} else {
print(paste("Inputfile:",inputfile))
print(paste("Cellfile:",cellfile))
}

if(savegrid)
return(cellfile)

ret <- dg_process_kml(cellfile,frame,wrapcells)

#Clean up more
file.remove(cellfile)
if(!get("dg_debug", envir=dg_env)){
file.remove(cellfile)
}

ret
}
Expand Down Expand Up @@ -1044,7 +1077,11 @@ dgshptogrid <- function(dggs,shpfname,frame=TRUE,wrapcells=TRUE,savegrid=FALSE){
ret <- dg_process_kml(cellfile,frame,wrapcells)

#Clean up more
file.remove(cellfile)
if(!get("dg_debug", envir=dg_env)){
file.remove(cellfile)
} else {
print(paste("Cellfile:",cellfile))
}

ret
}

0 comments on commit 1dfd91d

Please sign in to comment.