Wednesday, March 4, 2015

Accessing HEC-DSS files from 'C' Python

3 August 2016: I've had a lot of people ask about this. I was able to access the DSSVue Java libraries through this method but it's a real pain to convert between java types and data usable in Python. I've found it is much easier to export through another means, such as a jython script in DSS to some intermediate data format, or use dssrip in R. I leave this up here in case someone truly needs it, but there are probably faster ways to access your data.

HEC-DSSVue is scriptable in the Jython language, but unfortunately isn't compatible with the popular scientific computing and plotting libraries like numpy, scipy, and matplotlib.

I've been searching for a solution to this for a while, eventually settling on using R's rJava library to write my dssrip library for R.  Recently I experimented with using the Py4j library to call Java code from the 'C' Python environment.  Using the Py4j library to talk to Java isn't quite as clean of a solution as rJava or Jython allows.  Introspection of Java objects isn't as easy, requiring the use of Java's introspection methods instead of Java objects showing up like native objects.   The biggest problem however is that it requires the JVM to open a gateway server to connect to, which may cause problems on strictly firewalled systems.  Py4j's .jar file should be dropped in the HEC-DSSVue plugins directory.  I tested these with Anaconda Python on a 64-bit Windows 7 machine, where Py4j was installed with the pip command.

The server script should be launched as a script from within the DSSVue GUI, or executed with a version of Jython linked to the DSSVue .jar files and javaHeclib.dll library.  The client script, is an example with a couple of methods to open a DSS file and read time series data into a Pandas TimeSeries object.

If you want to take advantage of the scientific Python ecosystem with your hydrology data, this is a proof of concept that you may be able to work directly with DSS files in Python.