Tuesday, October 28, 2014

Automating HEC-RAS!

Written by Chris Goodell, P.E., D.WRE  |  WEST Consultants
Copyright © The RAS Solution 2014.  All rights reserved.

Are you an HEC-RAS user?  Yes?  OK, try this out:

1.  If you haven’t already done so, install the HEC-RAS example projects to the default location.

image
image

2.  Open up a blank workbook in Excel.

3.  Press Alt-F11.  When the Visual Basic Editor Window opens up, select Tools…References…

4.  Scroll down until you see HEC River Analysis System.  If you see more than one “HEC River Analysis System”, that means you have more than one version of HEC-RAS installed on your computer.  That’s okay, just select the one for the version of HEC-RAS you want to use.  Check the box and click OK.

  image

5.  Click Insert…Module.  Then in the module editor that pops up type (or copy and paste) the following code.  In the line that starts with strRASProject = “C:\Users…, make sure to use your User Name, not cgoodell.  The directory path should match where you’ve installed the HEC-RAS example projects.  Also, only include the last line, HRC.QuitRAS, if you are using HEC-RAS Version 5.0 or higher.  If you are using Version 4.1 or lower, omit this line of code. 

Sub RunRAS()
    'Instantiate a new HECRASController Class
    Dim HRC As New HECRASController

    'Open a new project.   
    Dim strRASProject As String
    strRASProject = "C:\Users\cgoodell\Documents\HEC Data\HEC-RAS\Steady Examples\BEAVCREK.prj"
    HRC.Project_Open strRASProject

    'Run the HEC-RAS Project
    Dim lngMessages As Long
    Dim strMessages() As String
    HRC.Compute_CurrentPlan lngMessages, strMessages()

    'Get water surface elevations at Node 5
    Dim sngWSElev As Single
    sngWSElev = HRC.Output_NodeOutput(1, 1, 5, 0, 1, 2)
    MsgBox "The water surface elevation is " & sngWSElev & "."

    'Close HEC-RAS.  The following line is only needed for HEC-RAS Version 5.0.
    HRC.QuitRas

End Sub

6.  Click Run…Run Sub/UserForm, or just press F5, and watch what happens.  If you get the following error message (or similar), make sure you typed in the HEC-RAS project path and filename correctly and that you actually do have a project located there.  Remember to replace “cgoodell” with your computer User name in the strRASProject definition in the code. 

image
 
If you see HEC-RAS compute and then get the following message, then congratulations…it worked!

image

There.  You’ve just run your first HECRASController application!  Let me know in the Comments below if it worked for you.  There is so much more you can do with the HECRASController.  Automation of HEC-RAS projects, specific output data retrieval, batch mode simulations, real time modeling, Monte Carlo simulations, to name a few.  As you’ve just found out, if you have HEC-RAS, you already have the HECRASController. 

You can learn more about using the HECRASController in my new book, "Breaking the HEC-RAS Code."  Get your copy today!

  image













47 comments:

  1. This is great. Are there libraries to run in .NET as well or only vba?

    ReplyDelete
    Replies
    1. Yes, you can do this in .NET also. I just like to demonstrate it in VBA, since most everyone has Excel. Thanks.

      Delete
  2. Chris, this is great..
    Looking forward to see the book in few weeks.

    ReplyDelete
  3. Worked for me as well. Thanks Chris. Looking forward to the book.

    ReplyDelete
  4. Can I perform these using R-programming or Python....

    ReplyDelete
    Replies
    1. I think so. Give it a try and let me know if it works.

      Delete
    2. Did this ever work for you Anonymous? I'm considering trying both just for the heck of it but I don't want to if you've already found out it can't work...

      Delete
    3. Joshua-I've had some users email me about their attempts at using R for the RASController with some success. If you send me your email address, I can forward it on and maybe you can collaborate directly? Just a thought.

      Delete
    4. Hi Chris, at the moment I am attempting to use R to automate HEC-RAS. It has been difficult to find any documentation or previous experience on this subject. Could you please share with me some information so I can use it as a guide and probably collaborate?

      Delete
  5. Hi Chris, sorry for the late reply. My email is joshua/.cal/eb.ste/ele@/gmail.com (remove the slashes), feel free to forward me onto whoever.

    I'm having almost no trouble getting it working in Excel, but I want to be better versed in R than I am too so it couldn't hurt.

    ReplyDelete
  6. Hi Chris,
    being in a need to autmate HEC-RAS launching I tested your example and it worked well for me - so well that ordered the book. However being in a hurry, let me please ask a question:
    - do you have any Java API/library for the HEC-RAS Controller ?
    - are there any hints in the book how to launch HEC-RAS and collect the results without Excel ?

    Thanks in advance

    Martin

    ReplyDelete
    Replies
    1. Martin-Thanks for getting the book! To answer your questions, the API should work with Java, as long as your Java programming platform can reference the HECRASController. But, I have not tried that. If you get it to work, please let me know. Although the book is written around using the HECRASController in VBA (Excel), it does discuss briefly how to get it set up in Visual Studio. I imagine the procedure is similar in other platforms.

      Good luck. Let me know how it goes!
      Chris

      Delete
  7. Hi Chris,
    I am using the Excel to run my project (1 river, 1 reach, 35 river stations for 24 hours - 13AUG2015 0000 - 14AUG2015 0000).

    However I cannot get the stage output values for the respective hours of simulation:

    - the attempt to get the data from DSS file by the call:
    HRC.OutputDSS_GetStageFlow rivers(1), reaches(1), strRS(6), nValue, dateTimes(), stage(), flow(), errmsg

    results in nValue=25, correct dateTimes however with error message:
    No records were found (the data returned all -902's)
    No records were found (the data returned all -902's)

    However when I run the project in HEC-RAS GUI, I can see in the DSSFile the records with the data:
    /MORAVA DOLNY TOK//LOCATION-FLOW//13AUG2015 0000/FALOSNA/
    ...
    /MORAVA DOLNY TOK//LOCATION-FLOW//13AUG2015 2400/FALOSNA/
    and I can see stage data for all 35 RS for any particular time chosen.

    The calsl like Ouput_NodeOutput are probably of no use since I cannot get the data for all times, just the final value, or am I missing something ?

    Thanks in advance.

    Martin

    ReplyDelete
    Replies
    1. please post your code here so I can see what is going on.

      Delete
  8. Hi Chris,
    I am still trying to automate the HEC-RAS - we need to run it on Windows machine from Linux one via ssh.
    I have the VBS script alunching an Excel file as well as Excel makro doing the HEC-RAS calculations.
    The problem is, that when I run the VBS script by command
    cscript C:\IMS\bin\models\runHECRAS.vbs c:\IMS\bin\models\project2.xls d:\Shares\data-model02\HEC_RAS_Morava
    from Windows Power Shell locally, I can see starting Excel as background process in Task Manager, HEC-RAS starts either as Background process or as App as I can see in Task Manager (anyway very strange behaviour), I can see the HEC-RAS Window open, calculations are performed and everything works as it should.

    When I run the command over ssh:
    ssh root@10.40.152.2 cscript "c:\\IMS\\bin\\models\\runHECRAS.vbs" "c:\\IMS\\bin\\models\\project2.xls" "D:\\Shares\\data-model02\\HEC_RAS_Morava"
    the command starts, I can see starting Excel as background process in Task Manager, HEC-RAS starts as Background process but gets stuck.
    Logs from the Excel makro show, that the call which gets stuck is:

    Dim HRC As New HECRASController

    The macro stops and must be terminated manually, no error can be obtained.

    This may be a sort of security issue - the FreeSshd however runs as service under Local System Account and is allowed to interact with desktop.
    I wonder, if you have any sort of hint how to enable starting HEC-RAS from system service (I found in the book none).
    Any help would be appreciated.

    Martin

    ReplyDelete
    Replies
    1. Martin- That is some advanced stuff. I really don't know anything about ssh and I'm afraid I don't know what is going on with your situation. I will forward this on to someone I know that may be able to help and I'll let you know if I get anything useful for you.
      Chris

      Delete
  9. Chris,
    just more information: running VBS script via ssh / cscript with starting of Excel but without access to HECRASController works well - the Excel starts, the macro produces expected output to log files, Excel finishes.
    The real problem is in HECRASController.
    Is there any debug mode in HEC-RAS which can be activated by me to see when and where it gets stuck ?
    Because being stuck, I cannot access even any Error from VBA in Excel.

    Martin

    ReplyDelete
    Replies
    1. Unfortunately Martin, there is no debug mode for the HECRASController. I'm still waiting to hear back from HEC on your issue. Hopefully they'll have a useful workaround.

      Chris

      Delete
  10. Chris,
    I found the workaround by wrapping cscript command to Windows scheduled task and launching schtasks command via ssh. It worked automatically until some changes in User accounts on Windows have been introduced.

    Now I am facing very strange problem:
    - when launching the HEC-RAS via Excel directly using cscript command, everything is OK and the HEC-RAS produces output files including project.dss:
    ----------+ 1 matog None 32000 2016-01-27 14:24 project.IC.O01
    ----------+ 1 matog None 384000 2016-01-27 14:24 project.O01
    ----------+ 1 matog None 3348 2016-01-27 14:24 project.b01
    ----------+ 1 matog None 2840 2016-01-27 14:24 project.bco01
    ----------+ 1 matog None 27916 2016-01-27 14:24 project.c01
    ----------+ 1 matog None 558592 2016-01-27 14:24 project.dss
    ----------+ 1 matog None 258915 2016-01-27 14:24 project.g01.hdf
    ----------+ 1 matog None 208 2016-01-27 14:24 project.p01.blf
    ----------+ 1 matog None 344203 2016-01-27 14:24 project.p01.hdf
    ----------+ 1 matog None 60190 2016-01-27 14:24 project.r01
    ----------+ 1 matog None 43292 2016-01-27 14:24 project.x01

    In addition, the command HRC.Compute_CurrentPlan lngMessages, strMessages() produces following strMessages:
    Starting Unsteady Computations
    Computing
    Computations Completed

    - however when I launch the HEC-RAS via schtasks command, Excel/HEC-RAS get launched, even the command HRC.Compute_CurrentPlan lngMessages, strMessages() produces the same correct strMessages:
    Starting Unsteady Computations
    Computing
    Computations Completed
    but the model outputs are incomplete, the project.dss is missing:
    ----------+ 1 matog None 3348 2016-01-27 14:46 project.b01
    ----------+ 1 matog None 98748 2016-01-27 14:46 project.g01.hdf
    ----------+ 1 matog None 98748 2016-01-27 14:46 project.g01.tmp.hdf
    ----------+ 1 matog None 9976 2016-01-27 14:46 project.p01.hdf
    ----------+ 1 matog None 43292 2016-01-27 14:46 project.x01

    Do you have any idea where the HEC-RAS may crash after producing correct messages on HRC.Compute_CurrentPlan output but before writing output data ?

    Thanks in advance

    Martin

    ReplyDelete
    Replies
    1. Did you make sure to set all cross sections to produce stage and flow output? From the unsteady flow analysis window, Options...stage and flow output locations.

      Delete
    2. Hi Chris/Martin

      I am sorry to hijack your conversation, but I am also have the same sort of problems Martin is having... But unlike Martin I am sticking purely within the VBA zone using the HECRASController to try to output stages/flows for a user specified Ri/Re/Node.

      My code runs but it only outputs the same first plan/dss file output for each plan i want to output. I have tried to set different dss files for each plan, and even creating different projects for each plan. But the function "RC.OutputDSS_GetStageFlow" will keep outputing the same output...

      Is there a way to manually set the different DSS pathnames so that I may run the "RC.OutputDSS_GetStageFlow" inside a for loop to output multiple plans? or maybe you can tell me if I am missing something in my code which I will paste in a reply to this comment.

      Delete
    3. Sub EXAMPLECODE()
      'Open an HEC-RAS Project
      Dim RC As New RAS500.HECRASController
      OpenRASProjectByRef RC '***Update: removed the "z" before OpenRASProjectByRef
      'Run the Project so there's output to retrieve
      Dim blnDidItWork As Boolean
      Dim blnDidItCompute As Boolean
      Dim lngMsg As Long, strMsg() As String
      'Compute
      'blnDidItWork = RC.Compute_CurrentPlan(lngMsg, strMsg())
      'Get a list of the plans
      Dim blnIncludeBasePlansOnly As Boolean
      Dim lngPlanCount As Long, strPlanNames As String
      blnIncludeBasePlansOnly = True
      'RC.Plan_Names lngPlanCount, strPlanNames(), _
      blnIncludeBasePlansOnly
      'Declare looping ints
      Dim i As Long, j As Long, k As Long, l As Long, m As Long, n As Long, p As Long
      Dim length As Integer, timesteps As Integer
      p = 0
      'Select plan
      Sheets("RASProjects").Select
      Cells.Select
      Range("B6").Select
      lngPlanCount = ActiveCell.Value
      'start loop for plans
      For m = 1 To lngPlanCount
      Sheets("RASProjects").Select
      Cells.Select
      Range("B7").Select
      ActiveCell.Offset(m, 0).Activate
      strPlanNames = ActiveCell.Value
      ActiveCell.Offset(0, 1).Activate: ActiveCell.Value = "COMPLETE"
      'Set the current output plan to next iteration
      Dim blnSetIt As Boolean, blnOutputFile As Boolean
      Dim strPlanErrMsg As String
      'blnOutputFile = RC.PlanOutput_IsCurrent(strPlanNames, True, strPlanErrMsg)
      blnSetIt = RC.PlanOutput_SetCurrent(strPlanNames)
      Sheets("RASProjects").Select
      Cells.Select
      Range("M2").Select
      length = ActiveCell.Value
      ActiveCell.Offset(0, 2).Activate
      timesteps = ActiveCell.Value
      'Populate a RAS Geometry Type using the GetRiverReachesNodes Function
      Dim typRASGeometry As TypeRASGeom
      blnDidItWork = GetRiversReachesNodes(typRASGeometry, RC)
      'Write results to the excel sheet
      Sheets.Add
      ActiveSheet.Name = strPlanNames
      Sheets(strPlanNames).Select
      Cells.Select
      Selection.ClearContents
      Range("A1").Select
      'Rivers
      For n = 1 To length
      'get first row of selected river/reach/node ids
      Sheets("RASProjects").Select
      Cells.Select
      Range("N3").Select
      ActiveCell.Offset(n, 0).Activate
      i = ActiveCell.Value
      ActiveCell.Offset(0, 2).Activate
      j = ActiveCell.Value
      ActiveCell.Offset(0, 2).Activate
      k = ActiveCell.Value
      Application.StatusBar = "Working " & "on River " & i & ", Reach " & j & ", RS " & _
      Trim(typRASGeometry.Riv(i).Rch(j).Node(k).RiverStation) & "for plan" & m & " ."
      'Go back to output current sheet
      Sheets(strPlanNames).Select
      Cells.Select
      Range("A2").Select
      'Reaches
      With typRASGeometry.Riv(i)
      'write each river/reach
      ActiveCell.Offset(0, (n - 1) * 3).Activate: ActiveCell.Value = _
      .RivName
      ActiveCell.Offset(0, 1).Activate: ActiveCell.Value = _
      .Rch(j).RchName

      BREAK (cont'd next reply)

      Delete
    4. (2/2 Cont'd from above comment)

      'Nodes
      With .Rch(j)
      'write each node/RS
      ActiveCell.Offset(0, 1).Activate: ActiveCell.Value = _
      .Node(k).RiverStation
      'Write column headers
      ActiveCell.Offset(2, -2).Activate: ActiveCell.Value = _
      "Time"
      ActiveCell.Offset(0, 1).Activate: ActiveCell.Value = _
      "Stage (ft)"
      ActiveCell.Offset(0, 1).Activate: ActiveCell.Value = _
      "Flow (cfs)"
      ActiveCell.Offset(0, -2).Activate
      'Declare names of each river, reach and node for each iteration
      Dim strRiver As String, strReach As String, strRS As String
      strRiver = RC.Geometry.RiverName(i)
      strReach = RC.Geometry.ReachName(i, j)
      strRS = RC.Geometry.NodeRS(i, j, k)
      'Retrieve the output from the dss file
      Dim lngNumValue As Long, dblDateTime() As Double, _
      sngStage() As Single, sngFlow() As Single, _
      strErrMsg As String
      blnDidItCompute = RC.OutputDSS_GetStageFlow(strRiver, _
      strReach, strRS, lngNumValue, dblDateTime(), _
      sngStage(), sngFlow(), strErrMsg)
      'Write time/stage/flow
      For l = 1 To lngNumValue
      ActiveCell.Offset(1, 0).Activate: ActiveCell.Value = _
      dblDateTime(l) 'make sure to set this column number format to "time"
      ActiveCell.Offset(0, 1).Activate: ActiveCell.Value = _
      Round(CStr(sngStage(l + p)), 2)
      ActiveCell.Offset(0, 1).Activate: ActiveCell.Value = _
      Round(CStr(sngFlow(l)), 0)
      ActiveCell.Offset(0, -2).Activate
      Next l
      End With
      End With
      Next n
      Next m
      'Close HEC-RAS
      RC.QuitRAS
      End Sub

      Delete
    5. Logan-

      NOt sure if this is what is causing the problem or not, but the strPlanNames() is an array of plan names and requires an index number when using it to set the current plan. So, when you set it in the command RC.PlanOutput_SetCurrent(), the argument should be an indexed form of strPlanNames(), like strPlanNames(1). Also, I noticed that some important pieces of your code are commented out with an '. I'm guessing that you did that while trying to debug, but thought I'd mention it just in case. Hope this helps.
      Chris

      Delete
  11. Chris,
    thank you very much for your quick response. The problem has been solved - due to the security reasons / OS hardening the model did not write .dss file (and some other files) when running from scheduled task. So my problem is solved.

    However it might be worth to change the return value of the call "HRC.Compute_CurrentPlan lngMessages, strMessages()" because in both cases (success, failure) the output was:
    Starting Unsteady Computations
    Computing
    Computations Completed

    which was true, however there was no indication that the writing of the output data failed.
    Reporting of the output writing failure would help me (and maybe someone else in future) in integration/automation.

    Best regards

    Martin

    ReplyDelete
  12. Thanks for the fast reply Chris, I will try this now! Will respond in a couple hours.

    And oops sorry about the commented lines, I lose track sometimes of what version I am working on :P.

    Cheers,

    ReplyDelete
  13. Chris,

    You were right, I assumed from the start I could just pass the function a string, but it turns out it has to have that index number. So instead of grabbing strings from the excel spreadsheet, I am now using the plan_names function to extract all plans in the project.

    Thanks,
    logan

    ReplyDelete
  14. Nayyar Minhaj AsifApril 24, 2016 at 2:25 AM

    Hello Chris

    I recently purchased your book and i found it really helpful in automating HEC ras for my modeling problem.

    So far, i have no issues because the book is very well explained. Just a very small query, there is no QUITRAS command available in the Controller. I am using HEC RAS 4.1 and have looked through the library but this command does not exsit nor does it works.
    Well, i have figured another way round to address the problem but i believe that this command can be very helpful.
    Looking forward to your reply. Take care.

    Regards
    Nayyar Minhaj ASIF

    ReplyDelete
    Replies
    1. Hi Nayyar. Thanks for buying the book. Glad you find it useful. The QuitRAS procedure is new for version 5.0. Since you are using Version 4.1, it won't be there. Sounds like you found a work around, so that's good.

      Delete
  15. Nayyar Minhaj ASIFJune 28, 2016 at 8:52 AM

    Hello Chris

    I would appreciate your help over the following issue.

    I am trying to extract flow hydrograph for a river station. I am using the OUTOUTDSS_GETSTAGE FLOW command for that. It works fine, however, whatif i want a flow hydrograph through a gated lateral structure? Even if i mention "Lateral Structure RS" in the "FLOW OUTPUT LOCATIONS", it does not give me the gated flow. It gives me the flow at that river station in the river but i would like to have the flow through the gated structure (lateral). How can i handle this?

    Currently, i have connected a temporary storage area with my lateral structure to actually extract the hydrograph using the DSS_GETSTAGEFLOWSA but that doesn't seems neat. Any suggestions?

    Thanking in advance.
    Regards
    Nayyar

    ReplyDelete
  16. Nayyar Minhaj ASIFJune 28, 2016 at 9:11 AM

    Forgot to mention.

    I cannot simply extract discharge of a lateral structure for a fix profile number. The reason is quite weird actually.

    I am using rule based BC's for the operations. And there seems a glitch in the HEC RAS output table. The discharge passing through the gates (lateral structure) in the SUMMARY OUTPUT TABLE BY PROFILE is not the same as shown in FLOW HYDROGRAPHS. The values shown in FLOW HYDROGRAPH are correct as those are the values that are writed in the text file and also present in the DSS, however, values shown in SUMMARY OUTPUT TABLE are incorrect. Using the "Discharge extraction by profile number" will give me the wrong value.

    As an example,

    ReplyDelete
  17. Nayyar Minhaj ASIFAugust 29, 2016 at 1:51 AM

    Hello Chris. I have a question regarding the OUTPUTDSS_GETSTAGEFLOW.

    Does this command works for extracting hydrographs of the Lateral Structures i.e. flow flowing through gates??

    I know i can extract gate flows through the profiles but that would be requiring post processing which i am looking to avoid. Is there a way to interact with the DSS for extracting hydrographs for lateral structures?

    Regards
    Nayyar

    ReplyDelete
  18. Hi Chris,
    Would like to have few clarification to atomize HecRAS setup..
    1. I do have sdf file (300mb)generated from ArcGIS for geometry input. How can I import sdf file without using HecRAS GUI. Since it is taking too much time to load in HecRAS.
    2. How can I setup a new HecRAS project without using GUI and import all the relevant file in it?

    ReplyDelete
    Replies
    1. 1. You would have to learn the structure of the sdf file, then write code to write a .geo file based on the data in the sdf file. "Breaking the HEC-RAS Code" can help you with the structure of the geometry input file, but this would not be a trivial task.

      2. See my answer to question 1.

      Delete
  19. Hi, Chris,

    I tested your code above with a replaced path for the Beaver Creek example in HEC-RAS 5.0.3. I used the RAS500 DLL. I got an error message in Microsoft Visual Basic "Run-time error'429':ActiveX component can't create object". What did I miss? Thanks for your time.

    Ken Ying

    ReplyDelete
    Replies
    1. You should use the RAS503 DLL if you are using HEC-RAS 5.0.3.

      Delete
    2. Chris,

      I got the 4.1 version working. But 5.03 just didn't work. It seems that the function name HECRASController is different in 5.03. Any idea? Thanks.

      Delete
    3. Chris,

      I tried the RAS503.HECRASController and it gave me the error message: "Compile error: User-defined type not defined".

      Delete
    4. What line of code returned that error message?

      Delete
    5. I received the same message in relation to the line;
      Dim RC As New RAS41.HECRASController

      Delete
    6. Alex, have you added RAS41 as a reference to your code (steps 3 and 4 above)? In any case, I suggest using RAS503 instead. Just make sure you have RAS Version 5.0.3 installed on your computer first.

      Delete
  20. Hi Chris, we are trying to automate the process in producing flood map and I just want to ask if this is achievable?

    Thanks.

    ReplyDelete
    Replies
    1. Yes, it's possible, but you'll have to learn how to read from an hdf5 file.

      Delete
  21. Is there API calls in the controller to create DSS file and add timeseries data to it?

    ReplyDelete
    Replies
    1. Not to write to the DSS file. You can read from it though. You might explore DSSVue and the DSSVue plugins from the HEC website.

      Delete