How to import the Scenarios from InfoSewer or InfoSWMM to ICM SWMM or ICM InfoWorks using Ruby
InfoSWMM Scenarios Imported to ICM InfoWorks or ICM SWMM

How to import the Scenarios from InfoSewer or InfoSWMM to ICM SWMM or ICM InfoWorks using Ruby

Introduction. Why do we have to do this?

The direct import of InfoSWMM to ICM SWMM only imports one scenario at a time. If we want to import all of the scenarios, we end up with a lot of networks and ancillary data for those networks. Duplicates of rainfall, inflow and patterns are examples. The Ruby code (it is the same for both SWMM and InfoWorks networks) is called Step25_User_123_ICM_Scenario_csv.rb and is in the subfolder 0100 - ODIC and SQL, Ruby Scripts for Importing InfoSewer to ICM under 02 SWMM.

Step 1. Import the Base InfoSWMM or InfoSewer Network
Only the Base Scenario has been imported
Step 2. Import the file SCENARIO.CSV - it is the same name in InfoSWMM and InfoSewer

You must first convert scenario.dbf to scenario.csv

Use a folder picker and select either the ISDB or IEDB Folder
Step 3. A successful import shows you a log file

A successful import shows you a log file that you can copy to the scenario notes.

A successful import shows you a log file that you can copy to the scenario notes.
Folder path: C:\Training\Greenville
Scenario CSV: C:\Training\Greenville/scenario.csv
Finished Import of InfoSewer Scenario Table to ICM InfoWorks
All scenarios deleted
Adding scenario 1DONLY
Adding scenario 2DWLID
Adding scenario RDIIANALYST
Adding scenario SUSTAINONLY
Adding scenario SFEM
Adding scenario CALIBRATOR
Adding scenario 1DW2D
Adding scenario SUBMANAGER
Adding scenario DESIGNER
Adding scenario ELEVATIONS
Adding scenario DWF
Adding scenario 10-YEAR_STORM
Adding scenario NONSTEADY
Adding scenario MESHRIVERS
Adding scenario 1_0
Adding scenario 0_5
Adding scenario 2_0
Adding scenario 0_01
Adding scenario 0_05
Adding scenario 0_10
Adding scenario 0_25
Adding scenario NOROUTING
Adding scenario LID_USAGE
Total scenarios added: 23        
Section 4. The Ruby Code
require 'csv'
require 'pathname'

def import_csv(open_net)
  # Prompt the user to pick a folder
  val = WSApplication.prompt "Folder for an InfoSewer or InfoSWMM Scenario", [
    ['Pick the Scenario','String',nil,nil,'FOLDER','IEDB or ISDB Folder']], false
  folder_path = val[0]
  puts "Folder path: #{folder_path}"

  # Check if folder path is given
  return unless folder_path

  # Initialize an empty array to hold the hashes
  rows = []

  scenario_csv = "#{folder_path}/scenario.csv"
  puts "Scenario CSV: #{scenario_csv}"

  # Headers to exclude
  exclude_headers = ["USE_TIME", "TIME_SET", "USE_REPORT", "REPORT_SET", "USE_OPTION", "OPTION_SET","PISLT_SET"]

  # Read the CSV file
  CSV.open(scenario_csv, 'r', headers: true) do |csv|

  # Process the rows
  csv.each do |row|
    row_string = ""
    row.headers.each do |header|
      unless row[header].nil? || exclude_headers.include?(header)
        row_string += sprintf("%-15s: %s, ", header, row[header])
      end
    end
    puts row_string

      # Add the row to the array as a hash
      rows << row.to_h
    end
  end

  rows
end

# Access the current open network in the application
open_net = WSApplication.current_network

# Call the import_anode method
rows = import_csv(open_net)

# Indicate the completion of the import process
puts "Finished Import of InfoSewer Scenario Table to ICM InfoWorks"

added_scenarios_count = 0

open_net.scenarios do |scenario|
 if scenario != 'Base'
  open_net.delete_scenario(scenario)
 end
end

puts "All scenarios deleted"
rows.each do |scenario|
 if scenario['ID'] != 'BASE'
   puts "Adding scenario #{scenario['ID']}"
   open_net.add_scenario(scenario['ID'],nil,'')
   added_scenarios_count += 1
 end
end
puts "Total scenarios added: #{added_scenarios_count}"        
Section 5 The Readme File
# Summary of Step25_User_123_ICM_Scenario_csv.rb

This script imports a CSV file containing scenarios for an InfoSewer or InfoSWMM network, deletes all existing scenarios in the current network (except the 'Base' scenario), and adds the scenarios from the CSV file to the network.

## Steps

1. **Prompt for folder**: The script prompts the user to select a folder containing the CSV file.

2. **Read CSV file**: The script reads the CSV file named 'scenario.csv' in the selected folder. It excludes certain headers and stores each row as a hash in an array.

3. **Delete existing scenarios**: The script deletes all scenarios in the current network, except for the 'Base' scenario.

4. **Add new scenarios**: The script iterates over the array of hashes (each representing a row in the CSV file). For each hash, it checks if the 'ID' value is not 'BASE'. If it's not, it adds a new scenario to the network with the 'ID' value as the scenario name.

5. **Print results**: The script prints the total number of scenarios added to the network.

This script is useful for updating the scenarios in an InfoSewer or InfoSWMM network based on a CSV file. It allows for easy bulk addition of scenarios.        

Closing Note: Thank you so much for journeying with me through this content. This space is reserved for future updates and insights. Your engagement and time are truly appreciated. Until next time! You can also see my past articles on LinkedIn (91 in 2023) .

要查看或添加评论,请登录

Robert Dickinson的更多文章

社区洞察

其他会员也浏览了