-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRRSumTotalRunoff.cs
87 lines (79 loc) · 2.68 KB
/
RRSumTotalRunoff.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Globalization;
using System.IO;
using DHI.Mike1D.Generic;
using DHI.Mike1D.Mike1DDataAccess;
using DHI.Mike1D.Plugins;
using DHI.Mike1D.RainfallRunoffModule;
namespace DHI.Mike1D.Examples.Scripts
{
/// <summary>
/// Script class that makes a catchment that sums Total Runoff
/// for all catchments in the setup
/// </summary>
public class RRSumTotalRunoff
{
CatchmentCombined sumTotalRunoffCatchment;
Func<double> runoffGetter;
StreamWriter writer;
/// <summary>
/// Create Combined catchments containing all other catchments
/// Make sure to close <see cref="writer"/> on simulation end
/// </summary>
[Script]
public void CreateSumAllCatchment(IMike1DController controller)
{
Mike1DData mike1DData = controller.Mike1DData;
sumTotalRunoffCatchment = new CatchmentCombined("SumAllCatchments")
{
ScaleByArea = false,
Area = 1,
};
double minTimestep = double.MaxValue;
double maxTimestep = double.MinValue;
foreach (ICatchment catchment in mike1DData.RainfallRunoffData.Catchments)
{
if (!(catchment is CatchmentCombined))
{
sumTotalRunoffCatchment.AddNewCatchment(catchment.ModelId, 1.0);
minTimestep = System.Math.Min(minTimestep, catchment.TimeStep.TotalSeconds);
maxTimestep = System.Math.Max(maxTimestep, catchment.TimeStep.TotalSeconds);
}
}
sumTotalRunoffCatchment.TimeStep = TimeSpan.FromSeconds(minTimestep);
mike1DData.RainfallRunoffData.Catchments.Add(sumTotalRunoffCatchment);
// Setup writer to write total runoff to csv file
writer = new StreamWriter("SumTotalRunoff.csv");
writer.WriteLine("sep=;");
sumTotalRunoffCatchment.PostTimeStepEvent +=
delegate (DateTime time)
{
writer.WriteLine("{0};{1}",
time.ToString(Util.DateTimeFormatString),
runoffGetter().ToString(CultureInfo.InvariantCulture));
};
controller.ControllerEvent += HandleControllerEvent;
}
/// <summary>
/// Make sure to close <see cref="writer"/> on simulation end
/// </summary>
private void HandleControllerEvent(object sender, ControllerEventArgs e)
{
switch (e.State)
{
case ControllerState.Prepared:
// Getter is available when Prepared
runoffGetter = sumTotalRunoffCatchment.Getter(Quantity.Create(PredefinedQuantity.TotalRunOff));
break;
case ControllerState.Finalized:
case ControllerState.Failed:
if (writer != null)
{
writer.Close();
writer = null;
}
break;
}
}
}
}