|
4 | 4 | #'
|
5 | 5 | #' @param x Either a Field, or a list of PFT objects
|
6 | 6 | #' @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. |
8 | 8 | #' @param return.ids A logical, if TRUE (default) then return the the id of the PFT, if FALSE return the entire PFT object
|
9 | 9 | #'
|
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) |
12 | 11 | #'
|
13 | 12 | #' @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 | + |
| 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 | +#' |
14 | 65 | #' @export
|
15 | 66 |
|
16 | 67 | listPFTs <- function(x, criteria = NULL, return.ids = TRUE) {
|
17 | 68 |
|
18 | 69 |
|
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 |
23 | 81 | else if(is.list(x)){
|
24 | 82 | for(this in x) {
|
25 | 83 | if(!is.PFT(this)) stop("At least one item in the input list is not an object PFT class")
|
26 | 84 | }
|
27 | 85 | pft.list <- x
|
28 | 86 | }
|
| 87 | + |
29 | 88 | # else fail
|
30 | 89 | else {
|
31 | 90 | stop("Unexpected input for argument 'x' in listPFTs().")
|
32 | 91 | }
|
33 | 92 |
|
34 |
| - # get a list of the layers of x, to check that the PFT is actually present |
35 |
| - layers.x <- names(x) |
36 |
| - |
37 | 93 |
|
38 | 94 | # now check each PFT in X to see if it matches the criteria
|
39 | 95 | matched.pfts <- list()
|
40 | 96 | if(!is.null(criteria)) criteria <- tolower(criteria)
|
41 | 97 | for(PFT in pft.list) {
|
42 | 98 |
|
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)) { |
45 | 104 |
|
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 |
56 | 107 |
|
57 | 108 | }
|
58 | 109 |
|
|
0 commit comments