## Veneer started elsewhere (probably from a command line using veneer.manager.start)
ports = list(range(15004,15008))
ports

bv = BulkVeneer(ports)

v = bv.veneers[1]

network = v.network()
network.as_dataframe().plot()

network.outlet_nodes()

outlet_node = network.outlet_nodes()[0]['properties']['name'] + '$'

"""
Explanation: Connect to Source and test connection
Using 4 instances
End of explanation
"""


import numpy as np

N_RUNS=100

params = {
    'x1':np.random.uniform(1.0,1500.0,size=N_RUNS),
    'x2':np.random.uniform(1.0,5.0,size=N_RUNS),
    'x3':np.random.uniform(1.0,200.0,size=N_RUNS),
    'x4':np.random.uniform(0.5,3.0,size=N_RUNS)
}
params = pd.DataFrame(params)
params

"""
Explanation: Enumerate the parameter combinations
End of explanation
"""


runner = BatchRunner(bv.veneers)

v.model.catchment.runoff.set_param_values?

for p in ['x1','x2','x3','x4']:
    runner.parameters.model.catchment.runoff.set_param_values(p,'$%s$'%p,fus=['Grazing'])

"""
Explanation: Specify the model changes
(Much like we do when setting up a PEST job)
End of explanation
"""


runner.retrieve('y').retrieve_multiple_time_series(criteria={'NetworkElement':outlet_node,'RecordingVariable':'Downstream Flow Volume'}).sum()[0]

%xmode Verbose
print(runner._retrieval.script())

"""
Explanation: Specify the result 'y' that we want to retrieve
End of explanation
"""


jobs,results = runner.run(params)

#jobs

"""
Explanation: Trigger the run...
Will run the 100 simulations across the four instances of Source (25 runs each)
End of explanation
"""


pd.DataFrame(results)

"""
Explanation: The results
End of explanation
"""


