Skip to content

Commit

Permalink
Merge pull request #143 from jread-usgs/master
Browse files Browse the repository at this point in the history
v0.7.0 updates
  • Loading branch information
Jordan S Read committed Mar 13, 2015
2 parents 31bed33 + e1537f0 commit 48e05d2
Show file tree
Hide file tree
Showing 13 changed files with 547 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: glmtools
Type: Package
Title: glmtools
Version: 0.6.2
Version: 0.7.0
Date: 2013-09-18
Author: Jordan S Read, Luke A Winslow
Maintainer: Jordan S Read <jread@usgs.gov>
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export(resample_to_field)
export(run_example_sim)
export(set_nml)
export(sim_metrics)
export(sim_var_longname)
export(sim_var_units)
export(sim_vars)
export(summarize_sim)
export(validate_sim)
Expand Down
17 changes: 15 additions & 2 deletions R/nml_helpers.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@


# private function
buildVal <- function(textLine){
buildVal <- function(textLine, lineNum, blckName){
#-----function appends nml list with new values-----
# remove all text after comment string
textLine <- strsplit(textLine,'!')[[1]][1]

if (!any(grep("=",textLine))){stop(c("no hanging lines allowed in .nml, used ",textLine))}
if (!any(grep("=",textLine))){
stop(c("no hanging lines allowed in .nml, used ",textLine,'.\nSee line number:',lineNum,' in "&',blckName,'" section.'))
}
params <- strsplit(textLine,"=") # break text at "="
parNm <- params[[1]][1]
parVl <- params[[1]][2]
Expand Down Expand Up @@ -101,3 +103,14 @@ ascii_only <- function(file){
}

}

.validate_nml <- function(nml){
# test for required blocks:
required_blks <- c('outflow', 'inflow', 'meteorology', 'init_profiles', 'output', 'time', 'morphometry', 'glm_setup')
blk_match <- required_blks %in% names(nml)
if (!all(blk_match)){
stop('parsing error in nml file.',required_blks[blk_match],'missing.')
}
return(TRUE)

}
48 changes: 32 additions & 16 deletions R/plot_var.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#'@param num_cells number of vertical cells to use for heatmap
#'@param fig_path F if plot to screen, string path if save plot as .png
#'@param add F if create new figure, T if add to existing
#'@param bar_title NULL if use var_name, or specify as a string to name plot variable
#'@param bar_title NULL if use \code{\link{sim_var_longname}}, or specify as a string to name plot variable
#'@keywords methods
#'@seealso \link{get_temp}
#'@seealso \code{\link{get_temp}}, \code{\link{sim_var_longname}}
#'@author
#'Jordan S. Read, Luke A. Winslow
#'@examples
Expand All @@ -29,24 +29,43 @@ plot_var <- function(file, var_name, col_lim, reference = 'surface', num_cells =
min_depth <- 0
z_out <- seq(min_depth, max_depth,length.out = num_cells)
variable_df <- get_var(file, reference = reference, z_out, var_name=var_name)

if (is.null(bar_title)){
bar_title <- sim_var_longname(file, var_name)
}

if (is.character(fig_path)){
gen_default_fig(file_name = fig_path)
}

if (ncol(variable_df) == 2){
stop('plot_var() not yet supported for 1D output variables')
.plot_timeseries(variable_df, add, bar_title)
} else {
.plot_heatmap(variable_df, col_lim, reference, add, bar_title, z_out)
}

if (is.character(fig_path)){
dev.off()
} else {
#layout(as.matrix(1))
}

}

.plot_heatmap <- function(variable_df, col_lim, reference, add, bar_title, z_out){
palette <- colorRampPalette(c("violet","blue","cyan", "green3", "yellow", "orange", "red"),
bias = 1, space = "rgb")

if (missing(col_lim)) col_lim <- range(variable_df[, -1], na.rm = TRUE)
levels <- seq(col_lim[1], col_lim[2], by = diff(col_lim)/15)
#col_subs <- unique(floor(seq(col_lim[1], col_lim[2]-1, length.out = 15)))
col_subs <- levels
colors <- palette(n = length(levels)-1)
dates <- variable_df[, 1]
matrix_var <- data.matrix(variable_df[, -1])
xaxis <- get_xaxis(dates)
yaxis <- get_yaxis_2D(z_out, reference)

if (is.character(fig_path)){
gen_default_fig(file_name = fig_path)
}



plot_layout(xaxis, yaxis, add)
.filled.contour(x = dates, y = z_out, z =matrix_var,
Expand All @@ -55,14 +74,11 @@ plot_var <- function(file, var_name, col_lim, reference = 'surface', num_cells =

axis_layout(xaxis, yaxis) #doing this after heatmap so the axis are on top

if (is.null(bar_title)){
bar_title = var_name
}
color_key(levels, colors, subs=col_subs, col_label = bar_title)
if (is.character(fig_path)){
dev.off()
} else {
#layout(as.matrix(1))
}

}

.plot_timeseries <- function(variable_df, add, bar_title){
if (add) par(new=TRUE)
plot(variable_df, ylab = bar_title)
}
9 changes: 6 additions & 3 deletions R/read_nml.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ read_nml <- function(nml_file = 'template'){
#browser()
if(substr(textLine,1,1)!='!'){
# Add a check here, sometimes, if there is a hanging comma,
#and only sumtimes that means add next row
#and only sometimes that means add next row
if(substr(textLine,nchar(textLine), nchar(textLine)) == ',' &&
j+1 <= length(fileLines) && !any(grep("=",fileLines[j+1])) && !any(grep("/",fileLines[j+1]))){

Expand All @@ -66,14 +66,17 @@ read_nml <- function(nml_file = 'template'){
carryover = ''
}
# else, line is commented out
lineVal <- buildVal(textLine)
lineVal <- buildVal(textLine, lineNum=j, blckName)
nml[[i]] <- c(nml[[i]],lineVal)
}
}
}
return(.nml(nml))
nml <- .nml(nml)
.validate_nml(nml)
return(nml)
}


nml_path_norm <- function(nml_file){
if (nml_file == "template"){
nml_file <- nml_template_path()
Expand Down
25 changes: 25 additions & 0 deletions R/sim_var_longname.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#'@title get long name of variable from a GLM simulation
#'@param file a string with the path to the netcdf output from GLM
#'@param var_name name of a valid variable from a simulation (see \code{\link{sim_vars}})
#'@keywords methods
#'@seealso \code{\link{sim_vars}}, \code{\link{sim_var_units}}
#'@author
#'Jordan S. Read
#'@examples
#'sim_folder <- run_example_sim(verbose = FALSE)
#'nc_file <- file.path(sim_folder, 'output.nc')
#'vars <- sim_vars(file = nc_file)
#'sim_var_units(nc_file, vars[1])
#'sim_var_units(nc_file, 'u_mean')
#'@export
sim_var_longname <- function(file, var_name){

glm_nc <- get_glm_nc(file)
longname <- glm_nc$var[[var_name]]$longname
close_glm_nc(glm_nc)
if (is.null(longname)){stop(var_name,' not found. Check variable name')}
return(longname)
}



24 changes: 24 additions & 0 deletions R/sim_var_units.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#'@title get units of variable from a GLM simulation
#'@param file a string with the path to the netcdf output from GLM
#'@param var_name name of a valid variable from a simulation (see \code{\link{sim_vars}})
#'@keywords methods
#'@seealso \code{\link{sim_vars}}, \code{\link{sim_var_longname}}
#'@author
#'Jordan S. Read
#'@examples
#'sim_folder <- run_example_sim(verbose = FALSE)
#'nc_file <- file.path(sim_folder, 'output.nc')
#'vars <- sim_vars(file = nc_file)
#'sim_var_longname(nc_file, vars[1])
#'sim_var_longname(nc_file, 'u_mean')
#'@export
sim_var_units <- function(file, var_name){

glm_nc <- get_glm_nc(file)
units <- glm_nc$var[[var_name]]$units
close_glm_nc(glm_nc)
if (is.null(units)){stop(var_name,' not found. Check variable name')}
return(units)
}


Loading

0 comments on commit 48e05d2

Please sign in to comment.