averageFields: Average Fields

View source: R/averageFields.R

averageFieldsR Documentation

Average Fields

Description

Average Fields provided to the function as a list; useful for averaging model ensembles, replicate simulations etc. Note that since you can supply the function, you don't actually need to average, you can also do, for example, max, min, sd, etc. Fails with an error message if the supplied Fields don't have the same layers.

Usage

averageFields(list.of.fields, method = mean, source = NULL)

Arguments

list.of.fields

The Fields that you want to average, stored in a list.

method

The function which you want to use. Is mean by default, but it can be anything that operates on a vector of numbers. Useful options might be max, min and sd.

source

Either a Source object which provides the metadata about the newly created data (representing, say, the ensemble mean) or NULL/missing, in which case a Source will be created using the metadata from the input Fields.

Details

The function currently does no checks that the spatial extent, yearly period and Quantity are consistent between runs. The resulting Field simple takes these things from the first object from list.of.fields. It is therefore up to the user not do anything silly and average quantites, or spatial or temporal extents that don't makes sense.

It is also only defined for numeric layers (no checks currently done), but could be definied for categorical ones if there was demand.

Value

A Field

Author(s)

Matthew Forrest matthew.forrest@senckenberg.de

Examples


 
# Get two example Fields (which will contain the same data but different meta-data)
test.dir <- system.file("extdata", "LPJ-GUESS_Runs", "CentralEurope", package = "DGVMTools")
test.Source1 <- defineSource(name = "LPJ-GUESS run 1", dir = test.dir,  format = GUESS)
field1 <- getField(source = test.Source1, quant = "lai", year.aggregate.method = "mean")
test.Source2 <- defineSource(name = "LPJ-GUESS run 2", dir = test.dir,  format = GUESS)
field2 <- getField(source = test.Source2, quant = "lai", year.aggregate.method = "mean")

# Average these two fields, specifically take the mean
field.mean <- averageFields(list(field1, field2))
print(field.mean)
print(field.mean@source)

# Plot them all together (they should be the same)
print(plotSpatial(list(field1, field2, field.mean)))

# Plot just the Total layer for clarity and set out panels vertically in three rows
p <- plotSpatial(list(field1, field2, field.mean), layers = "Total")
p <- p + facet_wrap(~ Facet, nrow = 3)
print(p)

# Repeat the procedure, but this time define a nicer Source to make nicer metadata

test.Source.averaged <- defineSource(name = "Ensemble Mean", dir = test.dir,  format = GUESS)
field.mean.2 <- averageFields(list(field1, field2), source = test.Source.averaged)
print(field.mean.2)
print(plotSpatial(list(field.mean.2)))

# also make the standard deviation (should be zero since the runs are identical)
field.sd <- averageFields(list(field1, field2), method = sd)
print(plotSpatial(field.sd))



MagicForrest/DGVMTools documentation built on Aug. 23, 2024, 8:05 a.m.