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 intprefixLengthprotected 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 StringcombineIds(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 theVariableMetadatatree, and return any new objects added to it.Array1D<Number>generateArray1D(String varId, Array1D<HorizontalPosition> positions, Array1D<Number>... sourceArrays)Convenience method for generating anArray1Dfrom sourceArray2D<Number>generateArray2D(String varId, Array2D<HorizontalPosition> positions, Array2D<Number>... sourceArrays)Convenience method for generating anArray2Dfrom sourceprotected abstract NumbergenerateValue(String varSuffix, HorizontalPosition pos, Number... sourceValues)Subclasses should override this method to generate values based on source variable valuesprotected StringgetFullId(String suffix)Returns an ID based on the combined ID of all used variables and the suffix of a provided variable.NumbergetValue(String varId, HorizontalPosition pos, Number... values)Generates a value for the desired IDprotected VariableMetadatanewVariableMetadataFromDomains(Parameter parameter, boolean scalar, HorizontalDomain[] hDomains, VerticalDomain[] zDomains, TemporalDomain[] tDomains)GeneratesVariableMetadatafrom the given arguments and domains.protected VariableMetadatanewVariableMetadataFromMetadata(Parameter parameter, boolean scalar, VariableMetadata... metadata)GeneratesVariableMetadatafrom the given arguments and metadata.VariableMetadata[]processVariableMetadata(VariableMetadata... metadata)Modifies the currentVariableMetadatatree 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 anArray1Dfrom source
-
generateArray2D
public Array2D<Number> generateArray2D(String varId, Array2D<HorizontalPosition> positions, Array2D<Number>... sourceArrays)
Convenience method for generating anArray2Dfrom source
-
processVariableMetadata
public VariableMetadata[] processVariableMetadata(VariableMetadata... metadata) throws EdalException
Modifies the currentVariableMetadatatree to reflect the changes this plugin implements.- Parameters:
metadata- An array ofVariableMetadataof the source variables- Returns:
- An array of any new
VariableMetadataobjects 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- TheHorizontalPositionat which the data is being generated. This may be relevant to how the plugin processes the valuesvalues- An array ofNumbers representing the source values- Returns:
- The derived value
-
doProcessVariableMetadata
protected abstract VariableMetadata[] doProcessVariableMetadata(VariableMetadata... metadata) throws EdalException
Subclasses should override this method to modify theVariableMetadatatree, 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-createdVariableMetadataobjects 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 ofVariableMetadataof 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 generateVariableMetadatafor. 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- TheHorizontalPositionat which the value is generated. This may affect the returned valuesourceValues- An array ofNumbers 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 newVariableMetadataobjects 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
GeneratesVariableMetadatafrom the given arguments and metadata. If allVariableMetadataarguments are of the same type (e.g.GridVariableMetadataorHorizontalMesh4dVariableMetadata), 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 generatingVariableMetadatafrom several otherVariableMetadataobjects.- Parameters:
parameter- TheParameterdescribing the variablescalar- Whether the resulting variable is a scalar (if not, it is a grouping variable)metadata- An array ofVariableMetadataobjects for the source data- Returns:
- A
VariableMetadataobject with the specified parameters, and domains which encompass the intersection of all of the supplied domains. If appropriate, this will be aGridVariableMetadataobject. - Throws:
IncorrectDomainException- If a common domain of the same type as the domains of the suppliedVariableMetadataobjects cannot be found.
-
newVariableMetadataFromDomains
protected VariableMetadata newVariableMetadataFromDomains(Parameter parameter, boolean scalar, HorizontalDomain[] hDomains, VerticalDomain[] zDomains, TemporalDomain[] tDomains) throws IncorrectDomainException
GeneratesVariableMetadatafrom the given arguments and domains. If all of the domains areHorizontalGrids, then the resultingVariableMetadatawill also beGridVariableMetadataand can be cast as such. This should be used by subclasses when generatingVariableMetadatafrom several otherVariableMetadataobjects.- Parameters:
parameter- TheParameterdescribing the variablescalar- Whether the resulting variable is a scalar (if not, it is a grouping variable)hDomains- An array ofHorizontalDomains for the source datazDomains- An array ofVerticalDomains for the source datatDomains- An array ofTemporalDomains for the source data- Returns:
- A
VariableMetadataobject with the specified parameters, and domains which encompass the intersection of all of the supplied domains. If appropriate, this will be aGridVariableMetadataobject. - Throws:
IncorrectDomainException
-
-