Package noaa.coastwatch.util
Class DataVariable
java.lang.Object
noaa.coastwatch.util.MetadataContainer
noaa.coastwatch.util.DataVariable
- All Implemented Interfaces:
Cloneable
,ValueSource
A data variable is a container class to hold data values
and a specific set of metadata. The required metadata includes the
variable name, units, dimensions, number format, scaling, and
missing data value. Other metadata may also be attached using the
TreeMap
functions, where it is expected that the key
be a Java String
object.
Note that this class and any subclasses are not guaranteed to be
thread safe.- Since:
- 3.1.0
- Author:
- Peter Hollemans
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
TheAccessType
class describes a type of data accesss that is anticipated. -
Field Summary
Modifier and TypeFieldDescriptionprotected Object
Array of data values.protected int[]
Array of data dimension sizes.protected boolean
The unsigned flag, true if the data values are unsigned.protected double[]
The lookup table.protected int
The unsigned type. -
Constructor Summary
ConstructorDescriptionDataVariable
(String name, String longName, String units, int[] dimensions, Object data, NumberFormat format, double[] scaling, Object missing) Constructs a new data variable with the specified properties. -
Method Summary
Modifier and TypeMethodDescriptionvoid
convertUnits
(String newUnitSpec) Converts the units of this variable to the new units.void
dispose()
Disposes of any resources used by this variable prior to being finalized.format
(double val) Formats a data value to a string.format
(int index) Formats a data value to a string.format
(DataLocation loc) Formats a data value to a string.static int
Gets the memory required by a primitive class.getData()
Gets the variable data array.Gets the class associated with components of the data array.static int
getDecimals
(String number) Gets the number of digits after the decimal place in a string representing a fractional number with possible exponential part.int[]
Gets the variable data array dimensions.Gets the data number format.Gets the descriptive variable name.Gets the data missing value.getName()
Gets the short variable name.int[]
getOptimalStride
(DataLocation start, DataLocation end, double factor, int minCount) Gets the optimal statistical sampling stride for this variable based on a desired sampling factor and minimum number of values.int
getRank()
Gets the variable data array rank.double[]
Gets the scaling factor and offset.getStatistics
(double factor) Gets the data variable statistics.getStatistics
(int[] stride) Gets the data variable statistics.getStatistics
(DataLocation start, DataLocation end, double factor) Gets the data variable statistics.getStatistics
(DataLocation start, DataLocation end, int[] stride) Gets the data variable statistics.getUnits()
Gets the variable units.boolean
Gets the unsigned flag.double
getValue
(int index) Reads a scaled data value.protected double
Reads a scaled data value.double
getValue
(DataLocation loc) Reads a scaled data value.int
Gets the total number of data values indicated by the dimensions.abstract double
interpolate
(DataLocation loc) Reads an interpolated data value.void
setAccessHint
(int[] start, int[] end, int[] stride) Sets a hint to the variable that subsequent data access will only read/write values within the specified data extents.void
Sets a hint to the variable that subsequent data access will be of the specified access type.void
Sets the variable data array.void
setFormat
(NumberFormat format) Sets the data number format.void
setLongName
(String longName) Sets the descriptive variable name.void
setLookup
(double[] lookup) Sets the lookup table.void
setMissing
(Object missing) Sets the data missing value.void
Sets the short variable name.void
Sets the variable units.void
setUnsigned
(boolean flag) Sets the unsigned data flag.void
setValue
(int index, double val) Writes a scaled data value.protected void
Writes a scaled data value.void
setValue
(DataLocation loc, double val) Writes a scaled data value.toString()
Gets a string representation of this variable.Methods inherited from class noaa.coastwatch.util.MetadataContainer
clone, getMetadataMap
-
Field Details
-
dims
protected int[] dimsArray of data dimension sizes. -
data
Array of data values. -
isUnsigned
protected boolean isUnsignedThe unsigned flag, true if the data values are unsigned. -
unsignedType
protected int unsignedTypeThe unsigned type. -
lookup
protected double[] lookupThe lookup table.
-
-
Constructor Details
-
DataVariable
public DataVariable(String name, String longName, String units, int[] dimensions, Object data, NumberFormat format, double[] scaling, Object missing) Constructs a new data variable with the specified properties.- Parameters:
name
- a short variable name. The variable name should reflect what the data values represent without being overly verbose. For example,avhrr_ch4
would describe data from the AVHRR sensor, channel 4.longName
- a long variable name. The long name is a verbose string to describe the variable in common terms. For example, the variable namedsst
might have the long namesea surface temperature
.units
- the real world variable units. For example if the variable data describes a temperature in Celsius, the variable units might beCelsius
.dimensions
- the variable data array dimensions. For a 2D image data array, the dimensions would be the number of rows and columns in the image.data
- the variable data array. The data should be stored as a 1D array of values. It is up to the subclass to determine the mapping between data coordinate and index into the data array for data with dimensions greater than 1. If the passed array isnull
, data get/set/format methods are undefined.format
- a number format specification. The number format will be used to convert data values from their numerical representation to a string representation.scaling
- the scaling factor and offset. These values will be used to convert between data stored in the data array and real world values. In general, scaling is only necessary when floating point data is stored as integers. When data is retrieved from the data array, the following formula is used to scale the values:(val - scaling[1])*scaling[0]
. When setting values in the data array, the following formula is used:val/scaling[0] + scaling[1]
. Ifscaling
isnull
, no scaling is performed.missing
- the missing data value. The missing value is used to represent data values in the data array that have purposely not been recorded or were flagged as invalid. Ifmissing
isnull
, no missing value is used.
-
-
Method Details
-
getName
Gets the short variable name. -
setName
Sets the short variable name. -
getLongName
Gets the descriptive variable name. -
setLongName
Sets the descriptive variable name. -
getUnits
Gets the variable units. -
setUnits
Sets the variable units. -
getDimensions
public int[] getDimensions()Gets the variable data array dimensions. -
getRank
public int getRank()Gets the variable data array rank. -
getData
Gets the variable data array. The returned data is read-only -- the result of attempting to set values in the data is undefined. -
setData
Sets the variable data array. -
getScaling
public double[] getScaling()Gets the scaling factor and offset. -
getFormat
Gets the data number format. -
setFormat
Sets the data number format. -
getMissing
Gets the data missing value. -
setMissing
Sets the data missing value. -
setLookup
public void setLookup(double[] lookup) Sets the lookup table. The lookup table is an alternative to the scaling factor and offset, and may be used to translate integer valued data to floating point data values. If a lookup table is employed, thesetValue()
may not be used. If both lookup and scaling are set, the lookup takes precedence over the scaling and the scaling is ignored.- Parameters:
lookup
- the lookup table to use.
-
getDataClass
Gets the class associated with components of the data array.- Returns:
- the data class or null if the data array is null.
-
setUnsigned
public void setUnsigned(boolean flag) Sets the unsigned data flag. This may only be used if the data array is of type byte[], short[], int[], or long[]. If the data class is null, this method has no effect.- Parameters:
flag
- the unsigned flag, true is the data array contains unsigned data.- Throws:
IllegalArgumentException
- if the data class is not allowed to be unsigned.- See Also:
-
getUnsigned
public boolean getUnsigned()Gets the unsigned flag. -
getClassBits
Gets the memory required by a primitive class.- Parameters:
c
- the primitive class.- Returns:
- the number of bits needed for each primitive value. The value 0 is returned if the class is not primitive.
-
interpolate
Reads an interpolated data value.- Parameters:
loc
- the data value location.- Returns:
- the scaled data value as a
double
. TheDouble.NaN
value is used if the data value is missing. - See Also:
-
getValue
Reads a scaled data value. The data value is read from the data array and scaled according to the scaling factor and offset.- Specified by:
getValue
in interfaceValueSource
- Parameters:
loc
- the data value location.- Returns:
- the scaled data value as a
double
. TheDouble.NaN
value is used if the data value is missing. - See Also:
-
getValues
public int getValues()Gets the total number of data values indicated by the dimensions.- Returns:
- the number of data values.
-
getValue
Reads a scaled data value.- Parameters:
index
- the index into the data array.data
- the data array to use for raw data.- Returns:
- the scaled data value as a
double
. TheDouble.NaN
value is used if the data value is missing.
-
getValue
public double getValue(int index) Reads a scaled data value. The data value is read from the data array and scaled according to the scaling factor and offset.- Parameters:
index
- the index into the data array.- Returns:
- the scaled data value as a
double
. TheDouble.NaN
value is used if the data value is missing. - See Also:
-
setValue
Writes a scaled data value. The data value is scaled according to the scaling factor and offset and written to the data array.- Parameters:
loc
- the data location.val
- the data value as a double. If the data value isDouble.NaN
and the missing value is non-null, the missing value is written to the array.- See Also:
-
setValue
public void setValue(int index, double val) Writes a scaled data value. The data value is scaled according to the scaling factor and offset and written to the data array.- Parameters:
index
- the index into the data array.val
- the data value as a double. If the data value isDouble.NaN
and the missing value is non-null, the missing value is written to the array.- See Also:
-
setValue
Writes a scaled data value. The data value is scaled according to the scaling factor and offset and written to the data array.- Parameters:
index
- the index into the data array.data
- the data array to use for raw data.val
- the data value as a double. If the data value isDouble.NaN
and the missing value is non-null, the missing value is written to the array.
-
format
Formats a data value to a string.- Parameters:
loc
- the data value location.- Returns:
- the formatted data value string.
- See Also:
-
format
format(int)
-
format
Formats a data value to a string.- Parameters:
index
- the index into the data array.- Returns:
- the formatted data value string.
- See Also:
-
format
format(DataLocation)
-
format
Formats a data value to a string.- Parameters:
val
- the data value as a double.- Returns:
- the formatted data value string.
- See Also:
-
getStatistics
Gets the data variable statistics. The computed statistics include the number of valid data values sampled, minimum, maximum, mean, and standard deviation.- Parameters:
factor
- the sample size as a fraction of the total number of values. For example, to sample 1 percent of the data the sample size is 0.01. The sample size is used to compute an optimal stride while preserving sample frequency uniformity in each dimension. The total number of sampled values is guaranteed to be bounded below by the requested sample size.- Returns:
- the data statistics. If the number of valid data values is zero, the minimum, maximum, mean, and standard deviation values are undefined.
- See Also:
-
getOptimalStride
Gets the optimal statistical sampling stride for this variable based on a desired sampling factor and minimum number of values. This method may be used in the case where statistics are desired for a subset of the variable, but the sampling factor produces a stride which is too large and results in too few data values being retrieved.- Parameters:
start
- the starting data location.end
- the ending data location.factor
- the sampling factor as a value in the range [0..1].minCount
- the minimum desired number of values. The stride should ensure that the number of values actually sampled is greater than this quantity.- Returns:
- the optimal stride vector.
- See Also:
-
getStatistics
Gets the data variable statistics. The computed statistics include the number of valid data values sampled, minimum, maximum, mean, and standard deviation.- Parameters:
start
- the starting data location. If null, the start is the first location.end
- the ending data location. If null, the end is the last location.factor
- the sample size as a fraction of the total number of values. For example, to sample 1 percent of the data the sample size is 0.01. The sample size is used to compute an optimal stride while preserving sample frequency uniformity in each dimension. The total number of sampled values is guaranteed to be bounded below by the requested sample size.- Returns:
- the data statistics. If the number of valid data values is zero, the minimum, maximum, mean, and standard deviation values are undefined.
- See Also:
-
getStatistics
Gets the data variable statistics. The computed statistics include the number of valid data values sampled, minimum, maximum, mean, and standard deviation.- Parameters:
stride
- the data location stride in each dimension. The stride is used to reduce the number of calculations required to compute the statistics. The data variable is sampled at every nth value along each dimension. For example if the stride is 4, every 4th value is sampled. If null, the stride is 1 in each dimension.- Returns:
- the data statistics. If the number of valid data values is zero, the minimum, maximum, mean, and standard deviation values are undefined.
- See Also:
-
getStatistics
Gets the data variable statistics. The computed statistics include the number of valid data values sampled, minimum, maximum, mean, and standard deviation.- Parameters:
start
- the starting data location. If null, the start is the first location.end
- the ending data location. If null, the end is the last location.stride
- the data location stride in each dimension. The stride is used to reduce the number of calculations required to compute the statistics. The data variable is sampled at every nth value along each dimension. For example if the stride is 4, every 4th value is sampled. If null, the stride is 1 in each dimension.- Returns:
- the data statistics. If the number of valid data values
is zero, the minimum, maximum, mean, and standard deviation
values are set to
Double.NaN
. - Throws:
RuntimeException
- if the start or end locations are invalid.- See Also:
-
getDecimals
Gets the number of digits after the decimal place in a string representing a fractional number with possible exponential part.- Parameters:
number
- the fractional number string.- Returns:
- the number of decimal digits.
-
convertUnits
Converts the units of this variable to the new units. The scaling factor and offset are adjusted accordingly. If the new units are identical to the existing units, no operation is performed.- Parameters:
newUnitSpec
- the new units to convert to.- Throws:
IllegalArgumentException
- if the existing or new data units cannot be recognized, or if the new units are incompatible with the existing units.
-
setAccessHint
public void setAccessHint(int[] start, int[] end, int[] stride) Sets a hint to the variable that subsequent data access will only read/write values within the specified data extents. This method performs no action in the abstract class, but may be used by subclasses that wish to setup internal structures prior to some sort of intensive data access. For example, if data rendering is to occur, it might be useful to read in only the data that will be rendered and make it availalble.- Parameters:
start
- the starting data coordinates.end
- the ending data coordinates.stride
- the data stride along each dimension.
-
setAccessType
Sets a hint to the variable that subsequent data access will be of the specified access type. This is an opportunity for the underlying data structures to be rearranged to better accomodate the specified access type.- Parameters:
type
- the access type hint.- Since:
- 3.8.1
-
toString
Gets a string representation of this variable. -
dispose
public void dispose()Disposes of any resources used by this variable prior to being finalized. After calling this method, any calls to retrieve data values may generate errors.- Since:
- 3.3.1
-