In HEC Java programs, GridData objects can be retrieved from and stored to HEC-DSS files using the get and put methods of the HecDss class with GridContainer objects, which are similar to other data container objects like TimeSeriesContainer or PairedDataContainer.
Table 1.1 – GridContainer Data Fields (extending DataContainer)

Field

Type

Description

gridData

GridData

A java object representing a grid (the GridData object contains a GridInfo object that holds its metadata)



Table 1.2 – GridContainer Methods (extending DataContainer)

Method

Returns

Description

getGridData()

GridData

Retrieves the GridData object from the data container

getGridInfo()

GridInfo

Retrieves the GridInfo() object from the data container (Calls getInfo() method on the GridData object held by the GridContainer)


The data and methods listed in the tables above are in addition to those in the DataContainer base class, which are described in the Scripting chapter of the HEC-DssVue User's Manual.

Retrieving a Grid from a DSS File


Like other DSS data types, a grid can be read from a DSS file using the get() method on an HecDss object. If the get method retrieves a grid, the DataContainer object that it returns will be a GridContainer (not a TimeSeriesContainer or PairedDataContainer, which are also subclasses of DataContainer).

Example 1: Identifying Grids in a DSS File
from hec.heclib.dss import *
from hec.io import GridContainer

theFile = HecDss.open("MyFile.dss ")
pathList = theFile.getCatalogedPathnames()
numPaths = len(pathList)
numGrids = 0

for path in pathList:
gc = theFile.get(path)
if isinstance(gc, GridContainer):
numGrids += 1

message = "%d of %d records contain grids." % (numGrids, numPaths)
MessageBox.showInformation(message, "DSS Grid")


The Jython example above uses the HecDss.get() method to retrieve DataContainer objects from a DSS file, then tests each one, using the Python built-in function instanceof to determine whether the DataContainer objects returned are grids or other data types.

Example 2: Retrieving a GridData Object from a DSS Record
from hec.heclib.dss import *
from hec.heclib.grid import *
from hec.io import GridContainer

theFile = HecDss.open("MyFile.dss")

gc = theFile.get( "/HRAP/MBRFC/PRECIP/04JUL2014:1100/04JUL2014:1200/MPE/" )

theGrid = gc.getGridData()
theInfo = theGrid.getGridInfo()

message = "Maximum Value = %5.2f" % theInfo.getMaxValue()
MessageBox.showInformation(message, "Maximum Value in Grid")


The Jython example above retrieves a GridContainer object from a DSS record, then uses the container's getGridData() method to access the gridData object it holds. The metadata for the grid is accessed by using the getGridInfo() method on the GridData object. The same GridInfo object can be retrieved directly from the GridContainer object.

Storing a Grid to a DSS File


Grids can be stored to DSS using the put(GridContainer) method of an HecDss object.

Example 3: Storing a GridData Object to a DSS Record

  1. Jython snippet – not a complete script
    outContainer = GridContainer()
    outContainer.fullName = thePathname
    outContainer.gridData = theGrid

    outFile = HecDss.open("out.dss")
    outFile.put(outContainer)


In the Jython snippet above a new GridContainer object is created using the base GridContainer() constructor method. A DSS path (contained in the variable thePathname) and a GridData object are assigned directly to the fullName and gridData fields of the object. With those assignments, the GridData object is complete enough to be stored to a DSS record, using the put(DataContainer) method.
When using HecDss.put(GridContainer) it is very important to assure that the date or dates in the pathname are the same as those in the GridInfo. The put method does not check or update the pathname to assure consistency.