Class CachedGrid
- All Implemented Interfaces:
Cloneable
,ValueSource
- Direct Known Subclasses:
HDFCachedGrid
,NCCachedGrid
,NOAA1bCachedGrid
A CachedGrid
is a Grid
that uses temporary caching
to reduce the overall memory footprint of gridded data. The cache
uses a similar strategy to how modern operating systems cache
memory pages. A data stream is kept open, and a number of tiles
(rectangular sections of data) are brought into memory as they are
needed. The number of tiles in memory (the cache size) and the
size of each tile may be set by the user. Tiles that are no longer
needed are swapped out to make room for new tiles in the cache. In
order to avoid excessive data I/O, a least-recently-used rule is
used to determine which tile to remove from the cache when the
cache reaches its maximum capacity.
The standard DataVariable.setValue(noaa.coastwatch.util.DataLocation, double)
method is
supported by keeping a dirty flag for each tile. If the tile has been
written to, it is kept in the cache until, upon removal, it is written
to the data stream.
The only methods that subclasses need to implement are readTile(noaa.coastwatch.io.tile.TilingScheme.TilePosition)
to
retrieve tiles from the data source, writeTile(noaa.coastwatch.io.tile.TilingScheme.Tile)
to update the
data with any changes made to tiles, and getDataStream()
to retrieve
the object used to read and write data (only used to check for equality).
- Since:
- 3.1.0
- Author:
- Peter Hollemans
-
Field Summary
Modifier and TypeFieldDescriptionprotected int
The access mode.static final int
Default cache tiles.static final int
Default tile dimensions.protected static final int
Read-only mode.protected static final int
Read-write mode.protected TilingScheme
The tiling scheme.Fields inherited from class noaa.coastwatch.util.DataVariable
data, dims, isUnsigned, lookup, unsignedType
-
Constructor Summary
ModifierConstructorDescriptionprotected
CachedGrid
(Grid grid, int accessMode) Constructs a new cached grid with the specified properties. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears the existing cache.void
flush()
Writes any unwritten tiles to the data stream.getCoveringPositions
(int[] start, int[] count) Gets a minimal list of tile positions that cover a subset of this grid.getData()
Gets the variable data array.getData
(int[] start, int[] count) Gets a subset of grid data values.abstract Object
Gets the cache data stream as an object.int
Gets the current maximum number of tiles allowed in the cache.static int[]
getTileDims
(int tileSize, Grid grid) Calculates the tile dimensions required to fill the specified memory.static int
getTileSize
(int[] tileDims, Grid grid) Calculates the tile memory size based on dimensions.Gets the tiling scheme for this grid if one is available.double
getValue
(int index) Reads a scaled data value.double
getValue
(int row, int col) Reads a scaled data value with no navigation.static void
Tests this class.protected abstract TilingScheme.Tile
Reads the specified tile.protected void
Resets the tile cache to be empty.void
setCacheSize
(int cacheSize) Creates a new cache with the specified size.void
Sets the variable data array.void
Sets a subset of grid data values.void
setMaxTiles
(int tiles) Creates a new cache with a maximum number of tiles.void
setOptimizedCacheSize
(int cacheSize) Creates a new cache with the specified size.void
setTileDims
(int[] tileDims) Creates a new cache where each tile has the specified dimensions.void
setTileSize
(int tileSize) Creates a new cache where each tile has the specified size.void
setValue
(int index, double val) Writes a scaled data value.void
setValue
(int row, int col, double val) Writes a scaled data value with no navigation.protected abstract void
writeTile
(TilingScheme.Tile tile) Writes the specified tile.Methods inherited from class noaa.coastwatch.util.Grid
arraycopy, checkSubset, getNavigation, getSubset, getValue, interpolate, navigate, setNavigation, setValue
Methods inherited from class noaa.coastwatch.util.DataVariable
convertUnits, dispose, format, format, format, getClassBits, getDataClass, getDecimals, getDimensions, getFormat, getLongName, getMissing, getName, getOptimalStride, getRank, getScaling, getStatistics, getStatistics, getStatistics, getStatistics, getUnits, getUnsigned, getValue, getValues, setAccessHint, setFormat, setLongName, setLookup, setMissing, setName, setUnits, setUnsigned, setValue, toString
Methods inherited from class noaa.coastwatch.util.MetadataContainer
clone, getMetadataMap
-
Field Details
-
DEFAULT_MAX_TILES
public static final int DEFAULT_MAX_TILESDefault cache tiles.- See Also:
-
DEFAULT_TILE_DIMS
public static final int DEFAULT_TILE_DIMSDefault tile dimensions.- See Also:
-
READ_ONLY
protected static final int READ_ONLYRead-only mode.- See Also:
-
READ_WRITE
protected static final int READ_WRITERead-write mode.- See Also:
-
tiling
The tiling scheme. -
accessMode
protected int accessModeThe access mode.
-
-
Constructor Details
-
CachedGrid
Constructs a new cached grid with the specified properties. The cache is created with a default size and maximum number of tiles.- Parameters:
grid
- the grid to use for attributes.accessMode
- the access mode, eitherREAD_ONLY
orREAD_WRITE
.
-
-
Method Details
-
getTilingScheme
Description copied from class:Grid
Gets the tiling scheme for this grid if one is available.- Overrides:
getTilingScheme
in classGrid
- Returns:
- the tiling scheme or null for none.
-
setOptimizedCacheSize
public void setOptimizedCacheSize(int cacheSize) Creates a new cache with the specified size. The cache size is optimized so that the cache can contain a set of tiles which span at least the entire width of the grid for more efficient caching. Thus, the actual cache size may be slightly larger than that requested.- Parameters:
cacheSize
- the cache memory size in bytes.- See Also:
-
getDataStream
Gets the cache data stream as an object. This is useful for classes that read and write cached grids.- Returns:
- the data stream used for reading and writing data.
-
clearCache
public void clearCache()Clears the existing cache. All tiles are removed and the cache set to empty. Use this method to release the memory used by the cache when it won't be needed again. The same notes inresetCache()
on usingflush()
apply here as well.- Since:
- 3.5.0
-
resetCache
protected void resetCache()Resets the tile cache to be empty. This method should be used with caution, since it does not perform aflush()
prior to resetting the cache. Any tiles whose contents have been modified but not written will lose those modifications. Therefore, users of read/write mode tiles should explicitly callflush()
before this method. -
setCacheSize
public void setCacheSize(int cacheSize) Creates a new cache with the specified size.- Parameters:
cacheSize
- the cache memory size in bytes.- See Also:
-
getMaxTiles
public int getMaxTiles()Gets the current maximum number of tiles allowed in the cache.- Returns:
- the maximum tiles allowed.
-
setMaxTiles
public void setMaxTiles(int tiles) Creates a new cache with a maximum number of tiles.- Parameters:
tiles
- the maximum number of tiles.- See Also:
-
setTileSize
public void setTileSize(int tileSize) Creates a new cache where each tile has the specified size.- Parameters:
tileSize
- the tile memory size in bytes.
-
setTileDims
public void setTileDims(int[] tileDims) Creates a new cache where each tile has the specified dimensions.- Parameters:
tileDims
- the tile dimensions as [rows, columns].
-
getTileSize
Calculates the tile memory size based on dimensions.- Parameters:
tileDims
- the tile dimensions as [rows, columns].grid
- the grid for calculation.- Returns:
- the memory size used by each tile in bytes.
-
getTileDims
Calculates the tile dimensions required to fill the specified memory.- Parameters:
tileSize
- the tile memory size in bytes.grid
- the grid for calculation.- Returns:
- the dimensions of the largest tile that fills the memory without being larger than the grid dimensions.
-
readTile
Reads the specified tile.- Parameters:
pos
- the tile position to read.- Returns:
- the tile at the specified position.
- Throws:
IOException
- if an error occurred reading the tile data.
-
writeTile
Writes the specified tile. The implementation of this method must set the tile to be not dirty upon successfully writing it, or throw an error.- Parameters:
tile
- the tile to write.- Throws:
IOException
- if an error occurred writing the tile data.
-
flush
Writes any unwritten tiles to the data stream.- Throws:
IOException
- if an error occurred writing the tile data.
-
setValue
public void setValue(int index, double val) Description copied from class:DataVariable
Writes a scaled data value. The data value is scaled according to the scaling factor and offset and written to the data array.- Overrides:
setValue
in classDataVariable
- 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
public void setValue(int row, int col, double val) Description copied from class:Grid
Writes a scaled data value with no navigation. The data value is scaled according to the scaling factor and offset and written to the data array. No navigation transform is applied to correct the data location. -
getValue
public double getValue(int index) Description copied from class:DataVariable
Reads a scaled data value. The data value is read from the data array and scaled according to the scaling factor and offset.- Overrides:
getValue
in classDataVariable
- 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:
-
getValue
public double getValue(int row, int col) Description copied from class:Grid
Reads a scaled data value with no navigation. The data value is read from the data array and scaled according to the scaling factor and offset. No navigation transform is applied to correct the data location. -
setData
Description copied from class:DataVariable
Sets the variable data array.- Overrides:
setData
in classDataVariable
-
getData
Description copied from class:DataVariable
Gets the variable data array. The returned data is read-only -- the result of attempting to set values in the data is undefined.- Overrides:
getData
in classDataVariable
-
getCoveringPositions
Gets a minimal list of tile positions that cover a subset of this grid.- Parameters:
start
- the subset starting [row, column].count
- the subset dimension [rows, columns].- Returns:
- the list of tile positions.
- Throws:
IndexOutOfBoundsException
- if the subset falls outside the grid dimensions.
-
setData
Description copied from class:Grid
Sets a subset of grid data values. This method is similar toDataVariable.setData(Object)
, but sets only a subset of data values in the raw, unscaled form. -
getData
Description copied from class:Grid
Gets a subset of grid data values. This method is similar toGrid.getData(int[], int[])
, but retrieves only a subset of data values in the raw, unscaled form. -
main
Tests this class.- Parameters:
argv
- the array of command line parameters.- Throws:
Exception
-