Store CSV Data on HEC DSSVUE DSS for HEC HMS Model Automation

Store CSV Data on HEC DSSVUE DSS for HEC HMS Model Automation


Recently I have encountered with a problem of automating the HEC-HMS model running with real-time weather data. If you are familiar with Hydrologic Modeling System HEC-HMS, you can design the model by running the program in UI mode. When setting the Data Source as Manual Entry HEC-HMS 3.5 Data source set to Manual Entry  as shown in figure 1, users can enter the value using the inbuilt Table tool as shown in figure 2.
Figure 1: HEC-HMS 3.5 Data source set to Manual Entry
Figure 2: HEC-HMS 3.5 Insert Timeseries using Table tool
Before you are going to update the HEC-HMS data without Table tool, first you have to change the Data Source into Data Storage System (HEC-DSS) as shown in the figure 3.
Figure 3: HEC-HMS 3.5 Set Data Source into Data Storage System (HEC-DSS)
According to your HEC-HMS model setup, the .gage files content will change as shown below;
Gage Manager: 2008_2_Events
Version: 3.5
Filepath Separator: /
End:

Gage: Awissawella
Last Modified Date: 21 March 2017
Last Modified Time: 10:38:58
Units System: SI
Reference Height Units: Meters
Reference Height: 10.0
Gage Type: Precipitation
Precipitation Type: Incremental
Units: MM
Data Type: PER-CUM
Local to Project: NO
Start Time: 1 April 2008, 00:00
End Time: 30 July 2008, 00:00
DSS File: KelaniBasin_force.dss
Pathname: //AWISSAWELLA/PRECIP-INC//1DAY/GAGE/
End:

Follow the HEC-HMS_Users_Manual_3.5.pdf under Chapter 2 - Command Line Operation, in order to run the HEC-HMS model without application interface. Next what we have to do is, update the .dss file with input weather data (in my case .csv Daily Rainfall data) before running the HEC-HMS model. HEC-DSS provide some utility lib to handle .dss files.
  • But those are not available for Linux platforms (only for Windows 32bit and Solaris at the moment - http://www.hec.usace.army.mil/software/hec-dss/utilities/downloads.aspx)
  • In order to run on Windows, need a 32 bit version of Windows Operating System as well
Then found a way that handling .dss files with HEC-DSSVUE programs scripts . We can create a Jython script, and use the HEC-DSSVUE programs hec .jar files to get the work done.

But there is a minor issue of using HEC-DSSVUE Jython library. Default HEC-DSSVUE support Jython 2.2.1, and in order to read .csv files, we need at least Jython 2.3 or greater according to the Jython CSV documentation.

Compatible Jython Version

In Jython downloads, after the Jython 2.2.1 release, there is 2.5.x and 2.7.x versions are available. Since HEC-DSSVUE is using JDK1.6, itll be better to use a Jython version which is compatible with JDK1.6.
Thus I did couple of experiments with installing Jython. Jython 2.7 (latest release) is not compatible with JDK1.6. But I found that Jython 2.5 version is compatible with JDK1.6. Then I installed the Jython with JDK1.6 (you can use the JDK1.6 which is located inside HEC-DSSVUE installation directory) using below command;
<PATH/TO/JDK1.6>/bin/java -jar jython_installer-2.5.0.jar
After successfully installing Jython 2.5, then build jars as mentioned below (refer to Building jars - some samples);
$ cd $JYTHON_HOME
$ cp jython.jar jythonlib.jar
$ zip -r jythonlib.jar Lib
Then replaced <HEC-DSSVUE_HOME>/jar/sys/jythonlib.jar with above created jythonlib.jar file. Then run the Jython script with;
$ cd <HEC-DSSVUE_HOME>
$ ./hec-dssvue.sh ../CSVTODSS.py

Sample Codes

CSVTODSS.py
from hec.script import MessageBox
from hec.heclib.dss import HecDss
from hec.heclib.util import HecTime
from hec.io import TimeSeriesContainer
import java
import csv

try :
try :
#print Jython version: , sys.version
NUM_METADATA_LINES = 3;
DSS_FILE_PATH = ./2008_2_Events/2008_2_Events_force.dss
CSV_FILE_PATH = DailyRain.csv

myDss = HecDss.open(DSS_FILE_PATH)
csvReader = csv.reader(open(CSV_FILE_PATH, r), delimiter=,, quotechar=|)
csvList = list(csvReader)

numLocations = len(csvList[0]) - 1
numValues = len(csvList) - NUM_METADATA_LINES # Ignore Metadata
locationIds = csvList[1][1:]
print Start reading, numLocations, csvList[0][0], :, , .join(csvList[0][1:])
print Period of , numValues, values
print Location Ids :, locationIds

for i in range(0, numLocations):
print >>>>>>> Start processing , locationIds[i], <<<<<<<<<<<<
precipitations = []
for j in range(NUM_METADATA_LINES, numValues + NUM_METADATA_LINES):
p = float(csvList[j][i+1])
precipitations.append(p)

print Precipitation of , locationIds[i], precipitations[:10]
tsc = TimeSeriesContainer()
# tsc.fullName = "/BASIN/LOC/FLOW//1HOUR/OBS/"
tsc.fullName = // + locationIds[i].upper() + /PRECIP-INC//1DAY/GAGE/

print Start time : , csvList[NUM_METADATA_LINES][0]
start = HecTime(csvList[NUM_METADATA_LINES][0])
tsc.interval = 24 * 60
times = []
for value in precipitations :
times.append(start.value())
start.add(tsc.interval)
tsc.times = times
tsc.values = precipitations
tsc.numberValues = len(precipitations)
tsc.units = "MM"
tsc.type = "PER-CUM"
myDss.put(tsc)

except Exception, e :
MessageBox.showError( .join(e.args), "Python Error")
except java.lang.Exception, e :
MessageBox.showError(e.getMessage(), "Error")
finally :
myDss.done()
print Completed converting , CSV_FILE_PATH, to , DSS_FILE_PATH
DailyRain.csv
Location Names, Colombo Met Station, Awissawella
Location Ids,Colombo,Awissawella
Time,Rainfall,Rainfall
2008-04-01 00:00:00,0,0
2008-04-02 00:00:00,35.2,35.2
2008-04-03 00:00:00,22.36,22.36
2008-04-04 00:00:00,14.3,14.3
2008-04-05 00:00:00,12.92,12.92
2008-04-06 00:00:00,11.14,11.14
2008-04-07 00:00:00,18.8,18.8
2008-04-08 00:00:00,44.98,44.98
2008-04-09 00:00:00,25.82,25.82
2008-04-10 00:00:00,15.56,15.56
...

download file now

Comments

Popular posts from this blog

Sony Xperia T2 Ultra dual

Sorry Mutter closed unexpectedly on Ubuntu 10 10

Sol Trigger ENGLISH PATCH V1 ISO Free Download