# Run from ICM Exchange
# Call from command prompt of form: 
# InstallPath\IExchange "ThisScriptPath\syntheticrainfall.rb" /ADSK "RootPath" 
#
#cd /d D:\Source\Repos\global_bluezone\DropBear\output\Debug.x64
#
#D:\Source\Repos\global_bluezone\DropBear\output\Debug.x64>iexchange "D:\Data\SWMM Run Ruby\syntheticrainfall.rb" /ADSK "D:\Data"

require  'fileutils'
require	 'date'

class SyntheticRainfall
	def initialize(root)
		@root=root # store the root e.g. e:\testdata (everything else is appended to this when appropriate)
	end
	
	def CreateRainfall()
		# make sure all the working folders exist
		FileUtils.cd(@root) do
			if !File.exist?("working")
				FileUtils.mkdir("working")
			end
			FileUtils.cd("working") do
				if !File.exist?("w")
					FileUtils.mkdir("w")
				end		
			end
			FileUtils.cd("working") do
				if !File.exist?("r")
					FileUtils.mkdir("r")
				end		
			end
		end
			
		WSApplication.set_working_folder("#{@root}\\working\\w")
		WSApplication.set_results_folder("#{@root}\\working\\r")
		
		if !File.exists? @root+"\\rainfalldb.icmm"
			WSApplication.create(@root+"\\rainfalldb.icmm")
		end
		
		# open master database and model group
		masterDB = WSApplication.open(@root+"\\rainfalldb.icmm", false)
		
		# Find Model Group 'SynthRain'
		moModelGroup=masterDB.find_root_model_object("Model Group","SynthRain")
		
		if moModelGroup == nil
			moModelGroup=masterDB.new_model_object("Model Group","SynthRain")
		end
		

		hash = Hash.new
			hash["UCWI"] = 1
			hash["Antecedentdepth"] = 2
			hash["Evaporation"] = 3
			hash["WetnessIndex"] = 3
			hash["API30"] = 5
			hash["SMS"] = 6
			hash["SMD"] = 7
			hash["Cini"] = 8
			hash["BF0"] = 9
			hash["InitialDeficit"] = 10
			hash["ConstantLoss"] = 11
			hash["Profile"] = 2
			hash["ReturnPeriod"] = 10
			hash["Duration"] = 30
			hash["Location"] = 1
			hash["5yr1hr"] = 13
			hash["RainfallRatio"] = 1
			hash["CatchmentArea"] = 100
			hash["Series"] = 1
			
		# Find Rainfall 'UK Rain'
		sName = "UK Rain"
		moRainfall=moModelGroup.find_child_model_object("Rainfall Event",sName)
					
		while moRainfall != nil
			sName += "!"
			moRainfall=moModelGroup.find_child_model_object("Rainfall Event",sName)
		end
			
		moModelGroup.new_synthetic_rainfall(sName, 'UKRain', hash)
	end
end

r=SyntheticRainfall.new(ARGV[1])
r.CreateRainfall()