Skip to content

Commit cdb32fa

Browse files
author
Luke Winslow
committed
Merge pull request #196 from jread-usgs/master
fixes #195
2 parents 8cecd32 + e146658 commit cdb32fa

11 files changed

+118
-106
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: glmtools
22
Type: Package
33
Title: glmtools
4-
Version: 0.13.0
4+
Version: 0.13.1
55
Date: 2016-01-15
66
Authors@R: c( person("Jordan", "Read", role = c("aut","cre"),
77
email = "[email protected]"),

R/plot_temp.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#'@param file a string with the path to the netcdf output from GLM
33
#'@param reference a string for 'surface' or 'bottom'
44
#'@param fig_path F if plot to screen, string path if save plot as .png
5+
#' @param col_lim range for heatmap (in units of the variable)
56
#'@param ... additional arguments passed to \code{par()}
67
#'@keywords methods
78
#'@seealso \code{\link{get_temp}}, \code{\link{plot_var}}
@@ -16,8 +17,8 @@
1617
#'plot_temp(file = nc_file, fig_path = FALSE)
1718
#'plot_temp(file = nc_file, fig_path = 'test_figure.png', height = 3, reference = 'surface')
1819
#'@export
19-
plot_temp <- function(file='output.nc', fig_path = FALSE, reference = 'surface', ...){
20+
plot_temp <- function(file='output.nc', fig_path = FALSE, reference = 'surface', col_lim, ...){
2021

21-
plot_var(file, var_name = 'temp', fig_path, reference,...)
22+
plot_var(file, var_name = 'temp', fig_path, reference, col_lim, ...)
2223

2324
}

R/plot_temp_compare.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
#'@title Plot matching heatmaps for modeled and observed temp
2-
#'@param nc_file Netcdf model output file
3-
#'@param field_file CSV or TSV field data file (see \link{resample_to_field} for format)
1+
#' @title Plot matching heatmaps for modeled and observed temp
2+
#' @param nc_file Netcdf model output file
3+
#' @param field_file CSV or TSV field data file (see \link{resample_to_field} for format)
44
#' @param fig_path F if plot to screen, string path if save plot as .png
5-
#'@param \dots additional arguments passed to \code{\link{resample_to_field}}
5+
#' @param resample sample the model output to the same time points as the observations?
6+
#' @param col_lim range for heatmap (in units of the variable)
7+
#' @param \dots additional arguments passed to \code{\link{resample_to_field}}
68
#'
79
#'@seealso Internally uses \code{\link{plot_var_compare}}, \code{\link{get_temp}} and \code{\link{resample_to_field}}
810
#'
@@ -16,8 +18,8 @@
1618
#'
1719
#'plot_temp_compare(nc_file, field_file) ##makes a plot!
1820
#'@export
19-
plot_temp_compare = function(nc_file, field_file, fig_path=FALSE, ...){
21+
plot_temp_compare = function(nc_file, field_file, fig_path=FALSE, resample=TRUE, col_lim, ...){
2022

21-
plot_var_compare(nc_file, field_file, var_name='temp', fig_path=fig_path, ...)
23+
plot_var_compare(nc_file, field_file, var_name='temp', fig_path=fig_path, resample=resample, col_lim=col_lim, ...)
2224

2325
}

R/plot_var.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#'@param var_name a character vector of valid variable names (see \code{\link{sim_vars}})
44
#'@param fig_path F if plot to screen, string path if save plot as .png
55
#'@param reference 'surface' or 'bottom'. Only used for heatmap plots.
6+
#' @param col_lim range for heatmap (in units of the variable)
67
#'@param ... additional arguments passed to \code{par()}
78
#'@keywords methods
89
#'@seealso \code{\link{get_temp}}, \code{\link{sim_var_longname}},
@@ -25,7 +26,7 @@
2526
#'fig_path = 'aed_out.png')
2627
#'}
2728
#'@export
28-
plot_var <- function(file='output.nc', var_name, fig_path = F, reference='surface', ...){
29+
plot_var <- function(file='output.nc', var_name, fig_path = F, reference='surface', col_lim, ...){
2930

3031
heatmaps <- .is_heatmap(file, var_name)
3132
num_divs <- length(var_name)
@@ -42,7 +43,7 @@ plot_var <- function(file='output.nc', var_name, fig_path = F, reference='surfac
4243
# iterate through plots
4344
for (j in 1:num_divs){
4445
if (heatmaps[j]){
45-
.plot_nc_heatmap(file, var_name[j], reference)
46+
.plot_nc_heatmap(file, var_name[j], reference, col_lim=col_lim)
4647
} else {
4748
.plot_nc_timeseries(file, var_name[j])
4849
if(is_heatmap) .plot_null() # to fill up the colormap div

R/plot_var_compare.R

Lines changed: 82 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,83 @@
1-
#' @title Plot matching heatmaps for modeled and observed temp
2-
#' @param nc_file Netcdf model output file
3-
#' @param field_file CSV or TSV field data file (see \link{resample_to_field} for format)
4-
#' @param var_name a character vector of valid variable names (see \code{\link{sim_vars}})
5-
#' @param fig_path F if plot to screen, string path if save plot as .png
6-
#' @param resample sample the model output to the same time points as the observations?
7-
#' @param \dots additional arguments passed to \code{\link{resample_to_field}}
8-
#'
9-
#' @seealso Internally uses \link{get_var} and \link{resample_to_field}
10-
#'
11-
#'
12-
#'@examples
13-
#'sim_folder <- run_example_sim(verbose = FALSE)
14-
#'nc_file <- file.path(sim_folder, 'output.nc')
15-
#'nml_file <- file.path(sim_folder, 'glm2.nml')
16-
#'field_file <- file.path(sim_folder, 'field_data.tsv')
17-
#'
18-
#'run_glm(sim_folder)
19-
#'
20-
#'plot_var_compare(nc_file, field_file, 'temp', resample=FALSE) ##makes a plot!
21-
#'
22-
#'@importFrom akima interp
23-
#'@export
24-
plot_var_compare = function(nc_file, field_file, var_name, fig_path = FALSE, resample = TRUE, ...){
25-
26-
heatmaps <- .is_heatmap(nc_file, var_name)
27-
if (!heatmaps){
28-
warning('plot_var_compare not implemented for 1D variables')
29-
return()
30-
}
31-
32-
start_par = par(no.readonly = TRUE)
33-
#Create layout
34-
35-
mod_temp = get_var(nc_file, var_name, reference='surface')
36-
mod_depths = get.offsets(mod_temp)
37-
38-
39-
data = resample_to_field(nc_file, field_file, var_name=var_name, ...)
40-
if(resample){
41-
model_df <- resample_sim(mod_temp, t_out = unique(data$DateTime))
42-
}else{
43-
model_df = mod_temp
44-
}
45-
46-
#Pivot observed into table
47-
x = as.numeric(as.POSIXct(data$DateTime))
48-
y = data$Depth
49-
z = data[,paste0('Observed_', var_name)]
50-
x_out = sort(unique(x))
51-
y_out = sort(unique(mod_depths))
52-
53-
#remove any NA values before the 2D interp
54-
x = x[!is.na(z)]
55-
y = y[!is.na(z)]
56-
z = z[!is.na(z)]
57-
58-
#Added a scaling factor to Y. Interp won't interpolate if X and Y are on vastly different scales.
59-
# I don't use Y from here later, so it doesn't matter what the mangitude of the values is.
60-
interped = interp(x, y*1e6, z, x_out, y_out*1e6)
61-
62-
gen_default_fig(filename=fig_path, num_divs=2)#, omi = c(0.1, 0.5, 0, 0))
63-
.stacked_layout(heatmaps, num_divs=2)
64-
obs_df <- data.frame(interped$z)
65-
names(obs_df) <- paste('var_', y_out, sep='')
66-
obs_df <- cbind(data.frame(DateTime=as.POSIXct(x_out, origin='1970-01-01')), obs_df)
67-
68-
#Use model to define X-axis plotting extent for both graphs
69-
xaxis <- get_xaxis(model_df[,1])
70-
71-
y.text = y_out[1]+diff(range(y_out))*0.05 # note, reference will ALWAYS be surface for compare to field data
72-
.plot_df_heatmap(obs_df, bar_title = .unit_label(nc_file,var_name), overlays=c(points(x=x,y=y),text(x_out[1],y=y.text,'Observed', pos=4, offset = 1)), xaxis=xaxis)
73-
74-
.plot_df_heatmap(model_df, bar_title = .unit_label(nc_file,var_name), overlays=text(x_out[1],y=y.text,'Modeled', pos=4, offset = 1), xaxis=xaxis)
75-
76-
par(start_par)#set PAR back to what it started at
77-
if(is.character(fig_path))
78-
dev.off()
1+
#' @title Plot matching heatmaps for modeled and observed temp
2+
#' @param nc_file Netcdf model output file
3+
#' @param field_file CSV or TSV field data file (see \link{resample_to_field} for format)
4+
#' @param var_name a character vector of valid variable names (see \code{\link{sim_vars}})
5+
#' @param fig_path F if plot to screen, string path if save plot as .png
6+
#' @param resample sample the model output to the same time points as the observations?
7+
#' @param col_lim range for heatmap (in units of the variable)
8+
#' @param \dots additional arguments passed to \code{\link{resample_to_field}}
9+
#'
10+
#' @seealso Internally uses \link{get_var} and \link{resample_to_field}
11+
#'
12+
#'
13+
#'@examples
14+
#'sim_folder <- run_example_sim(verbose = FALSE)
15+
#'nc_file <- file.path(sim_folder, 'output.nc')
16+
#'nml_file <- file.path(sim_folder, 'glm2.nml')
17+
#'field_file <- file.path(sim_folder, 'field_data.tsv')
18+
#'
19+
#'run_glm(sim_folder)
20+
#'
21+
#'plot_var_compare(nc_file, field_file, 'temp', resample=FALSE) ##makes a plot!
22+
#'
23+
#'@importFrom akima interp
24+
#'@export
25+
plot_var_compare = function(nc_file, field_file, var_name, fig_path = FALSE, resample = TRUE, col_lim, ...){
26+
27+
heatmaps <- .is_heatmap(nc_file, var_name)
28+
if (!heatmaps){
29+
warning('plot_var_compare not implemented for 1D variables')
30+
return()
31+
}
32+
33+
start_par = par(no.readonly = TRUE)
34+
#Create layout
35+
36+
mod_temp = get_var(nc_file, var_name, reference='surface')
37+
mod_depths = get.offsets(mod_temp)
38+
39+
40+
data = resample_to_field(nc_file, field_file, var_name=var_name, ...)
41+
if(resample){
42+
model_df <- resample_sim(mod_temp, t_out = unique(data$DateTime))
43+
}else{
44+
model_df = mod_temp
45+
}
46+
47+
#Pivot observed into table
48+
x = as.numeric(as.POSIXct(data$DateTime))
49+
y = data$Depth
50+
z = data[,paste0('Observed_', var_name)]
51+
x_out = sort(unique(x))
52+
y_out = sort(unique(mod_depths))
53+
54+
#remove any NA values before the 2D interp
55+
x = x[!is.na(z)]
56+
y = y[!is.na(z)]
57+
z = z[!is.na(z)]
58+
59+
#Added a scaling factor to Y. Interp won't interpolate if X and Y are on vastly different scales.
60+
# I don't use Y from here later, so it doesn't matter what the mangitude of the values is.
61+
interped = interp(x, y*1e6, z, x_out, y_out*1e6)
62+
63+
gen_default_fig(filename=fig_path, num_divs=2)#, omi = c(0.1, 0.5, 0, 0))
64+
.stacked_layout(heatmaps, num_divs=2)
65+
obs_df <- data.frame(interped$z)
66+
names(obs_df) <- paste('var_', y_out, sep='')
67+
obs_df <- cbind(data.frame(DateTime=as.POSIXct(x_out, origin='1970-01-01')), obs_df)
68+
69+
#Use model to define X-axis plotting extent for both graphs
70+
xaxis <- get_xaxis(model_df[,1])
71+
72+
y.text = y_out[1]+diff(range(y_out))*0.05 # note, reference will ALWAYS be surface for compare to field data
73+
if (missing(col_lim))
74+
col_lim = range(data[, -1], na.rm = TRUE)
75+
76+
.plot_df_heatmap(obs_df, bar_title = .unit_label(nc_file,var_name), overlays=c(points(x=x,y=y),text(x_out[1],y=y.text,'Observed', pos=4, offset = 1)), xaxis=xaxis, col_lim=col_lim)
77+
78+
.plot_df_heatmap(model_df, bar_title = .unit_label(nc_file,var_name), overlays=text(x_out[1],y=y.text,'Modeled', pos=4, offset = 1), xaxis=xaxis, col_lim=col_lim)
79+
80+
par(start_par)#set PAR back to what it started at
81+
if(is.character(fig_path))
82+
dev.off()
7983
}

R/timeseries_plots.R

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11

2-
.plot_nc_heatmap <- function(file, var_name, reference, num_cells=100, palette){
2+
.plot_nc_heatmap <- function(file, var_name, reference, num_cells=100, palette, ...){
33

44
surface <- get_surface_height(file)
55
max_depth <- max(surface[, 2])
66
min_depth <- 0
77
z_out <- seq(min_depth, max_depth,length.out = num_cells)
88
data <- get_var(file, z_out = z_out, var_name = var_name, reference = reference)
99
title = .unit_label(file, var_name)
10-
.plot_df_heatmap(data, title, num_cells, palette)
10+
.plot_df_heatmap(data, title, num_cells, palette, ...)
1111
}
1212

13-
.plot_df_heatmap <- function(data, bar_title, num_cells, palette, title_prefix=NULL, overlays=NULL, xaxis=NULL, ...){
13+
.plot_df_heatmap <- function(data, bar_title, num_cells, palette, title_prefix=NULL, overlays=NULL, xaxis=NULL, col_lim){
1414

1515
z_out <- rLakeAnalyzer::get.offsets(data)
1616
reference = ifelse(substr(names(data)[2],1,3) == 'elv', 'bottom', 'surface')
1717

18+
if (missing(col_lim))
19+
col_lim = range(data[, -1], na.rm = TRUE)
1820
if (missing(palette))
1921
palette <- colorRampPalette(c("violet","blue","cyan", "green3", "yellow", "orange", "red"),
2022
bias = 1, space = "rgb")
2123

22-
col_lim <- range(data[, -1], na.rm = TRUE)
23-
24-
2524
col_subs <- head(pretty(col_lim, 6), -1)
2625
levels <- sort(unique(c(col_subs, pretty(col_lim, 15))))
2726
colors <- palette(n = length(levels)-1)
@@ -35,7 +34,7 @@
3534
plot_layout(xaxis, yaxis, add=T)
3635
.filled.contour(x = dates, y = z_out, z =matrix_var,
3736
levels= levels,
38-
col=colors, ...)
37+
col=colors)
3938
overlays # will plot any overlay functions
4039
axis_layout(xaxis, yaxis) #doing this after heatmap so the axis are on top
4140

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ glmtools
66
Tools for interacting with the [General Lake Model (GLM)](http://aed.see.uwa.edu.au/research/models/GLM/ "General Lake Model's website") in R. `glmtools` includes some basic functions for calculating physical derivatives and thermal properties of model output, and some plotting functionality (see example image below).
77

88

9-
![alt tag](http://github.gleon.io/images/test_figure.png)
10-
11-
`glmtools`, as of v0.2.5, can also call GLM using the `GLMr` package. Shown here running GLM from R with example driver data that is part of the package:
12-
13-
![alt tag](http://github.gleon.io/images/glm-r.png)
14-
159
`glmtools` Functions (as of v0.2.5.2)
1610
=====
1711
| Function | Title |

man/plot_temp.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot_temp_compare.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot_var.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)