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.

9 comments:

  1. Hi there,
    Firstly I’d like to thank you for this article. It has helped me a lot and I’m going to quote your words in my thesis! Currently I’m working on flood modelling automation between HEC’s programs and I wanted to ask you how I can execute the server script with a version of Jython linked to the DSSVue?
    When I want to run any batch file from DSSVue I have this error:
    OSError: (0, 'Failed to execute command ([\'sh\', \'-c\', \'batch.bat\']): java.io.IOException: Cannot run program "sh": CreateProcess error=2, Nie mo\xBFna odnale\x9F\xE6 okre\x9Clonego pl')
    Any ideas how can I fix it?
    Thank you!

    ReplyDelete
  2. Dorota,

    I haven't run DSSVue in batch mode before, but it looks like it might be trying to execute a UNIX shell command.

    I primarily tested the server script as one that can be launched from the DSSVue GUI, where you can add it using the "tools->script editor" menu option. If you install the Jython interactive interpreter, and link it to the DSSVue .jar files, you can also launch the server using the exec_file function. I don't have documentation for linking Jython to DSSVue, but the O'Reilly Jython Essentials book should if you can't find it online.

    I've also heard good success linking a Jython interpreter to DSSVue through the Eclipse GUI:
    https://geotechrichard.files.wordpress.com/2013/01/configureeclipse_hecdssvue_jythondevelopment.pdf

    ReplyDelete
    Replies
    1. As a correction, it's the 'execfile(filename)' function, not exec_file.

      Delete
  3. Hi Evan,
    1. I was trying to install 'dssrip' on 32 bit R as per the installation instructions. I am getting error as follows
    Error : .onLoad failed in loadNamespace() for 'dssrip', details:
    call: .jnew("hec/io/TimeSeriesContainer")
    error: java.lang.ClassNotFoundException
    Error: loading failed
    Execution halted
    ERROR: loading failed
    * removing 'C:/Program Files/R/R-3.3.1/library/dssrip'

    Is it the hec dlls are not getting called correctly?

    2. As an alternative I thought of reading DSS files in python. I installed Py4j using pip. Then onwards not sure how to proceed.

    Any comments on these would be great help

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. ATK,

      It looks like dssrip isn't finding DSSVue on your computer correctly? Did you install the default version available on the HEC website? You may need to manually set the options before trying to install or load dssrip, the locations being dependent on where DSS is installed on your computer:

      ```
      options(dss_jre_location="C:\\Program Files (x86)\\HEC\\HEC-DSSVue")
      options(dss_jre_location="C:\\Program Files (x86)\\HEC\\HEC-DSSVue\\java")
      Sys.setenv(JAVA_HOME="C:\\Program Files (x86)\\HEC\\HEC-DSSVue\\java")
      ```

      Delete
    3. Thanks!! I was able to open DSS file using http://eaheisman.blogspot.in/2013/04/hec-dss-files-and-r-and-python.html. Thats great.
      One more thing - I was wondering there are host of other functions like dssFile$saveAs() etc... where could one get documentation on its correct syntax and usage.
      Thanks

      Delete
    4. Chapter 8 of the DSSVue manual is on scripting in Jython. It details the API fairly well, although there are a few typos that I have found. The examples are all in Jython, but the API is the same. For example, dssFile.get(pathname) in Jython can be written in R as dssFile$get(pathname)

      Delete

Note: Only a member of this blog may post a comment.