Class VariablePlugin
- java.lang.Object
-
- uk.ac.rdg.resc.edal.dataset.plugins.VariablePlugin
-
- Direct Known Subclasses:
ArbitraryGroupPlugin
,DifferencePlugin
,NormalisedDifferencePlugin
,ValueErrorPlugin
,VectorPlugin
public abstract class VariablePlugin extends Object
This class specifies a way of generating new variables on-the-fly from existing ones. It works by supplying to the constructor a list of variable IDs which the plugin uses, and a list of suffixes which it provides. The full variable IDs which the plugin provides are obtained by combining the input IDs and adding each of the suffixes. The plugin must then override the two abstract methodsdoProcessVariableMetadata(VariableMetadata...)
andgenerateValue(String, HorizontalPosition, Number...)
to generate appropriate metadata and values respectively. For an example of usage, seeVectorPlugin
, which groups vector components and generates magnitude and direction variables.- Author:
- Guy Griffiths
-
-
Field Summary
Fields Modifier and Type Field Description protected int
prefixLength
protected String[]
uses
-
Constructor Summary
Constructors Constructor Description VariablePlugin(String[] usesVariables, String[] providesSuffixes)
Instantiate a plugin
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected String
combineIds(String... partsToUse)
Provides a convenience method for mangling several IDs into one new one.protected abstract VariableMetadata[]
doProcessVariableMetadata(VariableMetadata... metadata)
Subclasses should override this method to modify theVariableMetadata
tree, and return any new objects added to it.Array1D<Number>
generateArray1D(String varId, Array1D<HorizontalPosition> positions, Array1D<Number>... sourceArrays)
Convenience method for generating anArray1D
from sourceArray2D<Number>
generateArray2D(String varId, Array2D<HorizontalPosition> positions, Array2D<Number>... sourceArrays)
Convenience method for generating anArray2D
from sourceprotected abstract Number
generateValue(String varSuffix, HorizontalPosition pos, Number... sourceValues)
Subclasses should override this method to generate values based on source variable valuesprotected String
getFullId(String suffix)
Returns an ID based on the combined ID of all used variables and the suffix of a provided variable.Number
getValue(String varId, HorizontalPosition pos, Number... values)
Generates a value for the desired IDprotected VariableMetadata
newVariableMetadataFromDomains(Parameter parameter, boolean scalar, HorizontalDomain[] hDomains, VerticalDomain[] zDomains, TemporalDomain[] tDomains)
GeneratesVariableMetadata
from the given arguments and domains.protected VariableMetadata
newVariableMetadataFromMetadata(Parameter parameter, boolean scalar, VariableMetadata... metadata)
GeneratesVariableMetadata
from the given arguments and metadata.VariableMetadata[]
processVariableMetadata(VariableMetadata... metadata)
Modifies the currentVariableMetadata
tree to reflect the changes this plugin implements.String[]
providesVariables()
String[]
usesVariables()
-
-
-
Field Detail
-
uses
protected String[] uses
-
prefixLength
protected int prefixLength
-
-
Method Detail
-
usesVariables
public String[] usesVariables()
- Returns:
- The IDs of the variables which this plugin uses, in the order it needs them
-
providesVariables
public String[] providesVariables()
- Returns:
- The IDs of the variables which this plugin provides
-
generateArray1D
public Array1D<Number> generateArray1D(String varId, Array1D<HorizontalPosition> positions, Array1D<Number>... sourceArrays)
Convenience method for generating anArray1D
from source
-
generateArray2D
public Array2D<Number> generateArray2D(String varId, Array2D<HorizontalPosition> positions, Array2D<Number>... sourceArrays)
Convenience method for generating anArray2D
from source
-
processVariableMetadata
public VariableMetadata[] processVariableMetadata(VariableMetadata... metadata) throws EdalException
Modifies the currentVariableMetadata
tree to reflect the changes this plugin implements.- Parameters:
metadata
- An array ofVariableMetadata
of the source variables- Returns:
- An array of any new
VariableMetadata
objects inserted into the tree - Throws:
EdalException
- If there is a problem processing the metadata
-
getValue
public Number getValue(String varId, HorizontalPosition pos, Number... values)
Generates a value for the desired ID- Parameters:
varId
- The ID of the variable to generate a value forpos
- TheHorizontalPosition
at which the data is being generated. This may be relevant to how the plugin processes the valuesvalues
- An array ofNumber
s representing the source values- Returns:
- The derived value
-
doProcessVariableMetadata
protected abstract VariableMetadata[] doProcessVariableMetadata(VariableMetadata... metadata) throws EdalException
Subclasses should override this method to modify theVariableMetadata
tree, and return any new objects added to it. This allows subclasses to arbitrarily restructure the metadata tree by calling theVariableMetadata.setParent(VariableMetadata, String)
methods. Note that the IDs in the newly-createdVariableMetadata
objects should be generated by callinggetFullId(String)
with the variable's suffix. If the plugin is designed to group several child variables, the first child will be used to automatically estimate the scale range of the parent. This is guaranteed to only be called once.- Parameters:
metadata
- An array ofVariableMetadata
of the source variables in the order they were supplied to the constructor- Returns:
- The derived
VariableMetadata
- Throws:
EdalException
- If there is a problem generating new metadata
-
generateValue
protected abstract Number generateValue(String varSuffix, HorizontalPosition pos, Number... sourceValues)
Subclasses should override this method to generate values based on source variable values- Parameters:
varSuffix
- The suffix ID of the variable to generateVariableMetadata
for. This will be one of the provided suffixes in the constructor, but not the actual variable ID (which subclasses do not need to worry about)pos
- TheHorizontalPosition
at which the value is generated. This may affect the returned valuesourceValues
- An array ofNumber
s representing the source values in the order they were supplied to the constructor- Returns:
- The derived value
-
combineIds
protected String combineIds(String... partsToUse)
Provides a convenience method for mangling several IDs into one new one. This just concatenates them, but subclasses may wish to override this if they require a specific format for IDs. This is guaranteed to be called once upon construction.- Parameters:
partsToUse
- The IDs to base this name on
-
getFullId
protected final String getFullId(String suffix)
Returns an ID based on the combined ID of all used variables and the suffix of a provided variable. This should be used by subclasses to generate newVariableMetadata
objects indoProcessVariableMetadata(VariableMetadata...)
if required- Parameters:
suffix
- The suffix used to identify the generated variable.- Returns:
- The full ID
-
newVariableMetadataFromMetadata
protected VariableMetadata newVariableMetadataFromMetadata(Parameter parameter, boolean scalar, VariableMetadata... metadata) throws IncorrectDomainException
GeneratesVariableMetadata
from the given arguments and metadata. If allVariableMetadata
arguments are of the same type (e.g.GridVariableMetadata
orHorizontalMesh4dVariableMetadata
), then the return type is guaranteed to be of that type. If that is not possible, an exception will be thrown. This should be used by subclasses when generatingVariableMetadata
from several otherVariableMetadata
objects.- Parameters:
parameter
- TheParameter
describing the variablescalar
- Whether the resulting variable is a scalar (if not, it is a grouping variable)metadata
- An array ofVariableMetadata
objects for the source data- Returns:
- A
VariableMetadata
object with the specified parameters, and domains which encompass the intersection of all of the supplied domains. If appropriate, this will be aGridVariableMetadata
object. - Throws:
IncorrectDomainException
- If a common domain of the same type as the domains of the suppliedVariableMetadata
objects cannot be found.
-
newVariableMetadataFromDomains
protected VariableMetadata newVariableMetadataFromDomains(Parameter parameter, boolean scalar, HorizontalDomain[] hDomains, VerticalDomain[] zDomains, TemporalDomain[] tDomains) throws IncorrectDomainException
GeneratesVariableMetadata
from the given arguments and domains. If all of the domains areHorizontalGrid
s, then the resultingVariableMetadata
will also beGridVariableMetadata
and can be cast as such. This should be used by subclasses when generatingVariableMetadata
from several otherVariableMetadata
objects.- Parameters:
parameter
- TheParameter
describing the variablescalar
- Whether the resulting variable is a scalar (if not, it is a grouping variable)hDomains
- An array ofHorizontalDomain
s for the source datazDomains
- An array ofVerticalDomain
s for the source datatDomains
- An array ofTemporalDomain
s for the source data- Returns:
- A
VariableMetadata
object with the specified parameters, and domains which encompass the intersection of all of the supplied domains. If appropriate, this will be aGridVariableMetadata
object. - Throws:
IncorrectDomainException
-
-