Skip to content

Commit 66c0880

Browse files
committed
** v0.7.1 **� Bugfix due to a small regression in listPFTs(). Also took the opportunity to include examples for listPFTs, unit tests for listPFTs, and changed the 'print' functions to 'show' functions as is appropriate for S4 methods (but this does not effect users since print/show methods seem to dispatch to each depending on the appropriate class type).
1 parent b1001dc commit 66c0880

11 files changed

+496
-326
lines changed

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: DGVMTools
22
Type: Package
3-
Version: 0.7.0
3+
Version: 0.7.1
44
Title: DGVM Processing, Analysis and Plotting Tools
55
Date: 2019-02-27
66
Authors@R: c(person("Matthew", "Forrest", role=c("aut", "cre"), email="[email protected]"),
@@ -91,14 +91,14 @@ Collate:
9191
'plotSpatialComparison.R'
9292
'plotTemporal.R'
9393
'plotting-framework-functions.R'
94-
'print-methods.R'
9594
'renameLayers.R'
9695
'selectDays.R'
9796
'selectGridcells.R'
9897
'selectLayers.R'
9998
'selectMonths.R'
10099
'selectSeasons.R'
101100
'selectYears.R'
101+
'show-methods.R'
102102
'small-utility-functions.R'
103103
'summary-methods.R'
104104
'writeField-methods.R'

NAMESPACE

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ export(plotSeasonal)
7676
export(plotSpatial)
7777
export(plotSpatialComparison)
7878
export(plotTemporal)
79-
export(print)
8079
export(promoteToRaster)
8180
export(proportionsComparison)
8281
export(renameLayers)
@@ -110,12 +109,13 @@ exportMethods(crop)
110109
exportMethods(extent)
111110
exportMethods(layers)
112111
exportMethods(names)
113-
exportMethods(print)
112+
exportMethods(show)
114113
exportMethods(summary)
115114
exportMethods(writeField)
116115
exportMethods(writeNetCDF)
117116
import(data.table)
118117
import(ggplot2)
119118
import(methods)
119+
importMethodsFrom(methods,show)
120120
importMethodsFrom(raster,crop)
121121
importMethodsFrom(raster,extent)

R/Format-GUESS.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ GUESS.PFTs <- list(
10011001
),
10021002

10031003
# BNS
1004-
BNS = new("PFT",
1004+
new("PFT",
10051005
id = "BNS",
10061006
name = "Boreal Needleleaved Summergreen Tree",
10071007
growth.form = "Tree",

R/listPFTs.R

+73-22
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,106 @@
44
#'
55
#' @param x Either a Field, or a list of PFT objects
66
#' @param criteria A character string to use to select PFTs. This compared to every the value of every slot of a PFT object. If one matches, the PFT is inlucded in the return.
7-
# If left empty return all PFTs ()
7+
# If left empty or set to NULL the return all PFTs.
88
#' @param return.ids A logical, if TRUE (default) then return the the id of the PFT, if FALSE return the entire PFT object
99
#'
10-
#' @return Either a list of characters (the ids of the PFTs) or a list of PFT object (depending on argument return.ids)
11-
#'
10+
#' @return Either a vector of characters (the ids of the PFTs) or a list of PFT object (depending on argument return.ids)
1211
#'
1312
#' @author Matthew Forrest \email{matthew.forrest@@senckenberg.de}
13+
#'
14+
#' @examples
15+
#'
16+
#' ## List PFTs from a list of PFT objects
17+
#'
18+
#' # make a list of PFTs for selecting from
19+
#' PFT.list <- [email protected]
20+
#' print(PFT.list)
21+
#'
22+
#' # return with no criteria (trivial)
23+
#' listPFTs(PFT.list)
24+
#'
25+
#' # return as the full PFT objects
26+
#' listPFTs(PFT.list, return.ids = FALSE)
27+
#'
28+
#' # return Tree, Grass, Evergreen and Tropical PFTs
29+
#' listPFTs(PFT.list, criteria = "Tree")
30+
#' listPFTs(PFT.list, criteria = "Grass")
31+
#' listPFTs(PFT.list, criteria = "Evergreen")
32+
#' listPFTs(PFT.list, criteria = "Tropical")
33+
#'
34+
#' # return Tropical PFTs as a list of PFTs objects (not just the ids)
35+
#' listPFTs(PFT.list, criteria = "Tropical", return.ids = FALSE)
36+
#'
37+
#' \donttest{
38+
#'
39+
#' ## List PFTs from a Field object
40+
#'
41+
#' # Load a per-PFT Field from the example data
42+
#' run.dir <- system.file("extdata", "LPJ-GUESS_Runs", "CentralEurope", package = "DGVMTools")
43+
#' test.Source <- defineSource(id = "LPJ-GUESS_Example", dir = run.dir, format = GUESS)
44+
#' test.Field.perPFT <- getField(source = test.Source, var = "lai")
45+
#'
46+
#' # Perform the same examples as above but with the Field instead of a list of PFTs objects
47+
#'
48+
#' # return with no criteria (trivial)#'
49+
#' listPFTs(test.Field.perPFT )
50+
#'
51+
#' # return as the full PFT objects
52+
#' listPFTs(test.Field.perPFT, return.ids = FALSE)
53+
#'
54+
#' # return Tree, Grass, Evergreen and Tropical PFTs
55+
#' listPFTs(test.Field.perPFT, criteria = "Tree")
56+
#' listPFTs(test.Field.perPFT, criteria = "Grass")
57+
#' listPFTs(test.Field.perPFT, criteria = "Evergreen")
58+
#' listPFTs(test.Field.perPFT, criteria = "Tropical")
59+
#'
60+
#' # return Tropical PFTs as a list of PFTs objects (not just the ids)
61+
#' listPFTs(test.Field.perPFT, criteria = "Tropical", return.ids = FALSE)
62+
#'
63+
#' }
64+
#'
1465
#' @export
1566

1667
listPFTs <- function(x, criteria = NULL, return.ids = TRUE) {
1768

1869

19-
# check input and get the PFT superset
20-
# if Field
21-
if(is.Field(x)) pft.list <- x@source@pft.set
22-
# if list
70+
# first prepare a list of all PFTs present
71+
72+
# if Field compare the layers of x with the PFTs in the Format object
73+
if(is.Field(x)) {
74+
pft.list <- list()
75+
for(this.PFT in x@source@pft.set) {
76+
if(this.PFT@id %in% layers(x)) pft.list <- append(pft.list, this.PFT)
77+
}
78+
}
79+
80+
# else if list, check that all elements are a PFT
2381
else if(is.list(x)){
2482
for(this in x) {
2583
if(!is.PFT(this)) stop("At least one item in the input list is not an object PFT class")
2684
}
2785
pft.list <- x
2886
}
87+
2988
# else fail
3089
else {
3190
stop("Unexpected input for argument 'x' in listPFTs().")
3291
}
3392

34-
# get a list of the layers of x, to check that the PFT is actually present
35-
layers.x <- names(x)
36-
3793

3894
# now check each PFT in X to see if it matches the criteria
3995
matched.pfts <- list()
4096
if(!is.null(criteria)) criteria <- tolower(criteria)
4197
for(PFT in pft.list) {
4298

43-
# check PFT is present in data.table
44-
if(PFT@id %in% layers.x) {
99+
if(tolower(PFT@growth.form) == criteria
100+
|| tolower(PFT@climate.zone) == criteria
101+
|| tolower(PFT@leaf.form) == criteria
102+
|| tolower(PFT@phenology) == criteria
103+
|| is.null(criteria)) {
45104

46-
if(tolower(PFT@growth.form) == criteria
47-
|| tolower(PFT@climate.zone) == criteria
48-
|| tolower(PFT@leaf.form) == criteria
49-
|| tolower(PFT@phenology) == criteria
50-
|| is.null(criteria)) {
51-
52-
if(return.ids) matched.pfts[[PFT@id]] <- PFT@id
53-
else matched.pfts[[PFT@id]] <- PFT
54-
55-
}
105+
if(return.ids) matched.pfts[[PFT@id]] <- PFT@id
106+
else matched.pfts[[PFT@id]] <- PFT
56107

57108
}
58109

R/print-methods.R

-205
This file was deleted.

0 commit comments

Comments
 (0)