Skip to content
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

Viz cleanup #2526

Merged
merged 4 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ For more information about this file see also [Keep a Changelog](http://keepacha

### Changed
- Rebuilt documentation using Roxygen 7. Readers get nicer formatting of usage sections, writers get more flexible behavior when inheriting parameters and less hassle when maintaining namespaces (#2524).
- PEcAn.priors: renamed functions that looked like S3 methods but were not: `plot.posterior.density`->`plot_posterior.density`, `plot.prior.density`->`plot_prior.density`, `plot.trait`->`plot_trait` (#2439).
- Renamed functions that looked like S3 methods but were not:
* PEcAn.priors: `plot.posterior.density`->`plot_posterior.density`, `plot.prior.density`->`plot_prior.density`, `plot.trait`->`plot_trait` (#2439).
* PEcAn.visualization: `plot.netcdf`->`plot_netcdf` (#2526).
- Stricter package checking: `make check` and CI builds will now fail if `R CMD check` returns any ERRORs or any "newly-added" WARNINGs or NOTEs. "Newly-added" is determined by strict string comparison against a check result saved 2019-09-03; messages that exist in the reference result do not break the build but will be fixed as time allows in future refactorings (#2404).
- No longer writing an arbitrary num for each PFT, this was breaking ED runs potentially.
- The pecan/data container has no longer hardcoded path for postgres
Expand Down
7 changes: 3 additions & 4 deletions base/visualization/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ Description: The Predictive Ecosystem Carbon Analyzer (PEcAn) is a scientific
efficacy of scientific investigation.
This module is used to create more complex visualizations from the data
generated by PEcAn code, specifically the models.
Depends:
ggplot2,
raster,
sp
Imports:
data.table,
ggplot2,
maps,
ncdf4 (>= 1.15),
PEcAn.DB,
Expand All @@ -40,6 +37,8 @@ Imports:
Suggests:
grid,
png,
raster,
sp,
testthat (>= 1.0.2)
License: BSD_3_clause + file LICENSE
Copyright: Authors
Expand Down
2 changes: 1 addition & 1 deletion base/visualization/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export(add_icon)
export(ciEnvelope)
export(create_status_page)
export(map.output)
export(plot.netcdf)
export(plot_netcdf)
export(vwReg)
7 changes: 5 additions & 2 deletions base/visualization/R/ciEnvelope.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#' @param x Vector defining CI center
#' @param ylo Vector defining bottom of CI envelope
#' @param yhi Vector defining top of CI envelope
#' @export
#' @param ... further arguments passed on to `graphics::polygon`
#'
#' @export
#' @author Michael Dietze, David LeBauer
ciEnvelope <- function(x, ylo, yhi, ...) {
m <- rbind(x, ylo, yhi)
Expand All @@ -30,6 +32,7 @@ ciEnvelope <- function(x, ylo, yhi, ...) {
x <- sub.m[[i]]["x", ]
ylo <- sub.m[[i]]["ylo", ]
yhi <- sub.m[[i]]["yhi", ]
polygon(cbind(c(x, rev(x), x[1]), c(ylo, rev(yhi), ylo[1])), border = NA, ...)
graphics::polygon(
cbind(c(x, rev(x), x[1]), c(ylo, rev(yhi), ylo[1])), border = NA, ...)
}
} # ciEnvelope
38 changes: 23 additions & 15 deletions base/visualization/R/create_status_page.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ create_status_page <- function(config_file, file_prefix='status', delta=3600) {
lastdump <- 0 # never dumped
if(RCurl::url.exists(version_url)) {
temporaryFile <- tempfile()
download.file(version_url, destfile = temporaryFile, quiet = TRUE)
utils::download.file(version_url, destfile = temporaryFile, quiet = TRUE)
version <- scan(temporaryFile,what = "character", sep = "\t", quiet = TRUE)
unlink(temporaryFile)

Expand All @@ -113,8 +113,8 @@ create_status_page <- function(config_file, file_prefix='status', delta=3600) {
# check if the schema has been updated, if so log the event
if ((hostname %in% nodes) && (nodes[[hostname]]['schema'] != schema)) {
msg <- paste(Sys.time(), "SCHEMA UPDATE DETECTED ON NODE", x$sync_host_id, hostname,
"FROM", nodeinfo[[hostname]]['schema'], "TO", schema)
write(msg, file=paste0(file_prefix, ".log"), append=TRUE)
"FROM", nodes[[hostname]]['schema'], "TO", schema)
write(msg, file = paste0(file_prefix, ".log"), append = TRUE)
}
}
}
Expand All @@ -124,7 +124,7 @@ create_status_page <- function(config_file, file_prefix='status', delta=3600) {
sync <- list()
if (RCurl::url.exists(log_url)) {
temporaryFile <- tempfile()
download.file(log_url, destfile = temporaryFile, quiet = TRUE)
utils::download.file(log_url, destfile = temporaryFile, quiet = TRUE)
log <- scan(temporaryFile,what = "character", sep = "\n", quiet = TRUE)
unlink(temporaryFile)

Expand Down Expand Up @@ -176,30 +176,38 @@ create_status_page <- function(config_file, file_prefix='status', delta=3600) {
save(geoinfo, schema_versions, nodes, file=paste0(file_prefix, ".RData"))

## create image
png(filename=paste0(file_prefix, ".png"), width=1200)
xlim <- extendrange(sapply(nodes, function(x) { x$lon }), f=1)
ylim <- extendrange(sapply(nodes, function(x) { x$lat }), f=1)
maps::map("world", xlim=xlim, ylim=ylim)
maps::map("state",add=TRUE)
grDevices::png(filename = paste0(file_prefix, ".png"), width = 1200)
xlim <- grDevices::extendrange(sapply(nodes, function(x) { x$lon }), f = 1)
ylim <- grDevices::extendrange(sapply(nodes, function(x) { x$lat }), f = 1)
maps::map("world", xlim = xlim, ylim = ylim)
maps::map("state", add = TRUE)

# show all edges
edgecolors <- c("green","red", "yellow")
x <- lapply(nodes, function(x) {
lapply(x$sync, function(y) {
segments((x$lon+nodes[[y$id]]$lon)/2, ( x$lat+nodes[[y$id]]$lat)/2, x$lon, x$lat, col=edgecolors[y$status+1], lwd=2)
graphics::segments(
(x$lon + nodes[[y$id]]$lon) / 2,
(x$lat + nodes[[y$id]]$lat) / 2,
x$lon,
x$lat,
col = edgecolors[y$status + 1], lwd = 2)
})
})

# show all pecan sites
nodecolors <- c("green","yellow", "red")
nodecolors <- c("green", "yellow", "red")
x <- lapply(nodes, function(x) {
points(x$lon, x$lat, col=nodecolors[x$state+1], pch=19, cex=3)
text(x$lon, x$lat, labels=x$sync_host_id)
graphics::points(
x$lon, x$lat,
col = nodecolors[x$state + 1],
pch = 19, cex = 3)
graphics::text(x$lon, x$lat, labels = x$sync_host_id)
})

# graph done
text(xlim[1], ylim[1], labels = Sys.time(), pos=4)
dev.off()
graphics::text(xlim[1], ylim[1], labels = Sys.time(), pos = 4)
grDevices::dev.off()

## create html file
htmltable <- '<html>'
Expand Down
26 changes: 17 additions & 9 deletions base/visualization/R/map.output.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@
#' @author David LeBauer
map.output <- function(table, variable) {
if (any(table$lat < 0) | any(table$lon > 0)) {
world <- data.table::data.table(map_data("world"))
world <- data.table::data.table(ggplot2::map_data("world"))
} else {
world <- data.table::data.table(map_data("usa"))
world <- data.table::data.table(ggplot2::map_data("usa"))
}
map <- ggplot() +
geom_polygon(data = world, aes(x = long, y = lat, group = group), fill = "white", color = "darkgrey") +
geom_point(data = table, aes(x = lon, y = lat, color = table[, variable]), size = 5) +
scale_color_gradientn(colours = c("red", "orange", "yellow", "green", "blue", "violet")) +
theme_bw() +
xlim(range(pretty(table$lon))) +
ylim(range(pretty(table$lat)))
map <- ggplot2::ggplot() +
ggplot2::geom_polygon(
data = world,
ggplot2::aes(x = long, y = lat, group = group),
fill = "white",
color = "darkgrey") +
ggplot2::geom_point(
data = table,
ggplot2::aes(x = lon, y = lat, color = table[, variable]),
size = 5) +
ggplot2::scale_color_gradientn(
colours = c("red", "orange", "yellow", "green", "blue", "violet")) +
ggplot2::theme_bw() +
ggplot2::xlim(range(pretty(table$lon))) +
ggplot2::ylim(range(pretty(table$lat)))
return(map)
} # map.output
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ data.fetch <- function(var, nc, fun = mean) {

# aggregate the data
data <- ncdf4::ncvar_get(nc, var)
val <- aggregate(data[indices], by = aggrlist, FUN = fun)$x
val <- stats::aggregate(data[indices], by = aggrlist, FUN = fun)$x

# get the label
title <- nc$var[[var]]$longname
Expand All @@ -59,11 +59,10 @@ data.fetch <- function(var, nc, fun = mean) {

##' Load the tower dataset and create a plot.
##'
##' \code{plot.hdf5} loads the tower data from an HDF5 file generated by
##' Loads the tower data from an HDF5 file generated by
##' ED and will plot the values against one another. The default is for
##' the given variable to be plotted against time.
##'
##' @name plot.hdf5
##' @param datafile the specific datafile to use.
##' @param yvar the variable to plot along the y-axis.
##' @param xvar the variable to plot along the x-axis, by default time is
Expand All @@ -72,11 +71,13 @@ data.fetch <- function(var, nc, fun = mean) {
##' @param height the height of the image generated, default is 600 pixels.
##' @param filename is the name of the file name that is geneated, this
##' can be null to use existing device, otherwise it will try and
# @' create an image based on filename, or display if x11.
##' create an image based on filename, or display if x11.
##' @param year the year this data is for (only used in the title).
##'
##' @aliases plot.netcdf
##' @export
##' @author Rob Kooper
plot.netcdf <- function(datafile, yvar, xvar = "time", width = 800, height = 600,
plot_netcdf <- function(datafile, yvar, xvar = "time", width = 800, height = 600,
filename = NULL, year = NULL) {
# open netcdf file
nc <- ncdf4::nc_open(datafile)
Expand All @@ -90,33 +91,33 @@ plot.netcdf <- function(datafile, yvar, xvar = "time", width = 800, height = 600
# setup output
if (!is.null(filename)) {
if (tolower(filename) == "x11") {
x11(width = width / 96, height = height / 96)
grDevices::x11(width = width / 96, height = height / 96)
} else if (tolower(stringr::str_sub(filename, -4)) == ".png") {
png(filename = filename, width = width, height = height)
grDevices::png(filename = filename, width = width, height = height)
} else if (tolower(stringr::str_sub(filename, -4)) == ".pdf") {
pdf(filename = filename, width = width, height = height)
grDevices::pdf(file = filename, width = width, height = height)
} else if (tolower(stringr::str_sub(filename, -4)) == ".jpg") {
jpg(filename = filename, width = width, height = height)
grDevices::jpeg(filename = filename, width = width, height = height)
} else if (tolower(stringr::str_sub(filename, -5)) == ".tiff") {
tiff(filename = filename, width = width, height = height)
grDevices::tiff(filename = filename, width = width, height = height)
}
}

# setup plot (needs to be done before removing of NA since that removes attr as well).
plot.new()
title(xlab = attr(xval_mean, "lbl"))
title(ylab = attr(yval_mean, "lbl"))
graphics::plot.new()
graphics::title(xlab = attr(xval_mean, "lbl"))
graphics::title(ylab = attr(yval_mean, "lbl"))
if (xvar == "time") {
if (is.null(year)) {
title(main = nc$var[[yvar]]$longname)
graphics::title(main = nc$var[[yvar]]$longname)
} else {
title(main = paste(nc$var[[yvar]]$longname, "for", year))
graphics::title(main = paste(nc$var[[yvar]]$longname, "for", year))
}
} else {
if (is.null(year)) {
title(main = paste(xvar, "VS", yvar))
graphics::title(main = paste(xvar, "VS", yvar))
} else {
title(main = paste(xvar, "VS", yvar, "for", year))
graphics::title(main = paste(xvar, "VS", yvar, "for", year))
}
}
# done with netcdf file
Expand All @@ -139,29 +140,35 @@ plot.netcdf <- function(datafile, yvar, xvar = "time", width = 800, height = 600
o <- order(xval_mean, yval_mean)

# plot actual data
plot.window(xlim = c(min(xval_mean), max(xval_mean)),
graphics::plot.window(xlim = c(min(xval_mean), max(xval_mean)),
ylim = c(min(yvals), max(yvals)))
polygon(c(xval_mean[o], rev(xval_mean[o])),
c(yval_max[o], rev(yval_min[o])),
col = "gray",
border = "black")
lines(x = xval_mean[o], y = yval_mean[o], col = "red")
points(x = xval_mean[o], y = yval_mean[o], col = "black", pch = ".", cex = 5)

graphics::polygon(
c(xval_mean[o], rev(xval_mean[o])),
c(yval_max[o], rev(yval_min[o])),
col = "gray",
border = "black")
graphics::lines(x = xval_mean[o], y = yval_mean[o], col = "red")
graphics::points(
x = xval_mean[o],
y = yval_mean[o],
col = "black",
pch = ".",
cex = 5)

# legend
legend("bottomright", col = c(1, "gray"), lwd = c(3, 6),
legend = c("mean", "min/max"),
graphics::legend("bottomright", col = c(1, "gray"), lwd = c(3, 6),
legend = c("mean", "min/max"),
cex = 1.5)

# draw axis and box
axis(1)
axis(2)
box()
graphics::axis(1)
graphics::axis(2)
graphics::box()

## add PEcAn icon
add_icon()

if (!is.null(filename) && (tolower(filename) != "x11")) {
dev.off()
grDevices::dev.off()
}
} # plot.netcdf
} # plot_netcdf
Loading